1 /*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <keymaster/km_openssl/curve25519_key.h>
18 #include <openssl/curve25519.h>
19 #include <openssl/evp.h>
20
21 namespace keymaster {
22
IsEd25519Key(const AuthorizationSet & hw_enforced,const AuthorizationSet & sw_enforced)23 bool IsEd25519Key(const AuthorizationSet& hw_enforced, const AuthorizationSet& sw_enforced) {
24 AuthProxy proxy(hw_enforced, sw_enforced);
25 return (proxy.Contains(TAG_ALGORITHM, KM_ALGORITHM_EC) &&
26 proxy.Contains(TAG_EC_CURVE, KM_EC_CURVE_CURVE_25519) &&
27 (proxy.Contains(TAG_PURPOSE, KM_PURPOSE_SIGN) ||
28 proxy.Contains(TAG_PURPOSE, KM_PURPOSE_ATTEST_KEY)));
29 }
30
IsX25519Key(const AuthorizationSet & hw_enforced,const AuthorizationSet & sw_enforced)31 bool IsX25519Key(const AuthorizationSet& hw_enforced, const AuthorizationSet& sw_enforced) {
32 AuthProxy proxy(hw_enforced, sw_enforced);
33 return (proxy.Contains(TAG_ALGORITHM, KM_ALGORITHM_EC) &&
34 proxy.Contains(TAG_EC_CURVE, KM_EC_CURVE_CURVE_25519) &&
35 proxy.Contains(TAG_PURPOSE, KM_PURPOSE_AGREE_KEY));
36 }
37
EvpToInternal(const EVP_PKEY * pkey)38 bool Curve25519Key::EvpToInternal(const EVP_PKEY* pkey) {
39 return (EvpKeyToKeyMaterial(pkey, &key_material_) == KM_ERROR_OK);
40 }
41
InternalToEvp() const42 EVP_PKEY_Ptr Curve25519Key::InternalToEvp() const {
43 const uint8_t* tmp = key_material().key_material;
44 return EVP_PKEY_Ptr(
45 d2i_PrivateKey(evp_key_type(), nullptr /* pkey */, &tmp, key_material().key_material_size));
46 }
47
48 } // namespace keymaster
49