blob: 3a0753732ae245b97590206280768072617f40f7 [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
Shawn Willden63ac0432014-12-29 14:07:08 -070019#include "ecdsa_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 Willden63ac0432014-12-29 14:07:08 -070026class EcdsaSignOperationFactory : public OperationFactory {
27 public:
28 virtual KeyType registry_key() const { return KeyType(KM_ALGORITHM_ECDSA, KM_PURPOSE_SIGN); }
29
Shawn Willden567a4a02014-12-31 12:14:46 -070030 virtual Operation* CreateOperation(const Key& key, keymaster_error_t* error) {
Shawn Willden63ac0432014-12-29 14:07:08 -070031 const EcdsaKey* ecdsa_key = static_cast<const EcdsaKey*>(&key);
32 if (!ecdsa_key) {
33 *error = KM_ERROR_UNKNOWN_ERROR;
34 return NULL;
35 }
36
Shawn Willden567a4a02014-12-31 12:14:46 -070037 Operation* op = new EcdsaSignOperation(KM_PURPOSE_SIGN, ecdsa_key->key());
Shawn Willden63ac0432014-12-29 14:07:08 -070038 if (!op)
39 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
40 return op;
41 }
42};
43static OperationFactoryRegistry::Registration<EcdsaSignOperationFactory> sign_registration;
44
45class EcdsaVerifyOperationFactory : public OperationFactory {
46 public:
47 virtual KeyType registry_key() const { return KeyType(KM_ALGORITHM_ECDSA, KM_PURPOSE_VERIFY); }
48
Shawn Willden567a4a02014-12-31 12:14:46 -070049 virtual Operation* CreateOperation(const Key& key, keymaster_error_t* error) {
Shawn Willden63ac0432014-12-29 14:07:08 -070050 const EcdsaKey* ecdsa_key = static_cast<const EcdsaKey*>(&key);
51 if (!ecdsa_key) {
52 *error = KM_ERROR_UNKNOWN_ERROR;
53 return NULL;
54 }
55
Shawn Willden567a4a02014-12-31 12:14:46 -070056 Operation* op = new EcdsaVerifyOperation(KM_PURPOSE_VERIFY, ecdsa_key->key());
Shawn Willden63ac0432014-12-29 14:07:08 -070057 if (!op)
58 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
59 return op;
60 }
61
62 // Informational methods
63 virtual const keymaster_padding_t* SupportedPaddingModes(size_t* padding_count) const {
64 *padding_count = 0;
65 return NULL;
66 }
67
68 virtual const keymaster_block_mode_t* SupportedBlockModes(size_t* block_mode_count) const {
69 *block_mode_count = 0;
70 return NULL;
71 }
72
73 virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const {
74 *digest_count = 0;
75 return NULL;
76 }
77};
78static OperationFactoryRegistry::Registration<EcdsaVerifyOperationFactory> verify_registration;
79
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060080EcdsaOperation::~EcdsaOperation() {
81 if (ecdsa_key_ != NULL)
82 EC_KEY_free(ecdsa_key_);
83}
84
Shawn Willden6bfbff02015-02-06 19:48:24 -070085keymaster_error_t EcdsaOperation::Update(const AuthorizationSet& /* additional_params */,
86 const Buffer& input, Buffer* /* output */,
Shawn Willdenb7361132014-12-08 08:15:14 -070087 size_t* input_consumed) {
88 assert(input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060089 switch (purpose()) {
90 default:
91 return KM_ERROR_UNIMPLEMENTED;
92 case KM_PURPOSE_SIGN:
93 case KM_PURPOSE_VERIFY:
Shawn Willdenb7361132014-12-08 08:15:14 -070094 return StoreData(input, input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060095 }
96}
97
Shawn Willdenb7361132014-12-08 08:15:14 -070098keymaster_error_t EcdsaOperation::StoreData(const Buffer& input, size_t* input_consumed) {
Shawn Willdenb3407022014-08-27 06:30:52 -060099 if (!data_.reserve(data_.available_read() + input.available_read()) ||
Shawn Willdend67afae2014-08-19 12:36:27 -0600100 !data_.write(input.peek_read(), input.available_read()))
101 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willdenb7361132014-12-08 08:15:14 -0700102 *input_consumed = input.available_read();
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600103 return KM_ERROR_OK;
104}
105
Shawn Willden6bfbff02015-02-06 19:48:24 -0700106keymaster_error_t EcdsaSignOperation::Finish(const AuthorizationSet& /* additional_params */,
107 const Buffer& /* signature */, Buffer* output) {
Shawn Willdenb7361132014-12-08 08:15:14 -0700108 assert(output);
Shawn Willdend67afae2014-08-19 12:36:27 -0600109 output->Reinitialize(ECDSA_size(ecdsa_key_));
110 unsigned int siglen;
111 if (!ECDSA_sign(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
112 output->peek_write(), &siglen, ecdsa_key_))
Shawn Willden567a4a02014-12-31 12:14:46 -0700113 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -0600114 output->advance_write(siglen);
115 return KM_ERROR_OK;
116}
117
Shawn Willden6bfbff02015-02-06 19:48:24 -0700118keymaster_error_t EcdsaVerifyOperation::Finish(const AuthorizationSet& /* additional_params */,
119 const Buffer& signature, Buffer* /* output */) {
Shawn Willdend67afae2014-08-19 12:36:27 -0600120 int result = ECDSA_verify(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
121 signature.peek_read(), signature.available_read(), ecdsa_key_);
122 if (result < 0)
Shawn Willden567a4a02014-12-31 12:14:46 -0700123 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -0600124 else if (result == 0)
125 return KM_ERROR_VERIFICATION_FAILED;
126 else
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600127 return KM_ERROR_OK;
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600128}
129
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600130} // namespace keymaster