sp.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* sp.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. #ifndef WOLF_CRYPT_SP_H
  22. #define WOLF_CRYPT_SP_H
  23. #include <wolfssl/wolfcrypt/types.h>
  24. #if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \
  25. defined(WOLFSSL_HAVE_SP_ECC)
  26. #ifdef _WIN32_WCE
  27. typedef __int8 int8_t;
  28. typedef __int32 int32_t;
  29. typedef __int64 int64_t;
  30. typedef unsigned __int8 uint8_t;
  31. typedef unsigned __int32 uint32_t;
  32. typedef unsigned __int64 uint64_t;
  33. #elif !defined(WOLFSSL_LINUXKM)
  34. #include <stdint.h>
  35. #endif
  36. #include <wolfssl/wolfcrypt/integer.h>
  37. #include <wolfssl/wolfcrypt/sp_int.h>
  38. #include <wolfssl/wolfcrypt/ecc.h>
  39. #ifdef noinline
  40. #define SP_NOINLINE noinline
  41. #elif defined(_MSC_VER)
  42. #define SP_NOINLINE __declspec(noinline)
  43. #elif defined(__ICCARM__) || defined(__IAR_SYSTEMS_ICC__)
  44. #define SP_NOINLINE _Pragma("inline = never")
  45. #elif defined(__GNUC__) || defined(__KEIL__)
  46. #define SP_NOINLINE __attribute__((noinline))
  47. #else
  48. #define SP_NOINLINE
  49. #endif
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. #ifdef WOLFSSL_HAVE_SP_RSA
  54. /* non-const versions only needed for inlined ARM assembly */
  55. #if defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION == 2 \
  56. && ( defined(WOLFSSL_SP_ARM32_ASM) || defined(WOLFSSL_SP_ARM64_ASM) )
  57. WOLFSSL_LOCAL int sp_RsaPublic_2048(const byte* in, word32 inLen,
  58. mp_int* em, mp_int* mm, byte* out, word32* outLen);
  59. WOLFSSL_LOCAL int sp_RsaPrivate_2048(const byte* in, word32 inLen,
  60. mp_int* dm, mp_int* pm, mp_int* qm, mp_int* dpm, mp_int* dqm, mp_int* qim,
  61. mp_int* mm, byte* out, word32* outLen);
  62. WOLFSSL_LOCAL int sp_RsaPublic_3072(const byte* in, word32 inLen,
  63. mp_int* em, mp_int* mm, byte* out, word32* outLen);
  64. WOLFSSL_LOCAL int sp_RsaPrivate_3072(const byte* in, word32 inLen,
  65. mp_int* dm, mp_int* pm, mp_int* qm, mp_int* dpm, mp_int* dqm, mp_int* qim,
  66. mp_int* mm, byte* out, word32* outLen);
  67. WOLFSSL_LOCAL int sp_RsaPublic_4096(const byte* in, word32 inLen,
  68. mp_int* em, mp_int* mm, byte* out, word32* outLen);
  69. WOLFSSL_LOCAL int sp_RsaPrivate_4096(const byte* in, word32 inLen,
  70. mp_int* dm, mp_int* pm, mp_int* qm, mp_int* dpm, mp_int* dqm, mp_int* qim,
  71. mp_int* mm, byte* out, word32* outLen);
  72. #else
  73. WOLFSSL_LOCAL int sp_RsaPublic_2048(const byte* in, word32 inLen,
  74. const mp_int* em, const mp_int* mm, byte* out, word32* outLen);
  75. WOLFSSL_LOCAL int sp_RsaPrivate_2048(const byte* in, word32 inLen,
  76. const mp_int* dm, const mp_int* pm, const mp_int* qm, const mp_int* dpm,
  77. const mp_int* dqm, const mp_int* qim, const mp_int* mm, byte* out,
  78. word32* outLen);
  79. WOLFSSL_LOCAL int sp_RsaPublic_3072(const byte* in, word32 inLen,
  80. const mp_int* em, const mp_int* mm, byte* out, word32* outLen);
  81. WOLFSSL_LOCAL int sp_RsaPrivate_3072(const byte* in, word32 inLen,
  82. const mp_int* dm, const mp_int* pm, const mp_int* qm, const mp_int* dpm,
  83. const mp_int* dqm, const mp_int* qim, const mp_int* mm, byte* out,
  84. word32* outLen);
  85. WOLFSSL_LOCAL int sp_RsaPublic_4096(const byte* in, word32 inLen,
  86. const mp_int* em, const mp_int* mm, byte* out, word32* outLen);
  87. WOLFSSL_LOCAL int sp_RsaPrivate_4096(const byte* in, word32 inLen,
  88. const mp_int* dm, const mp_int* pm, const mp_int* qm, const mp_int* dpm,
  89. const mp_int* dqm, const mp_int* qim, const mp_int* mm, byte* out,
  90. word32* outLen);
  91. #endif /* HAVE_FIPS_VERSION && HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM[32|64]_ASM */
  92. #endif /* WOLFSSL_HAVE_SP_RSA */
  93. #if defined(WOLFSSL_HAVE_SP_DH) || defined(WOLFSSL_HAVE_SP_RSA)
  94. /* non-const versions only needed for inlined ARM assembly */
  95. #if defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION == 2 \
  96. && ( defined(WOLFSSL_SP_ARM32_ASM) || defined(WOLFSSL_SP_ARM64_ASM) )
  97. WOLFSSL_LOCAL int sp_ModExp_1024(mp_int* base, mp_int* exp, mp_int* mod,
  98. mp_int* res);
  99. WOLFSSL_LOCAL int sp_ModExp_1536(mp_int* base, mp_int* exp, mp_int* mod,
  100. mp_int* res);
  101. WOLFSSL_LOCAL int sp_ModExp_2048(mp_int* base, mp_int* exp, mp_int* mod,
  102. mp_int* res);
  103. WOLFSSL_LOCAL int sp_ModExp_3072(mp_int* base, mp_int* exp, mp_int* mod,
  104. mp_int* res);
  105. WOLFSSL_LOCAL int sp_ModExp_4096(mp_int* base, mp_int* exp, mp_int* mod,
  106. mp_int* res);
  107. #else
  108. WOLFSSL_LOCAL int sp_ModExp_1024(const mp_int* base, const mp_int* exp,
  109. const mp_int* mod, mp_int* res);
  110. WOLFSSL_LOCAL int sp_ModExp_1536(const mp_int* base, const mp_int* exp,
  111. const mp_int* mod, mp_int* res);
  112. WOLFSSL_LOCAL int sp_ModExp_2048(const mp_int* base, const mp_int* exp,
  113. const mp_int* mod, mp_int* res);
  114. WOLFSSL_LOCAL int sp_ModExp_3072(const mp_int* base, const mp_int* exp,
  115. const mp_int* mod, mp_int* res);
  116. WOLFSSL_LOCAL int sp_ModExp_4096(const mp_int* base, const mp_int* exp,
  117. const mp_int* mod, mp_int* res);
  118. #endif /* HAVE_FIPS_VERSION && HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM[32|64]_ASM */
  119. #endif
  120. #ifdef WOLFSSL_HAVE_SP_DH
  121. /* non-const versions only needed for inlined ARM assembly */
  122. #if defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION == 2 \
  123. && ( defined(WOLFSSL_SP_ARM32_ASM) || defined(WOLFSSL_SP_ARM64_ASM) )
  124. WOLFSSL_LOCAL int sp_DhExp_2048(mp_int* base, const byte* exp, word32 expLen,
  125. mp_int* mod, byte* out, word32* outLen);
  126. WOLFSSL_LOCAL int sp_DhExp_3072(mp_int* base, const byte* exp, word32 expLen,
  127. mp_int* mod, byte* out, word32* outLen);
  128. WOLFSSL_LOCAL int sp_DhExp_4096(mp_int* base, const byte* exp, word32 expLen,
  129. mp_int* mod, byte* out, word32* outLen);
  130. #else
  131. WOLFSSL_LOCAL int sp_DhExp_2048(const mp_int* base, const byte* exp,
  132. word32 expLen, const mp_int* mod, byte* out, word32* outLen);
  133. WOLFSSL_LOCAL int sp_DhExp_3072(const mp_int* base, const byte* exp,
  134. word32 expLen, const mp_int* mod, byte* out, word32* outLen);
  135. WOLFSSL_LOCAL int sp_DhExp_4096(const mp_int* base, const byte* exp,
  136. word32 expLen, const mp_int* mod, byte* out, word32* outLen);
  137. #endif /* HAVE_FIPS_VERSION && HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM[32|64]_ASM */
  138. #endif /* WOLFSSL_HAVE_SP_DH */
  139. #ifdef WOLFSSL_HAVE_SP_ECC
  140. /* non-const versions only needed for inlined ARM assembly */
  141. #if defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION == 2 \
  142. && ( defined(WOLFSSL_SP_ARM32_ASM) || defined(WOLFSSL_SP_ARM64_ASM) )
  143. int sp_ecc_mulmod_256(mp_int* km, ecc_point* gm, ecc_point* rm, int map,
  144. void* heap);
  145. int sp_ecc_mulmod_base_256(mp_int* km, ecc_point* rm, int map, void* heap);
  146. int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap);
  147. int sp_ecc_secret_gen_256(mp_int* priv, ecc_point* pub, byte* out,
  148. word32* outlen, void* heap);
  149. int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, mp_int* priv,
  150. mp_int* rm, mp_int* sm, mp_int* km, void* heap);
  151. int sp_ecc_verify_256(const byte* hash, word32 hashLen, mp_int* pX, mp_int* pY,
  152. mp_int* pZ, mp_int* r, mp_int* sm, int* res, void* heap);
  153. int sp_ecc_is_point_256(mp_int* pX, mp_int* pY);
  154. int sp_ecc_check_key_256(mp_int* pX, mp_int* pY, mp_int* privm, void* heap);
  155. int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ,
  156. mp_int* qX, mp_int* qY, mp_int* qZ,
  157. mp_int* rX, mp_int* rY, mp_int* rZ);
  158. int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ,
  159. mp_int* rX, mp_int* rY, mp_int* rZ);
  160. int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ);
  161. int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym);
  162. int sp_ecc_mulmod_384(mp_int* km, ecc_point* gm, ecc_point* rm, int map,
  163. void* heap);
  164. int sp_ecc_mulmod_base_384(mp_int* km, ecc_point* rm, int map, void* heap);
  165. int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap);
  166. int sp_ecc_secret_gen_384(mp_int* priv, ecc_point* pub, byte* out,
  167. word32* outlen, void* heap);
  168. int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, mp_int* priv,
  169. mp_int* rm, mp_int* sm, mp_int* km, void* heap);
  170. int sp_ecc_verify_384(const byte* hash, word32 hashLen, mp_int* pX, mp_int* pY,
  171. mp_int* pZ, mp_int* r, mp_int* sm, int* res, void* heap);
  172. int sp_ecc_is_point_384(mp_int* pX, mp_int* pY);
  173. int sp_ecc_check_key_384(mp_int* pX, mp_int* pY, mp_int* privm, void* heap);
  174. int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ,
  175. mp_int* qX, mp_int* qY, mp_int* qZ,
  176. mp_int* rX, mp_int* rY, mp_int* rZ);
  177. int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ,
  178. mp_int* rX, mp_int* rY, mp_int* rZ);
  179. int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ);
  180. int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym);
  181. #else
  182. int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* rm,
  183. int map, void* heap);
  184. int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm,
  185. const ecc_point* am, int inMont, ecc_point* rm, int map, void* heap);
  186. int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* rm, int map,
  187. void* heap);
  188. int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am,
  189. int inMont, ecc_point* rm, int map, void* heap);
  190. int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap);
  191. int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out,
  192. word32* outlen, void* heap);
  193. int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng,
  194. const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap);
  195. int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX,
  196. const mp_int* pY, const mp_int* pZ, const mp_int* r, const mp_int* sm,
  197. int* res, void* heap);
  198. int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY);
  199. int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY,
  200. const mp_int* privm, void* heap);
  201. int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ,
  202. mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ);
  203. int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ,
  204. mp_int* rX, mp_int* rY, mp_int* rZ);
  205. int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ);
  206. int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym);
  207. int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* rm,
  208. int map, void* heap);
  209. int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm,
  210. const ecc_point* am, int inMont, ecc_point* rm, int map, void* heap);
  211. int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* rm, int map,
  212. void* heap);
  213. int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am,
  214. int inMont, ecc_point* rm, int map, void* heap);
  215. int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap);
  216. int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out,
  217. word32* outlen, void* heap);
  218. int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng,
  219. const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap);
  220. int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX,
  221. const mp_int* pY, const mp_int* pZ, const mp_int* r, const mp_int* sm,
  222. int* res, void* heap);
  223. int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY);
  224. int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY,
  225. const mp_int* privm, void* heap);
  226. int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ,
  227. mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ);
  228. int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ,
  229. mp_int* rX, mp_int* rY, mp_int* rZ);
  230. int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ);
  231. int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym);
  232. int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* rm,
  233. int map, void* heap);
  234. int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* rm, int map,
  235. void* heap);
  236. int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am,
  237. int inMont, ecc_point* rm, int map, void* heap);
  238. int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len,
  239. void* heap);
  240. int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table,
  241. ecc_point* r, int map, void* heap);
  242. int sp_ModExp_Fp_star_1024(const mp_int* base, mp_int* exp, mp_int* res);
  243. int sp_Pairing_1024(const ecc_point* p, const ecc_point* q, mp_int* res);
  244. int sp_Pairing_gen_precomp_1024(const ecc_point* p, byte* table, word32* len);
  245. int sp_Pairing_precomp_1024(const ecc_point* p, const ecc_point* q, mp_int* res,
  246. const byte* table, word32 len);
  247. int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY);
  248. int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY,
  249. const mp_int* privm, void* heap);
  250. #endif /* HAVE_FIPS_VERSION && HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM[32|64]_ASM */
  251. #ifdef WOLFSSL_SP_NONBLOCK
  252. int sp_ecc_sign_256_nb(sp_ecc_ctx_t* ctx, const byte* hash, word32 hashLen,
  253. WC_RNG* rng, mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap);
  254. int sp_ecc_verify_256_nb(sp_ecc_ctx_t* ctx, const byte* hash, word32 hashLen,
  255. const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* r,
  256. const mp_int* sm, int* res, void* heap);
  257. int sp_ecc_sign_384_nb(sp_ecc_ctx_t* ctx, const byte* hash, word32 hashLen,
  258. WC_RNG* rng, mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap);
  259. int sp_ecc_verify_384_nb(sp_ecc_ctx_t* ctx, const byte* hash, word32 hashLen,
  260. const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* r,
  261. const mp_int* sm, int* res, void* heap);
  262. #endif /* WOLFSSL_SP_NONBLOCK */
  263. #endif /* WOLFSSL_HAVE_SP_ECC */
  264. #ifdef __cplusplus
  265. } /* extern "C" */
  266. #endif
  267. #endif /* WOLFSSL_HAVE_SP_RSA || WOLFSSL_HAVE_SP_DH || WOLFSSL_HAVE_SP_ECC */
  268. #endif /* WOLF_CRYPT_SP_H */