| 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 <openssl/curve25519.h> |
| 18 | #include <openssl/err.h> |
| 19 | #include <openssl/mem.h> |
| 20 |
| 21 | #include "internal.h" |
| 22 |
| 23 |
| 24 | // X25519 has no parameters to copy. |
| 25 | static int pkey_x25519_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { return 1; } |
| 26 |
| 27 | static int pkey_x25519_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) { |
| 28 | X25519_KEY *key = |
| 29 | reinterpret_cast<X25519_KEY *>(OPENSSL_malloc(sizeof(X25519_KEY))); |
| 30 | if (key == NULL) { |
| 31 | return 0; |
| 32 | } |
| 33 |
| 34 | evp_pkey_set_method(pkey, &x25519_asn1_meth); |
| 35 |
| 36 | X25519_keypair(key->pub, key->priv); |
| 37 | key->has_private = 1; |
| 38 |
| 39 | OPENSSL_free(pkey->pkey); |
| 40 | pkey->pkey = key; |
| 41 | return 1; |
| 42 | } |
| 43 |
| 44 | static int pkey_x25519_derive(EVP_PKEY_CTX *ctx, uint8_t *out, |
| 45 | size_t *out_len) { |
| 46 | if (ctx->pkey == NULL || ctx->peerkey == NULL) { |
| 47 | OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET); |
| 48 | return 0; |
| 49 | } |
| 50 |
| 51 | const X25519_KEY *our_key = |
| 52 | reinterpret_cast<const X25519_KEY *>(ctx->pkey->pkey); |
| 53 | const X25519_KEY *peer_key = |
| 54 | reinterpret_cast<const X25519_KEY *>(ctx->peerkey->pkey); |
| 55 | if (our_key == NULL || peer_key == NULL) { |
| 56 | OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET); |
| 57 | return 0; |
| 58 | } |
| 59 |
| 60 | if (!our_key->has_private) { |
| 61 | OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); |
| 62 | return 0; |
| 63 | } |
| 64 |
| 65 | if (out != NULL) { |
| 66 | if (*out_len < 32) { |
| 67 | OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); |
| 68 | return 0; |
| 69 | } |
| 70 | if (!X25519(out, our_key->priv, peer_key->pub)) { |
| 71 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); |
| 72 | return 0; |
| 73 | } |
| 74 | } |
| 75 |
| 76 | *out_len = 32; |
| 77 | return 1; |
| 78 | } |
| 79 |
| 80 | static int pkey_x25519_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) { |
| 81 | switch (type) { |
| 82 | case EVP_PKEY_CTRL_PEER_KEY: |
| 83 | // |EVP_PKEY_derive_set_peer| requires the key implement this command, |
| 84 | // even if it is a no-op. |
| 85 | return 1; |
| 86 |
| 87 | default: |
| 88 | OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 89 | return 0; |
| 90 | } |
| 91 | } |
| 92 |
| 93 | const EVP_PKEY_METHOD x25519_pkey_meth = { |
| 94 | /*pkey_id=*/EVP_PKEY_X25519, |
| 95 | /*init=*/NULL, |
| 96 | /*copy=*/pkey_x25519_copy, |
| 97 | /*cleanup=*/NULL, |
| 98 | /*keygen=*/pkey_x25519_keygen, |
| 99 | /*sign=*/NULL, |
| 100 | /*sign_message=*/NULL, |
| 101 | /*verify=*/NULL, |
| 102 | /*verify_message=*/NULL, |
| 103 | /*verify_recover=*/NULL, |
| 104 | /*encrypt=*/NULL, |
| 105 | /*decrypt=*/NULL, |
| 106 | /*derive=*/pkey_x25519_derive, |
| 107 | /*paramgen=*/NULL, |
| 108 | /*ctrl=*/pkey_x25519_ctrl, |
| 109 | }; |
| 110 |