1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2015, Linaro Limited 4 */ 5 6 7 /* 8 * Acronyms: 9 * 10 * FEK - File Encryption Key 11 * SSK - Secure Storage Key 12 * TSK - Trusted app Storage Key 13 * IV - Initial vector 14 * HUK - Hardware Unique Key 15 * RNG - Random Number Generator 16 */ 17 18 #include <compiler.h> 19 #include <crypto/crypto.h> 20 #include <initcall.h> 21 #include <kernel/panic.h> 22 #include <kernel/tee_common_otp.h> 23 #include <kernel/tee_ta_manager.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <tee/tee_cryp_utl.h> 27 #include <tee/tee_fs_key_manager.h> 28 #include <trace.h> 29 #include <util.h> 30 31 struct tee_fs_ssk { 32 bool is_init; 33 uint8_t key[TEE_FS_KM_SSK_SIZE]; 34 }; 35 36 static struct tee_fs_ssk tee_fs_ssk; 37 static uint8_t string_for_ssk_gen[] = "ONLY_FOR_tee_fs_ssk"; 38 39 40 static TEE_Result do_hmac(void *out_key, size_t out_key_size, 41 const void *in_key, size_t in_key_size, 42 const void *message, size_t message_size) 43 { 44 TEE_Result res = TEE_ERROR_GENERIC; 45 uint8_t *ctx = NULL; 46 size_t hash_ctx_size = 0; 47 48 if (!out_key || !in_key || !message) 49 return TEE_ERROR_BAD_PARAMETERS; 50 51 res = crypto_mac_get_ctx_size(TEE_FS_KM_HMAC_ALG, &hash_ctx_size); 52 if (res != TEE_SUCCESS) 53 return res; 54 55 ctx = malloc(hash_ctx_size); 56 if (!ctx) 57 return TEE_ERROR_OUT_OF_MEMORY; 58 59 res = crypto_mac_init(ctx, TEE_FS_KM_HMAC_ALG, in_key, in_key_size); 60 if (res != TEE_SUCCESS) 61 goto exit; 62 63 res = crypto_mac_update(ctx, TEE_FS_KM_HMAC_ALG, message, message_size); 64 if (res != TEE_SUCCESS) 65 goto exit; 66 67 res = crypto_mac_final(ctx, TEE_FS_KM_HMAC_ALG, out_key, out_key_size); 68 if (res != TEE_SUCCESS) 69 goto exit; 70 71 res = TEE_SUCCESS; 72 73 exit: 74 free(ctx); 75 return res; 76 } 77 78 TEE_Result tee_fs_fek_crypt(const TEE_UUID *uuid, TEE_OperationMode mode, 79 const uint8_t *in_key, size_t size, 80 uint8_t *out_key) 81 { 82 TEE_Result res; 83 uint8_t *ctx = NULL; 84 size_t ctx_size; 85 uint8_t tsk[TEE_FS_KM_TSK_SIZE]; 86 uint8_t dst_key[size]; 87 88 if (!in_key || !out_key) 89 return TEE_ERROR_BAD_PARAMETERS; 90 91 if (size != TEE_FS_KM_FEK_SIZE) 92 return TEE_ERROR_BAD_PARAMETERS; 93 94 if (tee_fs_ssk.is_init == 0) 95 return TEE_ERROR_GENERIC; 96 97 if (uuid) { 98 res = do_hmac(tsk, sizeof(tsk), tee_fs_ssk.key, 99 TEE_FS_KM_SSK_SIZE, uuid, sizeof(*uuid)); 100 if (res != TEE_SUCCESS) 101 return res; 102 } else { 103 /* 104 * Pick something of a different size than TEE_UUID to 105 * guarantee that there's never a conflict. 106 */ 107 uint8_t dummy[1] = { 0 }; 108 109 res = do_hmac(tsk, sizeof(tsk), tee_fs_ssk.key, 110 TEE_FS_KM_SSK_SIZE, dummy, sizeof(dummy)); 111 if (res != TEE_SUCCESS) 112 return res; 113 } 114 115 res = crypto_cipher_get_ctx_size(TEE_FS_KM_ENC_FEK_ALG, &ctx_size); 116 if (res != TEE_SUCCESS) 117 return res; 118 119 ctx = malloc(ctx_size); 120 if (!ctx) 121 return TEE_ERROR_OUT_OF_MEMORY; 122 123 res = crypto_cipher_init(ctx, TEE_FS_KM_ENC_FEK_ALG, mode, tsk, 124 sizeof(tsk), NULL, 0, NULL, 0); 125 if (res != TEE_SUCCESS) 126 goto exit; 127 128 res = crypto_cipher_update(ctx, TEE_FS_KM_ENC_FEK_ALG, 129 mode, true, in_key, size, dst_key); 130 if (res != TEE_SUCCESS) 131 goto exit; 132 133 crypto_cipher_final(ctx, TEE_FS_KM_ENC_FEK_ALG); 134 135 memcpy(out_key, dst_key, sizeof(dst_key)); 136 137 exit: 138 free(ctx); 139 140 return res; 141 } 142 143 static TEE_Result generate_fek(uint8_t *key, uint8_t len) 144 { 145 return crypto_rng_read(key, len); 146 } 147 148 static TEE_Result tee_fs_init_key_manager(void) 149 { 150 int res = TEE_SUCCESS; 151 struct tee_hw_unique_key huk; 152 uint8_t chip_id[TEE_FS_KM_CHIP_ID_LENGTH]; 153 uint8_t message[sizeof(chip_id) + sizeof(string_for_ssk_gen)]; 154 155 /* Secure Storage Key Generation: 156 * 157 * SSK = HMAC(HUK, message) 158 * message := concatenate(chip_id, static string) 159 * */ 160 tee_otp_get_hw_unique_key(&huk); 161 tee_otp_get_die_id(chip_id, sizeof(chip_id)); 162 163 memcpy(message, chip_id, sizeof(chip_id)); 164 memcpy(message + sizeof(chip_id), string_for_ssk_gen, 165 sizeof(string_for_ssk_gen)); 166 167 res = do_hmac(tee_fs_ssk.key, sizeof(tee_fs_ssk.key), 168 huk.data, sizeof(huk.data), 169 message, sizeof(message)); 170 171 if (res == TEE_SUCCESS) 172 tee_fs_ssk.is_init = 1; 173 174 return res; 175 } 176 177 TEE_Result tee_fs_generate_fek(const TEE_UUID *uuid, void *buf, size_t buf_size) 178 { 179 TEE_Result res; 180 181 if (buf_size != TEE_FS_KM_FEK_SIZE) 182 return TEE_ERROR_BAD_PARAMETERS; 183 184 res = generate_fek(buf, TEE_FS_KM_FEK_SIZE); 185 if (res != TEE_SUCCESS) 186 return res; 187 188 return tee_fs_fek_crypt(uuid, TEE_MODE_ENCRYPT, buf, 189 TEE_FS_KM_FEK_SIZE, buf); 190 } 191 192 static TEE_Result sha256(uint8_t *out, size_t out_size, const uint8_t *in, 193 size_t in_size) 194 { 195 return tee_hash_createdigest(TEE_ALG_SHA256, in, in_size, 196 out, out_size); 197 } 198 199 static TEE_Result aes_ecb(uint8_t out[TEE_AES_BLOCK_SIZE], 200 const uint8_t in[TEE_AES_BLOCK_SIZE], 201 const uint8_t *key, size_t key_size) 202 { 203 TEE_Result res; 204 uint8_t *ctx = NULL; 205 size_t ctx_size; 206 uint32_t algo = TEE_ALG_AES_ECB_NOPAD; 207 208 res = crypto_cipher_get_ctx_size(algo, &ctx_size); 209 if (res != TEE_SUCCESS) 210 return res; 211 212 ctx = malloc(ctx_size); 213 if (!ctx) 214 return TEE_ERROR_OUT_OF_MEMORY; 215 216 res = crypto_cipher_init(ctx, algo, TEE_MODE_ENCRYPT, key, 217 key_size, NULL, 0, NULL, 0); 218 if (res != TEE_SUCCESS) 219 goto out; 220 221 res = crypto_cipher_update(ctx, algo, TEE_MODE_ENCRYPT, true, in, 222 TEE_AES_BLOCK_SIZE, out); 223 if (res != TEE_SUCCESS) 224 goto out; 225 226 crypto_cipher_final(ctx, algo); 227 res = TEE_SUCCESS; 228 229 out: 230 free(ctx); 231 return res; 232 } 233 234 static TEE_Result essiv(uint8_t iv[TEE_AES_BLOCK_SIZE], 235 const uint8_t fek[TEE_FS_KM_FEK_SIZE], 236 uint16_t blk_idx) 237 { 238 TEE_Result res; 239 uint8_t sha[TEE_SHA256_HASH_SIZE]; 240 uint8_t pad_blkid[TEE_AES_BLOCK_SIZE] = { 0, }; 241 242 res = sha256(sha, sizeof(sha), fek, TEE_FS_KM_FEK_SIZE); 243 if (res != TEE_SUCCESS) 244 return res; 245 246 pad_blkid[0] = (blk_idx & 0xFF); 247 pad_blkid[1] = (blk_idx & 0xFF00) >> 8; 248 249 return aes_ecb(iv, pad_blkid, sha, 16); 250 } 251 252 /* 253 * Encryption/decryption of RPMB FS file data. This is AES CBC with ESSIV. 254 */ 255 TEE_Result tee_fs_crypt_block(const TEE_UUID *uuid, uint8_t *out, 256 const uint8_t *in, size_t size, 257 uint16_t blk_idx, const uint8_t *encrypted_fek, 258 TEE_OperationMode mode) 259 { 260 TEE_Result res; 261 uint8_t fek[TEE_FS_KM_FEK_SIZE]; 262 uint8_t iv[TEE_AES_BLOCK_SIZE]; 263 uint8_t *ctx; 264 size_t ctx_size; 265 uint32_t algo = TEE_ALG_AES_CBC_NOPAD; 266 267 DMSG("%scrypt block #%u", (mode == TEE_MODE_ENCRYPT) ? "En" : "De", 268 blk_idx); 269 270 /* Decrypt FEK */ 271 res = tee_fs_fek_crypt(uuid, TEE_MODE_DECRYPT, encrypted_fek, 272 TEE_FS_KM_FEK_SIZE, fek); 273 if (res != TEE_SUCCESS) 274 return res; 275 276 /* Compute initialization vector for this block */ 277 res = essiv(iv, fek, blk_idx); 278 279 /* Run AES CBC */ 280 res = crypto_cipher_get_ctx_size(algo, &ctx_size); 281 if (res != TEE_SUCCESS) 282 return res; 283 ctx = malloc(ctx_size); 284 if (!ctx) 285 return TEE_ERROR_OUT_OF_MEMORY; 286 287 res = crypto_cipher_init(ctx, algo, mode, fek, sizeof(fek), NULL, 288 0, iv, TEE_AES_BLOCK_SIZE); 289 if (res != TEE_SUCCESS) 290 goto exit; 291 res = crypto_cipher_update(ctx, algo, mode, true, in, size, out); 292 if (res != TEE_SUCCESS) 293 goto exit; 294 295 crypto_cipher_final(ctx, algo); 296 297 exit: 298 free(ctx); 299 return res; 300 } 301 302 service_init_late(tee_fs_init_key_manager); 303