wolfmath.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /* wolfmath.c
  2. *
  3. * Copyright (C) 2006-2023 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. /* common functions between all math libraries */
  22. /* HAVE_WOLF_BIGINT: Used with asynchronous crypto hardware where "raw" math
  23. * buffers are required.
  24. * NO_BIG_INT: Disable support for all multi-precision math libraries
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include <config.h>
  28. #endif
  29. /* in case user set USE_FAST_MATH there */
  30. #include <wolfssl/wolfcrypt/settings.h>
  31. #include <wolfssl/wolfcrypt/wolfmath.h>
  32. #include <wolfssl/wolfcrypt/error-crypt.h>
  33. #include <wolfssl/wolfcrypt/logging.h>
  34. #ifdef WOLFSSL_ASYNC_CRYPT
  35. #include <wolfssl/wolfcrypt/async.h>
  36. #endif
  37. #ifdef NO_INLINE
  38. #include <wolfssl/wolfcrypt/misc.h>
  39. #else
  40. #define WOLFSSL_MISC_INCLUDED
  41. #include <wolfcrypt/src/misc.c>
  42. #endif
  43. #if !defined(NO_BIG_INT) || defined(WOLFSSL_SP_MATH)
  44. #if !defined(WC_NO_CACHE_RESISTANT) && \
  45. ((defined(HAVE_ECC) && defined(ECC_TIMING_RESISTANT)) || \
  46. (defined(USE_FAST_MATH) && defined(TFM_TIMING_RESISTANT)))
  47. /* all off / all on pointer addresses for constant calculations */
  48. /* ecc.c uses same table */
  49. const wc_ptr_t wc_off_on_addr[2] =
  50. {
  51. #if defined(WC_64BIT_CPU)
  52. W64LIT(0x0000000000000000),
  53. W64LIT(0xffffffffffffffff)
  54. #elif defined(WC_16BIT_CPU)
  55. 0x0000U,
  56. 0xffffU
  57. #else
  58. /* 32 bit */
  59. 0x00000000U,
  60. 0xffffffffU
  61. #endif
  62. };
  63. #endif
  64. /* reverse an array, used for radix code */
  65. void mp_reverse(unsigned char *s, int len)
  66. {
  67. int ix, iy;
  68. if (s == NULL)
  69. return;
  70. ix = 0;
  71. iy = len - 1;
  72. while (ix < iy) {
  73. unsigned char t = s[ix];
  74. s[ix] = s[iy];
  75. s[iy] = t;
  76. ++ix;
  77. --iy;
  78. }
  79. }
  80. int get_digit_count(const mp_int* a)
  81. {
  82. if (a == NULL)
  83. return 0;
  84. return (int)a->used;
  85. }
  86. mp_digit get_digit(const mp_int* a, int n)
  87. {
  88. if (a == NULL)
  89. return 0;
  90. return (n < 0 || (unsigned int)n >= (unsigned int)a->used) ? 0 : a->dp[n];
  91. }
  92. #if defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)
  93. /* Conditionally copy a into b. Performed in constant time.
  94. *
  95. * a MP integer to copy.
  96. * copy On 1, copy a into b. on 0 leave b unchanged.
  97. * b MP integer to copy into.
  98. * returns BAD_FUNC_ARG when a or b is NULL, MEMORY_E when growing b fails and
  99. * MP_OKAY otherwise.
  100. */
  101. int mp_cond_copy(mp_int* a, int copy, mp_int* b)
  102. {
  103. int err = MP_OKAY;
  104. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE == 8
  105. unsigned int mask = (unsigned int)0 - copy;
  106. #else
  107. mp_digit mask = (mp_digit)0 - (mp_digit)copy;
  108. #endif
  109. if (a == NULL || b == NULL)
  110. err = BAD_FUNC_ARG;
  111. /* Ensure b has enough space to copy a into */
  112. if (err == MP_OKAY)
  113. err = mp_grow(b, (int)a->used + 1);
  114. if (err == MP_OKAY) {
  115. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  116. unsigned int i;
  117. #else
  118. int i;
  119. #endif
  120. /* When mask 0, b is unchanged2
  121. * When mask all set, b ^ b ^ a = a
  122. */
  123. /* Conditionally copy all digits and then number of used digits.
  124. * get_digit() returns 0 when index greater than available digit.
  125. */
  126. for (i = 0; i < a->used; i++) {
  127. b->dp[i] ^= (get_digit(a, (int)i) ^ get_digit(b, (int)i)) & mask;
  128. }
  129. for (; i < b->used; i++) {
  130. b->dp[i] ^= (get_digit(a, (int)i) ^ get_digit(b, (int)i)) & mask;
  131. }
  132. b->used ^= (a->used ^ b->used) & (unsigned int)mask;
  133. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  134. defined(WOLFSSL_SP_INT_NEGATIVE)
  135. b->sign ^= (a->sign ^ b->sign) & (unsigned int)mask;
  136. #endif
  137. }
  138. return err;
  139. }
  140. #endif /* HAVE_ECC || WOLFSSL_MP_COND_COPY */
  141. #ifndef WC_NO_RNG
  142. int get_rand_digit(WC_RNG* rng, mp_digit* d)
  143. {
  144. return wc_RNG_GenerateBlock(rng, (byte*)d, sizeof(mp_digit));
  145. }
  146. #if defined(WC_RSA_BLINDING) || defined(WOLFCRYPT_HAVE_SAKKE)
  147. int mp_rand(mp_int* a, int digits, WC_RNG* rng)
  148. {
  149. int ret = 0;
  150. int cnt = digits * (int)sizeof(mp_digit);
  151. if (rng == NULL) {
  152. ret = MISSING_RNG_E;
  153. }
  154. else if (a == NULL || digits <= 0) {
  155. ret = BAD_FUNC_ARG;
  156. }
  157. #ifdef USE_INTEGER_HEAP_MATH
  158. /* allocate space for digits */
  159. if (ret == MP_OKAY) {
  160. ret = mp_set_bit(a, digits * DIGIT_BIT - 1);
  161. }
  162. #else
  163. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  164. if ((ret == MP_OKAY) && ((unsigned int)digits > a->size))
  165. #else
  166. if ((ret == MP_OKAY) && (digits > FP_SIZE))
  167. #endif
  168. {
  169. ret = BAD_FUNC_ARG;
  170. }
  171. if (ret == MP_OKAY) {
  172. a->used = (word32)digits;
  173. }
  174. #endif
  175. /* fill the data with random bytes */
  176. if (ret == MP_OKAY) {
  177. ret = wc_RNG_GenerateBlock(rng, (byte*)a->dp, (word32)cnt);
  178. }
  179. if (ret == MP_OKAY) {
  180. #ifdef USE_INTEGER_HEAP_MATH
  181. int i;
  182. /* Mask down each digit to only bits used */
  183. for (i = 0; i < a->used; i++) {
  184. a->dp[i] &= MP_MASK;
  185. }
  186. #endif
  187. /* ensure top digit is not zero */
  188. while ((ret == MP_OKAY) && (a->dp[a->used - 1] == 0)) {
  189. ret = get_rand_digit(rng, &a->dp[a->used - 1]);
  190. #ifdef USE_INTEGER_HEAP_MATH
  191. a->dp[a->used - 1] &= MP_MASK;
  192. #endif
  193. }
  194. }
  195. return ret;
  196. }
  197. #endif /* WC_RSA_BLINDING || WOLFCRYPT_HAVE_SAKKE */
  198. #endif /* !WC_NO_RNG */
  199. #if defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)
  200. /* export an mp_int as unsigned char or hex string
  201. * encType is WC_TYPE_UNSIGNED_BIN or WC_TYPE_HEX_STR
  202. * return MP_OKAY on success */
  203. int wc_export_int(mp_int* mp, byte* buf, word32* len, word32 keySz,
  204. int encType)
  205. {
  206. int err;
  207. if (mp == NULL || buf == NULL || len == NULL)
  208. return BAD_FUNC_ARG;
  209. if (encType == WC_TYPE_HEX_STR) {
  210. /* for WC_TYPE_HEX_STR the keySz is not used.
  211. * The size is computed via mp_radix_size and checked with len input */
  212. #ifdef WC_MP_TO_RADIX
  213. int size = 0;
  214. err = mp_radix_size(mp, MP_RADIX_HEX, &size);
  215. if (err == MP_OKAY) {
  216. /* make sure we can fit result */
  217. if (*len < (word32)size) {
  218. *len = (word32)size;
  219. return BUFFER_E;
  220. }
  221. *len = (word32)size;
  222. err = mp_tohex(mp, (char*)buf);
  223. }
  224. #else
  225. err = NOT_COMPILED_IN;
  226. #endif
  227. }
  228. else {
  229. /* for WC_TYPE_UNSIGNED_BIN keySz is used to zero pad.
  230. * The key size is always returned as the size */
  231. if (*len < keySz) {
  232. *len = keySz;
  233. return BUFFER_E;
  234. }
  235. *len = keySz;
  236. XMEMSET(buf, 0, *len);
  237. err = mp_to_unsigned_bin(mp, buf +
  238. (keySz - (word32)mp_unsigned_bin_size(mp)));
  239. }
  240. return err;
  241. }
  242. #endif
  243. #ifdef HAVE_WOLF_BIGINT
  244. void wc_bigint_init(WC_BIGINT* a)
  245. {
  246. if (a != NULL) {
  247. a->buf = NULL;
  248. a->len = 0;
  249. a->heap = NULL;
  250. }
  251. }
  252. int wc_bigint_alloc(WC_BIGINT* a, word32 sz)
  253. {
  254. int err = MP_OKAY;
  255. if (a == NULL)
  256. return BAD_FUNC_ARG;
  257. if (sz > 0) {
  258. if (a->buf && sz > a->len) {
  259. wc_bigint_free(a);
  260. }
  261. if (a->buf == NULL) {
  262. a->buf = (byte*)XMALLOC(sz, a->heap, DYNAMIC_TYPE_WOLF_BIGINT);
  263. if (a->buf == NULL) {
  264. err = MP_MEM;
  265. }
  266. }
  267. else {
  268. XMEMSET(a->buf, 0, sz);
  269. }
  270. }
  271. a->len = sz;
  272. return err;
  273. }
  274. /* assumes input is big endian format */
  275. int wc_bigint_from_unsigned_bin(WC_BIGINT* a, const byte* in, word32 inlen)
  276. {
  277. int err;
  278. if (a == NULL || in == NULL || inlen == 0)
  279. return BAD_FUNC_ARG;
  280. err = wc_bigint_alloc(a, inlen);
  281. if (err == 0) {
  282. XMEMCPY(a->buf, in, inlen);
  283. }
  284. return err;
  285. }
  286. int wc_bigint_to_unsigned_bin(WC_BIGINT* a, byte* out, word32* outlen)
  287. {
  288. word32 sz;
  289. if (a == NULL || out == NULL || outlen == NULL || *outlen == 0)
  290. return BAD_FUNC_ARG;
  291. /* trim to fit into output buffer */
  292. sz = a->len;
  293. if (a->len > *outlen) {
  294. WOLFSSL_MSG("wc_bigint_export: Truncating output");
  295. sz = *outlen;
  296. }
  297. if (a->buf) {
  298. XMEMCPY(out, a->buf, sz);
  299. }
  300. *outlen = sz;
  301. return MP_OKAY;
  302. }
  303. void wc_bigint_zero(WC_BIGINT* a)
  304. {
  305. if (a && a->buf) {
  306. ForceZero(a->buf, a->len);
  307. }
  308. }
  309. void wc_bigint_free(WC_BIGINT* a)
  310. {
  311. if (a) {
  312. if (a->buf) {
  313. XFREE(a->buf, a->heap, DYNAMIC_TYPE_WOLF_BIGINT);
  314. }
  315. a->buf = NULL;
  316. a->len = 0;
  317. }
  318. }
  319. /* sz: make sure the buffer is at least that size and zero padded.
  320. * A `sz == 0` will use the size of `src`.
  321. * The calculated sz is stored into dst->len in `wc_bigint_alloc`.
  322. */
  323. int wc_mp_to_bigint_sz(mp_int* src, WC_BIGINT* dst, word32 sz)
  324. {
  325. int err;
  326. word32 x;
  327. if (src == NULL || dst == NULL)
  328. return BAD_FUNC_ARG;
  329. /* get size of source */
  330. x = mp_unsigned_bin_size(src);
  331. if (sz < x)
  332. sz = x;
  333. /* make sure destination is allocated and large enough */
  334. err = wc_bigint_alloc(dst, sz);
  335. if (err == MP_OKAY && sz > 0) {
  336. /* leading zero pad */
  337. word32 y = sz - x;
  338. XMEMSET(dst->buf, 0, y);
  339. /* export src as unsigned bin to destination buf */
  340. err = mp_to_unsigned_bin(src, dst->buf + y);
  341. }
  342. return err;
  343. }
  344. int wc_mp_to_bigint(mp_int* src, WC_BIGINT* dst)
  345. {
  346. if (src == NULL || dst == NULL)
  347. return BAD_FUNC_ARG;
  348. return wc_mp_to_bigint_sz(src, dst, 0);
  349. }
  350. int wc_bigint_to_mp(WC_BIGINT* src, mp_int* dst)
  351. {
  352. int err;
  353. if (src == NULL || dst == NULL)
  354. return BAD_FUNC_ARG;
  355. if (src->buf == NULL)
  356. return BAD_FUNC_ARG;
  357. err = mp_read_unsigned_bin(dst, src->buf, src->len);
  358. wc_bigint_free(src);
  359. return err;
  360. }
  361. #endif /* HAVE_WOLF_BIGINT */
  362. #endif /* !NO_BIG_INT || WOLFSSL_SP_MATH */
  363. #ifdef HAVE_WC_INTROSPECTION
  364. const char *wc_GetMathInfo(void)
  365. {
  366. return
  367. "\tMulti-Precision: "
  368. #ifdef WOLFSSL_SP_MATH_ALL
  369. "Wolf(SP)"
  370. #ifdef WOLFSSL_SP_NO_DYN_STACK
  371. " no-dyn-stack"
  372. #endif
  373. " word-size=" WC_STRINGIFY(SP_WORD_SIZE)
  374. " bits=" WC_STRINGIFY(SP_INT_BITS)
  375. " sp_int.c"
  376. #elif defined(USE_FAST_MATH)
  377. "Fast"
  378. " max-bits=" WC_STRINGIFY(FP_MAX_BITS)
  379. #ifndef TFM_TIMING_RESISTANT
  380. " not-constant-time"
  381. #endif
  382. " tfm.c"
  383. #elif defined(USE_INTEGER_HEAP_MATH)
  384. "Heap"
  385. " not-constant-time"
  386. " integer.c"
  387. #elif defined(NO_BIG_INT) || defined(WOLFSSL_SP_MATH)
  388. "Disabled"
  389. #else
  390. "Unknown"
  391. #endif
  392. #if defined(WOLFSSL_HAVE_SP_ECC) || defined(WOLFSSL_HAVE_SP_DH) || \
  393. defined(WOLFSSL_HAVE_SP_RSA)
  394. "\n\tSingle Precision:"
  395. #ifdef WOLFSSL_HAVE_SP_ECC
  396. " ecc"
  397. #ifndef WOLFSSL_SP_NO_256
  398. " 256"
  399. #endif
  400. #ifdef WOLFSSL_SP_384
  401. " 384"
  402. #endif
  403. #ifdef WOLFSSL_SP_521
  404. " 521"
  405. #endif
  406. #endif
  407. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(WOLFSSL_HAVE_SP_DH)
  408. " rsa/dh"
  409. #elif defined(WOLFSSL_HAVE_SP_RSA)
  410. " rsa"
  411. #elif defined(WOLFSSL_HAVE_SP_DH)
  412. " dh"
  413. #endif
  414. #ifndef WOLFSSL_SP_NO_2048
  415. " 2048"
  416. #endif
  417. #ifndef WOLFSSL_SP_NO_3072
  418. " 3072"
  419. #endif
  420. #ifdef WOLFSSL_SP_4096
  421. " 4096"
  422. #endif
  423. #ifdef WOLFSSL_SP_ASM
  424. " asm"
  425. #endif
  426. #if !defined(WOLFSSL_SP_ASM)
  427. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE == 32
  428. " sp_c32.c"
  429. #else
  430. " sp_c64.c"
  431. #endif
  432. #elif defined(WOLFSSL_SP_ARM32_ASM)
  433. " sp_arm32.c"
  434. #elif defined(WOLFSSL_SP_ARM64_ASM)
  435. " sp_arm64.c"
  436. #elif defined(WOLFSSL_SP_ARM_THUMB_ASM)
  437. " sp_armthumb.c"
  438. #elif defined(WOLFSSL_SP_ARM_CORTEX_M_ASM)
  439. " sp_cortexm.c"
  440. #elif defined(WOLFSSL_SP_X86_64_ASM)
  441. " sp_x86_64.c"
  442. #else
  443. " sp_[arch].c"
  444. #endif
  445. #endif
  446. /* other SP math options */
  447. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_ECC) || \
  448. defined(WOLFSSL_HAVE_SP_DH) || defined(WOLFSSL_HAVE_SP_RSA)
  449. #ifdef WOLFSSL_SP_SMALL
  450. " small"
  451. #endif
  452. #ifdef WOLFSSL_SP_NO_MALLOC
  453. " no-malloc"
  454. #endif
  455. #endif
  456. ;
  457. }
  458. #endif /* HAVE_WC_INTROSPECTION */