2 * Copyright(C) 2006 Cameron Rich
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifndef HEADER_CRYPTO_H
24 #define HEADER_CRYPTO_H
32 /**************************************************************************
34 **************************************************************************/
36 #define AES_MAXROUNDS 14
38 typedef struct aes_key_st
42 uint32_t ks[(AES_MAXROUNDS+1)*8];
52 void AES_set_key(AES_CTX *ctx, const uint8_t *key,
53 const uint8_t *iv, AES_MODE mode);
54 void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg,
55 uint8_t *out, int length);
56 void AES_cbc_decrypt(AES_CTX *ks, const uint8_t *in, uint8_t *out, int length);
57 void AES_convert_key(AES_CTX *ctx);
59 /**************************************************************************
61 **************************************************************************/
68 void RC4_setup(RC4_CTX *s, const uint8_t *key, int length);
69 void RC4_crypt(RC4_CTX *s, const uint8_t *msg, uint8_t *data, int length);
71 /**************************************************************************
73 **************************************************************************/
78 * This structure will hold context information for the SHA-1
83 uint32_t Intermediate_Hash[SHA1_SIZE/4]; /* Message Digest */
84 uint32_t Length_Low; /* Message length in bits */
85 uint32_t Length_High; /* Message length in bits */
86 uint16_t Message_Block_Index; /* Index into message block array */
87 uint8_t Message_Block[64]; /* 512-bit message blocks */
90 void SHA1Init(SHA1_CTX *);
91 void SHA1Update(SHA1_CTX *, const uint8_t * msg, int len);
92 void SHA1Final(SHA1_CTX *, uint8_t *digest);
94 /**************************************************************************
96 **************************************************************************/
104 uint32_t state[4]; /* state (ABCD) */
105 uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
106 uint8_t buffer[64]; /* input buffer */
109 void MD5Init(MD5_CTX *);
110 void MD5Update(MD5_CTX *, const uint8_t *msg, int len);
111 void MD5Final(MD5_CTX *, uint8_t *digest);
113 /**************************************************************************
115 **************************************************************************/
116 void hmac_md5(const uint8_t *msg, int length, const uint8_t *key,
117 int key_len, uint8_t *digest);
118 void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key,
119 int key_len, uint8_t *digest);
121 /**************************************************************************
123 **************************************************************************/
124 void RNG_initialize(const uint8_t *seed_buf, int size);
125 void RNG_terminate(void);
126 void get_random(int num_rand_bytes, uint8_t *rand_data);
127 //void get_random_NZ(int num_rand_bytes, uint8_t *rand_data);
130 static inline void get_random_NZ(int num_rand_bytes, uint8_t *rand_data) {
131 memset ( rand_data, 0x01, num_rand_bytes );
134 /**************************************************************************
136 **************************************************************************/
140 bigint *m; /* modulus */
141 bigint *e; /* public exponent */
142 bigint *d; /* private exponent */
143 #ifdef CONFIG_BIGINT_CRT
144 bigint *p; /* p as in m = pq */
145 bigint *q; /* q as in m = pq */
146 bigint *dP; /* d mod (p-1) */
147 bigint *dQ; /* d mod (q-1) */
148 bigint *qInv; /* q^-1 mod p */
151 bigint *sig_m; /* signature modulus */
155 void RSA_priv_key_new(RSA_CTX **rsa_ctx,
156 const uint8_t *modulus, int mod_len,
157 const uint8_t *pub_exp, int pub_len,
158 const uint8_t *priv_exp, int priv_len
159 #ifdef CONFIG_BIGINT_CRT
160 , const uint8_t *p, int p_len,
161 const uint8_t *q, int q_len,
162 const uint8_t *dP, int dP_len,
163 const uint8_t *dQ, int dQ_len,
164 const uint8_t *qInv, int qInv_len
167 void RSA_pub_key_new(RSA_CTX **rsa_ctx,
168 const uint8_t *modulus, int mod_len,
169 const uint8_t *pub_exp, int pub_len);
170 void RSA_free(RSA_CTX *ctx);
171 int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data,
173 bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg);
174 #ifdef CONFIG_SSL_CERT_VERIFICATION
175 bigint *RSA_raw_sign_verify(RSA_CTX *c, bigint *bi_msg);
176 bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
177 bigint *modulus, bigint *pub_exp);
178 bigint *RSA_public(const RSA_CTX *c, bigint *bi_msg);
179 int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
180 uint8_t *out_data, int is_signing);
181 void RSA_print(const RSA_CTX *ctx);
184 /**************************************************************************
186 **************************************************************************/
188 #define X509_NOT_OK -1
189 #define X509_VFY_ERROR_NO_TRUSTED_CERT -2
190 #define X509_VFY_ERROR_BAD_SIGNATURE -3
191 #define X509_VFY_ERROR_NOT_YET_VALID -4
192 #define X509_VFY_ERROR_EXPIRED -5
193 #define X509_VFY_ERROR_SELF_SIGNED -6
194 #define X509_VFY_ERROR_INVALID_CHAIN -7
195 #define X509_VFY_ERROR_UNSUPPORTED_DIGEST -8
196 #define X509_INVALID_PRIV_KEY -9
199 * The Distinguished Name
201 #define X509_NUM_DN_TYPES 3
202 #define X509_COMMON_NAME 0
203 #define X509_ORGANIZATION 1
204 #define X509_ORGANIZATIONAL_TYPE 2
206 #define ASN1_INTEGER 0x02
207 #define ASN1_BIT_STRING 0x03
208 #define ASN1_OCTET_STRING 0x04
209 #define ASN1_NULL 0x05
210 #define ASN1_OID 0x06
211 #define ASN1_PRINTABLE_STR 0x13
212 #define ASN1_TELETEX_STR 0x14
213 #define ASN1_IA5_STR 0x16
214 #define ASN1_UTC_TIME 0x17
215 #define ASN1_SEQUENCE 0x30
216 #define ASN1_SET 0x31
217 #define ASN1_IMPLICIT_TAG 0x80
218 #define ASN1_EXPLICIT_TAG 0xa0
224 char *ca_cert_dn[X509_NUM_DN_TYPES];
225 char *cert_dn[X509_NUM_DN_TYPES];
226 #if defined(_WIN32_WCE)
238 struct _x509_ctx *next;
241 typedef struct _x509_ctx X509_CTX;
243 #ifdef CONFIG_SSL_CERT_VERIFICATION
246 X509_CTX *cert[CONFIG_X509_MAX_CA_CERTS];
250 int asn1_get_private_key(const uint8_t *buf, int len, RSA_CTX **rsa_ctx);
251 int asn1_next_obj(const uint8_t *buf, int *offset, int obj_type);
252 int asn1_skip_obj(const uint8_t *buf, int *offset, int obj_type);
253 int asn1_get_int(const uint8_t *buf, int *offset, uint8_t **object);
254 int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx);
255 void x509_free(X509_CTX *x509_ctx);
256 #ifdef CONFIG_SSL_CERT_VERIFICATION
257 int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert);
258 const uint8_t *x509_get_signature(const uint8_t *asn1_signature, int *len);
260 #ifdef CONFIG_SSL_FULL_MODE
261 void x509_print(CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert);
262 void x509_display_error(int error);
265 /**************************************************************************
267 **************************************************************************/
269 extern const char * const unsupported_str;
271 typedef void (*crypt_func)(void *, const uint8_t *, uint8_t *, int);
272 typedef void (*hmac_func)(const uint8_t *msg, int length, const uint8_t *key,
273 int key_len, uint8_t *digest);
277 uint8_t *pre_data; /* include the ssl record bytes */
278 uint8_t *data; /* the regular ssl data */
283 BUF_MEM buf_new(void);
284 void buf_grow(BUF_MEM *bm, int len);
285 void buf_free(BUF_MEM *bm);
286 int get_file(const char *filename, uint8_t **buf);
288 #if defined(CONFIG_SSL_FULL_MODE) || defined(WIN32) || defined(CONFIG_DEBUG)
289 void print_blob(const char *format, const uint8_t *data, int size, ...);
291 #define print_blob(...)