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 <gtest/gtest.h>
18 #include <openssl/cipher.h>
19 #include <openssl/evp.h>
20 #include <string.h>
21 
22 #include "vts_kernel_encryption.h"
23 
24 namespace android {
25 namespace kernel {
26 
DoXtsMasking(uint8_t * data,int num_blocks,const uint8_t tweak[kAesBlockSize])27 static void DoXtsMasking(uint8_t *data, int num_blocks,
28                          const uint8_t tweak[kAesBlockSize]) {
29   uint8_t mask[kAesBlockSize];
30 
31   memcpy(mask, tweak, kAesBlockSize);
32 
33   for (int i = 0; i < num_blocks; i++) {
34     // XOR the next block with the current mask.
35     for (int j = 0; j < kAesBlockSize; j++) {
36       data[i * kAesBlockSize + j] ^= mask[j];
37     }
38     // Multipy the mask by 'x' in GF(2^128).
39     int carry = 0;
40     for (int j = 0; j < kAesBlockSize; j++) {
41       int next_carry = mask[j] >> 7;
42 
43       mask[j] = (mask[j] << 1) ^ carry;
44       carry = next_carry;
45     }
46     if (carry != 0) {
47       mask[0] ^= 0x87;
48     }
49   }
50 }
51 
DoEncrypt(const uint8_t key[kAes256XtsKeySize],const uint8_t iv[kAesBlockSize],const uint8_t * src,uint8_t * dst,int nbytes) const52 bool Aes256XtsCipher::DoEncrypt(const uint8_t key[kAes256XtsKeySize],
53                                 const uint8_t iv[kAesBlockSize],
54                                 const uint8_t *src, uint8_t *dst,
55                                 int nbytes) const {
56   std::unique_ptr<EVP_CIPHER_CTX, void (*)(EVP_CIPHER_CTX *)> ctx(
57       EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free);
58   int outl;
59 
60   if (ctx == nullptr) {
61     ADD_FAILURE() << "Failed to allocate BoringSSL cipher context";
62     return false;
63   }
64   if (nbytes % kAesBlockSize != 0) {
65     ADD_FAILURE() << "Bad input size";
66     return false;
67   }
68 
69   // For some reason, BoringSSL considers AES-256-XTS to be deprecated, and it's
70   // in a directory of deprecated algorithms that's not compiled on Android.
71   // AES-256-ECB is still available though, so just implement XTS manually...
72 
73   // Encrypt the IV.  This uses the second half of the AES-256-XTS key.
74   uint8_t tweak[kAesBlockSize];
75   if (EVP_EncryptInit_ex(ctx.get(), EVP_aes_256_ecb(), nullptr,
76                          key + kAes256KeySize, nullptr) != 1) {
77     ADD_FAILURE() << "Failed to initialize BoringSSL AES context";
78     return false;
79   }
80   if (EVP_EncryptUpdate(ctx.get(), tweak, &outl, iv, kAesBlockSize) != 1 ||
81       outl != kAesBlockSize) {
82     ADD_FAILURE() << "BoringSSL AES encryption failed (tweak)";
83     return false;
84   }
85 
86   // Copy plaintext to output buffer, so that we can just transform it in-place.
87   memmove(dst, src, nbytes);
88 
89   // Mask the data pre-encryption.
90   DoXtsMasking(dst, nbytes / kAesBlockSize, tweak);
91 
92   // Encrypt the data.
93   if (EVP_EncryptInit_ex(ctx.get(), EVP_aes_256_ecb(), nullptr, key, nullptr) !=
94       1) {
95     ADD_FAILURE() << "Failed to reinitialize BoringSSL AES context";
96     return false;
97   }
98   if (EVP_EncryptUpdate(ctx.get(), dst, &outl, dst, nbytes) != 1 ||
99       outl != nbytes) {
100     ADD_FAILURE() << "BoringSSL AES encryption failed (data)";
101     return false;
102   }
103   // Mask the data post-encryption.
104   DoXtsMasking(dst, nbytes / kAesBlockSize, tweak);
105   return true;
106 }
107 
108 }  // namespace kernel
109 }  // namespace android
110