blob: 3bc0d88d745f67bc8ce75d53e4dc5ae9814547d5 [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"
21#include "openssl_utils.h"
22
23namespace keymaster {
24
Shawn Willden63ac0432014-12-29 14:07:08 -070025class EcdsaSignOperationFactory : public OperationFactory {
26 public:
27 virtual KeyType registry_key() const { return KeyType(KM_ALGORITHM_ECDSA, KM_PURPOSE_SIGN); }
28
29 virtual Operation* CreateOperation(const Key& key, const Logger& logger,
30 keymaster_error_t* error){
31 const EcdsaKey* ecdsa_key = static_cast<const EcdsaKey*>(&key);
32 if (!ecdsa_key) {
33 *error = KM_ERROR_UNKNOWN_ERROR;
34 return NULL;
35 }
36
37 Operation* op = new EcdsaSignOperation(KM_PURPOSE_SIGN, logger, ecdsa_key->key());
38 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
49 virtual Operation* CreateOperation(const Key& key, const Logger& logger,
50 keymaster_error_t* error){
51 const EcdsaKey* ecdsa_key = static_cast<const EcdsaKey*>(&key);
52 if (!ecdsa_key) {
53 *error = KM_ERROR_UNKNOWN_ERROR;
54 return NULL;
55 }
56
57 Operation* op = new EcdsaVerifyOperation(KM_PURPOSE_VERIFY, logger, ecdsa_key->key());
58 if (!op)
59 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
60 return op;
61 }
62
63 // Informational methods
64 virtual const keymaster_padding_t* SupportedPaddingModes(size_t* padding_count) const {
65 *padding_count = 0;
66 return NULL;
67 }
68
69 virtual const keymaster_block_mode_t* SupportedBlockModes(size_t* block_mode_count) const {
70 *block_mode_count = 0;
71 return NULL;
72 }
73
74 virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const {
75 *digest_count = 0;
76 return NULL;
77 }
78};
79static OperationFactoryRegistry::Registration<EcdsaVerifyOperationFactory> verify_registration;
80
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060081EcdsaOperation::~EcdsaOperation() {
82 if (ecdsa_key_ != NULL)
83 EC_KEY_free(ecdsa_key_);
84}
85
Shawn Willden6bfbff02015-02-06 19:48:24 -070086keymaster_error_t EcdsaOperation::Update(const AuthorizationSet& /* additional_params */,
87 const Buffer& input, Buffer* /* output */,
Shawn Willdenb7361132014-12-08 08:15:14 -070088 size_t* input_consumed) {
89 assert(input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060090 switch (purpose()) {
91 default:
92 return KM_ERROR_UNIMPLEMENTED;
93 case KM_PURPOSE_SIGN:
94 case KM_PURPOSE_VERIFY:
Shawn Willdenb7361132014-12-08 08:15:14 -070095 return StoreData(input, input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060096 }
97}
98
Shawn Willdenb7361132014-12-08 08:15:14 -070099keymaster_error_t EcdsaOperation::StoreData(const Buffer& input, size_t* input_consumed) {
Shawn Willdenb3407022014-08-27 06:30:52 -0600100 if (!data_.reserve(data_.available_read() + input.available_read()) ||
Shawn Willdend67afae2014-08-19 12:36:27 -0600101 !data_.write(input.peek_read(), input.available_read()))
102 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willdenb7361132014-12-08 08:15:14 -0700103 *input_consumed = input.available_read();
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600104 return KM_ERROR_OK;
105}
106
Shawn Willden6bfbff02015-02-06 19:48:24 -0700107keymaster_error_t EcdsaSignOperation::Finish(const AuthorizationSet& /* additional_params */,
108 const Buffer& /* signature */, Buffer* output) {
Shawn Willdenb7361132014-12-08 08:15:14 -0700109 assert(output);
Shawn Willdend67afae2014-08-19 12:36:27 -0600110 output->Reinitialize(ECDSA_size(ecdsa_key_));
111 unsigned int siglen;
112 if (!ECDSA_sign(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
113 output->peek_write(), &siglen, ecdsa_key_))
114 return KM_ERROR_UNKNOWN_ERROR;
115 output->advance_write(siglen);
116 return KM_ERROR_OK;
117}
118
Shawn Willden6bfbff02015-02-06 19:48:24 -0700119keymaster_error_t EcdsaVerifyOperation::Finish(const AuthorizationSet& /* additional_params */,
120 const Buffer& signature, Buffer* /* output */) {
Shawn Willdend67afae2014-08-19 12:36:27 -0600121 int result = ECDSA_verify(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
122 signature.peek_read(), signature.available_read(), ecdsa_key_);
123 if (result < 0)
124 return KM_ERROR_UNKNOWN_ERROR;
125 else if (result == 0)
126 return KM_ERROR_VERIFICATION_FAILED;
127 else
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600128 return KM_ERROR_OK;
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600129}
130
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600131} // namespace keymaster