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 "triple_des_operation.h"
18 
19 namespace keymaster {
20 
21 static const keymaster_block_mode_t supported_block_modes[] = {KM_MODE_ECB, KM_MODE_CBC};
22 
23 const keymaster_block_mode_t*
SupportedBlockModes(size_t * block_mode_count) const24 TripleDesEvpCipherDescription::SupportedBlockModes(size_t* block_mode_count) const {
25     *block_mode_count = array_length(supported_block_modes);
26     return supported_block_modes;
27 }
28 
29 const EVP_CIPHER*
GetCipherInstance(size_t key_size,keymaster_block_mode_t block_mode,keymaster_error_t * error) const30 TripleDesEvpCipherDescription::GetCipherInstance(size_t key_size, keymaster_block_mode_t block_mode,
31                                                  keymaster_error_t* error) const {
32     *error = KM_ERROR_OK;
33 
34     switch (block_mode) {
35     case KM_MODE_ECB:
36         switch (key_size) {
37         case 16:
38             return EVP_des_ede();  // Note: OpenSSL 1.1.0 renamed this to EVP_des_ede_ecb
39         case 24:
40             return EVP_des_ede3();  // Note: OpenSSL 1.1.0 renamed this to EVP_des_ede3_ecb
41         default:
42             *error = KM_ERROR_UNSUPPORTED_KEY_SIZE;
43             break;
44         }
45         break;
46 
47     case KM_MODE_CBC:
48         switch (key_size) {
49         case 16:
50             return EVP_des_ede_cbc();
51         case 24:
52             return EVP_des_ede3_cbc();
53         default:
54             *error = KM_ERROR_UNSUPPORTED_KEY_SIZE;
55             break;
56         }
57         break;
58 
59     default:
60         *error = KM_ERROR_UNSUPPORTED_BLOCK_MODE;
61         break;
62     }
63 
64     assert(*error != KM_ERROR_OK);
65     return nullptr;
66 }
67 
68 static TripleDesEvpCipherDescription description;
GetCipherDescription() const69 const EvpCipherDescription& TripleDesOperationFactory::GetCipherDescription() const {
70     return description;
71 }
72 
73 }  // namespace keymaster
74