Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

238 rindas
6.7 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/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/nid.h>
  67. #include "internal.h"
  68. #include "../ec/internal.h"
  69. #include "../internal.h"
  70. typedef struct {
  71. /* message digest */
  72. const EVP_MD *md;
  73. } EC_PKEY_CTX;
  74. static int pkey_ec_init(EVP_PKEY_CTX *ctx) {
  75. EC_PKEY_CTX *dctx;
  76. dctx = OPENSSL_malloc(sizeof(EC_PKEY_CTX));
  77. if (!dctx) {
  78. return 0;
  79. }
  80. OPENSSL_memset(dctx, 0, sizeof(EC_PKEY_CTX));
  81. ctx->data = dctx;
  82. return 1;
  83. }
  84. static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) {
  85. EC_PKEY_CTX *dctx, *sctx;
  86. if (!pkey_ec_init(dst)) {
  87. return 0;
  88. }
  89. sctx = src->data;
  90. dctx = dst->data;
  91. dctx->md = sctx->md;
  92. return 1;
  93. }
  94. static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx) {
  95. EC_PKEY_CTX *dctx = ctx->data;
  96. if (!dctx) {
  97. return;
  98. }
  99. OPENSSL_free(dctx);
  100. }
  101. static int pkey_ec_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
  102. const uint8_t *tbs, size_t tbslen) {
  103. unsigned int sltmp;
  104. EC_KEY *ec = ctx->pkey->pkey.ec;
  105. if (!sig) {
  106. *siglen = ECDSA_size(ec);
  107. return 1;
  108. } else if (*siglen < (size_t)ECDSA_size(ec)) {
  109. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  110. return 0;
  111. }
  112. if (!ECDSA_sign(0, tbs, tbslen, sig, &sltmp, ec)) {
  113. return 0;
  114. }
  115. *siglen = (size_t)sltmp;
  116. return 1;
  117. }
  118. static int pkey_ec_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen,
  119. const uint8_t *tbs, size_t tbslen) {
  120. return ECDSA_verify(0, tbs, tbslen, sig, siglen, ctx->pkey->pkey.ec);
  121. }
  122. static int pkey_ec_derive(EVP_PKEY_CTX *ctx, uint8_t *key,
  123. size_t *keylen) {
  124. int ret;
  125. size_t outlen;
  126. const EC_POINT *pubkey = NULL;
  127. EC_KEY *eckey;
  128. if (!ctx->pkey || !ctx->peerkey) {
  129. OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET);
  130. return 0;
  131. }
  132. eckey = ctx->pkey->pkey.ec;
  133. if (!key) {
  134. const EC_GROUP *group;
  135. group = EC_KEY_get0_group(eckey);
  136. *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
  137. return 1;
  138. }
  139. pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
  140. /* NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is
  141. * not an error, the result is truncated. */
  142. outlen = *keylen;
  143. ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
  144. if (ret < 0) {
  145. return 0;
  146. }
  147. *keylen = ret;
  148. return 1;
  149. }
  150. static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
  151. EC_PKEY_CTX *dctx = ctx->data;
  152. switch (type) {
  153. case EVP_PKEY_CTRL_MD:
  154. if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
  155. EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
  156. EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
  157. EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
  158. EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
  159. EVP_MD_type((const EVP_MD *)p2) != NID_sha512) {
  160. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_DIGEST_TYPE);
  161. return 0;
  162. }
  163. dctx->md = p2;
  164. return 1;
  165. case EVP_PKEY_CTRL_GET_MD:
  166. *(const EVP_MD **)p2 = dctx->md;
  167. return 1;
  168. case EVP_PKEY_CTRL_PEER_KEY:
  169. /* Default behaviour is OK */
  170. return 1;
  171. default:
  172. OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  173. return 0;
  174. }
  175. }
  176. static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
  177. if (ctx->pkey == NULL) {
  178. OPENSSL_PUT_ERROR(EVP, EVP_R_NO_PARAMETERS_SET);
  179. return 0;
  180. }
  181. EC_KEY *ec = EC_KEY_new();
  182. if (ec == NULL ||
  183. !EC_KEY_set_group(ec, EC_KEY_get0_group(ctx->pkey->pkey.ec)) ||
  184. !EC_KEY_generate_key(ec)) {
  185. EC_KEY_free(ec);
  186. return 0;
  187. }
  188. EVP_PKEY_assign_EC_KEY(pkey, ec);
  189. return 1;
  190. }
  191. const EVP_PKEY_METHOD ec_pkey_meth = {
  192. EVP_PKEY_EC,
  193. pkey_ec_init,
  194. pkey_ec_copy,
  195. pkey_ec_cleanup,
  196. pkey_ec_keygen,
  197. pkey_ec_sign,
  198. pkey_ec_verify,
  199. 0 /* verify_recover */,
  200. 0 /* encrypt */,
  201. 0 /* decrypt */,
  202. pkey_ec_derive,
  203. pkey_ec_ctrl,
  204. };