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 "aes_operation.h"
18 
19 namespace keymaster {
20 
21 static const keymaster_block_mode_t supported_block_modes[] = {KM_MODE_ECB, KM_MODE_CBC,
22                                                                KM_MODE_CTR, KM_MODE_GCM};
23 
24 const keymaster_block_mode_t*
SupportedBlockModes(size_t * block_mode_count) const25 AesEvpCipherDescription::SupportedBlockModes(size_t* block_mode_count) const {
26     *block_mode_count = array_length(supported_block_modes);
27     return supported_block_modes;
28 }
29 
GetCipherInstance(size_t key_size,keymaster_block_mode_t block_mode,keymaster_error_t * error) const30 const EVP_CIPHER* AesEvpCipherDescription::GetCipherInstance(size_t key_size,
31                                                              keymaster_block_mode_t block_mode,
32                                                              keymaster_error_t* error) const {
33     *error = KM_ERROR_OK;
34 
35     switch (block_mode) {
36     case KM_MODE_ECB:
37         switch (key_size) {
38         case 16:
39             return EVP_aes_128_ecb();
40         case 24:
41             return EVP_aes_192_ecb();
42         case 32:
43             return EVP_aes_256_ecb();
44         };
45         *error = KM_ERROR_UNSUPPORTED_KEY_SIZE;
46         break;
47 
48     case KM_MODE_CBC:
49         switch (key_size) {
50         case 16:
51             return EVP_aes_128_cbc();
52         case 24:
53             return EVP_aes_192_cbc();
54         case 32:
55             return EVP_aes_256_cbc();
56         };
57         *error = KM_ERROR_UNSUPPORTED_KEY_SIZE;
58         break;
59 
60     case KM_MODE_CTR:
61         switch (key_size) {
62         case 16:
63             return EVP_aes_128_ctr();
64         case 24:
65             return EVP_aes_192_ctr();
66         case 32:
67             return EVP_aes_256_ctr();
68         }
69         *error = KM_ERROR_UNSUPPORTED_KEY_SIZE;
70         break;
71 
72     case KM_MODE_GCM:
73         switch (key_size) {
74         case 16:
75             return EVP_aes_128_gcm();
76         case 24:
77             return EVP_aes_192_gcm();
78         case 32:
79             return EVP_aes_256_gcm();
80         }
81         *error = KM_ERROR_UNSUPPORTED_KEY_SIZE;
82         break;
83 
84     default:
85         *error = KM_ERROR_UNSUPPORTED_BLOCK_MODE;
86         break;
87     }
88 
89     assert(*error != KM_ERROR_OK);
90     return nullptr;
91 }
92 
93 static AesEvpCipherDescription description;
GetCipherDescription() const94 const EvpCipherDescription& AesOperationFactory::GetCipherDescription() const {
95     return description;
96 }
97 
98 }  // namespace keymaster
99