2018-09-07 16:00:13 +01:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2018-09-07 16:00:13 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
#include <stdint.h>
|
2018-02-22 23:32:47 +00:00
|
|
|
#include <string.h>
|
2018-02-20 17:39:50 +00:00
|
|
|
|
2018-03-25 22:05:08 +01:00
|
|
|
#include "arm.h"
|
2018-02-20 17:39:50 +00:00
|
|
|
#include "utils.h"
|
2018-02-25 09:21:52 +00:00
|
|
|
#include "fuse.h"
|
2018-02-20 17:39:50 +00:00
|
|
|
#include "gcm.h"
|
|
|
|
|
|
|
|
#include "sealedkeys.h"
|
|
|
|
#include "se.h"
|
|
|
|
|
|
|
|
/* Shifts right a little endian 128-bit value. */
|
2018-02-20 18:29:44 +00:00
|
|
|
static void shr_128(uint64_t *val) {
|
2018-02-20 17:39:50 +00:00
|
|
|
val[0] >>= 1;
|
|
|
|
val[0] |= (val[1] & 1) << 63;
|
|
|
|
val[1] >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Shifts left a little endian 128-bit value. */
|
2018-02-20 18:29:44 +00:00
|
|
|
static void shl_128(uint64_t *val) {
|
2018-02-20 17:39:50 +00:00
|
|
|
val[1] <<= 1;
|
2018-02-26 21:09:35 +00:00
|
|
|
val[1] |= (val[0] & (1ull << 63)) >> 63;
|
2018-02-20 17:39:50 +00:00
|
|
|
val[0] <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Multiplies two 128-bit numbers X,Y in the GF(128) Galois Field. */
|
2018-02-20 18:29:44 +00:00
|
|
|
static void gf128_mul(uint8_t *dst, const uint8_t *x, const uint8_t *y) {
|
2018-02-20 17:39:50 +00:00
|
|
|
uint8_t x_work[0x10];
|
|
|
|
uint8_t y_work[0x10];
|
|
|
|
uint8_t dst_work[0x10];
|
2018-02-20 18:29:44 +00:00
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
uint64_t *p_x = (uint64_t *)(&x_work[0]);
|
|
|
|
uint64_t *p_y = (uint64_t *)(&y_work[0]);
|
|
|
|
uint64_t *p_dst = (uint64_t *)(&dst_work[0]);
|
2018-02-26 21:09:35 +00:00
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
/* Initialize buffers. */
|
|
|
|
for (unsigned int i = 0; i < 0x10; i++) {
|
|
|
|
x_work[i] = x[0xF-i];
|
|
|
|
y_work[i] = y[0xF-i];
|
|
|
|
dst_work[i] = 0;
|
|
|
|
}
|
2018-02-20 18:29:44 +00:00
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
/* Perform operation for each bit in y. */
|
|
|
|
for (unsigned int round = 0; round < 0x80; round++) {
|
|
|
|
p_dst[0] ^= p_x[0] * ((y_work[0xF] & 0x80) >> 7);
|
|
|
|
p_dst[1] ^= p_x[1] * ((y_work[0xF] & 0x80) >> 7);
|
|
|
|
shl_128(p_y);
|
2018-02-20 18:29:44 +00:00
|
|
|
uint8_t xval = 0xE1 * (x_work[0] & 1);
|
2018-02-20 17:39:50 +00:00
|
|
|
shr_128(p_x);
|
2018-02-20 18:29:44 +00:00
|
|
|
x_work[0xF] ^= xval;
|
2018-02-20 17:39:50 +00:00
|
|
|
}
|
2018-02-20 18:29:44 +00:00
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
for (unsigned int i = 0; i < 0x10; i++) {
|
|
|
|
dst[i] = dst_work[0xF-i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Performs an AES-GCM GHASH operation over the data into dst. */
|
2018-02-23 03:58:39 +00:00
|
|
|
static void ghash(void *dst, const void *data, size_t data_size, const void *j_block, bool encrypt) {
|
2018-03-09 01:26:42 +00:00
|
|
|
uint8_t x[0x10] = {0};
|
2018-02-20 17:39:50 +00:00
|
|
|
uint8_t h[0x10];
|
2018-02-20 18:29:44 +00:00
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
uint64_t *p_x = (uint64_t *)(&x[0]);
|
|
|
|
uint64_t *p_data = (uint64_t *)data;
|
2018-02-20 18:29:44 +00:00
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
/* H = aes_ecb_encrypt(zeroes) */
|
2018-02-23 00:17:12 +00:00
|
|
|
se_aes_128_ecb_encrypt_block(KEYSLOT_SWITCH_TEMPKEY, h, 0x10, x, 0x10);
|
2018-03-09 01:26:42 +00:00
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
size_t total_size = data_size;
|
2018-02-20 18:29:44 +00:00
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
while (data_size >= 0x10) {
|
|
|
|
/* X = (X ^ current_block) * H */
|
|
|
|
p_x[0] ^= p_data[0];
|
|
|
|
p_x[1] ^= p_data[1];
|
2018-02-20 18:29:44 +00:00
|
|
|
gf128_mul(x, x, h);
|
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
/* Increment p_data by 0x10 bytes. */
|
|
|
|
p_data += 2;
|
|
|
|
data_size -= 0x10;
|
|
|
|
}
|
2018-02-20 18:29:44 +00:00
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
/* Nintendo's code *discards all data in the last block* if unaligned. */
|
|
|
|
/* And treats that block as though it were all-zero. */
|
|
|
|
/* This is a bug, they just forget to XOR with the copy of the last block they save. */
|
|
|
|
if (data_size & 0xF) {
|
2018-02-20 18:29:44 +00:00
|
|
|
gf128_mul(x, x, h);
|
2018-02-20 17:39:50 +00:00
|
|
|
}
|
2018-03-09 01:26:42 +00:00
|
|
|
|
|
|
|
uint64_t xor_size = total_size << 3;
|
|
|
|
xor_size = __builtin_bswap64(xor_size);
|
2018-02-20 18:29:44 +00:00
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
/* Due to a Nintendo bug, the wrong QWORD gets XOR'd in the "final output block" case. */
|
|
|
|
if (encrypt) {
|
2018-03-09 01:26:42 +00:00
|
|
|
p_x[0] ^= xor_size;
|
2018-02-20 17:39:50 +00:00
|
|
|
} else {
|
2018-03-09 01:26:42 +00:00
|
|
|
p_x[1] ^= xor_size;
|
2018-02-20 17:39:50 +00:00
|
|
|
}
|
2018-03-09 01:26:42 +00:00
|
|
|
|
2018-02-20 18:29:44 +00:00
|
|
|
gf128_mul(x, x, h);
|
2018-03-09 01:26:42 +00:00
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
/* If final output block, XOR with encrypted J block. */
|
|
|
|
if (encrypt) {
|
2018-02-23 00:17:12 +00:00
|
|
|
se_aes_128_ecb_encrypt_block(KEYSLOT_SWITCH_TEMPKEY, h, 0x10, j_block, 0x10);
|
2018-02-20 17:39:50 +00:00
|
|
|
for (unsigned int i = 0; i < 0x10; i++) {
|
|
|
|
x[i] ^= h[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Copy output. */
|
2018-02-20 18:29:44 +00:00
|
|
|
memcpy(dst, x, 0x10);
|
2018-02-20 17:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This function is a doozy. It decrypts and validates a (non-standard) AES-GCM wrapped keypair. */
|
2018-03-25 22:05:08 +01:00
|
|
|
size_t gcm_decrypt_key(void *dst, size_t dst_size, const void *src, size_t src_size, const void *sealed_kek, size_t kek_size, const void *wrapped_key, size_t key_size, unsigned int usecase, bool is_personalized, uint8_t *out_deviceid_high) {
|
2018-02-20 17:39:50 +00:00
|
|
|
if (is_personalized == 0) {
|
|
|
|
/* Devkit keys use a different keyformat without a MAC/Device ID. */
|
|
|
|
if (src_size <= 0x10 || src_size - 0x10 > dst_size) {
|
2018-02-24 14:20:45 +00:00
|
|
|
generic_panic();
|
2018-02-20 17:39:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (src_size <= 0x30 || src_size - 0x20 > dst_size) {
|
2018-02-24 14:20:45 +00:00
|
|
|
generic_panic();
|
2018-02-20 17:39:50 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-09 01:26:42 +00:00
|
|
|
|
|
|
|
uint8_t intermediate_buf[0x400] = {0};
|
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
/* Unwrap the key */
|
|
|
|
unseal_key(KEYSLOT_SWITCH_TEMPKEY, sealed_kek, kek_size, usecase);
|
|
|
|
decrypt_data_into_keyslot(KEYSLOT_SWITCH_TEMPKEY, KEYSLOT_SWITCH_TEMPKEY, wrapped_key, key_size);
|
2018-03-09 01:26:42 +00:00
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
/* Decrypt the GCM keypair, AES-CTR with CTR = blob[:0x10]. */
|
2018-03-09 01:26:42 +00:00
|
|
|
se_aes_ctr_crypt(KEYSLOT_SWITCH_TEMPKEY, intermediate_buf, dst_size, src + 0x10, src_size - 0x10, src, 0x10);
|
|
|
|
|
2018-02-23 03:58:39 +00:00
|
|
|
if (!is_personalized) {
|
2018-02-20 17:39:50 +00:00
|
|
|
/* Devkit non-personalized keys have no further authentication. */
|
2018-03-09 01:26:42 +00:00
|
|
|
memcpy(dst, intermediate_buf, src_size - 0x10);
|
|
|
|
memset(intermediate_buf, 0, sizeof(intermediate_buf));
|
2018-02-20 17:39:50 +00:00
|
|
|
return src_size - 0x10;
|
2018-03-09 01:26:42 +00:00
|
|
|
}
|
2018-02-20 18:29:44 +00:00
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
/* J = GHASH(CTR); */
|
|
|
|
uint8_t j_block[0x10];
|
2018-02-23 03:58:39 +00:00
|
|
|
ghash(j_block, src, 0x10, NULL, false);
|
2018-02-20 18:29:44 +00:00
|
|
|
|
2018-02-20 17:41:16 +00:00
|
|
|
/* MAC = GHASH(PLAINTEXT) ^ ENCRYPT(J) */
|
|
|
|
/* Note: That MAC is calculated over plaintext is non-standard. */
|
|
|
|
/* It is supposed to be over the ciphertext. */
|
2018-02-20 17:39:50 +00:00
|
|
|
uint8_t calc_mac[0x10];
|
2018-03-09 01:26:42 +00:00
|
|
|
ghash(calc_mac, intermediate_buf, src_size - 0x20, j_block, true);
|
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
/* Const-time memcmp. */
|
2018-02-23 02:44:27 +00:00
|
|
|
const uint8_t *src_bytes = src;
|
2018-02-20 17:39:50 +00:00
|
|
|
int different = 0;
|
|
|
|
for (unsigned int i = 0; i < 0x10; i++) {
|
2018-02-23 02:44:27 +00:00
|
|
|
different |= src_bytes[src_size - 0x10 + i] ^ calc_mac[i];
|
2018-02-20 17:39:50 +00:00
|
|
|
}
|
|
|
|
if (different) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-20 18:29:44 +00:00
|
|
|
|
2018-03-09 01:26:42 +00:00
|
|
|
if ((read64be(intermediate_buf, src_size - 0x28) & 0x00FFFFFFFFFFFFFFULL) != fuse_get_device_id()) {
|
2018-02-25 09:21:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-03-25 22:05:08 +01:00
|
|
|
|
|
|
|
if (out_deviceid_high != NULL) {
|
|
|
|
*out_deviceid_high = intermediate_buf[src_size - 0x28];
|
|
|
|
}
|
2018-03-09 01:26:42 +00:00
|
|
|
|
|
|
|
memcpy(dst, intermediate_buf, src_size - 0x30);
|
|
|
|
memset(intermediate_buf, 0, sizeof(intermediate_buf));
|
2018-02-20 17:39:50 +00:00
|
|
|
return src_size - 0x30;
|
2018-02-20 18:29:44 +00:00
|
|
|
}
|
2018-03-25 22:05:08 +01:00
|
|
|
|
|
|
|
void gcm_encrypt_key(void *dst, size_t dst_size, const void *src, size_t src_size, const void *sealed_kek, size_t kek_size, const void *wrapped_key, size_t key_size, unsigned int usecase, uint64_t deviceid_high) {
|
|
|
|
uint8_t intermediate_buf[0x400] = {0};
|
|
|
|
if (src_size + 0x30 > dst_size) {
|
|
|
|
generic_panic();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unwrap the key */
|
|
|
|
unseal_key(KEYSLOT_SWITCH_TEMPKEY, sealed_kek, kek_size, usecase);
|
|
|
|
decrypt_data_into_keyslot(KEYSLOT_SWITCH_TEMPKEY, KEYSLOT_SWITCH_TEMPKEY, wrapped_key, key_size);
|
|
|
|
|
|
|
|
/* Generate a random CTR. */
|
|
|
|
flush_dcache_range(intermediate_buf, intermediate_buf + 0x10);
|
|
|
|
se_generate_random(KEYSLOT_SWITCH_RNGKEY, intermediate_buf, 0x10);
|
|
|
|
flush_dcache_range(intermediate_buf, intermediate_buf + 0x10);
|
|
|
|
|
|
|
|
/* Write Device ID. */
|
|
|
|
write64be(intermediate_buf, src_size + 0x18, fuse_get_device_id() | (deviceid_high << 56));
|
|
|
|
|
|
|
|
|
|
|
|
/* J = GHASH(CTR); */
|
|
|
|
uint8_t j_block[0x10];
|
|
|
|
ghash(j_block, intermediate_buf, 0x10, NULL, false);
|
|
|
|
|
|
|
|
/* MAC = GHASH(PLAINTEXT) ^ ENCRYPT(J) */
|
|
|
|
/* Note: That MAC is calculated over plaintext is non-standard. */
|
|
|
|
/* It is supposed to be over the ciphertext. */
|
|
|
|
ghash(intermediate_buf + src_size + 0x20, intermediate_buf + 0x10, src_size + 0x10, j_block, true);
|
|
|
|
|
|
|
|
/* Encrypt the GCM keypair, AES-CTR with CTR = blob[:0x10]. */
|
|
|
|
se_aes_ctr_crypt(KEYSLOT_SWITCH_TEMPKEY, intermediate_buf + 0x10, src_size + 0x10, intermediate_buf + 0x10, src_size + 0x10, intermediate_buf, 0x10);
|
|
|
|
|
|
|
|
/* Copy the wrapped key out. */
|
|
|
|
memcpy(dst, intermediate_buf, src_size + 0x30);
|
|
|
|
memset(intermediate_buf, 0, sizeof(intermediate_buf));
|
|
|
|
}
|