2
0

ecdsa.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. /*
  217. * Derive a suitable integer for group grp from a buffer of length len
  218. * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
  219. */
  220. static int derive_mpi( const mbedtls_ecp_group *grp, mbedtls_mpi *x,
  221. const unsigned char *buf, size_t blen )
  222. {
  223. int ret;
  224. size_t n_size = ( grp->nbits + 7 ) / 8;
  225. size_t use_size = blen > n_size ? n_size : blen;
  226. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( x, buf, use_size ) );
  227. if( use_size * 8 > grp->nbits )
  228. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( x, use_size * 8 - grp->nbits ) );
  229. /* While at it, reduce modulo N */
  230. if( mbedtls_mpi_cmp_mpi( x, &grp->N ) >= 0 )
  231. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( x, x, &grp->N ) );
  232. cleanup:
  233. return( ret );
  234. }
  235. #if !defined(MBEDTLS_ECDSA_SIGN_ALT)
  236. /*
  237. * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
  238. * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
  239. */
  240. static int ecdsa_sign_restartable( mbedtls_ecp_group *grp,
  241. mbedtls_mpi *r, mbedtls_mpi *s,
  242. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  243. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  244. int (*f_rng_blind)(void *, unsigned char *, size_t),
  245. void *p_rng_blind,
  246. mbedtls_ecdsa_restart_ctx *rs_ctx )
  247. {
  248. int ret, key_tries, sign_tries;
  249. int *p_sign_tries = &sign_tries, *p_key_tries = &key_tries;
  250. mbedtls_ecp_point R;
  251. mbedtls_mpi k, e, t;
  252. mbedtls_mpi *pk = &k, *pr = r;
  253. /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
  254. if( grp->N.p == NULL )
  255. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  256. /* Make sure d is in range 1..n-1 */
  257. if( mbedtls_mpi_cmp_int( d, 1 ) < 0 || mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
  258. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  259. mbedtls_ecp_point_init( &R );
  260. mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t );
  261. ECDSA_RS_ENTER( sig );
  262. #if defined(MBEDTLS_ECP_RESTARTABLE)
  263. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  264. {
  265. /* redirect to our context */
  266. p_sign_tries = &rs_ctx->sig->sign_tries;
  267. p_key_tries = &rs_ctx->sig->key_tries;
  268. pk = &rs_ctx->sig->k;
  269. pr = &rs_ctx->sig->r;
  270. /* jump to current step */
  271. if( rs_ctx->sig->state == ecdsa_sig_mul )
  272. goto mul;
  273. if( rs_ctx->sig->state == ecdsa_sig_modn )
  274. goto modn;
  275. }
  276. #endif /* MBEDTLS_ECP_RESTARTABLE */
  277. *p_sign_tries = 0;
  278. do
  279. {
  280. if( (*p_sign_tries)++ > 10 )
  281. {
  282. ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
  283. goto cleanup;
  284. }
  285. /*
  286. * Steps 1-3: generate a suitable ephemeral keypair
  287. * and set r = xR mod n
  288. */
  289. *p_key_tries = 0;
  290. do
  291. {
  292. if( (*p_key_tries)++ > 10 )
  293. {
  294. ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
  295. goto cleanup;
  296. }
  297. MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, pk, f_rng, p_rng ) );
  298. #if defined(MBEDTLS_ECP_RESTARTABLE)
  299. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  300. rs_ctx->sig->state = ecdsa_sig_mul;
  301. mul:
  302. #endif
  303. MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &R, pk, &grp->G,
  304. f_rng_blind,
  305. p_rng_blind,
  306. ECDSA_RS_ECP ) );
  307. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pr, &R.X, &grp->N ) );
  308. }
  309. while( mbedtls_mpi_cmp_int( pr, 0 ) == 0 );
  310. #if defined(MBEDTLS_ECP_RESTARTABLE)
  311. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  312. rs_ctx->sig->state = ecdsa_sig_modn;
  313. modn:
  314. #endif
  315. /*
  316. * Accounting for everything up to the end of the loop
  317. * (step 6, but checking now avoids saving e and t)
  318. */
  319. ECDSA_BUDGET( MBEDTLS_ECP_OPS_INV + 4 );
  320. /*
  321. * Step 5: derive MPI from hashed message
  322. */
  323. MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
  324. /*
  325. * Generate a random value to blind inv_mod in next step,
  326. * avoiding a potential timing leak.
  327. */
  328. MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, &t, f_rng_blind,
  329. p_rng_blind ) );
  330. /*
  331. * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
  332. */
  333. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, pr, d ) );
  334. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) );
  335. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) );
  336. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pk, pk, &t ) );
  337. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pk, pk, &grp->N ) );
  338. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, pk, &grp->N ) );
  339. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) );
  340. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) );
  341. }
  342. while( mbedtls_mpi_cmp_int( s, 0 ) == 0 );
  343. #if defined(MBEDTLS_ECP_RESTARTABLE)
  344. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  345. mbedtls_mpi_copy( r, pr );
  346. #endif
  347. cleanup:
  348. mbedtls_ecp_point_free( &R );
  349. mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t );
  350. ECDSA_RS_LEAVE( sig );
  351. return( ret );
  352. }
  353. /*
  354. * Compute ECDSA signature of a hashed message
  355. */
  356. int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  357. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  358. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  359. {
  360. ECDSA_VALIDATE_RET( grp != NULL );
  361. ECDSA_VALIDATE_RET( r != NULL );
  362. ECDSA_VALIDATE_RET( s != NULL );
  363. ECDSA_VALIDATE_RET( d != NULL );
  364. ECDSA_VALIDATE_RET( f_rng != NULL );
  365. ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
  366. /* Use the same RNG for both blinding and ephemeral key generation */
  367. return( ecdsa_sign_restartable( grp, r, s, d, buf, blen,
  368. f_rng, p_rng, f_rng, p_rng, NULL ) );
  369. }
  370. #endif /* !MBEDTLS_ECDSA_SIGN_ALT */
  371. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  372. /*
  373. * Deterministic signature wrapper
  374. */
  375. static int ecdsa_sign_det_restartable( mbedtls_ecp_group *grp,
  376. mbedtls_mpi *r, mbedtls_mpi *s,
  377. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  378. mbedtls_md_type_t md_alg,
  379. int (*f_rng_blind)(void *, unsigned char *, size_t),
  380. void *p_rng_blind,
  381. mbedtls_ecdsa_restart_ctx *rs_ctx )
  382. {
  383. int ret;
  384. mbedtls_hmac_drbg_context rng_ctx;
  385. mbedtls_hmac_drbg_context *p_rng = &rng_ctx;
  386. unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
  387. size_t grp_len = ( grp->nbits + 7 ) / 8;
  388. const mbedtls_md_info_t *md_info;
  389. mbedtls_mpi h;
  390. if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
  391. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  392. mbedtls_mpi_init( &h );
  393. mbedtls_hmac_drbg_init( &rng_ctx );
  394. ECDSA_RS_ENTER( det );
  395. #if defined(MBEDTLS_ECP_RESTARTABLE)
  396. if( rs_ctx != NULL && rs_ctx->det != NULL )
  397. {
  398. /* redirect to our context */
  399. p_rng = &rs_ctx->det->rng_ctx;
  400. /* jump to current step */
  401. if( rs_ctx->det->state == ecdsa_det_sign )
  402. goto sign;
  403. }
  404. #endif /* MBEDTLS_ECP_RESTARTABLE */
  405. /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
  406. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) );
  407. MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
  408. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) );
  409. mbedtls_hmac_drbg_seed_buf( p_rng, md_info, data, 2 * grp_len );
  410. #if defined(MBEDTLS_ECP_RESTARTABLE)
  411. if( rs_ctx != NULL && rs_ctx->det != NULL )
  412. rs_ctx->det->state = ecdsa_det_sign;
  413. sign:
  414. #endif
  415. #if defined(MBEDTLS_ECDSA_SIGN_ALT)
  416. ret = mbedtls_ecdsa_sign( grp, r, s, d, buf, blen,
  417. mbedtls_hmac_drbg_random, p_rng );
  418. #else
  419. if( f_rng_blind != NULL )
  420. ret = ecdsa_sign_restartable( grp, r, s, d, buf, blen,
  421. mbedtls_hmac_drbg_random, p_rng,
  422. f_rng_blind, p_rng_blind, rs_ctx );
  423. else
  424. {
  425. mbedtls_hmac_drbg_context *p_rng_blind_det;
  426. #if !defined(MBEDTLS_ECP_RESTARTABLE)
  427. /*
  428. * To avoid reusing rng_ctx and risking incorrect behavior we seed a
  429. * second HMAC-DRBG with the same seed. We also apply a label to avoid
  430. * reusing the bits of the ephemeral key for blinding and eliminate the
  431. * risk that they leak this way.
  432. */
  433. const char* blind_label = "BLINDING CONTEXT";
  434. mbedtls_hmac_drbg_context rng_ctx_blind;
  435. mbedtls_hmac_drbg_init( &rng_ctx_blind );
  436. p_rng_blind_det = &rng_ctx_blind;
  437. mbedtls_hmac_drbg_seed_buf( p_rng_blind_det, md_info,
  438. data, 2 * grp_len );
  439. ret = mbedtls_hmac_drbg_update_ret( p_rng_blind_det,
  440. (const unsigned char*) blind_label,
  441. strlen( blind_label ) );
  442. if( ret != 0 )
  443. {
  444. mbedtls_hmac_drbg_free( &rng_ctx_blind );
  445. goto cleanup;
  446. }
  447. #else
  448. /*
  449. * In the case of restartable computations we would either need to store
  450. * the second RNG in the restart context too or set it up at every
  451. * restart. The first option would penalize the correct application of
  452. * the function and the second would defeat the purpose of the
  453. * restartable feature.
  454. *
  455. * Therefore in this case we reuse the original RNG. This comes with the
  456. * price that the resulting signature might not be a valid deterministic
  457. * ECDSA signature with a very low probability (same magnitude as
  458. * successfully guessing the private key). However even then it is still
  459. * a valid ECDSA signature.
  460. */
  461. p_rng_blind_det = p_rng;
  462. #endif /* MBEDTLS_ECP_RESTARTABLE */
  463. /*
  464. * Since the output of the RNGs is always the same for the same key and
  465. * message, this limits the efficiency of blinding and leaks information
  466. * through side channels. After mbedtls_ecdsa_sign_det() is removed NULL
  467. * won't be a valid value for f_rng_blind anymore. Therefore it should
  468. * be checked by the caller and this branch and check can be removed.
  469. */
  470. ret = ecdsa_sign_restartable( grp, r, s, d, buf, blen,
  471. mbedtls_hmac_drbg_random, p_rng,
  472. mbedtls_hmac_drbg_random, p_rng_blind_det,
  473. rs_ctx );
  474. #if !defined(MBEDTLS_ECP_RESTARTABLE)
  475. mbedtls_hmac_drbg_free( &rng_ctx_blind );
  476. #endif
  477. }
  478. #endif /* MBEDTLS_ECDSA_SIGN_ALT */
  479. cleanup:
  480. mbedtls_hmac_drbg_free( &rng_ctx );
  481. mbedtls_mpi_free( &h );
  482. ECDSA_RS_LEAVE( det );
  483. return( ret );
  484. }
  485. /*
  486. * Deterministic signature wrappers
  487. */
  488. int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r,
  489. mbedtls_mpi *s, const mbedtls_mpi *d,
  490. const unsigned char *buf, size_t blen,
  491. mbedtls_md_type_t md_alg )
  492. {
  493. ECDSA_VALIDATE_RET( grp != NULL );
  494. ECDSA_VALIDATE_RET( r != NULL );
  495. ECDSA_VALIDATE_RET( s != NULL );
  496. ECDSA_VALIDATE_RET( d != NULL );
  497. ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
  498. return( ecdsa_sign_det_restartable( grp, r, s, d, buf, blen, md_alg,
  499. NULL, NULL, NULL ) );
  500. }
  501. int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r,
  502. mbedtls_mpi *s, const mbedtls_mpi *d,
  503. const unsigned char *buf, size_t blen,
  504. mbedtls_md_type_t md_alg,
  505. int (*f_rng_blind)(void *, unsigned char *,
  506. size_t),
  507. void *p_rng_blind )
  508. {
  509. ECDSA_VALIDATE_RET( grp != NULL );
  510. ECDSA_VALIDATE_RET( r != NULL );
  511. ECDSA_VALIDATE_RET( s != NULL );
  512. ECDSA_VALIDATE_RET( d != NULL );
  513. ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
  514. ECDSA_VALIDATE_RET( f_rng_blind != NULL );
  515. return( ecdsa_sign_det_restartable( grp, r, s, d, buf, blen, md_alg,
  516. f_rng_blind, p_rng_blind, NULL ) );
  517. }
  518. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  519. #if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
  520. /*
  521. * Verify ECDSA signature of hashed message (SEC1 4.1.4)
  522. * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
  523. */
  524. static int ecdsa_verify_restartable( mbedtls_ecp_group *grp,
  525. const unsigned char *buf, size_t blen,
  526. const mbedtls_ecp_point *Q,
  527. const mbedtls_mpi *r, const mbedtls_mpi *s,
  528. mbedtls_ecdsa_restart_ctx *rs_ctx )
  529. {
  530. int ret;
  531. mbedtls_mpi e, s_inv, u1, u2;
  532. mbedtls_ecp_point R;
  533. mbedtls_mpi *pu1 = &u1, *pu2 = &u2;
  534. mbedtls_ecp_point_init( &R );
  535. mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv );
  536. mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
  537. /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
  538. if( grp->N.p == NULL )
  539. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  540. ECDSA_RS_ENTER( ver );
  541. #if defined(MBEDTLS_ECP_RESTARTABLE)
  542. if( rs_ctx != NULL && rs_ctx->ver != NULL )
  543. {
  544. /* redirect to our context */
  545. pu1 = &rs_ctx->ver->u1;
  546. pu2 = &rs_ctx->ver->u2;
  547. /* jump to current step */
  548. if( rs_ctx->ver->state == ecdsa_ver_muladd )
  549. goto muladd;
  550. }
  551. #endif /* MBEDTLS_ECP_RESTARTABLE */
  552. /*
  553. * Step 1: make sure r and s are in range 1..n-1
  554. */
  555. if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 ||
  556. mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 )
  557. {
  558. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  559. goto cleanup;
  560. }
  561. /*
  562. * Step 3: derive MPI from hashed message
  563. */
  564. MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
  565. /*
  566. * Step 4: u1 = e / s mod n, u2 = r / s mod n
  567. */
  568. ECDSA_BUDGET( MBEDTLS_ECP_OPS_CHK + MBEDTLS_ECP_OPS_INV + 2 );
  569. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) );
  570. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu1, &e, &s_inv ) );
  571. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu1, pu1, &grp->N ) );
  572. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu2, r, &s_inv ) );
  573. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu2, pu2, &grp->N ) );
  574. #if defined(MBEDTLS_ECP_RESTARTABLE)
  575. if( rs_ctx != NULL && rs_ctx->ver != NULL )
  576. rs_ctx->ver->state = ecdsa_ver_muladd;
  577. muladd:
  578. #endif
  579. /*
  580. * Step 5: R = u1 G + u2 Q
  581. */
  582. MBEDTLS_MPI_CHK( mbedtls_ecp_muladd_restartable( grp,
  583. &R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP ) );
  584. if( mbedtls_ecp_is_zero( &R ) )
  585. {
  586. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  587. goto cleanup;
  588. }
  589. /*
  590. * Step 6: convert xR to an integer (no-op)
  591. * Step 7: reduce xR mod n (gives v)
  592. */
  593. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
  594. /*
  595. * Step 8: check if v (that is, R.X) is equal to r
  596. */
  597. if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 )
  598. {
  599. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  600. goto cleanup;
  601. }
  602. cleanup:
  603. mbedtls_ecp_point_free( &R );
  604. mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv );
  605. mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
  606. ECDSA_RS_LEAVE( ver );
  607. return( ret );
  608. }
  609. /*
  610. * Verify ECDSA signature of hashed message
  611. */
  612. int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
  613. const unsigned char *buf, size_t blen,
  614. const mbedtls_ecp_point *Q,
  615. const mbedtls_mpi *r,
  616. const mbedtls_mpi *s)
  617. {
  618. ECDSA_VALIDATE_RET( grp != NULL );
  619. ECDSA_VALIDATE_RET( Q != NULL );
  620. ECDSA_VALIDATE_RET( r != NULL );
  621. ECDSA_VALIDATE_RET( s != NULL );
  622. ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
  623. return( ecdsa_verify_restartable( grp, buf, blen, Q, r, s, NULL ) );
  624. }
  625. #endif /* !MBEDTLS_ECDSA_VERIFY_ALT */
  626. /*
  627. * Convert a signature (given by context) to ASN.1
  628. */
  629. static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
  630. unsigned char *sig, size_t *slen )
  631. {
  632. int ret;
  633. unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
  634. unsigned char *p = buf + sizeof( buf );
  635. size_t len = 0;
  636. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) );
  637. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) );
  638. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) );
  639. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf,
  640. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  641. memcpy( sig, p, len );
  642. *slen = len;
  643. return( 0 );
  644. }
  645. /*
  646. * Compute and write signature
  647. */
  648. int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
  649. mbedtls_md_type_t md_alg,
  650. const unsigned char *hash, size_t hlen,
  651. unsigned char *sig, size_t *slen,
  652. int (*f_rng)(void *, unsigned char *, size_t),
  653. void *p_rng,
  654. mbedtls_ecdsa_restart_ctx *rs_ctx )
  655. {
  656. int ret;
  657. mbedtls_mpi r, s;
  658. ECDSA_VALIDATE_RET( ctx != NULL );
  659. ECDSA_VALIDATE_RET( hash != NULL );
  660. ECDSA_VALIDATE_RET( sig != NULL );
  661. ECDSA_VALIDATE_RET( slen != NULL );
  662. mbedtls_mpi_init( &r );
  663. mbedtls_mpi_init( &s );
  664. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  665. MBEDTLS_MPI_CHK( ecdsa_sign_det_restartable( &ctx->grp, &r, &s, &ctx->d,
  666. hash, hlen, md_alg, f_rng,
  667. p_rng, rs_ctx ) );
  668. #else
  669. (void) md_alg;
  670. #if defined(MBEDTLS_ECDSA_SIGN_ALT)
  671. MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d,
  672. hash, hlen, f_rng, p_rng ) );
  673. #else
  674. /* Use the same RNG for both blinding and ephemeral key generation */
  675. MBEDTLS_MPI_CHK( ecdsa_sign_restartable( &ctx->grp, &r, &s, &ctx->d,
  676. hash, hlen, f_rng, p_rng, f_rng,
  677. p_rng, rs_ctx ) );
  678. #endif /* MBEDTLS_ECDSA_SIGN_ALT */
  679. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  680. MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
  681. cleanup:
  682. mbedtls_mpi_free( &r );
  683. mbedtls_mpi_free( &s );
  684. return( ret );
  685. }
  686. /*
  687. * Compute and write signature
  688. */
  689. int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
  690. mbedtls_md_type_t md_alg,
  691. const unsigned char *hash, size_t hlen,
  692. unsigned char *sig, size_t *slen,
  693. int (*f_rng)(void *, unsigned char *, size_t),
  694. void *p_rng )
  695. {
  696. ECDSA_VALIDATE_RET( ctx != NULL );
  697. ECDSA_VALIDATE_RET( hash != NULL );
  698. ECDSA_VALIDATE_RET( sig != NULL );
  699. ECDSA_VALIDATE_RET( slen != NULL );
  700. return( mbedtls_ecdsa_write_signature_restartable(
  701. ctx, md_alg, hash, hlen, sig, slen, f_rng, p_rng, NULL ) );
  702. }
  703. #if !defined(MBEDTLS_DEPRECATED_REMOVED) && \
  704. defined(MBEDTLS_ECDSA_DETERMINISTIC)
  705. int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
  706. const unsigned char *hash, size_t hlen,
  707. unsigned char *sig, size_t *slen,
  708. mbedtls_md_type_t md_alg )
  709. {
  710. ECDSA_VALIDATE_RET( ctx != NULL );
  711. ECDSA_VALIDATE_RET( hash != NULL );
  712. ECDSA_VALIDATE_RET( sig != NULL );
  713. ECDSA_VALIDATE_RET( slen != NULL );
  714. return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
  715. NULL, NULL ) );
  716. }
  717. #endif
  718. /*
  719. * Read and check signature
  720. */
  721. int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
  722. const unsigned char *hash, size_t hlen,
  723. const unsigned char *sig, size_t slen )
  724. {
  725. ECDSA_VALIDATE_RET( ctx != NULL );
  726. ECDSA_VALIDATE_RET( hash != NULL );
  727. ECDSA_VALIDATE_RET( sig != NULL );
  728. return( mbedtls_ecdsa_read_signature_restartable(
  729. ctx, hash, hlen, sig, slen, NULL ) );
  730. }
  731. /*
  732. * Restartable read and check signature
  733. */
  734. int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
  735. const unsigned char *hash, size_t hlen,
  736. const unsigned char *sig, size_t slen,
  737. mbedtls_ecdsa_restart_ctx *rs_ctx )
  738. {
  739. int ret;
  740. unsigned char *p = (unsigned char *) sig;
  741. const unsigned char *end = sig + slen;
  742. size_t len;
  743. mbedtls_mpi r, s;
  744. ECDSA_VALIDATE_RET( ctx != NULL );
  745. ECDSA_VALIDATE_RET( hash != NULL );
  746. ECDSA_VALIDATE_RET( sig != NULL );
  747. mbedtls_mpi_init( &r );
  748. mbedtls_mpi_init( &s );
  749. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  750. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  751. {
  752. ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  753. goto cleanup;
  754. }
  755. if( p + len != end )
  756. {
  757. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA +
  758. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
  759. goto cleanup;
  760. }
  761. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 ||
  762. ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 )
  763. {
  764. ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  765. goto cleanup;
  766. }
  767. #if defined(MBEDTLS_ECDSA_VERIFY_ALT)
  768. if( ( ret = mbedtls_ecdsa_verify( &ctx->grp, hash, hlen,
  769. &ctx->Q, &r, &s ) ) != 0 )
  770. goto cleanup;
  771. #else
  772. if( ( ret = ecdsa_verify_restartable( &ctx->grp, hash, hlen,
  773. &ctx->Q, &r, &s, rs_ctx ) ) != 0 )
  774. goto cleanup;
  775. #endif /* MBEDTLS_ECDSA_VERIFY_ALT */
  776. /* At this point we know that the buffer starts with a valid signature.
  777. * Return 0 if the buffer just contains the signature, and a specific
  778. * error code if the valid signature is followed by more data. */
  779. if( p != end )
  780. ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
  781. cleanup:
  782. mbedtls_mpi_free( &r );
  783. mbedtls_mpi_free( &s );
  784. return( ret );
  785. }
  786. #if !defined(MBEDTLS_ECDSA_GENKEY_ALT)
  787. /*
  788. * Generate key pair
  789. */
  790. int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
  791. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  792. {
  793. int ret = 0;
  794. ECDSA_VALIDATE_RET( ctx != NULL );
  795. ECDSA_VALIDATE_RET( f_rng != NULL );
  796. ret = mbedtls_ecp_group_load( &ctx->grp, gid );
  797. if( ret != 0 )
  798. return( ret );
  799. return( mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d,
  800. &ctx->Q, f_rng, p_rng ) );
  801. }
  802. #endif /* !MBEDTLS_ECDSA_GENKEY_ALT */
  803. /*
  804. * Set context from an mbedtls_ecp_keypair
  805. */
  806. int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key )
  807. {
  808. int ret;
  809. ECDSA_VALIDATE_RET( ctx != NULL );
  810. ECDSA_VALIDATE_RET( key != NULL );
  811. if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
  812. ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ||
  813. ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
  814. {
  815. mbedtls_ecdsa_free( ctx );
  816. }
  817. return( ret );
  818. }
  819. /*
  820. * Initialize context
  821. */
  822. void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx )
  823. {
  824. ECDSA_VALIDATE( ctx != NULL );
  825. mbedtls_ecp_keypair_init( ctx );
  826. }
  827. /*
  828. * Free context
  829. */
  830. void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx )
  831. {
  832. if( ctx == NULL )
  833. return;
  834. mbedtls_ecp_keypair_free( ctx );
  835. }
  836. #if defined(MBEDTLS_ECP_RESTARTABLE)
  837. /*
  838. * Initialize a restart context
  839. */
  840. void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx )
  841. {
  842. ECDSA_VALIDATE( ctx != NULL );
  843. mbedtls_ecp_restart_init( &ctx->ecp );
  844. ctx->ver = NULL;
  845. ctx->sig = NULL;
  846. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  847. ctx->det = NULL;
  848. #endif
  849. }
  850. /*
  851. * Free the components of a restart context
  852. */
  853. void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx )
  854. {
  855. if( ctx == NULL )
  856. return;
  857. mbedtls_ecp_restart_free( &ctx->ecp );
  858. ecdsa_restart_ver_free( ctx->ver );
  859. mbedtls_free( ctx->ver );
  860. ctx->ver = NULL;
  861. ecdsa_restart_sig_free( ctx->sig );
  862. mbedtls_free( ctx->sig );
  863. ctx->sig = NULL;
  864. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  865. ecdsa_restart_det_free( ctx->det );
  866. mbedtls_free( ctx->det );
  867. ctx->det = NULL;
  868. #endif
  869. }
  870. #endif /* MBEDTLS_ECP_RESTARTABLE */
  871. #endif /* MBEDTLS_ECDSA_C */