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.6KB

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