Shawn Willden | 19fca88 | 2015-01-22 16:35:30 -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 | |
Shawn Willden | 95e1382 | 2014-12-15 16:12:16 -0700 | [diff] [blame] | 17 | #include "aes_key.h" |
| 18 | |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 19 | #include <assert.h> |
| 20 | |
Shawn Willden | 0f906ec | 2015-06-20 09:16:30 -0600 | [diff] [blame^] | 21 | #include <new> |
| 22 | |
Shawn Willden | 19fca88 | 2015-01-22 16:35:30 -0700 | [diff] [blame] | 23 | #include <openssl/err.h> |
| 24 | #include <openssl/rand.h> |
| 25 | |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 26 | #include "aes_operation.h" |
Shawn Willden | 19fca88 | 2015-01-22 16:35:30 -0700 | [diff] [blame] | 27 | |
| 28 | namespace keymaster { |
| 29 | |
Shawn Willden | 0629810 | 2015-05-25 23:12:48 -0600 | [diff] [blame] | 30 | AesEncryptionOperationFactory encrypt_factory; |
| 31 | AesDecryptionOperationFactory decrypt_factory; |
| 32 | |
| 33 | OperationFactory* AesKeyFactory::GetOperationFactory(keymaster_purpose_t purpose) const { |
| 34 | switch (purpose) { |
| 35 | case KM_PURPOSE_ENCRYPT: |
| 36 | return &encrypt_factory; |
| 37 | case KM_PURPOSE_DECRYPT: |
| 38 | return &decrypt_factory; |
| 39 | default: |
| 40 | return nullptr; |
| 41 | } |
| 42 | } |
| 43 | |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 44 | keymaster_error_t AesKeyFactory::LoadKey(const KeymasterKeyBlob& key_material, |
| 45 | const AuthorizationSet& hw_enforced, |
Shawn Willden | 0629810 | 2015-05-25 23:12:48 -0600 | [diff] [blame] | 46 | const AuthorizationSet& sw_enforced, |
| 47 | UniquePtr<Key>* key) const { |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 48 | if (!key) |
| 49 | return KM_ERROR_OUTPUT_PARAMETER_NULL; |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 50 | |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 51 | keymaster_error_t error = KM_ERROR_OK; |
Shawn Willden | 0f906ec | 2015-06-20 09:16:30 -0600 | [diff] [blame^] | 52 | key->reset(new (std::nothrow) AesKey(key_material, hw_enforced, sw_enforced, &error)); |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 53 | if (!key->get()) |
| 54 | error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 55 | return error; |
Shawn Willden | f923963 | 2015-05-12 06:57:37 -0600 | [diff] [blame] | 56 | } |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 57 | |
Shawn Willden | 19fca88 | 2015-01-22 16:35:30 -0700 | [diff] [blame] | 58 | } // namespace keymaster |