You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crypto.h 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright(C) 2006 Cameron Rich
  3. *
  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.
  8. *
  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.
  13. *
  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
  17. */
  18. /**
  19. * @file crypto.h
  20. */
  21. #ifndef HEADER_CRYPTO_H
  22. #define HEADER_CRYPTO_H
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #include "bigint.h"
  27. /**************************************************************************
  28. * AES declarations
  29. **************************************************************************/
  30. #define AES_MAXROUNDS 14
  31. typedef struct aes_key_st
  32. {
  33. uint16_t rounds;
  34. uint16_t key_size;
  35. uint32_t ks[(AES_MAXROUNDS+1)*8];
  36. uint8_t iv[16];
  37. } AES_CTX;
  38. typedef enum
  39. {
  40. AES_MODE_128,
  41. AES_MODE_256
  42. } AES_MODE;
  43. void AES_set_key(AES_CTX *ctx, const uint8_t *key,
  44. const uint8_t *iv, AES_MODE mode);
  45. void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg,
  46. uint8_t *out, int length);
  47. void AES_cbc_decrypt(AES_CTX *ks, const uint8_t *in, uint8_t *out, int length);
  48. void AES_convert_key(AES_CTX *ctx);
  49. /**************************************************************************
  50. * RC4 declarations
  51. **************************************************************************/
  52. typedef struct
  53. {
  54. int x, y, m[256];
  55. } RC4_CTX;
  56. void RC4_setup(RC4_CTX *s, const uint8_t *key, int length);
  57. void RC4_crypt(RC4_CTX *s, const uint8_t *msg, uint8_t *data, int length);
  58. /**************************************************************************
  59. * SHA1 declarations
  60. **************************************************************************/
  61. #define SHA1_SIZE 20
  62. /*
  63. * This structure will hold context information for the SHA-1
  64. * hashing operation
  65. */
  66. typedef struct
  67. {
  68. uint32_t Intermediate_Hash[SHA1_SIZE/4]; /* Message Digest */
  69. uint32_t Length_Low; /* Message length in bits */
  70. uint32_t Length_High; /* Message length in bits */
  71. uint16_t Message_Block_Index; /* Index into message block array */
  72. uint8_t Message_Block[64]; /* 512-bit message blocks */
  73. } SHA1_CTX;
  74. void SHA1Init(SHA1_CTX *);
  75. void SHA1Update(SHA1_CTX *, const uint8_t * msg, int len);
  76. void SHA1Final(SHA1_CTX *, uint8_t *digest);
  77. /**************************************************************************
  78. * MD5 declarations
  79. **************************************************************************/
  80. /* MD5 context. */
  81. #define MD5_SIZE 16
  82. typedef struct
  83. {
  84. uint32_t state[4]; /* state (ABCD) */
  85. uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
  86. uint8_t buffer[64]; /* input buffer */
  87. } MD5_CTX;
  88. void MD5Init(MD5_CTX *);
  89. void MD5Update(MD5_CTX *, const uint8_t *msg, int len);
  90. void MD5Final(MD5_CTX *, uint8_t *digest);
  91. /**************************************************************************
  92. * HMAC declarations
  93. **************************************************************************/
  94. void hmac_md5(const uint8_t *msg, int length, const uint8_t *key,
  95. int key_len, uint8_t *digest);
  96. void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key,
  97. int key_len, uint8_t *digest);
  98. /**************************************************************************
  99. * RNG declarations
  100. **************************************************************************/
  101. void RNG_initialize(const uint8_t *seed_buf, int size);
  102. void RNG_terminate(void);
  103. void get_random(int num_rand_bytes, uint8_t *rand_data);
  104. void get_random_NZ(int num_rand_bytes, uint8_t *rand_data);
  105. /**************************************************************************
  106. * RSA declarations
  107. **************************************************************************/
  108. typedef struct
  109. {
  110. bigint *m; /* modulus */
  111. bigint *e; /* public exponent */
  112. bigint *d; /* private exponent */
  113. #ifdef CONFIG_BIGINT_CRT
  114. bigint *p; /* p as in m = pq */
  115. bigint *q; /* q as in m = pq */
  116. bigint *dP; /* d mod (p-1) */
  117. bigint *dQ; /* d mod (q-1) */
  118. bigint *qInv; /* q^-1 mod p */
  119. #endif
  120. int num_octets;
  121. bigint *sig_m; /* signature modulus */
  122. BI_CTX *bi_ctx;
  123. } RSA_CTX;
  124. void RSA_priv_key_new(RSA_CTX **rsa_ctx,
  125. const uint8_t *modulus, int mod_len,
  126. const uint8_t *pub_exp, int pub_len,
  127. const uint8_t *priv_exp, int priv_len
  128. #ifdef CONFIG_BIGINT_CRT
  129. , const uint8_t *p, int p_len,
  130. const uint8_t *q, int q_len,
  131. const uint8_t *dP, int dP_len,
  132. const uint8_t *dQ, int dQ_len,
  133. const uint8_t *qInv, int qInv_len
  134. #endif
  135. );
  136. void RSA_pub_key_new(RSA_CTX **rsa_ctx,
  137. const uint8_t *modulus, int mod_len,
  138. const uint8_t *pub_exp, int pub_len);
  139. void RSA_free(RSA_CTX *ctx);
  140. int RSA_decrypt(RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data,
  141. int is_decryption);
  142. bigint *RSA_private(RSA_CTX *c, bigint *bi_msg);
  143. #ifdef CONFIG_SSL_CERT_VERIFICATION
  144. bigint *RSA_raw_sign_verify(RSA_CTX *c, bigint *bi_msg);
  145. bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
  146. bigint *modulus, bigint *pub_exp);
  147. bigint *RSA_public(RSA_CTX *c, bigint *bi_msg);
  148. int RSA_encrypt(RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
  149. uint8_t *out_data, int is_signing);
  150. void RSA_print(const RSA_CTX *ctx);
  151. #endif
  152. /**************************************************************************
  153. * ASN1 declarations
  154. **************************************************************************/
  155. #define X509_OK 0
  156. #define X509_NOT_OK -1
  157. #define X509_VFY_ERROR_NO_TRUSTED_CERT -2
  158. #define X509_VFY_ERROR_BAD_SIGNATURE -3
  159. #define X509_VFY_ERROR_NOT_YET_VALID -4
  160. #define X509_VFY_ERROR_EXPIRED -5
  161. #define X509_VFY_ERROR_SELF_SIGNED -6
  162. #define X509_VFY_ERROR_INVALID_CHAIN -7
  163. #define X509_VFY_ERROR_UNSUPPORTED_DIGEST -8
  164. #define X509_INVALID_PRIV_KEY -9
  165. /*
  166. * The Distinguished Name
  167. */
  168. #define X509_NUM_DN_TYPES 3
  169. #define X509_COMMON_NAME 0
  170. #define X509_ORGANIZATION 1
  171. #define X509_ORGANIZATIONAL_TYPE 2
  172. #define ASN1_INTEGER 0x02
  173. #define ASN1_BIT_STRING 0x03
  174. #define ASN1_OCTET_STRING 0x04
  175. #define ASN1_NULL 0x05
  176. #define ASN1_OID 0x06
  177. #define ASN1_PRINTABLE_STR 0x13
  178. #define ASN1_TELETEX_STR 0x14
  179. #define ASN1_IA5_STR 0x16
  180. #define ASN1_UTC_TIME 0x17
  181. #define ASN1_SEQUENCE 0x30
  182. #define ASN1_SET 0x31
  183. #define ASN1_IMPLICIT_TAG 0x80
  184. #define ASN1_EXPLICIT_TAG 0xa0
  185. #define SALT_SIZE 8
  186. struct _x509_ctx
  187. {
  188. char *ca_cert_dn[X509_NUM_DN_TYPES];
  189. char *cert_dn[X509_NUM_DN_TYPES];
  190. #if defined(_WIN32_WCE)
  191. long not_before;
  192. long not_after;
  193. #else
  194. time_t not_before;
  195. time_t not_after;
  196. #endif
  197. uint8_t *signature;
  198. uint16_t sig_len;
  199. uint8_t sig_type;
  200. RSA_CTX *rsa_ctx;
  201. bigint *digest;
  202. struct _x509_ctx *next;
  203. };
  204. typedef struct _x509_ctx X509_CTX;
  205. #ifdef CONFIG_SSL_CERT_VERIFICATION
  206. typedef struct
  207. {
  208. X509_CTX *cert[CONFIG_X509_MAX_CA_CERTS];
  209. } CA_CERT_CTX;
  210. #endif
  211. int asn1_get_private_key(const uint8_t *buf, int len, RSA_CTX **rsa_ctx);
  212. int asn1_next_obj(const uint8_t *buf, int *offset, int obj_type);
  213. int asn1_skip_obj(const uint8_t *buf, int *offset, int obj_type);
  214. int asn1_get_int(const uint8_t *buf, int *offset, uint8_t **object);
  215. int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx);
  216. void x509_free(X509_CTX *x509_ctx);
  217. #ifdef CONFIG_SSL_CERT_VERIFICATION
  218. int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert);
  219. const uint8_t *x509_get_signature(const uint8_t *asn1_signature, int *len);
  220. #endif
  221. #ifdef CONFIG_SSL_FULL_MODE
  222. void x509_print(CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert);
  223. void x509_display_error(int error);
  224. #endif
  225. /**************************************************************************
  226. * MISC declarations
  227. **************************************************************************/
  228. extern const char * const unsupported_str;
  229. typedef void (*crypt_func)(void *, const uint8_t *, uint8_t *, int);
  230. typedef void (*hmac_func)(const uint8_t *msg, int length, const uint8_t *key,
  231. int key_len, uint8_t *digest);
  232. typedef struct
  233. {
  234. uint8_t *pre_data; /* include the ssl record bytes */
  235. uint8_t *data; /* the regular ssl data */
  236. int max_len;
  237. int index;
  238. } BUF_MEM;
  239. BUF_MEM buf_new(void);
  240. void buf_grow(BUF_MEM *bm, int len);
  241. void buf_free(BUF_MEM *bm);
  242. int get_file(const char *filename, uint8_t **buf);
  243. #if defined(CONFIG_SSL_FULL_MODE) || defined(WIN32) || defined(CONFIG_DEBUG)
  244. void print_blob(const char *format, const uint8_t *data, int size, ...);
  245. #else
  246. #define print_blob(...)
  247. #endif
  248. #ifdef __cplusplus
  249. }
  250. #endif
  251. #endif