blob: aa41095e395a08c9035d42b8bc8a35e9cb35fe19 [file] [log] [blame]
Shawn Willdenc3864dd2014-08-18 15:20:01 -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/ecdsa.h>
18
19#include "ecdsa_operation.h"
20#include "openssl_utils.h"
21
22namespace keymaster {
23
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060024EcdsaOperation::~EcdsaOperation() {
25 if (ecdsa_key_ != NULL)
26 EC_KEY_free(ecdsa_key_);
27}
28
Shawn Willden125febc2014-12-08 08:15:14 -070029keymaster_error_t EcdsaOperation::Update(const Buffer& input, Buffer* /* output */,
30 size_t* input_consumed) {
31 assert(input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060032 switch (purpose()) {
33 default:
34 return KM_ERROR_UNIMPLEMENTED;
35 case KM_PURPOSE_SIGN:
36 case KM_PURPOSE_VERIFY:
Shawn Willden125febc2014-12-08 08:15:14 -070037 return StoreData(input, input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060038 }
39}
40
Shawn Willden125febc2014-12-08 08:15:14 -070041keymaster_error_t EcdsaOperation::StoreData(const Buffer& input, size_t* input_consumed) {
Shawn Willdenb3407022014-08-27 06:30:52 -060042 if (!data_.reserve(data_.available_read() + input.available_read()) ||
Shawn Willdend67afae2014-08-19 12:36:27 -060043 !data_.write(input.peek_read(), input.available_read()))
44 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden125febc2014-12-08 08:15:14 -070045 *input_consumed = input.available_read();
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060046 return KM_ERROR_OK;
47}
48
Shawn Willdend67afae2014-08-19 12:36:27 -060049keymaster_error_t EcdsaSignOperation::Finish(const Buffer& /* signature */, Buffer* output) {
Shawn Willden125febc2014-12-08 08:15:14 -070050 assert(output);
Shawn Willdend67afae2014-08-19 12:36:27 -060051 output->Reinitialize(ECDSA_size(ecdsa_key_));
52 unsigned int siglen;
53 if (!ECDSA_sign(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
54 output->peek_write(), &siglen, ecdsa_key_))
55 return KM_ERROR_UNKNOWN_ERROR;
56 output->advance_write(siglen);
57 return KM_ERROR_OK;
58}
59
60keymaster_error_t EcdsaVerifyOperation::Finish(const Buffer& signature, Buffer* /* output */) {
61 int result = ECDSA_verify(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
62 signature.peek_read(), signature.available_read(), ecdsa_key_);
63 if (result < 0)
64 return KM_ERROR_UNKNOWN_ERROR;
65 else if (result == 0)
66 return KM_ERROR_VERIFICATION_FAILED;
67 else
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060068 return KM_ERROR_OK;
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060069}
70
Shawn Willdenc3864dd2014-08-18 15:20:01 -060071} // namespace keymaster