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 | 19fca88 | 2015-01-22 16:35:30 -0700 | [diff] [blame] | 21 | #include <openssl/err.h> |
| 22 | #include <openssl/rand.h> |
| 23 | |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 24 | #include "aes_operation.h" |
| 25 | #include "unencrypted_key_blob.h" |
Shawn Willden | 19fca88 | 2015-01-22 16:35:30 -0700 | [diff] [blame] | 26 | |
| 27 | namespace keymaster { |
| 28 | |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame^] | 29 | class AesKeyFactory : public SymmetricKeyFactory { |
| 30 | public: |
| 31 | keymaster_algorithm_t registry_key() const { return KM_ALGORITHM_AES; } |
| 32 | |
| 33 | virtual Key* LoadKey(const UnencryptedKeyBlob& blob, const Logger& logger, |
| 34 | keymaster_error_t* error) { |
| 35 | return new AesKey(blob, logger, error); |
| 36 | } |
| 37 | |
| 38 | virtual SymmetricKey* CreateKey(const AuthorizationSet& auths, const Logger& logger) { |
| 39 | return new AesKey(auths, logger); |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | static KeyFactoryRegistry::Registration<AesKeyFactory> registration; |
| 44 | |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 45 | Operation* AesKey::CreateOperation(keymaster_purpose_t purpose, keymaster_error_t* error) { |
| 46 | keymaster_block_mode_t block_mode; |
| 47 | if (!authorizations().GetTagValue(TAG_BLOCK_MODE, &block_mode)) { |
| 48 | *error = KM_ERROR_UNSUPPORTED_BLOCK_MODE; |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | switch (block_mode) { |
| 53 | case KM_MODE_OCB: |
| 54 | return CreateOcbOperation(purpose, error); |
| 55 | default: |
| 56 | *error = KM_ERROR_UNSUPPORTED_BLOCK_MODE; |
| 57 | return NULL; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | Operation* AesKey::CreateOcbOperation(keymaster_purpose_t purpose, keymaster_error_t* error) { |
| 62 | *error = KM_ERROR_OK; |
| 63 | |
Shawn Willden | 95e1382 | 2014-12-15 16:12:16 -0700 | [diff] [blame] | 64 | if (key_data_size() != 16 && key_data_size() != 24 && key_data_size() != 32) |
| 65 | *error = KM_ERROR_UNSUPPORTED_KEY_SIZE; |
| 66 | |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 67 | uint32_t chunk_length; |
| 68 | if (!authorizations().GetTagValue(TAG_CHUNK_LENGTH, &chunk_length)) |
| 69 | // TODO(swillden): Create and use a better return code. |
Shawn Willden | be4a2a3 | 2014-12-15 14:51:10 -0700 | [diff] [blame] | 70 | *error = KM_ERROR_INVALID_ARGUMENT; |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 71 | |
| 72 | uint32_t tag_length; |
| 73 | if (!authorizations().GetTagValue(TAG_MAC_LENGTH, &tag_length)) |
| 74 | // TODO(swillden): Create and use a better return code. |
Shawn Willden | be4a2a3 | 2014-12-15 14:51:10 -0700 | [diff] [blame] | 75 | *error = KM_ERROR_INVALID_ARGUMENT; |
| 76 | |
| 77 | keymaster_padding_t padding; |
| 78 | if (authorizations().GetTagValue(TAG_PADDING, &padding) && padding != KM_PAD_NONE) { |
| 79 | *error = KM_ERROR_UNSUPPORTED_PADDING_MODE; |
| 80 | } |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 81 | |
| 82 | if (*error != KM_ERROR_OK) |
| 83 | return NULL; |
| 84 | |
| 85 | keymaster_blob_t additional_data = {0, 0}; |
Shawn Willden | b751033 | 2015-02-06 19:58:29 -0700 | [diff] [blame] | 86 | authorizations().GetTagValue(TAG_ASSOCIATED_DATA, &additional_data); |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 87 | |
Shawn Willden | b25ea25 | 2014-12-17 08:02:44 -0700 | [diff] [blame] | 88 | UniquePtr<Operation> op; |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 89 | switch (purpose) { |
| 90 | case KM_PURPOSE_ENCRYPT: |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame] | 91 | case KM_PURPOSE_DECRYPT: |
Shawn Willden | b25ea25 | 2014-12-17 08:02:44 -0700 | [diff] [blame] | 92 | op.reset(new AesOcbOperation(purpose, logger_, key_data(), key_data_size(), chunk_length, |
| 93 | tag_length, additional_data)); |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 94 | break; |
| 95 | default: |
| 96 | *error = KM_ERROR_UNSUPPORTED_PURPOSE; |
| 97 | return NULL; |
| 98 | } |
| 99 | |
Shawn Willden | b25ea25 | 2014-12-17 08:02:44 -0700 | [diff] [blame] | 100 | if (!op.get()) |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 101 | *error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 102 | |
Shawn Willden | b25ea25 | 2014-12-17 08:02:44 -0700 | [diff] [blame] | 103 | return op.release(); |
Shawn Willden | 19fca88 | 2015-01-22 16:35:30 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Shawn Willden | 19fca88 | 2015-01-22 16:35:30 -0700 | [diff] [blame] | 106 | } // namespace keymaster |