Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -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 | |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 17 | #include <limits.h> |
| 18 | |
| 19 | #include <openssl/err.h> |
Shawn Willden | 5f42dba | 2015-01-20 11:45:19 -0700 | [diff] [blame] | 20 | #include <openssl/evp.h> |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 21 | #include <openssl/rsa.h> |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 22 | |
| 23 | #include "rsa_operation.h" |
| 24 | #include "openssl_utils.h" |
| 25 | |
| 26 | namespace keymaster { |
| 27 | |
| 28 | struct RSA_Delete { |
| 29 | void operator()(RSA* p) const { RSA_free(p); } |
| 30 | }; |
| 31 | |
| 32 | RsaOperation::~RsaOperation() { |
| 33 | if (rsa_key_ != NULL) |
| 34 | RSA_free(rsa_key_); |
| 35 | } |
| 36 | |
Shawn Willden | 5da34d2 | 2015-01-20 11:44:47 -0700 | [diff] [blame] | 37 | keymaster_error_t RsaOperation::Update(const Buffer& input, Buffer* /* output */) { |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 38 | switch (purpose()) { |
| 39 | default: |
| 40 | return KM_ERROR_UNIMPLEMENTED; |
| 41 | case KM_PURPOSE_SIGN: |
| 42 | case KM_PURPOSE_VERIFY: |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 43 | case KM_PURPOSE_ENCRYPT: |
| 44 | case KM_PURPOSE_DECRYPT: |
Shawn Willden | 5da34d2 | 2015-01-20 11:44:47 -0700 | [diff] [blame] | 45 | return StoreData(input); |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
Shawn Willden | 5da34d2 | 2015-01-20 11:44:47 -0700 | [diff] [blame] | 49 | keymaster_error_t RsaOperation::StoreData(const Buffer& input) { |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 50 | if (!data_.reserve(data_.available_read() + input.available_read()) || |
| 51 | !data_.write(input.peek_read(), input.available_read())) |
| 52 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 53 | return KM_ERROR_OK; |
| 54 | } |
| 55 | |
| 56 | keymaster_error_t RsaSignOperation::Finish(const Buffer& /* signature */, Buffer* output) { |
| 57 | output->Reinitialize(RSA_size(rsa_key_)); |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 58 | int bytes_encrypted = RSA_private_encrypt(data_.available_read(), data_.peek_read(), |
| 59 | output->peek_write(), rsa_key_, RSA_NO_PADDING); |
| 60 | if (bytes_encrypted < 0) |
| 61 | return KM_ERROR_UNKNOWN_ERROR; |
| 62 | assert(bytes_encrypted == RSA_size(rsa_key_)); |
| 63 | output->advance_write(bytes_encrypted); |
| 64 | return KM_ERROR_OK; |
| 65 | } |
| 66 | |
| 67 | keymaster_error_t RsaVerifyOperation::Finish(const Buffer& signature, Buffer* /* output */) { |
Adam Langley | f2aefdf | 2014-09-26 11:16:10 -0700 | [diff] [blame] | 68 | #if defined(OPENSSL_IS_BORINGSSL) |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 69 | size_t message_size = data_.available_read(); |
Adam Langley | f2aefdf | 2014-09-26 11:16:10 -0700 | [diff] [blame] | 70 | #else |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 71 | if (data_.available_read() > INT_MAX) |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 72 | return KM_ERROR_INVALID_INPUT_LENGTH; |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 73 | int message_size = (int)data_.available_read(); |
Adam Langley | f2aefdf | 2014-09-26 11:16:10 -0700 | [diff] [blame] | 74 | #endif |
| 75 | |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 76 | if (message_size != RSA_size(rsa_key_)) |
| 77 | return KM_ERROR_INVALID_INPUT_LENGTH; |
| 78 | |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 79 | if (data_.available_read() != signature.available_read()) |
| 80 | return KM_ERROR_VERIFICATION_FAILED; |
| 81 | |
| 82 | UniquePtr<uint8_t[]> decrypted_data(new uint8_t[RSA_size(rsa_key_)]); |
| 83 | int bytes_decrypted = RSA_public_decrypt(signature.available_read(), signature.peek_read(), |
| 84 | decrypted_data.get(), rsa_key_, RSA_NO_PADDING); |
| 85 | if (bytes_decrypted < 0) |
| 86 | return KM_ERROR_UNKNOWN_ERROR; |
| 87 | assert(bytes_decrypted == RSA_size(rsa_key_)); |
| 88 | |
| 89 | if (memcmp_s(decrypted_data.get(), data_.peek_read(), data_.available_read()) == 0) |
| 90 | return KM_ERROR_OK; |
| 91 | return KM_ERROR_VERIFICATION_FAILED; |
| 92 | } |
| 93 | |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 94 | const int OAEP_PADDING_OVERHEAD = 41; |
| 95 | const int PKCS1_PADDING_OVERHEAD = 11; |
| 96 | |
| 97 | keymaster_error_t RsaEncryptOperation::Finish(const Buffer& /* signature */, Buffer* output) { |
| 98 | int openssl_padding; |
| 99 | |
| 100 | #if defined(OPENSSL_IS_BORINGSSL) |
| 101 | size_t message_size = data_.available_read(); |
| 102 | #else |
| 103 | if (data_.available_read() > INT_MAX) |
| 104 | return KM_ERROR_INVALID_INPUT_LENGTH; |
| 105 | int message_size = (int)data_.available_read(); |
| 106 | #endif |
| 107 | |
| 108 | switch (padding_) { |
| 109 | case KM_PAD_RSA_OAEP: |
| 110 | openssl_padding = RSA_PKCS1_OAEP_PADDING; |
| 111 | if (message_size >= RSA_size(rsa_key_) - OAEP_PADDING_OVERHEAD) { |
| 112 | logger().error("Cannot encrypt %d bytes with %d-byte key and OAEP padding", |
| 113 | data_.available_read(), RSA_size(rsa_key_)); |
| 114 | return KM_ERROR_INVALID_INPUT_LENGTH; |
| 115 | } |
| 116 | break; |
| 117 | case KM_PAD_RSA_PKCS1_1_5_ENCRYPT: |
| 118 | openssl_padding = RSA_PKCS1_PADDING; |
| 119 | if (message_size >= RSA_size(rsa_key_) - PKCS1_PADDING_OVERHEAD) { |
| 120 | logger().error("Cannot encrypt %d bytes with %d-byte key and PKCS1 padding", |
| 121 | data_.available_read(), RSA_size(rsa_key_)); |
| 122 | return KM_ERROR_INVALID_INPUT_LENGTH; |
| 123 | } |
| 124 | break; |
| 125 | default: |
| 126 | logger().error("Padding mode %d not supported", padding_); |
| 127 | return KM_ERROR_UNSUPPORTED_PADDING_MODE; |
| 128 | } |
| 129 | |
| 130 | output->Reinitialize(RSA_size(rsa_key_)); |
| 131 | int bytes_encrypted = RSA_public_encrypt(data_.available_read(), data_.peek_read(), |
| 132 | output->peek_write(), rsa_key_, openssl_padding); |
| 133 | |
| 134 | if (bytes_encrypted < 0) { |
| 135 | logger().error("Error %d encrypting data with RSA", ERR_get_error()); |
| 136 | return KM_ERROR_UNKNOWN_ERROR; |
| 137 | } |
| 138 | assert(bytes_encrypted == RSA_size(rsa_key_)); |
| 139 | output->advance_write(bytes_encrypted); |
| 140 | |
| 141 | return KM_ERROR_OK; |
| 142 | } |
| 143 | |
| 144 | keymaster_error_t RsaDecryptOperation::Finish(const Buffer& /* signature */, Buffer* output) { |
| 145 | int openssl_padding; |
| 146 | switch (padding_) { |
| 147 | case KM_PAD_RSA_OAEP: |
| 148 | openssl_padding = RSA_PKCS1_OAEP_PADDING; |
| 149 | break; |
| 150 | case KM_PAD_RSA_PKCS1_1_5_ENCRYPT: |
| 151 | openssl_padding = RSA_PKCS1_PADDING; |
| 152 | break; |
| 153 | default: |
| 154 | logger().error("Padding mode %d not supported", padding_); |
| 155 | return KM_ERROR_UNSUPPORTED_PADDING_MODE; |
| 156 | } |
| 157 | |
| 158 | output->Reinitialize(RSA_size(rsa_key_)); |
| 159 | int bytes_decrypted = RSA_private_decrypt(data_.available_read(), data_.peek_read(), |
| 160 | output->peek_write(), rsa_key_, openssl_padding); |
| 161 | |
| 162 | if (bytes_decrypted < 0) { |
| 163 | logger().error("Error %d decrypting data with RSA", ERR_get_error()); |
| 164 | return KM_ERROR_UNKNOWN_ERROR; |
| 165 | } |
| 166 | output->advance_write(bytes_decrypted); |
| 167 | |
| 168 | return KM_ERROR_OK; |
| 169 | } |
| 170 | |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 171 | } // namespace keymaster |