Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 58 | #include <string.h> |
| 59 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 60 | #include <openssl/bn.h> |
| 61 | #include <openssl/buf.h> |
| 62 | #include <openssl/digest.h> |
| 63 | #include <openssl/ec.h> |
| 64 | #include <openssl/ec_key.h> |
| 65 | #include <openssl/ecdh.h> |
| 66 | #include <openssl/ecdsa.h> |
| 67 | #include <openssl/err.h> |
| 68 | #include <openssl/mem.h> |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 69 | #include <openssl/nid.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 70 | |
| 71 | #include "internal.h" |
| 72 | #include "../ec/internal.h" |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 73 | #include "../internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 74 | |
| 75 | |
| 76 | typedef struct { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 77 | /* message digest */ |
| 78 | const EVP_MD *md; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 79 | } EC_PKEY_CTX; |
| 80 | |
| 81 | |
| 82 | static int pkey_ec_init(EVP_PKEY_CTX *ctx) { |
| 83 | EC_PKEY_CTX *dctx; |
| 84 | dctx = OPENSSL_malloc(sizeof(EC_PKEY_CTX)); |
| 85 | if (!dctx) { |
| 86 | return 0; |
| 87 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 88 | OPENSSL_memset(dctx, 0, sizeof(EC_PKEY_CTX)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 89 | |
| 90 | ctx->data = dctx; |
| 91 | |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { |
| 96 | EC_PKEY_CTX *dctx, *sctx; |
| 97 | if (!pkey_ec_init(dst)) { |
| 98 | return 0; |
| 99 | } |
| 100 | sctx = src->data; |
| 101 | dctx = dst->data; |
| 102 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 103 | dctx->md = sctx->md; |
| 104 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx) { |
| 109 | EC_PKEY_CTX *dctx = ctx->data; |
| 110 | if (!dctx) { |
| 111 | return; |
| 112 | } |
| 113 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 114 | OPENSSL_free(dctx); |
| 115 | } |
| 116 | |
| 117 | static int pkey_ec_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen, |
| 118 | const uint8_t *tbs, size_t tbslen) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 119 | unsigned int sltmp; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 120 | EC_KEY *ec = ctx->pkey->pkey.ec; |
| 121 | |
| 122 | if (!sig) { |
| 123 | *siglen = ECDSA_size(ec); |
| 124 | return 1; |
| 125 | } else if (*siglen < (size_t)ECDSA_size(ec)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 126 | OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
Brian Smith | 1f5e945 | 2015-09-10 16:20:18 -0700 | [diff] [blame] | 130 | if (!ECDSA_sign(0, tbs, tbslen, sig, &sltmp, ec)) { |
Adam Langley | 5129e2d | 2014-07-25 10:06:44 -0700 | [diff] [blame] | 131 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 132 | } |
| 133 | *siglen = (size_t)sltmp; |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | static int pkey_ec_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen, |
| 138 | const uint8_t *tbs, size_t tbslen) { |
Brian Smith | 1f5e945 | 2015-09-10 16:20:18 -0700 | [diff] [blame] | 139 | return ECDSA_verify(0, tbs, tbslen, sig, siglen, ctx->pkey->pkey.ec); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static int pkey_ec_derive(EVP_PKEY_CTX *ctx, uint8_t *key, |
| 143 | size_t *keylen) { |
| 144 | int ret; |
| 145 | size_t outlen; |
| 146 | const EC_POINT *pubkey = NULL; |
| 147 | EC_KEY *eckey; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 148 | |
| 149 | if (!ctx->pkey || !ctx->peerkey) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 150 | OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
David Benjamin | 1681d79 | 2014-12-30 06:46:52 -0500 | [diff] [blame] | 154 | eckey = ctx->pkey->pkey.ec; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | |
| 156 | if (!key) { |
| 157 | const EC_GROUP *group; |
| 158 | group = EC_KEY_get0_group(eckey); |
| 159 | *keylen = (EC_GROUP_get_degree(group) + 7) / 8; |
| 160 | return 1; |
| 161 | } |
| 162 | pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec); |
| 163 | |
| 164 | /* NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is |
| 165 | * not an error, the result is truncated. */ |
| 166 | |
| 167 | outlen = *keylen; |
| 168 | |
| 169 | ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0); |
| 170 | if (ret < 0) { |
Adam Langley | 5129e2d | 2014-07-25 10:06:44 -0700 | [diff] [blame] | 171 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 172 | } |
| 173 | *keylen = ret; |
| 174 | return 1; |
| 175 | } |
| 176 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 177 | static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) { |
| 178 | EC_PKEY_CTX *dctx = ctx->data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 179 | |
| 180 | switch (type) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 181 | case EVP_PKEY_CTRL_MD: |
| 182 | if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 && |
| 183 | EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 && |
| 184 | EVP_MD_type((const EVP_MD *)p2) != NID_sha224 && |
| 185 | EVP_MD_type((const EVP_MD *)p2) != NID_sha256 && |
| 186 | EVP_MD_type((const EVP_MD *)p2) != NID_sha384 && |
| 187 | EVP_MD_type((const EVP_MD *)p2) != NID_sha512) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 188 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_DIGEST_TYPE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | dctx->md = p2; |
| 192 | return 1; |
| 193 | |
| 194 | case EVP_PKEY_CTRL_GET_MD: |
| 195 | *(const EVP_MD **)p2 = dctx->md; |
| 196 | return 1; |
| 197 | |
| 198 | case EVP_PKEY_CTRL_PEER_KEY: |
David Benjamin | 65ee9b7 | 2015-06-15 15:51:19 -0400 | [diff] [blame] | 199 | /* Default behaviour is OK */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 200 | return 1; |
| 201 | |
| 202 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 203 | OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
David Benjamin | e0ba4ddd | 2015-03-10 17:10:22 -0400 | [diff] [blame] | 204 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 208 | static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) { |
David Benjamin | 692878a | 2015-12-24 21:05:19 -0500 | [diff] [blame] | 209 | if (ctx->pkey == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 210 | OPENSSL_PUT_ERROR(EVP, EVP_R_NO_PARAMETERS_SET); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 211 | return 0; |
| 212 | } |
David Benjamin | 692878a | 2015-12-24 21:05:19 -0500 | [diff] [blame] | 213 | EC_KEY *ec = EC_KEY_new(); |
David Benjamin | 4e98e5c | 2015-12-24 21:07:32 -0500 | [diff] [blame] | 214 | if (ec == NULL || |
| 215 | !EC_KEY_set_group(ec, EC_KEY_get0_group(ctx->pkey->pkey.ec)) || |
| 216 | !EC_KEY_generate_key(ec)) { |
| 217 | EC_KEY_free(ec); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | EVP_PKEY_assign_EC_KEY(pkey, ec); |
David Benjamin | 4e98e5c | 2015-12-24 21:07:32 -0500 | [diff] [blame] | 221 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | const EVP_PKEY_METHOD ec_pkey_meth = { |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 225 | EVP_PKEY_EC, |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 226 | pkey_ec_init, |
| 227 | pkey_ec_copy, |
| 228 | pkey_ec_cleanup, |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 229 | pkey_ec_keygen, |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 230 | pkey_ec_sign, |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 231 | pkey_ec_verify, |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 232 | 0 /* verify_recover */, |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 233 | 0 /* encrypt */, |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 234 | 0 /* decrypt */, |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 235 | pkey_ec_derive, |
| 236 | pkey_ec_ctrl, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 237 | }; |