blob: 948d63c1a8b60da6d012d7446500d1f9a7c50381 [file] [log] [blame]
Shawn Willden19fca882015-01-22 16:35:30 -07001/*
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 Willden95e13822014-12-15 16:12:16 -070017#include "aes_key.h"
18
Shawn Willden907c3012014-12-08 15:51:55 -070019#include <assert.h>
20
Shawn Willden19fca882015-01-22 16:35:30 -070021#include <openssl/err.h>
22#include <openssl/rand.h>
23
Shawn Willden907c3012014-12-08 15:51:55 -070024#include "aes_operation.h"
25#include "unencrypted_key_blob.h"
Shawn Willden19fca882015-01-22 16:35:30 -070026
27namespace keymaster {
28
Shawn Willden907c3012014-12-08 15:51:55 -070029Operation* AesKey::CreateOperation(keymaster_purpose_t purpose, keymaster_error_t* error) {
30 keymaster_block_mode_t block_mode;
31 if (!authorizations().GetTagValue(TAG_BLOCK_MODE, &block_mode)) {
32 *error = KM_ERROR_UNSUPPORTED_BLOCK_MODE;
33 return NULL;
34 }
35
36 switch (block_mode) {
37 case KM_MODE_OCB:
38 return CreateOcbOperation(purpose, error);
39 default:
40 *error = KM_ERROR_UNSUPPORTED_BLOCK_MODE;
41 return NULL;
42 }
43}
44
45Operation* AesKey::CreateOcbOperation(keymaster_purpose_t purpose, keymaster_error_t* error) {
46 *error = KM_ERROR_OK;
47
Shawn Willden95e13822014-12-15 16:12:16 -070048 if (key_data_size() != 16 && key_data_size() != 24 && key_data_size() != 32)
49 *error = KM_ERROR_UNSUPPORTED_KEY_SIZE;
50
Shawn Willden907c3012014-12-08 15:51:55 -070051 uint32_t chunk_length;
52 if (!authorizations().GetTagValue(TAG_CHUNK_LENGTH, &chunk_length))
53 // TODO(swillden): Create and use a better return code.
Shawn Willdenbe4a2a32014-12-15 14:51:10 -070054 *error = KM_ERROR_INVALID_ARGUMENT;
Shawn Willden907c3012014-12-08 15:51:55 -070055
56 uint32_t tag_length;
57 if (!authorizations().GetTagValue(TAG_MAC_LENGTH, &tag_length))
58 // TODO(swillden): Create and use a better return code.
Shawn Willdenbe4a2a32014-12-15 14:51:10 -070059 *error = KM_ERROR_INVALID_ARGUMENT;
60
61 keymaster_padding_t padding;
62 if (authorizations().GetTagValue(TAG_PADDING, &padding) && padding != KM_PAD_NONE) {
63 *error = KM_ERROR_UNSUPPORTED_PADDING_MODE;
64 }
Shawn Willden907c3012014-12-08 15:51:55 -070065
66 if (*error != KM_ERROR_OK)
67 return NULL;
68
69 keymaster_blob_t additional_data = {0, 0};
70 authorizations().GetTagValue(TAG_ADDITIONAL_DATA, &additional_data);
71
72 Operation* op = NULL;
73 switch (purpose) {
74 case KM_PURPOSE_ENCRYPT:
Shawn Willden6dde87c2014-12-11 14:08:48 -070075 case KM_PURPOSE_DECRYPT:
Shawn Willden95e13822014-12-15 16:12:16 -070076 op = new AesOcbOperation(purpose, logger_, key_data(), key_data_size(), chunk_length,
Shawn Willden6dde87c2014-12-11 14:08:48 -070077 tag_length, additional_data);
Shawn Willden907c3012014-12-08 15:51:55 -070078 break;
79 default:
80 *error = KM_ERROR_UNSUPPORTED_PURPOSE;
81 return NULL;
82 }
83
84 if (!op)
85 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
86
87 return op;
Shawn Willden19fca882015-01-22 16:35:30 -070088}
89
Shawn Willden19fca882015-01-22 16:35:30 -070090} // namespace keymaster