Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 1 | /* |
| 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 Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 19 | #include "ecdsa_key.h" |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 20 | #include "ecdsa_operation.h" |
| 21 | #include "openssl_utils.h" |
| 22 | |
| 23 | namespace keymaster { |
| 24 | |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 25 | class 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 | }; |
| 43 | static OperationFactoryRegistry::Registration<EcdsaSignOperationFactory> sign_registration; |
| 44 | |
| 45 | class 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 | }; |
| 79 | static OperationFactoryRegistry::Registration<EcdsaVerifyOperationFactory> verify_registration; |
| 80 | |
Shawn Willden | 5ac2f8f | 2014-08-18 15:33:10 -0600 | [diff] [blame] | 81 | EcdsaOperation::~EcdsaOperation() { |
| 82 | if (ecdsa_key_ != NULL) |
| 83 | EC_KEY_free(ecdsa_key_); |
| 84 | } |
| 85 | |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 86 | keymaster_error_t EcdsaOperation::Update(const AuthorizationSet& /* additional_params */, |
| 87 | const Buffer& input, Buffer* /* output */, |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 88 | size_t* input_consumed) { |
| 89 | assert(input_consumed); |
Shawn Willden | 5ac2f8f | 2014-08-18 15:33:10 -0600 | [diff] [blame] | 90 | switch (purpose()) { |
| 91 | default: |
| 92 | return KM_ERROR_UNIMPLEMENTED; |
| 93 | case KM_PURPOSE_SIGN: |
| 94 | case KM_PURPOSE_VERIFY: |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 95 | return StoreData(input, input_consumed); |
Shawn Willden | 5ac2f8f | 2014-08-18 15:33:10 -0600 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 99 | keymaster_error_t EcdsaOperation::StoreData(const Buffer& input, size_t* input_consumed) { |
Shawn Willden | b340702 | 2014-08-27 06:30:52 -0600 | [diff] [blame] | 100 | if (!data_.reserve(data_.available_read() + input.available_read()) || |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 101 | !data_.write(input.peek_read(), input.available_read())) |
| 102 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 103 | *input_consumed = input.available_read(); |
Shawn Willden | 5ac2f8f | 2014-08-18 15:33:10 -0600 | [diff] [blame] | 104 | return KM_ERROR_OK; |
| 105 | } |
| 106 | |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 107 | keymaster_error_t EcdsaSignOperation::Finish(const AuthorizationSet& /* additional_params */, |
| 108 | const Buffer& /* signature */, Buffer* output) { |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 109 | assert(output); |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 110 | 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 Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 119 | keymaster_error_t EcdsaVerifyOperation::Finish(const AuthorizationSet& /* additional_params */, |
| 120 | const Buffer& signature, Buffer* /* output */) { |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 121 | 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 Willden | 5ac2f8f | 2014-08-18 15:33:10 -0600 | [diff] [blame] | 128 | return KM_ERROR_OK; |
Shawn Willden | 5ac2f8f | 2014-08-18 15:33:10 -0600 | [diff] [blame] | 129 | } |
| 130 | |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 131 | } // namespace keymaster |