1
0

crypto.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "config.h"
  39. #include "os_port.h"
  40. #include "bigint_impl.h"
  41. #include "bigint.h"
  42. /* enable features based on a 'super-set' capbaility. */
  43. #if defined(CONFIG_SSL_FULL_MODE)
  44. #define CONFIG_SSL_ENABLE_CLIENT
  45. #define CONFIG_SSL_CERT_VERIFICATION
  46. #elif defined(CONFIG_SSL_ENABLE_CLIENT)
  47. #define CONFIG_SSL_CERT_VERIFICATION
  48. #endif
  49. /**************************************************************************
  50. * AES declarations
  51. **************************************************************************/
  52. #define AES_MAXROUNDS 14
  53. #define AES_BLOCKSIZE 16
  54. #define AES_IV_SIZE 16
  55. typedef struct aes_key_st
  56. {
  57. uint16_t rounds;
  58. uint16_t key_size;
  59. uint32_t ks[(AES_MAXROUNDS+1)*8];
  60. uint8_t iv[AES_IV_SIZE];
  61. } AES_CTX;
  62. typedef enum
  63. {
  64. AES_MODE_128,
  65. AES_MODE_256
  66. } AES_MODE;
  67. void AES_set_key(AES_CTX *ctx, const uint8_t *key,
  68. const uint8_t *iv, AES_MODE mode);
  69. void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg,
  70. uint8_t *out, int length);
  71. void AES_cbc_decrypt(AES_CTX *ks, const uint8_t *in, uint8_t *out, int length);
  72. void AES_convert_key(AES_CTX *ctx);
  73. /**************************************************************************
  74. * RC4 declarations
  75. **************************************************************************/
  76. typedef struct
  77. {
  78. uint8_t x, y, m[256];
  79. } RC4_CTX;
  80. void RC4_setup(RC4_CTX *s, const uint8_t *key, int length);
  81. void RC4_crypt(RC4_CTX *s, const uint8_t *msg, uint8_t *data, int length);
  82. /**************************************************************************
  83. * SHA1 declarations
  84. **************************************************************************/
  85. #define SHA1_SIZE 20
  86. /*
  87. * This structure will hold context information for the SHA-1
  88. * hashing operation
  89. */
  90. typedef struct
  91. {
  92. uint32_t Intermediate_Hash[SHA1_SIZE/4]; /* Message Digest */
  93. uint32_t Length_Low; /* Message length in bits */
  94. uint32_t Length_High; /* Message length in bits */
  95. uint16_t Message_Block_Index; /* Index into message block array */
  96. uint8_t Message_Block[64]; /* 512-bit message blocks */
  97. } SHA1_CTX;
  98. void SHA1_Init(SHA1_CTX *);
  99. void SHA1_Update(SHA1_CTX *, const uint8_t * msg, int len);
  100. void SHA1_Final(uint8_t *digest, SHA1_CTX *);
  101. /**************************************************************************
  102. * MD2 declarations
  103. **************************************************************************/
  104. #define MD2_SIZE 16
  105. typedef struct
  106. {
  107. unsigned char cksum[16]; /* checksum of the data block */
  108. unsigned char state[48]; /* intermediate digest state */
  109. unsigned char buffer[16]; /* data block being processed */
  110. int left; /* amount of data in buffer */
  111. } MD2_CTX;
  112. EXP_FUNC void STDCALL MD2_Init(MD2_CTX *ctx);
  113. EXP_FUNC void STDCALL MD2_Update(MD2_CTX *ctx, const uint8_t *input, int ilen);
  114. EXP_FUNC void STDCALL MD2_Final(uint8_t *digest, MD2_CTX *ctx);
  115. /**************************************************************************
  116. * MD5 declarations
  117. **************************************************************************/
  118. #define MD5_SIZE 16
  119. typedef struct
  120. {
  121. uint32_t state[4]; /* state (ABCD) */
  122. uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
  123. uint8_t buffer[64]; /* input buffer */
  124. } MD5_CTX;
  125. EXP_FUNC void STDCALL MD5_Init(MD5_CTX *);
  126. EXP_FUNC void STDCALL MD5_Update(MD5_CTX *, const uint8_t *msg, int len);
  127. EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *);
  128. /**************************************************************************
  129. * HMAC declarations
  130. **************************************************************************/
  131. void hmac_md5(const uint8_t *msg, int length, const uint8_t *key,
  132. int key_len, uint8_t *digest);
  133. void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key,
  134. int key_len, uint8_t *digest);
  135. /**************************************************************************
  136. * RSA declarations
  137. **************************************************************************/
  138. typedef struct
  139. {
  140. bigint *m; /* modulus */
  141. bigint *e; /* public exponent */
  142. bigint *d; /* private exponent */
  143. #ifdef CONFIG_BIGINT_CRT
  144. bigint *p; /* p as in m = pq */
  145. bigint *q; /* q as in m = pq */
  146. bigint *dP; /* d mod (p-1) */
  147. bigint *dQ; /* d mod (q-1) */
  148. bigint *qInv; /* q^-1 mod p */
  149. #endif
  150. int num_octets;
  151. BI_CTX *bi_ctx;
  152. } RSA_CTX;
  153. void RSA_priv_key_new(RSA_CTX **rsa_ctx,
  154. const uint8_t *modulus, int mod_len,
  155. const uint8_t *pub_exp, int pub_len,
  156. const uint8_t *priv_exp, int priv_len
  157. #ifdef CONFIG_BIGINT_CRT
  158. , const uint8_t *p, int p_len,
  159. const uint8_t *q, int q_len,
  160. const uint8_t *dP, int dP_len,
  161. const uint8_t *dQ, int dQ_len,
  162. const uint8_t *qInv, int qInv_len
  163. #endif
  164. );
  165. void RSA_pub_key_new(RSA_CTX **rsa_ctx,
  166. const uint8_t *modulus, int mod_len,
  167. const uint8_t *pub_exp, int pub_len);
  168. void RSA_free(RSA_CTX *ctx);
  169. int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data,
  170. int is_decryption);
  171. bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg);
  172. #if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_SSL_GENERATE_X509_CERT)
  173. bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
  174. bigint *modulus, bigint *pub_exp);
  175. bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg);
  176. int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
  177. uint8_t *out_data, int is_signing);
  178. void RSA_print(const RSA_CTX *ctx);
  179. #endif
  180. /**************************************************************************
  181. * RNG declarations
  182. **************************************************************************/
  183. EXP_FUNC void STDCALL RNG_initialize(const uint8_t *seed_buf, int size);
  184. EXP_FUNC void STDCALL RNG_terminate(void);
  185. EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data);
  186. void get_random_NZ(int num_rand_bytes, uint8_t *rand_data);
  187. #ifdef __cplusplus
  188. }
  189. #endif
  190. #endif