2
0

dsa.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. /* dsa.c
  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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifndef NO_DSA
  26. #include <wolfssl/wolfcrypt/random.h>
  27. #include <wolfssl/wolfcrypt/wolfmath.h>
  28. #include <wolfssl/wolfcrypt/error-crypt.h>
  29. #include <wolfssl/wolfcrypt/logging.h>
  30. #include <wolfssl/wolfcrypt/sha.h>
  31. #include <wolfssl/wolfcrypt/dsa.h>
  32. #ifdef NO_INLINE
  33. #include <wolfssl/wolfcrypt/misc.h>
  34. #else
  35. #define WOLFSSL_MISC_INCLUDED
  36. #include <wolfcrypt/src/misc.c>
  37. #endif
  38. #if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_SP_ASM)
  39. /* force off unneeded vector register save/restore. */
  40. #undef SAVE_VECTOR_REGISTERS
  41. #define SAVE_VECTOR_REGISTERS(...) WC_DO_NOTHING
  42. #undef RESTORE_VECTOR_REGISTERS
  43. #define RESTORE_VECTOR_REGISTERS() WC_DO_NOTHING
  44. #endif
  45. #ifdef _MSC_VER
  46. /* disable for while(0) cases (MSVC bug) */
  47. #pragma warning(disable:4127)
  48. #endif
  49. int wc_InitDsaKey(DsaKey* key)
  50. {
  51. if (key == NULL)
  52. return BAD_FUNC_ARG;
  53. key->type = -1; /* haven't decided yet */
  54. key->heap = NULL;
  55. return mp_init_multi(
  56. /* public alloc parts */
  57. &key->p,
  58. &key->q,
  59. &key->g,
  60. &key->y,
  61. /* private alloc parts */
  62. &key->x,
  63. NULL
  64. );
  65. }
  66. int wc_InitDsaKey_h(DsaKey* key, void* h)
  67. {
  68. int ret = wc_InitDsaKey(key);
  69. if (ret == 0)
  70. key->heap = h;
  71. return ret;
  72. }
  73. void wc_FreeDsaKey(DsaKey* key)
  74. {
  75. if (key == NULL)
  76. return;
  77. if (key->type == DSA_PRIVATE)
  78. mp_forcezero(&key->x);
  79. mp_clear(&key->x);
  80. mp_clear(&key->y);
  81. mp_clear(&key->g);
  82. mp_clear(&key->q);
  83. mp_clear(&key->p);
  84. }
  85. /* validate that (L,N) match allowed sizes from FIPS 186-4, Section 4.2.
  86. * modLen - represents L, the size of p (prime modulus) in bits
  87. * divLen - represents N, the size of q (prime divisor) in bits
  88. * return 0 on success, -1 on error */
  89. static int CheckDsaLN(int modLen, int divLen)
  90. {
  91. int ret = -1;
  92. switch (modLen) {
  93. #ifdef WOLFSSL_DSA_768_MODULUS
  94. case 768:
  95. #endif
  96. case 1024:
  97. if (divLen == 160)
  98. ret = 0;
  99. break;
  100. case 2048:
  101. if (divLen == 224 || divLen == 256)
  102. ret = 0;
  103. break;
  104. case 3072:
  105. if (divLen == 256)
  106. ret = 0;
  107. break;
  108. default:
  109. break;
  110. }
  111. return ret;
  112. }
  113. #ifdef WOLFSSL_KEY_GEN
  114. /* Create DSA key pair (&dsa->x, &dsa->y)
  115. *
  116. * Based on NIST FIPS 186-4,
  117. * "B.1.1 Key Pair Generation Using Extra Random Bits"
  118. *
  119. * rng - pointer to initialized WC_RNG structure
  120. * dsa - pointer to initialized DsaKey structure, will hold generated key
  121. *
  122. * return 0 on success, negative on error */
  123. int wc_MakeDsaKey(WC_RNG *rng, DsaKey *dsa)
  124. {
  125. byte* cBuf;
  126. int qSz, pSz, cSz, err;
  127. #ifdef WOLFSSL_SMALL_STACK
  128. mp_int *tmpQ = NULL;
  129. #else
  130. mp_int tmpQ[1];
  131. #endif
  132. if (rng == NULL || dsa == NULL)
  133. return BAD_FUNC_ARG;
  134. qSz = mp_unsigned_bin_size(&dsa->q);
  135. pSz = mp_unsigned_bin_size(&dsa->p);
  136. /* verify (L,N) pair bit lengths */
  137. if (CheckDsaLN(pSz * WOLFSSL_BIT_SIZE, qSz * WOLFSSL_BIT_SIZE) != 0)
  138. return BAD_FUNC_ARG;
  139. /* generate extra 64 bits so that bias from mod function is negligible */
  140. cSz = qSz + (64 / WOLFSSL_BIT_SIZE);
  141. cBuf = (byte*)XMALLOC((size_t)cSz, dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
  142. if (cBuf == NULL) {
  143. return MEMORY_E;
  144. }
  145. SAVE_VECTOR_REGISTERS();
  146. #ifdef WOLFSSL_SMALL_STACK
  147. if ((tmpQ = (mp_int *)XMALLOC(sizeof(*tmpQ), NULL, DYNAMIC_TYPE_WOLF_BIGINT)) == NULL)
  148. err = MEMORY_E;
  149. else
  150. err = MP_OKAY;
  151. if (err == MP_OKAY)
  152. #endif
  153. err = mp_init_multi(&dsa->x, &dsa->y, tmpQ, NULL, NULL, NULL);
  154. if (err == MP_OKAY) {
  155. do {
  156. /* Generate N+64 bits (c) from RNG into &dsa->x, making sure
  157. * result is positive.
  158. * Hash_DRBG uses SHA-256 which matches maximum
  159. * requested_security_strength of (L,N).
  160. */
  161. err = wc_RNG_GenerateBlock(rng, cBuf, (word32)cSz);
  162. if (err != MP_OKAY)
  163. break;
  164. err = mp_read_unsigned_bin(&dsa->x, cBuf, (word32)cSz);
  165. if (err != MP_OKAY)
  166. break;
  167. } while (mp_cmp_d(&dsa->x, 1) != MP_GT);
  168. }
  169. /* tmpQ = q - 1 */
  170. if (err == MP_OKAY)
  171. err = mp_copy(&dsa->q, tmpQ);
  172. if (err == MP_OKAY)
  173. err = mp_sub_d(tmpQ, 1, tmpQ);
  174. /* x = c mod (q-1), &dsa->x holds c */
  175. if (err == MP_OKAY)
  176. err = mp_mod(&dsa->x, tmpQ, &dsa->x);
  177. /* x = c mod (q-1) + 1 */
  178. if (err == MP_OKAY)
  179. err = mp_add_d(&dsa->x, 1, &dsa->x);
  180. /* public key : y = g^x mod p */
  181. if (err == MP_OKAY) {
  182. err = mp_exptmod_ex(&dsa->g, &dsa->x, (int)dsa->q.used, &dsa->p,
  183. &dsa->y);
  184. }
  185. if (err == MP_OKAY)
  186. dsa->type = DSA_PRIVATE;
  187. if (err != MP_OKAY) {
  188. mp_clear(&dsa->x);
  189. mp_clear(&dsa->y);
  190. }
  191. XFREE(cBuf, dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
  192. #ifdef WOLFSSL_SMALL_STACK
  193. if (tmpQ != NULL) {
  194. mp_clear(tmpQ);
  195. XFREE(tmpQ, dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
  196. }
  197. #else
  198. mp_clear(tmpQ);
  199. #endif
  200. RESTORE_VECTOR_REGISTERS();
  201. return err;
  202. }
  203. /* modulus_size in bits */
  204. int wc_MakeDsaParameters(WC_RNG *rng, int modulus_size, DsaKey *dsa)
  205. {
  206. #ifdef WOLFSSL_SMALL_STACK
  207. mp_int *tmp = NULL, *tmp2 = NULL;
  208. #else
  209. mp_int tmp[1], tmp2[1];
  210. #endif
  211. int err, msize, qsize,
  212. loop_check_prime = 0,
  213. check_prime = MP_NO;
  214. unsigned char *buf;
  215. if (rng == NULL || dsa == NULL)
  216. return BAD_FUNC_ARG;
  217. /* set group size in bytes from modulus size
  218. * FIPS 186-4 defines valid values (1024, 160) (2048, 256) (3072, 256)
  219. */
  220. switch (modulus_size) {
  221. #ifdef WOLFSSL_DSA_768_MODULUS
  222. /* This key length is insecure and only included for bind 9 testing */
  223. case 768:
  224. #endif
  225. case 1024:
  226. qsize = 20;
  227. break;
  228. case 2048:
  229. case 3072:
  230. qsize = 32;
  231. break;
  232. default:
  233. return BAD_FUNC_ARG;
  234. }
  235. /* modulus size in bytes */
  236. msize = modulus_size / WOLFSSL_BIT_SIZE;
  237. /* allocate ram */
  238. buf = (unsigned char *)XMALLOC((size_t)(msize - qsize),
  239. dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
  240. if (buf == NULL) {
  241. return MEMORY_E;
  242. }
  243. /* make a random string that will be multiplied against q */
  244. err = wc_RNG_GenerateBlock(rng, buf, (word32)(msize - qsize));
  245. if (err != MP_OKAY) {
  246. XFREE(buf, dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
  247. return err;
  248. }
  249. /* force magnitude */
  250. buf[0] |= 0xC0;
  251. /* force even */
  252. buf[msize - qsize - 1] &= (unsigned char)~1;
  253. #ifdef WOLFSSL_SMALL_STACK
  254. if (((tmp = (mp_int *)XMALLOC(sizeof(*tmp), NULL, DYNAMIC_TYPE_WOLF_BIGINT)) == NULL) ||
  255. ((tmp2 = (mp_int *)XMALLOC(sizeof(*tmp2), NULL, DYNAMIC_TYPE_WOLF_BIGINT)) == NULL))
  256. err = MEMORY_E;
  257. else
  258. err = MP_OKAY;
  259. if (err == MP_OKAY)
  260. #endif
  261. err = mp_init_multi(tmp, tmp2, &dsa->p, &dsa->q, 0, 0);
  262. if (err == MP_OKAY)
  263. err = mp_read_unsigned_bin(tmp2, buf, (word32)(msize - qsize));
  264. /* make our prime q */
  265. if (err == MP_OKAY)
  266. err = mp_rand_prime(&dsa->q, qsize, rng, NULL);
  267. /* p = random * q */
  268. if (err == MP_OKAY)
  269. err = mp_mul(&dsa->q, tmp2, &dsa->p);
  270. /* p = random * q + 1, so q is a prime divisor of p-1 */
  271. if (err == MP_OKAY)
  272. err = mp_add_d(&dsa->p, 1, &dsa->p);
  273. /* tmp = 2q */
  274. if (err == MP_OKAY)
  275. err = mp_add(&dsa->q, &dsa->q, tmp);
  276. if (err == MP_OKAY) {
  277. /* loop until p is prime */
  278. while (check_prime == MP_NO) {
  279. err = mp_prime_is_prime_ex(&dsa->p, 8, &check_prime, rng);
  280. if (err != MP_OKAY)
  281. break;
  282. if (check_prime != MP_YES) {
  283. /* p += 2q */
  284. err = mp_add(tmp, &dsa->p, &dsa->p);
  285. if (err != MP_OKAY)
  286. break;
  287. loop_check_prime++;
  288. }
  289. }
  290. }
  291. /* tmp2 += (2*loop_check_prime)
  292. * to have p = (q * tmp2) + 1 prime
  293. */
  294. if (err == MP_OKAY) {
  295. if (loop_check_prime)
  296. err = mp_add_d(tmp2, 2 * (mp_digit)loop_check_prime, tmp2);
  297. }
  298. if (err == MP_OKAY)
  299. err = mp_init(&dsa->g);
  300. /* find a value g for which g^tmp2 != 1 */
  301. if (err == MP_OKAY)
  302. err = mp_set(&dsa->g, 1);
  303. if (err == MP_OKAY) {
  304. do {
  305. err = mp_add_d(&dsa->g, 1, &dsa->g);
  306. if (err != MP_OKAY)
  307. break;
  308. err = mp_exptmod(&dsa->g, tmp2, &dsa->p, tmp);
  309. if (err != MP_OKAY)
  310. break;
  311. } while (mp_cmp_d(tmp, 1) == MP_EQ);
  312. }
  313. /* at this point tmp generates a group of order q mod p */
  314. if (err == MP_OKAY) {
  315. #ifndef USE_FAST_MATH
  316. /* Exchanging is quick when the data pointer can be copied. */
  317. err = mp_exch(tmp, &dsa->g);
  318. #else
  319. err = mp_copy(tmp, &dsa->g);
  320. #endif
  321. }
  322. XFREE(buf, dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
  323. #ifdef WOLFSSL_SMALL_STACK
  324. if (tmp != NULL) {
  325. mp_clear(tmp);
  326. XFREE(tmp, NULL, DYNAMIC_TYPE_WOLF_BIGINT);
  327. }
  328. if (tmp2 != NULL) {
  329. mp_clear(tmp2);
  330. XFREE(tmp2, NULL, DYNAMIC_TYPE_WOLF_BIGINT);
  331. }
  332. #else
  333. mp_clear(tmp);
  334. mp_clear(tmp2);
  335. #endif
  336. if (err != MP_OKAY) {
  337. mp_clear(&dsa->q);
  338. mp_clear(&dsa->p);
  339. mp_clear(&dsa->g);
  340. }
  341. return err;
  342. }
  343. #endif /* WOLFSSL_KEY_GEN */
  344. static int _DsaImportParamsRaw(DsaKey* dsa, const char* p, const char* q,
  345. const char* g, int trusted, WC_RNG* rng)
  346. {
  347. int err;
  348. int pSz, qSz;
  349. if (dsa == NULL || p == NULL || q == NULL || g == NULL)
  350. return BAD_FUNC_ARG;
  351. /* read p */
  352. err = mp_read_radix(&dsa->p, p, MP_RADIX_HEX);
  353. if (err == MP_OKAY && !trusted) {
  354. int isPrime = 1;
  355. if (rng == NULL)
  356. err = mp_prime_is_prime(&dsa->p, 8, &isPrime);
  357. else
  358. err = mp_prime_is_prime_ex(&dsa->p, 8, &isPrime, rng);
  359. if (err == MP_OKAY) {
  360. if (!isPrime)
  361. err = DH_CHECK_PUB_E;
  362. }
  363. }
  364. /* read q */
  365. if (err == MP_OKAY)
  366. err = mp_read_radix(&dsa->q, q, MP_RADIX_HEX);
  367. /* read g */
  368. if (err == MP_OKAY)
  369. err = mp_read_radix(&dsa->g, g, MP_RADIX_HEX);
  370. /* verify (L,N) pair bit lengths */
  371. pSz = mp_unsigned_bin_size(&dsa->p);
  372. qSz = mp_unsigned_bin_size(&dsa->q);
  373. if (CheckDsaLN(pSz * WOLFSSL_BIT_SIZE, qSz * WOLFSSL_BIT_SIZE) != 0) {
  374. WOLFSSL_MSG("Invalid DSA p or q parameter size");
  375. err = BAD_FUNC_ARG;
  376. }
  377. if (err != MP_OKAY) {
  378. mp_clear(&dsa->p);
  379. mp_clear(&dsa->q);
  380. mp_clear(&dsa->g);
  381. }
  382. return err;
  383. }
  384. /* Import raw DSA parameters into DsaKey structure for use with wc_MakeDsaKey(),
  385. * input parameters (p,q,g) should be represented as ASCII hex values.
  386. *
  387. * dsa - pointer to initialized DsaKey structure
  388. * p - DSA (p) parameter, ASCII hex string
  389. * pSz - length of p
  390. * q - DSA (q) parameter, ASCII hex string
  391. * qSz - length of q
  392. * g - DSA (g) parameter, ASCII hex string
  393. * gSz - length of g
  394. *
  395. * returns 0 on success, negative upon failure
  396. */
  397. int wc_DsaImportParamsRaw(DsaKey* dsa, const char* p, const char* q,
  398. const char* g)
  399. {
  400. return _DsaImportParamsRaw(dsa, p, q, g, 1, NULL);
  401. }
  402. /* Import raw DSA parameters into DsaKey structure for use with wc_MakeDsaKey(),
  403. * input parameters (p,q,g) should be represented as ASCII hex values. Check
  404. * that the p value is probably prime.
  405. *
  406. * dsa - pointer to initialized DsaKey structure
  407. * p - DSA (p) parameter, ASCII hex string
  408. * pSz - length of p
  409. * q - DSA (q) parameter, ASCII hex string
  410. * qSz - length of q
  411. * g - DSA (g) parameter, ASCII hex string
  412. * gSz - length of g
  413. * trusted - trust that p is OK
  414. * rng - random number generator for the prime test
  415. *
  416. * returns 0 on success, negative upon failure
  417. */
  418. int wc_DsaImportParamsRawCheck(DsaKey* dsa, const char* p, const char* q,
  419. const char* g, int trusted, WC_RNG* rng)
  420. {
  421. return _DsaImportParamsRaw(dsa, p, q, g, trusted, rng);
  422. }
  423. /* Export raw DSA parameters from DsaKey structure
  424. *
  425. * dsa - pointer to initialized DsaKey structure
  426. * p - output location for DSA (p) parameter
  427. * pSz - [IN/OUT] size of output buffer for p, size of p
  428. * q - output location for DSA (q) parameter
  429. * qSz - [IN/OUT] size of output buffer for q, size of q
  430. * g - output location for DSA (g) parameter
  431. * gSz - [IN/OUT] size of output buffer for g, size of g
  432. *
  433. * If p, q, and g pointers are all passed in as NULL, the function
  434. * will set pSz, qSz, and gSz to the required output buffer sizes for p,
  435. * q, and g. In this case, the function will return LENGTH_ONLY_E.
  436. *
  437. * returns 0 on success, negative upon failure
  438. */
  439. int wc_DsaExportParamsRaw(DsaKey* dsa, byte* p, word32* pSz,
  440. byte* q, word32* qSz, byte* g, word32* gSz)
  441. {
  442. int err;
  443. word32 pLen, qLen, gLen;
  444. if (dsa == NULL || pSz == NULL || qSz == NULL || gSz == NULL)
  445. return BAD_FUNC_ARG;
  446. /* get required output buffer sizes */
  447. pLen = (word32)mp_unsigned_bin_size(&dsa->p);
  448. qLen = (word32)mp_unsigned_bin_size(&dsa->q);
  449. gLen = (word32)mp_unsigned_bin_size(&dsa->g);
  450. /* return buffer sizes and LENGTH_ONLY_E if buffers are NULL */
  451. if (p == NULL && q == NULL && g == NULL) {
  452. *pSz = pLen;
  453. *qSz = qLen;
  454. *gSz = gLen;
  455. return WC_NO_ERR_TRACE(LENGTH_ONLY_E);
  456. }
  457. if (p == NULL || q == NULL || g == NULL)
  458. return BAD_FUNC_ARG;
  459. /* export p */
  460. if (*pSz < pLen) {
  461. WOLFSSL_MSG("Output buffer for DSA p parameter too small, "
  462. "required size placed into pSz");
  463. *pSz = pLen;
  464. return BUFFER_E;
  465. }
  466. *pSz = pLen;
  467. err = mp_to_unsigned_bin(&dsa->p, p);
  468. /* export q */
  469. if (err == MP_OKAY) {
  470. if (*qSz < qLen) {
  471. WOLFSSL_MSG("Output buffer for DSA q parameter too small, "
  472. "required size placed into qSz");
  473. *qSz = qLen;
  474. return BUFFER_E;
  475. }
  476. *qSz = qLen;
  477. err = mp_to_unsigned_bin(&dsa->q, q);
  478. }
  479. /* export g */
  480. if (err == MP_OKAY) {
  481. if (*gSz < gLen) {
  482. WOLFSSL_MSG("Output buffer for DSA g parameter too small, "
  483. "required size placed into gSz");
  484. *gSz = gLen;
  485. return BUFFER_E;
  486. }
  487. *gSz = gLen;
  488. err = mp_to_unsigned_bin(&dsa->g, g);
  489. }
  490. return err;
  491. }
  492. /* Export raw DSA key (x, y) from DsaKey structure
  493. *
  494. * dsa - pointer to initialized DsaKey structure
  495. * x - output location for private key
  496. * xSz - [IN/OUT] size of output buffer for x, size of x
  497. * y - output location for public key
  498. * ySz - [IN/OUT] size of output buffer for y, size of y
  499. *
  500. * If x and y pointers are all passed in as NULL, the function
  501. * will set xSz and ySz to the required output buffer sizes for x
  502. * and y. In this case, the function will return LENGTH_ONLY_E.
  503. *
  504. * returns 0 on success, negative upon failure
  505. */
  506. int wc_DsaExportKeyRaw(DsaKey* dsa, byte* x, word32* xSz, byte* y, word32* ySz)
  507. {
  508. int err;
  509. word32 xLen, yLen;
  510. if (dsa == NULL || xSz == NULL || ySz == NULL)
  511. return BAD_FUNC_ARG;
  512. /* get required output buffer sizes */
  513. xLen = (word32)mp_unsigned_bin_size(&dsa->x);
  514. yLen = (word32)mp_unsigned_bin_size(&dsa->y);
  515. /* return buffer sizes and LENGTH_ONLY_E if buffers are NULL */
  516. if (x == NULL && y == NULL) {
  517. *xSz = xLen;
  518. *ySz = yLen;
  519. return WC_NO_ERR_TRACE(LENGTH_ONLY_E);
  520. }
  521. if (x == NULL || y == NULL)
  522. return BAD_FUNC_ARG;
  523. /* export x */
  524. if (*xSz < xLen) {
  525. WOLFSSL_MSG("Output buffer for DSA private key (x) too small, "
  526. "required size placed into xSz");
  527. *xSz = xLen;
  528. return BUFFER_E;
  529. }
  530. *xSz = xLen;
  531. err = mp_to_unsigned_bin(&dsa->x, x);
  532. /* export y */
  533. if (err == MP_OKAY) {
  534. if (*ySz < yLen) {
  535. WOLFSSL_MSG("Output buffer to DSA public key (y) too small, "
  536. "required size placed into ySz");
  537. *ySz = yLen;
  538. return BUFFER_E;
  539. }
  540. *ySz = yLen;
  541. err = mp_to_unsigned_bin(&dsa->y, y);
  542. }
  543. return err;
  544. }
  545. int wc_DsaSign(const byte* digest, byte* out, DsaKey* key, WC_RNG* rng)
  546. {
  547. /* use sha1 by default for backwards compatibility */
  548. return wc_DsaSign_ex(digest, WC_SHA_DIGEST_SIZE, out, key, rng);
  549. }
  550. int wc_DsaSign_ex(const byte* digest, word32 digestSz, byte* out, DsaKey* key,
  551. WC_RNG* rng)
  552. {
  553. #ifdef WOLFSSL_SMALL_STACK
  554. mp_int *k = NULL;
  555. mp_int *kInv = NULL;
  556. mp_int *r = NULL;
  557. mp_int *s = NULL;
  558. mp_int *H = NULL;
  559. #ifndef WOLFSSL_MP_INVMOD_CONSTANT_TIME
  560. mp_int *b = NULL;
  561. #endif
  562. byte *buffer = NULL;
  563. #else
  564. mp_int k[1], kInv[1], r[1], s[1], H[1];
  565. #ifndef WOLFSSL_MP_INVMOD_CONSTANT_TIME
  566. mp_int b[1];
  567. #endif
  568. byte buffer[DSA_MAX_HALF_SIZE];
  569. #endif
  570. mp_int* qMinus1;
  571. int ret = 0;
  572. word32 halfSz = 0;
  573. if (digest == NULL || out == NULL || key == NULL || rng == NULL)
  574. return BAD_FUNC_ARG;
  575. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  576. do {
  577. #ifdef WOLFSSL_SMALL_STACK
  578. k = (mp_int *)XMALLOC(sizeof *k, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  579. kInv = (mp_int *)XMALLOC(sizeof *kInv, key->heap,
  580. DYNAMIC_TYPE_TMP_BUFFER);
  581. r = (mp_int *)XMALLOC(sizeof *r, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  582. s = (mp_int *)XMALLOC(sizeof *s, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  583. H = (mp_int *)XMALLOC(sizeof *H, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  584. #ifndef WOLFSSL_MP_INVMOD_CONSTANT_TIME
  585. b = (mp_int *)XMALLOC(sizeof *b, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  586. #endif
  587. buffer = (byte *)XMALLOC(DSA_MAX_HALF_SIZE, key->heap,
  588. DYNAMIC_TYPE_TMP_BUFFER);
  589. if ((k == NULL) ||
  590. (kInv == NULL) ||
  591. (r == NULL) ||
  592. (s == NULL) ||
  593. (H == NULL)
  594. #ifndef WOLFSSL_MP_INVMOD_CONSTANT_TIME
  595. || (b == NULL)
  596. #endif
  597. || (buffer == NULL)) {
  598. ret = MEMORY_E;
  599. break;
  600. }
  601. #endif
  602. #ifdef WOLFSSL_MP_INVMOD_CONSTANT_TIME
  603. if (mp_init_multi(k, kInv, r, s, H, 0) != MP_OKAY)
  604. #else
  605. if (mp_init_multi(k, kInv, r, s, H, b) != MP_OKAY)
  606. #endif
  607. {
  608. ret = MP_INIT_E;
  609. break;
  610. }
  611. halfSz = min(DSA_MAX_HALF_SIZE, (word32)mp_unsigned_bin_size(&key->q));
  612. /* NIST FIPS 186-4: Sections 4.1
  613. * q is a prime divisor where 2^(N-1) < q < 2^N and N is the bit length
  614. * of q.
  615. * To satisfy this constraint if N is 0 then q would still need to be
  616. * larger than 0.5, but since there is 0 bits in q it can not be any
  617. * value.
  618. */
  619. if (halfSz == 0) {
  620. ret = BAD_FUNC_ARG;
  621. break;
  622. }
  623. qMinus1 = kInv;
  624. /* NIST FIPS 186-4: B.2.2
  625. * Per-Message Secret Number Generation by Testing Candidates
  626. * Generate k in range [1, q-1].
  627. * Check that k is less than q-1: range [0, q-2].
  628. * Add 1 to k: range [1, q-1].
  629. */
  630. if (mp_sub_d(&key->q, 1, qMinus1)) {
  631. ret = MP_SUB_E;
  632. break;
  633. }
  634. /* if q-1 is 0 or smaller, k will never end up being less than it */
  635. if (mp_iszero(qMinus1) || mp_isneg(qMinus1)) {
  636. ret = BAD_FUNC_ARG;
  637. break;
  638. }
  639. do {
  640. /* Step 4: generate k */
  641. if ((ret = wc_RNG_GenerateBlock(rng, buffer, halfSz))) {
  642. break;
  643. }
  644. /* Step 5 */
  645. if (mp_read_unsigned_bin(k, buffer, halfSz) != MP_OKAY) {
  646. ret = MP_READ_E;
  647. break;
  648. }
  649. /* k is a random number and it should be less than q-1
  650. * if k greater than repeat
  651. */
  652. /* Step 6 */
  653. } while (mp_cmp(k, qMinus1) != MP_LT);
  654. if (ret != 0)
  655. break;
  656. /* Step 7 */
  657. if (mp_add_d(k, 1, k) != MP_OKAY) {
  658. ret = MP_MOD_E;
  659. break;
  660. }
  661. #ifdef WOLFSSL_MP_INVMOD_CONSTANT_TIME
  662. /* inverse k mod q */
  663. if (mp_invmod(k, &key->q, kInv) != MP_OKAY) {
  664. ret = MP_INVMOD_E;
  665. break;
  666. }
  667. /* generate r, r = (g exp k mod p) mod q */
  668. if (mp_exptmod_ex(&key->g, k, key->q.used, &key->p, r) != MP_OKAY) {
  669. ret = MP_EXPTMOD_E;
  670. break;
  671. }
  672. if (mp_mod(r, &key->q, r) != MP_OKAY) {
  673. ret = MP_MOD_E;
  674. break;
  675. }
  676. /* generate H from sha digest */
  677. if (mp_read_unsigned_bin(H, digest, digestSz) != MP_OKAY) {
  678. ret = MP_READ_E;
  679. break;
  680. }
  681. /* generate s, s = (kInv * (H + x*r)) % q */
  682. if (mp_mul(&key->x, r, s) != MP_OKAY) {
  683. ret = MP_MUL_E;
  684. break;
  685. }
  686. if (mp_add(s, H, s) != MP_OKAY) {
  687. ret = MP_ADD_E;
  688. break;
  689. }
  690. if (mp_mulmod(s, kInv, &key->q, s) != MP_OKAY) {
  691. ret = MP_MULMOD_E;
  692. break;
  693. }
  694. #else
  695. /* Blinding value
  696. * Generate b in range [1, q-1].
  697. */
  698. do {
  699. if ((ret = wc_RNG_GenerateBlock(rng, buffer, halfSz))) {
  700. break;
  701. }
  702. if (mp_read_unsigned_bin(b, buffer, halfSz) != MP_OKAY) {
  703. ret = MP_READ_E;
  704. break;
  705. }
  706. } while (mp_cmp(b, qMinus1) != MP_LT);
  707. if (ret != 0)
  708. break;
  709. if (mp_add_d(b, 1, b) != MP_OKAY) {
  710. ret = MP_MOD_E;
  711. break;
  712. }
  713. /* set H from sha digest */
  714. if (mp_read_unsigned_bin(H, digest, digestSz) != MP_OKAY) {
  715. ret = MP_READ_E;
  716. break;
  717. }
  718. /* generate r, r = (g exp k mod p) mod q */
  719. if (mp_exptmod_ex(&key->g, k, (int)key->q.used, &key->p, r) !=
  720. MP_OKAY) {
  721. ret = MP_EXPTMOD_E;
  722. break;
  723. }
  724. /* calculate s = (H + xr)/k = b.(H/k.b + x.r/k.b) */
  725. /* k = k.b */
  726. if (mp_mulmod(k, b, &key->q, k) != MP_OKAY) {
  727. ret = MP_MULMOD_E;
  728. break;
  729. }
  730. /* kInv = 1/k.b mod q */
  731. if (mp_invmod(k, &key->q, kInv) != MP_OKAY) {
  732. ret = MP_INVMOD_E;
  733. break;
  734. }
  735. if (mp_mod(r, &key->q, r) != MP_OKAY) {
  736. ret = MP_MOD_E;
  737. break;
  738. }
  739. /* s = x.r */
  740. if (mp_mul(&key->x, r, s) != MP_OKAY) {
  741. ret = MP_MUL_E;
  742. break;
  743. }
  744. /* s = x.r/k.b */
  745. if (mp_mulmod(s, kInv, &key->q, s) != MP_OKAY) {
  746. ret = MP_MULMOD_E;
  747. break;
  748. }
  749. /* H = H/k.b */
  750. if (mp_mulmod(H, kInv, &key->q, H) != MP_OKAY) {
  751. ret = MP_MULMOD_E;
  752. break;
  753. }
  754. /* s = H/k.b + x.r/k.b = (H + x.r)/k.b */
  755. if (mp_add(s, H, s) != MP_OKAY) {
  756. ret = MP_ADD_E;
  757. break;
  758. }
  759. /* s = b.(e + x.r)/k.b = (e + x.r)/k */
  760. if (mp_mulmod(s, b, &key->q, s) != MP_OKAY) {
  761. ret = MP_MULMOD_E;
  762. break;
  763. }
  764. /* s = (e + x.r)/k */
  765. if (mp_mod(s, &key->q, s) != MP_OKAY) {
  766. ret = MP_MOD_E;
  767. break;
  768. }
  769. #endif
  770. /* detect zero r or s */
  771. if ((mp_iszero(r) == MP_YES) || (mp_iszero(s) == MP_YES)) {
  772. ret = MP_ZERO_E;
  773. break;
  774. }
  775. /* write out */
  776. {
  777. if (mp_to_unsigned_bin_len(r, out, (int)halfSz) != MP_OKAY)
  778. ret = MP_TO_E;
  779. else {
  780. out += halfSz; /* advance to s in output */
  781. ret = mp_to_unsigned_bin_len(s, out, (int)halfSz);
  782. }
  783. }
  784. } while (0);
  785. RESTORE_VECTOR_REGISTERS();
  786. #ifdef WOLFSSL_SMALL_STACK
  787. if (k) {
  788. if ((ret != WC_NO_ERR_TRACE(MP_INIT_E)) &&
  789. (ret != WC_NO_ERR_TRACE(MEMORY_E)))
  790. mp_forcezero(k);
  791. XFREE(k, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  792. }
  793. if (kInv) {
  794. if ((ret != WC_NO_ERR_TRACE(MP_INIT_E)) &&
  795. (ret != WC_NO_ERR_TRACE(MEMORY_E)))
  796. mp_forcezero(kInv);
  797. XFREE(kInv, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  798. }
  799. if (r) {
  800. if ((ret != WC_NO_ERR_TRACE(MP_INIT_E)) &&
  801. (ret != WC_NO_ERR_TRACE(MEMORY_E)))
  802. mp_clear(r);
  803. XFREE(r, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  804. }
  805. if (s) {
  806. if ((ret != WC_NO_ERR_TRACE(MP_INIT_E)) &&
  807. (ret != WC_NO_ERR_TRACE(MEMORY_E)))
  808. mp_clear(s);
  809. XFREE(s, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  810. }
  811. if (H) {
  812. if ((ret != WC_NO_ERR_TRACE(MP_INIT_E)) &&
  813. (ret != WC_NO_ERR_TRACE(MEMORY_E)))
  814. mp_clear(H);
  815. XFREE(H, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  816. }
  817. #ifndef WOLFSSL_MP_INVMOD_CONSTANT_TIME
  818. if (b) {
  819. if ((ret != WC_NO_ERR_TRACE(MP_INIT_E)) &&
  820. (ret != WC_NO_ERR_TRACE(MEMORY_E)))
  821. mp_forcezero(b);
  822. XFREE(b, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  823. }
  824. #endif
  825. if (buffer) {
  826. ForceZero(buffer, halfSz);
  827. XFREE(buffer, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  828. }
  829. #else /* !WOLFSSL_SMALL_STACK */
  830. if (ret != WC_NO_ERR_TRACE(MP_INIT_E)) {
  831. ForceZero(buffer, halfSz);
  832. mp_forcezero(kInv);
  833. mp_forcezero(k);
  834. #ifndef WOLFSSL_MP_INVMOD_CONSTANT_TIME
  835. mp_forcezero(b);
  836. #endif
  837. mp_clear(H);
  838. mp_clear(s);
  839. mp_clear(r);
  840. }
  841. #endif
  842. return ret;
  843. }
  844. int wc_DsaVerify(const byte* digest, const byte* sig, DsaKey* key, int* answer)
  845. {
  846. /* use sha1 by default for backwards compatibility */
  847. return wc_DsaVerify_ex(digest, WC_SHA_DIGEST_SIZE, sig, key, answer);
  848. }
  849. int wc_DsaVerify_ex(const byte* digest, word32 digestSz, const byte* sig,
  850. DsaKey* key, int* answer)
  851. {
  852. #ifdef WOLFSSL_SMALL_STACK
  853. mp_int *w = NULL;
  854. mp_int *u1 = NULL;
  855. mp_int *u2 = NULL;
  856. mp_int *v = NULL;
  857. mp_int *r = NULL;
  858. mp_int *s = NULL;
  859. #else
  860. mp_int w[1], u1[1], u2[1], v[1], r[1], s[1];
  861. #endif
  862. int ret = 0;
  863. int qSz;
  864. if (digest == NULL || sig == NULL || key == NULL || answer == NULL)
  865. return BAD_FUNC_ARG;
  866. do {
  867. #ifdef WOLFSSL_SMALL_STACK
  868. w = (mp_int *)XMALLOC(sizeof *w, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  869. u1 = (mp_int *)XMALLOC(sizeof *u1, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  870. u2 = (mp_int *)XMALLOC(sizeof *u2, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  871. v = (mp_int *)XMALLOC(sizeof *v, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  872. r = (mp_int *)XMALLOC(sizeof *r, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  873. s = (mp_int *)XMALLOC(sizeof *s, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  874. if ((w == NULL) ||
  875. (u1 == NULL) ||
  876. (u2 == NULL) ||
  877. (v == NULL) ||
  878. (r == NULL) ||
  879. (s == NULL)) {
  880. ret = MEMORY_E;
  881. break;
  882. }
  883. #endif
  884. if (mp_init_multi(w, u1, u2, v, r, s) != MP_OKAY) {
  885. ret = MP_INIT_E;
  886. break;
  887. }
  888. qSz = mp_unsigned_bin_size(&key->q);
  889. if (qSz <= 0) {
  890. ret = BAD_FUNC_ARG;
  891. break;
  892. }
  893. /* set r and s from signature */
  894. if (mp_read_unsigned_bin(r, sig, (word32)qSz) != MP_OKAY ||
  895. mp_read_unsigned_bin(s, sig + qSz, (word32)qSz) != MP_OKAY) {
  896. ret = MP_READ_E;
  897. break;
  898. }
  899. /* sanity checks */
  900. if (mp_iszero(r) == MP_YES || mp_iszero(s) == MP_YES ||
  901. mp_cmp(r, &key->q) != MP_LT || mp_cmp(s, &key->q) != MP_LT) {
  902. ret = MP_ZERO_E;
  903. break;
  904. }
  905. /* put H into u1 from sha digest */
  906. if (mp_read_unsigned_bin(u1,digest, digestSz) != MP_OKAY) {
  907. ret = MP_READ_E;
  908. break;
  909. }
  910. /* w = s invmod q */
  911. if (mp_invmod(s, &key->q, w) != MP_OKAY) {
  912. ret = MP_INVMOD_E;
  913. break;
  914. }
  915. /* u1 = (H * w) % q */
  916. if (mp_mulmod(u1, w, &key->q, u1) != MP_OKAY) {
  917. ret = MP_MULMOD_E;
  918. break;
  919. }
  920. /* u2 = (r * w) % q */
  921. if (mp_mulmod(r, w, &key->q, u2) != MP_OKAY) {
  922. ret = MP_MULMOD_E;
  923. break;
  924. }
  925. /* verify v = ((g^u1 * y^u2) mod p) mod q */
  926. if (mp_exptmod(&key->g, u1, &key->p, u1) != MP_OKAY) {
  927. ret = MP_EXPTMOD_E;
  928. break;
  929. }
  930. if (mp_exptmod(&key->y, u2, &key->p, u2) != MP_OKAY) {
  931. ret = MP_EXPTMOD_E;
  932. break;
  933. }
  934. if (mp_mulmod(u1, u2, &key->p, v) != MP_OKAY) {
  935. ret = MP_MULMOD_E;
  936. break;
  937. }
  938. if (mp_mod(v, &key->q, v) != MP_OKAY) {
  939. ret = MP_MULMOD_E;
  940. break;
  941. }
  942. /* do they match */
  943. if (mp_cmp(r, v) == MP_EQ)
  944. *answer = 1;
  945. else
  946. *answer = 0;
  947. } while (0);
  948. #ifdef WOLFSSL_SMALL_STACK
  949. if (s) {
  950. if (ret != WC_NO_ERR_TRACE(MP_INIT_E))
  951. mp_clear(s);
  952. XFREE(s, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  953. }
  954. if (r) {
  955. if (ret != WC_NO_ERR_TRACE(MP_INIT_E))
  956. mp_clear(r);
  957. XFREE(r, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  958. }
  959. if (u1) {
  960. if (ret != WC_NO_ERR_TRACE(MP_INIT_E))
  961. mp_clear(u1);
  962. XFREE(u1, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  963. }
  964. if (u2) {
  965. if (ret != WC_NO_ERR_TRACE(MP_INIT_E))
  966. mp_clear(u2);
  967. XFREE(u2, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  968. }
  969. if (w) {
  970. if (ret != WC_NO_ERR_TRACE(MP_INIT_E))
  971. mp_clear(w);
  972. XFREE(w, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  973. }
  974. if (v) {
  975. if (ret != WC_NO_ERR_TRACE(MP_INIT_E))
  976. mp_clear(v);
  977. XFREE(v, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  978. }
  979. #else
  980. if (ret != WC_NO_ERR_TRACE(MP_INIT_E)) {
  981. mp_clear(s);
  982. mp_clear(r);
  983. mp_clear(u1);
  984. mp_clear(u2);
  985. mp_clear(w);
  986. mp_clear(v);
  987. }
  988. #endif
  989. return ret;
  990. }
  991. #endif /* NO_DSA */