blob: 620df65f5fd3badde023293b4b3258a1e0bafb29 [file] [log] [blame]
Shawn Willden1615f2e2014-08-13 10:37:40 -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/rsa.h>
18#include <openssl/evp.h>
19
20#include "rsa_operation.h"
21
22namespace keymaster {
23
24struct EVP_PKEY_Delete {
25 void operator()(EVP_PKEY* p) const {
26 EVP_PKEY_free(p);
27 }
28};
29
30RsaOperation::RsaOperation(keymaster_purpose_t purpose, const KeyBlob& key)
31 : Operation(purpose), rsa_key_(NULL) {
32 assert(key.algorithm() == KM_ALGORITHM_RSA);
33
34 if ((!key.enforced().GetTagValue(TAG_DIGEST, &digest_) &&
35 !key.unenforced().GetTagValue(TAG_DIGEST, &digest_)) ||
36 digest_ != KM_DIGEST_NONE) {
37 error_ = KM_ERROR_UNSUPPORTED_DIGEST;
38 return;
39 }
40
41 if ((!key.enforced().GetTagValue(TAG_PADDING, &padding_) &&
42 !key.unenforced().GetTagValue(TAG_PADDING, &padding_)) ||
43 padding_ != KM_PAD_NONE) {
44 error_ = KM_ERROR_UNSUPPORTED_PADDING_MODE;
45 return;
46 }
47
48 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> evp_key(EVP_PKEY_new());
49 if (evp_key.get() == NULL) {
50 error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED;
51 return;
52 }
53
54 EVP_PKEY* tmp_pkey = evp_key.get();
55 const uint8_t* key_material = key.key_material();
56 if (d2i_PrivateKey(EVP_PKEY_RSA, &tmp_pkey, &key_material, key.key_material_length()) == NULL) {
57 error_ = KM_ERROR_INVALID_KEY_BLOB;
58 return;
59 }
60
61 rsa_key_ = EVP_PKEY_get1_RSA(evp_key.get());
62 if (rsa_key_ == NULL) {
63 error_ = KM_ERROR_UNKNOWN_ERROR;
64 return;
65 }
66
67 // Since we're not using a digest function, we just need to store the text, up to the key
68 // size, until Finish is called, so we allocate a place to put it.
Shawn Willden43e999e2014-08-13 13:29:50 -060069 if (!data_.Reinitialize(RSA_size(rsa_key_))) {
Shawn Willden1615f2e2014-08-13 10:37:40 -060070 error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED;
71 return;
72 }
73 error_ = KM_ERROR_OK;
74}
75
76RsaOperation::~RsaOperation() {
77 if (rsa_key_ != NULL)
78 RSA_free(rsa_key_);
79}
80
Shawn Willden43e999e2014-08-13 13:29:50 -060081keymaster_error_t RsaOperation::Update(const Buffer& input, Buffer* /* output */) {
Shawn Willden1615f2e2014-08-13 10:37:40 -060082 switch (purpose()) {
83 default:
84 return KM_ERROR_UNIMPLEMENTED;
85 case KM_PURPOSE_SIGN:
Shawn Willden43e999e2014-08-13 13:29:50 -060086 case KM_PURPOSE_VERIFY:
87 return StoreData(input);
Shawn Willden1615f2e2014-08-13 10:37:40 -060088 }
89}
90
Shawn Willden43e999e2014-08-13 13:29:50 -060091keymaster_error_t RsaOperation::StoreData(const Buffer& input) {
92 if (!data_.write(input.peek_read(), input.available_read()))
Shawn Willden1615f2e2014-08-13 10:37:40 -060093 return KM_ERROR_INVALID_INPUT_LENGTH;
94 return KM_ERROR_OK;
95}
96
Shawn Willden43e999e2014-08-13 13:29:50 -060097keymaster_error_t RsaOperation::Finish(const Buffer& signature, Buffer* output) {
Shawn Willden1615f2e2014-08-13 10:37:40 -060098 switch (purpose()) {
99 case KM_PURPOSE_SIGN: {
Shawn Willden43e999e2014-08-13 13:29:50 -0600100 output->Reinitialize(RSA_size(rsa_key_));
101 if (data_.available_read() != output->buffer_size())
Shawn Willden1615f2e2014-08-13 10:37:40 -0600102 return KM_ERROR_INVALID_INPUT_LENGTH;
103
Shawn Willden43e999e2014-08-13 13:29:50 -0600104 int bytes_encrypted = RSA_private_encrypt(data_.available_read(), data_.peek_read(),
105 output->peek_write(), rsa_key_, RSA_NO_PADDING);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600106 if (bytes_encrypted < 0)
107 return KM_ERROR_UNKNOWN_ERROR;
108 assert(bytes_encrypted == RSA_size(rsa_key_));
Shawn Willden43e999e2014-08-13 13:29:50 -0600109 output->advance_write(bytes_encrypted);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600110 return KM_ERROR_OK;
111 }
Shawn Willden43e999e2014-08-13 13:29:50 -0600112 case KM_PURPOSE_VERIFY: {
113 if ((int)data_.available_read() != RSA_size(rsa_key_))
114 return KM_ERROR_INVALID_INPUT_LENGTH;
115 if (data_.available_read() != signature.available_read())
116 return KM_ERROR_VERIFICATION_FAILED;
117
118 UniquePtr<uint8_t[]> decrypted_data(new uint8_t[RSA_size(rsa_key_)]);
119 int bytes_decrypted = RSA_public_decrypt(signature.available_read(), signature.peek_read(),
120 decrypted_data.get(), rsa_key_, RSA_NO_PADDING);
121 if (bytes_decrypted < 0)
122 return KM_ERROR_UNKNOWN_ERROR;
123 assert(bytes_decrypted == RSA_size(rsa_key_));
124
125 if (memcmp_s(decrypted_data.get(), data_.peek_read(), data_.available_read()) == 0)
126 return KM_ERROR_OK;
127 return KM_ERROR_VERIFICATION_FAILED;
128 }
Shawn Willden1615f2e2014-08-13 10:37:40 -0600129 default:
130 return KM_ERROR_UNIMPLEMENTED;
131 }
132}
133
134} // namespace keymaster