blob: fba743f11a2bd24cb82c709b76e0c724cc98172d [file] [log] [blame]
Shawn Willden0a4df7e2014-08-28 16:09:05 -06001/*
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_ECDSA_OPERATION_H_
18#define SYSTEM_KEYMASTER_ECDSA_OPERATION_H_
19
20#include <openssl/ec.h>
Shawn Willdenefbd7e42015-06-01 07:07:33 -060021#include <openssl/evp.h>
Shawn Willden0a4df7e2014-08-28 16:09:05 -060022
23#include <UniquePtr.h>
24
Shawn Willden0a4df7e2014-08-28 16:09:05 -060025#include "operation.h"
26
27namespace keymaster {
28
29class EcdsaOperation : public Operation {
30 public:
Shawn Willdenefbd7e42015-06-01 07:07:33 -060031 EcdsaOperation(keymaster_purpose_t purpose, keymaster_digest_t digest, EVP_PKEY* key)
32 : Operation(purpose), digest_(digest), ecdsa_key_(key) {
33 EVP_MD_CTX_init(&digest_ctx_);
34 }
Shawn Willden0a4df7e2014-08-28 16:09:05 -060035 ~EcdsaOperation();
36
Shawn Willdenefbd7e42015-06-01 07:07:33 -060037 keymaster_error_t Abort() override { return KM_ERROR_OK; }
Shawn Willden0a4df7e2014-08-28 16:09:05 -060038
39 protected:
Shawn Willdenb7361132014-12-08 08:15:14 -070040 keymaster_error_t StoreData(const Buffer& input, size_t* input_consumed);
Shawn Willdenefbd7e42015-06-01 07:07:33 -060041 keymaster_error_t InitDigest();
Shawn Willden0a4df7e2014-08-28 16:09:05 -060042
Shawn Willden84b8da52015-03-11 07:21:32 -060043 keymaster_digest_t digest_;
Shawn Willdenefbd7e42015-06-01 07:07:33 -060044 const EVP_MD* digest_algorithm_;
45 EVP_PKEY* ecdsa_key_;
46 EVP_MD_CTX digest_ctx_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -060047 Buffer data_;
48};
49
50class EcdsaSignOperation : public EcdsaOperation {
51 public:
Shawn Willden2612fb52015-07-27 16:58:30 -060052 EcdsaSignOperation(keymaster_digest_t digest, EVP_PKEY* key)
53 : EcdsaOperation(KM_PURPOSE_SIGN, digest, key) {}
Shawn Willdenefbd7e42015-06-01 07:07:33 -060054 keymaster_error_t Begin(const AuthorizationSet& input_params,
55 AuthorizationSet* output_params) override;
56 keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
Shawn Willdended8e7d2015-06-01 15:29:12 -060057 AuthorizationSet* output_params, Buffer* output,
58 size_t* input_consumed) override;
Shawn Willdenefbd7e42015-06-01 07:07:33 -060059 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& signature,
Shawn Willdended8e7d2015-06-01 15:29:12 -060060 AuthorizationSet* output_params, Buffer* output) override;
Shawn Willden0a4df7e2014-08-28 16:09:05 -060061};
62
63class EcdsaVerifyOperation : public EcdsaOperation {
64 public:
Shawn Willden2612fb52015-07-27 16:58:30 -060065 EcdsaVerifyOperation(keymaster_digest_t digest, EVP_PKEY* key)
66 : EcdsaOperation(KM_PURPOSE_VERIFY, digest, key) {}
Shawn Willdenefbd7e42015-06-01 07:07:33 -060067 keymaster_error_t Begin(const AuthorizationSet& input_params,
68 AuthorizationSet* output_params) override;
69 keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
Shawn Willdended8e7d2015-06-01 15:29:12 -060070 AuthorizationSet* output_params, Buffer* output,
71 size_t* input_consumed) override;
Shawn Willdenefbd7e42015-06-01 07:07:33 -060072 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& signature,
Shawn Willdended8e7d2015-06-01 15:29:12 -060073 AuthorizationSet* output_params, Buffer* output) override;
Shawn Willden0a4df7e2014-08-28 16:09:05 -060074};
75
Shawn Willden06298102015-05-25 23:12:48 -060076class EcdsaOperationFactory : public OperationFactory {
77 private:
78 KeyType registry_key() const override { return KeyType(KM_ALGORITHM_EC, purpose()); }
79 Operation* CreateOperation(const Key& key, const AuthorizationSet& begin_params,
80 keymaster_error_t* error) override;
81 const keymaster_digest_t* SupportedDigests(size_t* digest_count) const override;
82
83 virtual keymaster_purpose_t purpose() const = 0;
Shawn Willdenefbd7e42015-06-01 07:07:33 -060084 virtual Operation* InstantiateOperation(keymaster_digest_t digest, EVP_PKEY* key) = 0;
Shawn Willden06298102015-05-25 23:12:48 -060085};
86
87class EcdsaSignOperationFactory : public EcdsaOperationFactory {
88 private:
89 keymaster_purpose_t purpose() const override { return KM_PURPOSE_SIGN; }
Shawn Willdenefbd7e42015-06-01 07:07:33 -060090 Operation* InstantiateOperation(keymaster_digest_t digest, EVP_PKEY* key) {
Shawn Willden2612fb52015-07-27 16:58:30 -060091 return new (std::nothrow) EcdsaSignOperation(digest, key);
Shawn Willden06298102015-05-25 23:12:48 -060092 }
93};
94
95class EcdsaVerifyOperationFactory : public EcdsaOperationFactory {
96 public:
97 keymaster_purpose_t purpose() const override { return KM_PURPOSE_VERIFY; }
Shawn Willdenefbd7e42015-06-01 07:07:33 -060098 Operation* InstantiateOperation(keymaster_digest_t digest, EVP_PKEY* key) {
Shawn Willden2612fb52015-07-27 16:58:30 -060099 return new (std::nothrow) EcdsaVerifyOperation(digest, key);
Shawn Willden06298102015-05-25 23:12:48 -0600100 }
101};
102
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600103} // namespace keymaster
104
105#endif // SYSTEM_KEYMASTER_ECDSA_OPERATION_H_