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