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