blob: 18211a800ea5e796f97cee8f734fd336173e05c9 [file] [log] [blame]
Shawn Willden28e41472014-08-18 13:35:22 -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/bn.h>
18
19#include "dsa_operation.h"
20#include "openssl_utils.h"
21
22namespace keymaster {
23
Shawn Willden5b41ca22014-08-18 14:29:14 -060024DsaOperation::~DsaOperation() {
25 if (dsa_key_ != NULL)
26 DSA_free(dsa_key_);
27}
28
29keymaster_error_t DsaOperation::Update(const Buffer& input, Buffer* /* output */) {
30 switch (purpose()) {
31 default:
32 return KM_ERROR_UNIMPLEMENTED;
33 case KM_PURPOSE_SIGN:
34 case KM_PURPOSE_VERIFY:
35 return StoreData(input);
36 }
37}
38
39keymaster_error_t DsaOperation::StoreData(const Buffer& input) {
Shawn Willdend67afae2014-08-19 12:36:27 -060040 if (!data_.reserve(input.available_read()) ||
41 !data_.write(input.peek_read(), input.available_read()))
Shawn Willden5b41ca22014-08-18 14:29:14 -060042 return KM_ERROR_INVALID_INPUT_LENGTH;
43 return KM_ERROR_OK;
44}
45
Shawn Willdend67afae2014-08-19 12:36:27 -060046keymaster_error_t DsaSignOperation::Finish(const Buffer& /* signature */, Buffer* output) {
47 output->Reinitialize(DSA_size(dsa_key_));
48 if (data_.available_read() != output->buffer_size())
49 return KM_ERROR_INVALID_INPUT_LENGTH;
Shawn Willden5b41ca22014-08-18 14:29:14 -060050
Shawn Willdend67afae2014-08-19 12:36:27 -060051 unsigned int siglen;
52 if (!DSA_sign(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
53 output->peek_write(), &siglen, dsa_key_))
54 return KM_ERROR_UNKNOWN_ERROR;
55 output->advance_write(siglen);
56 return KM_ERROR_OK;
57}
58
59keymaster_error_t DsaVerifyOperation::Finish(const Buffer& signature, Buffer* /* output */) {
60 if ((int)data_.available_read() != DSA_size(dsa_key_))
61 return KM_ERROR_INVALID_INPUT_LENGTH;
62
63 int result = DSA_verify(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
64 signature.peek_read(), signature.available_read(), dsa_key_);
65 if (result < 0)
66 return KM_ERROR_UNKNOWN_ERROR;
67 else if (result == 0)
68 return KM_ERROR_VERIFICATION_FAILED;
69 else
Shawn Willden5b41ca22014-08-18 14:29:14 -060070 return KM_ERROR_OK;
Shawn Willden5b41ca22014-08-18 14:29:14 -060071}
72
Shawn Willden28e41472014-08-18 13:35:22 -060073} // namespace keymaster