Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

475 рядки
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 <string.h>
  54. #include <openssl/bn.h>
  55. #include <openssl/err.h>
  56. #include <openssl/mem.h>
  57. #include "../ec/internal.h"
  58. int ECDSA_sign(int type, const uint8_t *digest, size_t digest_len, uint8_t *sig,
  59. unsigned int *sig_len, EC_KEY *eckey) {
  60. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  61. return eckey->ecdsa_meth->sign(digest, digest_len, sig, sig_len, eckey);
  62. }
  63. return ECDSA_sign_ex(type, digest, digest_len, sig, sig_len, NULL, NULL,
  64. eckey);
  65. }
  66. int ECDSA_verify(int type, const uint8_t *digest, size_t digest_len,
  67. const uint8_t *sig, size_t sig_len, EC_KEY *eckey) {
  68. ECDSA_SIG *s;
  69. int ret = 0;
  70. uint8_t *der = NULL;
  71. if (eckey->ecdsa_meth && eckey->ecdsa_meth->verify) {
  72. return eckey->ecdsa_meth->verify(digest, digest_len, sig, sig_len, eckey);
  73. }
  74. s = ECDSA_SIG_new();
  75. const uint8_t *sigp = sig;
  76. if (s == NULL || d2i_ECDSA_SIG(&s, &sigp, sig_len) == NULL ||
  77. sigp != sig + sig_len) {
  78. goto err;
  79. }
  80. /* Ensure that the signature uses DER and doesn't have trailing garbage. */
  81. const int der_len = i2d_ECDSA_SIG(s, &der);
  82. if (der_len < 0 || (size_t) der_len != sig_len || memcmp(sig, der, sig_len)) {
  83. goto err;
  84. }
  85. ret = ECDSA_do_verify(digest, digest_len, s, eckey);
  86. err:
  87. OPENSSL_free(der);
  88. ECDSA_SIG_free(s);
  89. return ret;
  90. }
  91. /* digest_to_bn interprets |digest_len| bytes from |digest| as a big-endian
  92. * number and sets |out| to that value. It then truncates |out| so that it's,
  93. * at most, as long as |order|. It returns one on success and zero otherwise. */
  94. static int digest_to_bn(BIGNUM *out, const uint8_t *digest, size_t digest_len,
  95. const BIGNUM *order) {
  96. size_t num_bits;
  97. num_bits = BN_num_bits(order);
  98. /* Need to truncate digest if it is too long: first truncate whole
  99. * bytes. */
  100. if (8 * digest_len > num_bits) {
  101. digest_len = (num_bits + 7) / 8;
  102. }
  103. if (!BN_bin2bn(digest, digest_len, out)) {
  104. OPENSSL_PUT_ERROR(ECDSA, digest_to_bn, ERR_R_BN_LIB);
  105. return 0;
  106. }
  107. /* If still too long truncate remaining bits with a shift */
  108. if ((8 * digest_len > num_bits) &&
  109. !BN_rshift(out, out, 8 - (num_bits & 0x7))) {
  110. OPENSSL_PUT_ERROR(ECDSA, digest_to_bn, ERR_R_BN_LIB);
  111. return 0;
  112. }
  113. return 1;
  114. }
  115. ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
  116. EC_KEY *key) {
  117. return ECDSA_do_sign_ex(digest, digest_len, NULL, NULL, key);
  118. }
  119. int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
  120. const ECDSA_SIG *sig, EC_KEY *eckey) {
  121. int ret = 0;
  122. BN_CTX *ctx;
  123. BIGNUM *order, *u1, *u2, *m, *X;
  124. EC_POINT *point = NULL;
  125. const EC_GROUP *group;
  126. const EC_POINT *pub_key;
  127. if (eckey->ecdsa_meth && eckey->ecdsa_meth->verify) {
  128. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ECDSA_R_NOT_IMPLEMENTED);
  129. return 0;
  130. }
  131. /* check input values */
  132. if ((group = EC_KEY_get0_group(eckey)) == NULL ||
  133. (pub_key = EC_KEY_get0_public_key(eckey)) == NULL ||
  134. sig == NULL) {
  135. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ECDSA_R_MISSING_PARAMETERS);
  136. return 0;
  137. }
  138. ctx = BN_CTX_new();
  139. if (!ctx) {
  140. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_MALLOC_FAILURE);
  141. return 0;
  142. }
  143. BN_CTX_start(ctx);
  144. order = BN_CTX_get(ctx);
  145. u1 = BN_CTX_get(ctx);
  146. u2 = BN_CTX_get(ctx);
  147. m = BN_CTX_get(ctx);
  148. X = BN_CTX_get(ctx);
  149. if (!X) {
  150. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  151. goto err;
  152. }
  153. if (!EC_GROUP_get_order(group, order, ctx)) {
  154. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_EC_LIB);
  155. goto err;
  156. }
  157. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  158. BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
  159. BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
  160. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ECDSA_R_BAD_SIGNATURE);
  161. ret = 0; /* signature is invalid */
  162. goto err;
  163. }
  164. /* calculate tmp1 = inv(S) mod order */
  165. if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
  166. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  167. goto err;
  168. }
  169. if (!digest_to_bn(m, digest, digest_len, order)) {
  170. goto err;
  171. }
  172. /* u1 = m * tmp mod order */
  173. if (!BN_mod_mul(u1, m, u2, order, ctx)) {
  174. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  175. goto err;
  176. }
  177. /* u2 = r * w mod q */
  178. if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
  179. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  180. goto err;
  181. }
  182. point = EC_POINT_new(group);
  183. if (point == NULL) {
  184. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_MALLOC_FAILURE);
  185. goto err;
  186. }
  187. if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
  188. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_EC_LIB);
  189. goto err;
  190. }
  191. if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
  192. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_EC_LIB);
  193. goto err;
  194. }
  195. if (!BN_nnmod(u1, X, order, ctx)) {
  196. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  197. goto err;
  198. }
  199. /* if the signature is correct u1 is equal to sig->r */
  200. ret = (BN_ucmp(u1, sig->r) == 0);
  201. err:
  202. BN_CTX_end(ctx);
  203. BN_CTX_free(ctx);
  204. EC_POINT_free(point);
  205. return ret;
  206. }
  207. static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  208. BIGNUM **rp, const uint8_t *digest,
  209. size_t digest_len) {
  210. BN_CTX *ctx = NULL;
  211. BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
  212. EC_POINT *tmp_point = NULL;
  213. const EC_GROUP *group;
  214. int ret = 0;
  215. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
  216. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_PASSED_NULL_PARAMETER);
  217. return 0;
  218. }
  219. if (ctx_in == NULL) {
  220. if ((ctx = BN_CTX_new()) == NULL) {
  221. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_MALLOC_FAILURE);
  222. return 0;
  223. }
  224. } else {
  225. ctx = ctx_in;
  226. }
  227. k = BN_new(); /* this value is later returned in *kinvp */
  228. r = BN_new(); /* this value is later returned in *rp */
  229. order = BN_new();
  230. X = BN_new();
  231. if (!k || !r || !order || !X) {
  232. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_MALLOC_FAILURE);
  233. goto err;
  234. }
  235. tmp_point = EC_POINT_new(group);
  236. if (tmp_point == NULL) {
  237. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_EC_LIB);
  238. goto err;
  239. }
  240. if (!EC_GROUP_get_order(group, order, ctx)) {
  241. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_EC_LIB);
  242. goto err;
  243. }
  244. do {
  245. /* If possible, we'll include the private key and message digest in the k
  246. * generation. The |digest| argument is only empty if |ECDSA_sign_setup| is
  247. * being used. */
  248. do {
  249. int ok;
  250. if (digest_len > 0) {
  251. ok = BN_generate_dsa_nonce(k, order, EC_KEY_get0_private_key(eckey),
  252. digest, digest_len, ctx);
  253. } else {
  254. ok = BN_rand_range(k, order);
  255. }
  256. if (!ok) {
  257. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup,
  258. ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
  259. goto err;
  260. }
  261. } while (BN_is_zero(k));
  262. /* We do not want timing information to leak the length of k,
  263. * so we compute G*k using an equivalent scalar of fixed
  264. * bit-length. */
  265. if (!BN_add(k, k, order)) {
  266. goto err;
  267. }
  268. if (BN_num_bits(k) <= BN_num_bits(order)) {
  269. if (!BN_add(k, k, order)) {
  270. goto err;
  271. }
  272. }
  273. /* compute r the x-coordinate of generator * k */
  274. if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
  275. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_EC_LIB);
  276. goto err;
  277. }
  278. if (!EC_POINT_get_affine_coordinates_GFp(group, tmp_point, X, NULL, ctx)) {
  279. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_EC_LIB);
  280. goto err;
  281. }
  282. if (!BN_nnmod(r, X, order, ctx)) {
  283. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_BN_LIB);
  284. goto err;
  285. }
  286. } while (BN_is_zero(r));
  287. /* compute the inverse of k */
  288. if (!BN_mod_inverse(k, k, order, ctx)) {
  289. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_BN_LIB);
  290. goto err;
  291. }
  292. /* clear old values if necessary */
  293. BN_clear_free(*rp);
  294. BN_clear_free(*kinvp);
  295. /* save the pre-computed values */
  296. *rp = r;
  297. *kinvp = k;
  298. ret = 1;
  299. err:
  300. if (!ret) {
  301. BN_clear_free(k);
  302. BN_clear_free(r);
  303. }
  304. if (ctx_in == NULL) {
  305. BN_CTX_free(ctx);
  306. }
  307. BN_free(order);
  308. EC_POINT_free(tmp_point);
  309. BN_clear_free(X);
  310. return ret;
  311. }
  312. int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp) {
  313. return ecdsa_sign_setup(eckey, ctx, kinv, rp, NULL, 0);
  314. }
  315. ECDSA_SIG *ECDSA_do_sign_ex(const uint8_t *digest, size_t digest_len,
  316. const BIGNUM *in_kinv, const BIGNUM *in_r,
  317. EC_KEY *eckey) {
  318. int ok = 0;
  319. BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
  320. const BIGNUM *ckinv;
  321. BN_CTX *ctx = NULL;
  322. const EC_GROUP *group;
  323. ECDSA_SIG *ret;
  324. const BIGNUM *priv_key;
  325. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  326. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ECDSA_R_NOT_IMPLEMENTED);
  327. return NULL;
  328. }
  329. group = EC_KEY_get0_group(eckey);
  330. priv_key = EC_KEY_get0_private_key(eckey);
  331. if (group == NULL || priv_key == NULL) {
  332. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_PASSED_NULL_PARAMETER);
  333. return NULL;
  334. }
  335. ret = ECDSA_SIG_new();
  336. if (!ret) {
  337. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_MALLOC_FAILURE);
  338. return NULL;
  339. }
  340. s = ret->s;
  341. if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
  342. (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
  343. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_MALLOC_FAILURE);
  344. goto err;
  345. }
  346. if (!EC_GROUP_get_order(group, order, ctx)) {
  347. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_EC_LIB);
  348. goto err;
  349. }
  350. if (!digest_to_bn(m, digest, digest_len, order)) {
  351. goto err;
  352. }
  353. for (;;) {
  354. if (in_kinv == NULL || in_r == NULL) {
  355. if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, digest, digest_len)) {
  356. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_ECDSA_LIB);
  357. goto err;
  358. }
  359. ckinv = kinv;
  360. } else {
  361. ckinv = in_kinv;
  362. if (BN_copy(ret->r, in_r) == NULL) {
  363. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_MALLOC_FAILURE);
  364. goto err;
  365. }
  366. }
  367. if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
  368. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_BN_LIB);
  369. goto err;
  370. }
  371. if (!BN_mod_add_quick(s, tmp, m, order)) {
  372. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_BN_LIB);
  373. goto err;
  374. }
  375. if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
  376. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_BN_LIB);
  377. goto err;
  378. }
  379. if (BN_is_zero(s)) {
  380. /* if kinv and r have been supplied by the caller
  381. * don't to generate new kinv and r values */
  382. if (in_kinv != NULL && in_r != NULL) {
  383. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ECDSA_R_NEED_NEW_SETUP_VALUES);
  384. goto err;
  385. }
  386. } else {
  387. /* s != 0 => we have a valid signature */
  388. break;
  389. }
  390. }
  391. ok = 1;
  392. err:
  393. if (!ok) {
  394. ECDSA_SIG_free(ret);
  395. ret = NULL;
  396. }
  397. BN_CTX_free(ctx);
  398. BN_clear_free(m);
  399. BN_clear_free(tmp);
  400. BN_free(order);
  401. BN_clear_free(kinv);
  402. return ret;
  403. }
  404. int ECDSA_sign_ex(int type, const uint8_t *digest, size_t digest_len,
  405. uint8_t *sig, unsigned int *sig_len, const BIGNUM *kinv,
  406. const BIGNUM *r, EC_KEY *eckey) {
  407. ECDSA_SIG *s = NULL;
  408. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  409. OPENSSL_PUT_ERROR(ECDSA, ECDSA_sign_ex, ECDSA_R_NOT_IMPLEMENTED);
  410. *sig_len = 0;
  411. return 0;
  412. }
  413. s = ECDSA_do_sign_ex(digest, digest_len, kinv, r, eckey);
  414. if (s == NULL) {
  415. *sig_len = 0;
  416. return 0;
  417. }
  418. *sig_len = i2d_ECDSA_SIG(s, &sig);
  419. ECDSA_SIG_free(s);
  420. return 1;
  421. }