選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

p_ec.c 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 2006.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com). */
  55. #include <openssl/evp.h>
  56. #include <openssl/asn1.h>
  57. #include <openssl/bn.h>
  58. #include <openssl/buf.h>
  59. #include <openssl/digest.h>
  60. #include <openssl/ec.h>
  61. #include <openssl/ec_key.h>
  62. #include <openssl/ecdh.h>
  63. #include <openssl/ecdsa.h>
  64. #include <openssl/err.h>
  65. #include <openssl/mem.h>
  66. #include <openssl/obj.h>
  67. #include "internal.h"
  68. #include "../ec/internal.h"
  69. typedef struct {
  70. /* Key and paramgen group */
  71. EC_GROUP *gen_group;
  72. /* message digest */
  73. const EVP_MD *md;
  74. /* Duplicate key if custom cofactor needed */
  75. EC_KEY *co_key;
  76. /* Cofactor mode */
  77. signed char cofactor_mode;
  78. /* KDF (if any) to use for ECDH */
  79. char kdf_type;
  80. /* Message digest to use for key derivation */
  81. const EVP_MD *kdf_md;
  82. /* User key material */
  83. unsigned char *kdf_ukm;
  84. size_t kdf_ukmlen;
  85. /* KDF output length */
  86. size_t kdf_outlen;
  87. } EC_PKEY_CTX;
  88. static int pkey_ec_init(EVP_PKEY_CTX *ctx) {
  89. EC_PKEY_CTX *dctx;
  90. dctx = OPENSSL_malloc(sizeof(EC_PKEY_CTX));
  91. if (!dctx) {
  92. return 0;
  93. }
  94. memset(dctx, 0, sizeof(EC_PKEY_CTX));
  95. dctx->cofactor_mode = -1;
  96. dctx->kdf_type = EVP_PKEY_ECDH_KDF_NONE;
  97. ctx->data = dctx;
  98. return 1;
  99. }
  100. static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) {
  101. EC_PKEY_CTX *dctx, *sctx;
  102. if (!pkey_ec_init(dst)) {
  103. return 0;
  104. }
  105. sctx = src->data;
  106. dctx = dst->data;
  107. if (sctx->gen_group) {
  108. dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
  109. if (!dctx->gen_group) {
  110. return 0;
  111. }
  112. }
  113. dctx->md = sctx->md;
  114. if (sctx->co_key) {
  115. dctx->co_key = EC_KEY_dup(sctx->co_key);
  116. if (!dctx->co_key) {
  117. return 0;
  118. }
  119. }
  120. dctx->kdf_type = sctx->kdf_type;
  121. dctx->kdf_md = sctx->kdf_md;
  122. dctx->kdf_outlen = sctx->kdf_outlen;
  123. if (sctx->kdf_ukm) {
  124. dctx->kdf_ukm = BUF_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen);
  125. if (!dctx->kdf_ukm) {
  126. return 0;
  127. }
  128. } else {
  129. dctx->kdf_ukm = NULL;
  130. }
  131. dctx->kdf_ukmlen = sctx->kdf_ukmlen;
  132. return 1;
  133. }
  134. static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx) {
  135. EC_PKEY_CTX *dctx = ctx->data;
  136. if (!dctx) {
  137. return;
  138. }
  139. if (dctx->gen_group) {
  140. EC_GROUP_free(dctx->gen_group);
  141. }
  142. if (dctx->co_key) {
  143. EC_KEY_free(dctx->co_key);
  144. }
  145. if (dctx->kdf_ukm) {
  146. OPENSSL_free(dctx->kdf_ukm);
  147. }
  148. OPENSSL_free(dctx);
  149. }
  150. static int pkey_ec_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
  151. const uint8_t *tbs, size_t tbslen) {
  152. int ret, type;
  153. unsigned int sltmp;
  154. EC_PKEY_CTX *dctx = ctx->data;
  155. EC_KEY *ec = ctx->pkey->pkey.ec;
  156. if (!sig) {
  157. *siglen = ECDSA_size(ec);
  158. return 1;
  159. } else if (*siglen < (size_t)ECDSA_size(ec)) {
  160. OPENSSL_PUT_ERROR(EVP, pkey_ec_sign, EVP_R_BUFFER_TOO_SMALL);
  161. return 0;
  162. }
  163. type = NID_sha1;
  164. if (dctx->md) {
  165. type = EVP_MD_type(dctx->md);
  166. }
  167. ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
  168. if (ret <= 0) {
  169. return ret;
  170. }
  171. *siglen = (size_t)sltmp;
  172. return 1;
  173. }
  174. static int pkey_ec_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen,
  175. const uint8_t *tbs, size_t tbslen) {
  176. int type;
  177. EC_PKEY_CTX *dctx = ctx->data;
  178. EC_KEY *ec = ctx->pkey->pkey.ec;
  179. type = NID_sha1;
  180. if (dctx->md) {
  181. type = EVP_MD_type(dctx->md);
  182. }
  183. return ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
  184. }
  185. static int pkey_ec_derive(EVP_PKEY_CTX *ctx, uint8_t *key,
  186. size_t *keylen) {
  187. int ret;
  188. size_t outlen;
  189. const EC_POINT *pubkey = NULL;
  190. EC_KEY *eckey;
  191. EC_PKEY_CTX *dctx = ctx->data;
  192. if (!ctx->pkey || !ctx->peerkey) {
  193. OPENSSL_PUT_ERROR(EVP, pkey_ec_derive, EVP_R_KEYS_NOT_SET);
  194. return 0;
  195. }
  196. eckey = dctx->co_key ? dctx->co_key : ctx->pkey->pkey.ec;
  197. if (!key) {
  198. const EC_GROUP *group;
  199. group = EC_KEY_get0_group(eckey);
  200. *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
  201. return 1;
  202. }
  203. pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
  204. /* NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is
  205. * not an error, the result is truncated. */
  206. outlen = *keylen;
  207. ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
  208. if (ret < 0) {
  209. return ret;
  210. }
  211. *keylen = ret;
  212. return 1;
  213. }
  214. static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx, uint8_t *key,
  215. size_t *keylen) {
  216. EC_PKEY_CTX *dctx = ctx->data;
  217. uint8_t *ktmp = NULL;
  218. size_t ktmplen;
  219. int rv = 0;
  220. if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
  221. return pkey_ec_derive(ctx, key, keylen);
  222. }
  223. if (!key) {
  224. *keylen = dctx->kdf_outlen;
  225. return 1;
  226. }
  227. if (*keylen != dctx->kdf_outlen ||
  228. !pkey_ec_derive(ctx, NULL, &ktmplen)) {
  229. return 0;
  230. }
  231. ktmp = OPENSSL_malloc(ktmplen);
  232. if (!ktmp) {
  233. return 0;
  234. }
  235. if (!pkey_ec_derive(ctx, ktmp, &ktmplen)) {
  236. goto err;
  237. }
  238. if (!ECDH_KDF_X9_62(key, *keylen, ktmp, ktmplen, dctx->kdf_ukm,
  239. dctx->kdf_ukmlen, dctx->kdf_md)) {
  240. goto err;
  241. }
  242. rv = 1;
  243. err:
  244. if (ktmp) {
  245. OPENSSL_cleanse(ktmp, ktmplen);
  246. OPENSSL_free(ktmp);
  247. }
  248. return rv;
  249. }
  250. static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
  251. EC_PKEY_CTX *dctx = ctx->data;
  252. EC_GROUP *group;
  253. switch (type) {
  254. case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
  255. group = EC_GROUP_new_by_curve_name(p1);
  256. if (group == NULL) {
  257. OPENSSL_PUT_ERROR(EVP, pkey_ec_ctrl, EVP_R_INVALID_CURVE);
  258. return 0;
  259. }
  260. if (dctx->gen_group)
  261. EC_GROUP_free(dctx->gen_group);
  262. dctx->gen_group = group;
  263. return 1;
  264. case EVP_PKEY_CTRL_EC_KDF_TYPE:
  265. if (p1 == -2)
  266. return dctx->kdf_type;
  267. if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_62)
  268. return -2;
  269. dctx->kdf_type = p1;
  270. return 1;
  271. case EVP_PKEY_CTRL_EC_KDF_MD:
  272. dctx->kdf_md = p2;
  273. return 1;
  274. case EVP_PKEY_CTRL_GET_EC_KDF_MD:
  275. *(const EVP_MD **)p2 = dctx->kdf_md;
  276. return 1;
  277. case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
  278. if (p1 <= 0)
  279. return -2;
  280. dctx->kdf_outlen = (size_t)p1;
  281. return 1;
  282. case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
  283. *(int *)p2 = dctx->kdf_outlen;
  284. return 1;
  285. case EVP_PKEY_CTRL_EC_KDF_UKM:
  286. if (dctx->kdf_ukm)
  287. OPENSSL_free(dctx->kdf_ukm);
  288. dctx->kdf_ukm = p2;
  289. if (p2)
  290. dctx->kdf_ukmlen = p1;
  291. else
  292. dctx->kdf_ukmlen = 0;
  293. return 1;
  294. case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
  295. *(unsigned char **)p2 = dctx->kdf_ukm;
  296. return dctx->kdf_ukmlen;
  297. case EVP_PKEY_CTRL_MD:
  298. if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
  299. EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
  300. EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
  301. EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
  302. EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
  303. EVP_MD_type((const EVP_MD *)p2) != NID_sha512) {
  304. OPENSSL_PUT_ERROR(EVP, pkey_ec_ctrl, EVP_R_INVALID_DIGEST_TYPE);
  305. return 0;
  306. }
  307. dctx->md = p2;
  308. return 1;
  309. case EVP_PKEY_CTRL_GET_MD:
  310. *(const EVP_MD **)p2 = dctx->md;
  311. return 1;
  312. case EVP_PKEY_CTRL_PEER_KEY:
  313. /* Default behaviour is OK */
  314. case EVP_PKEY_CTRL_DIGESTINIT:
  315. return 1;
  316. default:
  317. return -2;
  318. }
  319. }
  320. static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
  321. EC_KEY *ec = NULL;
  322. EC_PKEY_CTX *dctx = ctx->data;
  323. int ret = 0;
  324. if (dctx->gen_group == NULL) {
  325. OPENSSL_PUT_ERROR(EVP, pkey_ec_paramgen, EVP_R_NO_PARAMETERS_SET);
  326. return 0;
  327. }
  328. ec = EC_KEY_new();
  329. if (!ec) {
  330. return 0;
  331. }
  332. ret = EC_KEY_set_group(ec, dctx->gen_group);
  333. if (ret) {
  334. EVP_PKEY_assign_EC_KEY(pkey, ec);
  335. } else {
  336. EC_KEY_free(ec);
  337. }
  338. return ret;
  339. }
  340. static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
  341. EC_KEY *ec = NULL;
  342. EC_PKEY_CTX *dctx = ctx->data;
  343. if (ctx->pkey == NULL && dctx->gen_group == NULL) {
  344. OPENSSL_PUT_ERROR(EVP, pkey_ec_keygen, EVP_R_NO_PARAMETERS_SET);
  345. return 0;
  346. }
  347. ec = EC_KEY_new();
  348. if (!ec) {
  349. return 0;
  350. }
  351. EVP_PKEY_assign_EC_KEY(pkey, ec);
  352. if (ctx->pkey) {
  353. /* Note: if error return, pkey is freed by parent routine */
  354. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey)) {
  355. return 0;
  356. }
  357. } else {
  358. if (!EC_KEY_set_group(ec, dctx->gen_group)) {
  359. return 0;
  360. }
  361. }
  362. return EC_KEY_generate_key(pkey->pkey.ec);
  363. }
  364. const EVP_PKEY_METHOD ec_pkey_meth = {
  365. EVP_PKEY_EC, 0 /* flags */, pkey_ec_init,
  366. pkey_ec_copy, pkey_ec_cleanup, 0 /* paramgen_init */,
  367. pkey_ec_paramgen, 0 /* keygen_init */, pkey_ec_keygen,
  368. 0 /* sign_init */, pkey_ec_sign, 0 /* verify_init */,
  369. pkey_ec_verify, 0 /* signctx_init */, 0 /* signctx */,
  370. 0 /* verifyctx_init */, 0 /* verifyctx */, 0 /* encrypt_init */,
  371. 0 /* encrypt */, 0 /* decrypt_init */, 0 /* decrypt */,
  372. 0 /* derive_init */, pkey_ec_kdf_derive, pkey_ec_ctrl,
  373. };