crypto.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* crypto.h
  2. *
  3. * Copyright (C) 2006-2021 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. long long holder[98]; /* big enough to hold internal, but check on init */
  83. } CRYPT_HMAC_CTX;
  84. int CRYPT_HMAC_SetKey(CRYPT_HMAC_CTX*, int, const unsigned char*, unsigned int);
  85. int CRYPT_HMAC_DataAdd(CRYPT_HMAC_CTX*, const unsigned char*, unsigned int);
  86. int CRYPT_HMAC_Finalize(CRYPT_HMAC_CTX*, unsigned char*);
  87. /* HMAC types */
  88. enum {
  89. CRYPT_HMAC_SHA = 4,
  90. CRYPT_HMAC_SHA256 = 6,
  91. CRYPT_HMAC_SHA384 = 7,
  92. CRYPT_HMAC_SHA512 = 8
  93. };
  94. /* Huffman */
  95. int CRYPT_HUFFMAN_Compress(unsigned char*, unsigned int, const unsigned char*,
  96. unsigned int, unsigned int);
  97. int CRYPT_HUFFMAN_DeCompress(unsigned char*, unsigned int, const unsigned char*,
  98. unsigned int);
  99. /* flag to use static huffman */
  100. enum {
  101. CRYPT_HUFFMAN_COMPRESS_STATIC = 1
  102. };
  103. /* RNG */
  104. typedef struct CRYPT_RNG_CTX {
  105. int holder[66]; /* big enough to hold internal, but check on init */
  106. } CRYPT_RNG_CTX;
  107. int CRYPT_RNG_Initialize(CRYPT_RNG_CTX*);
  108. int CRYPT_RNG_Get(CRYPT_RNG_CTX*, unsigned char*);
  109. int CRYPT_RNG_BlockGenerate(CRYPT_RNG_CTX*, unsigned char*, unsigned int);
  110. /* TDES */
  111. typedef struct CRYPT_TDES_CTX {
  112. int holder[104]; /* big enough to hold internal, but check on init */
  113. } CRYPT_TDES_CTX;
  114. int CRYPT_TDES_KeySet(CRYPT_TDES_CTX*, const unsigned char*,
  115. const unsigned char*, int);
  116. int CRYPT_TDES_IvSet(CRYPT_TDES_CTX*, const unsigned char*);
  117. int CRYPT_TDES_CBC_Encrypt(CRYPT_TDES_CTX*, unsigned char*,
  118. const unsigned char*, unsigned int);
  119. int CRYPT_TDES_CBC_Decrypt(CRYPT_TDES_CTX*, unsigned char*,
  120. const unsigned char*, unsigned int);
  121. /* key direction flags for setup */
  122. enum {
  123. CRYPT_TDES_ENCRYPTION = 0,
  124. CRYPT_TDES_DECRYPTION = 1
  125. };
  126. /* AES */
  127. typedef struct CRYPT_AES_CTX {
  128. int holder[90]; /* big enough to hold internal, but check on init */
  129. } CRYPT_AES_CTX;
  130. /* key */
  131. int CRYPT_AES_KeySet(CRYPT_AES_CTX*, const unsigned char*, unsigned int,
  132. const unsigned char*, int);
  133. int CRYPT_AES_IvSet(CRYPT_AES_CTX*, const unsigned char*);
  134. /* cbc */
  135. int CRYPT_AES_CBC_Encrypt(CRYPT_AES_CTX*, unsigned char*,
  136. const unsigned char*, unsigned int);
  137. int CRYPT_AES_CBC_Decrypt(CRYPT_AES_CTX*, unsigned char*,
  138. const unsigned char*, unsigned int);
  139. /* ctr (counter), use Encrypt both ways with ENCRYPT key setup */
  140. int CRYPT_AES_CTR_Encrypt(CRYPT_AES_CTX*, unsigned char*,
  141. const unsigned char*, unsigned int);
  142. /* direct, one block at a time */
  143. int CRYPT_AES_DIRECT_Encrypt(CRYPT_AES_CTX*, unsigned char*,
  144. const unsigned char*);
  145. int CRYPT_AES_DIRECT_Decrypt(CRYPT_AES_CTX*, unsigned char*,
  146. const unsigned char*);
  147. /* key direction flags for setup, ctr always uses ENCRYPT flag */
  148. enum {
  149. CRYPT_AES_ENCRYPTION = 0,
  150. CRYPT_AES_DECRYPTION = 1,
  151. CRYPT_AES_BLOCK_SIZE = 16
  152. };
  153. /* RSA */
  154. typedef struct CRYPT_RSA_CTX {
  155. void* holder;
  156. } CRYPT_RSA_CTX;
  157. /* init/free */
  158. int CRYPT_RSA_Initialize(CRYPT_RSA_CTX*);
  159. int CRYPT_RSA_Free(CRYPT_RSA_CTX*);
  160. /* key decode */
  161. int CRYPT_RSA_PublicKeyDecode(CRYPT_RSA_CTX*, const unsigned char*,
  162. unsigned int);
  163. int CRYPT_RSA_PrivateKeyDecode(CRYPT_RSA_CTX*, const unsigned char*,
  164. unsigned int);
  165. /* encrypt/decrypt */
  166. int CRYPT_RSA_PublicEncrypt(CRYPT_RSA_CTX*, unsigned char*,
  167. unsigned int, const unsigned char*, unsigned int,
  168. CRYPT_RNG_CTX*);
  169. int CRYPT_RSA_PrivateDecrypt(CRYPT_RSA_CTX*, unsigned char*,
  170. unsigned int, const unsigned char*, unsigned int);
  171. /* helpers */
  172. int CRYPT_RSA_EncryptSizeGet(CRYPT_RSA_CTX*);
  173. int CRYPT_RSA_SetRng(CRYPT_RSA_CTX*, CRYPT_RNG_CTX*);
  174. /* ECC */
  175. typedef struct CRYPT_ECC_CTX {
  176. void* holder;
  177. } CRYPT_ECC_CTX;
  178. /* init/free */
  179. int CRYPT_ECC_Initialize(CRYPT_ECC_CTX*);
  180. int CRYPT_ECC_Free(CRYPT_ECC_CTX*);
  181. /* key coders */
  182. int CRYPT_ECC_PublicExport(CRYPT_ECC_CTX*, unsigned char*, unsigned int,
  183. unsigned int*);
  184. int CRYPT_ECC_PublicImport(CRYPT_ECC_CTX*, const unsigned char*, unsigned int);
  185. int CRYPT_ECC_PrivateImport(CRYPT_ECC_CTX*, const unsigned char*, unsigned int,
  186. const unsigned char*, unsigned int);
  187. /* dhe */
  188. int CRYPT_ECC_DHE_KeyMake(CRYPT_ECC_CTX*, CRYPT_RNG_CTX*, int);
  189. int CRYPT_ECC_DHE_SharedSecretMake(CRYPT_ECC_CTX*, CRYPT_ECC_CTX*,
  190. unsigned char*, unsigned int, unsigned int*);
  191. /* dsa */
  192. int CRYPT_ECC_DSA_HashSign(CRYPT_ECC_CTX*, CRYPT_RNG_CTX*, unsigned char*,
  193. unsigned int, unsigned int*, const unsigned char*, unsigned int);
  194. int CRYPT_ECC_DSA_HashVerify(CRYPT_ECC_CTX*, const unsigned char*,
  195. unsigned int, unsigned char*, unsigned int, int*);
  196. /* helpers */
  197. int CRYPT_ECC_KeySizeGet(CRYPT_ECC_CTX*);
  198. int CRYPT_ECC_SignatureSizeGet(CRYPT_ECC_CTX*);
  199. /* Error string helper, string needs to be >= 80 chars */
  200. int CRYPT_ERROR_StringGet(int, char*);
  201. #ifdef __cplusplus
  202. } /* extern "C" */
  203. #endif
  204. #endif /* MC_CRYPTO_API_H */