blob: 15079a1ca24fe813fd0aa982e13e7adde08380ed [file] [log] [blame]
Joel Galensonca0efb12020-10-01 14:32:30 -07001/*
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#define LOG_TAG "keystore2"
18
19#include "crypto.hpp"
20
Ryan Prichard9cf7f662022-08-30 14:28:42 -070021#include <assert.h>
Joel Galensonca0efb12020-10-01 14:32:30 -070022#include <log/log.h>
23#include <openssl/aes.h>
Joel Galenson05914582021-01-08 09:30:41 -080024#include <openssl/ec.h>
25#include <openssl/ec_key.h>
26#include <openssl/ecdh.h>
Joel Galensonca0efb12020-10-01 14:32:30 -070027#include <openssl/evp.h>
Joel Galenson05914582021-01-08 09:30:41 -080028#include <openssl/hkdf.h>
David Drysdalec97eb9e2022-01-26 13:03:48 -080029#include <openssl/hmac.h>
Janis Danisevskis9d90b812020-11-25 21:02:11 -080030#include <openssl/rand.h>
Shawn Willden8fde4c22021-02-14 13:58:22 -070031#include <openssl/x509.h>
Joel Galensonca0efb12020-10-01 14:32:30 -070032
33#include <vector>
34
35// Copied from system/security/keystore/blob.h.
36
37constexpr size_t kGcmTagLength = 128 / 8;
38constexpr size_t kAes128KeySizeBytes = 128 / 8;
39
40// Copied from system/security/keystore/blob.cpp.
41
42#if defined(__clang__)
43#define OPTNONE __attribute__((optnone))
44#elif defined(__GNUC__)
45#define OPTNONE __attribute__((optimize("O0")))
46#else
47#error Need a definition for OPTNONE
48#endif
49
50class ArrayEraser {
51 public:
52 ArrayEraser(uint8_t* arr, size_t size) : mArr(arr), mSize(size) {}
53 OPTNONE ~ArrayEraser() { std::fill(mArr, mArr + mSize, 0); }
54
55 private:
56 volatile uint8_t* mArr;
57 size_t mSize;
58};
59
60/**
61 * Returns a EVP_CIPHER appropriate for the given key size.
62 */
63const EVP_CIPHER* getAesCipherForKey(size_t key_size) {
64 const EVP_CIPHER* cipher = EVP_aes_256_gcm();
65 if (key_size == kAes128KeySizeBytes) {
66 cipher = EVP_aes_128_gcm();
67 }
68 return cipher;
69}
70
David Drysdalec97eb9e2022-01-26 13:03:48 -080071bool hmacSha256(const uint8_t* key, size_t key_size, const uint8_t* msg, size_t msg_size,
72 uint8_t* out, size_t out_size) {
73 const EVP_MD* digest = EVP_sha256();
74 unsigned int actual_out_size = out_size;
75 uint8_t* p = HMAC(digest, key, key_size, msg, msg_size, out, &actual_out_size);
76 return (p != nullptr);
77}
78
Janis Danisevskis9d90b812020-11-25 21:02:11 -080079bool randomBytes(uint8_t* out, size_t len) {
80 return RAND_bytes(out, len);
81}
82
Joel Galensonca0efb12020-10-01 14:32:30 -070083/*
84 * Encrypt 'len' data at 'in' with AES-GCM, using 128-bit or 256-bit key at 'key', 96-bit IV at
85 * 'iv' and write output to 'out' (which may be the same location as 'in') and 128-bit tag to
86 * 'tag'.
87 */
88bool AES_gcm_encrypt(const uint8_t* in, uint8_t* out, size_t len, const uint8_t* key,
89 size_t key_size, const uint8_t* iv, uint8_t* tag) {
90
91 // There can be 128-bit and 256-bit keys
92 const EVP_CIPHER* cipher = getAesCipherForKey(key_size);
93
94 bssl::UniquePtr<EVP_CIPHER_CTX> ctx(EVP_CIPHER_CTX_new());
95
96 EVP_EncryptInit_ex(ctx.get(), cipher, nullptr /* engine */, key, iv);
97 EVP_CIPHER_CTX_set_padding(ctx.get(), 0 /* no padding needed with GCM */);
98
99 std::vector<uint8_t> out_tmp(len);
100 uint8_t* out_pos = out_tmp.data();
101 int out_len;
102
103 EVP_EncryptUpdate(ctx.get(), out_pos, &out_len, in, len);
104 out_pos += out_len;
105 EVP_EncryptFinal_ex(ctx.get(), out_pos, &out_len);
106 out_pos += out_len;
107 if (out_pos - out_tmp.data() != static_cast<ssize_t>(len)) {
108 ALOGD("Encrypted ciphertext is the wrong size, expected %zu, got %zd", len,
109 out_pos - out_tmp.data());
110 return false;
111 }
112
113 std::copy(out_tmp.data(), out_pos, out);
114 EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_GET_TAG, kGcmTagLength, tag);
115
116 return true;
117}
118
119/*
120 * Decrypt 'len' data at 'in' with AES-GCM, using 128-bit or 256-bit key at 'key', 96-bit IV at
121 * 'iv', checking 128-bit tag at 'tag' and writing plaintext to 'out'(which may be the same
122 * location as 'in').
123 */
124bool AES_gcm_decrypt(const uint8_t* in, uint8_t* out, size_t len, const uint8_t* key,
125 size_t key_size, const uint8_t* iv, const uint8_t* tag) {
126
127 // There can be 128-bit and 256-bit keys
128 const EVP_CIPHER* cipher = getAesCipherForKey(key_size);
129
130 bssl::UniquePtr<EVP_CIPHER_CTX> ctx(EVP_CIPHER_CTX_new());
131
132 EVP_DecryptInit_ex(ctx.get(), cipher, nullptr /* engine */, key, iv);
133 EVP_CIPHER_CTX_set_padding(ctx.get(), 0 /* no padding needed with GCM */);
134 EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_TAG, kGcmTagLength, const_cast<uint8_t*>(tag));
135
136 std::vector<uint8_t> out_tmp(len);
137 ArrayEraser out_eraser(out_tmp.data(), len);
138 uint8_t* out_pos = out_tmp.data();
139 int out_len;
140
141 EVP_DecryptUpdate(ctx.get(), out_pos, &out_len, in, len);
142 out_pos += out_len;
143 if (!EVP_DecryptFinal_ex(ctx.get(), out_pos, &out_len)) {
144 ALOGE("Failed to decrypt blob; ciphertext or tag is likely corrupted");
145 return false;
146 }
147 out_pos += out_len;
148 if (out_pos - out_tmp.data() != static_cast<ssize_t>(len)) {
149 ALOGE("Encrypted plaintext is the wrong size, expected %zu, got %zd", len,
150 out_pos - out_tmp.data());
151 return false;
152 }
153
154 std::copy(out_tmp.data(), out_pos, out);
155
156 return true;
157}
158
159// Copied from system/security/keystore/keymaster_enforcement.cpp.
160
161class EvpMdCtx {
162 public:
163 EvpMdCtx() { EVP_MD_CTX_init(&ctx_); }
164 ~EvpMdCtx() { EVP_MD_CTX_cleanup(&ctx_); }
165
166 EVP_MD_CTX* get() { return &ctx_; }
167
168 private:
169 EVP_MD_CTX ctx_;
170};
171
172bool CreateKeyId(const uint8_t* key_blob, size_t len, km_id_t* out_id) {
173 EvpMdCtx ctx;
174
175 uint8_t hash[EVP_MAX_MD_SIZE];
176 unsigned int hash_len;
177 if (EVP_DigestInit_ex(ctx.get(), EVP_sha256(), nullptr /* ENGINE */) &&
178 EVP_DigestUpdate(ctx.get(), key_blob, len) &&
179 EVP_DigestFinal_ex(ctx.get(), hash, &hash_len)) {
180 assert(hash_len >= sizeof(*out_id));
181 memcpy(out_id, hash, sizeof(*out_id));
182 return true;
183 }
184
185 return false;
186}
187
188// Copied from system/security/keystore/user_state.h
189
190static constexpr size_t SALT_SIZE = 16;
191
192// Copied from system/security/keystore/user_state.cpp.
193
Eric Biggersd68e6912024-01-17 03:54:11 +0000194void PBKDF2(uint8_t* key, size_t key_len, const char* pw, size_t pw_len, const uint8_t* salt) {
Joel Galensonca0efb12020-10-01 14:32:30 -0700195 const EVP_MD* digest = EVP_sha256();
196
197 // SHA1 was used prior to increasing the key size
198 if (key_len == kAes128KeySizeBytes) {
199 digest = EVP_sha1();
200 }
201
David Drysdale6a0ec2c2022-04-19 08:11:18 +0100202 PKCS5_PBKDF2_HMAC(pw, pw_len, salt, SALT_SIZE, 8192, digest, key_len, key);
Joel Galensonca0efb12020-10-01 14:32:30 -0700203}
Joel Galenson05914582021-01-08 09:30:41 -0800204
205// New code.
206
207bool HKDFExtract(uint8_t* out_key, size_t* out_len, const uint8_t* secret, size_t secret_len,
208 const uint8_t* salt, size_t salt_len) {
209 const EVP_MD* digest = EVP_sha256();
210 auto result = HKDF_extract(out_key, out_len, digest, secret, secret_len, salt, salt_len);
211 return result == 1;
212}
213
214bool HKDFExpand(uint8_t* out_key, size_t out_len, const uint8_t* prk, size_t prk_len,
215 const uint8_t* info, size_t info_len) {
216 const EVP_MD* digest = EVP_sha256();
217 auto result = HKDF_expand(out_key, out_len, digest, prk, prk_len, info, info_len);
218 return result == 1;
219}
220
221int ECDHComputeKey(void* out, const EC_POINT* pub_key, const EC_KEY* priv_key) {
222 return ECDH_compute_key(out, EC_MAX_BYTES, pub_key, priv_key, nullptr);
223}
224
225EC_KEY* ECKEYGenerateKey() {
226 EC_KEY* key = EC_KEY_new();
Paul Crowley52f017f2021-06-22 08:16:01 -0700227 EC_GROUP* group = EC_GROUP_new_by_curve_name(NID_secp521r1);
Joel Galenson05914582021-01-08 09:30:41 -0800228 EC_KEY_set_group(key, group);
229 auto result = EC_KEY_generate_key(key);
230 if (result == 0) {
231 EC_GROUP_free(group);
232 EC_KEY_free(key);
233 return nullptr;
234 }
235 return key;
236}
237
Paul Crowley7bb5edd2021-03-20 20:26:43 -0700238size_t ECKEYMarshalPrivateKey(const EC_KEY* priv_key, uint8_t* buf, size_t len) {
239 CBB cbb;
240 size_t out_len;
241 if (!CBB_init_fixed(&cbb, buf, len) ||
242 !EC_KEY_marshal_private_key(&cbb, priv_key, EC_PKEY_NO_PARAMETERS | EC_PKEY_NO_PUBKEY) ||
243 !CBB_finish(&cbb, nullptr, &out_len)) {
244 return 0;
245 } else {
246 return out_len;
247 }
248}
249
250EC_KEY* ECKEYParsePrivateKey(const uint8_t* buf, size_t len) {
251 CBS cbs;
252 CBS_init(&cbs, buf, len);
Paul Crowley52f017f2021-06-22 08:16:01 -0700253 EC_GROUP* group = EC_GROUP_new_by_curve_name(NID_secp521r1);
Paul Crowley7bb5edd2021-03-20 20:26:43 -0700254 auto result = EC_KEY_parse_private_key(&cbs, group);
Joel Galenson05914582021-01-08 09:30:41 -0800255 EC_GROUP_free(group);
Paul Crowley7bb5edd2021-03-20 20:26:43 -0700256 if (result != nullptr && CBS_len(&cbs) != 0) {
257 EC_KEY_free(result);
258 return nullptr;
259 }
Joel Galenson05914582021-01-08 09:30:41 -0800260 return result;
261}
262
263size_t ECPOINTPoint2Oct(const EC_POINT* point, uint8_t* buf, size_t len) {
Paul Crowley52f017f2021-06-22 08:16:01 -0700264 EC_GROUP* group = EC_GROUP_new_by_curve_name(NID_secp521r1);
Joel Galenson05914582021-01-08 09:30:41 -0800265 point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
266 auto result = EC_POINT_point2oct(group, point, form, buf, len, nullptr);
267 EC_GROUP_free(group);
268 return result;
269}
270
271EC_POINT* ECPOINTOct2Point(const uint8_t* buf, size_t len) {
Paul Crowley52f017f2021-06-22 08:16:01 -0700272 EC_GROUP* group = EC_GROUP_new_by_curve_name(NID_secp521r1);
Joel Galenson05914582021-01-08 09:30:41 -0800273 EC_POINT* point = EC_POINT_new(group);
274 auto result = EC_POINT_oct2point(group, point, buf, len, nullptr);
275 EC_GROUP_free(group);
276 if (result == 0) {
277 EC_POINT_free(point);
278 return nullptr;
279 }
280 return point;
281}
Shawn Willden8fde4c22021-02-14 13:58:22 -0700282
283int extractSubjectFromCertificate(const uint8_t* cert_buf, size_t cert_len, uint8_t* subject_buf,
284 size_t subject_buf_len) {
285 if (!cert_buf || !subject_buf) {
286 ALOGE("extractSubjectFromCertificate: received null pointer");
287 return 0;
288 }
289
290 const uint8_t* p = cert_buf;
291 bssl::UniquePtr<X509> cert(d2i_X509(nullptr /* Allocate X509 struct */, &p, cert_len));
292 if (!cert) {
293 ALOGE("extractSubjectFromCertificate: failed to parse certificate");
294 return 0;
295 }
296
297 X509_NAME* subject = X509_get_subject_name(cert.get());
298 if (!subject) {
299 ALOGE("extractSubjectFromCertificate: failed to retrieve subject name");
300 return 0;
301 }
302
303 int subject_len = i2d_X509_NAME(subject, nullptr /* Don't copy the data */);
304 if (subject_len < 0) {
305 ALOGE("extractSubjectFromCertificate: error obtaining encoded subject name length");
306 return 0;
307 }
308
309 if (subject_len > subject_buf_len) {
310 // Return the subject length, negated, so the caller knows how much
311 // buffer space is required.
312 ALOGI("extractSubjectFromCertificate: needed %d bytes for subject, caller provided %zu",
313 subject_len, subject_buf_len);
314 return -subject_len;
315 }
316
317 // subject_buf has enough space.
318 uint8_t* tmp = subject_buf;
319 return i2d_X509_NAME(subject, &tmp);
320}