mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-08 14:06:11 +02:00
Harmonizing coding style.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
124
src/eac.c
124
src/eac.c
@@ -32,16 +32,25 @@ static uint8_t sm_iv[16];
|
||||
size_t sm_session_pin_len = 0;
|
||||
uint8_t sm_session_pin[16];
|
||||
|
||||
bool is_secured_apdu() {
|
||||
return (CLA(apdu) & 0xC);
|
||||
bool is_secured_apdu()
|
||||
{
|
||||
return CLA(apdu) & 0xC;
|
||||
}
|
||||
|
||||
void sm_derive_key(const uint8_t *input, size_t input_len, uint8_t counter, const uint8_t *nonce, size_t nonce_len, uint8_t *out) {
|
||||
uint8_t *b = (uint8_t *)calloc(1, input_len+nonce_len+4);
|
||||
if (input)
|
||||
void sm_derive_key(const uint8_t *input,
|
||||
size_t input_len,
|
||||
uint8_t counter,
|
||||
const uint8_t *nonce,
|
||||
size_t nonce_len,
|
||||
uint8_t *out)
|
||||
{
|
||||
uint8_t *b = (uint8_t *) calloc(1, input_len+nonce_len+4);
|
||||
if (input) {
|
||||
memcpy(b, input, input_len);
|
||||
if (nonce)
|
||||
}
|
||||
if (nonce) {
|
||||
memcpy(b+input_len, nonce, nonce_len);
|
||||
}
|
||||
b[input_len+nonce_len+3] = counter;
|
||||
uint8_t digest[20];
|
||||
generic_hash(MBEDTLS_MD_SHA1, b, input_len+nonce_len+4, digest);
|
||||
@@ -49,7 +58,8 @@ void sm_derive_key(const uint8_t *input, size_t input_len, uint8_t counter, cons
|
||||
free(b);
|
||||
}
|
||||
|
||||
void sm_derive_all_keys(const uint8_t *derived, size_t derived_len) {
|
||||
void sm_derive_all_keys(const uint8_t *derived, size_t derived_len)
|
||||
{
|
||||
memcpy(nonce, random_bytes_get(8), 8);
|
||||
sm_derive_key(derived, derived_len, 1, nonce, sizeof(nonce), sm_kenc);
|
||||
sm_derive_key(derived, derived_len, 2, nonce, sizeof(nonce), sm_kmac);
|
||||
@@ -60,36 +70,50 @@ void sm_derive_all_keys(const uint8_t *derived, size_t derived_len) {
|
||||
sm_session_pin_len = 0;
|
||||
}
|
||||
|
||||
void sm_set_protocol(MSE_protocol proto) {
|
||||
void sm_set_protocol(MSE_protocol proto)
|
||||
{
|
||||
sm_protocol = proto;
|
||||
if (proto == MSE_AES)
|
||||
if (proto == MSE_AES) {
|
||||
sm_blocksize = 16;
|
||||
else if (proto == MSE_3DES)
|
||||
} else if (proto == MSE_3DES) {
|
||||
sm_blocksize = 8;
|
||||
}
|
||||
}
|
||||
|
||||
MSE_protocol sm_get_protocol() {
|
||||
MSE_protocol sm_get_protocol()
|
||||
{
|
||||
return sm_protocol;
|
||||
}
|
||||
|
||||
uint8_t *sm_get_nonce() {
|
||||
uint8_t *sm_get_nonce()
|
||||
{
|
||||
return nonce;
|
||||
}
|
||||
|
||||
int sm_sign(uint8_t *in, size_t in_len, uint8_t *out) {
|
||||
return mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB), sm_kmac, 128, in, in_len, out);
|
||||
int sm_sign(uint8_t *in, size_t in_len, uint8_t *out)
|
||||
{
|
||||
return mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB),
|
||||
sm_kmac,
|
||||
128,
|
||||
in,
|
||||
in_len,
|
||||
out);
|
||||
}
|
||||
|
||||
int sm_unwrap() {
|
||||
int sm_unwrap()
|
||||
{
|
||||
uint8_t sm_indicator = (CLA(apdu) >> 2) & 0x3;
|
||||
if (sm_indicator == 0)
|
||||
if (sm_indicator == 0) {
|
||||
return CCID_OK;
|
||||
}
|
||||
int r = sm_verify();
|
||||
if (r != CCID_OK)
|
||||
if (r != CCID_OK) {
|
||||
return r;
|
||||
}
|
||||
int le = sm_get_le();
|
||||
if (le >= 0)
|
||||
if (le >= 0) {
|
||||
apdu.ne = le;
|
||||
}
|
||||
uint8_t *body = NULL;
|
||||
size_t body_size = 0;
|
||||
bool is87 = false;
|
||||
@@ -117,14 +141,16 @@ int sm_unwrap() {
|
||||
aes_decrypt(sm_kenc, sm_iv, 128, HSM_AES_MODE_CBC, body, body_size);
|
||||
memmove(apdu.data, body, body_size);
|
||||
apdu.nc = sm_remove_padding(apdu.data, body_size);
|
||||
DEBUG_PAYLOAD(apdu.data, (int)apdu.nc);
|
||||
DEBUG_PAYLOAD(apdu.data, (int) apdu.nc);
|
||||
return CCID_OK;
|
||||
}
|
||||
|
||||
int sm_wrap() {
|
||||
int sm_wrap()
|
||||
{
|
||||
uint8_t sm_indicator = (CLA(apdu) >> 2) & 0x3;
|
||||
if (sm_indicator == 0)
|
||||
if (sm_indicator == 0) {
|
||||
return CCID_OK;
|
||||
}
|
||||
uint8_t input[1024];
|
||||
size_t input_len = 0;
|
||||
memset(input, 0, sizeof(input));
|
||||
@@ -133,8 +159,9 @@ int sm_wrap() {
|
||||
mbedtls_mpi_add_int(&ssc, &sm_mSSC, 1);
|
||||
mbedtls_mpi_copy(&sm_mSSC, &ssc);
|
||||
int r = mbedtls_mpi_write_binary(&ssc, input, sm_blocksize);
|
||||
if (r != 0)
|
||||
if (r != 0) {
|
||||
return CCID_EXEC_ERROR;
|
||||
}
|
||||
input_len += sm_blocksize;
|
||||
mbedtls_mpi_free(&ssc);
|
||||
if (res_APDU_size > 0) {
|
||||
@@ -151,14 +178,12 @@ int sm_wrap() {
|
||||
memmove(res_APDU+2, res_APDU, res_APDU_size);
|
||||
res_APDU[1] = res_APDU_size;
|
||||
res_APDU_size += 2;
|
||||
}
|
||||
else if (res_APDU_size < 256) {
|
||||
} else if (res_APDU_size < 256) {
|
||||
memmove(res_APDU+3, res_APDU, res_APDU_size);
|
||||
res_APDU[1] = 0x81;
|
||||
res_APDU[2] = res_APDU_size;
|
||||
res_APDU_size += 3;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
memmove(res_APDU+4, res_APDU, res_APDU_size);
|
||||
res_APDU[1] = 0x82;
|
||||
res_APDU[2] = res_APDU_size >> 8;
|
||||
@@ -179,27 +204,31 @@ int sm_wrap() {
|
||||
res_APDU[res_APDU_size++] = 0x8E;
|
||||
res_APDU[res_APDU_size++] = 8;
|
||||
res_APDU_size += 8;
|
||||
if (apdu.ne > 0)
|
||||
if (apdu.ne > 0) {
|
||||
apdu.ne = res_APDU_size;
|
||||
}
|
||||
return CCID_OK;
|
||||
}
|
||||
|
||||
int sm_get_le() {
|
||||
int sm_get_le()
|
||||
{
|
||||
uint16_t tag = 0x0;
|
||||
uint8_t *tag_data = NULL, *p = NULL;
|
||||
size_t tag_len = 0;
|
||||
while (walk_tlv(apdu.data, apdu.nc, &p, &tag, &tag_len, &tag_data)) {
|
||||
if (tag == 0x97) {
|
||||
uint32_t le = 0;
|
||||
for (int t = 1; t <= tag_len; t++)
|
||||
for (int t = 1; t <= tag_len; t++) {
|
||||
le |= (*tag_data++) << (tag_len-t);
|
||||
}
|
||||
return le;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void sm_update_iv() {
|
||||
void sm_update_iv()
|
||||
{
|
||||
uint8_t tmp_iv[16], sc_counter[16];
|
||||
memset(tmp_iv, 0, sizeof(tmp_iv)); //IV is always 0 for encryption of IV based on counter
|
||||
mbedtls_mpi_write_binary(&sm_mSSC, sc_counter, sizeof(sc_counter));
|
||||
@@ -207,16 +236,19 @@ void sm_update_iv() {
|
||||
memcpy(sm_iv, sc_counter, sizeof(sc_counter));
|
||||
}
|
||||
|
||||
int sm_verify() {
|
||||
int sm_verify()
|
||||
{
|
||||
uint8_t input[1024];
|
||||
memset(input, 0, sizeof(input));
|
||||
int input_len = 0, r = 0;
|
||||
bool add_header = (CLA(apdu) & 0xC) == 0xC;
|
||||
int data_len = (int)(apdu.nc/sm_blocksize)*sm_blocksize;
|
||||
if (data_len % sm_blocksize)
|
||||
int data_len = (int) (apdu.nc/sm_blocksize)*sm_blocksize;
|
||||
if (data_len % sm_blocksize) {
|
||||
data_len += sm_blocksize;
|
||||
if (data_len+(add_header ? sm_blocksize : 0) > 1024)
|
||||
}
|
||||
if (data_len+(add_header ? sm_blocksize : 0) > 1024) {
|
||||
return CCID_WRONG_LENGTH;
|
||||
}
|
||||
mbedtls_mpi ssc;
|
||||
mbedtls_mpi_init(&ssc);
|
||||
mbedtls_mpi_add_int(&ssc, &sm_mSSC, 1);
|
||||
@@ -224,8 +256,9 @@ int sm_verify() {
|
||||
r = mbedtls_mpi_write_binary(&ssc, input, sm_blocksize);
|
||||
input_len += sm_blocksize;
|
||||
mbedtls_mpi_free(&ssc);
|
||||
if (r != 0)
|
||||
if (r != 0) {
|
||||
return CCID_EXEC_ERROR;
|
||||
}
|
||||
if (add_header) {
|
||||
input[input_len++] = CLA(apdu);
|
||||
input[input_len++] = INS(apdu);
|
||||
@@ -254,25 +287,32 @@ int sm_verify() {
|
||||
mac_len = tag_len;
|
||||
}
|
||||
}
|
||||
if (!mac)
|
||||
if (!mac) {
|
||||
return CCID_WRONG_DATA;
|
||||
}
|
||||
if (some_added) {
|
||||
input[input_len++] = 0x80;
|
||||
input_len += (sm_blocksize - (input_len%sm_blocksize));
|
||||
}
|
||||
uint8_t signature[16];
|
||||
r = sm_sign(input, input_len, signature);
|
||||
if (r != 0)
|
||||
if (r != 0) {
|
||||
return CCID_EXEC_ERROR;
|
||||
if (memcmp(signature, mac, mac_len) == 0)
|
||||
}
|
||||
if (memcmp(signature, mac, mac_len) == 0) {
|
||||
return CCID_OK;
|
||||
}
|
||||
return CCID_VERIFICATION_FAILED;
|
||||
}
|
||||
|
||||
int sm_remove_padding(const uint8_t *data, size_t data_len) {
|
||||
int sm_remove_padding(const uint8_t *data, size_t data_len)
|
||||
{
|
||||
int i = data_len-1;
|
||||
for (; i >= 0 && data[i] == 0; i--);
|
||||
if (i < 0 || data[i] != 0x80)
|
||||
for (; i >= 0 && data[i] == 0; i--) {
|
||||
;
|
||||
}
|
||||
if (i < 0 || data[i] != 0x80) {
|
||||
return -1;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user