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 | |
Thai Duong | f862a76 | 2015-03-18 14:10:56 -0700 | [diff] [blame] | 19 | #include "ec_key.h" |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 20 | #include "ecdsa_operation.h" |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 21 | #include "openssl_err.h" |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 22 | #include "openssl_utils.h" |
| 23 | |
| 24 | namespace keymaster { |
| 25 | |
Shawn Willden | 84b8da5 | 2015-03-11 07:21:32 -0600 | [diff] [blame] | 26 | static const keymaster_digest_t supported_digests[] = {KM_DIGEST_NONE}; |
| 27 | |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 28 | class EcdsaSignOperationFactory : public OperationFactory { |
| 29 | public: |
Shawn Willden | 9c65b2b | 2015-04-07 17:01:10 -0600 | [diff] [blame] | 30 | virtual KeyType registry_key() const { return KeyType(KM_ALGORITHM_EC, KM_PURPOSE_SIGN); } |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 31 | |
Shawn Willden | 898f564 | 2015-04-15 13:39:38 -0600 | [diff] [blame^] | 32 | virtual Operation* CreateOperation(const Key& key, const AuthorizationSet& /* begin_params */, |
| 33 | keymaster_error_t* error) { |
Thai Duong | f862a76 | 2015-03-18 14:10:56 -0700 | [diff] [blame] | 34 | const EcKey* ecdsa_key = static_cast<const EcKey*>(&key); |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 35 | if (!ecdsa_key) { |
| 36 | *error = KM_ERROR_UNKNOWN_ERROR; |
| 37 | return NULL; |
| 38 | } |
| 39 | |
Shawn Willden | 84b8da5 | 2015-03-11 07:21:32 -0600 | [diff] [blame] | 40 | keymaster_digest_t digest; |
Shawn Willden | d9d7acf | 2015-02-25 17:37:20 -0700 | [diff] [blame] | 41 | if (!ecdsa_key->authorizations().GetTagValue(TAG_DIGEST, &digest) && |
| 42 | !ecdsa_key->authorizations().GetTagValue(TAG_DIGEST_OLD, &digest)) { |
Shawn Willden | 84b8da5 | 2015-03-11 07:21:32 -0600 | [diff] [blame] | 43 | *error = KM_ERROR_UNSUPPORTED_DIGEST; |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | Operation* op = new EcdsaSignOperation(KM_PURPOSE_SIGN, digest, ecdsa_key->key()); |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 48 | if (!op) |
| 49 | *error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 50 | return op; |
| 51 | } |
Shawn Willden | 84b8da5 | 2015-03-11 07:21:32 -0600 | [diff] [blame] | 52 | |
| 53 | virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const { |
| 54 | *digest_count = array_length(supported_digests); |
| 55 | return supported_digests; |
| 56 | } |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 57 | }; |
| 58 | static OperationFactoryRegistry::Registration<EcdsaSignOperationFactory> sign_registration; |
| 59 | |
| 60 | class EcdsaVerifyOperationFactory : public OperationFactory { |
| 61 | public: |
Shawn Willden | 9c65b2b | 2015-04-07 17:01:10 -0600 | [diff] [blame] | 62 | virtual KeyType registry_key() const { return KeyType(KM_ALGORITHM_EC, KM_PURPOSE_VERIFY); } |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 63 | |
Shawn Willden | 898f564 | 2015-04-15 13:39:38 -0600 | [diff] [blame^] | 64 | virtual Operation* CreateOperation(const Key& key, const AuthorizationSet& /* begin_params */, |
| 65 | keymaster_error_t* error) { |
Thai Duong | f862a76 | 2015-03-18 14:10:56 -0700 | [diff] [blame] | 66 | const EcKey* ecdsa_key = static_cast<const EcKey*>(&key); |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 67 | if (!ecdsa_key) { |
| 68 | *error = KM_ERROR_UNKNOWN_ERROR; |
| 69 | return NULL; |
| 70 | } |
| 71 | |
Shawn Willden | 84b8da5 | 2015-03-11 07:21:32 -0600 | [diff] [blame] | 72 | keymaster_digest_t digest; |
Shawn Willden | d9d7acf | 2015-02-25 17:37:20 -0700 | [diff] [blame] | 73 | if (!ecdsa_key->authorizations().GetTagValue(TAG_DIGEST, &digest) && |
| 74 | !ecdsa_key->authorizations().GetTagValue(TAG_DIGEST_OLD, &digest)) { |
Shawn Willden | 84b8da5 | 2015-03-11 07:21:32 -0600 | [diff] [blame] | 75 | *error = KM_ERROR_UNSUPPORTED_DIGEST; |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | Operation* op = new EcdsaVerifyOperation(KM_PURPOSE_VERIFY, digest, ecdsa_key->key()); |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 80 | if (!op) |
| 81 | *error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 82 | return op; |
| 83 | } |
| 84 | |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 85 | virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const { |
Shawn Willden | 84b8da5 | 2015-03-11 07:21:32 -0600 | [diff] [blame] | 86 | *digest_count = array_length(supported_digests); |
| 87 | return supported_digests; |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 88 | } |
| 89 | }; |
| 90 | static OperationFactoryRegistry::Registration<EcdsaVerifyOperationFactory> verify_registration; |
| 91 | |
Shawn Willden | 5ac2f8f | 2014-08-18 15:33:10 -0600 | [diff] [blame] | 92 | EcdsaOperation::~EcdsaOperation() { |
| 93 | if (ecdsa_key_ != NULL) |
| 94 | EC_KEY_free(ecdsa_key_); |
| 95 | } |
| 96 | |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 97 | keymaster_error_t EcdsaOperation::Update(const AuthorizationSet& /* additional_params */, |
| 98 | const Buffer& input, Buffer* /* output */, |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 99 | size_t* input_consumed) { |
| 100 | assert(input_consumed); |
Shawn Willden | 5ac2f8f | 2014-08-18 15:33:10 -0600 | [diff] [blame] | 101 | switch (purpose()) { |
| 102 | default: |
| 103 | return KM_ERROR_UNIMPLEMENTED; |
| 104 | case KM_PURPOSE_SIGN: |
| 105 | case KM_PURPOSE_VERIFY: |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 106 | return StoreData(input, input_consumed); |
Shawn Willden | 5ac2f8f | 2014-08-18 15:33:10 -0600 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 110 | keymaster_error_t EcdsaOperation::StoreData(const Buffer& input, size_t* input_consumed) { |
Shawn Willden | b340702 | 2014-08-27 06:30:52 -0600 | [diff] [blame] | 111 | if (!data_.reserve(data_.available_read() + input.available_read()) || |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 112 | !data_.write(input.peek_read(), input.available_read())) |
| 113 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 114 | *input_consumed = input.available_read(); |
Shawn Willden | 5ac2f8f | 2014-08-18 15:33:10 -0600 | [diff] [blame] | 115 | return KM_ERROR_OK; |
| 116 | } |
| 117 | |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 118 | keymaster_error_t EcdsaSignOperation::Finish(const AuthorizationSet& /* additional_params */, |
| 119 | const Buffer& /* signature */, Buffer* output) { |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 120 | assert(output); |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 121 | output->Reinitialize(ECDSA_size(ecdsa_key_)); |
| 122 | unsigned int siglen; |
| 123 | if (!ECDSA_sign(0 /* type -- ignored */, data_.peek_read(), data_.available_read(), |
| 124 | output->peek_write(), &siglen, ecdsa_key_)) |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 125 | return TranslateLastOpenSslError(); |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 126 | output->advance_write(siglen); |
| 127 | return KM_ERROR_OK; |
| 128 | } |
| 129 | |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 130 | keymaster_error_t EcdsaVerifyOperation::Finish(const AuthorizationSet& /* additional_params */, |
| 131 | const Buffer& signature, Buffer* /* output */) { |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 132 | int result = ECDSA_verify(0 /* type -- ignored */, data_.peek_read(), data_.available_read(), |
| 133 | signature.peek_read(), signature.available_read(), ecdsa_key_); |
| 134 | if (result < 0) |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 135 | return TranslateLastOpenSslError(); |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 136 | else if (result == 0) |
| 137 | return KM_ERROR_VERIFICATION_FAILED; |
| 138 | else |
Shawn Willden | 5ac2f8f | 2014-08-18 15:33:10 -0600 | [diff] [blame] | 139 | return KM_ERROR_OK; |
Shawn Willden | 5ac2f8f | 2014-08-18 15:33:10 -0600 | [diff] [blame] | 140 | } |
| 141 | |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 142 | } // namespace keymaster |