Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

494 rader
14 KiB

  1. /* ====================================================================
  2. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@OpenSSL.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com). */
  52. #include <openssl/ecdsa.h>
  53. #include <assert.h>
  54. #include <string.h>
  55. #include <openssl/bn.h>
  56. #include <openssl/bytestring.h>
  57. #include <openssl/err.h>
  58. #include <openssl/mem.h>
  59. #include "../ec/internal.h"
  60. int ECDSA_sign(int type, const uint8_t *digest, size_t digest_len, uint8_t *sig,
  61. unsigned int *sig_len, EC_KEY *eckey) {
  62. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  63. return eckey->ecdsa_meth->sign(digest, digest_len, sig, sig_len, eckey);
  64. }
  65. return ECDSA_sign_ex(type, digest, digest_len, sig, sig_len, NULL, NULL,
  66. eckey);
  67. }
  68. int ECDSA_verify(int type, const uint8_t *digest, size_t digest_len,
  69. const uint8_t *sig, size_t sig_len, EC_KEY *eckey) {
  70. ECDSA_SIG *s;
  71. int ret = 0;
  72. uint8_t *der = NULL;
  73. if (eckey->ecdsa_meth && eckey->ecdsa_meth->verify) {
  74. return eckey->ecdsa_meth->verify(digest, digest_len, sig, sig_len, eckey);
  75. }
  76. /* Decode the ECDSA signature. */
  77. s = ECDSA_SIG_from_bytes(sig, sig_len);
  78. if (s == NULL) {
  79. goto err;
  80. }
  81. /* Defend against potential laxness in the DER parser. */
  82. size_t der_len;
  83. if (!ECDSA_SIG_to_bytes(&der, &der_len, s) ||
  84. der_len != sig_len || memcmp(sig, der, sig_len) != 0) {
  85. /* This should never happen. crypto/bytestring is strictly DER. */
  86. OPENSSL_PUT_ERROR(ECDSA, ERR_R_INTERNAL_ERROR);
  87. goto err;
  88. }
  89. ret = ECDSA_do_verify(digest, digest_len, s, eckey);
  90. err:
  91. OPENSSL_free(der);
  92. ECDSA_SIG_free(s);
  93. return ret;
  94. }
  95. /* digest_to_bn interprets |digest_len| bytes from |digest| as a big-endian
  96. * number and sets |out| to that value. It then truncates |out| so that it's,
  97. * at most, as long as |order|. It returns one on success and zero otherwise. */
  98. static int digest_to_bn(BIGNUM *out, const uint8_t *digest, size_t digest_len,
  99. const BIGNUM *order) {
  100. size_t num_bits;
  101. num_bits = BN_num_bits(order);
  102. /* Need to truncate digest if it is too long: first truncate whole
  103. * bytes. */
  104. if (8 * digest_len > num_bits) {
  105. digest_len = (num_bits + 7) / 8;
  106. }
  107. if (!BN_bin2bn(digest, digest_len, out)) {
  108. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  109. return 0;
  110. }
  111. /* If still too long truncate remaining bits with a shift */
  112. if ((8 * digest_len > num_bits) &&
  113. !BN_rshift(out, out, 8 - (num_bits & 0x7))) {
  114. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  115. return 0;
  116. }
  117. return 1;
  118. }
  119. ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
  120. EC_KEY *key) {
  121. return ECDSA_do_sign_ex(digest, digest_len, NULL, NULL, key);
  122. }
  123. int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
  124. const ECDSA_SIG *sig, EC_KEY *eckey) {
  125. int ret = 0;
  126. BN_CTX *ctx;
  127. BIGNUM *order, *u1, *u2, *m, *X;
  128. EC_POINT *point = NULL;
  129. const EC_GROUP *group;
  130. const EC_POINT *pub_key;
  131. if (eckey->ecdsa_meth && eckey->ecdsa_meth->verify) {
  132. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
  133. return 0;
  134. }
  135. /* check input values */
  136. if ((group = EC_KEY_get0_group(eckey)) == NULL ||
  137. (pub_key = EC_KEY_get0_public_key(eckey)) == NULL ||
  138. sig == NULL) {
  139. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_MISSING_PARAMETERS);
  140. return 0;
  141. }
  142. ctx = BN_CTX_new();
  143. if (!ctx) {
  144. OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
  145. return 0;
  146. }
  147. BN_CTX_start(ctx);
  148. order = BN_CTX_get(ctx);
  149. u1 = BN_CTX_get(ctx);
  150. u2 = BN_CTX_get(ctx);
  151. m = BN_CTX_get(ctx);
  152. X = BN_CTX_get(ctx);
  153. if (!X) {
  154. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  155. goto err;
  156. }
  157. if (!EC_GROUP_get_order(group, order, ctx)) {
  158. OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
  159. goto err;
  160. }
  161. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  162. BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
  163. BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
  164. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE);
  165. ret = 0; /* signature is invalid */
  166. goto err;
  167. }
  168. /* calculate tmp1 = inv(S) mod order */
  169. if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
  170. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  171. goto err;
  172. }
  173. if (!digest_to_bn(m, digest, digest_len, order)) {
  174. goto err;
  175. }
  176. /* u1 = m * tmp mod order */
  177. if (!BN_mod_mul(u1, m, u2, order, ctx)) {
  178. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  179. goto err;
  180. }
  181. /* u2 = r * w mod q */
  182. if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
  183. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  184. goto err;
  185. }
  186. point = EC_POINT_new(group);
  187. if (point == NULL) {
  188. OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
  189. goto err;
  190. }
  191. if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
  192. OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
  193. goto err;
  194. }
  195. if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
  196. OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
  197. goto err;
  198. }
  199. if (!BN_nnmod(u1, X, order, ctx)) {
  200. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  201. goto err;
  202. }
  203. /* if the signature is correct u1 is equal to sig->r */
  204. ret = (BN_ucmp(u1, sig->r) == 0);
  205. err:
  206. BN_CTX_end(ctx);
  207. BN_CTX_free(ctx);
  208. EC_POINT_free(point);
  209. return ret;
  210. }
  211. static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  212. BIGNUM **rp, const uint8_t *digest,
  213. size_t digest_len) {
  214. BN_CTX *ctx = NULL;
  215. BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
  216. EC_POINT *tmp_point = NULL;
  217. const EC_GROUP *group;
  218. int ret = 0;
  219. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
  220. OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
  221. return 0;
  222. }
  223. if (ctx_in == NULL) {
  224. if ((ctx = BN_CTX_new()) == NULL) {
  225. OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
  226. return 0;
  227. }
  228. } else {
  229. ctx = ctx_in;
  230. }
  231. k = BN_new(); /* this value is later returned in *kinvp */
  232. r = BN_new(); /* this value is later returned in *rp */
  233. order = BN_new();
  234. X = BN_new();
  235. if (!k || !r || !order || !X) {
  236. OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
  237. goto err;
  238. }
  239. tmp_point = EC_POINT_new(group);
  240. if (tmp_point == NULL) {
  241. OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
  242. goto err;
  243. }
  244. if (!EC_GROUP_get_order(group, order, ctx)) {
  245. OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
  246. goto err;
  247. }
  248. do {
  249. /* If possible, we'll include the private key and message digest in the k
  250. * generation. The |digest| argument is only empty if |ECDSA_sign_setup| is
  251. * being used. */
  252. do {
  253. int ok;
  254. if (digest_len > 0) {
  255. ok = BN_generate_dsa_nonce(k, order, EC_KEY_get0_private_key(eckey),
  256. digest, digest_len, ctx);
  257. } else {
  258. ok = BN_rand_range(k, order);
  259. }
  260. if (!ok) {
  261. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
  262. goto err;
  263. }
  264. } while (BN_is_zero(k));
  265. /* We do not want timing information to leak the length of k,
  266. * so we compute G*k using an equivalent scalar of fixed
  267. * bit-length. */
  268. if (!BN_add(k, k, order)) {
  269. goto err;
  270. }
  271. if (BN_num_bits(k) <= BN_num_bits(order)) {
  272. if (!BN_add(k, k, order)) {
  273. goto err;
  274. }
  275. }
  276. /* compute r the x-coordinate of generator * k */
  277. if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
  278. OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
  279. goto err;
  280. }
  281. if (!EC_POINT_get_affine_coordinates_GFp(group, tmp_point, X, NULL, ctx)) {
  282. OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
  283. goto err;
  284. }
  285. if (!BN_nnmod(r, X, order, ctx)) {
  286. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  287. goto err;
  288. }
  289. } while (BN_is_zero(r));
  290. /* compute the inverse of k */
  291. if (!BN_mod_inverse(k, k, order, ctx)) {
  292. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  293. goto err;
  294. }
  295. /* clear old values if necessary */
  296. BN_clear_free(*rp);
  297. BN_clear_free(*kinvp);
  298. /* save the pre-computed values */
  299. *rp = r;
  300. *kinvp = k;
  301. ret = 1;
  302. err:
  303. if (!ret) {
  304. BN_clear_free(k);
  305. BN_clear_free(r);
  306. }
  307. if (ctx_in == NULL) {
  308. BN_CTX_free(ctx);
  309. }
  310. BN_free(order);
  311. EC_POINT_free(tmp_point);
  312. BN_clear_free(X);
  313. return ret;
  314. }
  315. int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp) {
  316. return ecdsa_sign_setup(eckey, ctx, kinv, rp, NULL, 0);
  317. }
  318. ECDSA_SIG *ECDSA_do_sign_ex(const uint8_t *digest, size_t digest_len,
  319. const BIGNUM *in_kinv, const BIGNUM *in_r,
  320. EC_KEY *eckey) {
  321. int ok = 0;
  322. BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
  323. const BIGNUM *ckinv;
  324. BN_CTX *ctx = NULL;
  325. const EC_GROUP *group;
  326. ECDSA_SIG *ret;
  327. const BIGNUM *priv_key;
  328. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  329. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
  330. return NULL;
  331. }
  332. group = EC_KEY_get0_group(eckey);
  333. priv_key = EC_KEY_get0_private_key(eckey);
  334. if (group == NULL || priv_key == NULL) {
  335. OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
  336. return NULL;
  337. }
  338. ret = ECDSA_SIG_new();
  339. if (!ret) {
  340. OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
  341. return NULL;
  342. }
  343. s = ret->s;
  344. if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
  345. (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
  346. OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
  347. goto err;
  348. }
  349. if (!EC_GROUP_get_order(group, order, ctx)) {
  350. OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
  351. goto err;
  352. }
  353. if (!digest_to_bn(m, digest, digest_len, order)) {
  354. goto err;
  355. }
  356. for (;;) {
  357. if (in_kinv == NULL || in_r == NULL) {
  358. if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, digest, digest_len)) {
  359. OPENSSL_PUT_ERROR(ECDSA, ERR_R_ECDSA_LIB);
  360. goto err;
  361. }
  362. ckinv = kinv;
  363. } else {
  364. ckinv = in_kinv;
  365. if (BN_copy(ret->r, in_r) == NULL) {
  366. OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
  367. goto err;
  368. }
  369. }
  370. if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
  371. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  372. goto err;
  373. }
  374. if (!BN_mod_add_quick(s, tmp, m, order)) {
  375. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  376. goto err;
  377. }
  378. if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
  379. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  380. goto err;
  381. }
  382. if (BN_is_zero(s)) {
  383. /* if kinv and r have been supplied by the caller
  384. * don't to generate new kinv and r values */
  385. if (in_kinv != NULL && in_r != NULL) {
  386. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NEED_NEW_SETUP_VALUES);
  387. goto err;
  388. }
  389. } else {
  390. /* s != 0 => we have a valid signature */
  391. break;
  392. }
  393. }
  394. ok = 1;
  395. err:
  396. if (!ok) {
  397. ECDSA_SIG_free(ret);
  398. ret = NULL;
  399. }
  400. BN_CTX_free(ctx);
  401. BN_clear_free(m);
  402. BN_clear_free(tmp);
  403. BN_free(order);
  404. BN_clear_free(kinv);
  405. return ret;
  406. }
  407. int ECDSA_sign_ex(int type, const uint8_t *digest, size_t digest_len,
  408. uint8_t *sig, unsigned int *sig_len, const BIGNUM *kinv,
  409. const BIGNUM *r, EC_KEY *eckey) {
  410. int ret = 0;
  411. ECDSA_SIG *s = NULL;
  412. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  413. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
  414. *sig_len = 0;
  415. goto err;
  416. }
  417. s = ECDSA_do_sign_ex(digest, digest_len, kinv, r, eckey);
  418. if (s == NULL) {
  419. *sig_len = 0;
  420. goto err;
  421. }
  422. CBB cbb;
  423. CBB_zero(&cbb);
  424. size_t len;
  425. if (!CBB_init_fixed(&cbb, sig, ECDSA_size(eckey)) ||
  426. !ECDSA_SIG_marshal(&cbb, s) ||
  427. !CBB_finish(&cbb, NULL, &len)) {
  428. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_ENCODE_ERROR);
  429. CBB_cleanup(&cbb);
  430. *sig_len = 0;
  431. goto err;
  432. }
  433. *sig_len = (unsigned)len;
  434. ret = 1;
  435. err:
  436. ECDSA_SIG_free(s);
  437. return ret;
  438. }