blob: df39006fff598643de4aa57ad7bfe58d3dfbd050 [file] [log] [blame]
Shawn Willden0a4df7e2014-08-28 16:09:05 -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
Shawn Willden46a420d2014-12-02 07:01:21 -070017#include <limits.h>
18
19#include <openssl/err.h>
Shawn Willden0a4df7e2014-08-28 16:09:05 -060020#include <openssl/evp.h>
Shawn Willden46a420d2014-12-02 07:01:21 -070021#include <openssl/rsa.h>
Shawn Willden0a4df7e2014-08-28 16:09:05 -060022
23#include "rsa_operation.h"
24#include "openssl_utils.h"
25
26namespace keymaster {
27
28struct RSA_Delete {
29 void operator()(RSA* p) const { RSA_free(p); }
30};
31
32RsaOperation::~RsaOperation() {
33 if (rsa_key_ != NULL)
34 RSA_free(rsa_key_);
35}
36
37keymaster_error_t RsaOperation::Update(const Buffer& input, Buffer* /* output */) {
38 switch (purpose()) {
39 default:
40 return KM_ERROR_UNIMPLEMENTED;
41 case KM_PURPOSE_SIGN:
42 case KM_PURPOSE_VERIFY:
Shawn Willden46a420d2014-12-02 07:01:21 -070043 case KM_PURPOSE_ENCRYPT:
44 case KM_PURPOSE_DECRYPT:
Shawn Willden0a4df7e2014-08-28 16:09:05 -060045 return StoreData(input);
46 }
47}
48
49keymaster_error_t RsaOperation::StoreData(const Buffer& input) {
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
56keymaster_error_t RsaSignOperation::Finish(const Buffer& /* signature */, Buffer* output) {
57 output->Reinitialize(RSA_size(rsa_key_));
Shawn Willden0a4df7e2014-08-28 16:09:05 -060058 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
67keymaster_error_t RsaVerifyOperation::Finish(const Buffer& signature, Buffer* /* output */) {
Adam Langleyf2aefdf2014-09-26 11:16:10 -070068#if defined(OPENSSL_IS_BORINGSSL)
Shawn Willden46a420d2014-12-02 07:01:21 -070069 size_t message_size = data_.available_read();
Adam Langleyf2aefdf2014-09-26 11:16:10 -070070#else
Shawn Willden46a420d2014-12-02 07:01:21 -070071 if (data_.available_read() > INT_MAX)
Shawn Willden0a4df7e2014-08-28 16:09:05 -060072 return KM_ERROR_INVALID_INPUT_LENGTH;
Shawn Willden46a420d2014-12-02 07:01:21 -070073 int message_size = (int)data_.available_read();
Adam Langleyf2aefdf2014-09-26 11:16:10 -070074#endif
75
Shawn Willden46a420d2014-12-02 07:01:21 -070076 if (message_size != RSA_size(rsa_key_))
77 return KM_ERROR_INVALID_INPUT_LENGTH;
78
Shawn Willden0a4df7e2014-08-28 16:09:05 -060079 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 Willden46a420d2014-12-02 07:01:21 -070094const int OAEP_PADDING_OVERHEAD = 41;
95const int PKCS1_PADDING_OVERHEAD = 11;
96
97keymaster_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
144keymaster_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 Willden0a4df7e2014-08-28 16:09:05 -0600171} // namespace keymaster