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 #ifndef SYSTEM_KEYMASTER_HMAC_OPERATION_H_ 18 #define SYSTEM_KEYMASTER_HMAC_OPERATION_H_ 19 20 #include <keymaster/operation.h> 21 #include <openssl/hmac.h> 22 23 namespace keymaster { 24 25 class HmacOperation : public Operation { 26 public: 27 HmacOperation(Key&& key, keymaster_purpose_t purpose, keymaster_digest_t digest, 28 size_t mac_length, size_t min_mac_length); 29 ~HmacOperation(); 30 31 virtual keymaster_error_t Begin(const AuthorizationSet& input_params, 32 AuthorizationSet* output_params); 33 virtual keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input, 34 AuthorizationSet* output_params, Buffer* output, 35 size_t* input_consumed); 36 virtual keymaster_error_t Abort(); 37 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& input, 38 const Buffer& signature, AuthorizationSet* output_params, 39 Buffer* output); 40 error()41 keymaster_error_t error() { return error_; } 42 43 private: 44 HMAC_CTX ctx_; 45 keymaster_error_t error_; 46 const size_t mac_length_; 47 const size_t min_mac_length_; 48 }; 49 50 /** 51 * Abstract base for HMAC operation factories. This class does all of the work to create 52 * HMAC operations. 53 */ 54 class HmacOperationFactory : public OperationFactory { 55 public: registry_key()56 virtual KeyType registry_key() const { return KeyType(KM_ALGORITHM_HMAC, purpose()); } 57 58 virtual OperationPtr CreateOperation(Key&& key, const AuthorizationSet& begin_params, 59 keymaster_error_t* error); 60 61 virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const; 62 63 virtual keymaster_purpose_t purpose() const = 0; 64 }; 65 66 class HmacSignOperationFactory : public HmacOperationFactory { purpose()67 keymaster_purpose_t purpose() const { return KM_PURPOSE_SIGN; } 68 }; 69 70 class HmacVerifyOperationFactory : public HmacOperationFactory { purpose()71 keymaster_purpose_t purpose() const { return KM_PURPOSE_VERIFY; } 72 }; 73 74 } // namespace keymaster 75 76 #endif // SYSTEM_KEYMASTER_HMAC_OPERATION_H_ 77