ecdsa.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. /*
  39. * Derive a suitable integer for group grp from a buffer of length len
  40. * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
  41. */
  42. static int derive_mpi( const mbedtls_ecp_group *grp, mbedtls_mpi *x,
  43. const unsigned char *buf, size_t blen )
  44. {
  45. int ret;
  46. size_t n_size = ( grp->nbits + 7 ) / 8;
  47. size_t use_size = blen > n_size ? n_size : blen;
  48. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( x, buf, use_size ) );
  49. if( use_size * 8 > grp->nbits )
  50. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( x, use_size * 8 - grp->nbits ) );
  51. /* While at it, reduce modulo N */
  52. if( mbedtls_mpi_cmp_mpi( x, &grp->N ) >= 0 )
  53. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( x, x, &grp->N ) );
  54. cleanup:
  55. return( ret );
  56. }
  57. /*
  58. * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
  59. * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
  60. */
  61. int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  62. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  63. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  64. {
  65. int ret, key_tries, sign_tries, blind_tries;
  66. mbedtls_ecp_point R;
  67. mbedtls_mpi k, e, t;
  68. /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
  69. if( grp->N.p == NULL )
  70. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  71. mbedtls_ecp_point_init( &R );
  72. mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t );
  73. sign_tries = 0;
  74. do
  75. {
  76. /*
  77. * Steps 1-3: generate a suitable ephemeral keypair
  78. * and set r = xR mod n
  79. */
  80. key_tries = 0;
  81. do
  82. {
  83. MBEDTLS_MPI_CHK( mbedtls_ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
  84. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( r, &R.X, &grp->N ) );
  85. if( key_tries++ > 10 )
  86. {
  87. ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
  88. goto cleanup;
  89. }
  90. }
  91. while( mbedtls_mpi_cmp_int( r, 0 ) == 0 );
  92. /*
  93. * Step 5: derive MPI from hashed message
  94. */
  95. MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
  96. /*
  97. * Generate a random value to blind inv_mod in next step,
  98. * avoiding a potential timing leak.
  99. */
  100. blind_tries = 0;
  101. do
  102. {
  103. size_t n_size = ( grp->nbits + 7 ) / 8;
  104. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &t, n_size, f_rng, p_rng ) );
  105. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &t, 8 * n_size - grp->nbits ) );
  106. /* See mbedtls_ecp_gen_keypair() */
  107. if( ++blind_tries > 30 )
  108. return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
  109. }
  110. while( mbedtls_mpi_cmp_int( &t, 1 ) < 0 ||
  111. mbedtls_mpi_cmp_mpi( &t, &grp->N ) >= 0 );
  112. /*
  113. * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
  114. */
  115. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, r, d ) );
  116. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) );
  117. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) );
  118. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &k, &k, &t ) );
  119. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, &k, &grp->N ) );
  120. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) );
  121. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) );
  122. if( sign_tries++ > 10 )
  123. {
  124. ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
  125. goto cleanup;
  126. }
  127. }
  128. while( mbedtls_mpi_cmp_int( s, 0 ) == 0 );
  129. cleanup:
  130. mbedtls_ecp_point_free( &R );
  131. mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t );
  132. return( ret );
  133. }
  134. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  135. /*
  136. * Deterministic signature wrapper
  137. */
  138. int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  139. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  140. mbedtls_md_type_t md_alg )
  141. {
  142. int ret;
  143. mbedtls_hmac_drbg_context rng_ctx;
  144. unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
  145. size_t grp_len = ( grp->nbits + 7 ) / 8;
  146. const mbedtls_md_info_t *md_info;
  147. mbedtls_mpi h;
  148. if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
  149. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  150. mbedtls_mpi_init( &h );
  151. mbedtls_hmac_drbg_init( &rng_ctx );
  152. /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
  153. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) );
  154. MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
  155. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) );
  156. mbedtls_hmac_drbg_seed_buf( &rng_ctx, md_info, data, 2 * grp_len );
  157. ret = mbedtls_ecdsa_sign( grp, r, s, d, buf, blen,
  158. mbedtls_hmac_drbg_random, &rng_ctx );
  159. cleanup:
  160. mbedtls_hmac_drbg_free( &rng_ctx );
  161. mbedtls_mpi_free( &h );
  162. return( ret );
  163. }
  164. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  165. /*
  166. * Verify ECDSA signature of hashed message (SEC1 4.1.4)
  167. * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
  168. */
  169. int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
  170. const unsigned char *buf, size_t blen,
  171. const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s)
  172. {
  173. int ret;
  174. mbedtls_mpi e, s_inv, u1, u2;
  175. mbedtls_ecp_point R;
  176. mbedtls_ecp_point_init( &R );
  177. mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv ); mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
  178. /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
  179. if( grp->N.p == NULL )
  180. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  181. /*
  182. * Step 1: make sure r and s are in range 1..n-1
  183. */
  184. if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 ||
  185. mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 )
  186. {
  187. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  188. goto cleanup;
  189. }
  190. /*
  191. * Additional precaution: make sure Q is valid
  192. */
  193. MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
  194. /*
  195. * Step 3: derive MPI from hashed message
  196. */
  197. MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
  198. /*
  199. * Step 4: u1 = e / s mod n, u2 = r / s mod n
  200. */
  201. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) );
  202. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u1, &e, &s_inv ) );
  203. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u1, &u1, &grp->N ) );
  204. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u2, r, &s_inv ) );
  205. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u2, &u2, &grp->N ) );
  206. /*
  207. * Step 5: R = u1 G + u2 Q
  208. *
  209. * Since we're not using any secret data, no need to pass a RNG to
  210. * mbedtls_ecp_mul() for countermesures.
  211. */
  212. MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( grp, &R, &u1, &grp->G, &u2, Q ) );
  213. if( mbedtls_ecp_is_zero( &R ) )
  214. {
  215. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  216. goto cleanup;
  217. }
  218. /*
  219. * Step 6: convert xR to an integer (no-op)
  220. * Step 7: reduce xR mod n (gives v)
  221. */
  222. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
  223. /*
  224. * Step 8: check if v (that is, R.X) is equal to r
  225. */
  226. if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 )
  227. {
  228. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  229. goto cleanup;
  230. }
  231. cleanup:
  232. mbedtls_ecp_point_free( &R );
  233. mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv ); mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
  234. return( ret );
  235. }
  236. /*
  237. * Convert a signature (given by context) to ASN.1
  238. */
  239. static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
  240. unsigned char *sig, size_t *slen )
  241. {
  242. int ret;
  243. unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
  244. unsigned char *p = buf + sizeof( buf );
  245. size_t len = 0;
  246. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) );
  247. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) );
  248. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) );
  249. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf,
  250. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  251. memcpy( sig, p, len );
  252. *slen = len;
  253. return( 0 );
  254. }
  255. /*
  256. * Compute and write signature
  257. */
  258. int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
  259. const unsigned char *hash, size_t hlen,
  260. unsigned char *sig, size_t *slen,
  261. int (*f_rng)(void *, unsigned char *, size_t),
  262. void *p_rng )
  263. {
  264. int ret;
  265. mbedtls_mpi r, s;
  266. mbedtls_mpi_init( &r );
  267. mbedtls_mpi_init( &s );
  268. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  269. (void) f_rng;
  270. (void) p_rng;
  271. MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ctx->grp, &r, &s, &ctx->d,
  272. hash, hlen, md_alg ) );
  273. #else
  274. (void) md_alg;
  275. MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d,
  276. hash, hlen, f_rng, p_rng ) );
  277. #endif
  278. MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
  279. cleanup:
  280. mbedtls_mpi_free( &r );
  281. mbedtls_mpi_free( &s );
  282. return( ret );
  283. }
  284. #if ! defined(MBEDTLS_DEPRECATED_REMOVED) && \
  285. defined(MBEDTLS_ECDSA_DETERMINISTIC)
  286. int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
  287. const unsigned char *hash, size_t hlen,
  288. unsigned char *sig, size_t *slen,
  289. mbedtls_md_type_t md_alg )
  290. {
  291. return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
  292. NULL, NULL ) );
  293. }
  294. #endif
  295. /*
  296. * Read and check signature
  297. */
  298. int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
  299. const unsigned char *hash, size_t hlen,
  300. const unsigned char *sig, size_t slen )
  301. {
  302. int ret;
  303. unsigned char *p = (unsigned char *) sig;
  304. const unsigned char *end = sig + slen;
  305. size_t len;
  306. mbedtls_mpi r, s;
  307. mbedtls_mpi_init( &r );
  308. mbedtls_mpi_init( &s );
  309. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  310. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  311. {
  312. ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  313. goto cleanup;
  314. }
  315. if( p + len != end )
  316. {
  317. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA +
  318. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
  319. goto cleanup;
  320. }
  321. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 ||
  322. ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 )
  323. {
  324. ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  325. goto cleanup;
  326. }
  327. if( ( ret = mbedtls_ecdsa_verify( &ctx->grp, hash, hlen,
  328. &ctx->Q, &r, &s ) ) != 0 )
  329. goto cleanup;
  330. if( p != end )
  331. ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
  332. cleanup:
  333. mbedtls_mpi_free( &r );
  334. mbedtls_mpi_free( &s );
  335. return( ret );
  336. }
  337. /*
  338. * Generate key pair
  339. */
  340. int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
  341. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  342. {
  343. return( mbedtls_ecp_group_load( &ctx->grp, gid ) ||
  344. mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
  345. }
  346. /*
  347. * Set context from an mbedtls_ecp_keypair
  348. */
  349. int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key )
  350. {
  351. int ret;
  352. if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
  353. ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ||
  354. ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
  355. {
  356. mbedtls_ecdsa_free( ctx );
  357. }
  358. return( ret );
  359. }
  360. /*
  361. * Initialize context
  362. */
  363. void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx )
  364. {
  365. mbedtls_ecp_keypair_init( ctx );
  366. }
  367. /*
  368. * Free context
  369. */
  370. void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx )
  371. {
  372. mbedtls_ecp_keypair_free( ctx );
  373. }
  374. #endif /* MBEDTLS_ECDSA_C */