ecdsa.c 32 KB

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