2
0

crypto.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* crypto.h
  2. *
  3. * Copyright (C) 2006-2024 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  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-1335, 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[28]; /* 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. int CRYPT_MD5_DataSizeSet(CRYPT_MD5_CTX* md5, unsigned int sz);
  35. enum {
  36. CRYPT_MD5_DIGEST_SIZE = 16
  37. };
  38. /* SHA */
  39. typedef struct CRYPT_SHA_CTX {
  40. int holder[29]; /* big enough to hold internal, but check on init */
  41. } CRYPT_SHA_CTX;
  42. int CRYPT_SHA_Initialize(CRYPT_SHA_CTX*);
  43. int CRYPT_SHA_DataAdd(CRYPT_SHA_CTX*, const unsigned char*, unsigned int);
  44. int CRYPT_SHA_Finalize(CRYPT_SHA_CTX*, unsigned char*);
  45. int CRYPT_SHA_DataSizeSet(CRYPT_SHA_CTX* sha, unsigned int sz);
  46. enum {
  47. CRYPT_SHA_DIGEST_SIZE = 20
  48. };
  49. /* SHA-256 */
  50. typedef struct CRYPT_SHA256_CTX {
  51. int holder[32]; /* big enough to hold internal, but check on init */
  52. } CRYPT_SHA256_CTX;
  53. int CRYPT_SHA256_Initialize(CRYPT_SHA256_CTX*);
  54. int CRYPT_SHA256_DataAdd(CRYPT_SHA256_CTX*, const unsigned char*, unsigned int);
  55. int CRYPT_SHA256_Finalize(CRYPT_SHA256_CTX*, unsigned char*);
  56. int CRYPT_SHA256_DataSizeSet(CRYPT_SHA256_CTX* sha256, unsigned int sz);
  57. enum {
  58. CRYPT_SHA256_DIGEST_SIZE = 32
  59. };
  60. /* SHA-384 */
  61. typedef struct CRYPT_SHA384_CTX {
  62. long long holder[36]; /* big enough to hold internal, but check on init */
  63. } CRYPT_SHA384_CTX;
  64. int CRYPT_SHA384_Initialize(CRYPT_SHA384_CTX*);
  65. int CRYPT_SHA384_DataAdd(CRYPT_SHA384_CTX*, const unsigned char*, unsigned int);
  66. int CRYPT_SHA384_Finalize(CRYPT_SHA384_CTX*, unsigned char*);
  67. enum {
  68. CRYPT_SHA384_DIGEST_SIZE = 48
  69. };
  70. /* SHA-512 */
  71. typedef struct CRYPT_SHA512_CTX {
  72. long long holder[36]; /* big enough to hold internal, but check on init */
  73. } CRYPT_SHA512_CTX;
  74. int CRYPT_SHA512_Initialize(CRYPT_SHA512_CTX*);
  75. int CRYPT_SHA512_DataAdd(CRYPT_SHA512_CTX*, const unsigned char*, unsigned int);
  76. int CRYPT_SHA512_Finalize(CRYPT_SHA512_CTX*, unsigned char*);
  77. enum {
  78. CRYPT_SHA512_DIGEST_SIZE = 64
  79. };
  80. /* HMAC */
  81. typedef struct CRYPT_HMAC_CTX {
  82. /* big enough to hold internal, but check on init */
  83. #ifdef WOLF_PRIVATE_KEY_ID
  84. long long holder[108];
  85. #else
  86. long long holder[98];
  87. #endif
  88. } CRYPT_HMAC_CTX;
  89. int CRYPT_HMAC_SetKey(CRYPT_HMAC_CTX*, int, const unsigned char*, unsigned int);
  90. int CRYPT_HMAC_DataAdd(CRYPT_HMAC_CTX*, const unsigned char*, unsigned int);
  91. int CRYPT_HMAC_Finalize(CRYPT_HMAC_CTX*, unsigned char*);
  92. /* HMAC types */
  93. enum {
  94. CRYPT_HMAC_SHA = 4,
  95. CRYPT_HMAC_SHA256 = 6,
  96. CRYPT_HMAC_SHA384 = 7,
  97. CRYPT_HMAC_SHA512 = 8
  98. };
  99. /* Huffman */
  100. int CRYPT_HUFFMAN_Compress(unsigned char*, unsigned int, const unsigned char*,
  101. unsigned int, unsigned int);
  102. int CRYPT_HUFFMAN_DeCompress(unsigned char*, unsigned int, const unsigned char*,
  103. unsigned int);
  104. /* flag to use static huffman */
  105. enum {
  106. CRYPT_HUFFMAN_COMPRESS_STATIC = 1
  107. };
  108. /* RNG */
  109. typedef struct CRYPT_RNG_CTX {
  110. int holder[66]; /* big enough to hold internal, but check on init */
  111. } CRYPT_RNG_CTX;
  112. int CRYPT_RNG_Initialize(CRYPT_RNG_CTX*);
  113. int CRYPT_RNG_Get(CRYPT_RNG_CTX*, unsigned char*);
  114. int CRYPT_RNG_BlockGenerate(CRYPT_RNG_CTX*, unsigned char*, unsigned int);
  115. /* TDES */
  116. typedef struct CRYPT_TDES_CTX {
  117. int holder[104]; /* big enough to hold internal, but check on init */
  118. } CRYPT_TDES_CTX;
  119. int CRYPT_TDES_KeySet(CRYPT_TDES_CTX*, const unsigned char*,
  120. const unsigned char*, int);
  121. int CRYPT_TDES_IvSet(CRYPT_TDES_CTX*, const unsigned char*);
  122. int CRYPT_TDES_CBC_Encrypt(CRYPT_TDES_CTX*, unsigned char*,
  123. const unsigned char*, unsigned int);
  124. int CRYPT_TDES_CBC_Decrypt(CRYPT_TDES_CTX*, unsigned char*,
  125. const unsigned char*, unsigned int);
  126. /* key direction flags for setup */
  127. enum {
  128. CRYPT_TDES_ENCRYPTION = 0,
  129. CRYPT_TDES_DECRYPTION = 1
  130. };
  131. /* AES */
  132. typedef struct CRYPT_AES_CTX {
  133. /* big enough to hold internal, but check on init */
  134. #ifdef WOLF_PRIVATE_KEY_ID
  135. int holder[110];
  136. #else
  137. int holder[92];
  138. #endif
  139. } CRYPT_AES_CTX;
  140. /* key */
  141. int CRYPT_AES_KeySet(CRYPT_AES_CTX*, const unsigned char*, unsigned int,
  142. const unsigned char*, int);
  143. int CRYPT_AES_IvSet(CRYPT_AES_CTX*, const unsigned char*);
  144. /* cbc */
  145. int CRYPT_AES_CBC_Encrypt(CRYPT_AES_CTX*, unsigned char*,
  146. const unsigned char*, unsigned int);
  147. int CRYPT_AES_CBC_Decrypt(CRYPT_AES_CTX*, unsigned char*,
  148. const unsigned char*, unsigned int);
  149. /* ctr (counter), use Encrypt both ways with ENCRYPT key setup */
  150. int CRYPT_AES_CTR_Encrypt(CRYPT_AES_CTX*, unsigned char*,
  151. const unsigned char*, unsigned int);
  152. /* direct, one block at a time */
  153. int CRYPT_AES_DIRECT_Encrypt(CRYPT_AES_CTX*, unsigned char*,
  154. const unsigned char*);
  155. int CRYPT_AES_DIRECT_Decrypt(CRYPT_AES_CTX*, unsigned char*,
  156. const unsigned char*);
  157. /* key direction flags for setup, ctr always uses ENCRYPT flag */
  158. enum {
  159. CRYPT_AES_ENCRYPTION = 0,
  160. CRYPT_AES_DECRYPTION = 1,
  161. CRYPT_AES_BLOCK_SIZE = 16
  162. };
  163. /* RSA */
  164. typedef struct CRYPT_RSA_CTX {
  165. void* holder;
  166. } CRYPT_RSA_CTX;
  167. /* init/free */
  168. int CRYPT_RSA_Initialize(CRYPT_RSA_CTX*);
  169. int CRYPT_RSA_Free(CRYPT_RSA_CTX*);
  170. /* key decode */
  171. int CRYPT_RSA_PublicKeyDecode(CRYPT_RSA_CTX*, const unsigned char*,
  172. unsigned int);
  173. int CRYPT_RSA_PrivateKeyDecode(CRYPT_RSA_CTX*, const unsigned char*,
  174. unsigned int);
  175. /* encrypt/decrypt */
  176. int CRYPT_RSA_PublicEncrypt(CRYPT_RSA_CTX*, unsigned char*,
  177. unsigned int, const unsigned char*, unsigned int,
  178. CRYPT_RNG_CTX*);
  179. int CRYPT_RSA_PrivateDecrypt(CRYPT_RSA_CTX*, unsigned char*,
  180. unsigned int, const unsigned char*, unsigned int);
  181. /* helpers */
  182. int CRYPT_RSA_EncryptSizeGet(CRYPT_RSA_CTX*);
  183. int CRYPT_RSA_SetRng(CRYPT_RSA_CTX*, CRYPT_RNG_CTX*);
  184. /* ECC */
  185. typedef struct CRYPT_ECC_CTX {
  186. void* holder;
  187. } CRYPT_ECC_CTX;
  188. /* init/free */
  189. int CRYPT_ECC_Initialize(CRYPT_ECC_CTX*);
  190. int CRYPT_ECC_Free(CRYPT_ECC_CTX*);
  191. /* key coders */
  192. int CRYPT_ECC_PublicExport(CRYPT_ECC_CTX*, unsigned char*, unsigned int,
  193. unsigned int*);
  194. int CRYPT_ECC_PublicImport(CRYPT_ECC_CTX*, const unsigned char*, unsigned int);
  195. int CRYPT_ECC_PrivateImport(CRYPT_ECC_CTX*, const unsigned char*, unsigned int,
  196. const unsigned char*, unsigned int);
  197. /* dhe */
  198. int CRYPT_ECC_DHE_KeyMake(CRYPT_ECC_CTX*, CRYPT_RNG_CTX*, int);
  199. int CRYPT_ECC_DHE_SharedSecretMake(CRYPT_ECC_CTX*, CRYPT_ECC_CTX*,
  200. unsigned char*, unsigned int, unsigned int*);
  201. /* dsa */
  202. int CRYPT_ECC_DSA_HashSign(CRYPT_ECC_CTX*, CRYPT_RNG_CTX*, unsigned char*,
  203. unsigned int, unsigned int*, const unsigned char*, unsigned int);
  204. int CRYPT_ECC_DSA_HashVerify(CRYPT_ECC_CTX*, const unsigned char*,
  205. unsigned int, unsigned char*, unsigned int, int*);
  206. /* helpers */
  207. int CRYPT_ECC_KeySizeGet(CRYPT_ECC_CTX*);
  208. int CRYPT_ECC_SignatureSizeGet(CRYPT_ECC_CTX*);
  209. /* Error string helper, string needs to be >= 80 chars */
  210. int CRYPT_ERROR_StringGet(int, char*);
  211. #ifdef __cplusplus
  212. } /* extern "C" */
  213. #endif
  214. #endif /* MC_CRYPTO_API_H */