Switching to new style.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2023-02-15 00:10:00 +01:00
parent 12bdcbd1f9
commit 43ef33d60b
16 changed files with 429 additions and 521 deletions

View File

@@ -32,8 +32,7 @@ static uint8_t sm_iv[16];
size_t sm_session_pin_len = 0;
uint8_t sm_session_pin[16];
bool is_secured_apdu()
{
bool is_secured_apdu() {
return CLA(apdu) & 0xC;
}
@@ -42,24 +41,22 @@ void sm_derive_key(const uint8_t *input,
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);
uint8_t *out) {
uint8_t *b = (uint8_t *) calloc(1, input_len + nonce_len + 4);
if (input) {
memcpy(b, input, input_len);
}
if (nonce) {
memcpy(b+input_len, nonce, nonce_len);
memcpy(b + input_len, nonce, nonce_len);
}
b[input_len+nonce_len+3] = counter;
b[input_len + nonce_len + 3] = counter;
uint8_t digest[20];
generic_hash(MBEDTLS_MD_SHA1, b, input_len+nonce_len+4, digest);
generic_hash(MBEDTLS_MD_SHA1, b, input_len + nonce_len + 4, digest);
memcpy(out, digest, 16);
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);
@@ -70,28 +67,25 @@ 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) {
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)
{
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,
@@ -100,8 +94,7 @@ int sm_sign(uint8_t *in, size_t in_len, uint8_t *out)
out);
}
int sm_unwrap()
{
int sm_unwrap() {
uint8_t sm_indicator = (CLA(apdu) >> 2) & 0x3;
if (sm_indicator == 0) {
return CCID_OK;
@@ -145,8 +138,7 @@ int sm_unwrap()
return CCID_OK;
}
int sm_wrap()
{
int sm_wrap() {
uint8_t sm_indicator = (CLA(apdu) >> 2) & 0x3;
if (sm_indicator == 0) {
return CCID_OK;
@@ -166,25 +158,27 @@ int sm_wrap()
mbedtls_mpi_free(&ssc);
if (res_APDU_size > 0) {
res_APDU[res_APDU_size++] = 0x80;
memset(res_APDU+res_APDU_size, 0, (sm_blocksize - (res_APDU_size%sm_blocksize)));
res_APDU_size += (sm_blocksize - (res_APDU_size%sm_blocksize));
memset(res_APDU + res_APDU_size, 0, (sm_blocksize - (res_APDU_size % sm_blocksize)));
res_APDU_size += (sm_blocksize - (res_APDU_size % sm_blocksize));
DEBUG_PAYLOAD(res_APDU, res_APDU_size);
sm_update_iv();
aes_encrypt(sm_kenc, sm_iv, 128, HSM_AES_MODE_CBC, res_APDU, res_APDU_size);
memmove(res_APDU+1, res_APDU, res_APDU_size);
memmove(res_APDU + 1, res_APDU, res_APDU_size);
res_APDU[0] = 0x1;
res_APDU_size++;
if (res_APDU_size < 128) {
memmove(res_APDU+2, res_APDU, res_APDU_size);
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) {
memmove(res_APDU+3, res_APDU, res_APDU_size);
}
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 {
memmove(res_APDU+4, res_APDU, res_APDU_size);
}
else {
memmove(res_APDU + 4, res_APDU, res_APDU_size);
res_APDU[1] = 0x82;
res_APDU[2] = res_APDU_size >> 8;
res_APDU[3] = res_APDU_size & 0xff;
@@ -196,11 +190,11 @@ int sm_wrap()
res_APDU[res_APDU_size++] = 2;
res_APDU[res_APDU_size++] = apdu.sw >> 8;
res_APDU[res_APDU_size++] = apdu.sw & 0xff;
memcpy(input+input_len, res_APDU, res_APDU_size);
memcpy(input + input_len, res_APDU, res_APDU_size);
input_len += res_APDU_size;
input[input_len++] = 0x80;
input_len += (sm_blocksize - (input_len%sm_blocksize));
r = sm_sign(input, input_len, res_APDU+res_APDU_size+2);
input_len += (sm_blocksize - (input_len % sm_blocksize));
r = sm_sign(input, input_len, res_APDU + res_APDU_size + 2);
res_APDU[res_APDU_size++] = 0x8E;
res_APDU[res_APDU_size++] = 8;
res_APDU_size += 8;
@@ -210,8 +204,7 @@ int sm_wrap()
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;
@@ -219,7 +212,7 @@ int sm_get_le()
if (tag == 0x97) {
uint32_t le = 0;
for (int t = 1; t <= tag_len; t++) {
le |= (*tag_data++) << (tag_len-t);
le |= (*tag_data++) << (tag_len - t);
}
return le;
}
@@ -227,8 +220,7 @@ int sm_get_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));
@@ -236,17 +228,16 @@ 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;
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;
@@ -265,7 +256,7 @@ int sm_verify()
input[input_len++] = P1(apdu);
input[input_len++] = P2(apdu);
input[input_len++] = 0x80;
input_len += sm_blocksize-5;
input_len += sm_blocksize - 5;
}
bool some_added = false;
const uint8_t *mac = NULL;
@@ -276,9 +267,9 @@ int sm_verify()
while (walk_tlv(apdu.data, apdu.nc, &p, &tag, &tag_len, &tag_data)) {
if (tag & 0x1) {
input[input_len++] = tag;
int tlen = format_tlv_len(tag_len, input+input_len);
int tlen = format_tlv_len(tag_len, input + input_len);
input_len += tlen;
memcpy(input+input_len, tag_data, tag_len);
memcpy(input + input_len, tag_data, tag_len);
input_len += tag_len;
some_added = true;
}
@@ -292,7 +283,7 @@ int sm_verify()
}
if (some_added) {
input[input_len++] = 0x80;
input_len += (sm_blocksize - (input_len%sm_blocksize));
input_len += (sm_blocksize - (input_len % sm_blocksize));
}
uint8_t signature[16];
r = sm_sign(input, input_len, signature);
@@ -305,9 +296,8 @@ int sm_verify()
return CCID_VERIFICATION_FAILED;
}
int sm_remove_padding(const uint8_t *data, size_t data_len)
{
int i = data_len-1;
int sm_remove_padding(const uint8_t *data, size_t data_len) {
int i = data_len - 1;
for (; i >= 0 && data[i] == 0; i--) {
;
}