mirror of
https://github.com/polhenarejos/pico-hsm.git
synced 2024-09-20 11:20:08 +00:00
Added support for HD symmetric ciphering.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
parent
6af4cc7fec
commit
f880ee6c93
@ -35,6 +35,9 @@
|
|||||||
#include "mbedtls/oid.h"
|
#include "mbedtls/oid.h"
|
||||||
#include "mbedtls/ccm.h"
|
#include "mbedtls/ccm.h"
|
||||||
|
|
||||||
|
extern mbedtls_ecp_keypair hd_context;
|
||||||
|
extern uint8_t hd_keytype;
|
||||||
|
|
||||||
/* This is copied from pkcs5.c Mbedtls */
|
/* This is copied from pkcs5.c Mbedtls */
|
||||||
/** Unfortunately it is declared as static, so I cannot call it. **/
|
/** Unfortunately it is declared as static, so I cannot call it. **/
|
||||||
|
|
||||||
@ -165,20 +168,22 @@ int cmd_cipher_sym() {
|
|||||||
if (!isUserAuthenticated) {
|
if (!isUserAuthenticated) {
|
||||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||||
}
|
}
|
||||||
|
if (wait_button_pressed() == true) { // timeout
|
||||||
|
return SW_SECURE_MESSAGE_EXEC_ERROR();
|
||||||
|
}
|
||||||
file_t *ef = search_dynamic_file((KEY_PREFIX << 8) | key_id);
|
file_t *ef = search_dynamic_file((KEY_PREFIX << 8) | key_id);
|
||||||
|
if (hd_keytype == 0) {
|
||||||
if (!ef) {
|
if (!ef) {
|
||||||
return SW_FILE_NOT_FOUND();
|
return SW_FILE_NOT_FOUND();
|
||||||
}
|
}
|
||||||
if (key_has_purpose(ef, algo) == false) {
|
if (key_has_purpose(ef, algo) == false) {
|
||||||
return SW_CONDITIONS_NOT_SATISFIED();
|
return SW_CONDITIONS_NOT_SATISFIED();
|
||||||
}
|
}
|
||||||
if (wait_button_pressed() == true) { // timeout
|
|
||||||
return SW_SECURE_MESSAGE_EXEC_ERROR();
|
|
||||||
}
|
}
|
||||||
int key_size = file_get_size(ef);
|
int key_size = file_get_size(ef);
|
||||||
uint8_t kdata[64]; //maximum AES key size
|
uint8_t kdata[64]; //maximum AES key size
|
||||||
memcpy(kdata, file_get_data(ef), key_size);
|
memcpy(kdata, file_get_data(ef), key_size);
|
||||||
if (mkek_decrypt(kdata, key_size) != 0) {
|
if (hd_keytype == 0 && mkek_decrypt(kdata, key_size) != 0) {
|
||||||
return SW_EXEC_ERROR();
|
return SW_EXEC_ERROR();
|
||||||
}
|
}
|
||||||
if (algo == ALGO_AES_CBC_ENCRYPT || algo == ALGO_AES_CBC_DECRYPT) {
|
if (algo == ALGO_AES_CBC_ENCRYPT || algo == ALGO_AES_CBC_DECRYPT) {
|
||||||
@ -467,8 +472,6 @@ int cmd_cipher_sym() {
|
|||||||
mbedtls_aes_context ctx;
|
mbedtls_aes_context ctx;
|
||||||
int r = 0;
|
int r = 0;
|
||||||
mbedtls_aes_init(&ctx);
|
mbedtls_aes_init(&ctx);
|
||||||
uint8_t tmp_iv[16];
|
|
||||||
memset(tmp_iv, 0, sizeof(tmp_iv));
|
|
||||||
if (iv == NULL || iv_len == 0) {
|
if (iv == NULL || iv_len == 0) {
|
||||||
iv = tmp_iv;
|
iv = tmp_iv;
|
||||||
iv_len = sizeof(tmp_iv);
|
iv_len = sizeof(tmp_iv);
|
||||||
@ -617,9 +620,7 @@ int cmd_cipher_sym() {
|
|||||||
res_APDU_size = enc_len - 16;
|
res_APDU_size = enc_len - 16;
|
||||||
}
|
}
|
||||||
mbedtls_ccm_free(&gctx);
|
mbedtls_ccm_free(&gctx);
|
||||||
printf("r %d\n", r);
|
if (r != 0) {
|
||||||
if (r != 0)
|
|
||||||
{
|
|
||||||
return SW_EXEC_ERROR();
|
return SW_EXEC_ERROR();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -657,6 +658,50 @@ int cmd_cipher_sym() {
|
|||||||
}
|
}
|
||||||
res_APDU_size = enc_len;
|
res_APDU_size = enc_len;
|
||||||
}
|
}
|
||||||
|
else if (memcmp(oid, OID_HD, 11) == 0) {
|
||||||
|
mbedtls_aes_context ctx;
|
||||||
|
int r = 0;
|
||||||
|
uint8_t mode =
|
||||||
|
(algo == ALGO_EXT_CIPHER_ENCRYPT ? MBEDTLS_AES_ENCRYPT : MBEDTLS_AES_DECRYPT),
|
||||||
|
secret[64] = { 0 };
|
||||||
|
mbedtls_aes_init(&ctx);
|
||||||
|
if (hd_keytype != 0x3) {
|
||||||
|
return SW_INCORRECT_PARAMS();
|
||||||
|
}
|
||||||
|
key_size = 32;
|
||||||
|
mbedtls_mpi_write_binary(&hd_context.d, kdata, key_size);
|
||||||
|
r = mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA512),
|
||||||
|
kdata,
|
||||||
|
key_size,
|
||||||
|
aad,
|
||||||
|
aad_len,
|
||||||
|
secret);
|
||||||
|
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||||
|
if (r != 0) {
|
||||||
|
return SW_EXEC_ERROR();
|
||||||
|
}
|
||||||
|
if (iv == tmp_iv || iv_len == 0) {
|
||||||
|
iv = secret + 32;
|
||||||
|
iv_len = 16;
|
||||||
|
}
|
||||||
|
if (algo == ALGO_EXT_CIPHER_ENCRYPT) {
|
||||||
|
r = mbedtls_aes_setkey_enc(&ctx, secret, key_size * 8);
|
||||||
|
}
|
||||||
|
else if (algo == ALGO_EXT_CIPHER_DECRYPT) {
|
||||||
|
r = mbedtls_aes_setkey_dec(&ctx, secret, key_size * 8);
|
||||||
|
}
|
||||||
|
if (r != 0) {
|
||||||
|
return SW_EXEC_ERROR();
|
||||||
|
}
|
||||||
|
r = mbedtls_aes_crypt_cbc(&ctx, mode, enc_len, iv, enc, res_APDU);
|
||||||
|
mbedtls_aes_free(&ctx);
|
||||||
|
if (r != 0) {
|
||||||
|
return SW_EXEC_ERROR();
|
||||||
|
}
|
||||||
|
res_APDU_size = enc_len;
|
||||||
|
mbedtls_ecdsa_free(&hd_context);
|
||||||
|
hd_keytype = 0;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
return SW_WRONG_DATA();
|
return SW_WRONG_DATA();
|
||||||
}
|
}
|
||||||
|
@ -172,4 +172,6 @@
|
|||||||
#define OID_AES128_XTS OID_IEEE_ALG "\x01"
|
#define OID_AES128_XTS OID_IEEE_ALG "\x01"
|
||||||
#define OID_AES256_XTS OID_IEEE_ALG "\x02"
|
#define OID_AES256_XTS OID_IEEE_ALG "\x02"
|
||||||
|
|
||||||
|
#define OID_HD "\x2B\x06\x01\x04\x01\x83\xA8\x78\x05\x8D\x6B"
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user