1 /*- 2 * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 #ifndef RSA_H_ 26 #define RSA_H_ 20120325 27 28 #include "bn.h" 29 30 #ifndef __BEGIN_DECLS 31 # if defined(__cplusplus) 32 # define __BEGIN_DECLS extern "C" { 33 # define __END_DECLS } 34 # else 35 # define __BEGIN_DECLS 36 # define __END_DECLS 37 # endif 38 #endif 39 40 __BEGIN_DECLS 41 42 typedef struct rsa_pubkey_t { 43 BIGNUM *n; /* RSA public modulus n */ 44 BIGNUM *e; /* RSA public encryption exponent e */ 45 } rsa_pubkey_t; 46 47 typedef struct mpi_rsa_t { 48 int f1; /* openssl pad */ 49 long f2; /* openssl version */ 50 const void *f3; /* openssl method */ 51 void *f4; /* openssl engine */ 52 BIGNUM *n; 53 BIGNUM *e; 54 BIGNUM *d; 55 BIGNUM *p; 56 BIGNUM *q; 57 BIGNUM *dmp1; 58 BIGNUM *dmq1; 59 BIGNUM *iqmp; 60 } mpi_rsa_t; 61 62 #define RSA mpi_rsa_t 63 64 typedef struct dsa_pubkey_t { 65 BIGNUM *p; /* DSA public modulus n */ 66 BIGNUM *q; /* DSA public encryption exponent e */ 67 BIGNUM *g; 68 BIGNUM *y; 69 } dsa_pubkey_t; 70 71 typedef struct mpi_dsa_t { 72 BIGNUM *p; 73 BIGNUM *q; 74 BIGNUM *g; 75 BIGNUM *y; 76 BIGNUM *x; 77 BIGNUM *pub_key; 78 BIGNUM *priv_key; 79 } mpi_dsa_t; 80 81 #define DSA mpi_dsa_t 82 83 typedef struct rsasig_t { 84 BIGNUM *sig; /* mpi which is actual signature */ 85 } rsasig_t; 86 87 typedef struct dsasig_t { 88 BIGNUM *r; /* mpi which is actual signature */ 89 BIGNUM *s; /* mpi which is actual signature */ 90 } dsasig_t; 91 92 #define DSA_SIG dsasig_t 93 94 /* misc defs */ 95 #define RSA_NO_PADDING 3 96 97 #define SIGNETBSD_ID_SIZE 8 98 #define SIGNETBSD_NAME_SIZE 128 99 100 #define RSA_PUBKEY_ALG 1 101 #define DSA_PUBKEY_ALG 17 102 103 /* the public part of the key */ 104 typedef struct pubkey_t { 105 uint32_t version; /* key version - usually 4 */ 106 uint8_t id[SIGNETBSD_ID_SIZE]; /* binary id */ 107 char name[SIGNETBSD_NAME_SIZE]; /* name of identity - not necessary, but looks better */ 108 int64_t birthtime; /* time of creation of key */ 109 int64_t expiry; /* expiration time of the key */ 110 uint32_t validity; /* validity in days */ 111 uint32_t alg; /* pubkey algorithm - rsa/dss etc */ 112 rsa_pubkey_t rsa; /* specific RSA keys */ 113 dsa_pubkey_t dsa; /* specific DSA keys */ 114 } pubkey_t; 115 116 /* signature details (for a specific file) */ 117 typedef struct signature_t { 118 uint32_t version; /* signature version number */ 119 uint32_t type; /* signature type value */ 120 int64_t birthtime; /* creation time of the signature */ 121 int64_t expiry; /* expiration time of the signature */ 122 uint8_t id[SIGNETBSD_ID_SIZE]; /* binary id */ 123 uint32_t key_alg; /* public key algorithm number */ 124 uint32_t hash_alg; /* hashing algorithm number */ 125 rsasig_t rsa; /* RSA signature */ 126 dsasig_t dsa; /* DSA signature */ 127 size_t v4_hashlen; /* length of hashed info */ 128 uint8_t *v4_hashed; /* hashed info */ 129 uint8_t hash2[2]; /* high 2 bytes of hashed value - for quick test */ 130 pubkey_t *signer; /* pubkey of signer */ 131 } signature_t; 132 133 unsigned dsa_verify(const signature_t */*sig*/, const dsa_pubkey_t */*pubdsa*/, const uint8_t */*calc*/, size_t /*hashlen*/); 134 135 RSA *RSA_new(void); 136 int RSA_size(const RSA */*rsa*/); 137 void RSA_free(RSA */*rsa*/); 138 int RSA_check_key(RSA */*rsa*/); 139 RSA *RSA_generate_key(int /*num*/, unsigned long /*e*/, void (*callback)(int,int,void *), void */*cb_arg*/); 140 int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding); 141 int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding); 142 int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding); 143 int RSA_public_decrypt(int flen, const uint8_t *from, uint8_t *to, RSA *rsa, int padding); 144 145 DSA *DSA_new(void); 146 int DSA_size(const DSA */*rsa*/); 147 void DSA_free(DSA */*dsa*/); 148 DSA_SIG *DSA_SIG_new(void); 149 void DSA_SIG_free(DSA_SIG */*sig*/); 150 int DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa); 151 DSA_SIG *DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa); 152 153 __END_DECLS 154 155 #endif 156