crypto.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2007-2015, 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 "config.h"
  39. #include "bigint_impl.h"
  40. #include "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. * SHA256 declarations
  108. **************************************************************************/
  109. #define SHA256_SIZE 32
  110. typedef struct
  111. {
  112. uint32_t total[2];
  113. uint32_t state[8];
  114. uint8_t buffer[64];
  115. } SHA256_CTX;
  116. void SHA256_Init(SHA256_CTX *c);
  117. void SHA256_Update(SHA256_CTX *, const uint8_t *input, int len);
  118. void SHA256_Final(uint8_t *digest, SHA256_CTX *);
  119. /**************************************************************************
  120. * SHA512 declarations
  121. **************************************************************************/
  122. #define SHA512_SIZE 64
  123. typedef struct
  124. {
  125. union
  126. {
  127. uint64_t h[8];
  128. uint8_t digest[64];
  129. } h_dig;
  130. union
  131. {
  132. uint64_t w[80];
  133. uint8_t buffer[128];
  134. } w_buf;
  135. size_t size;
  136. uint64_t totalSize;
  137. } SHA512_CTX;
  138. void SHA512_Init(SHA512_CTX *c);
  139. void SHA512_Update(SHA512_CTX *, const uint8_t *input, int len);
  140. void SHA512_Final(uint8_t *digest, SHA512_CTX *);
  141. /**************************************************************************
  142. * SHA384 declarations
  143. **************************************************************************/
  144. #define SHA384_SIZE 48
  145. typedef SHA512_CTX SHA384_CTX;
  146. void SHA384_Init(SHA384_CTX *c);
  147. void SHA384_Update(SHA384_CTX *, const uint8_t *input, int len);
  148. void SHA384_Final(uint8_t *digest, SHA384_CTX *);
  149. /**************************************************************************
  150. * MD5 declarations
  151. **************************************************************************/
  152. #define MD5_SIZE 16
  153. typedef struct
  154. {
  155. uint32_t state[4]; /* state (ABCD) */
  156. uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
  157. uint8_t buffer[64]; /* input buffer */
  158. } MD5_CTX;
  159. EXP_FUNC void STDCALL MD5_Init(MD5_CTX *);
  160. EXP_FUNC void STDCALL MD5_Update(MD5_CTX *, const uint8_t *msg, int len);
  161. EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *);
  162. /**************************************************************************
  163. * HMAC declarations
  164. **************************************************************************/
  165. void hmac_md5(const uint8_t *msg, int length, const uint8_t *key,
  166. int key_len, uint8_t *digest);
  167. void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key,
  168. int key_len, uint8_t *digest);
  169. /**************************************************************************
  170. * RSA declarations
  171. **************************************************************************/
  172. typedef struct
  173. {
  174. bigint *m; /* modulus */
  175. bigint *e; /* public exponent */
  176. bigint *d; /* private exponent */
  177. #ifdef CONFIG_BIGINT_CRT
  178. bigint *p; /* p as in m = pq */
  179. bigint *q; /* q as in m = pq */
  180. bigint *dP; /* d mod (p-1) */
  181. bigint *dQ; /* d mod (q-1) */
  182. bigint *qInv; /* q^-1 mod p */
  183. #endif
  184. int num_octets;
  185. BI_CTX *bi_ctx;
  186. } RSA_CTX;
  187. void RSA_priv_key_new(RSA_CTX **rsa_ctx,
  188. const uint8_t *modulus, int mod_len,
  189. const uint8_t *pub_exp, int pub_len,
  190. const uint8_t *priv_exp, int priv_len
  191. #ifdef CONFIG_BIGINT_CRT
  192. , const uint8_t *p, int p_len,
  193. const uint8_t *q, int q_len,
  194. const uint8_t *dP, int dP_len,
  195. const uint8_t *dQ, int dQ_len,
  196. const uint8_t *qInv, int qInv_len
  197. #endif
  198. );
  199. void RSA_pub_key_new(RSA_CTX **rsa_ctx,
  200. const uint8_t *modulus, int mod_len,
  201. const uint8_t *pub_exp, int pub_len);
  202. void RSA_free(RSA_CTX *ctx);
  203. int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data,
  204. int out_len, int is_decryption);
  205. bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg);
  206. #if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_SSL_GENERATE_X509_CERT)
  207. bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
  208. bigint *modulus, bigint *pub_exp);
  209. bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg);
  210. int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
  211. uint8_t *out_data, int is_signing);
  212. void RSA_print(const RSA_CTX *ctx);
  213. #endif
  214. /**************************************************************************
  215. * RNG declarations
  216. **************************************************************************/
  217. EXP_FUNC void STDCALL RNG_initialize(void);
  218. EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size);
  219. EXP_FUNC void STDCALL RNG_terminate(void);
  220. EXP_FUNC int STDCALL get_random(int num_rand_bytes, uint8_t *rand_data);
  221. int get_random_NZ(int num_rand_bytes, uint8_t *rand_data);
  222. #ifdef __cplusplus
  223. }
  224. #endif
  225. #endif