Added support for AES CTR.

Note: the OID used by CTR does not exist.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2023-03-23 18:47:32 +01:00
parent ad3304a384
commit f5e875a6b7
3 changed files with 58 additions and 3 deletions

View File

@@ -458,9 +458,9 @@ int cmd_cipher_sym() {
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)) {
if ((aes_algo >= 0x01 && aes_algo <= 0x09 && key_size != 16) ||
(aes_algo >= 0x15 && aes_algo <= 0x1D && key_size != 24) ||
(aes_algo >= 0x29 && aes_algo <= 0x31 && key_size != 32)) {
return SW_WRONG_DATA();
}
mbedtls_aes_context ctx;
@@ -565,6 +565,18 @@ int cmd_cipher_sym() {
return SW_EXEC_ERROR();
}
}
else if (aes_algo == 0x09 || aes_algo == 0x1D || aes_algo == 0x31) { /* CTR */
size_t iv_off = 0;
uint8_t stream_block[16];
r = mbedtls_aes_setkey_enc(&ctx, kdata, key_size * 8);
mbedtls_platform_zeroize(kdata, sizeof(kdata));
r = mbedtls_aes_crypt_ctr(&ctx, enc_len, &iv_off, iv, stream_block, enc, res_APDU);
mbedtls_aes_free(&ctx);
if (r != 0) {
return SW_EXEC_ERROR();
}
res_APDU_size = enc_len;
}
}
else if (memcmp(oid, OID_IEEE_ALG, 8) == 0) {
if (oid_len != 9) {

View File

@@ -151,16 +151,19 @@
#define OID_AES128_OFB OID_NIST_AES "\x03"
#define OID_AES128_CFB OID_NIST_AES "\x04"
#define OID_AES128_GCM OID_NIST_AES "\x06"
#define OID_AES128_CTR OID_NIST_AES "\x09" // Not existing
#define OID_AES192_ECB OID_NIST_AES "\x15"
#define OID_AES192_CBC OID_NIST_AES "\x16"
#define OID_AES192_OFB OID_NIST_AES "\x17"
#define OID_AES192_CFB OID_NIST_AES "\x18"
#define OID_AES192_GCM OID_NIST_AES "\x1A"
#define OID_AES192_CTR OID_NIST_AES "\x1D" // Not existing
#define OID_AES256_ECB OID_NIST_AES "\x29"
#define OID_AES256_CBC OID_NIST_AES "\x2A"
#define OID_AES256_OFB OID_NIST_AES "\x2B"
#define OID_AES256_CFB OID_NIST_AES "\x2C"
#define OID_AES256_GCM OID_NIST_AES "\x2E"
#define OID_AES256_CTR OID_NIST_AES "\x31" // Not existing
#define OID_IEEE_ALG "\x2B\x6F\x02\x8C\x53\x00\x00\x01"
#define OID_AES128_XTS OID_IEEE_ALG "\x01"