Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

422 linhas
11 KiB

  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 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. if (!ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec)) {
  168. return 0;
  169. }
  170. *siglen = (size_t)sltmp;
  171. return 1;
  172. }
  173. static int pkey_ec_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen,
  174. const uint8_t *tbs, size_t tbslen) {
  175. int type;
  176. EC_PKEY_CTX *dctx = ctx->data;
  177. EC_KEY *ec = ctx->pkey->pkey.ec;
  178. type = NID_sha1;
  179. if (dctx->md) {
  180. type = EVP_MD_type(dctx->md);
  181. }
  182. return ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
  183. }
  184. static int pkey_ec_derive(EVP_PKEY_CTX *ctx, uint8_t *key,
  185. size_t *keylen) {
  186. int ret;
  187. size_t outlen;
  188. const EC_POINT *pubkey = NULL;
  189. EC_KEY *eckey;
  190. EC_PKEY_CTX *dctx = ctx->data;
  191. if (!ctx->pkey || !ctx->peerkey) {
  192. OPENSSL_PUT_ERROR(EVP, pkey_ec_derive, EVP_R_KEYS_NOT_SET);
  193. return 0;
  194. }
  195. eckey = dctx->co_key ? dctx->co_key : ctx->pkey->pkey.ec;
  196. if (!key) {
  197. const EC_GROUP *group;
  198. group = EC_KEY_get0_group(eckey);
  199. *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
  200. return 1;
  201. }
  202. pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
  203. /* NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is
  204. * not an error, the result is truncated. */
  205. outlen = *keylen;
  206. ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
  207. if (ret < 0) {
  208. return 0;
  209. }
  210. *keylen = ret;
  211. return 1;
  212. }
  213. static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx, uint8_t *key,
  214. size_t *keylen) {
  215. EC_PKEY_CTX *dctx = ctx->data;
  216. uint8_t *ktmp = NULL;
  217. size_t ktmplen;
  218. int rv = 0;
  219. if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
  220. return pkey_ec_derive(ctx, key, keylen);
  221. }
  222. if (!key) {
  223. *keylen = dctx->kdf_outlen;
  224. return 1;
  225. }
  226. if (*keylen != dctx->kdf_outlen ||
  227. !pkey_ec_derive(ctx, NULL, &ktmplen)) {
  228. return 0;
  229. }
  230. ktmp = OPENSSL_malloc(ktmplen);
  231. if (!ktmp) {
  232. return 0;
  233. }
  234. if (!pkey_ec_derive(ctx, ktmp, &ktmplen)) {
  235. goto err;
  236. }
  237. if (!ECDH_KDF_X9_62(key, *keylen, ktmp, ktmplen, dctx->kdf_ukm,
  238. dctx->kdf_ukmlen, dctx->kdf_md)) {
  239. goto err;
  240. }
  241. rv = 1;
  242. err:
  243. if (ktmp) {
  244. OPENSSL_cleanse(ktmp, ktmplen);
  245. OPENSSL_free(ktmp);
  246. }
  247. return rv;
  248. }
  249. static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
  250. EC_PKEY_CTX *dctx = ctx->data;
  251. EC_GROUP *group;
  252. switch (type) {
  253. case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
  254. group = EC_GROUP_new_by_curve_name(p1);
  255. if (group == NULL) {
  256. OPENSSL_PUT_ERROR(EVP, pkey_ec_ctrl, EVP_R_INVALID_CURVE);
  257. return 0;
  258. }
  259. if (dctx->gen_group)
  260. EC_GROUP_free(dctx->gen_group);
  261. dctx->gen_group = group;
  262. return 1;
  263. case EVP_PKEY_CTRL_EC_KDF_TYPE:
  264. if (p1 == -2)
  265. return dctx->kdf_type;
  266. if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_62)
  267. return -2;
  268. dctx->kdf_type = p1;
  269. return 1;
  270. case EVP_PKEY_CTRL_EC_KDF_MD:
  271. dctx->kdf_md = p2;
  272. return 1;
  273. case EVP_PKEY_CTRL_GET_EC_KDF_MD:
  274. *(const EVP_MD **)p2 = dctx->kdf_md;
  275. return 1;
  276. case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
  277. if (p1 <= 0)
  278. return -2;
  279. dctx->kdf_outlen = (size_t)p1;
  280. return 1;
  281. case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
  282. *(int *)p2 = dctx->kdf_outlen;
  283. return 1;
  284. case EVP_PKEY_CTRL_EC_KDF_UKM:
  285. if (dctx->kdf_ukm)
  286. OPENSSL_free(dctx->kdf_ukm);
  287. dctx->kdf_ukm = p2;
  288. if (p2)
  289. dctx->kdf_ukmlen = p1;
  290. else
  291. dctx->kdf_ukmlen = 0;
  292. return 1;
  293. case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
  294. *(unsigned char **)p2 = dctx->kdf_ukm;
  295. return dctx->kdf_ukmlen;
  296. case EVP_PKEY_CTRL_MD:
  297. if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
  298. EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
  299. EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
  300. EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
  301. EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
  302. EVP_MD_type((const EVP_MD *)p2) != NID_sha512) {
  303. OPENSSL_PUT_ERROR(EVP, pkey_ec_ctrl, EVP_R_INVALID_DIGEST_TYPE);
  304. return 0;
  305. }
  306. dctx->md = p2;
  307. return 1;
  308. case EVP_PKEY_CTRL_GET_MD:
  309. *(const EVP_MD **)p2 = dctx->md;
  310. return 1;
  311. case EVP_PKEY_CTRL_PEER_KEY:
  312. /* Default behaviour is OK */
  313. case EVP_PKEY_CTRL_DIGESTINIT:
  314. return 1;
  315. default:
  316. return -2;
  317. }
  318. }
  319. static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
  320. EC_KEY *ec = NULL;
  321. EC_PKEY_CTX *dctx = ctx->data;
  322. int ret = 0;
  323. if (dctx->gen_group == NULL) {
  324. OPENSSL_PUT_ERROR(EVP, pkey_ec_paramgen, EVP_R_NO_PARAMETERS_SET);
  325. return 0;
  326. }
  327. ec = EC_KEY_new();
  328. if (!ec) {
  329. return 0;
  330. }
  331. ret = EC_KEY_set_group(ec, dctx->gen_group);
  332. if (ret) {
  333. EVP_PKEY_assign_EC_KEY(pkey, ec);
  334. } else {
  335. EC_KEY_free(ec);
  336. }
  337. return ret;
  338. }
  339. static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
  340. EC_KEY *ec = NULL;
  341. EC_PKEY_CTX *dctx = ctx->data;
  342. if (ctx->pkey == NULL && dctx->gen_group == NULL) {
  343. OPENSSL_PUT_ERROR(EVP, pkey_ec_keygen, EVP_R_NO_PARAMETERS_SET);
  344. return 0;
  345. }
  346. ec = EC_KEY_new();
  347. if (!ec) {
  348. return 0;
  349. }
  350. EVP_PKEY_assign_EC_KEY(pkey, ec);
  351. if (ctx->pkey) {
  352. /* Note: if error return, pkey is freed by parent routine */
  353. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey)) {
  354. return 0;
  355. }
  356. } else {
  357. if (!EC_KEY_set_group(ec, dctx->gen_group)) {
  358. return 0;
  359. }
  360. }
  361. return EC_KEY_generate_key(pkey->pkey.ec);
  362. }
  363. const EVP_PKEY_METHOD ec_pkey_meth = {
  364. EVP_PKEY_EC, 0 /* flags */, pkey_ec_init,
  365. pkey_ec_copy, pkey_ec_cleanup, 0 /* paramgen_init */,
  366. pkey_ec_paramgen, 0 /* keygen_init */, pkey_ec_keygen,
  367. 0 /* sign_init */, pkey_ec_sign, 0 /* verify_init */,
  368. pkey_ec_verify, 0 /* signctx_init */, 0 /* signctx */,
  369. 0 /* verifyctx_init */, 0 /* verifyctx */, 0 /* encrypt_init */,
  370. 0 /* encrypt */, 0 /* decrypt_init */, 0 /* decrypt */,
  371. 0 /* derive_init */, pkey_ec_kdf_derive, pkey_ec_ctrl,
  372. };