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_key.h" |
| 18 | |
| 19 | #include <openssl/err.h> |
| 20 | #include <openssl/rand.h> |
| 21 | |
| 22 | #include "hmac_operation.h" |
| 23 | |
| 24 | namespace keymaster { |
| 25 | |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame^] | 26 | class HmacKeyFactory : public SymmetricKeyFactory { |
| 27 | public: |
| 28 | keymaster_algorithm_t registry_key() const { return KM_ALGORITHM_HMAC; } |
| 29 | |
| 30 | virtual Key* LoadKey(const UnencryptedKeyBlob& blob, const Logger& logger, |
| 31 | keymaster_error_t* error) { |
| 32 | return new HmacKey(blob, logger, error); |
| 33 | } |
| 34 | |
| 35 | virtual SymmetricKey* CreateKey(const AuthorizationSet& auths, const Logger& logger) { |
| 36 | return new HmacKey(auths, logger); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | static KeyFactoryRegistry::Registration<HmacKeyFactory> registration; |
| 41 | |
Shawn Willden | 0d560bf | 2014-12-15 17:44:02 -0700 | [diff] [blame] | 42 | Operation* HmacKey::CreateOperation(keymaster_purpose_t purpose, keymaster_error_t* error) { |
| 43 | *error = KM_ERROR_OK; |
| 44 | |
| 45 | uint32_t tag_length; |
| 46 | if (!authorizations().GetTagValue(TAG_MAC_LENGTH, &tag_length)) |
| 47 | *error = KM_ERROR_UNSUPPORTED_MAC_LENGTH; |
| 48 | |
| 49 | keymaster_digest_t digest; |
Shawn Willden | 62c2286 | 2014-12-17 08:36:20 -0700 | [diff] [blame] | 50 | if (!authorizations().GetTagValue(TAG_DIGEST, &digest)) |
Shawn Willden | 0d560bf | 2014-12-15 17:44:02 -0700 | [diff] [blame] | 51 | *error = KM_ERROR_UNSUPPORTED_DIGEST; |
| 52 | |
| 53 | if (*error != KM_ERROR_OK) |
| 54 | return NULL; |
| 55 | |
| 56 | UniquePtr<Operation> op; |
| 57 | switch (purpose) { |
| 58 | case KM_PURPOSE_SIGN: |
| 59 | case KM_PURPOSE_VERIFY: |
| 60 | op.reset( |
| 61 | new HmacOperation(purpose, logger_, key_data(), key_data_size(), digest, tag_length)); |
| 62 | break; |
| 63 | default: |
| 64 | *error = KM_ERROR_UNSUPPORTED_PURPOSE; |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | if (!op.get()) |
| 69 | *error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 70 | |
| 71 | return op.release(); |
| 72 | } |
| 73 | |
| 74 | } // namespace keymaster |