Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

302 righe
8.5 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 <string.h>
  57. #include <openssl/asn1.h>
  58. #include <openssl/bn.h>
  59. #include <openssl/buf.h>
  60. #include <openssl/digest.h>
  61. #include <openssl/ec.h>
  62. #include <openssl/ec_key.h>
  63. #include <openssl/ecdh.h>
  64. #include <openssl/ecdsa.h>
  65. #include <openssl/err.h>
  66. #include <openssl/mem.h>
  67. #include <openssl/obj.h>
  68. #include "internal.h"
  69. #include "../ec/internal.h"
  70. typedef struct {
  71. /* Key and paramgen group */
  72. EC_GROUP *gen_group;
  73. /* message digest */
  74. const EVP_MD *md;
  75. } EC_PKEY_CTX;
  76. static int pkey_ec_init(EVP_PKEY_CTX *ctx) {
  77. EC_PKEY_CTX *dctx;
  78. dctx = OPENSSL_malloc(sizeof(EC_PKEY_CTX));
  79. if (!dctx) {
  80. return 0;
  81. }
  82. memset(dctx, 0, sizeof(EC_PKEY_CTX));
  83. ctx->data = dctx;
  84. return 1;
  85. }
  86. static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) {
  87. EC_PKEY_CTX *dctx, *sctx;
  88. if (!pkey_ec_init(dst)) {
  89. return 0;
  90. }
  91. sctx = src->data;
  92. dctx = dst->data;
  93. if (sctx->gen_group) {
  94. dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
  95. if (!dctx->gen_group) {
  96. return 0;
  97. }
  98. }
  99. dctx->md = sctx->md;
  100. return 1;
  101. }
  102. static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx) {
  103. EC_PKEY_CTX *dctx = ctx->data;
  104. if (!dctx) {
  105. return;
  106. }
  107. EC_GROUP_free(dctx->gen_group);
  108. OPENSSL_free(dctx);
  109. }
  110. static int pkey_ec_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
  111. const uint8_t *tbs, size_t tbslen) {
  112. int type;
  113. unsigned int sltmp;
  114. EC_PKEY_CTX *dctx = ctx->data;
  115. EC_KEY *ec = ctx->pkey->pkey.ec;
  116. if (!sig) {
  117. *siglen = ECDSA_size(ec);
  118. return 1;
  119. } else if (*siglen < (size_t)ECDSA_size(ec)) {
  120. OPENSSL_PUT_ERROR(EVP, pkey_ec_sign, EVP_R_BUFFER_TOO_SMALL);
  121. return 0;
  122. }
  123. type = NID_sha1;
  124. if (dctx->md) {
  125. type = EVP_MD_type(dctx->md);
  126. }
  127. if (!ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec)) {
  128. return 0;
  129. }
  130. *siglen = (size_t)sltmp;
  131. return 1;
  132. }
  133. static int pkey_ec_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen,
  134. const uint8_t *tbs, size_t tbslen) {
  135. int type;
  136. EC_PKEY_CTX *dctx = ctx->data;
  137. EC_KEY *ec = ctx->pkey->pkey.ec;
  138. type = NID_sha1;
  139. if (dctx->md) {
  140. type = EVP_MD_type(dctx->md);
  141. }
  142. return ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
  143. }
  144. static int pkey_ec_derive(EVP_PKEY_CTX *ctx, uint8_t *key,
  145. size_t *keylen) {
  146. int ret;
  147. size_t outlen;
  148. const EC_POINT *pubkey = NULL;
  149. EC_KEY *eckey;
  150. if (!ctx->pkey || !ctx->peerkey) {
  151. OPENSSL_PUT_ERROR(EVP, pkey_ec_derive, EVP_R_KEYS_NOT_SET);
  152. return 0;
  153. }
  154. eckey = ctx->pkey->pkey.ec;
  155. if (!key) {
  156. const EC_GROUP *group;
  157. group = EC_KEY_get0_group(eckey);
  158. *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
  159. return 1;
  160. }
  161. pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
  162. /* NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is
  163. * not an error, the result is truncated. */
  164. outlen = *keylen;
  165. ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
  166. if (ret < 0) {
  167. return 0;
  168. }
  169. *keylen = ret;
  170. return 1;
  171. }
  172. static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
  173. EC_PKEY_CTX *dctx = ctx->data;
  174. EC_GROUP *group;
  175. switch (type) {
  176. case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
  177. group = EC_GROUP_new_by_curve_name(p1);
  178. if (group == NULL) {
  179. OPENSSL_PUT_ERROR(EVP, pkey_ec_ctrl, EVP_R_INVALID_CURVE);
  180. return 0;
  181. }
  182. EC_GROUP_free(dctx->gen_group);
  183. dctx->gen_group = group;
  184. return 1;
  185. case EVP_PKEY_CTRL_MD:
  186. if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
  187. EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
  188. EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
  189. EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
  190. EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
  191. EVP_MD_type((const EVP_MD *)p2) != NID_sha512) {
  192. OPENSSL_PUT_ERROR(EVP, pkey_ec_ctrl, EVP_R_INVALID_DIGEST_TYPE);
  193. return 0;
  194. }
  195. dctx->md = p2;
  196. return 1;
  197. case EVP_PKEY_CTRL_GET_MD:
  198. *(const EVP_MD **)p2 = dctx->md;
  199. return 1;
  200. case EVP_PKEY_CTRL_PEER_KEY:
  201. /* Default behaviour is OK */
  202. case EVP_PKEY_CTRL_DIGESTINIT:
  203. return 1;
  204. default:
  205. OPENSSL_PUT_ERROR(EVP, pkey_ec_ctrl, EVP_R_COMMAND_NOT_SUPPORTED);
  206. return 0;
  207. }
  208. }
  209. static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
  210. EC_KEY *ec = NULL;
  211. EC_PKEY_CTX *dctx = ctx->data;
  212. int ret = 0;
  213. if (dctx->gen_group == NULL) {
  214. OPENSSL_PUT_ERROR(EVP, pkey_ec_paramgen, EVP_R_NO_PARAMETERS_SET);
  215. return 0;
  216. }
  217. ec = EC_KEY_new();
  218. if (!ec) {
  219. return 0;
  220. }
  221. ret = EC_KEY_set_group(ec, dctx->gen_group);
  222. if (ret) {
  223. EVP_PKEY_assign_EC_KEY(pkey, ec);
  224. } else {
  225. EC_KEY_free(ec);
  226. }
  227. return ret;
  228. }
  229. static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
  230. EC_KEY *ec = NULL;
  231. EC_PKEY_CTX *dctx = ctx->data;
  232. if (ctx->pkey == NULL && dctx->gen_group == NULL) {
  233. OPENSSL_PUT_ERROR(EVP, pkey_ec_keygen, EVP_R_NO_PARAMETERS_SET);
  234. return 0;
  235. }
  236. ec = EC_KEY_new();
  237. if (!ec) {
  238. return 0;
  239. }
  240. EVP_PKEY_assign_EC_KEY(pkey, ec);
  241. if (ctx->pkey) {
  242. /* Note: if error return, pkey is freed by parent routine */
  243. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey)) {
  244. return 0;
  245. }
  246. } else {
  247. if (!EC_KEY_set_group(ec, dctx->gen_group)) {
  248. return 0;
  249. }
  250. }
  251. return EC_KEY_generate_key(pkey->pkey.ec);
  252. }
  253. const EVP_PKEY_METHOD ec_pkey_meth = {
  254. EVP_PKEY_EC, 0 /* flags */, pkey_ec_init,
  255. pkey_ec_copy, pkey_ec_cleanup, 0 /* paramgen_init */,
  256. pkey_ec_paramgen, 0 /* keygen_init */, pkey_ec_keygen,
  257. 0 /* sign_init */, pkey_ec_sign, 0 /* verify_init */,
  258. pkey_ec_verify, 0 /* signctx_init */, 0 /* signctx */,
  259. 0 /* verifyctx_init */, 0 /* verifyctx */, 0 /* encrypt_init */,
  260. 0 /* encrypt */, 0 /* decrypt_init */, 0 /* decrypt */,
  261. 0 /* derive_init */, pkey_ec_derive, pkey_ec_ctrl,
  262. };