1 /*
2  * Copyright (C) 2020 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 "adb/pairing/aes_128_gcm.h"
18 
19 #include <android-base/endian.h>
20 #include <android-base/logging.h>
21 
22 #include <openssl/evp.h>
23 #include <openssl/hkdf.h>
24 #include <openssl/rand.h>
25 
26 #include <string.h>
27 
28 namespace adb {
29 namespace pairing {
30 
31 namespace {
32 // Size of AES-128-GCM key, in bytes
33 static constexpr size_t kHkdfKeyLength = 16;
34 
35 }  // namespace
36 
Aes128Gcm(const uint8_t * key_material,size_t key_material_len)37 Aes128Gcm::Aes128Gcm(const uint8_t* key_material, size_t key_material_len) {
38     CHECK(key_material);
39     CHECK_NE(key_material_len, 0ul);
40 
41     uint8_t key[kHkdfKeyLength];
42     uint8_t info[] = "adb pairing_auth aes-128-gcm key";
43     CHECK_EQ(HKDF(key, sizeof(key), EVP_sha256(), key_material, key_material_len, nullptr, 0, info,
44                   sizeof(info) - 1),
45              1);
46     CHECK(EVP_AEAD_CTX_init(context_.get(), EVP_aead_aes_128_gcm(), key, sizeof(key),
47                             EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr));
48 }
49 
Encrypt(const uint8_t * in,size_t in_len,uint8_t * out,size_t out_len)50 std::optional<size_t> Aes128Gcm::Encrypt(const uint8_t* in, size_t in_len, uint8_t* out,
51                                          size_t out_len) {
52     std::vector<uint8_t> nonce(EVP_AEAD_nonce_length(EVP_AEAD_CTX_aead(context_.get())), 0);
53     memcpy(nonce.data(), &enc_sequence_, sizeof(enc_sequence_));
54     size_t written_sz;
55     if (!EVP_AEAD_CTX_seal(context_.get(), out, &written_sz, out_len, nonce.data(), nonce.size(),
56                            in, in_len, nullptr, 0)) {
57         LOG(ERROR) << "Failed to encrypt (in_len=" << in_len << ", out_len=" << out_len
58                    << ", out_len_needed=" << EncryptedSize(in_len) << ")";
59         return std::nullopt;
60     }
61 
62     ++enc_sequence_;
63     return written_sz;
64 }
65 
Decrypt(const uint8_t * in,size_t in_len,uint8_t * out,size_t out_len)66 std::optional<size_t> Aes128Gcm::Decrypt(const uint8_t* in, size_t in_len, uint8_t* out,
67                                          size_t out_len) {
68     std::vector<uint8_t> nonce(EVP_AEAD_nonce_length(EVP_AEAD_CTX_aead(context_.get())), 0);
69     memcpy(nonce.data(), &dec_sequence_, sizeof(dec_sequence_));
70     size_t written_sz;
71     if (!EVP_AEAD_CTX_open(context_.get(), out, &written_sz, out_len, nonce.data(), nonce.size(),
72                            in, in_len, nullptr, 0)) {
73         LOG(ERROR) << "Failed to decrypt (in_len=" << in_len << ", out_len=" << out_len
74                    << ", out_len_needed=" << DecryptedSize(in_len) << ")";
75         return std::nullopt;
76     }
77 
78     ++dec_sequence_;
79     return written_sz;
80 }
81 
EncryptedSize(size_t size)82 size_t Aes128Gcm::EncryptedSize(size_t size) {
83     // https://commondatastorage.googleapis.com/chromium-boringssl-docs/aead.h.html#EVP_AEAD_CTX_seal
84     return size + EVP_AEAD_max_overhead(EVP_AEAD_CTX_aead(context_.get()));
85 }
86 
DecryptedSize(size_t size)87 size_t Aes128Gcm::DecryptedSize(size_t size) {
88     // https://commondatastorage.googleapis.com/chromium-boringssl-docs/aead.h.html#EVP_AEAD_CTX_open
89     return size;
90 }
91 
92 }  // namespace pairing
93 }  // namespace adb
94