blob: 1748fe048a159f93c1b397a0cdf4ea02c56544bd [file] [log] [blame]
Thai Duongf862a762015-03-18 14:10:56 -07001/*
2 * Copyright 2014 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#ifndef SYSTEM_KEYMASTER_EC_KEY_H_
18#define SYSTEM_KEYMASTER_EC_KEY_H_
19
20#include <openssl/ec.h>
21
22#include "asymmetric_key.h"
23
24namespace keymaster {
25
26class EcKeyFactory : public AsymmetricKeyFactory {
27 public:
Shawn Willden13fbe3e2015-05-23 03:36:30 +000028 Key* GenerateKey(const AuthorizationSet& key_description, keymaster_error_t* error) override;
29 Key* ImportKey(const AuthorizationSet& key_description, keymaster_key_format_t key_format,
30 const uint8_t* key_data, size_t key_data_length,
31 keymaster_error_t* error) override;
32 Key* LoadKey(const UnencryptedKeyBlob& blob, keymaster_error_t* error) override;
Thai Duongf862a762015-03-18 14:10:56 -070033
34 private:
35 static EC_GROUP* choose_group(size_t key_size_bits);
36 static keymaster_error_t get_group_size(const EC_GROUP& group, size_t* key_size_bits);
37
38 struct EC_GROUP_Delete {
39 void operator()(EC_GROUP* p) { EC_GROUP_free(p); }
40 };
41};
42
43class EcdsaKeyFactory : public EcKeyFactory {
44 public:
Shawn Willden13fbe3e2015-05-23 03:36:30 +000045 virtual keymaster_algorithm_t registry_key() const { return KM_ALGORITHM_EC; }
Thai Duongf862a762015-03-18 14:10:56 -070046};
47
Shawn Willden13e29e02015-05-08 11:02:46 -060048class EcdsaOperationFactory;
Thai Duongf862a762015-03-18 14:10:56 -070049
50class EcKey : public AsymmetricKey {
Shawn Willden13fbe3e2015-05-23 03:36:30 +000051 private:
52 friend EcKeyFactory;
53 friend EcdsaKeyFactory;
54 friend EcdsaOperationFactory;
Thai Duongf862a762015-03-18 14:10:56 -070055
Shawn Willden13fbe3e2015-05-23 03:36:30 +000056 EcKey(const UnencryptedKeyBlob& blob, keymaster_error_t* error);
57 EcKey(EC_KEY* ec_key, const AuthorizationSet auths) : AsymmetricKey(auths), ec_key_(ec_key) {}
58
59 virtual int evp_key_type() { return EVP_PKEY_EC; }
60 virtual bool InternalToEvp(EVP_PKEY* pkey) const;
61 virtual bool EvpToInternal(const EVP_PKEY* pkey);
Thai Duongf862a762015-03-18 14:10:56 -070062
63 struct EC_Delete {
64 void operator()(EC_KEY* p) { EC_KEY_free(p); }
65 };
66
67 EC_KEY* key() const { return EC_KEY_dup(ec_key_.get()); }
68
69 UniquePtr<EC_KEY, EC_Delete> ec_key_;
70};
71
72} // namespace keymaster
73
74#endif // SYSTEM_KEYMASTER_EC_KEY_H_