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; 45 void *ctx = NULL; 46 47 if (!out_key || !in_key || !message) 48 return TEE_ERROR_BAD_PARAMETERS; 49 50 res = crypto_mac_alloc_ctx(&ctx, TEE_FS_KM_HMAC_ALG); 51 if (res != TEE_SUCCESS) 52 return res; 53 54 res = crypto_mac_init(ctx, TEE_FS_KM_HMAC_ALG, in_key, in_key_size); 55 if (res != TEE_SUCCESS) 56 goto exit; 57 58 res = crypto_mac_update(ctx, TEE_FS_KM_HMAC_ALG, message, message_size); 59 if (res != TEE_SUCCESS) 60 goto exit; 61 62 res = crypto_mac_final(ctx, TEE_FS_KM_HMAC_ALG, out_key, out_key_size); 63 if (res != TEE_SUCCESS) 64 goto exit; 65 66 res = TEE_SUCCESS; 67 68 exit: 69 crypto_mac_free_ctx(ctx, TEE_FS_KM_HMAC_ALG); 70 return res; 71 } 72 73 TEE_Result tee_fs_fek_crypt(const TEE_UUID *uuid, TEE_OperationMode mode, 74 const uint8_t *in_key, size_t size, 75 uint8_t *out_key) 76 { 77 TEE_Result res; 78 void *ctx = NULL; 79 uint8_t tsk[TEE_FS_KM_TSK_SIZE]; 80 uint8_t dst_key[size]; 81 82 if (!in_key || !out_key) 83 return TEE_ERROR_BAD_PARAMETERS; 84 85 if (size != TEE_FS_KM_FEK_SIZE) 86 return TEE_ERROR_BAD_PARAMETERS; 87 88 if (tee_fs_ssk.is_init == 0) 89 return TEE_ERROR_GENERIC; 90 91 if (uuid) { 92 res = do_hmac(tsk, sizeof(tsk), tee_fs_ssk.key, 93 TEE_FS_KM_SSK_SIZE, uuid, sizeof(*uuid)); 94 if (res != TEE_SUCCESS) 95 return res; 96 } else { 97 /* 98 * Pick something of a different size than TEE_UUID to 99 * guarantee that there's never a conflict. 100 */ 101 uint8_t dummy[1] = { 0 }; 102 103 res = do_hmac(tsk, sizeof(tsk), tee_fs_ssk.key, 104 TEE_FS_KM_SSK_SIZE, dummy, sizeof(dummy)); 105 if (res != TEE_SUCCESS) 106 return res; 107 } 108 109 res = crypto_cipher_alloc_ctx(&ctx, TEE_FS_KM_ENC_FEK_ALG); 110 if (res != TEE_SUCCESS) 111 return res; 112 113 res = crypto_cipher_init(ctx, TEE_FS_KM_ENC_FEK_ALG, mode, tsk, 114 sizeof(tsk), NULL, 0, NULL, 0); 115 if (res != TEE_SUCCESS) 116 goto exit; 117 118 res = crypto_cipher_update(ctx, TEE_FS_KM_ENC_FEK_ALG, 119 mode, true, in_key, size, dst_key); 120 if (res != TEE_SUCCESS) 121 goto exit; 122 123 crypto_cipher_final(ctx, TEE_FS_KM_ENC_FEK_ALG); 124 125 memcpy(out_key, dst_key, sizeof(dst_key)); 126 127 exit: 128 crypto_cipher_free_ctx(ctx, TEE_FS_KM_ENC_FEK_ALG); 129 130 return res; 131 } 132 133 static TEE_Result generate_fek(uint8_t *key, uint8_t len) 134 { 135 return crypto_rng_read(key, len); 136 } 137 138 static TEE_Result tee_fs_init_key_manager(void) 139 { 140 int res = TEE_SUCCESS; 141 struct tee_hw_unique_key huk; 142 uint8_t chip_id[TEE_FS_KM_CHIP_ID_LENGTH]; 143 uint8_t message[sizeof(chip_id) + sizeof(string_for_ssk_gen)]; 144 145 /* Secure Storage Key Generation: 146 * 147 * SSK = HMAC(HUK, message) 148 * message := concatenate(chip_id, static string) 149 * */ 150 tee_otp_get_hw_unique_key(&huk); 151 memset(chip_id, 0, sizeof(chip_id)); 152 if (tee_otp_get_die_id(chip_id, sizeof(chip_id))) 153 return TEE_ERROR_BAD_STATE; 154 155 memcpy(message, chip_id, sizeof(chip_id)); 156 memcpy(message + sizeof(chip_id), string_for_ssk_gen, 157 sizeof(string_for_ssk_gen)); 158 159 res = do_hmac(tee_fs_ssk.key, sizeof(tee_fs_ssk.key), 160 huk.data, sizeof(huk.data), 161 message, sizeof(message)); 162 163 if (res == TEE_SUCCESS) 164 tee_fs_ssk.is_init = 1; 165 166 return res; 167 } 168 169 TEE_Result tee_fs_generate_fek(const TEE_UUID *uuid, void *buf, size_t buf_size) 170 { 171 TEE_Result res; 172 173 if (buf_size != TEE_FS_KM_FEK_SIZE) 174 return TEE_ERROR_BAD_PARAMETERS; 175 176 res = generate_fek(buf, TEE_FS_KM_FEK_SIZE); 177 if (res != TEE_SUCCESS) 178 return res; 179 180 return tee_fs_fek_crypt(uuid, TEE_MODE_ENCRYPT, buf, 181 TEE_FS_KM_FEK_SIZE, buf); 182 } 183 184 static TEE_Result sha256(uint8_t *out, size_t out_size, const uint8_t *in, 185 size_t in_size) 186 { 187 return tee_hash_createdigest(TEE_ALG_SHA256, in, in_size, 188 out, out_size); 189 } 190 191 static TEE_Result aes_ecb(uint8_t out[TEE_AES_BLOCK_SIZE], 192 const uint8_t in[TEE_AES_BLOCK_SIZE], 193 const uint8_t *key, size_t key_size) 194 { 195 TEE_Result res; 196 void *ctx = NULL; 197 const uint32_t algo = TEE_ALG_AES_ECB_NOPAD; 198 199 res = crypto_cipher_alloc_ctx(&ctx, algo); 200 if (res != TEE_SUCCESS) 201 return res; 202 203 res = crypto_cipher_init(ctx, algo, TEE_MODE_ENCRYPT, key, 204 key_size, NULL, 0, NULL, 0); 205 if (res != TEE_SUCCESS) 206 goto out; 207 208 res = crypto_cipher_update(ctx, algo, TEE_MODE_ENCRYPT, true, in, 209 TEE_AES_BLOCK_SIZE, out); 210 if (res != TEE_SUCCESS) 211 goto out; 212 213 crypto_cipher_final(ctx, algo); 214 res = TEE_SUCCESS; 215 216 out: 217 crypto_cipher_free_ctx(ctx, algo); 218 return res; 219 } 220 221 static TEE_Result essiv(uint8_t iv[TEE_AES_BLOCK_SIZE], 222 const uint8_t fek[TEE_FS_KM_FEK_SIZE], 223 uint16_t blk_idx) 224 { 225 TEE_Result res; 226 uint8_t sha[TEE_SHA256_HASH_SIZE]; 227 uint8_t pad_blkid[TEE_AES_BLOCK_SIZE] = { 0, }; 228 229 res = sha256(sha, sizeof(sha), fek, TEE_FS_KM_FEK_SIZE); 230 if (res != TEE_SUCCESS) 231 return res; 232 233 pad_blkid[0] = (blk_idx & 0xFF); 234 pad_blkid[1] = (blk_idx & 0xFF00) >> 8; 235 236 return aes_ecb(iv, pad_blkid, sha, 16); 237 } 238 239 /* 240 * Encryption/decryption of RPMB FS file data. This is AES CBC with ESSIV. 241 */ 242 TEE_Result tee_fs_crypt_block(const TEE_UUID *uuid, uint8_t *out, 243 const uint8_t *in, size_t size, 244 uint16_t blk_idx, const uint8_t *encrypted_fek, 245 TEE_OperationMode mode) 246 { 247 TEE_Result res; 248 uint8_t fek[TEE_FS_KM_FEK_SIZE]; 249 uint8_t iv[TEE_AES_BLOCK_SIZE]; 250 void *ctx; 251 const uint32_t algo = TEE_ALG_AES_CBC_NOPAD; 252 253 DMSG("%scrypt block #%u", (mode == TEE_MODE_ENCRYPT) ? "En" : "De", 254 blk_idx); 255 256 /* Decrypt FEK */ 257 res = tee_fs_fek_crypt(uuid, TEE_MODE_DECRYPT, encrypted_fek, 258 TEE_FS_KM_FEK_SIZE, fek); 259 if (res != TEE_SUCCESS) 260 return res; 261 262 /* Compute initialization vector for this block */ 263 res = essiv(iv, fek, blk_idx); 264 if (res != TEE_SUCCESS) 265 return res; 266 267 /* Run AES CBC */ 268 res = crypto_cipher_alloc_ctx(&ctx, algo); 269 if (res != TEE_SUCCESS) 270 return res; 271 272 res = crypto_cipher_init(ctx, algo, mode, fek, sizeof(fek), NULL, 273 0, iv, TEE_AES_BLOCK_SIZE); 274 if (res != TEE_SUCCESS) 275 goto exit; 276 res = crypto_cipher_update(ctx, algo, mode, true, in, size, out); 277 if (res != TEE_SUCCESS) 278 goto exit; 279 280 crypto_cipher_final(ctx, algo); 281 282 exit: 283 crypto_cipher_free_ctx(ctx, algo); 284 return res; 285 } 286 287 service_init_late(tee_fs_init_key_manager); 288