ecdsa.c 31 KB

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