blob: a013b78b8bd3ddac96cd071c956c8aaf8e0388bf [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
17#include <openssl/rsa.h>
18#include <openssl/evp.h>
19
20#include "rsa_operation.h"
21#include "openssl_utils.h"
22
23namespace keymaster {
24
25struct RSA_Delete {
26 void operator()(RSA* p) const { RSA_free(p); }
27};
28
29RsaOperation::~RsaOperation() {
30 if (rsa_key_ != NULL)
31 RSA_free(rsa_key_);
32}
33
34keymaster_error_t RsaOperation::Update(const Buffer& input, Buffer* /* output */) {
35 switch (purpose()) {
36 default:
37 return KM_ERROR_UNIMPLEMENTED;
38 case KM_PURPOSE_SIGN:
39 case KM_PURPOSE_VERIFY:
40 return StoreData(input);
41 }
42}
43
44keymaster_error_t RsaOperation::StoreData(const Buffer& input) {
45 if (!data_.reserve(data_.available_read() + input.available_read()) ||
46 !data_.write(input.peek_read(), input.available_read()))
47 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
48 return KM_ERROR_OK;
49}
50
51keymaster_error_t RsaSignOperation::Finish(const Buffer& /* signature */, Buffer* output) {
52 output->Reinitialize(RSA_size(rsa_key_));
Shawn Willden0a4df7e2014-08-28 16:09:05 -060053 int bytes_encrypted = RSA_private_encrypt(data_.available_read(), data_.peek_read(),
54 output->peek_write(), rsa_key_, RSA_NO_PADDING);
55 if (bytes_encrypted < 0)
56 return KM_ERROR_UNKNOWN_ERROR;
57 assert(bytes_encrypted == RSA_size(rsa_key_));
58 output->advance_write(bytes_encrypted);
59 return KM_ERROR_OK;
60}
61
62keymaster_error_t RsaVerifyOperation::Finish(const Buffer& signature, Buffer* /* output */) {
Adam Langleyf2aefdf2014-09-26 11:16:10 -070063#if defined(OPENSSL_IS_BORINGSSL)
64 if (data_.available_read() != RSA_size(rsa_key_))
65 return KM_ERROR_INVALID_INPUT_LENGTH;
66#else
Shawn Willden0a4df7e2014-08-28 16:09:05 -060067 if ((int)data_.available_read() != RSA_size(rsa_key_))
68 return KM_ERROR_INVALID_INPUT_LENGTH;
Adam Langleyf2aefdf2014-09-26 11:16:10 -070069#endif
70
Shawn Willden0a4df7e2014-08-28 16:09:05 -060071 if (data_.available_read() != signature.available_read())
72 return KM_ERROR_VERIFICATION_FAILED;
73
74 UniquePtr<uint8_t[]> decrypted_data(new uint8_t[RSA_size(rsa_key_)]);
75 int bytes_decrypted = RSA_public_decrypt(signature.available_read(), signature.peek_read(),
76 decrypted_data.get(), rsa_key_, RSA_NO_PADDING);
77 if (bytes_decrypted < 0)
78 return KM_ERROR_UNKNOWN_ERROR;
79 assert(bytes_decrypted == RSA_size(rsa_key_));
80
81 if (memcmp_s(decrypted_data.get(), data_.peek_read(), data_.available_read()) == 0)
82 return KM_ERROR_OK;
83 return KM_ERROR_VERIFICATION_FAILED;
84}
85
86} // namespace keymaster