Shawn Willden | 0d560bf | 2014-12-15 17:44:02 -0700 | [diff] [blame] | 1 | /* |
| 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 "hmac_operation.h" |
| 18 | |
| 19 | #include <openssl/evp.h> |
| 20 | #include <openssl/hmac.h> |
| 21 | |
| 22 | namespace keymaster { |
| 23 | |
| 24 | HmacOperation::HmacOperation(keymaster_purpose_t purpose, const Logger& logger, |
| 25 | const uint8_t* key_data, size_t key_data_size, |
| 26 | keymaster_digest_t digest, size_t tag_length) |
| 27 | : Operation(purpose, logger), error_(KM_ERROR_OK), tag_length_(tag_length) { |
| 28 | // Initialize CTX first, so dtor won't crash even if we error out later. |
| 29 | HMAC_CTX_init(&ctx_); |
| 30 | |
| 31 | const EVP_MD* md; |
| 32 | switch (digest) { |
Shawn Willden | 62c2286 | 2014-12-17 08:36:20 -0700 | [diff] [blame] | 33 | case KM_DIGEST_SHA_2_224: |
| 34 | md = EVP_sha224(); |
| 35 | break; |
Shawn Willden | 0d560bf | 2014-12-15 17:44:02 -0700 | [diff] [blame] | 36 | case KM_DIGEST_SHA_2_256: |
| 37 | md = EVP_sha256(); |
| 38 | break; |
Shawn Willden | 62c2286 | 2014-12-17 08:36:20 -0700 | [diff] [blame] | 39 | case KM_DIGEST_SHA_2_384: |
| 40 | md = EVP_sha384(); |
| 41 | break; |
| 42 | case KM_DIGEST_SHA_2_512: |
| 43 | md = EVP_sha512(); |
| 44 | break; |
Shawn Willden | 0d560bf | 2014-12-15 17:44:02 -0700 | [diff] [blame] | 45 | default: |
| 46 | error_ = KM_ERROR_UNSUPPORTED_DIGEST; |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | if ((int)tag_length_ > EVP_MD_size(md)) { |
| 51 | error_ = KM_ERROR_UNSUPPORTED_MAC_LENGTH; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | HMAC_Init_ex(&ctx_, key_data, key_data_size, md, NULL /* engine */); |
| 56 | } |
| 57 | |
| 58 | HmacOperation::~HmacOperation() { |
| 59 | HMAC_CTX_cleanup(&ctx_); |
| 60 | } |
| 61 | |
| 62 | keymaster_error_t HmacOperation::Begin() { |
| 63 | return error_; |
| 64 | } |
| 65 | |
| 66 | keymaster_error_t HmacOperation::Update(const Buffer& input, Buffer* /* output */, |
| 67 | size_t* input_consumed) { |
| 68 | if (!HMAC_Update(&ctx_, input.peek_read(), input.available_read())) |
| 69 | return KM_ERROR_UNKNOWN_ERROR; |
| 70 | *input_consumed = input.available_read(); |
| 71 | return KM_ERROR_OK; |
| 72 | } |
| 73 | |
| 74 | keymaster_error_t HmacOperation::Abort() { |
| 75 | return KM_ERROR_OK; |
| 76 | } |
| 77 | |
| 78 | keymaster_error_t HmacOperation::Finish(const Buffer& signature, Buffer* output) { |
| 79 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 80 | unsigned int digest_len; |
| 81 | if (!HMAC_Final(&ctx_, digest, &digest_len)) |
| 82 | return KM_ERROR_UNKNOWN_ERROR; |
| 83 | |
| 84 | switch (purpose()) { |
| 85 | case KM_PURPOSE_SIGN: |
| 86 | output->reserve(tag_length_); |
| 87 | output->write(digest, tag_length_); |
| 88 | return KM_ERROR_OK; |
| 89 | case KM_PURPOSE_VERIFY: |
| 90 | if (signature.available_read() != tag_length_) |
| 91 | return KM_ERROR_INVALID_INPUT_LENGTH; |
| 92 | if (memcmp(signature.peek_read(), digest, tag_length_) != 0) |
| 93 | return KM_ERROR_VERIFICATION_FAILED; |
| 94 | return KM_ERROR_OK; |
| 95 | default: |
| 96 | return KM_ERROR_UNSUPPORTED_PURPOSE; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | } // namespace keymaster |