crypto.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* crypto.h
  2. *
  3. * Copyright (C) 2006-2015 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL. (formerly known as CyaSSL)
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. /* Defines Microchip CRYPTO API layer */
  22. #ifndef MC_CRYPTO_API_H
  23. #define MC_CRYPTO_API_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /* MD5 */
  28. typedef struct CRYPT_MD5_CTX {
  29. int holder[24]; /* big enough to hold internal, but check on init */
  30. } CRYPT_MD5_CTX;
  31. int CRYPT_MD5_Initialize(CRYPT_MD5_CTX*);
  32. int CRYPT_MD5_DataAdd(CRYPT_MD5_CTX*, const unsigned char*, unsigned int);
  33. int CRYPT_MD5_Finalize(CRYPT_MD5_CTX*, unsigned char*);
  34. enum {
  35. CRYPT_MD5_DIGEST_SIZE = 16
  36. };
  37. /* SHA */
  38. typedef struct CRYPT_SHA_CTX {
  39. int holder[24]; /* big enough to hold internal, but check on init */
  40. } CRYPT_SHA_CTX;
  41. int CRYPT_SHA_Initialize(CRYPT_SHA_CTX*);
  42. int CRYPT_SHA_DataAdd(CRYPT_SHA_CTX*, const unsigned char*, unsigned int);
  43. int CRYPT_SHA_Finalize(CRYPT_SHA_CTX*, unsigned char*);
  44. enum {
  45. CRYPT_SHA_DIGEST_SIZE = 20
  46. };
  47. /* SHA-256 */
  48. typedef struct CRYPT_SHA256_CTX {
  49. int holder[28]; /* big enough to hold internal, but check on init */
  50. } CRYPT_SHA256_CTX;
  51. int CRYPT_SHA256_Initialize(CRYPT_SHA256_CTX*);
  52. int CRYPT_SHA256_DataAdd(CRYPT_SHA256_CTX*, const unsigned char*, unsigned int);
  53. int CRYPT_SHA256_Finalize(CRYPT_SHA256_CTX*, unsigned char*);
  54. enum {
  55. CRYPT_SHA256_DIGEST_SIZE = 32
  56. };
  57. /* SHA-384 */
  58. typedef struct CRYPT_SHA384_CTX {
  59. long long holder[32]; /* big enough to hold internal, but check on init */
  60. } CRYPT_SHA384_CTX;
  61. int CRYPT_SHA384_Initialize(CRYPT_SHA384_CTX*);
  62. int CRYPT_SHA384_DataAdd(CRYPT_SHA384_CTX*, const unsigned char*, unsigned int);
  63. int CRYPT_SHA384_Finalize(CRYPT_SHA384_CTX*, unsigned char*);
  64. enum {
  65. CRYPT_SHA384_DIGEST_SIZE = 48
  66. };
  67. /* SHA-512 */
  68. typedef struct CRYPT_SHA512_CTX {
  69. long long holder[36]; /* big enough to hold internal, but check on init */
  70. } CRYPT_SHA512_CTX;
  71. int CRYPT_SHA512_Initialize(CRYPT_SHA512_CTX*);
  72. int CRYPT_SHA512_DataAdd(CRYPT_SHA512_CTX*, const unsigned char*, unsigned int);
  73. int CRYPT_SHA512_Finalize(CRYPT_SHA512_CTX*, unsigned char*);
  74. enum {
  75. CRYPT_SHA512_DIGEST_SIZE = 64
  76. };
  77. /* HMAC */
  78. typedef struct CRYPT_HMAC_CTX {
  79. long long holder[67]; /* big enough to hold internal, but check on init */
  80. } CRYPT_HMAC_CTX;
  81. int CRYPT_HMAC_SetKey(CRYPT_HMAC_CTX*, int, const unsigned char*, unsigned int);
  82. int CRYPT_HMAC_DataAdd(CRYPT_HMAC_CTX*, const unsigned char*, unsigned int);
  83. int CRYPT_HMAC_Finalize(CRYPT_HMAC_CTX*, unsigned char*);
  84. /* HMAC types */
  85. enum {
  86. CRYPT_HMAC_SHA = 1,
  87. CRYPT_HMAC_SHA256 = 2,
  88. CRYPT_HMAC_SHA384 = 5,
  89. CRYPT_HMAC_SHA512 = 4
  90. };
  91. /* Huffman */
  92. int CRYPT_HUFFMAN_Compress(unsigned char*, unsigned int, const unsigned char*,
  93. unsigned int, unsigned int);
  94. int CRYPT_HUFFMAN_DeCompress(unsigned char*, unsigned int, const unsigned char*,
  95. unsigned int);
  96. /* flag to use static huffman */
  97. enum {
  98. CRYPT_HUFFMAN_COMPRESS_STATIC = 1
  99. };
  100. /* RNG */
  101. typedef struct CRYPT_RNG_CTX {
  102. int holder[66]; /* big enough to hold internal, but check on init */
  103. } CRYPT_RNG_CTX;
  104. int CRYPT_RNG_Initialize(CRYPT_RNG_CTX*);
  105. int CRYPT_RNG_Get(CRYPT_RNG_CTX*, unsigned char*);
  106. int CRYPT_RNG_BlockGenerate(CRYPT_RNG_CTX*, unsigned char*, unsigned int);
  107. /* TDES */
  108. typedef struct CRYPT_TDES_CTX {
  109. int holder[100]; /* big enough to hold internal, but check on init */
  110. } CRYPT_TDES_CTX;
  111. int CRYPT_TDES_KeySet(CRYPT_TDES_CTX*, const unsigned char*,
  112. const unsigned char*, int);
  113. int CRYPT_TDES_IvSet(CRYPT_TDES_CTX*, const unsigned char*);
  114. int CRYPT_TDES_CBC_Encrypt(CRYPT_TDES_CTX*, unsigned char*,
  115. const unsigned char*, unsigned int);
  116. int CRYPT_TDES_CBC_Decrypt(CRYPT_TDES_CTX*, unsigned char*,
  117. const unsigned char*, unsigned int);
  118. /* key direction flags for setup */
  119. enum {
  120. CRYPT_TDES_ENCRYPTION = 0,
  121. CRYPT_TDES_DECRYPTION = 1
  122. };
  123. /* AES */
  124. typedef struct CRYPT_AES_CTX {
  125. int holder[74]; /* big enough to hold internal, but check on init */
  126. } CRYPT_AES_CTX;
  127. /* key */
  128. int CRYPT_AES_KeySet(CRYPT_AES_CTX*, const unsigned char*, unsigned int,
  129. const unsigned char*, int);
  130. int CRYPT_AES_IvSet(CRYPT_AES_CTX*, const unsigned char*);
  131. /* cbc */
  132. int CRYPT_AES_CBC_Encrypt(CRYPT_AES_CTX*, unsigned char*,
  133. const unsigned char*, unsigned int);
  134. int CRYPT_AES_CBC_Decrypt(CRYPT_AES_CTX*, unsigned char*,
  135. const unsigned char*, unsigned int);
  136. /* ctr (counter), use Encrypt both ways with ENCRYPT key setup */
  137. int CRYPT_AES_CTR_Encrypt(CRYPT_AES_CTX*, unsigned char*,
  138. const unsigned char*, unsigned int);
  139. /* direct, one block at a time */
  140. int CRYPT_AES_DIRECT_Encrypt(CRYPT_AES_CTX*, unsigned char*,
  141. const unsigned char*);
  142. int CRYPT_AES_DIRECT_Decrypt(CRYPT_AES_CTX*, unsigned char*,
  143. const unsigned char*);
  144. /* key direction flags for setup, ctr always uses ENCRYPT flag */
  145. enum {
  146. CRYPT_AES_ENCRYPTION = 0,
  147. CRYPT_AES_DECRYPTION = 1,
  148. CRYPT_AES_BLOCK_SIZE = 16
  149. };
  150. /* RSA */
  151. typedef struct CRYPT_RSA_CTX {
  152. void* holder;
  153. } CRYPT_RSA_CTX;
  154. /* init/free */
  155. int CRYPT_RSA_Initialize(CRYPT_RSA_CTX*);
  156. int CRYPT_RSA_Free(CRYPT_RSA_CTX*);
  157. /* key decode */
  158. int CRYPT_RSA_PublicKeyDecode(CRYPT_RSA_CTX*, const unsigned char*,
  159. unsigned int);
  160. int CRYPT_RSA_PrivateKeyDecode(CRYPT_RSA_CTX*, const unsigned char*,
  161. unsigned int);
  162. /* encrypt/decrypt */
  163. int CRYPT_RSA_PublicEncrypt(CRYPT_RSA_CTX*, unsigned char*,
  164. unsigned int, const unsigned char*, unsigned int,
  165. CRYPT_RNG_CTX*);
  166. int CRYPT_RSA_PrivateDecrypt(CRYPT_RSA_CTX*, unsigned char*,
  167. unsigned int, const unsigned char*, unsigned int);
  168. /* helpers */
  169. int CRYPT_RSA_EncryptSizeGet(CRYPT_RSA_CTX*);
  170. /* ECC */
  171. typedef struct CRYPT_ECC_CTX {
  172. void* holder;
  173. } CRYPT_ECC_CTX;
  174. /* init/free */
  175. int CRYPT_ECC_Initialize(CRYPT_ECC_CTX*);
  176. int CRYPT_ECC_Free(CRYPT_ECC_CTX*);
  177. /* key coders */
  178. int CRYPT_ECC_PublicExport(CRYPT_ECC_CTX*, unsigned char*, unsigned int,
  179. unsigned int*);
  180. int CRYPT_ECC_PublicImport(CRYPT_ECC_CTX*, const unsigned char*, unsigned int);
  181. int CRYPT_ECC_PrivateImport(CRYPT_ECC_CTX*, const unsigned char*, unsigned int,
  182. const unsigned char*, unsigned int);
  183. /* dhe */
  184. int CRYPT_ECC_DHE_KeyMake(CRYPT_ECC_CTX*, CRYPT_RNG_CTX*, int);
  185. int CRYPT_ECC_DHE_SharedSecretMake(CRYPT_ECC_CTX*, CRYPT_ECC_CTX*,
  186. unsigned char*, unsigned int, unsigned int*);
  187. /* dsa */
  188. int CRYPT_ECC_DSA_HashSign(CRYPT_ECC_CTX*, CRYPT_RNG_CTX*, unsigned char*,
  189. unsigned int, unsigned int*, const unsigned char*, unsigned int);
  190. int CRYPT_ECC_DSA_HashVerify(CRYPT_ECC_CTX*, const unsigned char*,
  191. unsigned int, unsigned char*, unsigned int, int*);
  192. /* helpers */
  193. int CRYPT_ECC_KeySizeGet(CRYPT_ECC_CTX*);
  194. int CRYPT_ECC_SignatureSizeGet(CRYPT_ECC_CTX*);
  195. /* Error string helper, string needs to be >= 80 chars */
  196. int CRYPT_ERROR_StringGet(int, char*);
  197. #ifdef __cplusplus
  198. } /* extern "C" */
  199. #endif
  200. #endif /* MC_CRYPTO_API_H */