Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

crypto.h 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #if 0 /** currently unused function **/
  49. void AES_convert_key(AES_CTX *ctx);
  50. #endif
  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. /**************************************************************************
  108. * RSA declarations
  109. **************************************************************************/
  110. typedef struct
  111. {
  112. bigint *m; /* modulus */
  113. bigint *e; /* public exponent */
  114. bigint *d; /* private exponent */
  115. #ifdef CONFIG_BIGINT_CRT
  116. bigint *p; /* p as in m = pq */
  117. bigint *q; /* q as in m = pq */
  118. bigint *dP; /* d mod (p-1) */
  119. bigint *dQ; /* d mod (q-1) */
  120. bigint *qInv; /* q^-1 mod p */
  121. #endif
  122. int num_octets;
  123. bigint *sig_m; /* signature modulus */
  124. BI_CTX *bi_ctx;
  125. } RSA_CTX;
  126. void RSA_priv_key_new(RSA_CTX **rsa_ctx,
  127. const uint8_t *modulus, int mod_len,
  128. const uint8_t *pub_exp, int pub_len,
  129. const uint8_t *priv_exp, int priv_len
  130. #ifdef CONFIG_BIGINT_CRT
  131. , const uint8_t *p, int p_len,
  132. const uint8_t *q, int q_len,
  133. const uint8_t *dP, int dP_len,
  134. const uint8_t *dQ, int dQ_len,
  135. const uint8_t *qInv, int qInv_len
  136. #endif
  137. );
  138. void RSA_pub_key_new(RSA_CTX **rsa_ctx,
  139. const uint8_t *modulus, int mod_len,
  140. const uint8_t *pub_exp, int pub_len);
  141. void RSA_free(RSA_CTX *ctx);
  142. int RSA_decrypt(RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data,
  143. int is_decryption);
  144. bigint *RSA_private(RSA_CTX *c, bigint *bi_msg);
  145. #ifdef CONFIG_SSL_CERT_VERIFICATION
  146. bigint *RSA_raw_sign_verify(RSA_CTX *c, bigint *bi_msg);
  147. bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
  148. bigint *modulus, bigint *pub_exp);
  149. bigint *RSA_public(RSA_CTX *c, bigint *bi_msg);
  150. int RSA_encrypt(RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
  151. uint8_t *out_data, int is_signing);
  152. void RSA_print(const RSA_CTX *ctx);
  153. #endif
  154. /**************************************************************************
  155. * ASN1 declarations
  156. **************************************************************************/
  157. #define X509_OK 0
  158. #define X509_NOT_OK -1
  159. #define X509_VFY_ERROR_NO_TRUSTED_CERT -2
  160. #define X509_VFY_ERROR_BAD_SIGNATURE -3
  161. #define X509_VFY_ERROR_NOT_YET_VALID -4
  162. #define X509_VFY_ERROR_EXPIRED -5
  163. #define X509_VFY_ERROR_SELF_SIGNED -6
  164. #define X509_VFY_ERROR_INVALID_CHAIN -7
  165. #define X509_VFY_ERROR_UNSUPPORTED_DIGEST -8
  166. #define X509_INVALID_PRIV_KEY -9
  167. /*
  168. * The Distinguished Name
  169. */
  170. #define X509_NUM_DN_TYPES 3
  171. #define X509_COMMON_NAME 0
  172. #define X509_ORGANIZATION 1
  173. #define X509_ORGANIZATIONAL_TYPE 2
  174. #define ASN1_INTEGER 0x02
  175. #define ASN1_BIT_STRING 0x03
  176. #define ASN1_OCTET_STRING 0x04
  177. #define ASN1_NULL 0x05
  178. #define ASN1_OID 0x06
  179. #define ASN1_PRINTABLE_STR 0x13
  180. #define ASN1_TELETEX_STR 0x14
  181. #define ASN1_IA5_STR 0x16
  182. #define ASN1_UTC_TIME 0x17
  183. #define ASN1_SEQUENCE 0x30
  184. #define ASN1_SET 0x31
  185. #define ASN1_IMPLICIT_TAG 0x80
  186. #define ASN1_EXPLICIT_TAG 0xa0
  187. #define SALT_SIZE 8
  188. struct _x509_ctx
  189. {
  190. char *ca_cert_dn[X509_NUM_DN_TYPES];
  191. char *cert_dn[X509_NUM_DN_TYPES];
  192. #if defined(_WIN32_WCE)
  193. long not_before;
  194. long not_after;
  195. #else
  196. time_t not_before;
  197. time_t not_after;
  198. #endif
  199. uint8_t *signature;
  200. uint16_t sig_len;
  201. uint8_t sig_type;
  202. RSA_CTX *rsa_ctx;
  203. bigint *digest;
  204. struct _x509_ctx *next;
  205. };
  206. typedef struct _x509_ctx X509_CTX;
  207. #ifdef CONFIG_SSL_CERT_VERIFICATION
  208. typedef struct
  209. {
  210. X509_CTX *cert[CONFIG_X509_MAX_CA_CERTS];
  211. } CA_CERT_CTX;
  212. #endif
  213. int asn1_get_private_key(const uint8_t *buf, int len, RSA_CTX **rsa_ctx);
  214. int asn1_next_obj(const uint8_t *buf, int *offset, int obj_type);
  215. int asn1_skip_obj(const uint8_t *buf, int *offset, int obj_type);
  216. int asn1_get_int(const uint8_t *buf, int *offset, uint8_t **object);
  217. int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx);
  218. void x509_free(X509_CTX *x509_ctx);
  219. #ifdef CONFIG_SSL_CERT_VERIFICATION
  220. int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert);
  221. const uint8_t *x509_get_signature(const uint8_t *asn1_signature, int *len);
  222. #endif
  223. #ifdef CONFIG_SSL_FULL_MODE
  224. void x509_print(CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert);
  225. void x509_display_error(int error);
  226. #endif
  227. /**************************************************************************
  228. * MISC declarations
  229. **************************************************************************/
  230. extern const char * const unsupported_str;
  231. typedef void (*crypt_func)(void *, const uint8_t *, uint8_t *, int);
  232. typedef void (*hmac_func)(const uint8_t *msg, int length, const uint8_t *key,
  233. int key_len, uint8_t *digest);
  234. typedef struct
  235. {
  236. uint8_t *pre_data; /* include the ssl record bytes */
  237. uint8_t *data; /* the regular ssl data */
  238. int max_len;
  239. int index;
  240. } BUF_MEM;
  241. BUF_MEM buf_new(void);
  242. void buf_grow(BUF_MEM *bm, int len);
  243. void buf_free(BUF_MEM *bm);
  244. int get_file(const char *filename, uint8_t **buf);
  245. #if defined(CONFIG_SSL_FULL_MODE) || defined(WIN32) || defined(CONFIG_DEBUG)
  246. void print_blob(const char *format, const uint8_t *data, int size, ...);
  247. #else
  248. #define print_blob(...)
  249. #endif
  250. #ifdef __cplusplus
  251. }
  252. #endif
  253. #endif