crypto.h 7.6 KB

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