blob: 0d4ca45caac6fb7c5696015f18b7bf9ac587171a [file] [log] [blame]
Shawn Willdenc3864dd2014-08-18 15:20:01 -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#include <openssl/ecdsa.h>
18
Thai Duongf862a762015-03-18 14:10:56 -070019#include "ec_key.h"
Shawn Willdenc3864dd2014-08-18 15:20:01 -060020#include "ecdsa_operation.h"
Shawn Willden567a4a02014-12-31 12:14:46 -070021#include "openssl_err.h"
Shawn Willdenc3864dd2014-08-18 15:20:01 -060022#include "openssl_utils.h"
23
24namespace keymaster {
25
Shawn Willden84b8da52015-03-11 07:21:32 -060026static const keymaster_digest_t supported_digests[] = {KM_DIGEST_NONE};
27
Shawn Willden13e29e02015-05-08 11:02:46 -060028class EcdsaOperationFactory : public OperationFactory {
29 private:
30 KeyType registry_key() const override { return KeyType(KM_ALGORITHM_EC, purpose()); }
31 Operation* CreateOperation(const Key& key, const AuthorizationSet& begin_params,
32 keymaster_error_t* error) override;
33 const keymaster_digest_t* SupportedDigests(size_t* digest_count) const override;
Shawn Willden63ac0432014-12-29 14:07:08 -070034
Shawn Willden13e29e02015-05-08 11:02:46 -060035 virtual keymaster_purpose_t purpose() const = 0;
36 virtual Operation* InstantiateOperation(keymaster_digest_t digest, EC_KEY* key) = 0;
37};
Shawn Willden63ac0432014-12-29 14:07:08 -070038
Shawn Willden13e29e02015-05-08 11:02:46 -060039Operation* EcdsaOperationFactory::CreateOperation(const Key& key,
40 const AuthorizationSet& /* begin_params */,
41 keymaster_error_t* error) {
42 const EcKey* ecdsa_key = static_cast<const EcKey*>(&key);
43 if (!ecdsa_key) {
44 *error = KM_ERROR_UNKNOWN_ERROR;
45 return nullptr;
Shawn Willden63ac0432014-12-29 14:07:08 -070046 }
Shawn Willden84b8da52015-03-11 07:21:32 -060047
Shawn Willden13e29e02015-05-08 11:02:46 -060048 keymaster_digest_t digest;
49 if (!ecdsa_key->authorizations().GetTagValue(TAG_DIGEST, &digest) &&
50 !ecdsa_key->authorizations().GetTagValue(TAG_DIGEST_OLD, &digest)) {
51 *error = KM_ERROR_UNSUPPORTED_DIGEST;
52 return NULL;
53 }
54 Operation* op = InstantiateOperation(digest, ecdsa_key->key());
55 if (!op)
56 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
57 return op;
58}
59
60const keymaster_digest_t* EcdsaOperationFactory::SupportedDigests(size_t* digest_count) const {
61 *digest_count = array_length(supported_digests);
62 return supported_digests;
63}
64
65class EcdsaSignOperationFactory : public EcdsaOperationFactory {
66 private:
67 keymaster_purpose_t purpose() const override { return KM_PURPOSE_SIGN; }
68 Operation* InstantiateOperation(keymaster_digest_t digest, EC_KEY* key) {
69 return new EcdsaSignOperation(purpose(), digest, key);
Shawn Willden84b8da52015-03-11 07:21:32 -060070 }
Shawn Willden63ac0432014-12-29 14:07:08 -070071};
Shawn Willden13e29e02015-05-08 11:02:46 -060072
Shawn Willden63ac0432014-12-29 14:07:08 -070073static OperationFactoryRegistry::Registration<EcdsaSignOperationFactory> sign_registration;
74
Shawn Willden13e29e02015-05-08 11:02:46 -060075class EcdsaVerifyOperationFactory : public EcdsaOperationFactory {
Shawn Willden63ac0432014-12-29 14:07:08 -070076 public:
Shawn Willden13e29e02015-05-08 11:02:46 -060077 keymaster_purpose_t purpose() const override { return KM_PURPOSE_VERIFY; }
78 Operation* InstantiateOperation(keymaster_digest_t digest, EC_KEY* key) {
79 return new EcdsaVerifyOperation(KM_PURPOSE_VERIFY, digest, key);
Shawn Willden63ac0432014-12-29 14:07:08 -070080 }
81};
82static OperationFactoryRegistry::Registration<EcdsaVerifyOperationFactory> verify_registration;
83
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060084EcdsaOperation::~EcdsaOperation() {
85 if (ecdsa_key_ != NULL)
86 EC_KEY_free(ecdsa_key_);
87}
88
Shawn Willden6bfbff02015-02-06 19:48:24 -070089keymaster_error_t EcdsaOperation::Update(const AuthorizationSet& /* additional_params */,
90 const Buffer& input, Buffer* /* output */,
Shawn Willdenb7361132014-12-08 08:15:14 -070091 size_t* input_consumed) {
92 assert(input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060093 switch (purpose()) {
94 default:
95 return KM_ERROR_UNIMPLEMENTED;
96 case KM_PURPOSE_SIGN:
97 case KM_PURPOSE_VERIFY:
Shawn Willdenb7361132014-12-08 08:15:14 -070098 return StoreData(input, input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060099 }
100}
101
Shawn Willdenb7361132014-12-08 08:15:14 -0700102keymaster_error_t EcdsaOperation::StoreData(const Buffer& input, size_t* input_consumed) {
Shawn Willdenb3407022014-08-27 06:30:52 -0600103 if (!data_.reserve(data_.available_read() + input.available_read()) ||
Shawn Willdend67afae2014-08-19 12:36:27 -0600104 !data_.write(input.peek_read(), input.available_read()))
105 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willdenb7361132014-12-08 08:15:14 -0700106 *input_consumed = input.available_read();
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600107 return KM_ERROR_OK;
108}
109
Shawn Willden6bfbff02015-02-06 19:48:24 -0700110keymaster_error_t EcdsaSignOperation::Finish(const AuthorizationSet& /* additional_params */,
111 const Buffer& /* signature */, Buffer* output) {
Shawn Willdenb7361132014-12-08 08:15:14 -0700112 assert(output);
Shawn Willdend67afae2014-08-19 12:36:27 -0600113 output->Reinitialize(ECDSA_size(ecdsa_key_));
114 unsigned int siglen;
115 if (!ECDSA_sign(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
116 output->peek_write(), &siglen, ecdsa_key_))
Shawn Willden567a4a02014-12-31 12:14:46 -0700117 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -0600118 output->advance_write(siglen);
119 return KM_ERROR_OK;
120}
121
Shawn Willden6bfbff02015-02-06 19:48:24 -0700122keymaster_error_t EcdsaVerifyOperation::Finish(const AuthorizationSet& /* additional_params */,
123 const Buffer& signature, Buffer* /* output */) {
Shawn Willdend67afae2014-08-19 12:36:27 -0600124 int result = ECDSA_verify(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
125 signature.peek_read(), signature.available_read(), ecdsa_key_);
126 if (result < 0)
Shawn Willden567a4a02014-12-31 12:14:46 -0700127 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -0600128 else if (result == 0)
129 return KM_ERROR_VERIFICATION_FAILED;
130 else
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600131 return KM_ERROR_OK;
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600132}
133
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600134} // namespace keymaster