ecdsa.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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->ecp
  151. /* Utility macro for checking and updating ops budget */
  152. #define ECDSA_BUDGET( ops ) \
  153. MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, &rs_ctx->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. mbedtls_ecdsa_restart_ctx *rs_ctx )
  220. {
  221. int ret, key_tries, sign_tries;
  222. int *p_sign_tries = &sign_tries, *p_key_tries = &key_tries;
  223. mbedtls_ecp_point R;
  224. mbedtls_mpi k, e, t;
  225. mbedtls_mpi *pk = &k, *pr = r;
  226. /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
  227. if( grp->N.p == NULL )
  228. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  229. /* Make sure d is in range 1..n-1 */
  230. if( mbedtls_mpi_cmp_int( d, 1 ) < 0 || mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
  231. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  232. mbedtls_ecp_point_init( &R );
  233. mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t );
  234. ECDSA_RS_ENTER( sig );
  235. #if defined(MBEDTLS_ECP_RESTARTABLE)
  236. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  237. {
  238. /* redirect to our context */
  239. p_sign_tries = &rs_ctx->sig->sign_tries;
  240. p_key_tries = &rs_ctx->sig->key_tries;
  241. pk = &rs_ctx->sig->k;
  242. pr = &rs_ctx->sig->r;
  243. /* jump to current step */
  244. if( rs_ctx->sig->state == ecdsa_sig_mul )
  245. goto mul;
  246. if( rs_ctx->sig->state == ecdsa_sig_modn )
  247. goto modn;
  248. }
  249. #endif /* MBEDTLS_ECP_RESTARTABLE */
  250. *p_sign_tries = 0;
  251. do
  252. {
  253. if( *p_sign_tries++ > 10 )
  254. {
  255. ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
  256. goto cleanup;
  257. }
  258. /*
  259. * Steps 1-3: generate a suitable ephemeral keypair
  260. * and set r = xR mod n
  261. */
  262. *p_key_tries = 0;
  263. do
  264. {
  265. if( *p_key_tries++ > 10 )
  266. {
  267. ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
  268. goto cleanup;
  269. }
  270. MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, pk, f_rng, p_rng ) );
  271. #if defined(MBEDTLS_ECP_RESTARTABLE)
  272. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  273. rs_ctx->sig->state = ecdsa_sig_mul;
  274. mul:
  275. #endif
  276. MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &R, pk, &grp->G,
  277. f_rng, p_rng, ECDSA_RS_ECP ) );
  278. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pr, &R.X, &grp->N ) );
  279. }
  280. while( mbedtls_mpi_cmp_int( pr, 0 ) == 0 );
  281. #if defined(MBEDTLS_ECP_RESTARTABLE)
  282. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  283. rs_ctx->sig->state = ecdsa_sig_modn;
  284. modn:
  285. #endif
  286. /*
  287. * Accounting for everything up to the end of the loop
  288. * (step 6, but checking now avoids saving e and t)
  289. */
  290. ECDSA_BUDGET( MBEDTLS_ECP_OPS_INV + 4 );
  291. /*
  292. * Step 5: derive MPI from hashed message
  293. */
  294. MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
  295. /*
  296. * Generate a random value to blind inv_mod in next step,
  297. * avoiding a potential timing leak.
  298. */
  299. MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, &t, f_rng, p_rng ) );
  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_inv_mod( s, pk, &grp->N ) );
  308. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) );
  309. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) );
  310. }
  311. while( mbedtls_mpi_cmp_int( s, 0 ) == 0 );
  312. #if defined(MBEDTLS_ECP_RESTARTABLE)
  313. if( rs_ctx != NULL && rs_ctx->sig != NULL )
  314. mbedtls_mpi_copy( r, pr );
  315. #endif
  316. cleanup:
  317. mbedtls_ecp_point_free( &R );
  318. mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t );
  319. ECDSA_RS_LEAVE( sig );
  320. return( ret );
  321. }
  322. /*
  323. * Compute ECDSA signature of a hashed message
  324. */
  325. int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  326. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  327. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  328. {
  329. ECDSA_VALIDATE_RET( grp != NULL );
  330. ECDSA_VALIDATE_RET( r != NULL );
  331. ECDSA_VALIDATE_RET( s != NULL );
  332. ECDSA_VALIDATE_RET( d != NULL );
  333. ECDSA_VALIDATE_RET( f_rng != NULL );
  334. ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
  335. return( ecdsa_sign_restartable( grp, r, s, d, buf, blen,
  336. f_rng, p_rng, NULL ) );
  337. }
  338. #endif /* !MBEDTLS_ECDSA_SIGN_ALT */
  339. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  340. /*
  341. * Deterministic signature wrapper
  342. */
  343. static int ecdsa_sign_det_restartable( mbedtls_ecp_group *grp,
  344. mbedtls_mpi *r, mbedtls_mpi *s,
  345. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  346. mbedtls_md_type_t md_alg,
  347. mbedtls_ecdsa_restart_ctx *rs_ctx )
  348. {
  349. int ret;
  350. mbedtls_hmac_drbg_context rng_ctx;
  351. mbedtls_hmac_drbg_context *p_rng = &rng_ctx;
  352. unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
  353. size_t grp_len = ( grp->nbits + 7 ) / 8;
  354. const mbedtls_md_info_t *md_info;
  355. mbedtls_mpi h;
  356. if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
  357. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  358. mbedtls_mpi_init( &h );
  359. mbedtls_hmac_drbg_init( &rng_ctx );
  360. ECDSA_RS_ENTER( det );
  361. #if defined(MBEDTLS_ECP_RESTARTABLE)
  362. if( rs_ctx != NULL && rs_ctx->det != NULL )
  363. {
  364. /* redirect to our context */
  365. p_rng = &rs_ctx->det->rng_ctx;
  366. /* jump to current step */
  367. if( rs_ctx->det->state == ecdsa_det_sign )
  368. goto sign;
  369. }
  370. #endif /* MBEDTLS_ECP_RESTARTABLE */
  371. /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
  372. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) );
  373. MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
  374. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) );
  375. mbedtls_hmac_drbg_seed_buf( p_rng, md_info, data, 2 * grp_len );
  376. #if defined(MBEDTLS_ECP_RESTARTABLE)
  377. if( rs_ctx != NULL && rs_ctx->det != NULL )
  378. rs_ctx->det->state = ecdsa_det_sign;
  379. sign:
  380. #endif
  381. #if defined(MBEDTLS_ECDSA_SIGN_ALT)
  382. ret = mbedtls_ecdsa_sign( grp, r, s, d, buf, blen,
  383. mbedtls_hmac_drbg_random, p_rng );
  384. #else
  385. ret = ecdsa_sign_restartable( grp, r, s, d, buf, blen,
  386. mbedtls_hmac_drbg_random, p_rng, rs_ctx );
  387. #endif /* MBEDTLS_ECDSA_SIGN_ALT */
  388. cleanup:
  389. mbedtls_hmac_drbg_free( &rng_ctx );
  390. mbedtls_mpi_free( &h );
  391. ECDSA_RS_LEAVE( det );
  392. return( ret );
  393. }
  394. /*
  395. * Deterministic signature wrapper
  396. */
  397. int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  398. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  399. mbedtls_md_type_t md_alg )
  400. {
  401. ECDSA_VALIDATE_RET( grp != NULL );
  402. ECDSA_VALIDATE_RET( r != NULL );
  403. ECDSA_VALIDATE_RET( s != NULL );
  404. ECDSA_VALIDATE_RET( d != NULL );
  405. ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
  406. return( ecdsa_sign_det_restartable( grp, r, s, d, buf, blen, md_alg, NULL ) );
  407. }
  408. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  409. #if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
  410. /*
  411. * Verify ECDSA signature of hashed message (SEC1 4.1.4)
  412. * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
  413. */
  414. static int ecdsa_verify_restartable( mbedtls_ecp_group *grp,
  415. const unsigned char *buf, size_t blen,
  416. const mbedtls_ecp_point *Q,
  417. const mbedtls_mpi *r, const mbedtls_mpi *s,
  418. mbedtls_ecdsa_restart_ctx *rs_ctx )
  419. {
  420. int ret;
  421. mbedtls_mpi e, s_inv, u1, u2;
  422. mbedtls_ecp_point R;
  423. mbedtls_mpi *pu1 = &u1, *pu2 = &u2;
  424. mbedtls_ecp_point_init( &R );
  425. mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv );
  426. mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
  427. /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
  428. if( grp->N.p == NULL )
  429. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  430. ECDSA_RS_ENTER( ver );
  431. #if defined(MBEDTLS_ECP_RESTARTABLE)
  432. if( rs_ctx != NULL && rs_ctx->ver != NULL )
  433. {
  434. /* redirect to our context */
  435. pu1 = &rs_ctx->ver->u1;
  436. pu2 = &rs_ctx->ver->u2;
  437. /* jump to current step */
  438. if( rs_ctx->ver->state == ecdsa_ver_muladd )
  439. goto muladd;
  440. }
  441. #endif /* MBEDTLS_ECP_RESTARTABLE */
  442. /*
  443. * Step 1: make sure r and s are in range 1..n-1
  444. */
  445. if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 ||
  446. mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 )
  447. {
  448. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  449. goto cleanup;
  450. }
  451. /*
  452. * Step 3: derive MPI from hashed message
  453. */
  454. MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
  455. /*
  456. * Step 4: u1 = e / s mod n, u2 = r / s mod n
  457. */
  458. ECDSA_BUDGET( MBEDTLS_ECP_OPS_CHK + MBEDTLS_ECP_OPS_INV + 2 );
  459. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) );
  460. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu1, &e, &s_inv ) );
  461. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu1, pu1, &grp->N ) );
  462. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu2, r, &s_inv ) );
  463. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu2, pu2, &grp->N ) );
  464. #if defined(MBEDTLS_ECP_RESTARTABLE)
  465. if( rs_ctx != NULL && rs_ctx->ver != NULL )
  466. rs_ctx->ver->state = ecdsa_ver_muladd;
  467. muladd:
  468. #endif
  469. /*
  470. * Step 5: R = u1 G + u2 Q
  471. */
  472. MBEDTLS_MPI_CHK( mbedtls_ecp_muladd_restartable( grp,
  473. &R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP ) );
  474. if( mbedtls_ecp_is_zero( &R ) )
  475. {
  476. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  477. goto cleanup;
  478. }
  479. /*
  480. * Step 6: convert xR to an integer (no-op)
  481. * Step 7: reduce xR mod n (gives v)
  482. */
  483. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
  484. /*
  485. * Step 8: check if v (that is, R.X) is equal to r
  486. */
  487. if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 )
  488. {
  489. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  490. goto cleanup;
  491. }
  492. cleanup:
  493. mbedtls_ecp_point_free( &R );
  494. mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv );
  495. mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
  496. ECDSA_RS_LEAVE( ver );
  497. return( ret );
  498. }
  499. /*
  500. * Verify ECDSA signature of hashed message
  501. */
  502. int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
  503. const unsigned char *buf, size_t blen,
  504. const mbedtls_ecp_point *Q,
  505. const mbedtls_mpi *r,
  506. const mbedtls_mpi *s)
  507. {
  508. ECDSA_VALIDATE_RET( grp != NULL );
  509. ECDSA_VALIDATE_RET( Q != NULL );
  510. ECDSA_VALIDATE_RET( r != NULL );
  511. ECDSA_VALIDATE_RET( s != NULL );
  512. ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
  513. return( ecdsa_verify_restartable( grp, buf, blen, Q, r, s, NULL ) );
  514. }
  515. #endif /* !MBEDTLS_ECDSA_VERIFY_ALT */
  516. /*
  517. * Convert a signature (given by context) to ASN.1
  518. */
  519. static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
  520. unsigned char *sig, size_t *slen )
  521. {
  522. int ret;
  523. unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
  524. unsigned char *p = buf + sizeof( buf );
  525. size_t len = 0;
  526. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) );
  527. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) );
  528. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) );
  529. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf,
  530. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  531. memcpy( sig, p, len );
  532. *slen = len;
  533. return( 0 );
  534. }
  535. /*
  536. * Compute and write signature
  537. */
  538. int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
  539. mbedtls_md_type_t md_alg,
  540. const unsigned char *hash, size_t hlen,
  541. unsigned char *sig, size_t *slen,
  542. int (*f_rng)(void *, unsigned char *, size_t),
  543. void *p_rng,
  544. mbedtls_ecdsa_restart_ctx *rs_ctx )
  545. {
  546. int ret;
  547. mbedtls_mpi r, s;
  548. ECDSA_VALIDATE_RET( ctx != NULL );
  549. ECDSA_VALIDATE_RET( hash != NULL );
  550. ECDSA_VALIDATE_RET( sig != NULL );
  551. ECDSA_VALIDATE_RET( slen != NULL );
  552. mbedtls_mpi_init( &r );
  553. mbedtls_mpi_init( &s );
  554. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  555. (void) f_rng;
  556. (void) p_rng;
  557. MBEDTLS_MPI_CHK( ecdsa_sign_det_restartable( &ctx->grp, &r, &s, &ctx->d,
  558. hash, hlen, md_alg, rs_ctx ) );
  559. #else
  560. (void) md_alg;
  561. #if defined(MBEDTLS_ECDSA_SIGN_ALT)
  562. MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d,
  563. hash, hlen, f_rng, p_rng ) );
  564. #else
  565. MBEDTLS_MPI_CHK( ecdsa_sign_restartable( &ctx->grp, &r, &s, &ctx->d,
  566. hash, hlen, f_rng, p_rng, rs_ctx ) );
  567. #endif /* MBEDTLS_ECDSA_SIGN_ALT */
  568. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  569. MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
  570. cleanup:
  571. mbedtls_mpi_free( &r );
  572. mbedtls_mpi_free( &s );
  573. return( ret );
  574. }
  575. /*
  576. * Compute and write signature
  577. */
  578. int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
  579. mbedtls_md_type_t md_alg,
  580. const unsigned char *hash, size_t hlen,
  581. unsigned char *sig, size_t *slen,
  582. int (*f_rng)(void *, unsigned char *, size_t),
  583. void *p_rng )
  584. {
  585. ECDSA_VALIDATE_RET( ctx != NULL );
  586. ECDSA_VALIDATE_RET( hash != NULL );
  587. ECDSA_VALIDATE_RET( sig != NULL );
  588. ECDSA_VALIDATE_RET( slen != NULL );
  589. return( mbedtls_ecdsa_write_signature_restartable(
  590. ctx, md_alg, hash, hlen, sig, slen, f_rng, p_rng, NULL ) );
  591. }
  592. #if !defined(MBEDTLS_DEPRECATED_REMOVED) && \
  593. defined(MBEDTLS_ECDSA_DETERMINISTIC)
  594. int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
  595. const unsigned char *hash, size_t hlen,
  596. unsigned char *sig, size_t *slen,
  597. mbedtls_md_type_t md_alg )
  598. {
  599. ECDSA_VALIDATE_RET( ctx != NULL );
  600. ECDSA_VALIDATE_RET( hash != NULL );
  601. ECDSA_VALIDATE_RET( sig != NULL );
  602. ECDSA_VALIDATE_RET( slen != NULL );
  603. return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
  604. NULL, NULL ) );
  605. }
  606. #endif
  607. /*
  608. * Read and check signature
  609. */
  610. int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
  611. const unsigned char *hash, size_t hlen,
  612. const unsigned char *sig, size_t slen )
  613. {
  614. ECDSA_VALIDATE_RET( ctx != NULL );
  615. ECDSA_VALIDATE_RET( hash != NULL );
  616. ECDSA_VALIDATE_RET( sig != NULL );
  617. return( mbedtls_ecdsa_read_signature_restartable(
  618. ctx, hash, hlen, sig, slen, NULL ) );
  619. }
  620. /*
  621. * Restartable read and check signature
  622. */
  623. int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
  624. const unsigned char *hash, size_t hlen,
  625. const unsigned char *sig, size_t slen,
  626. mbedtls_ecdsa_restart_ctx *rs_ctx )
  627. {
  628. int ret;
  629. unsigned char *p = (unsigned char *) sig;
  630. const unsigned char *end = sig + slen;
  631. size_t len;
  632. mbedtls_mpi r, s;
  633. ECDSA_VALIDATE_RET( ctx != NULL );
  634. ECDSA_VALIDATE_RET( hash != NULL );
  635. ECDSA_VALIDATE_RET( sig != NULL );
  636. mbedtls_mpi_init( &r );
  637. mbedtls_mpi_init( &s );
  638. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  639. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  640. {
  641. ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  642. goto cleanup;
  643. }
  644. if( p + len != end )
  645. {
  646. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA +
  647. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
  648. goto cleanup;
  649. }
  650. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 ||
  651. ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 )
  652. {
  653. ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  654. goto cleanup;
  655. }
  656. #if defined(MBEDTLS_ECDSA_VERIFY_ALT)
  657. if( ( ret = mbedtls_ecdsa_verify( &ctx->grp, hash, hlen,
  658. &ctx->Q, &r, &s ) ) != 0 )
  659. goto cleanup;
  660. #else
  661. if( ( ret = ecdsa_verify_restartable( &ctx->grp, hash, hlen,
  662. &ctx->Q, &r, &s, rs_ctx ) ) != 0 )
  663. goto cleanup;
  664. #endif /* MBEDTLS_ECDSA_VERIFY_ALT */
  665. /* At this point we know that the buffer starts with a valid signature.
  666. * Return 0 if the buffer just contains the signature, and a specific
  667. * error code if the valid signature is followed by more data. */
  668. if( p != end )
  669. ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
  670. cleanup:
  671. mbedtls_mpi_free( &r );
  672. mbedtls_mpi_free( &s );
  673. return( ret );
  674. }
  675. #if !defined(MBEDTLS_ECDSA_GENKEY_ALT)
  676. /*
  677. * Generate key pair
  678. */
  679. int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
  680. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  681. {
  682. int ret = 0;
  683. ECDSA_VALIDATE_RET( ctx != NULL );
  684. ECDSA_VALIDATE_RET( f_rng != NULL );
  685. ret = mbedtls_ecp_group_load( &ctx->grp, gid );
  686. if( ret != 0 )
  687. return( ret );
  688. return( mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d,
  689. &ctx->Q, f_rng, p_rng ) );
  690. }
  691. #endif /* !MBEDTLS_ECDSA_GENKEY_ALT */
  692. /*
  693. * Set context from an mbedtls_ecp_keypair
  694. */
  695. int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key )
  696. {
  697. int ret;
  698. ECDSA_VALIDATE_RET( ctx != NULL );
  699. ECDSA_VALIDATE_RET( key != NULL );
  700. if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
  701. ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ||
  702. ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
  703. {
  704. mbedtls_ecdsa_free( ctx );
  705. }
  706. return( ret );
  707. }
  708. /*
  709. * Initialize context
  710. */
  711. void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx )
  712. {
  713. ECDSA_VALIDATE( ctx != NULL );
  714. mbedtls_ecp_keypair_init( ctx );
  715. }
  716. /*
  717. * Free context
  718. */
  719. void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx )
  720. {
  721. if( ctx == NULL )
  722. return;
  723. mbedtls_ecp_keypair_free( ctx );
  724. }
  725. #if defined(MBEDTLS_ECP_RESTARTABLE)
  726. /*
  727. * Initialize a restart context
  728. */
  729. void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx )
  730. {
  731. ECDSA_VALIDATE( ctx != NULL );
  732. mbedtls_ecp_restart_init( &ctx->ecp );
  733. ctx->ver = NULL;
  734. ctx->sig = NULL;
  735. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  736. ctx->det = NULL;
  737. #endif
  738. }
  739. /*
  740. * Free the components of a restart context
  741. */
  742. void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx )
  743. {
  744. if( ctx == NULL )
  745. return;
  746. mbedtls_ecp_restart_free( &ctx->ecp );
  747. ecdsa_restart_ver_free( ctx->ver );
  748. mbedtls_free( ctx->ver );
  749. ctx->ver = NULL;
  750. ecdsa_restart_sig_free( ctx->sig );
  751. mbedtls_free( ctx->sig );
  752. ctx->sig = NULL;
  753. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  754. ecdsa_restart_det_free( ctx->det );
  755. mbedtls_free( ctx->det );
  756. ctx->det = NULL;
  757. #endif
  758. }
  759. #endif /* MBEDTLS_ECP_RESTARTABLE */
  760. #endif /* MBEDTLS_ECDSA_C */