| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/evp.h> |
| 16 |
| 17 | #include <string.h> |
| 18 |
| 19 | #include <openssl/bn.h> |
| 20 | #include <openssl/digest.h> |
| 21 | #include <openssl/ec.h> |
| 22 | #include <openssl/ec_key.h> |
| 23 | #include <openssl/ecdh.h> |
| 24 | #include <openssl/ecdsa.h> |
| 25 | #include <openssl/err.h> |
| 26 | #include <openssl/mem.h> |
| 27 | #include <openssl/nid.h> |
| 28 |
| 29 | #include "../fipsmodule/ec/internal.h" |
| 30 | #include "../internal.h" |
| 31 | #include "internal.h" |
| 32 |
| 33 |
| 34 | typedef struct { |
| 35 | // message digest |
| 36 | const EVP_MD *md; |
| 37 | const EC_GROUP *gen_group; |
| 38 | } EC_PKEY_CTX; |
| 39 |
| 40 |
| 41 | static int pkey_ec_init(EVP_PKEY_CTX *ctx) { |
| 42 | EC_PKEY_CTX *dctx = |
| 43 | reinterpret_cast<EC_PKEY_CTX *>(OPENSSL_zalloc(sizeof(EC_PKEY_CTX))); |
| 44 | if (!dctx) { |
| 45 | return 0; |
| 46 | } |
| 47 |
| 48 | ctx->data = dctx; |
| 49 | return 1; |
| 50 | } |
| 51 |
| 52 | static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { |
| 53 | if (!pkey_ec_init(dst)) { |
| 54 | return 0; |
| 55 | } |
| 56 |
| 57 | const EC_PKEY_CTX *sctx = reinterpret_cast<EC_PKEY_CTX *>(src->data); |
| 58 | EC_PKEY_CTX *dctx = reinterpret_cast<EC_PKEY_CTX *>(dst->data); |
| 59 | dctx->md = sctx->md; |
| 60 | dctx->gen_group = sctx->gen_group; |
| 61 | return 1; |
| 62 | } |
| 63 |
| 64 | static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx) { |
| 65 | EC_PKEY_CTX *dctx = reinterpret_cast<EC_PKEY_CTX *>(ctx->data); |
| 66 | if (!dctx) { |
| 67 | return; |
| 68 | } |
| 69 |
| 70 | OPENSSL_free(dctx); |
| 71 | } |
| 72 |
| 73 | static int pkey_ec_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen, |
| 74 | const uint8_t *tbs, size_t tbslen) { |
| 75 | const EC_KEY *ec = reinterpret_cast<EC_KEY *>(ctx->pkey->pkey); |
| 76 | if (!sig) { |
| 77 | *siglen = ECDSA_size(ec); |
| 78 | return 1; |
| 79 | } else if (*siglen < (size_t)ECDSA_size(ec)) { |
| 80 | OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); |
| 81 | return 0; |
| 82 | } |
| 83 |
| 84 | unsigned int sltmp; |
| 85 | if (!ECDSA_sign(0, tbs, tbslen, sig, &sltmp, ec)) { |
| 86 | return 0; |
| 87 | } |
| 88 | *siglen = (size_t)sltmp; |
| 89 | return 1; |
| 90 | } |
| 91 |
| 92 | static int pkey_ec_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen, |
| 93 | const uint8_t *tbs, size_t tbslen) { |
| 94 | const EC_KEY *ec_key = reinterpret_cast<EC_KEY *>(ctx->pkey->pkey); |
| 95 | return ECDSA_verify(0, tbs, tbslen, sig, siglen, ec_key); |
| 96 | } |
| 97 |
| 98 | static int pkey_ec_derive(EVP_PKEY_CTX *ctx, uint8_t *key, size_t *keylen) { |
| 99 | if (!ctx->pkey || !ctx->peerkey) { |
| 100 | OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET); |
| 101 | return 0; |
| 102 | } |
| 103 |
| 104 | const EC_KEY *eckey = reinterpret_cast<EC_KEY *>(ctx->pkey->pkey); |
| 105 | if (!key) { |
| 106 | const EC_GROUP *group; |
| 107 | group = EC_KEY_get0_group(eckey); |
| 108 | *keylen = (EC_GROUP_get_degree(group) + 7) / 8; |
| 109 | return 1; |
| 110 | } |
| 111 |
| 112 | const EC_KEY *eckey_peer = reinterpret_cast<EC_KEY *>(ctx->peerkey->pkey); |
| 113 | const EC_POINT *pubkey = EC_KEY_get0_public_key(eckey_peer); |
| 114 |
| 115 | // NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is |
| 116 | // not an error, the result is truncated. |
| 117 | size_t outlen = *keylen; |
| 118 | int ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0); |
| 119 | if (ret < 0) { |
| 120 | return 0; |
| 121 | } |
| 122 | *keylen = ret; |
| 123 | return 1; |
| 124 | } |
| 125 |
| 126 | static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) { |
| 127 | EC_PKEY_CTX *dctx = reinterpret_cast<EC_PKEY_CTX *>(ctx->data); |
| 128 |
| 129 | switch (type) { |
| 130 | case EVP_PKEY_CTRL_MD: { |
| 131 | const EVP_MD *md = reinterpret_cast<const EVP_MD *>(p2); |
| 132 | int md_type = EVP_MD_type(md); |
| 133 | if (md_type != NID_sha1 && md_type != NID_sha224 && |
| 134 | md_type != NID_sha256 && md_type != NID_sha384 && |
| 135 | md_type != NID_sha512) { |
| 136 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_DIGEST_TYPE); |
| 137 | return 0; |
| 138 | } |
| 139 | dctx->md = md; |
| 140 | return 1; |
| 141 | } |
| 142 |
| 143 | case EVP_PKEY_CTRL_GET_MD: |
| 144 | *(const EVP_MD **)p2 = dctx->md; |
| 145 | return 1; |
| 146 |
| 147 | case EVP_PKEY_CTRL_PEER_KEY: |
| 148 | // Default behaviour is OK |
| 149 | return 1; |
| 150 |
| 151 | case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID: { |
| 152 | const EC_GROUP *group = EC_GROUP_new_by_curve_name(p1); |
| 153 | if (group == NULL) { |
| 154 | return 0; |
| 155 | } |
| 156 | dctx->gen_group = group; |
| 157 | return 1; |
| 158 | } |
| 159 |
| 160 | default: |
| 161 | OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 162 | return 0; |
| 163 | } |
| 164 | } |
| 165 |
| 166 | static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) { |
| 167 | EC_PKEY_CTX *dctx = reinterpret_cast<EC_PKEY_CTX *>(ctx->data); |
| 168 | const EC_GROUP *group = dctx->gen_group; |
| 169 | if (group == NULL) { |
| 170 | if (ctx->pkey == NULL) { |
| 171 | OPENSSL_PUT_ERROR(EVP, EVP_R_NO_PARAMETERS_SET); |
| 172 | return 0; |
| 173 | } |
| 174 | group = EC_KEY_get0_group(reinterpret_cast<EC_KEY *>(ctx->pkey->pkey)); |
| 175 | } |
| 176 | EC_KEY *ec = EC_KEY_new(); |
| 177 | if (ec == NULL || !EC_KEY_set_group(ec, group) || !EC_KEY_generate_key(ec)) { |
| 178 | EC_KEY_free(ec); |
| 179 | return 0; |
| 180 | } |
| 181 | EVP_PKEY_assign_EC_KEY(pkey, ec); |
| 182 | return 1; |
| 183 | } |
| 184 |
| 185 | static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) { |
| 186 | EC_PKEY_CTX *dctx = reinterpret_cast<EC_PKEY_CTX *>(ctx->data); |
| 187 | if (dctx->gen_group == NULL) { |
| 188 | OPENSSL_PUT_ERROR(EVP, EVP_R_NO_PARAMETERS_SET); |
| 189 | return 0; |
| 190 | } |
| 191 | EC_KEY *ec = EC_KEY_new(); |
| 192 | if (ec == NULL || !EC_KEY_set_group(ec, dctx->gen_group)) { |
| 193 | EC_KEY_free(ec); |
| 194 | return 0; |
| 195 | } |
| 196 | EVP_PKEY_assign_EC_KEY(pkey, ec); |
| 197 | return 1; |
| 198 | } |
| 199 |
| 200 | const EVP_PKEY_METHOD ec_pkey_meth = { |
| 201 | EVP_PKEY_EC, |
| 202 | pkey_ec_init, |
| 203 | pkey_ec_copy, |
| 204 | pkey_ec_cleanup, |
| 205 | pkey_ec_keygen, |
| 206 | pkey_ec_sign, |
| 207 | NULL /* sign_message */, |
| 208 | pkey_ec_verify, |
| 209 | NULL /* verify_message */, |
| 210 | NULL /* verify_recover */, |
| 211 | NULL /* encrypt */, |
| 212 | NULL /* decrypt */, |
| 213 | pkey_ec_derive, |
| 214 | pkey_ec_paramgen, |
| 215 | pkey_ec_ctrl, |
| 216 | }; |
| 217 |
| 218 | int EVP_PKEY_CTX_set_ec_paramgen_curve_nid(EVP_PKEY_CTX *ctx, int nid) { |
| 219 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, EVP_PKEY_OP_TYPE_GEN, |
| 220 | EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, nid, NULL); |
| 221 | } |
| 222 |
| 223 | int EVP_PKEY_CTX_set_ec_param_enc(EVP_PKEY_CTX *ctx, int encoding) { |
| 224 | // BoringSSL only supports named curve syntax. |
| 225 | if (encoding != OPENSSL_EC_NAMED_CURVE) { |
| 226 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PARAMETERS); |
| 227 | return 0; |
| 228 | } |
| 229 | return 1; |
| 230 | } |
| 231 |