2019-01-24 14:53:45 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +00:00
|
|
|
* This file is part of the Trezor project, https://trezor.io/
|
2019-01-24 14:53:45 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) SatoshiLabs
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that 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/>.
|
|
|
|
*/
|
|
|
|
|
2019-10-29 14:55:58 +00:00
|
|
|
#include <assert.h>
|
2019-01-24 14:53:45 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
#include "chacha20poly1305/rfc7539.h"
|
2019-01-24 14:53:45 +00:00
|
|
|
#include "common.h"
|
2019-04-18 14:27:27 +00:00
|
|
|
#include "hmac.h"
|
|
|
|
#include "memzero.h"
|
2019-01-24 14:53:45 +00:00
|
|
|
#include "norcow.h"
|
|
|
|
#include "pbkdf2.h"
|
|
|
|
#include "rand.h"
|
2019-04-18 14:27:27 +00:00
|
|
|
#include "sha2.h"
|
|
|
|
#include "storage.h"
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
#define LOW_MASK 0x55555555
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// The APP namespace which is reserved for storage related values.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define APP_STORAGE 0x00
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// Norcow storage key of the PIN entry log and PIN success log.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define PIN_LOGS_KEY ((APP_STORAGE << 8) | 0x01)
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Norcow storage key of the combined salt, EDEK, ESAK and PIN verification code
|
|
|
|
// entry.
|
|
|
|
#define EDEK_PVC_KEY ((APP_STORAGE << 8) | 0x02)
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// Norcow storage key of the PIN set flag.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define PIN_NOT_SET_KEY ((APP_STORAGE << 8) | 0x03)
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// Norcow storage key of the storage version.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define VERSION_KEY ((APP_STORAGE << 8) | 0x04)
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// Norcow storage key of the storage authentication tag.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define STORAGE_TAG_KEY ((APP_STORAGE << 8) | 0x05)
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-10-31 13:44:39 +00:00
|
|
|
// Norcow storage key of the wipe code data.
|
|
|
|
#define WIPE_CODE_DATA_KEY ((APP_STORAGE << 8) | 0x06)
|
|
|
|
|
2019-10-23 15:50:46 +00:00
|
|
|
// Norcow storage key of the storage upgrade flag.
|
|
|
|
#define STORAGE_UPGRADED_KEY ((APP_STORAGE << 8) | 0x07)
|
|
|
|
|
2019-01-24 14:53:45 +00:00
|
|
|
// The PIN value corresponding to an empty PIN.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define PIN_EMPTY 1
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// Maximum number of failed unlock attempts.
|
2019-04-18 14:27:27 +00:00
|
|
|
// NOTE: The PIN counter logic relies on this constant being less than or equal
|
|
|
|
// to 16.
|
|
|
|
#define PIN_MAX_TRIES 16
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// The total number of iterations to use in PBKDF2.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define PIN_ITER_COUNT 20000
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-02-23 00:52:25 +00:00
|
|
|
// The number of seconds required to derive the KEK and KEIV.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define DERIVE_SECS 1
|
2019-02-23 00:52:25 +00:00
|
|
|
|
2019-01-24 14:53:45 +00:00
|
|
|
// The length of the guard key in words.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define GUARD_KEY_WORDS 1
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// The length of the PIN entry log or the PIN success log in words.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define PIN_LOG_WORDS 16
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// The length of a word in bytes.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define WORD_SIZE (sizeof(uint32_t))
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// The length of the hashed hardware salt in bytes.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define HARDWARE_SALT_SIZE SHA256_DIGEST_LENGTH
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// The length of the random salt in bytes.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define RANDOM_SALT_SIZE 4
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// The length of the data encryption key in bytes.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define DEK_SIZE 32
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// The length of the storage authentication key in bytes.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define SAK_SIZE 16
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// The combined length of the data encryption key and the storage authentication
|
|
|
|
// key in bytes.
|
|
|
|
#define KEYS_SIZE (DEK_SIZE + SAK_SIZE)
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// The length of the PIN verification code in bytes.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define PVC_SIZE 8
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// The length of the storage authentication tag in bytes.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define STORAGE_TAG_SIZE 16
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// The length of the Poly1305 authentication tag in bytes.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define POLY1305_TAG_SIZE 16
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// The length of the ChaCha20 IV (aka nonce) in bytes as per RFC 7539.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define CHACHA20_IV_SIZE 12
|
2019-01-24 14:53:45 +00:00
|
|
|
|
|
|
|
// The length of the ChaCha20 block in bytes.
|
|
|
|
#define CHACHA20_BLOCK_SIZE 64
|
|
|
|
|
2019-10-31 13:44:39 +00:00
|
|
|
// The length of the wipe code in bytes.
|
|
|
|
#define WIPE_CODE_SIZE (sizeof(uint32_t))
|
|
|
|
|
|
|
|
// The byte length of the salt used in checking the wipe code.
|
|
|
|
#define WIPE_CODE_SALT_SIZE 8
|
|
|
|
|
|
|
|
// The byte length of the tag used in checking the wipe code.
|
|
|
|
#define WIPE_CODE_TAG_SIZE 8
|
|
|
|
|
|
|
|
// The total length of the WIPE_CODE_DATA_KEY entry.
|
|
|
|
#define WIPE_CODE_DATA_SIZE \
|
|
|
|
(WIPE_CODE_SIZE + WIPE_CODE_SALT_SIZE + WIPE_CODE_TAG_SIZE)
|
|
|
|
|
|
|
|
// The value corresponding to an unconfigured wipe code.
|
|
|
|
// NOTE: This is intentionally different from PIN_EMPTY so that we don't need
|
|
|
|
// special handling when both the PIN and wipe code are not set.
|
|
|
|
#define WIPE_CODE_EMPTY 0
|
|
|
|
|
2019-02-11 16:46:46 +00:00
|
|
|
// The length of the counter tail in words.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define COUNTER_TAIL_WORDS 2
|
2019-02-08 19:24:55 +00:00
|
|
|
|
2019-01-24 14:53:45 +00:00
|
|
|
// Values used in the guard key integrity check.
|
2019-04-18 14:27:27 +00:00
|
|
|
#define GUARD_KEY_MODULUS 6311
|
2019-01-24 14:53:45 +00:00
|
|
|
#define GUARD_KEY_REMAINDER 15
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
const char *const VERIFYING_PIN_MSG = "Verifying PIN";
|
|
|
|
const char *const PROCESSING_MSG = "Processing";
|
|
|
|
const char *const STARTING_MSG = "Starting up";
|
2019-02-22 17:45:49 +00:00
|
|
|
|
2019-01-24 14:53:45 +00:00
|
|
|
static secbool initialized = secfalse;
|
|
|
|
static secbool unlocked = secfalse;
|
|
|
|
static PIN_UI_WAIT_CALLBACK ui_callback = NULL;
|
2019-02-23 00:52:25 +00:00
|
|
|
static uint32_t ui_total = 0;
|
|
|
|
static uint32_t ui_rem = 0;
|
|
|
|
static const char *ui_message = NULL;
|
2019-01-24 14:53:45 +00:00
|
|
|
static uint8_t cached_keys[KEYS_SIZE] = {0};
|
|
|
|
static uint8_t *const cached_dek = cached_keys;
|
|
|
|
static uint8_t *const cached_sak = cached_keys + DEK_SIZE;
|
|
|
|
static uint8_t authentication_sum[SHA256_DIGEST_LENGTH] = {0};
|
|
|
|
static uint8_t hardware_salt[HARDWARE_SALT_SIZE] = {0};
|
|
|
|
static uint32_t norcow_active_version = 0;
|
|
|
|
static const uint8_t TRUE_BYTE = 0x01;
|
|
|
|
static const uint8_t FALSE_BYTE = 0x00;
|
2019-10-23 15:50:46 +00:00
|
|
|
static const uint32_t TRUE_WORD = 0xC35A69A5;
|
|
|
|
static const uint32_t FALSE_WORD = 0x3CA5965A;
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
static void __handle_fault(const char *msg, const char *file, int line,
|
|
|
|
const char *func);
|
2019-02-07 13:03:25 +00:00
|
|
|
#define handle_fault(msg) (__handle_fault(msg, __FILE__, __LINE__, __func__))
|
|
|
|
|
2019-01-25 15:23:20 +00:00
|
|
|
static secbool storage_upgrade(void);
|
2019-04-18 14:27:27 +00:00
|
|
|
static secbool storage_set_encrypted(const uint16_t key, const void *val,
|
|
|
|
const uint16_t len);
|
|
|
|
static secbool storage_get_encrypted(const uint16_t key, void *val_dest,
|
|
|
|
const uint16_t max_len, uint16_t *len);
|
|
|
|
|
|
|
|
static secbool secequal(const void *ptr1, const void *ptr2, size_t n) {
|
|
|
|
const uint8_t *p1 = ptr1;
|
|
|
|
const uint8_t *p2 = ptr2;
|
|
|
|
uint8_t diff = 0;
|
2019-10-04 13:00:24 +00:00
|
|
|
size_t i = 0;
|
2019-04-18 14:27:27 +00:00
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
diff |= *p1 ^ *p2;
|
|
|
|
++p1;
|
|
|
|
++p2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check loop completion in case of a fault injection attack.
|
|
|
|
if (i != n) {
|
|
|
|
handle_fault("loop completion check");
|
|
|
|
}
|
|
|
|
|
|
|
|
return diff ? secfalse : sectrue;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 14:55:58 +00:00
|
|
|
static secbool secequal32(const void *ptr1, const void *ptr2, size_t n) {
|
|
|
|
assert(n % sizeof(uint32_t) == 0);
|
2019-12-02 13:29:04 +00:00
|
|
|
assert((uintptr_t)ptr1 % sizeof(uint32_t) == 0);
|
|
|
|
assert((uintptr_t)ptr2 % sizeof(uint32_t) == 0);
|
|
|
|
|
2019-10-29 14:55:58 +00:00
|
|
|
size_t wn = n / sizeof(uint32_t);
|
|
|
|
const uint32_t *p1 = (const uint32_t *)ptr1;
|
|
|
|
const uint32_t *p2 = (const uint32_t *)ptr2;
|
2019-04-18 14:27:27 +00:00
|
|
|
uint32_t diff = 0;
|
2019-10-04 13:00:24 +00:00
|
|
|
size_t i = 0;
|
2019-10-29 14:55:58 +00:00
|
|
|
for (i = 0; i < wn; ++i) {
|
2019-04-18 14:27:27 +00:00
|
|
|
uint32_t mask = random32();
|
2019-10-29 14:55:58 +00:00
|
|
|
diff |= (*p1 + mask - *p2) ^ mask;
|
|
|
|
++p1;
|
|
|
|
++p2;
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check loop completion in case of a fault injection attack.
|
2019-10-29 14:55:58 +00:00
|
|
|
if (i != wn) {
|
2019-04-18 14:27:27 +00:00
|
|
|
handle_fault("loop completion check");
|
|
|
|
}
|
|
|
|
|
|
|
|
return diff ? secfalse : sectrue;
|
2019-02-21 13:32:19 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 14:53:45 +00:00
|
|
|
static secbool is_protected(uint16_t key) {
|
2019-04-18 14:27:27 +00:00
|
|
|
const uint8_t app = key >> 8;
|
|
|
|
return ((app & FLAG_PUBLIC) == 0 && app != APP_STORAGE) ? sectrue : secfalse;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the storage authentication tag for freshly wiped storage.
|
|
|
|
*/
|
2019-01-25 15:23:20 +00:00
|
|
|
static secbool auth_init(void) {
|
2019-10-04 13:00:24 +00:00
|
|
|
uint8_t tag[SHA256_DIGEST_LENGTH] = {0};
|
2019-04-18 14:27:27 +00:00
|
|
|
memzero(authentication_sum, sizeof(authentication_sum));
|
|
|
|
hmac_sha256(cached_sak, SAK_SIZE, authentication_sum,
|
|
|
|
sizeof(authentication_sum), tag);
|
|
|
|
return norcow_set(STORAGE_TAG_KEY, tag, STORAGE_TAG_SIZE);
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the storage authentication tag with the given key.
|
|
|
|
*/
|
|
|
|
static secbool auth_update(uint16_t key) {
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != is_protected(key)) {
|
|
|
|
return sectrue;
|
|
|
|
}
|
|
|
|
|
2019-10-04 13:00:24 +00:00
|
|
|
uint8_t tag[SHA256_DIGEST_LENGTH] = {0};
|
2019-04-18 14:27:27 +00:00
|
|
|
hmac_sha256(cached_sak, SAK_SIZE, (uint8_t *)&key, sizeof(key), tag);
|
|
|
|
for (uint32_t i = 0; i < SHA256_DIGEST_LENGTH; i++) {
|
|
|
|
authentication_sum[i] ^= tag[i];
|
|
|
|
}
|
|
|
|
hmac_sha256(cached_sak, SAK_SIZE, authentication_sum,
|
|
|
|
sizeof(authentication_sum), tag);
|
|
|
|
return norcow_set(STORAGE_TAG_KEY, tag, STORAGE_TAG_SIZE);
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-04-18 14:27:27 +00:00
|
|
|
* A secure version of norcow_set(), which updates the storage authentication
|
|
|
|
* tag.
|
2019-01-24 14:53:45 +00:00
|
|
|
*/
|
|
|
|
static secbool auth_set(uint16_t key, const void *val, uint16_t len) {
|
2019-10-04 13:00:24 +00:00
|
|
|
secbool found = secfalse;
|
2019-04-18 14:27:27 +00:00
|
|
|
secbool ret = norcow_set_ex(key, val, len, &found);
|
|
|
|
if (sectrue == ret && secfalse == found) {
|
|
|
|
ret = auth_update(key);
|
|
|
|
if (sectrue != ret) {
|
|
|
|
norcow_delete(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-04-18 14:27:27 +00:00
|
|
|
* A secure version of norcow_get(), which checks the storage authentication
|
|
|
|
* tag.
|
2019-01-24 14:53:45 +00:00
|
|
|
*/
|
2019-04-18 14:27:27 +00:00
|
|
|
static secbool auth_get(uint16_t key, const void **val, uint16_t *len) {
|
|
|
|
*val = NULL;
|
|
|
|
*len = 0;
|
|
|
|
uint32_t sum[SHA256_DIGEST_LENGTH / sizeof(uint32_t)] = {0};
|
|
|
|
|
|
|
|
// Prepare inner and outer digest.
|
2019-10-04 13:00:24 +00:00
|
|
|
uint32_t odig[SHA256_DIGEST_LENGTH / sizeof(uint32_t)] = {0};
|
|
|
|
uint32_t idig[SHA256_DIGEST_LENGTH / sizeof(uint32_t)] = {0};
|
2019-04-18 14:27:27 +00:00
|
|
|
hmac_sha256_prepare(cached_sak, SAK_SIZE, odig, idig);
|
|
|
|
|
|
|
|
// Prepare SHA-256 message padding.
|
|
|
|
uint32_t g[SHA256_BLOCK_LENGTH / sizeof(uint32_t)] = {0};
|
|
|
|
uint32_t h[SHA256_BLOCK_LENGTH / sizeof(uint32_t)] = {0};
|
|
|
|
g[15] = (SHA256_BLOCK_LENGTH + 2) * 8;
|
|
|
|
h[15] = (SHA256_BLOCK_LENGTH + SHA256_DIGEST_LENGTH) * 8;
|
|
|
|
h[8] = 0x80000000;
|
|
|
|
|
|
|
|
uint32_t offset = 0;
|
|
|
|
uint16_t k = 0;
|
|
|
|
uint16_t l = 0;
|
|
|
|
uint16_t tag_len = 0;
|
|
|
|
uint16_t entry_count = 0; // Mitigation against fault injection.
|
|
|
|
uint16_t other_count = 0; // Mitigation against fault injection.
|
|
|
|
const void *v = NULL;
|
|
|
|
const void *tag_val = NULL;
|
|
|
|
while (sectrue == norcow_get_next(&offset, &k, &v, &l)) {
|
|
|
|
++entry_count;
|
|
|
|
if (k == key) {
|
|
|
|
*val = v;
|
|
|
|
*len = l;
|
|
|
|
} else {
|
|
|
|
++other_count;
|
|
|
|
}
|
|
|
|
if (sectrue != is_protected(k)) {
|
|
|
|
if (k == STORAGE_TAG_KEY) {
|
|
|
|
tag_val = v;
|
|
|
|
tag_len = l;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
g[0] = (((uint32_t)k & 0xff) << 24) | (((uint32_t)k & 0xff00) << 8) |
|
|
|
|
0x8000; // Add SHA message padding.
|
|
|
|
sha256_Transform(idig, g, h);
|
|
|
|
sha256_Transform(odig, h, h);
|
|
|
|
for (uint32_t i = 0; i < SHA256_DIGEST_LENGTH / sizeof(uint32_t); i++) {
|
|
|
|
sum[i] ^= h[i];
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
|
|
|
memcpy(h, sum, sizeof(sum));
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
sha256_Transform(idig, h, h);
|
|
|
|
sha256_Transform(odig, h, h);
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
memzero(odig, sizeof(odig));
|
|
|
|
memzero(idig, sizeof(idig));
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Cache the authentication sum.
|
|
|
|
for (size_t i = 0; i < SHA256_DIGEST_LENGTH / sizeof(uint32_t); i++) {
|
2019-01-24 14:53:45 +00:00
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
2019-04-18 14:27:27 +00:00
|
|
|
REVERSE32(sum[i], ((uint32_t *)authentication_sum)[i]);
|
2019-01-24 14:53:45 +00:00
|
|
|
#else
|
2019-04-18 14:27:27 +00:00
|
|
|
((uint32_t *)authentication_sum)[i] = sum[i];
|
2019-01-24 14:53:45 +00:00
|
|
|
#endif
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Check loop completion in case of a fault injection attack.
|
|
|
|
if (secfalse != norcow_get_next(&offset, &k, &v, &l)) {
|
|
|
|
handle_fault("loop completion check");
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Check storage authentication tag.
|
2019-01-24 14:53:45 +00:00
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
2019-04-18 14:27:27 +00:00
|
|
|
for (size_t i = 0; i < SHA256_DIGEST_LENGTH / sizeof(uint32_t); i++) {
|
|
|
|
REVERSE32(h[i], h[i]);
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
#endif
|
2019-04-18 14:27:27 +00:00
|
|
|
if (tag_val == NULL || tag_len != STORAGE_TAG_SIZE ||
|
|
|
|
sectrue != secequal(h, tag_val, STORAGE_TAG_SIZE)) {
|
|
|
|
handle_fault("storage tag check");
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (*val == NULL) {
|
|
|
|
// Check for fault injection.
|
|
|
|
if (other_count != entry_count) {
|
|
|
|
handle_fault("sanity check");
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
return sectrue;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 13:44:39 +00:00
|
|
|
static secbool set_wipe_code(uint32_t wipe_code) {
|
|
|
|
if (wipe_code == PIN_EMPTY) {
|
|
|
|
// This is to avoid having to check pin != PIN_EMPTY when checking the wipe
|
|
|
|
// code.
|
|
|
|
wipe_code = WIPE_CODE_EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The format of the WIPE_CODE_DATA_KEY entry is:
|
|
|
|
// wipe code (4 bytes), random salt (16 bytes), authentication tag (16 bytes)
|
|
|
|
// NOTE: We allocate extra space for the HMAC computation.
|
|
|
|
uint8_t wipe_code_data[WIPE_CODE_SIZE + WIPE_CODE_SALT_SIZE +
|
|
|
|
SHA256_DIGEST_LENGTH] = {0};
|
|
|
|
uint8_t *salt = wipe_code_data + WIPE_CODE_SIZE;
|
|
|
|
uint8_t *tag = wipe_code_data + WIPE_CODE_SIZE + WIPE_CODE_SALT_SIZE;
|
|
|
|
memcpy(wipe_code_data, &wipe_code, sizeof(wipe_code));
|
|
|
|
memzero(&wipe_code, sizeof(wipe_code));
|
|
|
|
random_buffer(salt, WIPE_CODE_SALT_SIZE);
|
|
|
|
hmac_sha256(salt, WIPE_CODE_SALT_SIZE, wipe_code_data, WIPE_CODE_SIZE, tag);
|
|
|
|
return norcow_set(WIPE_CODE_DATA_KEY, wipe_code_data, WIPE_CODE_DATA_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static secbool is_not_wipe_code(uint32_t pin) {
|
|
|
|
uint8_t wipe_code[WIPE_CODE_SIZE] = {0};
|
|
|
|
uint8_t salt[WIPE_CODE_SALT_SIZE] = {0};
|
|
|
|
uint8_t stored_tag[WIPE_CODE_TAG_SIZE] = {0};
|
|
|
|
uint8_t computed_tag1[SHA256_DIGEST_LENGTH] = {0};
|
|
|
|
uint8_t computed_tag2[SHA256_DIGEST_LENGTH] = {0};
|
|
|
|
|
|
|
|
// Read the wipe code data from the storage.
|
|
|
|
const void *wipe_code_data = NULL;
|
|
|
|
uint16_t len = 0;
|
|
|
|
if (sectrue != norcow_get(WIPE_CODE_DATA_KEY, &wipe_code_data, &len) ||
|
|
|
|
len != WIPE_CODE_DATA_SIZE) {
|
|
|
|
handle_fault("no wipe code");
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
memcpy(wipe_code, wipe_code_data, sizeof(wipe_code));
|
|
|
|
memcpy(salt, (uint8_t *)wipe_code_data + WIPE_CODE_SIZE, sizeof(salt));
|
|
|
|
memcpy(stored_tag,
|
|
|
|
(uint8_t *)wipe_code_data + WIPE_CODE_SIZE + WIPE_CODE_SALT_SIZE,
|
|
|
|
sizeof(stored_tag));
|
|
|
|
|
|
|
|
// Check integrity in case of flash read manipulation attack.
|
|
|
|
hmac_sha256(salt, WIPE_CODE_SALT_SIZE, wipe_code, sizeof(wipe_code),
|
|
|
|
computed_tag1);
|
|
|
|
memzero(wipe_code, sizeof(wipe_code));
|
|
|
|
if (sectrue != secequal(stored_tag, computed_tag1, sizeof(stored_tag))) {
|
|
|
|
handle_fault("wipe code tag");
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the authentication tag of the entered PIN.
|
|
|
|
wait_random();
|
|
|
|
hmac_sha256(salt, WIPE_CODE_SALT_SIZE, (const uint8_t *)&pin, WIPE_CODE_SIZE,
|
|
|
|
computed_tag1);
|
|
|
|
|
|
|
|
// Recompute to check for fault injection attack.
|
|
|
|
wait_random();
|
|
|
|
hmac_sha256(salt, WIPE_CODE_SALT_SIZE, (const uint8_t *)&pin, WIPE_CODE_SIZE,
|
|
|
|
computed_tag2);
|
|
|
|
memzero(salt, sizeof(salt));
|
|
|
|
if (sectrue !=
|
|
|
|
secequal(computed_tag1, computed_tag2, sizeof(computed_tag1))) {
|
|
|
|
handle_fault("wipe code fault");
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
memzero(&pin, sizeof(pin));
|
|
|
|
|
|
|
|
// Compare wipe code with the entered PIN via the authentication tag.
|
|
|
|
wait_random();
|
|
|
|
if (secfalse != secequal(stored_tag, computed_tag1, sizeof(stored_tag))) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
memzero(stored_tag, sizeof(stored_tag));
|
|
|
|
return sectrue;
|
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
static void derive_kek(uint32_t pin, const uint8_t *random_salt,
|
2019-08-09 20:52:09 +00:00
|
|
|
const uint8_t *ext_salt,
|
2019-04-18 14:27:27 +00:00
|
|
|
uint8_t kek[SHA256_DIGEST_LENGTH],
|
|
|
|
uint8_t keiv[SHA256_DIGEST_LENGTH]) {
|
2019-01-24 14:53:45 +00:00
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
2019-04-18 14:27:27 +00:00
|
|
|
REVERSE32(pin, pin);
|
2019-01-24 14:53:45 +00:00
|
|
|
#endif
|
|
|
|
|
2019-10-04 13:00:24 +00:00
|
|
|
uint8_t salt[HARDWARE_SALT_SIZE + RANDOM_SALT_SIZE + EXTERNAL_SALT_SIZE] = {
|
|
|
|
0};
|
2019-08-09 20:52:09 +00:00
|
|
|
size_t salt_len = 0;
|
|
|
|
|
|
|
|
memcpy(salt + salt_len, hardware_salt, HARDWARE_SALT_SIZE);
|
|
|
|
salt_len += HARDWARE_SALT_SIZE;
|
|
|
|
|
|
|
|
memcpy(salt + salt_len, random_salt, RANDOM_SALT_SIZE);
|
|
|
|
salt_len += RANDOM_SALT_SIZE;
|
|
|
|
|
|
|
|
if (ext_salt != NULL) {
|
|
|
|
memcpy(salt + salt_len, ext_salt, EXTERNAL_SALT_SIZE);
|
|
|
|
salt_len += EXTERNAL_SALT_SIZE;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
uint32_t progress = (ui_total - ui_rem) * 1000 / ui_total;
|
|
|
|
if (ui_callback && ui_message) {
|
|
|
|
ui_callback(ui_rem, progress, ui_message);
|
|
|
|
}
|
|
|
|
|
2019-10-04 13:00:24 +00:00
|
|
|
PBKDF2_HMAC_SHA256_CTX ctx = {0};
|
2019-04-18 14:27:27 +00:00
|
|
|
pbkdf2_hmac_sha256_Init(&ctx, (const uint8_t *)&pin, sizeof(pin), salt,
|
2019-08-09 20:52:09 +00:00
|
|
|
salt_len, 1);
|
2019-04-18 14:27:27 +00:00
|
|
|
for (int i = 1; i <= 5; i++) {
|
|
|
|
pbkdf2_hmac_sha256_Update(&ctx, PIN_ITER_COUNT / 10);
|
2019-02-23 00:52:25 +00:00
|
|
|
if (ui_callback && ui_message) {
|
2019-04-18 14:27:27 +00:00
|
|
|
progress =
|
|
|
|
((ui_total - ui_rem) * 1000 + i * DERIVE_SECS * 100) / ui_total;
|
|
|
|
ui_callback(ui_rem - i * DERIVE_SECS / 10, progress, ui_message);
|
2019-02-23 00:52:25 +00:00
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
|
|
|
pbkdf2_hmac_sha256_Final(&ctx, kek);
|
2019-02-22 17:45:49 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
pbkdf2_hmac_sha256_Init(&ctx, (const uint8_t *)&pin, sizeof(pin), salt,
|
2019-08-09 20:52:09 +00:00
|
|
|
salt_len, 2);
|
2019-04-18 14:27:27 +00:00
|
|
|
for (int i = 6; i <= 10; i++) {
|
|
|
|
pbkdf2_hmac_sha256_Update(&ctx, PIN_ITER_COUNT / 10);
|
|
|
|
if (ui_callback && ui_message) {
|
|
|
|
progress =
|
|
|
|
((ui_total - ui_rem) * 1000 + i * DERIVE_SECS * 100) / ui_total;
|
|
|
|
ui_callback(ui_rem - i * DERIVE_SECS / 10, progress, ui_message);
|
2019-02-18 18:19:17 +00:00
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
|
|
|
pbkdf2_hmac_sha256_Final(&ctx, keiv);
|
2019-02-23 00:52:25 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
ui_rem -= DERIVE_SECS;
|
|
|
|
memzero(&ctx, sizeof(PBKDF2_HMAC_SHA256_CTX));
|
|
|
|
memzero(&pin, sizeof(pin));
|
|
|
|
memzero(&salt, sizeof(salt));
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 20:52:09 +00:00
|
|
|
static secbool set_pin(uint32_t pin, const uint8_t *ext_salt) {
|
2019-10-31 13:44:39 +00:00
|
|
|
// Fail if the PIN is the same as the wipe code. Ignore during upgrade.
|
|
|
|
if (norcow_active_version != 0 && sectrue != is_not_wipe_code(pin)) {
|
|
|
|
memzero(&pin, sizeof(pin));
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt the cached keys using the new PIN and set the new PVC.
|
2019-10-04 13:00:24 +00:00
|
|
|
uint8_t buffer[RANDOM_SALT_SIZE + KEYS_SIZE + POLY1305_TAG_SIZE] = {0};
|
2019-08-09 20:52:09 +00:00
|
|
|
uint8_t *rand_salt = buffer;
|
2019-04-18 14:27:27 +00:00
|
|
|
uint8_t *ekeys = buffer + RANDOM_SALT_SIZE;
|
|
|
|
uint8_t *pvc = buffer + RANDOM_SALT_SIZE + KEYS_SIZE;
|
|
|
|
|
2019-10-04 13:00:24 +00:00
|
|
|
uint8_t kek[SHA256_DIGEST_LENGTH] = {0};
|
|
|
|
uint8_t keiv[SHA256_DIGEST_LENGTH] = {0};
|
|
|
|
chacha20poly1305_ctx ctx = {0};
|
2019-08-09 20:52:09 +00:00
|
|
|
random_buffer(rand_salt, RANDOM_SALT_SIZE);
|
|
|
|
derive_kek(pin, rand_salt, ext_salt, kek, keiv);
|
2019-04-18 14:27:27 +00:00
|
|
|
rfc7539_init(&ctx, kek, keiv);
|
|
|
|
memzero(kek, sizeof(kek));
|
|
|
|
memzero(keiv, sizeof(keiv));
|
|
|
|
chacha20poly1305_encrypt(&ctx, cached_keys, ekeys, KEYS_SIZE);
|
|
|
|
rfc7539_finish(&ctx, 0, KEYS_SIZE, pvc);
|
|
|
|
memzero(&ctx, sizeof(ctx));
|
|
|
|
secbool ret =
|
|
|
|
norcow_set(EDEK_PVC_KEY, buffer, RANDOM_SALT_SIZE + KEYS_SIZE + PVC_SIZE);
|
|
|
|
memzero(buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
if (ret == sectrue) {
|
|
|
|
if (pin == PIN_EMPTY) {
|
|
|
|
ret = norcow_set(PIN_NOT_SET_KEY, &TRUE_BYTE, sizeof(TRUE_BYTE));
|
|
|
|
} else {
|
|
|
|
ret = norcow_set(PIN_NOT_SET_KEY, &FALSE_BYTE, sizeof(FALSE_BYTE));
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
memzero(&pin, sizeof(pin));
|
|
|
|
return ret;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
static secbool check_guard_key(const uint32_t guard_key) {
|
|
|
|
if (guard_key % GUARD_KEY_MODULUS != GUARD_KEY_REMAINDER) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Check that each byte of (guard_key & 0xAAAAAAAA) has exactly two bits set.
|
|
|
|
uint32_t count = (guard_key & 0x22222222) + ((guard_key >> 2) & 0x22222222);
|
|
|
|
count = count + (count >> 4);
|
|
|
|
if ((count & 0x0e0e0e0e) != 0x04040404) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Check that the guard_key does not contain a run of 5 (or more) zeros or
|
|
|
|
// ones.
|
|
|
|
uint32_t zero_runs = ~guard_key;
|
|
|
|
zero_runs = zero_runs & (zero_runs >> 2);
|
|
|
|
zero_runs = zero_runs & (zero_runs >> 1);
|
|
|
|
zero_runs = zero_runs & (zero_runs >> 1);
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
uint32_t one_runs = guard_key;
|
|
|
|
one_runs = one_runs & (one_runs >> 2);
|
|
|
|
one_runs = one_runs & (one_runs >> 1);
|
|
|
|
one_runs = one_runs & (one_runs >> 1);
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if ((one_runs != 0) || (zero_runs != 0)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
return sectrue;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
static uint32_t generate_guard_key(void) {
|
|
|
|
uint32_t guard_key = 0;
|
|
|
|
do {
|
|
|
|
guard_key = random_uniform((UINT32_MAX / GUARD_KEY_MODULUS) + 1) *
|
|
|
|
GUARD_KEY_MODULUS +
|
|
|
|
GUARD_KEY_REMAINDER;
|
|
|
|
} while (sectrue != check_guard_key(guard_key));
|
|
|
|
return guard_key;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
static secbool expand_guard_key(const uint32_t guard_key, uint32_t *guard_mask,
|
|
|
|
uint32_t *guard) {
|
|
|
|
if (sectrue != check_guard_key(guard_key)) {
|
|
|
|
handle_fault("guard key check");
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
*guard_mask = ((guard_key & LOW_MASK) << 1) | ((~guard_key) & LOW_MASK);
|
|
|
|
*guard = (((guard_key & LOW_MASK) << 1) & guard_key) |
|
|
|
|
(((~guard_key) & LOW_MASK) & (guard_key >> 1));
|
|
|
|
return sectrue;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
static secbool pin_logs_init(uint32_t fails) {
|
|
|
|
if (fails >= PIN_MAX_TRIES) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// The format of the PIN_LOGS_KEY entry is:
|
|
|
|
// guard_key (1 word), pin_success_log (PIN_LOG_WORDS), pin_entry_log
|
|
|
|
// (PIN_LOG_WORDS)
|
2019-10-04 13:00:24 +00:00
|
|
|
uint32_t logs[GUARD_KEY_WORDS + 2 * PIN_LOG_WORDS] = {0};
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
logs[0] = generate_guard_key();
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-10-04 13:00:24 +00:00
|
|
|
uint32_t guard_mask = 0;
|
|
|
|
uint32_t guard = 0;
|
2019-04-18 14:27:27 +00:00
|
|
|
wait_random();
|
|
|
|
if (sectrue != expand_guard_key(logs[0], &guard_mask, &guard)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
uint32_t unused = guard | ~guard_mask;
|
|
|
|
for (size_t i = 0; i < 2 * PIN_LOG_WORDS; ++i) {
|
|
|
|
logs[GUARD_KEY_WORDS + i] = unused;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Set the first word of the PIN entry log to indicate the requested number of
|
|
|
|
// fails.
|
|
|
|
logs[GUARD_KEY_WORDS + PIN_LOG_WORDS] =
|
|
|
|
((((uint32_t)0xFFFFFFFF) >> (2 * fails)) & ~guard_mask) | guard;
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
return norcow_set(PIN_LOGS_KEY, logs, sizeof(logs));
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-04-18 14:27:27 +00:00
|
|
|
* Initializes the values of VERSION_KEY, EDEK_PVC_KEY, PIN_NOT_SET_KEY and
|
|
|
|
* PIN_LOGS_KEY using an empty PIN. This function should be called to initialize
|
|
|
|
* freshly wiped storage.
|
2019-01-24 14:53:45 +00:00
|
|
|
*/
|
2019-04-18 14:27:27 +00:00
|
|
|
static void init_wiped_storage(void) {
|
|
|
|
if (sectrue != initialized) {
|
|
|
|
// We cannot initialize the storage contents if the hardware_salt is not
|
|
|
|
// set.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
random_buffer(cached_keys, sizeof(cached_keys));
|
2019-12-05 08:41:48 +00:00
|
|
|
unlocked = sectrue;
|
2019-04-18 14:27:27 +00:00
|
|
|
uint32_t version = NORCOW_VERSION;
|
|
|
|
ensure(auth_init(), "set_storage_auth_tag failed");
|
|
|
|
ensure(storage_set_encrypted(VERSION_KEY, &version, sizeof(version)),
|
|
|
|
"set_storage_version failed");
|
2019-10-23 15:50:46 +00:00
|
|
|
ensure(norcow_set(STORAGE_UPGRADED_KEY, &FALSE_WORD, sizeof(FALSE_WORD)),
|
|
|
|
"set_storage_not_upgraded failed");
|
2019-04-18 14:27:27 +00:00
|
|
|
ensure(pin_logs_init(0), "init_pin_logs failed");
|
2019-10-31 13:44:39 +00:00
|
|
|
ensure(set_wipe_code(WIPE_CODE_EMPTY), "set_wipe_code failed");
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
ui_total = DERIVE_SECS;
|
|
|
|
ui_rem = ui_total;
|
|
|
|
ui_message = PROCESSING_MSG;
|
2019-08-09 20:52:09 +00:00
|
|
|
ensure(set_pin(PIN_EMPTY, NULL), "init_pin failed");
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
void storage_init(PIN_UI_WAIT_CALLBACK callback, const uint8_t *salt,
|
|
|
|
const uint16_t salt_len) {
|
|
|
|
initialized = secfalse;
|
|
|
|
unlocked = secfalse;
|
|
|
|
norcow_init(&norcow_active_version);
|
|
|
|
initialized = sectrue;
|
|
|
|
ui_callback = callback;
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
sha256_Raw(salt, salt_len, hardware_salt);
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (norcow_active_version < NORCOW_VERSION) {
|
|
|
|
if (sectrue != storage_upgrade()) {
|
|
|
|
storage_wipe();
|
|
|
|
ensure(secfalse, "storage_upgrade failed");
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// If there is no EDEK, then generate a random DEK and SAK and store them.
|
2019-10-04 13:00:24 +00:00
|
|
|
const void *val = NULL;
|
|
|
|
uint16_t len = 0;
|
2019-04-18 14:27:27 +00:00
|
|
|
if (secfalse == norcow_get(EDEK_PVC_KEY, &val, &len)) {
|
|
|
|
init_wiped_storage();
|
2019-12-04 14:23:15 +00:00
|
|
|
storage_lock();
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
|
|
|
memzero(cached_keys, sizeof(cached_keys));
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
static secbool pin_fails_reset(void) {
|
|
|
|
const void *logs = NULL;
|
|
|
|
uint16_t len = 0;
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != norcow_get(PIN_LOGS_KEY, &logs, &len) ||
|
|
|
|
len != WORD_SIZE * (GUARD_KEY_WORDS + 2 * PIN_LOG_WORDS)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-10-04 13:00:24 +00:00
|
|
|
uint32_t guard_mask = 0;
|
|
|
|
uint32_t guard = 0;
|
2019-04-18 14:27:27 +00:00
|
|
|
wait_random();
|
|
|
|
if (sectrue !=
|
|
|
|
expand_guard_key(*(const uint32_t *)logs, &guard_mask, &guard)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t unused = guard | ~guard_mask;
|
|
|
|
const uint32_t *success_log = ((const uint32_t *)logs) + GUARD_KEY_WORDS;
|
|
|
|
const uint32_t *entry_log = success_log + PIN_LOG_WORDS;
|
|
|
|
for (size_t i = 0; i < PIN_LOG_WORDS; ++i) {
|
|
|
|
if (entry_log[i] == unused) {
|
|
|
|
return sectrue;
|
|
|
|
}
|
|
|
|
if (success_log[i] != guard) {
|
|
|
|
if (sectrue != norcow_update_word(
|
|
|
|
PIN_LOGS_KEY, sizeof(uint32_t) * (i + GUARD_KEY_WORDS),
|
|
|
|
entry_log[i])) {
|
2019-01-24 14:53:45 +00:00
|
|
|
return secfalse;
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
|
|
|
return pin_logs_init(0);
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
secbool storage_pin_fails_increase(void) {
|
|
|
|
if (sectrue != initialized) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-02-04 16:32:58 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
const void *logs = NULL;
|
|
|
|
uint16_t len = 0;
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
wait_random();
|
|
|
|
if (sectrue != norcow_get(PIN_LOGS_KEY, &logs, &len) ||
|
|
|
|
len != WORD_SIZE * (GUARD_KEY_WORDS + 2 * PIN_LOG_WORDS)) {
|
|
|
|
handle_fault("no PIN logs");
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
2019-10-04 13:00:24 +00:00
|
|
|
uint32_t guard_mask = 0;
|
|
|
|
uint32_t guard = 0;
|
2019-04-18 14:27:27 +00:00
|
|
|
wait_random();
|
|
|
|
if (sectrue !=
|
|
|
|
expand_guard_key(*(const uint32_t *)logs, &guard_mask, &guard)) {
|
|
|
|
handle_fault("guard key expansion");
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
const uint32_t *entry_log =
|
|
|
|
((const uint32_t *)logs) + GUARD_KEY_WORDS + PIN_LOG_WORDS;
|
|
|
|
for (size_t i = 0; i < PIN_LOG_WORDS; ++i) {
|
2019-01-24 14:53:45 +00:00
|
|
|
wait_random();
|
2019-04-18 14:27:27 +00:00
|
|
|
if ((entry_log[i] & guard_mask) != guard) {
|
|
|
|
handle_fault("guard bits check");
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
if (entry_log[i] != guard) {
|
|
|
|
wait_random();
|
|
|
|
uint32_t word = entry_log[i] & ~guard_mask;
|
|
|
|
word = ((word >> 1) | word) & LOW_MASK;
|
|
|
|
word = (word >> 2) | (word >> 1);
|
|
|
|
|
|
|
|
wait_random();
|
|
|
|
if (sectrue !=
|
|
|
|
norcow_update_word(
|
|
|
|
PIN_LOGS_KEY,
|
|
|
|
sizeof(uint32_t) * (i + GUARD_KEY_WORDS + PIN_LOG_WORDS),
|
|
|
|
(word & ~guard_mask) | guard)) {
|
|
|
|
handle_fault("PIN logs update");
|
2019-01-24 14:53:45 +00:00
|
|
|
return secfalse;
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
|
|
|
return sectrue;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
|
|
|
handle_fault("PIN log exhausted");
|
|
|
|
return secfalse;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
static uint32_t hamming_weight(uint32_t value) {
|
|
|
|
value = value - ((value >> 1) & 0x55555555);
|
|
|
|
value = (value & 0x33333333) + ((value >> 2) & 0x33333333);
|
|
|
|
value = (value + (value >> 4)) & 0x0F0F0F0F;
|
|
|
|
value = value + (value >> 8);
|
|
|
|
value = value + (value >> 16);
|
|
|
|
return value & 0x3F;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
static secbool pin_get_fails(uint32_t *ctr) {
|
|
|
|
*ctr = PIN_MAX_TRIES;
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
const void *logs = NULL;
|
|
|
|
uint16_t len = 0;
|
|
|
|
wait_random();
|
|
|
|
if (sectrue != norcow_get(PIN_LOGS_KEY, &logs, &len) ||
|
|
|
|
len != WORD_SIZE * (GUARD_KEY_WORDS + 2 * PIN_LOG_WORDS)) {
|
|
|
|
handle_fault("no PIN logs");
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
2019-10-04 13:00:24 +00:00
|
|
|
uint32_t guard_mask = 0;
|
|
|
|
uint32_t guard = 0;
|
2019-04-18 14:27:27 +00:00
|
|
|
wait_random();
|
|
|
|
if (sectrue !=
|
|
|
|
expand_guard_key(*(const uint32_t *)logs, &guard_mask, &guard)) {
|
|
|
|
handle_fault("guard key expansion");
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
const uint32_t unused = guard | ~guard_mask;
|
|
|
|
|
|
|
|
const uint32_t *success_log = ((const uint32_t *)logs) + GUARD_KEY_WORDS;
|
|
|
|
const uint32_t *entry_log = success_log + PIN_LOG_WORDS;
|
|
|
|
volatile int current = -1;
|
2019-10-04 13:00:24 +00:00
|
|
|
volatile size_t i = 0;
|
2019-04-18 14:27:27 +00:00
|
|
|
for (i = 0; i < PIN_LOG_WORDS; ++i) {
|
|
|
|
if ((entry_log[i] & guard_mask) != guard ||
|
|
|
|
(success_log[i] & guard_mask) != guard ||
|
|
|
|
(entry_log[i] & success_log[i]) != entry_log[i]) {
|
|
|
|
handle_fault("PIN logs format check");
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current == -1) {
|
|
|
|
if (entry_log[i] != guard) {
|
|
|
|
current = i;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (entry_log[i] != unused) {
|
2019-02-07 13:03:25 +00:00
|
|
|
handle_fault("PIN entry log format check");
|
2019-01-24 14:53:45 +00:00
|
|
|
return secfalse;
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (current < 0 || current >= PIN_LOG_WORDS || i != PIN_LOG_WORDS) {
|
|
|
|
handle_fault("PIN log exhausted");
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip the guard bits from the current entry word and duplicate each data
|
|
|
|
// bit.
|
|
|
|
wait_random();
|
|
|
|
uint32_t word = entry_log[current] & ~guard_mask;
|
|
|
|
word = ((word >> 1) | word) & LOW_MASK;
|
|
|
|
word = word | (word << 1);
|
|
|
|
// Verify that the entry word has form 0*1*.
|
|
|
|
if ((word & (word + 1)) != 0) {
|
|
|
|
handle_fault("PIN entry log format check");
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (current == 0) {
|
|
|
|
++current;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count the number of set bits in the two current words of the success log.
|
|
|
|
wait_random();
|
|
|
|
*ctr = hamming_weight(success_log[current - 1] ^ entry_log[current - 1]) +
|
|
|
|
hamming_weight(success_log[current] ^ entry_log[current]);
|
|
|
|
return sectrue;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
secbool storage_is_unlocked(void) {
|
|
|
|
if (sectrue != initialized) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-02-04 16:32:58 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
return unlocked;
|
2019-01-30 16:36:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
void storage_lock(void) {
|
|
|
|
unlocked = secfalse;
|
|
|
|
memzero(cached_keys, sizeof(cached_keys));
|
|
|
|
memzero(authentication_sum, sizeof(authentication_sum));
|
2019-01-30 15:34:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 13:44:39 +00:00
|
|
|
secbool check_storage_version(void) {
|
|
|
|
uint32_t version = 0;
|
|
|
|
uint16_t len = 0;
|
|
|
|
if (sectrue !=
|
|
|
|
storage_get_encrypted(VERSION_KEY, &version, sizeof(version), &len) ||
|
|
|
|
len != sizeof(version)) {
|
|
|
|
handle_fault("storage version check");
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-10-23 15:50:46 +00:00
|
|
|
const void *storage_upgraded = NULL;
|
|
|
|
if (sectrue != norcow_get(STORAGE_UPGRADED_KEY, &storage_upgraded, &len) ||
|
|
|
|
len != sizeof(TRUE_WORD)) {
|
|
|
|
handle_fault("storage version check");
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-10-31 13:44:39 +00:00
|
|
|
if (version > norcow_active_version) {
|
2019-10-23 15:50:46 +00:00
|
|
|
// Attack: Storage was downgraded.
|
2019-10-31 13:44:39 +00:00
|
|
|
storage_wipe();
|
2019-10-23 15:50:46 +00:00
|
|
|
handle_fault("storage version check");
|
|
|
|
return secfalse;
|
2019-10-31 13:44:39 +00:00
|
|
|
} else if (version < norcow_active_version) {
|
2019-10-23 15:50:46 +00:00
|
|
|
// Storage was upgraded.
|
|
|
|
if (*(const uint32_t *)storage_upgraded != TRUE_WORD) {
|
|
|
|
// Attack: The upgrade process was bypassed.
|
|
|
|
storage_wipe();
|
|
|
|
handle_fault("storage version check");
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
norcow_set(STORAGE_UPGRADED_KEY, &FALSE_WORD, sizeof(FALSE_WORD));
|
2019-10-31 13:44:39 +00:00
|
|
|
storage_set_encrypted(VERSION_KEY, &norcow_active_version,
|
|
|
|
sizeof(norcow_active_version));
|
2019-10-23 15:50:46 +00:00
|
|
|
} else {
|
|
|
|
// Standard operation. The storage was neither upgraded nor downgraded.
|
|
|
|
if (*(const uint32_t *)storage_upgraded != FALSE_WORD) {
|
|
|
|
// Attack: The upgrade process was launched when it shouldn't have been.
|
|
|
|
storage_wipe();
|
|
|
|
handle_fault("storage version check");
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-10-31 13:44:39 +00:00
|
|
|
}
|
|
|
|
return sectrue;
|
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
static secbool decrypt_dek(const uint8_t *kek, const uint8_t *keiv) {
|
|
|
|
const void *buffer = NULL;
|
|
|
|
uint16_t len = 0;
|
|
|
|
if (sectrue != initialized ||
|
|
|
|
sectrue != norcow_get(EDEK_PVC_KEY, &buffer, &len) ||
|
|
|
|
len != RANDOM_SALT_SIZE + KEYS_SIZE + PVC_SIZE) {
|
|
|
|
handle_fault("no EDEK");
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *ekeys = (const uint8_t *)buffer + RANDOM_SALT_SIZE;
|
|
|
|
const uint32_t *pvc = (const uint32_t *)buffer +
|
|
|
|
(RANDOM_SALT_SIZE + KEYS_SIZE) / sizeof(uint32_t);
|
|
|
|
_Static_assert(((RANDOM_SALT_SIZE + KEYS_SIZE) & 3) == 0, "PVC unaligned");
|
|
|
|
_Static_assert((PVC_SIZE & 3) == 0, "PVC size unaligned");
|
|
|
|
|
2019-10-04 13:00:24 +00:00
|
|
|
uint8_t keys[KEYS_SIZE] = {0};
|
2019-04-18 14:27:27 +00:00
|
|
|
uint8_t tag[POLY1305_TAG_SIZE] __attribute__((aligned(sizeof(uint32_t))));
|
2019-10-04 13:00:24 +00:00
|
|
|
chacha20poly1305_ctx ctx = {0};
|
2019-04-18 14:27:27 +00:00
|
|
|
|
|
|
|
// Decrypt the data encryption key and the storage authentication key and
|
|
|
|
// check the PIN verification code.
|
|
|
|
rfc7539_init(&ctx, kek, keiv);
|
|
|
|
chacha20poly1305_decrypt(&ctx, ekeys, keys, KEYS_SIZE);
|
|
|
|
rfc7539_finish(&ctx, 0, KEYS_SIZE, tag);
|
|
|
|
memzero(&ctx, sizeof(ctx));
|
|
|
|
wait_random();
|
2019-10-29 14:55:58 +00:00
|
|
|
if (secequal32(tag, pvc, PVC_SIZE) != sectrue) {
|
2019-01-24 14:53:45 +00:00
|
|
|
memzero(keys, sizeof(keys));
|
|
|
|
memzero(tag, sizeof(tag));
|
2019-04-18 14:27:27 +00:00
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
memcpy(cached_keys, keys, sizeof(keys));
|
|
|
|
memzero(keys, sizeof(keys));
|
|
|
|
memzero(tag, sizeof(tag));
|
|
|
|
// Check that the authenticated version number matches the norcow version.
|
2019-10-31 13:44:39 +00:00
|
|
|
// NOTE: This also initializes the authentication_sum by calling
|
|
|
|
// storage_get_encrypted() which calls auth_get().
|
|
|
|
return check_storage_version();
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 20:52:09 +00:00
|
|
|
static secbool unlock(uint32_t pin, const uint8_t *ext_salt) {
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != initialized) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-10-31 13:44:39 +00:00
|
|
|
// Check whether the user entered the wipe code.
|
|
|
|
if (sectrue != is_not_wipe_code(pin)) {
|
|
|
|
storage_wipe();
|
|
|
|
error_shutdown("You have entered the", "wipe code. All private",
|
|
|
|
"data has been erased.", NULL);
|
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Get the pin failure counter
|
2019-10-04 13:00:24 +00:00
|
|
|
uint32_t ctr = 0;
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != pin_get_fails(&ctr)) {
|
2019-12-02 15:06:36 +00:00
|
|
|
memzero(&pin, sizeof(pin));
|
2019-04-18 14:27:27 +00:00
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wipe storage if too many failures
|
|
|
|
wait_random();
|
|
|
|
if (ctr >= PIN_MAX_TRIES) {
|
|
|
|
storage_wipe();
|
|
|
|
error_shutdown("Too many wrong PIN", "attempts. Storage has", "been wiped.",
|
|
|
|
NULL);
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sleep for 2^ctr - 1 seconds before checking the PIN.
|
|
|
|
uint32_t wait = (1 << ctr) - 1;
|
|
|
|
ui_total += wait;
|
|
|
|
uint32_t progress = 0;
|
|
|
|
for (ui_rem = ui_total; ui_rem > ui_total - wait; ui_rem--) {
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
if (ui_callback && ui_message) {
|
|
|
|
if (ui_total > 1000000) { // precise enough
|
|
|
|
progress = (ui_total - ui_rem) / (ui_total / 1000);
|
|
|
|
} else {
|
|
|
|
progress = ((ui_total - ui_rem) * 10 + i) * 100 / ui_total;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue == ui_callback(ui_rem, progress, ui_message)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hal_delay(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the random salt from EDEK_PVC_KEY and use it to derive the KEK and
|
|
|
|
// KEIV from the PIN.
|
2019-08-09 20:52:09 +00:00
|
|
|
const void *rand_salt = NULL;
|
2019-04-18 14:27:27 +00:00
|
|
|
uint16_t len = 0;
|
|
|
|
if (sectrue != initialized ||
|
2019-08-09 20:52:09 +00:00
|
|
|
sectrue != norcow_get(EDEK_PVC_KEY, &rand_salt, &len) ||
|
2019-04-18 14:27:27 +00:00
|
|
|
len != RANDOM_SALT_SIZE + KEYS_SIZE + PVC_SIZE) {
|
2019-12-02 15:06:36 +00:00
|
|
|
memzero(&pin, sizeof(pin));
|
2019-04-18 14:27:27 +00:00
|
|
|
handle_fault("no EDEK");
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-10-04 13:00:24 +00:00
|
|
|
uint8_t kek[SHA256_DIGEST_LENGTH] = {0};
|
|
|
|
uint8_t keiv[SHA256_DIGEST_LENGTH] = {0};
|
2019-08-09 20:52:09 +00:00
|
|
|
derive_kek(pin, (const uint8_t *)rand_salt, ext_salt, kek, keiv);
|
2019-04-18 14:27:27 +00:00
|
|
|
memzero(&pin, sizeof(pin));
|
|
|
|
|
|
|
|
// First, we increase PIN fail counter in storage, even before checking the
|
|
|
|
// PIN. If the PIN is correct, we reset the counter afterwards. If not, we
|
|
|
|
// check if this is the last allowed attempt.
|
|
|
|
if (sectrue != storage_pin_fails_increase()) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-03-20 13:49:19 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Check that the PIN fail counter was incremented.
|
2019-10-04 13:00:24 +00:00
|
|
|
uint32_t ctr_ck = 0;
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != pin_get_fails(&ctr_ck) || ctr + 1 != ctr_ck) {
|
|
|
|
handle_fault("PIN counter increment");
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-10-31 13:44:39 +00:00
|
|
|
// Check whether the entered PIN is correct.
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != decrypt_dek(kek, keiv)) {
|
|
|
|
// Wipe storage if too many failures
|
|
|
|
wait_random();
|
|
|
|
if (ctr + 1 >= PIN_MAX_TRIES) {
|
|
|
|
storage_wipe();
|
|
|
|
error_shutdown("Too many wrong PIN", "attempts. Storage has",
|
|
|
|
"been wiped.", NULL);
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
memzero(kek, sizeof(kek));
|
|
|
|
memzero(keiv, sizeof(keiv));
|
2019-03-20 13:49:19 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
unlocked = sectrue;
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Finally set the counter to 0 to indicate success.
|
|
|
|
return pin_fails_reset();
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 20:52:09 +00:00
|
|
|
secbool storage_unlock(uint32_t pin, const uint8_t *ext_salt) {
|
2019-04-18 14:27:27 +00:00
|
|
|
ui_total = DERIVE_SECS;
|
|
|
|
ui_rem = ui_total;
|
|
|
|
if (pin == PIN_EMPTY) {
|
|
|
|
if (ui_message == NULL) {
|
|
|
|
ui_message = STARTING_MSG;
|
2019-02-23 00:52:25 +00:00
|
|
|
} else {
|
2019-04-18 14:27:27 +00:00
|
|
|
ui_message = PROCESSING_MSG;
|
2019-02-23 00:52:25 +00:00
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
} else {
|
|
|
|
ui_message = VERIFYING_PIN_MSG;
|
|
|
|
}
|
2019-08-09 20:52:09 +00:00
|
|
|
return unlock(pin, ext_salt);
|
2019-02-23 00:52:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 14:53:45 +00:00
|
|
|
/*
|
|
|
|
* Finds the encrypted data stored under key and writes its length to len.
|
|
|
|
* If val_dest is not NULL and max_len >= len, then the data is decrypted
|
|
|
|
* to val_dest using cached_dek as the decryption key.
|
|
|
|
*/
|
2019-04-18 14:27:27 +00:00
|
|
|
static secbool storage_get_encrypted(const uint16_t key, void *val_dest,
|
|
|
|
const uint16_t max_len, uint16_t *len) {
|
|
|
|
const void *val_stored = NULL;
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != auth_get(key, &val_stored, len)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (*len < CHACHA20_IV_SIZE + POLY1305_TAG_SIZE) {
|
|
|
|
handle_fault("ciphertext length check");
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
*len -= CHACHA20_IV_SIZE + POLY1305_TAG_SIZE;
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (val_dest == NULL) {
|
|
|
|
return sectrue;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (*len > max_len) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *iv = (const uint8_t *)val_stored;
|
|
|
|
const uint8_t *tag_stored = (const uint8_t *)val_stored + CHACHA20_IV_SIZE;
|
|
|
|
const uint8_t *ciphertext =
|
|
|
|
(const uint8_t *)val_stored + CHACHA20_IV_SIZE + POLY1305_TAG_SIZE;
|
2019-10-04 13:00:24 +00:00
|
|
|
uint8_t tag_computed[POLY1305_TAG_SIZE] = {0};
|
|
|
|
chacha20poly1305_ctx ctx = {0};
|
2019-04-18 14:27:27 +00:00
|
|
|
rfc7539_init(&ctx, cached_dek, iv);
|
|
|
|
rfc7539_auth(&ctx, (const uint8_t *)&key, sizeof(key));
|
|
|
|
chacha20poly1305_decrypt(&ctx, ciphertext, (uint8_t *)val_dest, *len);
|
|
|
|
rfc7539_finish(&ctx, sizeof(key), *len, tag_computed);
|
|
|
|
memzero(&ctx, sizeof(ctx));
|
|
|
|
|
|
|
|
// Verify authentication tag.
|
|
|
|
if (secequal(tag_computed, tag_stored, POLY1305_TAG_SIZE) != sectrue) {
|
|
|
|
memzero(val_dest, max_len);
|
2019-01-24 14:53:45 +00:00
|
|
|
memzero(tag_computed, sizeof(tag_computed));
|
2019-04-18 14:27:27 +00:00
|
|
|
handle_fault("authentication tag check");
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
memzero(tag_computed, sizeof(tag_computed));
|
|
|
|
return sectrue;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finds the data stored under key and writes its length to len. If val_dest is
|
|
|
|
* not NULL and max_len >= len, then the data is copied to val_dest.
|
|
|
|
*/
|
2019-04-18 14:27:27 +00:00
|
|
|
secbool storage_get(const uint16_t key, void *val_dest, const uint16_t max_len,
|
|
|
|
uint16_t *len) {
|
|
|
|
const uint8_t app = key >> 8;
|
|
|
|
// APP == 0 is reserved for PIN related values
|
|
|
|
if (sectrue != initialized || app == APP_STORAGE) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// If the top bit of APP is set, then the value is not encrypted and can be
|
|
|
|
// read from a locked device.
|
|
|
|
if ((app & FLAG_PUBLIC) != 0) {
|
|
|
|
const void *val_stored = NULL;
|
|
|
|
if (sectrue != norcow_get(key, &val_stored, len)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
if (val_dest == NULL) {
|
|
|
|
return sectrue;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
if (*len > max_len) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
memcpy(val_dest, val_stored, *len);
|
2019-11-09 11:25:17 +00:00
|
|
|
return sectrue;
|
2019-04-18 14:27:27 +00:00
|
|
|
} else {
|
|
|
|
if (sectrue != unlocked) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-11-09 11:25:17 +00:00
|
|
|
return storage_get_encrypted(key, val_dest, max_len, len);
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-04-18 14:27:27 +00:00
|
|
|
* Encrypts the data at val using cached_dek as the encryption key and stores
|
|
|
|
* the ciphertext under key.
|
2019-01-24 14:53:45 +00:00
|
|
|
*/
|
2019-04-18 14:27:27 +00:00
|
|
|
static secbool storage_set_encrypted(const uint16_t key, const void *val,
|
|
|
|
const uint16_t len) {
|
|
|
|
if (len > UINT16_MAX - CHACHA20_IV_SIZE - POLY1305_TAG_SIZE) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Preallocate space on the flash storage.
|
|
|
|
if (sectrue !=
|
|
|
|
auth_set(key, NULL, CHACHA20_IV_SIZE + POLY1305_TAG_SIZE + len)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Write the IV to the flash.
|
2019-10-04 13:00:24 +00:00
|
|
|
uint8_t buffer[CHACHA20_BLOCK_SIZE] = {0};
|
2019-04-18 14:27:27 +00:00
|
|
|
random_buffer(buffer, CHACHA20_IV_SIZE);
|
|
|
|
uint16_t offset = 0;
|
|
|
|
if (sectrue != norcow_update_bytes(key, offset, buffer, CHACHA20_IV_SIZE)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
offset += CHACHA20_IV_SIZE + POLY1305_TAG_SIZE;
|
|
|
|
|
|
|
|
// Encrypt all blocks except for the last one.
|
2019-10-04 13:00:24 +00:00
|
|
|
chacha20poly1305_ctx ctx = {0};
|
2019-04-18 14:27:27 +00:00
|
|
|
rfc7539_init(&ctx, cached_dek, buffer);
|
|
|
|
rfc7539_auth(&ctx, (const uint8_t *)&key, sizeof(key));
|
2019-10-04 13:00:24 +00:00
|
|
|
size_t i = 0;
|
2019-04-18 14:27:27 +00:00
|
|
|
for (i = 0; i + CHACHA20_BLOCK_SIZE < len;
|
|
|
|
i += CHACHA20_BLOCK_SIZE, offset += CHACHA20_BLOCK_SIZE) {
|
|
|
|
chacha20poly1305_encrypt(&ctx, ((const uint8_t *)val) + i, buffer,
|
|
|
|
CHACHA20_BLOCK_SIZE);
|
|
|
|
if (sectrue !=
|
|
|
|
norcow_update_bytes(key, offset, buffer, CHACHA20_BLOCK_SIZE)) {
|
|
|
|
memzero(&ctx, sizeof(ctx));
|
|
|
|
memzero(buffer, sizeof(buffer));
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt final block and compute message authentication tag.
|
|
|
|
chacha20poly1305_encrypt(&ctx, ((const uint8_t *)val) + i, buffer, len - i);
|
|
|
|
secbool ret = norcow_update_bytes(key, offset, buffer, len - i);
|
|
|
|
if (sectrue == ret) {
|
|
|
|
rfc7539_finish(&ctx, sizeof(key), len, buffer);
|
|
|
|
ret = norcow_update_bytes(key, CHACHA20_IV_SIZE, buffer, POLY1305_TAG_SIZE);
|
|
|
|
}
|
|
|
|
memzero(&ctx, sizeof(ctx));
|
|
|
|
memzero(buffer, sizeof(buffer));
|
|
|
|
return ret;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
secbool storage_set(const uint16_t key, const void *val, const uint16_t len) {
|
|
|
|
const uint8_t app = key >> 8;
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// APP == 0 is reserved for PIN related values
|
|
|
|
if (sectrue != initialized || app == APP_STORAGE) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != unlocked && (app & FLAGS_WRITE) != FLAGS_WRITE) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
secbool ret = secfalse;
|
|
|
|
if ((app & FLAG_PUBLIC) != 0) {
|
|
|
|
ret = norcow_set(key, val, len);
|
|
|
|
} else {
|
|
|
|
ret = storage_set_encrypted(key, val, len);
|
|
|
|
}
|
|
|
|
return ret;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
secbool storage_delete(const uint16_t key) {
|
|
|
|
const uint8_t app = key >> 8;
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// APP == 0 is reserved for storage related values
|
|
|
|
if (sectrue != initialized || app == APP_STORAGE) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-02-08 16:50:27 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != unlocked && (app & FLAGS_WRITE) != FLAGS_WRITE) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
secbool ret = norcow_delete(key);
|
|
|
|
if (sectrue == ret) {
|
|
|
|
ret = auth_update(key);
|
|
|
|
}
|
|
|
|
return ret;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
secbool storage_set_counter(const uint16_t key, const uint32_t count) {
|
|
|
|
const uint8_t app = key >> 8;
|
|
|
|
if ((app & FLAG_PUBLIC) == 0) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The count is stored as a 32-bit integer followed by a tail of "1" bits,
|
|
|
|
// which is used as a tally.
|
2019-10-04 13:00:24 +00:00
|
|
|
uint32_t value[1 + COUNTER_TAIL_WORDS] = {0};
|
2019-04-18 14:27:27 +00:00
|
|
|
memset(value, 0xff, sizeof(value));
|
|
|
|
value[0] = count;
|
|
|
|
return storage_set(key, value, sizeof(value));
|
2019-02-08 19:24:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
secbool storage_next_counter(const uint16_t key, uint32_t *count) {
|
|
|
|
const uint8_t app = key >> 8;
|
|
|
|
// APP == 0 is reserved for PIN related values
|
|
|
|
if (sectrue != initialized || app == APP_STORAGE ||
|
|
|
|
(app & FLAG_PUBLIC) == 0) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-02-08 19:24:55 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != unlocked && (app & FLAGS_WRITE) != FLAGS_WRITE) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-02-08 19:24:55 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
uint16_t len = 0;
|
|
|
|
const uint32_t *val_stored = NULL;
|
|
|
|
if (sectrue != norcow_get(key, (const void **)&val_stored, &len)) {
|
|
|
|
*count = 0;
|
|
|
|
return storage_set_counter(key, 0);
|
|
|
|
}
|
2019-02-08 19:24:55 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (len < sizeof(uint32_t) || len % sizeof(uint32_t) != 0) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
uint16_t len_words = len / sizeof(uint32_t);
|
|
|
|
|
|
|
|
uint16_t i = 1;
|
|
|
|
while (i < len_words && val_stored[i] == 0) {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
*count = val_stored[0] + 1 + 32 * (i - 1);
|
|
|
|
|
|
|
|
if (i < len_words) {
|
|
|
|
*count += hamming_weight(~val_stored[i]);
|
|
|
|
return norcow_update_word(key, sizeof(uint32_t) * i, val_stored[i] >> 1);
|
|
|
|
} else {
|
|
|
|
return storage_set_counter(key, *count);
|
|
|
|
}
|
2019-02-08 19:24:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
secbool storage_has_pin(void) {
|
|
|
|
if (sectrue != initialized) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
const void *val = NULL;
|
2019-10-04 13:00:24 +00:00
|
|
|
uint16_t len = 0;
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != norcow_get(PIN_NOT_SET_KEY, &val, &len) ||
|
|
|
|
(len > 0 && *(uint8_t *)val != FALSE_BYTE)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
return sectrue;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
uint32_t storage_get_pin_rem(void) {
|
|
|
|
if (sectrue != initialized) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-02-04 16:32:58 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
uint32_t ctr = 0;
|
|
|
|
if (sectrue != pin_get_fails(&ctr)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return PIN_MAX_TRIES - ctr;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 20:52:09 +00:00
|
|
|
secbool storage_change_pin(uint32_t oldpin, uint32_t newpin,
|
|
|
|
const uint8_t *old_ext_salt,
|
|
|
|
const uint8_t *new_ext_salt) {
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != initialized) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-02-23 00:52:25 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
ui_total = 2 * DERIVE_SECS;
|
|
|
|
ui_rem = ui_total;
|
|
|
|
ui_message = (oldpin != PIN_EMPTY && newpin == PIN_EMPTY) ? VERIFYING_PIN_MSG
|
|
|
|
: PROCESSING_MSG;
|
2019-02-23 00:52:25 +00:00
|
|
|
|
2019-08-09 20:52:09 +00:00
|
|
|
if (sectrue != unlock(oldpin, old_ext_salt)) {
|
2019-04-18 14:27:27 +00:00
|
|
|
return secfalse;
|
|
|
|
}
|
2019-08-09 20:52:09 +00:00
|
|
|
secbool ret = set_pin(newpin, new_ext_salt);
|
2019-04-18 14:27:27 +00:00
|
|
|
memzero(&oldpin, sizeof(oldpin));
|
|
|
|
memzero(&newpin, sizeof(newpin));
|
|
|
|
return ret;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 14:58:53 +00:00
|
|
|
secbool storage_has_wipe_code(void) {
|
|
|
|
if (sectrue != initialized || sectrue != unlocked) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_not_wipe_code(WIPE_CODE_EMPTY);
|
|
|
|
}
|
|
|
|
|
2019-10-31 13:44:39 +00:00
|
|
|
secbool storage_change_wipe_code(uint32_t pin, const uint8_t *ext_salt,
|
|
|
|
uint32_t wipe_code) {
|
|
|
|
if (sectrue != initialized || (pin != PIN_EMPTY && pin == wipe_code)) {
|
|
|
|
memzero(&pin, sizeof(pin));
|
|
|
|
memzero(&wipe_code, sizeof(wipe_code));
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_total = DERIVE_SECS;
|
|
|
|
ui_rem = ui_total;
|
|
|
|
ui_message = (pin != PIN_EMPTY && wipe_code == PIN_EMPTY) ? VERIFYING_PIN_MSG
|
|
|
|
: PROCESSING_MSG;
|
|
|
|
|
|
|
|
secbool ret = secfalse;
|
|
|
|
if (sectrue == unlock(pin, ext_salt)) {
|
|
|
|
ret = set_wipe_code(wipe_code);
|
|
|
|
}
|
|
|
|
memzero(&pin, sizeof(pin));
|
|
|
|
memzero(&wipe_code, sizeof(wipe_code));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
void storage_wipe(void) {
|
|
|
|
norcow_wipe();
|
|
|
|
norcow_active_version = NORCOW_VERSION;
|
|
|
|
memzero(authentication_sum, sizeof(authentication_sum));
|
|
|
|
memzero(cached_keys, sizeof(cached_keys));
|
|
|
|
init_wiped_storage();
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
static void __handle_fault(const char *msg, const char *file, int line,
|
|
|
|
const char *func) {
|
|
|
|
static secbool in_progress = secfalse;
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// If fault handling is already in progress, then we are probably facing a
|
|
|
|
// fault injection attack, so wipe.
|
|
|
|
if (secfalse != in_progress) {
|
|
|
|
storage_wipe();
|
|
|
|
__fatal_error("Fault detected", msg, file, line, func);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use the PIN fail counter as a fault counter. Increment the counter,
|
|
|
|
// check that it was incremented and halt.
|
|
|
|
in_progress = sectrue;
|
2019-10-04 13:00:24 +00:00
|
|
|
uint32_t ctr = 0;
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != pin_get_fails(&ctr)) {
|
|
|
|
storage_wipe();
|
|
|
|
__fatal_error("Fault detected", msg, file, line, func);
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != storage_pin_fails_increase()) {
|
|
|
|
storage_wipe();
|
2019-02-07 13:03:25 +00:00
|
|
|
__fatal_error("Fault detected", msg, file, line, func);
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 13:00:24 +00:00
|
|
|
uint32_t ctr_new = 0;
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue != pin_get_fails(&ctr_new) || ctr + 1 != ctr_new) {
|
|
|
|
storage_wipe();
|
|
|
|
}
|
|
|
|
__fatal_error("Fault detected", msg, file, line, func);
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-04-18 14:27:27 +00:00
|
|
|
* Reads the PIN fail counter in version 0 format. Returns the current number of
|
|
|
|
* failed PIN entries.
|
2019-01-24 14:53:45 +00:00
|
|
|
*/
|
2019-04-18 14:27:27 +00:00
|
|
|
static secbool v0_pin_get_fails(uint32_t *ctr) {
|
|
|
|
const uint16_t V0_PIN_FAIL_KEY = 0x0001;
|
|
|
|
// The PIN_FAIL_KEY points to an area of words, initialized to
|
|
|
|
// 0xffffffff (meaning no PIN failures). The first non-zero word
|
|
|
|
// in this area is the current PIN failure counter. If PIN_FAIL_KEY
|
|
|
|
// has no configuration or is empty, the PIN failure counter is 0.
|
|
|
|
// We rely on the fact that flash allows to clear bits and we clear one
|
|
|
|
// bit to indicate PIN failure. On success, the word is set to 0,
|
|
|
|
// indicating that the next word is the PIN failure counter.
|
|
|
|
|
|
|
|
// Find the current pin failure counter
|
|
|
|
const void *val = NULL;
|
|
|
|
uint16_t len = 0;
|
|
|
|
if (secfalse != norcow_get(V0_PIN_FAIL_KEY, &val, &len)) {
|
|
|
|
for (unsigned int i = 0; i < len / sizeof(uint32_t); i++) {
|
|
|
|
uint32_t word = ((const uint32_t *)val)[i];
|
|
|
|
if (word != 0) {
|
|
|
|
*ctr = hamming_weight(~word);
|
|
|
|
return sectrue;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// No PIN failures
|
|
|
|
*ctr = 0;
|
|
|
|
return sectrue;
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
static secbool storage_upgrade(void) {
|
|
|
|
const uint16_t V0_PIN_KEY = 0x0000;
|
|
|
|
const uint16_t V0_PIN_FAIL_KEY = 0x0001;
|
|
|
|
uint16_t key = 0;
|
|
|
|
uint16_t len = 0;
|
|
|
|
const void *val = NULL;
|
2019-10-31 13:44:39 +00:00
|
|
|
secbool ret = secfalse;
|
2019-04-18 14:27:27 +00:00
|
|
|
|
|
|
|
if (norcow_active_version == 0) {
|
|
|
|
random_buffer(cached_keys, sizeof(cached_keys));
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Initialize the storage authentication tag.
|
|
|
|
auth_init();
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Set the new storage version number.
|
2019-10-31 13:44:39 +00:00
|
|
|
uint32_t version = 1;
|
2019-04-18 14:27:27 +00:00
|
|
|
if (sectrue !=
|
|
|
|
storage_set_encrypted(VERSION_KEY, &version, sizeof(version))) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Set EDEK_PVC_KEY and PIN_NOT_SET_KEY.
|
|
|
|
ui_total = DERIVE_SECS;
|
|
|
|
ui_rem = ui_total;
|
|
|
|
ui_message = PROCESSING_MSG;
|
|
|
|
if (sectrue == norcow_get(V0_PIN_KEY, &val, &len)) {
|
2019-08-09 20:52:09 +00:00
|
|
|
set_pin(*(const uint32_t *)val, NULL);
|
2019-04-18 14:27:27 +00:00
|
|
|
} else {
|
2019-08-09 20:52:09 +00:00
|
|
|
set_pin(PIN_EMPTY, NULL);
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Convert PIN failure counter.
|
|
|
|
uint32_t fails = 0;
|
|
|
|
v0_pin_get_fails(&fails);
|
|
|
|
pin_logs_init(fails);
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
// Copy the remaining entries (encrypting the protected ones).
|
|
|
|
uint32_t offset = 0;
|
|
|
|
while (sectrue == norcow_get_next(&offset, &key, &val, &len)) {
|
|
|
|
if (key == V0_PIN_KEY || key == V0_PIN_FAIL_KEY) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if (((key >> 8) & FLAG_PUBLIC) != 0) {
|
|
|
|
ret = norcow_set(key, val, len);
|
|
|
|
} else {
|
|
|
|
ret = storage_set_encrypted(key, val, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sectrue != ret) {
|
2019-01-24 14:53:45 +00:00
|
|
|
return secfalse;
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
unlocked = secfalse;
|
|
|
|
memzero(cached_keys, sizeof(cached_keys));
|
|
|
|
} else {
|
2019-10-31 13:44:39 +00:00
|
|
|
// Copy all entries.
|
|
|
|
uint32_t offset = 0;
|
|
|
|
while (sectrue == norcow_get_next(&offset, &key, &val, &len)) {
|
|
|
|
if (sectrue != norcow_set(key, val, len)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (norcow_active_version <= 1) {
|
|
|
|
if (sectrue != set_wipe_code(WIPE_CODE_EMPTY)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-04-18 14:27:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 15:50:46 +00:00
|
|
|
if (sectrue !=
|
|
|
|
norcow_set(STORAGE_UPGRADED_KEY, &TRUE_WORD, sizeof(TRUE_WORD))) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
norcow_active_version = NORCOW_VERSION;
|
|
|
|
return norcow_upgrade_finish();
|
2019-01-24 14:53:45 +00:00
|
|
|
}
|