ecdsa.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. * Elliptic curve DSA
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /*
  20. * References:
  21. *
  22. * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
  23. */
  24. #include "common.h"
  25. #if defined(MBEDTLS_ECDSA_C)
  26. #include "mbedtls/ecdsa.h"
  27. #include "mbedtls/asn1write.h"
  28. #include <string.h>
  29. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  30. #include "mbedtls/hmac_drbg.h"
  31. #endif
  32. #if defined(MBEDTLS_PLATFORM_C)
  33. #include "mbedtls/platform.h"
  34. #else
  35. #include <stdlib.h>
  36. #define mbedtls_calloc calloc
  37. #define mbedtls_free free
  38. #endif
  39. #include "mbedtls/platform_util.h"
  40. #include "mbedtls/error.h"
  41. /* Parameter validation macros based on platform_util.h */
  42. #define ECDSA_VALIDATE_RET( cond ) \
  43. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
  44. #define ECDSA_VALIDATE( cond ) \
  45. MBEDTLS_INTERNAL_VALIDATE( cond )
  46. #if defined(MBEDTLS_ECP_RESTARTABLE)
  47. /*
  48. * Sub-context for ecdsa_verify()
  49. */
  50. struct mbedtls_ecdsa_restart_ver
  51. {
  52. mbedtls_mpi u1, u2; /* intermediate values */
  53. enum { /* what to do next? */
  54. ecdsa_ver_init = 0, /* getting started */
  55. ecdsa_ver_muladd, /* muladd step */
  56. } state;
  57. };
  58. /*
  59. * Init verify restart sub-context
  60. */
  61. static void ecdsa_restart_ver_init( mbedtls_ecdsa_restart_ver_ctx *ctx )
  62. {
  63. mbedtls_mpi_init( &ctx->u1 );
  64. mbedtls_mpi_init( &ctx->u2 );
  65. ctx->state = ecdsa_ver_init;
  66. }
  67. /*
  68. * Free the components of a verify restart sub-context
  69. */
  70. static void ecdsa_restart_ver_free( mbedtls_ecdsa_restart_ver_ctx *ctx )
  71. {
  72. if( ctx == NULL )
  73. return;
  74. mbedtls_mpi_free( &ctx->u1 );
  75. mbedtls_mpi_free( &ctx->u2 );
  76. ecdsa_restart_ver_init( ctx );
  77. }
  78. /*
  79. * Sub-context for ecdsa_sign()
  80. */
  81. struct mbedtls_ecdsa_restart_sig
  82. {
  83. int sign_tries;
  84. int key_tries;
  85. mbedtls_mpi k; /* per-signature random */
  86. mbedtls_mpi r; /* r value */
  87. enum { /* what to do next? */
  88. ecdsa_sig_init = 0, /* getting started */
  89. ecdsa_sig_mul, /* doing ecp_mul() */
  90. ecdsa_sig_modn, /* mod N computations */
  91. } state;
  92. };
  93. /*
  94. * Init verify sign sub-context
  95. */
  96. static void ecdsa_restart_sig_init( mbedtls_ecdsa_restart_sig_ctx *ctx )
  97. {
  98. ctx->sign_tries = 0;
  99. ctx->key_tries = 0;
  100. mbedtls_mpi_init( &ctx->k );
  101. mbedtls_mpi_init( &ctx->r );
  102. ctx->state = ecdsa_sig_init;
  103. }
  104. /*
  105. * Free the components of a sign restart sub-context
  106. */
  107. static void ecdsa_restart_sig_free( mbedtls_ecdsa_restart_sig_ctx *ctx )
  108. {
  109. if( ctx == NULL )
  110. return;
  111. mbedtls_mpi_free( &ctx->k );
  112. mbedtls_mpi_free( &ctx->r );
  113. }
  114. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  115. /*
  116. * Sub-context for ecdsa_sign_det()
  117. */
  118. struct mbedtls_ecdsa_restart_det
  119. {
  120. mbedtls_hmac_drbg_context rng_ctx; /* DRBG state */
  121. enum { /* what to do next? */
  122. ecdsa_det_init = 0, /* getting started */
  123. ecdsa_det_sign, /* make signature */
  124. } state;
  125. };
  126. /*
  127. * Init verify sign_det sub-context
  128. */
  129. static void ecdsa_restart_det_init( mbedtls_ecdsa_restart_det_ctx *ctx )
  130. {
  131. mbedtls_hmac_drbg_init( &ctx->rng_ctx );
  132. ctx->state = ecdsa_det_init;
  133. }
  134. /*
  135. * Free the components of a sign_det restart sub-context
  136. */
  137. static void ecdsa_restart_det_free( mbedtls_ecdsa_restart_det_ctx *ctx )
  138. {
  139. if( ctx == NULL )
  140. return;
  141. mbedtls_hmac_drbg_free( &ctx->rng_ctx );
  142. ecdsa_restart_det_init( ctx );
  143. }
  144. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  145. #define ECDSA_RS_ECP ( rs_ctx == NULL ? NULL : &rs_ctx->ecp )
  146. /* Utility macro for checking and updating ops budget */
  147. #define ECDSA_BUDGET( ops ) \
  148. MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, ECDSA_RS_ECP, ops ) );
  149. /* Call this when entering a function that needs its own sub-context */
  150. #define ECDSA_RS_ENTER( SUB ) do { \
  151. /* reset ops count for this call if top-level */ \
  152. if( rs_ctx != NULL && rs_ctx->ecp.depth++ == 0 ) \
  153. rs_ctx->ecp.ops_done = 0; \
  154. \
  155. /* set up our own sub-context if needed */ \
  156. if( mbedtls_ecp_restart_is_enabled() && \
  157. rs_ctx != NULL && rs_ctx->SUB == NULL ) \
  158. { \
  159. rs_ctx->SUB = mbedtls_calloc( 1, sizeof( *rs_ctx->SUB ) ); \
  160. if( rs_ctx->SUB == NULL ) \
  161. return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); \
  162. \
  163. ecdsa_restart_## SUB ##_init( rs_ctx->SUB ); \
  164. } \
  165. } while( 0 )
  166. /* Call this when leaving a function that needs its own sub-context */
  167. #define ECDSA_RS_LEAVE( SUB ) do { \
  168. /* clear our sub-context when not in progress (done or error) */ \
  169. if( rs_ctx != NULL && rs_ctx->SUB != NULL && \
  170. ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) \
  171. { \
  172. ecdsa_restart_## SUB ##_free( rs_ctx->SUB ); \
  173. mbedtls_free( rs_ctx->SUB ); \
  174. rs_ctx->SUB = NULL; \
  175. } \
  176. \
  177. if( rs_ctx != NULL ) \
  178. rs_ctx->ecp.depth--; \
  179. } while( 0 )
  180. #else /* MBEDTLS_ECP_RESTARTABLE */
  181. #define ECDSA_RS_ECP NULL
  182. #define ECDSA_BUDGET( ops ) /* no-op; for compatibility */
  183. #define ECDSA_RS_ENTER( SUB ) (void) rs_ctx
  184. #define ECDSA_RS_LEAVE( SUB ) (void) rs_ctx
  185. #endif /* MBEDTLS_ECP_RESTARTABLE */
  186. /*
  187. * Derive a suitable integer for group grp from a buffer of length len
  188. * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
  189. */
  190. static int derive_mpi( const mbedtls_ecp_group *grp, mbedtls_mpi *x,
  191. const unsigned char *buf, size_t blen )
  192. {
  193. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  194. size_t n_size = ( grp->nbits + 7 ) / 8;
  195. size_t use_size = blen > n_size ? n_size : blen;
  196. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( x, buf, use_size ) );
  197. if( use_size * 8 > grp->nbits )
  198. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( x, use_size * 8 - grp->nbits ) );
  199. /* While at it, reduce modulo N */
  200. if( mbedtls_mpi_cmp_mpi( x, &grp->N ) >= 0 )
  201. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( x, x, &grp->N ) );
  202. cleanup:
  203. return( ret );
  204. }
  205. #if !defined(MBEDTLS_ECDSA_SIGN_ALT)
  206. /*
  207. * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
  208. * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
  209. */
  210. static int ecdsa_sign_restartable( mbedtls_ecp_group *grp,
  211. mbedtls_mpi *r, mbedtls_mpi *s,
  212. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  213. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  214. int (*f_rng_blind)(void *, unsigned char *, size_t),
  215. void *p_rng_blind,
  216. mbedtls_ecdsa_restart_ctx *rs_ctx )
  217. {
  218. int ret, key_tries, sign_tries;
  219. int *p_sign_tries = &sign_tries, *p_key_tries = &key_tries;
  220. mbedtls_ecp_point R;
  221. mbedtls_mpi k, e, t;
  222. mbedtls_mpi *pk = &k, *pr = r;
  223. /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
  224. if( ! mbedtls_ecdsa_can_do( grp->id ) || grp->N.p == NULL )
  225. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  226. /* Make sure d is in range 1..n-1 */
  227. if( mbedtls_mpi_cmp_int( d, 1 ) < 0 || mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
  228. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  229. mbedtls_ecp_point_init( &R );
  230. mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t );
  231. ECDSA_RS_ENTER( sig );
  232. #if defined(MBEDTLS_ECP_RESTARTABLE)
  233. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  234. {
  235. /* redirect to our context */
  236. p_sign_tries = &rs_ctx->sig->sign_tries;
  237. p_key_tries = &rs_ctx->sig->key_tries;
  238. pk = &rs_ctx->sig->k;
  239. pr = &rs_ctx->sig->r;
  240. /* jump to current step */
  241. if( rs_ctx->sig->state == ecdsa_sig_mul )
  242. goto mul;
  243. if( rs_ctx->sig->state == ecdsa_sig_modn )
  244. goto modn;
  245. }
  246. #endif /* MBEDTLS_ECP_RESTARTABLE */
  247. *p_sign_tries = 0;
  248. do
  249. {
  250. if( (*p_sign_tries)++ > 10 )
  251. {
  252. ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
  253. goto cleanup;
  254. }
  255. /*
  256. * Steps 1-3: generate a suitable ephemeral keypair
  257. * and set r = xR mod n
  258. */
  259. *p_key_tries = 0;
  260. do
  261. {
  262. if( (*p_key_tries)++ > 10 )
  263. {
  264. ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
  265. goto cleanup;
  266. }
  267. MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, pk, f_rng, p_rng ) );
  268. #if defined(MBEDTLS_ECP_RESTARTABLE)
  269. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  270. rs_ctx->sig->state = ecdsa_sig_mul;
  271. mul:
  272. #endif
  273. MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &R, pk, &grp->G,
  274. f_rng_blind,
  275. p_rng_blind,
  276. ECDSA_RS_ECP ) );
  277. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pr, &R.X, &grp->N ) );
  278. }
  279. while( mbedtls_mpi_cmp_int( pr, 0 ) == 0 );
  280. #if defined(MBEDTLS_ECP_RESTARTABLE)
  281. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  282. rs_ctx->sig->state = ecdsa_sig_modn;
  283. modn:
  284. #endif
  285. /*
  286. * Accounting for everything up to the end of the loop
  287. * (step 6, but checking now avoids saving e and t)
  288. */
  289. ECDSA_BUDGET( MBEDTLS_ECP_OPS_INV + 4 );
  290. /*
  291. * Step 5: derive MPI from hashed message
  292. */
  293. MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
  294. /*
  295. * Generate a random value to blind inv_mod in next step,
  296. * avoiding a potential timing leak.
  297. */
  298. MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, &t, f_rng_blind,
  299. p_rng_blind ) );
  300. /*
  301. * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
  302. */
  303. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, pr, d ) );
  304. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) );
  305. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) );
  306. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pk, pk, &t ) );
  307. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pk, pk, &grp->N ) );
  308. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, pk, &grp->N ) );
  309. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) );
  310. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) );
  311. }
  312. while( mbedtls_mpi_cmp_int( s, 0 ) == 0 );
  313. #if defined(MBEDTLS_ECP_RESTARTABLE)
  314. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  315. mbedtls_mpi_copy( r, pr );
  316. #endif
  317. cleanup:
  318. mbedtls_ecp_point_free( &R );
  319. mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t );
  320. ECDSA_RS_LEAVE( sig );
  321. return( ret );
  322. }
  323. int mbedtls_ecdsa_can_do( mbedtls_ecp_group_id gid )
  324. {
  325. switch( gid )
  326. {
  327. #ifdef MBEDTLS_ECP_DP_CURVE25519_ENABLED
  328. case MBEDTLS_ECP_DP_CURVE25519: return 0;
  329. #endif
  330. #ifdef MBEDTLS_ECP_DP_CURVE448_ENABLED
  331. case MBEDTLS_ECP_DP_CURVE448: return 0;
  332. #endif
  333. default: return 1;
  334. }
  335. }
  336. /*
  337. * Compute ECDSA signature of a hashed message
  338. */
  339. int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  340. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  341. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  342. {
  343. ECDSA_VALIDATE_RET( grp != NULL );
  344. ECDSA_VALIDATE_RET( r != NULL );
  345. ECDSA_VALIDATE_RET( s != NULL );
  346. ECDSA_VALIDATE_RET( d != NULL );
  347. ECDSA_VALIDATE_RET( f_rng != NULL );
  348. ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
  349. /* Use the same RNG for both blinding and ephemeral key generation */
  350. return( ecdsa_sign_restartable( grp, r, s, d, buf, blen,
  351. f_rng, p_rng, f_rng, p_rng, NULL ) );
  352. }
  353. #endif /* !MBEDTLS_ECDSA_SIGN_ALT */
  354. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  355. /*
  356. * Deterministic signature wrapper
  357. */
  358. static int ecdsa_sign_det_restartable( mbedtls_ecp_group *grp,
  359. mbedtls_mpi *r, mbedtls_mpi *s,
  360. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  361. mbedtls_md_type_t md_alg,
  362. int (*f_rng_blind)(void *, unsigned char *, size_t),
  363. void *p_rng_blind,
  364. mbedtls_ecdsa_restart_ctx *rs_ctx )
  365. {
  366. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  367. mbedtls_hmac_drbg_context rng_ctx;
  368. mbedtls_hmac_drbg_context *p_rng = &rng_ctx;
  369. unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
  370. size_t grp_len = ( grp->nbits + 7 ) / 8;
  371. const mbedtls_md_info_t *md_info;
  372. mbedtls_mpi h;
  373. if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
  374. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  375. mbedtls_mpi_init( &h );
  376. mbedtls_hmac_drbg_init( &rng_ctx );
  377. ECDSA_RS_ENTER( det );
  378. #if defined(MBEDTLS_ECP_RESTARTABLE)
  379. if( rs_ctx != NULL && rs_ctx->det != NULL )
  380. {
  381. /* redirect to our context */
  382. p_rng = &rs_ctx->det->rng_ctx;
  383. /* jump to current step */
  384. if( rs_ctx->det->state == ecdsa_det_sign )
  385. goto sign;
  386. }
  387. #endif /* MBEDTLS_ECP_RESTARTABLE */
  388. /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
  389. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) );
  390. MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
  391. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) );
  392. mbedtls_hmac_drbg_seed_buf( p_rng, md_info, data, 2 * grp_len );
  393. #if defined(MBEDTLS_ECP_RESTARTABLE)
  394. if( rs_ctx != NULL && rs_ctx->det != NULL )
  395. rs_ctx->det->state = ecdsa_det_sign;
  396. sign:
  397. #endif
  398. #if defined(MBEDTLS_ECDSA_SIGN_ALT)
  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];
  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. MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d,
  657. hash, hlen, f_rng, p_rng ) );
  658. #else
  659. /* Use the same RNG for both blinding and ephemeral key generation */
  660. MBEDTLS_MPI_CHK( ecdsa_sign_restartable( &ctx->grp, &r, &s, &ctx->d,
  661. hash, hlen, f_rng, p_rng, f_rng,
  662. p_rng, rs_ctx ) );
  663. #endif /* MBEDTLS_ECDSA_SIGN_ALT */
  664. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  665. MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
  666. cleanup:
  667. mbedtls_mpi_free( &r );
  668. mbedtls_mpi_free( &s );
  669. return( ret );
  670. }
  671. /*
  672. * Compute and write signature
  673. */
  674. int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
  675. mbedtls_md_type_t md_alg,
  676. const unsigned char *hash, size_t hlen,
  677. unsigned char *sig, size_t *slen,
  678. int (*f_rng)(void *, unsigned char *, size_t),
  679. void *p_rng )
  680. {
  681. ECDSA_VALIDATE_RET( ctx != NULL );
  682. ECDSA_VALIDATE_RET( hash != NULL );
  683. ECDSA_VALIDATE_RET( sig != NULL );
  684. ECDSA_VALIDATE_RET( slen != NULL );
  685. return( mbedtls_ecdsa_write_signature_restartable(
  686. ctx, md_alg, hash, hlen, sig, slen, f_rng, p_rng, NULL ) );
  687. }
  688. #if !defined(MBEDTLS_DEPRECATED_REMOVED) && \
  689. defined(MBEDTLS_ECDSA_DETERMINISTIC)
  690. int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
  691. const unsigned char *hash, size_t hlen,
  692. unsigned char *sig, size_t *slen,
  693. mbedtls_md_type_t md_alg )
  694. {
  695. ECDSA_VALIDATE_RET( ctx != NULL );
  696. ECDSA_VALIDATE_RET( hash != NULL );
  697. ECDSA_VALIDATE_RET( sig != NULL );
  698. ECDSA_VALIDATE_RET( slen != NULL );
  699. return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
  700. NULL, NULL ) );
  701. }
  702. #endif
  703. /*
  704. * Read and check signature
  705. */
  706. int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
  707. const unsigned char *hash, size_t hlen,
  708. const unsigned char *sig, size_t slen )
  709. {
  710. ECDSA_VALIDATE_RET( ctx != NULL );
  711. ECDSA_VALIDATE_RET( hash != NULL );
  712. ECDSA_VALIDATE_RET( sig != NULL );
  713. return( mbedtls_ecdsa_read_signature_restartable(
  714. ctx, hash, hlen, sig, slen, NULL ) );
  715. }
  716. /*
  717. * Restartable read and check signature
  718. */
  719. int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
  720. const unsigned char *hash, size_t hlen,
  721. const unsigned char *sig, size_t slen,
  722. mbedtls_ecdsa_restart_ctx *rs_ctx )
  723. {
  724. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  725. unsigned char *p = (unsigned char *) sig;
  726. const unsigned char *end = sig + slen;
  727. size_t len;
  728. mbedtls_mpi r, s;
  729. ECDSA_VALIDATE_RET( ctx != NULL );
  730. ECDSA_VALIDATE_RET( hash != NULL );
  731. ECDSA_VALIDATE_RET( sig != NULL );
  732. mbedtls_mpi_init( &r );
  733. mbedtls_mpi_init( &s );
  734. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  735. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  736. {
  737. ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  738. goto cleanup;
  739. }
  740. if( p + len != end )
  741. {
  742. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA +
  743. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
  744. goto cleanup;
  745. }
  746. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 ||
  747. ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 )
  748. {
  749. ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  750. goto cleanup;
  751. }
  752. #if defined(MBEDTLS_ECDSA_VERIFY_ALT)
  753. if( ( ret = mbedtls_ecdsa_verify( &ctx->grp, hash, hlen,
  754. &ctx->Q, &r, &s ) ) != 0 )
  755. goto cleanup;
  756. #else
  757. if( ( ret = ecdsa_verify_restartable( &ctx->grp, hash, hlen,
  758. &ctx->Q, &r, &s, rs_ctx ) ) != 0 )
  759. goto cleanup;
  760. #endif /* MBEDTLS_ECDSA_VERIFY_ALT */
  761. /* At this point we know that the buffer starts with a valid signature.
  762. * Return 0 if the buffer just contains the signature, and a specific
  763. * error code if the valid signature is followed by more data. */
  764. if( p != end )
  765. ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
  766. cleanup:
  767. mbedtls_mpi_free( &r );
  768. mbedtls_mpi_free( &s );
  769. return( ret );
  770. }
  771. #if !defined(MBEDTLS_ECDSA_GENKEY_ALT)
  772. /*
  773. * Generate key pair
  774. */
  775. int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
  776. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  777. {
  778. int ret = 0;
  779. ECDSA_VALIDATE_RET( ctx != NULL );
  780. ECDSA_VALIDATE_RET( f_rng != NULL );
  781. ret = mbedtls_ecp_group_load( &ctx->grp, gid );
  782. if( ret != 0 )
  783. return( ret );
  784. return( mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d,
  785. &ctx->Q, f_rng, p_rng ) );
  786. }
  787. #endif /* !MBEDTLS_ECDSA_GENKEY_ALT */
  788. /*
  789. * Set context from an mbedtls_ecp_keypair
  790. */
  791. int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key )
  792. {
  793. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  794. ECDSA_VALIDATE_RET( ctx != NULL );
  795. ECDSA_VALIDATE_RET( key != NULL );
  796. if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
  797. ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ||
  798. ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
  799. {
  800. mbedtls_ecdsa_free( ctx );
  801. }
  802. return( ret );
  803. }
  804. /*
  805. * Initialize context
  806. */
  807. void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx )
  808. {
  809. ECDSA_VALIDATE( ctx != NULL );
  810. mbedtls_ecp_keypair_init( ctx );
  811. }
  812. /*
  813. * Free context
  814. */
  815. void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx )
  816. {
  817. if( ctx == NULL )
  818. return;
  819. mbedtls_ecp_keypair_free( ctx );
  820. }
  821. #if defined(MBEDTLS_ECP_RESTARTABLE)
  822. /*
  823. * Initialize a restart context
  824. */
  825. void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx )
  826. {
  827. ECDSA_VALIDATE( ctx != NULL );
  828. mbedtls_ecp_restart_init( &ctx->ecp );
  829. ctx->ver = NULL;
  830. ctx->sig = NULL;
  831. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  832. ctx->det = NULL;
  833. #endif
  834. }
  835. /*
  836. * Free the components of a restart context
  837. */
  838. void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx )
  839. {
  840. if( ctx == NULL )
  841. return;
  842. mbedtls_ecp_restart_free( &ctx->ecp );
  843. ecdsa_restart_ver_free( ctx->ver );
  844. mbedtls_free( ctx->ver );
  845. ctx->ver = NULL;
  846. ecdsa_restart_sig_free( ctx->sig );
  847. mbedtls_free( ctx->sig );
  848. ctx->sig = NULL;
  849. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  850. ecdsa_restart_det_free( ctx->det );
  851. mbedtls_free( ctx->det );
  852. ctx->det = NULL;
  853. #endif
  854. }
  855. #endif /* MBEDTLS_ECP_RESTARTABLE */
  856. #endif /* MBEDTLS_ECDSA_C */