1 /*
2 * Copyright 2023 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 "crypto/crypto.h"
18
19 #include <openssl/aes.h>
20
21 #include <algorithm>
22
23 namespace rootcanal::crypto {
24
25 /* This function computes AES_128(key, message) */
aes_128(const Octet16 & key,const Octet16 & message)26 Octet16 aes_128(const Octet16& key, const Octet16& message) {
27 Octet16 key_reversed;
28 Octet16 message_reversed;
29 Octet16 output;
30
31 std::reverse_copy(key.begin(), key.end(), key_reversed.begin());
32 std::reverse_copy(message.begin(), message.end(), message_reversed.begin());
33
34 AES_KEY aes_key;
35 (void)AES_set_encrypt_key(key_reversed.data(), 128, &aes_key);
36 (void)AES_encrypt(message_reversed.data(), output.data(), &aes_key);
37
38 std::reverse(output.begin(), output.end());
39 return output;
40 }
41
42 } // namespace rootcanal::crypto
43