mirror of
https://github.com/polhenarejos/pico-hsm.git
synced 2026-01-17 09:28:05 +00:00
More code style.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
parent
18bcf532e7
commit
0990805fb6
3 changed files with 43 additions and 8 deletions
|
|
@ -455,8 +455,12 @@ int cmd_cipher_sym() {
|
|||
if (oid_len != 9) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
uint8_t aes_algo = oid[8], mode = (algo == ALGO_EXT_CIPHER_ENCRYPT ? MBEDTLS_AES_ENCRYPT : MBEDTLS_AES_DECRYPT);
|
||||
if ((aes_algo >= 0x01 && aes_algo <= 0x06 && key_size != 16) || (aes_algo >= 0x15 && aes_algo <= 0x1A && key_size != 24) || (aes_algo >= 0x29 && aes_algo <= 0x2E && key_size != 32)) {
|
||||
uint8_t aes_algo = oid[8],
|
||||
mode =
|
||||
(algo == ALGO_EXT_CIPHER_ENCRYPT ? MBEDTLS_AES_ENCRYPT : MBEDTLS_AES_DECRYPT);
|
||||
if ((aes_algo >= 0x01 && aes_algo <= 0x06 && key_size != 16) ||
|
||||
(aes_algo >= 0x15 && aes_algo <= 0x1A && key_size != 24) ||
|
||||
(aes_algo >= 0x29 && aes_algo <= 0x2E && key_size != 32)) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
mbedtls_aes_context ctx;
|
||||
|
|
@ -527,11 +531,30 @@ int cmd_cipher_sym() {
|
|||
r = mbedtls_gcm_setkey(&gctx, MBEDTLS_CIPHER_ID_AES, kdata, key_size * 8);
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
if (algo == ALGO_EXT_CIPHER_ENCRYPT) {
|
||||
r = mbedtls_gcm_crypt_and_tag(&gctx, MBEDTLS_GCM_ENCRYPT, enc_len, iv, iv_len, aad, aad_len, enc, res_APDU, 16, res_APDU + enc_len);
|
||||
r = mbedtls_gcm_crypt_and_tag(&gctx,
|
||||
MBEDTLS_GCM_ENCRYPT,
|
||||
enc_len,
|
||||
iv,
|
||||
iv_len,
|
||||
aad,
|
||||
aad_len,
|
||||
enc,
|
||||
res_APDU,
|
||||
16,
|
||||
res_APDU + enc_len);
|
||||
res_APDU_size = enc_len + 16;
|
||||
}
|
||||
else if (algo == ALGO_EXT_CIPHER_DECRYPT) {
|
||||
r = mbedtls_gcm_auth_decrypt(&gctx, enc_len - 16, iv, iv_len, aad, aad_len, enc + enc_len - 16, 16, enc, res_APDU);
|
||||
r = mbedtls_gcm_auth_decrypt(&gctx,
|
||||
enc_len - 16,
|
||||
iv,
|
||||
iv_len,
|
||||
aad,
|
||||
aad_len,
|
||||
enc + enc_len - 16,
|
||||
16,
|
||||
enc,
|
||||
res_APDU);
|
||||
res_APDU_size = enc_len - 16;
|
||||
}
|
||||
mbedtls_gcm_free(&gctx);
|
||||
|
|
@ -544,7 +567,9 @@ int cmd_cipher_sym() {
|
|||
if (oid_len != 9) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
uint8_t aes_algo = oid[8], mode = (algo == ALGO_EXT_CIPHER_ENCRYPT ? MBEDTLS_AES_ENCRYPT : MBEDTLS_AES_DECRYPT);
|
||||
uint8_t aes_algo = oid[8],
|
||||
mode =
|
||||
(algo == ALGO_EXT_CIPHER_ENCRYPT ? MBEDTLS_AES_ENCRYPT : MBEDTLS_AES_DECRYPT);
|
||||
int r = 0;
|
||||
uint8_t tmp_iv[16];
|
||||
memset(tmp_iv, 0, sizeof(tmp_iv));
|
||||
|
|
|
|||
|
|
@ -130,7 +130,7 @@ int cmd_decrypt_asym() {
|
|||
}
|
||||
size_t olen = 0;
|
||||
// The SmartCard-HSM returns the point result of the DH operation
|
||||
// with a leading '04'
|
||||
// with a leading '04'
|
||||
res_APDU[0] = 0x04;
|
||||
r =
|
||||
mbedtls_ecdh_calc_secret(&ctx, &olen, res_APDU + 1, MBEDTLS_ECP_MAX_BYTES, random_gen,
|
||||
|
|
|
|||
|
|
@ -386,7 +386,12 @@ int dkek_encode_key(uint8_t id,
|
|||
kb_len += mbedtls_mpi_size(&ecdsa->grp.N);
|
||||
|
||||
size_t olen = 0;
|
||||
mbedtls_ecp_point_write_binary(&ecdsa->grp, &ecdsa->grp.G, MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, kb + 8 + kb_len + 2, sizeof(kb) - 8 - kb_len - 2);
|
||||
mbedtls_ecp_point_write_binary(&ecdsa->grp,
|
||||
&ecdsa->grp.G,
|
||||
MBEDTLS_ECP_PF_UNCOMPRESSED,
|
||||
&olen,
|
||||
kb + 8 + kb_len + 2,
|
||||
sizeof(kb) - 8 - kb_len - 2);
|
||||
put_uint16_t(olen, kb + 8 + kb_len);
|
||||
kb_len += 2 + olen;
|
||||
|
||||
|
|
@ -394,7 +399,12 @@ int dkek_encode_key(uint8_t id,
|
|||
mbedtls_mpi_write_binary(&ecdsa->d, kb + 8 + kb_len, mbedtls_mpi_size(&ecdsa->d));
|
||||
kb_len += mbedtls_mpi_size(&ecdsa->d);
|
||||
|
||||
mbedtls_ecp_point_write_binary(&ecdsa->grp, &ecdsa->Q, MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, kb + 8 + kb_len + 2, sizeof(kb) - 8 - kb_len - 2);
|
||||
mbedtls_ecp_point_write_binary(&ecdsa->grp,
|
||||
&ecdsa->Q,
|
||||
MBEDTLS_ECP_PF_UNCOMPRESSED,
|
||||
&olen,
|
||||
kb + 8 + kb_len + 2,
|
||||
sizeof(kb) - 8 - kb_len - 2);
|
||||
put_uint16_t(olen, kb + 8 + kb_len);
|
||||
kb_len += 2 + olen;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue