ecdsa.c 31 KB

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