2021-09-15 12:59:49 +03:00
|
|
|
#include <furi-hal-crypto.h>
|
2021-11-04 20:26:41 +03:00
|
|
|
#include <furi-hal-bt.h>
|
2021-09-15 12:59:49 +03:00
|
|
|
#include <furi.h>
|
|
|
|
#include <shci.h>
|
|
|
|
|
2021-11-12 16:04:35 +03:00
|
|
|
#define TAG "FuriHalCrypto"
|
|
|
|
|
2021-09-15 12:59:49 +03:00
|
|
|
CRYP_HandleTypeDef crypt;
|
|
|
|
|
|
|
|
void furi_hal_crypto_init() {
|
2021-11-12 16:04:35 +03:00
|
|
|
FURI_LOG_I(TAG, "Init OK");
|
2021-09-15 12:59:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_crypto_store_add_key(FuriHalCryptoKey* key, uint8_t* slot) {
|
|
|
|
furi_assert(key);
|
|
|
|
furi_assert(slot);
|
|
|
|
|
2021-11-04 20:26:41 +03:00
|
|
|
if(!furi_hal_bt_is_alive()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-15 12:59:49 +03:00
|
|
|
SHCI_C2_FUS_StoreUsrKey_Cmd_Param_t pParam;
|
2021-09-17 12:21:08 +03:00
|
|
|
size_t key_data_size = 0;
|
2021-09-15 12:59:49 +03:00
|
|
|
|
2021-11-01 16:11:25 +03:00
|
|
|
if(key->type == FuriHalCryptoKeyTypeMaster) {
|
2021-09-15 12:59:49 +03:00
|
|
|
pParam.KeyType = KEYTYPE_MASTER;
|
2021-11-01 16:11:25 +03:00
|
|
|
} else if(key->type == FuriHalCryptoKeyTypeSimple) {
|
2021-09-15 12:59:49 +03:00
|
|
|
pParam.KeyType = KEYTYPE_SIMPLE;
|
2021-11-01 16:11:25 +03:00
|
|
|
} else if(key->type == FuriHalCryptoKeyTypeEncrypted) {
|
2021-09-15 12:59:49 +03:00
|
|
|
pParam.KeyType = KEYTYPE_ENCRYPTED;
|
2021-09-17 12:21:08 +03:00
|
|
|
key_data_size += 12;
|
2021-09-15 12:59:49 +03:00
|
|
|
} else {
|
|
|
|
furi_crash("Incorrect key type");
|
|
|
|
}
|
|
|
|
|
2021-11-01 16:11:25 +03:00
|
|
|
if(key->size == FuriHalCryptoKeySize128) {
|
2021-09-15 12:59:49 +03:00
|
|
|
pParam.KeySize = KEYSIZE_16;
|
2021-09-17 12:21:08 +03:00
|
|
|
key_data_size += 16;
|
2021-11-01 16:11:25 +03:00
|
|
|
} else if(key->size == FuriHalCryptoKeySize256) {
|
2021-09-15 12:59:49 +03:00
|
|
|
pParam.KeySize = KEYSIZE_32;
|
2021-09-17 12:21:08 +03:00
|
|
|
key_data_size += 32;
|
2021-09-15 12:59:49 +03:00
|
|
|
} else {
|
|
|
|
furi_crash("Incorrect key size");
|
|
|
|
}
|
|
|
|
|
2021-09-17 12:21:08 +03:00
|
|
|
memcpy(pParam.KeyData, key->data, key_data_size);
|
|
|
|
|
2021-09-15 12:59:49 +03:00
|
|
|
return SHCI_C2_FUS_StoreUsrKey(&pParam, slot) == SHCI_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_crypto_store_load_key(uint8_t slot, const uint8_t* iv) {
|
|
|
|
furi_assert(slot > 0 && slot <= 100);
|
|
|
|
|
2021-11-04 20:26:41 +03:00
|
|
|
if(!furi_hal_bt_is_alive()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-15 12:59:49 +03:00
|
|
|
crypt.Instance = AES1;
|
|
|
|
crypt.Init.DataType = CRYP_DATATYPE_32B;
|
|
|
|
crypt.Init.KeySize = CRYP_KEYSIZE_256B;
|
|
|
|
crypt.Init.Algorithm = CRYP_AES_CBC;
|
|
|
|
crypt.Init.pInitVect = (uint32_t*)iv;
|
2021-11-01 16:11:25 +03:00
|
|
|
crypt.Init.KeyIVConfigSkip = CRYP_KEYIVCONFIG_ONCE;
|
2021-09-15 12:59:49 +03:00
|
|
|
crypt.Init.pKey = NULL;
|
|
|
|
|
|
|
|
furi_check(HAL_CRYP_Init(&crypt) == HAL_OK);
|
|
|
|
|
2021-11-01 16:11:25 +03:00
|
|
|
if(SHCI_C2_FUS_LoadUsrKey(slot) == SHCI_Success) {
|
2021-09-15 12:59:49 +03:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
furi_check(HAL_CRYP_DeInit(&crypt) == HAL_OK);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_crypto_store_unload_key(uint8_t slot) {
|
2021-11-04 20:26:41 +03:00
|
|
|
if(!furi_hal_bt_is_alive()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-15 12:59:49 +03:00
|
|
|
furi_check(HAL_CRYP_DeInit(&crypt) == HAL_OK);
|
|
|
|
return SHCI_C2_FUS_UnloadUsrKey(slot) == SHCI_Success;
|
|
|
|
}
|
|
|
|
|
2021-11-01 16:11:25 +03:00
|
|
|
bool furi_hal_crypto_encrypt(const uint8_t* input, uint8_t* output, size_t size) {
|
|
|
|
return HAL_CRYP_Encrypt(&crypt, (uint32_t*)input, size / 4, (uint32_t*)output, 1000) == HAL_OK;
|
2021-09-15 12:59:49 +03:00
|
|
|
}
|
|
|
|
|
2021-11-01 16:11:25 +03:00
|
|
|
bool furi_hal_crypto_decrypt(const uint8_t* input, uint8_t* output, size_t size) {
|
|
|
|
return HAL_CRYP_Decrypt(&crypt, (uint32_t*)input, size / 4, (uint32_t*)output, 1000) == HAL_OK;
|
2021-09-15 12:59:49 +03:00
|
|
|
}
|