ssl_crypto.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2007, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @file crypto.h
  32. */
  33. #ifndef HEADER_CRYPTO_H
  34. #define HEADER_CRYPTO_H
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. #include "ssl/ssl_config.h"
  39. #include "ssl/ssl_bigint_impl.h"
  40. #include "ssl/ssl_bigint.h"
  41. #ifndef STDCALL
  42. #define STDCALL
  43. #endif
  44. #ifndef EXP_FUNC
  45. #define EXP_FUNC
  46. #endif
  47. /* enable features based on a 'super-set' capbaility. */
  48. #if defined(CONFIG_SSL_FULL_MODE)
  49. #define CONFIG_SSL_ENABLE_CLIENT
  50. #define CONFIG_SSL_CERT_VERIFICATION
  51. #elif defined(CONFIG_SSL_ENABLE_CLIENT)
  52. #define CONFIG_SSL_CERT_VERIFICATION
  53. #endif
  54. /**************************************************************************
  55. * AES declarations
  56. **************************************************************************/
  57. #define AES_MAXROUNDS 14
  58. #define AES_BLOCKSIZE 16
  59. #define AES_IV_SIZE 16
  60. typedef struct aes_key_st
  61. {
  62. uint16_t rounds;
  63. uint16_t key_size;
  64. uint32_t ks[(AES_MAXROUNDS+1)*8];
  65. uint8_t iv[AES_IV_SIZE];
  66. } AES_CTX;
  67. typedef enum
  68. {
  69. AES_MODE_128,
  70. AES_MODE_256
  71. } AES_MODE;
  72. void AES_set_key(AES_CTX *ctx, const uint8_t *key,
  73. const uint8_t *iv, AES_MODE mode);
  74. void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg,
  75. uint8_t *out, int length);
  76. void AES_cbc_decrypt(AES_CTX *ks, const uint8_t *in, uint8_t *out, int length);
  77. void AES_convert_key(AES_CTX *ctx);
  78. /**************************************************************************
  79. * RC4 declarations
  80. **************************************************************************/
  81. typedef struct
  82. {
  83. uint8_t x, y, m[256];
  84. } RC4_CTX;
  85. void RC4_setup(RC4_CTX *s, const uint8_t *key, int length);
  86. void RC4_crypt(RC4_CTX *s, const uint8_t *msg, uint8_t *data, int length);
  87. /**************************************************************************
  88. * SHA1 declarations
  89. **************************************************************************/
  90. #define SHA1_SIZE 20
  91. /*
  92. * This structure will hold context information for the SHA-1
  93. * hashing operation
  94. */
  95. typedef struct
  96. {
  97. uint32_t Intermediate_Hash[SHA1_SIZE/4]; /* Message Digest */
  98. uint32_t Length_Low; /* Message length in bits */
  99. uint32_t Length_High; /* Message length in bits */
  100. uint16_t Message_Block_Index; /* Index into message block array */
  101. uint8_t Message_Block[64]; /* 512-bit message blocks */
  102. } SHA1_CTX;
  103. void SHA1_Init(SHA1_CTX *);
  104. void SHA1_Update(SHA1_CTX *, const uint8_t * msg, int len);
  105. void SHA1_Final(uint8_t *digest, SHA1_CTX *);
  106. /**************************************************************************
  107. * MD2 declarations
  108. **************************************************************************/
  109. #define MD2_SIZE 16
  110. typedef struct
  111. {
  112. unsigned char cksum[16]; /* checksum of the data block */
  113. unsigned char state[48]; /* intermediate digest state */
  114. unsigned char buffer[16]; /* data block being processed */
  115. int left; /* amount of data in buffer */
  116. } MD2_CTX;
  117. EXP_FUNC void STDCALL MD2_Init(MD2_CTX *ctx);
  118. EXP_FUNC void STDCALL MD2_Update(MD2_CTX *ctx, const uint8_t *input, int ilen);
  119. EXP_FUNC void STDCALL MD2_Final(uint8_t *digest, MD2_CTX *ctx);
  120. /**************************************************************************
  121. * MD5 declarations
  122. **************************************************************************/
  123. #define MD5_SIZE 16
  124. typedef struct
  125. {
  126. uint32_t state[4]; /* state (ABCD) */
  127. uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
  128. uint8_t buffer[64]; /* input buffer */
  129. } MD5_CTX;
  130. EXP_FUNC void STDCALL MD5_Init(MD5_CTX *);
  131. EXP_FUNC void STDCALL MD5_Update(MD5_CTX *, const uint8_t *msg, int len);
  132. EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *);
  133. /**************************************************************************
  134. * HMAC declarations
  135. **************************************************************************/
  136. void ssl_hmac_md5(const uint8_t *msg, int length, const uint8_t *key,
  137. int key_len, uint8_t *digest);// fix hmac_md5 to ssl_hmac_md5, discriminate ieee80211
  138. void ssl_hmac_sha1(const uint8_t *msg, int length, const uint8_t *key,
  139. int key_len, uint8_t *digest);// fix hmac_md5 to ssl_hmac_sha1, discriminate ieee80211
  140. /**************************************************************************
  141. * RSA declarations
  142. **************************************************************************/
  143. typedef struct
  144. {
  145. bigint *m; /* modulus */
  146. bigint *e; /* public exponent */
  147. bigint *d; /* private exponent */
  148. #ifdef CONFIG_BIGINT_CRT
  149. bigint *p; /* p as in m = pq */
  150. bigint *q; /* q as in m = pq */
  151. bigint *dP; /* d mod (p-1) */
  152. bigint *dQ; /* d mod (q-1) */
  153. bigint *qInv; /* q^-1 mod p */
  154. #endif
  155. int num_octets;
  156. BI_CTX *bi_ctx;
  157. } RSA_CTX;
  158. void RSA_priv_key_new(RSA_CTX **rsa_ctx,
  159. const uint8_t *modulus, int mod_len,
  160. const uint8_t *pub_exp, int pub_len,
  161. const uint8_t *priv_exp, int priv_len
  162. #ifdef CONFIG_BIGINT_CRT
  163. , const uint8_t *p, int p_len,
  164. const uint8_t *q, int q_len,
  165. const uint8_t *dP, int dP_len,
  166. const uint8_t *dQ, int dQ_len,
  167. const uint8_t *qInv, int qInv_len
  168. #endif
  169. );
  170. void RSA_pub_key_new(RSA_CTX **rsa_ctx,
  171. const uint8_t *modulus, int mod_len,
  172. const uint8_t *pub_exp, int pub_len);
  173. void RSA_free(RSA_CTX *ctx);
  174. int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data,
  175. int is_decryption);
  176. bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg);
  177. #if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_SSL_GENERATE_X509_CERT)
  178. bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
  179. bigint *modulus, bigint *pub_exp);
  180. bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg);
  181. int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
  182. uint8_t *out_data, int is_signing);
  183. void RSA_print(const RSA_CTX *ctx);
  184. #endif
  185. /**************************************************************************
  186. * RNG declarations
  187. **************************************************************************/
  188. EXP_FUNC void STDCALL RNG_initialize(void);
  189. EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size);
  190. EXP_FUNC void STDCALL RNG_terminate(void);
  191. EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data);
  192. void get_random_NZ(int num_rand_bytes, uint8_t *rand_data);
  193. #ifdef __cplusplus
  194. }
  195. #endif
  196. #endif