blob: a2dbc4db420b868af1ea23218f2f583d838acd65 [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 Willden6bfbff02015-02-06 19:48:24 -070029keymaster_error_t EcdsaOperation::Update(const AuthorizationSet& /* additional_params */,
30 const Buffer& input, Buffer* /* output */,
Shawn Willdenb7361132014-12-08 08:15:14 -070031 size_t* input_consumed) {
32 assert(input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060033 switch (purpose()) {
34 default:
35 return KM_ERROR_UNIMPLEMENTED;
36 case KM_PURPOSE_SIGN:
37 case KM_PURPOSE_VERIFY:
Shawn Willdenb7361132014-12-08 08:15:14 -070038 return StoreData(input, input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060039 }
40}
41
Shawn Willdenb7361132014-12-08 08:15:14 -070042keymaster_error_t EcdsaOperation::StoreData(const Buffer& input, size_t* input_consumed) {
Shawn Willdenb3407022014-08-27 06:30:52 -060043 if (!data_.reserve(data_.available_read() + input.available_read()) ||
Shawn Willdend67afae2014-08-19 12:36:27 -060044 !data_.write(input.peek_read(), input.available_read()))
45 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willdenb7361132014-12-08 08:15:14 -070046 *input_consumed = input.available_read();
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060047 return KM_ERROR_OK;
48}
49
Shawn Willden6bfbff02015-02-06 19:48:24 -070050keymaster_error_t EcdsaSignOperation::Finish(const AuthorizationSet& /* additional_params */,
51 const Buffer& /* signature */, Buffer* output) {
Shawn Willdenb7361132014-12-08 08:15:14 -070052 assert(output);
Shawn Willdend67afae2014-08-19 12:36:27 -060053 output->Reinitialize(ECDSA_size(ecdsa_key_));
54 unsigned int siglen;
55 if (!ECDSA_sign(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
56 output->peek_write(), &siglen, ecdsa_key_))
57 return KM_ERROR_UNKNOWN_ERROR;
58 output->advance_write(siglen);
59 return KM_ERROR_OK;
60}
61
Shawn Willden6bfbff02015-02-06 19:48:24 -070062keymaster_error_t EcdsaVerifyOperation::Finish(const AuthorizationSet& /* additional_params */,
63 const Buffer& signature, Buffer* /* output */) {
Shawn Willdend67afae2014-08-19 12:36:27 -060064 int result = ECDSA_verify(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
65 signature.peek_read(), signature.available_read(), ecdsa_key_);
66 if (result < 0)
67 return KM_ERROR_UNKNOWN_ERROR;
68 else if (result == 0)
69 return KM_ERROR_VERIFICATION_FAILED;
70 else
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060071 return KM_ERROR_OK;
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060072}
73
Shawn Willdenc3864dd2014-08-18 15:20:01 -060074} // namespace keymaster