ecdsa.c 32 KB

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