ecdsa.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * Elliptic curve DSA
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. /*
  47. * References:
  48. *
  49. * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
  50. */
  51. #if !defined(MBEDTLS_CONFIG_FILE)
  52. #include "mbedtls/config.h"
  53. #else
  54. #include MBEDTLS_CONFIG_FILE
  55. #endif
  56. #if defined(MBEDTLS_ECDSA_C)
  57. #include "mbedtls/ecdsa.h"
  58. #include "mbedtls/asn1write.h"
  59. #include <string.h>
  60. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  61. #include "mbedtls/hmac_drbg.h"
  62. #endif
  63. #if defined(MBEDTLS_PLATFORM_C)
  64. #include "mbedtls/platform.h"
  65. #else
  66. #include <stdlib.h>
  67. #define mbedtls_calloc calloc
  68. #define mbedtls_free free
  69. #endif
  70. #include "mbedtls/platform_util.h"
  71. /* Parameter validation macros based on platform_util.h */
  72. #define ECDSA_VALIDATE_RET( cond ) \
  73. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
  74. #define ECDSA_VALIDATE( cond ) \
  75. MBEDTLS_INTERNAL_VALIDATE( cond )
  76. #if defined(MBEDTLS_ECP_RESTARTABLE)
  77. /*
  78. * Sub-context for ecdsa_verify()
  79. */
  80. struct mbedtls_ecdsa_restart_ver
  81. {
  82. mbedtls_mpi u1, u2; /* intermediate values */
  83. enum { /* what to do next? */
  84. ecdsa_ver_init = 0, /* getting started */
  85. ecdsa_ver_muladd, /* muladd step */
  86. } state;
  87. };
  88. /*
  89. * Init verify restart sub-context
  90. */
  91. static void ecdsa_restart_ver_init( mbedtls_ecdsa_restart_ver_ctx *ctx )
  92. {
  93. mbedtls_mpi_init( &ctx->u1 );
  94. mbedtls_mpi_init( &ctx->u2 );
  95. ctx->state = ecdsa_ver_init;
  96. }
  97. /*
  98. * Free the components of a verify restart sub-context
  99. */
  100. static void ecdsa_restart_ver_free( mbedtls_ecdsa_restart_ver_ctx *ctx )
  101. {
  102. if( ctx == NULL )
  103. return;
  104. mbedtls_mpi_free( &ctx->u1 );
  105. mbedtls_mpi_free( &ctx->u2 );
  106. ecdsa_restart_ver_init( ctx );
  107. }
  108. /*
  109. * Sub-context for ecdsa_sign()
  110. */
  111. struct mbedtls_ecdsa_restart_sig
  112. {
  113. int sign_tries;
  114. int key_tries;
  115. mbedtls_mpi k; /* per-signature random */
  116. mbedtls_mpi r; /* r value */
  117. enum { /* what to do next? */
  118. ecdsa_sig_init = 0, /* getting started */
  119. ecdsa_sig_mul, /* doing ecp_mul() */
  120. ecdsa_sig_modn, /* mod N computations */
  121. } state;
  122. };
  123. /*
  124. * Init verify sign sub-context
  125. */
  126. static void ecdsa_restart_sig_init( mbedtls_ecdsa_restart_sig_ctx *ctx )
  127. {
  128. ctx->sign_tries = 0;
  129. ctx->key_tries = 0;
  130. mbedtls_mpi_init( &ctx->k );
  131. mbedtls_mpi_init( &ctx->r );
  132. ctx->state = ecdsa_sig_init;
  133. }
  134. /*
  135. * Free the components of a sign restart sub-context
  136. */
  137. static void ecdsa_restart_sig_free( mbedtls_ecdsa_restart_sig_ctx *ctx )
  138. {
  139. if( ctx == NULL )
  140. return;
  141. mbedtls_mpi_free( &ctx->k );
  142. mbedtls_mpi_free( &ctx->r );
  143. }
  144. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  145. /*
  146. * Sub-context for ecdsa_sign_det()
  147. */
  148. struct mbedtls_ecdsa_restart_det
  149. {
  150. mbedtls_hmac_drbg_context rng_ctx; /* DRBG state */
  151. enum { /* what to do next? */
  152. ecdsa_det_init = 0, /* getting started */
  153. ecdsa_det_sign, /* make signature */
  154. } state;
  155. };
  156. /*
  157. * Init verify sign_det sub-context
  158. */
  159. static void ecdsa_restart_det_init( mbedtls_ecdsa_restart_det_ctx *ctx )
  160. {
  161. mbedtls_hmac_drbg_init( &ctx->rng_ctx );
  162. ctx->state = ecdsa_det_init;
  163. }
  164. /*
  165. * Free the components of a sign_det restart sub-context
  166. */
  167. static void ecdsa_restart_det_free( mbedtls_ecdsa_restart_det_ctx *ctx )
  168. {
  169. if( ctx == NULL )
  170. return;
  171. mbedtls_hmac_drbg_free( &ctx->rng_ctx );
  172. ecdsa_restart_det_init( ctx );
  173. }
  174. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  175. #define ECDSA_RS_ECP ( rs_ctx == NULL ? NULL : &rs_ctx->ecp )
  176. /* Utility macro for checking and updating ops budget */
  177. #define ECDSA_BUDGET( ops ) \
  178. MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, ECDSA_RS_ECP, ops ) );
  179. /* Call this when entering a function that needs its own sub-context */
  180. #define ECDSA_RS_ENTER( SUB ) do { \
  181. /* reset ops count for this call if top-level */ \
  182. if( rs_ctx != NULL && rs_ctx->ecp.depth++ == 0 ) \
  183. rs_ctx->ecp.ops_done = 0; \
  184. \
  185. /* set up our own sub-context if needed */ \
  186. if( mbedtls_ecp_restart_is_enabled() && \
  187. rs_ctx != NULL && rs_ctx->SUB == NULL ) \
  188. { \
  189. rs_ctx->SUB = mbedtls_calloc( 1, sizeof( *rs_ctx->SUB ) ); \
  190. if( rs_ctx->SUB == NULL ) \
  191. return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); \
  192. \
  193. ecdsa_restart_## SUB ##_init( rs_ctx->SUB ); \
  194. } \
  195. } while( 0 )
  196. /* Call this when leaving a function that needs its own sub-context */
  197. #define ECDSA_RS_LEAVE( SUB ) do { \
  198. /* clear our sub-context when not in progress (done or error) */ \
  199. if( rs_ctx != NULL && rs_ctx->SUB != NULL && \
  200. ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) \
  201. { \
  202. ecdsa_restart_## SUB ##_free( rs_ctx->SUB ); \
  203. mbedtls_free( rs_ctx->SUB ); \
  204. rs_ctx->SUB = NULL; \
  205. } \
  206. \
  207. if( rs_ctx != NULL ) \
  208. rs_ctx->ecp.depth--; \
  209. } while( 0 )
  210. #else /* MBEDTLS_ECP_RESTARTABLE */
  211. #define ECDSA_RS_ECP NULL
  212. #define ECDSA_BUDGET( ops ) /* no-op; for compatibility */
  213. #define ECDSA_RS_ENTER( SUB ) (void) rs_ctx
  214. #define ECDSA_RS_LEAVE( SUB ) (void) rs_ctx
  215. #endif /* MBEDTLS_ECP_RESTARTABLE */
  216. #if defined(MBEDTLS_ECDSA_DETERMINISTIC) || \
  217. !defined(MBEDTLS_ECDSA_SIGN_ALT) || \
  218. !defined(MBEDTLS_ECDSA_VERIFY_ALT)
  219. /*
  220. * Derive a suitable integer for group grp from a buffer of length len
  221. * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
  222. */
  223. static int derive_mpi( const mbedtls_ecp_group *grp, mbedtls_mpi *x,
  224. const unsigned char *buf, size_t blen )
  225. {
  226. int ret;
  227. size_t n_size = ( grp->nbits + 7 ) / 8;
  228. size_t use_size = blen > n_size ? n_size : blen;
  229. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( x, buf, use_size ) );
  230. if( use_size * 8 > grp->nbits )
  231. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( x, use_size * 8 - grp->nbits ) );
  232. /* While at it, reduce modulo N */
  233. if( mbedtls_mpi_cmp_mpi( x, &grp->N ) >= 0 )
  234. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( x, x, &grp->N ) );
  235. cleanup:
  236. return( ret );
  237. }
  238. #endif /* ECDSA_DETERMINISTIC || !ECDSA_SIGN_ALT || !ECDSA_VERIFY_ALT */
  239. #if !defined(MBEDTLS_ECDSA_SIGN_ALT)
  240. /*
  241. * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
  242. * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
  243. */
  244. static int ecdsa_sign_restartable( mbedtls_ecp_group *grp,
  245. mbedtls_mpi *r, mbedtls_mpi *s,
  246. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  247. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  248. int (*f_rng_blind)(void *, unsigned char *, size_t),
  249. void *p_rng_blind,
  250. mbedtls_ecdsa_restart_ctx *rs_ctx )
  251. {
  252. int ret, key_tries, sign_tries;
  253. int *p_sign_tries = &sign_tries, *p_key_tries = &key_tries;
  254. mbedtls_ecp_point R;
  255. mbedtls_mpi k, e, t;
  256. mbedtls_mpi *pk = &k, *pr = r;
  257. /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
  258. if( grp->N.p == NULL )
  259. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  260. /* Make sure d is in range 1..n-1 */
  261. if( mbedtls_mpi_cmp_int( d, 1 ) < 0 || mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
  262. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  263. mbedtls_ecp_point_init( &R );
  264. mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t );
  265. ECDSA_RS_ENTER( sig );
  266. #if defined(MBEDTLS_ECP_RESTARTABLE)
  267. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  268. {
  269. /* redirect to our context */
  270. p_sign_tries = &rs_ctx->sig->sign_tries;
  271. p_key_tries = &rs_ctx->sig->key_tries;
  272. pk = &rs_ctx->sig->k;
  273. pr = &rs_ctx->sig->r;
  274. /* jump to current step */
  275. if( rs_ctx->sig->state == ecdsa_sig_mul )
  276. goto mul;
  277. if( rs_ctx->sig->state == ecdsa_sig_modn )
  278. goto modn;
  279. }
  280. #endif /* MBEDTLS_ECP_RESTARTABLE */
  281. *p_sign_tries = 0;
  282. do
  283. {
  284. if( (*p_sign_tries)++ > 10 )
  285. {
  286. ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
  287. goto cleanup;
  288. }
  289. /*
  290. * Steps 1-3: generate a suitable ephemeral keypair
  291. * and set r = xR mod n
  292. */
  293. *p_key_tries = 0;
  294. do
  295. {
  296. if( (*p_key_tries)++ > 10 )
  297. {
  298. ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
  299. goto cleanup;
  300. }
  301. MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, pk, f_rng, p_rng ) );
  302. #if defined(MBEDTLS_ECP_RESTARTABLE)
  303. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  304. rs_ctx->sig->state = ecdsa_sig_mul;
  305. mul:
  306. #endif
  307. MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &R, pk, &grp->G,
  308. f_rng_blind,
  309. p_rng_blind,
  310. ECDSA_RS_ECP ) );
  311. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pr, &R.X, &grp->N ) );
  312. }
  313. while( mbedtls_mpi_cmp_int( pr, 0 ) == 0 );
  314. #if defined(MBEDTLS_ECP_RESTARTABLE)
  315. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  316. rs_ctx->sig->state = ecdsa_sig_modn;
  317. modn:
  318. #endif
  319. /*
  320. * Accounting for everything up to the end of the loop
  321. * (step 6, but checking now avoids saving e and t)
  322. */
  323. ECDSA_BUDGET( MBEDTLS_ECP_OPS_INV + 4 );
  324. /*
  325. * Step 5: derive MPI from hashed message
  326. */
  327. MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
  328. /*
  329. * Generate a random value to blind inv_mod in next step,
  330. * avoiding a potential timing leak.
  331. */
  332. MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, &t, f_rng_blind,
  333. p_rng_blind ) );
  334. /*
  335. * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
  336. */
  337. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, pr, d ) );
  338. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) );
  339. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) );
  340. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pk, pk, &t ) );
  341. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pk, pk, &grp->N ) );
  342. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, pk, &grp->N ) );
  343. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) );
  344. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) );
  345. }
  346. while( mbedtls_mpi_cmp_int( s, 0 ) == 0 );
  347. #if defined(MBEDTLS_ECP_RESTARTABLE)
  348. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  349. mbedtls_mpi_copy( r, pr );
  350. #endif
  351. cleanup:
  352. mbedtls_ecp_point_free( &R );
  353. mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t );
  354. ECDSA_RS_LEAVE( sig );
  355. return( ret );
  356. }
  357. /*
  358. * Compute ECDSA signature of a hashed message
  359. */
  360. int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  361. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  362. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  363. {
  364. ECDSA_VALIDATE_RET( grp != NULL );
  365. ECDSA_VALIDATE_RET( r != NULL );
  366. ECDSA_VALIDATE_RET( s != NULL );
  367. ECDSA_VALIDATE_RET( d != NULL );
  368. ECDSA_VALIDATE_RET( f_rng != NULL );
  369. ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
  370. /* Use the same RNG for both blinding and ephemeral key generation */
  371. return( ecdsa_sign_restartable( grp, r, s, d, buf, blen,
  372. f_rng, p_rng, f_rng, p_rng, NULL ) );
  373. }
  374. #endif /* !MBEDTLS_ECDSA_SIGN_ALT */
  375. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  376. /*
  377. * Deterministic signature wrapper
  378. */
  379. static int ecdsa_sign_det_restartable( mbedtls_ecp_group *grp,
  380. mbedtls_mpi *r, mbedtls_mpi *s,
  381. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  382. mbedtls_md_type_t md_alg,
  383. int (*f_rng_blind)(void *, unsigned char *, size_t),
  384. void *p_rng_blind,
  385. mbedtls_ecdsa_restart_ctx *rs_ctx )
  386. {
  387. int ret;
  388. mbedtls_hmac_drbg_context rng_ctx;
  389. mbedtls_hmac_drbg_context *p_rng = &rng_ctx;
  390. unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
  391. size_t grp_len = ( grp->nbits + 7 ) / 8;
  392. const mbedtls_md_info_t *md_info;
  393. mbedtls_mpi h;
  394. if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
  395. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  396. mbedtls_mpi_init( &h );
  397. mbedtls_hmac_drbg_init( &rng_ctx );
  398. ECDSA_RS_ENTER( det );
  399. #if defined(MBEDTLS_ECP_RESTARTABLE)
  400. if( rs_ctx != NULL && rs_ctx->det != NULL )
  401. {
  402. /* redirect to our context */
  403. p_rng = &rs_ctx->det->rng_ctx;
  404. /* jump to current step */
  405. if( rs_ctx->det->state == ecdsa_det_sign )
  406. goto sign;
  407. }
  408. #endif /* MBEDTLS_ECP_RESTARTABLE */
  409. /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
  410. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) );
  411. MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
  412. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) );
  413. mbedtls_hmac_drbg_seed_buf( p_rng, md_info, data, 2 * grp_len );
  414. #if defined(MBEDTLS_ECP_RESTARTABLE)
  415. if( rs_ctx != NULL && rs_ctx->det != NULL )
  416. rs_ctx->det->state = ecdsa_det_sign;
  417. sign:
  418. #endif
  419. #if defined(MBEDTLS_ECDSA_SIGN_ALT)
  420. ret = mbedtls_ecdsa_sign( grp, r, s, d, buf, blen,
  421. mbedtls_hmac_drbg_random, p_rng );
  422. #else
  423. if( f_rng_blind != NULL )
  424. ret = ecdsa_sign_restartable( grp, r, s, d, buf, blen,
  425. mbedtls_hmac_drbg_random, p_rng,
  426. f_rng_blind, p_rng_blind, rs_ctx );
  427. else
  428. {
  429. mbedtls_hmac_drbg_context *p_rng_blind_det;
  430. #if !defined(MBEDTLS_ECP_RESTARTABLE)
  431. /*
  432. * To avoid reusing rng_ctx and risking incorrect behavior we seed a
  433. * second HMAC-DRBG with the same seed. We also apply a label to avoid
  434. * reusing the bits of the ephemeral key for blinding and eliminate the
  435. * risk that they leak this way.
  436. */
  437. const char* blind_label = "BLINDING CONTEXT";
  438. mbedtls_hmac_drbg_context rng_ctx_blind;
  439. mbedtls_hmac_drbg_init( &rng_ctx_blind );
  440. p_rng_blind_det = &rng_ctx_blind;
  441. mbedtls_hmac_drbg_seed_buf( p_rng_blind_det, md_info,
  442. data, 2 * grp_len );
  443. ret = mbedtls_hmac_drbg_update_ret( p_rng_blind_det,
  444. (const unsigned char*) blind_label,
  445. strlen( blind_label ) );
  446. if( ret != 0 )
  447. {
  448. mbedtls_hmac_drbg_free( &rng_ctx_blind );
  449. goto cleanup;
  450. }
  451. #else
  452. /*
  453. * In the case of restartable computations we would either need to store
  454. * the second RNG in the restart context too or set it up at every
  455. * restart. The first option would penalize the correct application of
  456. * the function and the second would defeat the purpose of the
  457. * restartable feature.
  458. *
  459. * Therefore in this case we reuse the original RNG. This comes with the
  460. * price that the resulting signature might not be a valid deterministic
  461. * ECDSA signature with a very low probability (same magnitude as
  462. * successfully guessing the private key). However even then it is still
  463. * a valid ECDSA signature.
  464. */
  465. p_rng_blind_det = p_rng;
  466. #endif /* MBEDTLS_ECP_RESTARTABLE */
  467. /*
  468. * Since the output of the RNGs is always the same for the same key and
  469. * message, this limits the efficiency of blinding and leaks information
  470. * through side channels. After mbedtls_ecdsa_sign_det() is removed NULL
  471. * won't be a valid value for f_rng_blind anymore. Therefore it should
  472. * be checked by the caller and this branch and check can be removed.
  473. */
  474. ret = ecdsa_sign_restartable( grp, r, s, d, buf, blen,
  475. mbedtls_hmac_drbg_random, p_rng,
  476. mbedtls_hmac_drbg_random, p_rng_blind_det,
  477. rs_ctx );
  478. #if !defined(MBEDTLS_ECP_RESTARTABLE)
  479. mbedtls_hmac_drbg_free( &rng_ctx_blind );
  480. #endif
  481. }
  482. #endif /* MBEDTLS_ECDSA_SIGN_ALT */
  483. cleanup:
  484. mbedtls_hmac_drbg_free( &rng_ctx );
  485. mbedtls_mpi_free( &h );
  486. ECDSA_RS_LEAVE( det );
  487. return( ret );
  488. }
  489. /*
  490. * Deterministic signature wrappers
  491. */
  492. int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r,
  493. mbedtls_mpi *s, const mbedtls_mpi *d,
  494. const unsigned char *buf, size_t blen,
  495. mbedtls_md_type_t md_alg )
  496. {
  497. ECDSA_VALIDATE_RET( grp != NULL );
  498. ECDSA_VALIDATE_RET( r != NULL );
  499. ECDSA_VALIDATE_RET( s != NULL );
  500. ECDSA_VALIDATE_RET( d != NULL );
  501. ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
  502. return( ecdsa_sign_det_restartable( grp, r, s, d, buf, blen, md_alg,
  503. NULL, NULL, NULL ) );
  504. }
  505. int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r,
  506. mbedtls_mpi *s, const mbedtls_mpi *d,
  507. const unsigned char *buf, size_t blen,
  508. mbedtls_md_type_t md_alg,
  509. int (*f_rng_blind)(void *, unsigned char *,
  510. size_t),
  511. void *p_rng_blind )
  512. {
  513. ECDSA_VALIDATE_RET( grp != NULL );
  514. ECDSA_VALIDATE_RET( r != NULL );
  515. ECDSA_VALIDATE_RET( s != NULL );
  516. ECDSA_VALIDATE_RET( d != NULL );
  517. ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
  518. ECDSA_VALIDATE_RET( f_rng_blind != NULL );
  519. return( ecdsa_sign_det_restartable( grp, r, s, d, buf, blen, md_alg,
  520. f_rng_blind, p_rng_blind, NULL ) );
  521. }
  522. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  523. #if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
  524. /*
  525. * Verify ECDSA signature of hashed message (SEC1 4.1.4)
  526. * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
  527. */
  528. static int ecdsa_verify_restartable( mbedtls_ecp_group *grp,
  529. const unsigned char *buf, size_t blen,
  530. const mbedtls_ecp_point *Q,
  531. const mbedtls_mpi *r, const mbedtls_mpi *s,
  532. mbedtls_ecdsa_restart_ctx *rs_ctx )
  533. {
  534. int ret;
  535. mbedtls_mpi e, s_inv, u1, u2;
  536. mbedtls_ecp_point R;
  537. mbedtls_mpi *pu1 = &u1, *pu2 = &u2;
  538. mbedtls_ecp_point_init( &R );
  539. mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv );
  540. mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
  541. /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
  542. if( grp->N.p == NULL )
  543. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  544. ECDSA_RS_ENTER( ver );
  545. #if defined(MBEDTLS_ECP_RESTARTABLE)
  546. if( rs_ctx != NULL && rs_ctx->ver != NULL )
  547. {
  548. /* redirect to our context */
  549. pu1 = &rs_ctx->ver->u1;
  550. pu2 = &rs_ctx->ver->u2;
  551. /* jump to current step */
  552. if( rs_ctx->ver->state == ecdsa_ver_muladd )
  553. goto muladd;
  554. }
  555. #endif /* MBEDTLS_ECP_RESTARTABLE */
  556. /*
  557. * Step 1: make sure r and s are in range 1..n-1
  558. */
  559. if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 ||
  560. mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 )
  561. {
  562. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  563. goto cleanup;
  564. }
  565. /*
  566. * Step 3: derive MPI from hashed message
  567. */
  568. MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
  569. /*
  570. * Step 4: u1 = e / s mod n, u2 = r / s mod n
  571. */
  572. ECDSA_BUDGET( MBEDTLS_ECP_OPS_CHK + MBEDTLS_ECP_OPS_INV + 2 );
  573. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) );
  574. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu1, &e, &s_inv ) );
  575. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu1, pu1, &grp->N ) );
  576. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu2, r, &s_inv ) );
  577. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu2, pu2, &grp->N ) );
  578. #if defined(MBEDTLS_ECP_RESTARTABLE)
  579. if( rs_ctx != NULL && rs_ctx->ver != NULL )
  580. rs_ctx->ver->state = ecdsa_ver_muladd;
  581. muladd:
  582. #endif
  583. /*
  584. * Step 5: R = u1 G + u2 Q
  585. */
  586. MBEDTLS_MPI_CHK( mbedtls_ecp_muladd_restartable( grp,
  587. &R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP ) );
  588. if( mbedtls_ecp_is_zero( &R ) )
  589. {
  590. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  591. goto cleanup;
  592. }
  593. /*
  594. * Step 6: convert xR to an integer (no-op)
  595. * Step 7: reduce xR mod n (gives v)
  596. */
  597. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
  598. /*
  599. * Step 8: check if v (that is, R.X) is equal to r
  600. */
  601. if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 )
  602. {
  603. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  604. goto cleanup;
  605. }
  606. cleanup:
  607. mbedtls_ecp_point_free( &R );
  608. mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv );
  609. mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
  610. ECDSA_RS_LEAVE( ver );
  611. return( ret );
  612. }
  613. /*
  614. * Verify ECDSA signature of hashed message
  615. */
  616. int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
  617. const unsigned char *buf, size_t blen,
  618. const mbedtls_ecp_point *Q,
  619. const mbedtls_mpi *r,
  620. const mbedtls_mpi *s)
  621. {
  622. ECDSA_VALIDATE_RET( grp != NULL );
  623. ECDSA_VALIDATE_RET( Q != NULL );
  624. ECDSA_VALIDATE_RET( r != NULL );
  625. ECDSA_VALIDATE_RET( s != NULL );
  626. ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
  627. return( ecdsa_verify_restartable( grp, buf, blen, Q, r, s, NULL ) );
  628. }
  629. #endif /* !MBEDTLS_ECDSA_VERIFY_ALT */
  630. /*
  631. * Convert a signature (given by context) to ASN.1
  632. */
  633. static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
  634. unsigned char *sig, size_t *slen )
  635. {
  636. int ret;
  637. unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
  638. unsigned char *p = buf + sizeof( buf );
  639. size_t len = 0;
  640. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) );
  641. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) );
  642. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) );
  643. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf,
  644. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  645. memcpy( sig, p, len );
  646. *slen = len;
  647. return( 0 );
  648. }
  649. /*
  650. * Compute and write signature
  651. */
  652. int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
  653. mbedtls_md_type_t md_alg,
  654. const unsigned char *hash, size_t hlen,
  655. unsigned char *sig, size_t *slen,
  656. int (*f_rng)(void *, unsigned char *, size_t),
  657. void *p_rng,
  658. mbedtls_ecdsa_restart_ctx *rs_ctx )
  659. {
  660. int ret;
  661. mbedtls_mpi r, s;
  662. ECDSA_VALIDATE_RET( ctx != NULL );
  663. ECDSA_VALIDATE_RET( hash != NULL );
  664. ECDSA_VALIDATE_RET( sig != NULL );
  665. ECDSA_VALIDATE_RET( slen != NULL );
  666. mbedtls_mpi_init( &r );
  667. mbedtls_mpi_init( &s );
  668. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  669. MBEDTLS_MPI_CHK( ecdsa_sign_det_restartable( &ctx->grp, &r, &s, &ctx->d,
  670. hash, hlen, md_alg, f_rng,
  671. p_rng, rs_ctx ) );
  672. #else
  673. (void) md_alg;
  674. #if defined(MBEDTLS_ECDSA_SIGN_ALT)
  675. (void) rs_ctx;
  676. MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d,
  677. hash, hlen, f_rng, p_rng ) );
  678. #else
  679. /* Use the same RNG for both blinding and ephemeral key generation */
  680. MBEDTLS_MPI_CHK( ecdsa_sign_restartable( &ctx->grp, &r, &s, &ctx->d,
  681. hash, hlen, f_rng, p_rng, f_rng,
  682. p_rng, rs_ctx ) );
  683. #endif /* MBEDTLS_ECDSA_SIGN_ALT */
  684. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  685. MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
  686. cleanup:
  687. mbedtls_mpi_free( &r );
  688. mbedtls_mpi_free( &s );
  689. return( ret );
  690. }
  691. /*
  692. * Compute and write signature
  693. */
  694. int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
  695. mbedtls_md_type_t md_alg,
  696. const unsigned char *hash, size_t hlen,
  697. unsigned char *sig, size_t *slen,
  698. int (*f_rng)(void *, unsigned char *, size_t),
  699. void *p_rng )
  700. {
  701. ECDSA_VALIDATE_RET( ctx != NULL );
  702. ECDSA_VALIDATE_RET( hash != NULL );
  703. ECDSA_VALIDATE_RET( sig != NULL );
  704. ECDSA_VALIDATE_RET( slen != NULL );
  705. return( mbedtls_ecdsa_write_signature_restartable(
  706. ctx, md_alg, hash, hlen, sig, slen, f_rng, p_rng, NULL ) );
  707. }
  708. #if !defined(MBEDTLS_DEPRECATED_REMOVED) && \
  709. defined(MBEDTLS_ECDSA_DETERMINISTIC)
  710. int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
  711. const unsigned char *hash, size_t hlen,
  712. unsigned char *sig, size_t *slen,
  713. mbedtls_md_type_t md_alg )
  714. {
  715. ECDSA_VALIDATE_RET( ctx != NULL );
  716. ECDSA_VALIDATE_RET( hash != NULL );
  717. ECDSA_VALIDATE_RET( sig != NULL );
  718. ECDSA_VALIDATE_RET( slen != NULL );
  719. return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
  720. NULL, NULL ) );
  721. }
  722. #endif
  723. /*
  724. * Read and check signature
  725. */
  726. int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
  727. const unsigned char *hash, size_t hlen,
  728. const unsigned char *sig, size_t slen )
  729. {
  730. ECDSA_VALIDATE_RET( ctx != NULL );
  731. ECDSA_VALIDATE_RET( hash != NULL );
  732. ECDSA_VALIDATE_RET( sig != NULL );
  733. return( mbedtls_ecdsa_read_signature_restartable(
  734. ctx, hash, hlen, sig, slen, NULL ) );
  735. }
  736. /*
  737. * Restartable read and check signature
  738. */
  739. int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
  740. const unsigned char *hash, size_t hlen,
  741. const unsigned char *sig, size_t slen,
  742. mbedtls_ecdsa_restart_ctx *rs_ctx )
  743. {
  744. int ret;
  745. unsigned char *p = (unsigned char *) sig;
  746. const unsigned char *end = sig + slen;
  747. size_t len;
  748. mbedtls_mpi r, s;
  749. ECDSA_VALIDATE_RET( ctx != NULL );
  750. ECDSA_VALIDATE_RET( hash != NULL );
  751. ECDSA_VALIDATE_RET( sig != NULL );
  752. mbedtls_mpi_init( &r );
  753. mbedtls_mpi_init( &s );
  754. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  755. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  756. {
  757. ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  758. goto cleanup;
  759. }
  760. if( p + len != end )
  761. {
  762. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA +
  763. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
  764. goto cleanup;
  765. }
  766. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 ||
  767. ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 )
  768. {
  769. ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  770. goto cleanup;
  771. }
  772. #if defined(MBEDTLS_ECDSA_VERIFY_ALT)
  773. (void) rs_ctx;
  774. if( ( ret = mbedtls_ecdsa_verify( &ctx->grp, hash, hlen,
  775. &ctx->Q, &r, &s ) ) != 0 )
  776. goto cleanup;
  777. #else
  778. if( ( ret = ecdsa_verify_restartable( &ctx->grp, hash, hlen,
  779. &ctx->Q, &r, &s, rs_ctx ) ) != 0 )
  780. goto cleanup;
  781. #endif /* MBEDTLS_ECDSA_VERIFY_ALT */
  782. /* At this point we know that the buffer starts with a valid signature.
  783. * Return 0 if the buffer just contains the signature, and a specific
  784. * error code if the valid signature is followed by more data. */
  785. if( p != end )
  786. ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
  787. cleanup:
  788. mbedtls_mpi_free( &r );
  789. mbedtls_mpi_free( &s );
  790. return( ret );
  791. }
  792. #if !defined(MBEDTLS_ECDSA_GENKEY_ALT)
  793. /*
  794. * Generate key pair
  795. */
  796. int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
  797. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  798. {
  799. int ret = 0;
  800. ECDSA_VALIDATE_RET( ctx != NULL );
  801. ECDSA_VALIDATE_RET( f_rng != NULL );
  802. ret = mbedtls_ecp_group_load( &ctx->grp, gid );
  803. if( ret != 0 )
  804. return( ret );
  805. return( mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d,
  806. &ctx->Q, f_rng, p_rng ) );
  807. }
  808. #endif /* !MBEDTLS_ECDSA_GENKEY_ALT */
  809. /*
  810. * Set context from an mbedtls_ecp_keypair
  811. */
  812. int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key )
  813. {
  814. int ret;
  815. ECDSA_VALIDATE_RET( ctx != NULL );
  816. ECDSA_VALIDATE_RET( key != NULL );
  817. if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
  818. ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ||
  819. ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
  820. {
  821. mbedtls_ecdsa_free( ctx );
  822. }
  823. return( ret );
  824. }
  825. /*
  826. * Initialize context
  827. */
  828. void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx )
  829. {
  830. ECDSA_VALIDATE( ctx != NULL );
  831. mbedtls_ecp_keypair_init( ctx );
  832. }
  833. /*
  834. * Free context
  835. */
  836. void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx )
  837. {
  838. if( ctx == NULL )
  839. return;
  840. mbedtls_ecp_keypair_free( ctx );
  841. }
  842. #if defined(MBEDTLS_ECP_RESTARTABLE)
  843. /*
  844. * Initialize a restart context
  845. */
  846. void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx )
  847. {
  848. ECDSA_VALIDATE( ctx != NULL );
  849. mbedtls_ecp_restart_init( &ctx->ecp );
  850. ctx->ver = NULL;
  851. ctx->sig = NULL;
  852. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  853. ctx->det = NULL;
  854. #endif
  855. }
  856. /*
  857. * Free the components of a restart context
  858. */
  859. void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx )
  860. {
  861. if( ctx == NULL )
  862. return;
  863. mbedtls_ecp_restart_free( &ctx->ecp );
  864. ecdsa_restart_ver_free( ctx->ver );
  865. mbedtls_free( ctx->ver );
  866. ctx->ver = NULL;
  867. ecdsa_restart_sig_free( ctx->sig );
  868. mbedtls_free( ctx->sig );
  869. ctx->sig = NULL;
  870. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  871. ecdsa_restart_det_free( ctx->det );
  872. mbedtls_free( ctx->det );
  873. ctx->det = NULL;
  874. #endif
  875. }
  876. #endif /* MBEDTLS_ECP_RESTARTABLE */
  877. #endif /* MBEDTLS_ECDSA_C */