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 
DoCrypt(const uint8_t key[kAes256XtsKeySize],const uint8_t iv[kAesBlockSize],const uint8_t * src,uint8_t * dst,int nbytes,bool encrypt) const52 bool Aes256XtsCipher::DoCrypt(const uint8_t key[kAes256XtsKeySize],
53                               const uint8_t iv[kAesBlockSize],
54                               const uint8_t *src, uint8_t *dst, int nbytes,
55                               bool encrypt) const {
56   std::unique_ptr<EVP_CIPHER_CTX, void (*)(EVP_CIPHER_CTX *)> ctx(
57       EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free);
58   const char *op = encrypt ? "encryption" : "decryption";
59   int outl;
60 
61   if (ctx == nullptr) {
62     ADD_FAILURE() << "Failed to allocate BoringSSL cipher context";
63     return false;
64   }
65   if (nbytes % kAesBlockSize != 0) {
66     ADD_FAILURE() << "Bad input size";
67     return false;
68   }
69 
70   // For some reason, BoringSSL considers AES-256-XTS to be deprecated, and it's
71   // in a directory of deprecated algorithms that's not compiled on Android.
72   // AES-256-ECB is still available though, so just implement XTS manually...
73 
74   // Encrypt the IV.  This uses the second half of the AES-256-XTS key.
75   uint8_t tweak[kAesBlockSize];
76   if (EVP_EncryptInit_ex(ctx.get(), EVP_aes_256_ecb(), nullptr,
77                          key + kAes256KeySize, nullptr) != 1) {
78     ADD_FAILURE() << "Failed to initialize BoringSSL AES context";
79     return false;
80   }
81   if (EVP_EncryptUpdate(ctx.get(), tweak, &outl, iv, kAesBlockSize) != 1 ||
82       outl != kAesBlockSize) {
83     ADD_FAILURE() << "BoringSSL AES encryption of tweak failed";
84     return false;
85   }
86 
87   // Copy plaintext to output buffer, so that we can just transform it in-place.
88   memmove(dst, src, nbytes);
89 
90   // Mask the data pre-encryption/decryption.
91   DoXtsMasking(dst, nbytes / kAesBlockSize, tweak);
92 
93   // Encrypt or decrypt the data.
94   if (EVP_CipherInit_ex(ctx.get(), EVP_aes_256_ecb(), nullptr, key, nullptr,
95                         encrypt) != 1) {
96     ADD_FAILURE() << "Failed to reinitialize BoringSSL AES context";
97     return false;
98   }
99   EVP_CIPHER_CTX_set_padding(ctx.get(), 0);
100   if (EVP_CipherUpdate(ctx.get(), dst, &outl, dst, nbytes) != 1 ||
101       outl != nbytes) {
102     ADD_FAILURE() << "BoringSSL AES " << op << " of data failed";
103     return false;
104   }
105   // Mask the data post-encryption/decryption.
106   DoXtsMasking(dst, nbytes / kAesBlockSize, tweak);
107   return true;
108 }
109 
110 }  // namespace kernel
111 }  // namespace android
112