Support the ASPEED HACE crypto command
Subsystems (2)
PATCH 4/15 hw/misc/aspeed_hace: Support the CTR mode for the crypto command
In reply to Jamin LinThe AST2600, AST1030 and later crypto engines add AES/DES/3DES CTR mode (HACE10[6:4] = 0b100) on top of the ECB/CBC modes shared with the AST2500. Decode the CTR selection, round the working buffers up to a whole block so the stream-like final block is still processed a block at a time, and write the counter advanced by the number of blocks consumed back to the context buffer so the driver can continue across requests.
--- hw/misc/aspeed_hace.c | 50 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-)
hw/misc/aspeed_hace.c +45 -5
@@ -42,6 +42,7 @@#define CRYPT_CMD_OP_MODE_MASK (0x7 << 4)#define CRYPT_CMD_ECB (0x0 << 4)#define CRYPT_CMD_CBC (0x1 << 4) +#define CRYPT_CMD_CTR (0x4 << 4)/* AES key length HACE10[3:2] */ #define CRYPT_CMD_AES_KEY_LEN_MASK (0x3 << 2)#define CRYPT_CMD_AES256 (0x2 << 2)
@@ -589,6 +590,9 @@ static bool crypt_decode_cmd(uint32_t cmd, QCryptoCipherAlgo *alg,case CRYPT_CMD_CBC : *mode =QCRYPTO_CIPHER_MODE_CBC ;break ; +case CRYPT_CMD_CTR : + *mode =QCRYPTO_CIPHER_MODE_CTR ; +break ;default :return false; }
@@ -652,6 +656,22 @@ static bool crypt_prepare_sg(AspeedHACEState *s, uint64_t addr,return copied == len; } +/* +* Add @add to the big-endian counter block @ctr (@len bytes) in place, so the +* CTR mode counter can be advanced by the number of blocks just consumed. +*/ +static void crypt_be_add (uint8_t *ctr,size_t len,uint64_t add) +{ +size_t i = len; + +while (i >0 && add) { + i--; + add += ctr[i]; + ctr[i] = add &0xff ; + add >>=8 ; + } +} +/* * Perform an AES/DES/3DES ECB/CBC operation. The source and destination are * either single contiguous buffers (direct access mode) or scatter-gather
@@ -677,6 +697,7 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd)uint64_t dst_addr;size_t iv_offset;size_t blocklen; +size_t buf_len;size_t keylen;bool status;
@@ -722,8 +743,14 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd)return ; } - src_buf =g_malloc0 (len); - dst_buf =g_malloc0 (len); +/* +* Round the working buffers up to a whole block. Block modes are already +* block-aligned; the stream-like CTR mode may leave a partial final block +* that the engine still processes a full block at a time. +*/ + buf_len =QEMU_ALIGN_UP (len, blocklen); + src_buf =g_malloc0 (buf_len); + dst_buf =g_malloc0 (buf_len);/* Gather the source into the bounce buffer, per the selected mode. */ src_addr = s->regs[R_CRYPT_SRC ];
@@ -744,7 +771,7 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd) }if (encrypt) { -if (qcrypto_cipher_encrypt (cipher, src_buf, dst_buf, len, +if (qcrypto_cipher_encrypt (cipher, src_buf, dst_buf, buf_len, &local_err) <0 ) {qemu_log_mask (LOG_GUEST_ERROR ,"%s: encrypt failed: %s\n" , __func__,error_get_pretty (local_err));
@@ -752,7 +779,7 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd)return ; } }else { -if (qcrypto_cipher_decrypt (cipher, src_buf, dst_buf, len, +if (qcrypto_cipher_decrypt (cipher, src_buf, dst_buf, buf_len, &local_err) <0 ) {qemu_log_mask (LOG_GUEST_ERROR ,"%s: decrypt failed: %s\n" , __func__,error_get_pretty (local_err));
@@ -785,13 +812,26 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd)* output when encrypting, or of the input when decrypting. Write it * back as the IV for the next request. */ - next_iv = (encrypt ? dst_buf : src_buf) + len - blocklen; + next_iv = (encrypt ? dst_buf : src_buf) + buf_len - blocklen;if (address_space_write (&s->dram_as, ctx_addr + iv_offset,MEMTXATTRS_UNSPECIFIED , next_iv, blocklen)) {qemu_log_mask (LOG_GUEST_ERROR ,"%s: Failed to write IV, addr=0x%" HWADDR_PRIx"\n" , __func__, ctx_addr + iv_offset); } + }else if (mode ==QCRYPTO_CIPHER_MODE_CTR ) { +/* +* CTR chains on the counter, which advances by one per block. Add the +* number of blocks processed (buf_len / blocklen) and write it back. +*/ +crypt_be_add (ctx + iv_offset, blocklen, buf_len / blocklen); +if (address_space_write (&s->dram_as, ctx_addr + iv_offset, +MEMTXATTRS_UNSPECIFIED , ctx + iv_offset, + blocklen)) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Failed to write IV, addr=0x%" HWADDR_PRIx"\n" , + __func__, ctx_addr + iv_offset); + } } }
Signature · 1 line
2.43.0
PATCH 8/15 crypto/cipher: Add setaad/gettag for AEAD modes
In reply to Jamin LinAEAD modes such as GCM authenticate optional associated data (AAD) and produce an authentication tag, which the block-cipher encrypt/decrypt interface cannot express. Add qcrypto_cipher_setaad() and qcrypto_cipher_gettag() plus the matching backend driver hooks. The generic front-end reports an error when the selected mode's driver does not implement them, so calling them on a non-AEAD mode fails cleanly.
--- crypto/cipherpriv.h | 8 ++++++++ include/crypto/cipher.h | 36 ++++++++++++++++++++++++++++++++++++ crypto/cipher.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+)
crypto/cipherpriv.h +8 -0
@@ -34,6 +34,14 @@ struct QCryptoCipherDriver { const uint8_t *iv, size_t niv, Error **errp); +int (*cipher_setaad)(QCryptoCipher *cipher, +const uint8_t *aad,size_t len, +Error **errp); + +int (*cipher_gettag)(QCryptoCipher *cipher, +uint8_t *tag,size_t len, +Error **errp); +void (*cipher_free)(QCryptoCipher *cipher); };
include/crypto/cipher.h +36 -0
@@ -235,4 +235,40 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, const uint8_t *iv, size_t niv, Error **errp); +/** +* qcrypto_cipher_setaad: +* @cipher: the cipher object +* @aad: the associated data to authenticate +* @len: the length of @aad +* @errp: pointer to a NULL-initialized error object +* +* For AEAD modes such as GCM, feed the associated data (AAD) that is +* authenticated but not encrypted. It must be called after +* qcrypto_cipher_setiv() and before the first encrypt/decrypt call. It is +* an error to call this on a mode that is not an AEAD mode. +* +* Returns: 0 on success, -1 on error +*/ +int qcrypto_cipher_setaad (QCryptoCipher *cipher, +const uint8_t *aad,size_t len, +Error **errp); + +/** +* qcrypto_cipher_gettag: +* @cipher: the cipher object +* @tag: buffer to receive the authentication tag +* @len: the length of @tag +* @errp: pointer to a NULL-initialized error object +* +* For AEAD modes such as GCM, read back the authentication tag computed +* over the associated data and the message. It must be called after the +* encrypt/decrypt operation. It is an error to call this on a mode that is +* not an AEAD mode. +* +* Returns: 0 on success, -1 on error +*/ +int qcrypto_cipher_gettag (QCryptoCipher *cipher, +uint8_t *tag,size_t len, +Error **errp); +#endif /* QCRYPTO_CIPHER_H */
crypto/cipher.c +31 -0
@@ -205,6 +205,37 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, } +int qcrypto_cipher_setaad (QCryptoCipher *cipher, +const uint8_t *aad,size_t len, +Error **errp) +{ +const QCryptoCipherDriver *drv = cipher->driver; + +if (!drv->cipher_setaad) { +error_setg (errp,"The cipher mode does not support associated data" ); +return -1 ; + } + +return drv->cipher_setaad (cipher, aad, len, errp); +} + + +int qcrypto_cipher_gettag (QCryptoCipher *cipher, +uint8_t *tag,size_t len, +Error **errp) +{ +const QCryptoCipherDriver *drv = cipher->driver; + +if (!drv->cipher_gettag) { +error_setg (errp, +"The cipher mode does not produce an authentication tag" ); +return -1 ; + } + +return drv->cipher_gettag (cipher, tag, len, errp); +} + +void qcrypto_cipher_free (QCryptoCipher *cipher) {if (cipher) {
Signature · 1 line
2.43.0
PATCH 9/15 crypto/cipher-gcrypt: Implement AES-GCM
In reply to Jamin LinMap QCRYPTO_CIPHER_MODE_GCM to GCRY_CIPHER_MODE_GCM and advertise it in qcrypto_cipher_supports() for 128-bit block ciphers. Add a GCM driver whose setiv accepts the (typically 96-bit) nonce, whose encrypt/decrypt do not require block-aligned lengths, and which implements setaad via gcry_cipher_authenticate() and gettag via gcry_cipher_gettag().
--- crypto/cipher-gcrypt.c.inc | 101 +++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+)
crypto/cipher-gcrypt.c.inc +101 -0
@@ -65,6 +65,8 @@ static int qcrypto_cipher_mode_to_gcry_mode(QCryptoCipherMode mode) return GCRY_CIPHER_MODE_CBC; case QCRYPTO_CIPHER_MODE_CTR: return GCRY_CIPHER_MODE_CTR; + case QCRYPTO_CIPHER_MODE_GCM: + return GCRY_CIPHER_MODE_GCM; default: return GCRY_CIPHER_MODE_NONE; }
@@ -104,6 +106,10 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgo alg, case QCRYPTO_CIPHER_MODE_XTS: case QCRYPTO_CIPHER_MODE_CTR: return true; + case QCRYPTO_CIPHER_MODE_GCM: + /* GCM requires a 128-bit block cipher. */ + return gcry_cipher_get_algo_blklen( + qcrypto_cipher_alg_to_gcry_alg(alg)) == 16; default: return false; }
@@ -228,6 +234,99 @@ static const struct QCryptoCipherDriver qcrypto_gcrypt_ctr_driver = { .cipher_free = qcrypto_gcrypt_ctx_free, }; +/* + * GCM is an AEAD stream mode: the IV/nonce need not match the block size, + * the message length need not be a multiple of the block size, associated + * data is fed with gcry_cipher_authenticate() and the authentication tag is + * read back with gcry_cipher_gettag(). + */ +static int qcrypto_gcrypt_gcm_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) +{ + QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base); + gcry_error_t err; + + gcry_cipher_reset(ctx->handle); + err = gcry_cipher_setiv(ctx->handle, iv, niv); + if (err != 0) { + error_setg(errp, "Cannot set IV: %s", gcry_strerror(err)); + return -1; + } + + return 0; +} + +static int qcrypto_gcrypt_gcm_setaad(QCryptoCipher *cipher, + const uint8_t *aad, size_t len, + Error **errp) +{ + QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base); + gcry_error_t err; + + err = gcry_cipher_authenticate(ctx->handle, aad, len); + if (err != 0) { + error_setg(errp, "Cannot set AAD: %s", gcry_strerror(err)); + return -1; + } + + return 0; +} + +static int qcrypto_gcrypt_gcm_encrypt(QCryptoCipher *cipher, const void *in, + void *out, size_t len, Error **errp) +{ + QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base); + gcry_error_t err; + + err = gcry_cipher_encrypt(ctx->handle, out, len, in, len); + if (err != 0) { + error_setg(errp, "Cannot encrypt data: %s", gcry_strerror(err)); + return -1; + } + + return 0; +} + +static int qcrypto_gcrypt_gcm_decrypt(QCryptoCipher *cipher, const void *in, + void *out, size_t len, Error **errp) +{ + QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base); + gcry_error_t err; + + err = gcry_cipher_decrypt(ctx->handle, out, len, in, len); + if (err != 0) { + error_setg(errp, "Cannot decrypt data: %s", gcry_strerror(err)); + return -1; + } + + return 0; +} + +static int qcrypto_gcrypt_gcm_gettag(QCryptoCipher *cipher, + uint8_t *tag, size_t len, Error **errp) +{ + QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base); + gcry_error_t err; + + err = gcry_cipher_gettag(ctx->handle, tag, len); + if (err != 0) { + error_setg(errp, "Cannot get tag: %s", gcry_strerror(err)); + return -1; + } + + return 0; +} + +static const struct QCryptoCipherDriver qcrypto_gcrypt_gcm_driver = { + .cipher_encrypt = qcrypto_gcrypt_gcm_encrypt, + .cipher_decrypt = qcrypto_gcrypt_gcm_decrypt, + .cipher_setiv = qcrypto_gcrypt_gcm_setiv, + .cipher_setaad = qcrypto_gcrypt_gcm_setaad, + .cipher_gettag = qcrypto_gcrypt_gcm_gettag, + .cipher_free = qcrypto_gcrypt_ctx_free, +}; + static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgo alg, QCryptoCipherMode mode, const uint8_t *key,
@@ -259,6 +358,8 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgo alg, if (mode == QCRYPTO_CIPHER_MODE_CTR) { drv = &qcrypto_gcrypt_ctr_driver; + } else if (mode == QCRYPTO_CIPHER_MODE_GCM) { + drv = &qcrypto_gcrypt_gcm_driver; } else { drv = &qcrypto_gcrypt_driver; }
Signature · 1 line
2.43.0
PATCH 15/15 tests/functional/aarch64/test_aspeed_ast2700: Drop the AST2700 crypto self-test workaround
In reply to Jamin LinThe AST2700 boot tests appended 'cryptomgr.notests=1' to the kernel command line to skip the Linux crypto manager self-tests.
Now that the HACE crypto engine emulates the crypto command, the self-tests pass, so drop the workaround and let them run. This also exercises the HACE crypto emulation during the functional boot tests.
--- tests/functional/aarch64/test_aspeed_ast2700a1.py | 5 ----- tests/functional/aarch64/test_aspeed_ast2700a2.py | 5 ----- tests/functional/aarch64/test_aspeed_ast2700fc.py | 5 ----- 3 files changed, 15 deletions(-)
tests/functional/aarch64/test_aspeed_ast2700a1.py +0 -5
@@ -51,10 +51,6 @@ def verify_vbootrom_firmware_flow(self): wait_for_console_pattern(self, 'pass') wait_for_console_pattern(self, 'Jumping to BL31 (Trusted Firmware-A)') - def disable_kernel_crypto_selftest(self): - exec_command_and_wait_for_pattern(self, - 'setenv bootargs "${bootargs} cryptomgr.notests=1"', '=>') - def enable_ast2700_pcie2(self): exec_command_and_wait_for_pattern(self, 'cp 100420000 403000000 900000', '=>')
@@ -71,7 +67,6 @@ def verify_openbmc_boot_start(self, enable_pcie=True): wait_for_console_pattern(self, 'U-Boot 2023.10') wait_for_console_pattern(self, 'Hit any key to stop autoboot') exec_command_and_wait_for_pattern(self, '\012', '=>') - self.disable_kernel_crypto_selftest() if enable_pcie: self.enable_ast2700_pcie2() else:
tests/functional/aarch64/test_aspeed_ast2700a2.py +0 -5
@@ -51,10 +51,6 @@ def verify_vbootrom_firmware_flow(self): wait_for_console_pattern(self, 'pass') wait_for_console_pattern(self, 'Jumping to BL31 (Trusted Firmware-A)') - def disable_kernel_crypto_selftest(self): - exec_command_and_wait_for_pattern(self, - 'setenv bootargs "${bootargs} cryptomgr.notests=1"', '=>') - def enable_ast2700_pcie2(self): exec_command_and_wait_for_pattern(self, 'cp 100420000 403000000 900000', '=>')
@@ -71,7 +67,6 @@ def verify_openbmc_boot_start(self, enable_pcie=True): wait_for_console_pattern(self, 'U-Boot 2023.10') wait_for_console_pattern(self, 'Hit any key to stop autoboot') exec_command_and_wait_for_pattern(self, '\012', '=>') - self.disable_kernel_crypto_selftest() if enable_pcie: self.enable_ast2700_pcie2() else:
tests/functional/aarch64/test_aspeed_ast2700fc.py +0 -5
@@ -27,10 +27,6 @@ def do_test_aarch64_aspeed_sdk_start(self, image): self.vm.launch() - def disable_kernel_crypto_selftest(self): - exec_command_and_wait_for_pattern(self, - 'setenv bootargs "${bootargs} cryptomgr.notests=1"', '=>') - def enable_ast2700_pcie2(self): exec_command_and_wait_for_pattern(self, 'cp 100420000 403000000 900000', '=>')
@@ -47,7 +43,6 @@ def verify_openbmc_boot_and_login(self, name): wait_for_console_pattern(self, 'U-Boot 2023.10') wait_for_console_pattern(self, 'Hit any key to stop autoboot') exec_command_and_wait_for_pattern(self, '\012', '=>') - self.disable_kernel_crypto_selftest() self.enable_ast2700_pcie2() wait_for_console_pattern(self, 'Starting kernel ...')
Signature · 1 line
2.43.0
PATCH 10/15 tests/unit/test-crypto-cipher: Test AES-GCM mode
In reply to Jamin LinExercise the new GCM mode and the setaad/gettag helpers with the canonical AES-GCM test vectors from the GCM specification (McGrew & Viega, also NIST SP 800-38D): AES-128 and AES-256, with and without associated data. Each vector is run through encrypt (checking the ciphertext and the generated tag) and decrypt (checking the recovered plaintext and the recomputed tag).
--- tests/unit/test-crypto-cipher.c | 240 ++++++++++++++++++++++++++++++++ 1 file changed, 240 insertions(+)
tests/unit/test-crypto-cipher.c +240 -0
@@ -810,6 +810,230 @@ static void test_cipher_short_plaintext(void)qcrypto_cipher_free (cipher); } +typedef struct QCryptoCipherGcmTestData QCryptoCipherGcmTestData ; +struct QCryptoCipherGcmTestData { +const char *path; +QCryptoCipherAlgo alg; +const char *key; +const char *iv; +/* associated data, or NULL for none */ +const char *aad; +const char *plaintext; +const char *ciphertext; +const char *tag; +}; + +/* +* AES-GCM test vectors from "The Galois/Counter Mode of Operation (GCM)" +* (McGrew & Viega, also NIST SP 800-38D), with a 96-bit IV and a 128-bit +* tag. Each entry's "Test case N" label is the numbered test case from that +* document (Appendix B / the GCM specification's test vectors). +*/ +static QCryptoCipherGcmTestData gcm_test_data[] = { + { +/* Test case 2 */ + .path ="/crypto/cipher/aes-128-gcm/2" , + .alg =QCRYPTO_CIPHER_ALGO_AES_128 , + .key ="00000000000000000000000000000000" , + .iv ="000000000000000000000000" , + .plaintext ="00000000000000000000000000000000" , + .ciphertext ="0388dace60b6a392f328c2b971b2fe78" , + .tag ="ab6e47d42cec13bdf53a67b21257bddf" , + }, + { +/* Test case 3 (no AAD) */ + .path ="/crypto/cipher/aes-128-gcm/3" , + .alg =QCRYPTO_CIPHER_ALGO_AES_128 , + .key ="feffe9928665731c6d6a8f9467308308" , + .iv ="cafebabefacedbaddecaf888" , + .plaintext = +"d9313225f88406e5a55909c5aff5269a" +"86a7a9531534f7da2e4c303d8a318a72" +"1c3c0c95956809532fcf0e2449a6b525" +"b16aedf5aa0de657ba637b391aafd255" , + .ciphertext = +"42831ec2217774244b7221b784d0d49c" +"e3aa212f2c02a4e035c17e2329aca12e" +"21d514b25466931c7d8f6a5aac84aa05" +"1ba30b396a0aac973d58e091473f5985" , + .tag ="4d5c2af327cd64a62cf35abd2ba6fab4" , + }, + { +/* Test case 4 (with AAD) */ + .path ="/crypto/cipher/aes-128-gcm/4" , + .alg =QCRYPTO_CIPHER_ALGO_AES_128 , + .key ="feffe9928665731c6d6a8f9467308308" , + .iv ="cafebabefacedbaddecaf888" , + .aad ="feedfacedeadbeeffeedfacedeadbeefabaddad2" , + .plaintext = +"d9313225f88406e5a55909c5aff5269a" +"86a7a9531534f7da2e4c303d8a318a72" +"1c3c0c95956809532fcf0e2449a6b525" +"b16aedf5aa0de657ba637b39" , + .ciphertext = +"42831ec2217774244b7221b784d0d49c" +"e3aa212f2c02a4e035c17e2329aca12e" +"21d514b25466931c7d8f6a5aac84aa05" +"1ba30b396a0aac973d58e091" , + .tag ="5bc94fbc3221a5db94fae95ae7121a47" , + }, + { +/* Test case 15 (AES-256, no AAD) */ + .path ="/crypto/cipher/aes-256-gcm/15" , + .alg =QCRYPTO_CIPHER_ALGO_AES_256 , + .key = +"feffe9928665731c6d6a8f9467308308" +"feffe9928665731c6d6a8f9467308308" , + .iv ="cafebabefacedbaddecaf888" , + .plaintext = +"d9313225f88406e5a55909c5aff5269a" +"86a7a9531534f7da2e4c303d8a318a72" +"1c3c0c95956809532fcf0e2449a6b525" +"b16aedf5aa0de657ba637b391aafd255" , + .ciphertext = +"522dc1f099567d07f47f37a32a84427d" +"643a8cdcbfe5c0c97598a2bd2555d1aa" +"8cb08e48590dbb3da7b08b1056828838" +"c5f61e6393ba7a0abcc9f662898015ad" , + .tag ="b094dac5d93471bdec1a502270e3cc6c" , + }, + { +/* Test case 16 (AES-256, with AAD) */ + .path ="/crypto/cipher/aes-256-gcm/16" , + .alg =QCRYPTO_CIPHER_ALGO_AES_256 , + .key = +"feffe9928665731c6d6a8f9467308308" +"feffe9928665731c6d6a8f9467308308" , + .iv ="cafebabefacedbaddecaf888" , + .aad ="feedfacedeadbeeffeedfacedeadbeefabaddad2" , + .plaintext = +"d9313225f88406e5a55909c5aff5269a" +"86a7a9531534f7da2e4c303d8a318a72" +"1c3c0c95956809532fcf0e2449a6b525" +"b16aedf5aa0de657ba637b39" , + .ciphertext = +"522dc1f099567d07f47f37a32a84427d" +"643a8cdcbfe5c0c97598a2bd2555d1aa" +"8cb08e48590dbb3da7b08b1056828838" +"c5f61e6393ba7a0abcc9f662" , + .tag ="76fc6ece0f4e1768cddf8853bb2d551b" , + }, +}; + +static void test_cipher_gcm (const void *opaque) +{ +const QCryptoCipherGcmTestData *data = opaque; + g_autofree uint8_t *key = NULL; + g_autofree uint8_t *iv = NULL; + g_autofree uint8_t *aad = NULL; + g_autofree uint8_t *ptext = NULL; + g_autofree uint8_t *ctext = NULL; + g_autofree uint8_t *tagexp = NULL; + g_autofree uint8_t *out = NULL; +uint8_t tag[16 ]; +size_t nkey; +size_t niv; +size_t naad =0 ; +size_t nptext; +size_t nctext; +size_t ntag; +QCryptoCipher *cipher; + + nkey =unhex_string (data->key, &key); + niv =unhex_string (data->iv, &iv); + nptext =unhex_string (data->plaintext, &ptext); + nctext =unhex_string (data->ciphertext, &ctext); + ntag =unhex_string (data->tag, &tagexp); +if (data->aad) { + naad =unhex_string (data->aad, &aad); + } + + g_assert_cmpint(nptext, ==, nctext); + g_assert_cmpint(ntag, ==, sizeof(tag)); + out =g_new0 (uint8_t, nptext); + +/* Encrypt: plaintext -> ciphertext, then read back the tag. */ + cipher =qcrypto_cipher_new (data->alg,QCRYPTO_CIPHER_MODE_GCM , + key, nkey, &error_abort); +g_assert (cipher !=NULL ); +g_assert (qcrypto_cipher_setiv (cipher, iv, niv, &error_abort) ==0 ); +if (naad) { +g_assert (qcrypto_cipher_setaad (cipher, aad, naad, &error_abort) ==0 ); + } +g_assert (qcrypto_cipher_encrypt (cipher, ptext, out, nptext, + &error_abort) ==0 ); +g_assert_cmpmem (out, nptext, ctext, nctext); +g_assert (qcrypto_cipher_gettag (cipher, tag,sizeof (tag), + &error_abort) ==0 ); +g_assert_cmpmem (tag,sizeof (tag), tagexp, ntag); +qcrypto_cipher_free (cipher); + +/* Decrypt: ciphertext -> plaintext, recomputed tag must match. */ +memset (out,0 , nptext); + cipher =qcrypto_cipher_new (data->alg,QCRYPTO_CIPHER_MODE_GCM , + key, nkey, &error_abort); +g_assert (cipher !=NULL ); +g_assert (qcrypto_cipher_setiv (cipher, iv, niv, &error_abort) ==0 ); +if (naad) { +g_assert (qcrypto_cipher_setaad (cipher, aad, naad, &error_abort) ==0 ); + } +g_assert (qcrypto_cipher_decrypt (cipher, ctext, out, nctext, + &error_abort) ==0 ); +g_assert_cmpmem (out, nctext, ptext, nptext); +g_assert (qcrypto_cipher_gettag (cipher, tag,sizeof (tag), + &error_abort) ==0 ); +g_assert_cmpmem (tag,sizeof (tag), tagexp, ntag); +qcrypto_cipher_free (cipher); +} + +/* +* Corrupt one ciphertext byte and confirm the recomputed GCM tag no longer +* matches: the authentication tag must detect tampering. +*/ +static void test_cipher_gcm_tamper (const void *opaque) +{ +const QCryptoCipherGcmTestData *data = opaque; + g_autofree uint8_t *key = NULL; + g_autofree uint8_t *iv = NULL; + g_autofree uint8_t *aad = NULL; + g_autofree uint8_t *ctext = NULL; + g_autofree uint8_t *tagexp = NULL; + g_autofree uint8_t *out = NULL; +uint8_t tag[16 ]; +size_t nkey; +size_t niv; +size_t naad =0 ; +size_t nctext; +size_t ntag; +QCryptoCipher *cipher; + + nkey =unhex_string (data->key, &key); + niv =unhex_string (data->iv, &iv); + nctext =unhex_string (data->ciphertext, &ctext); + ntag =unhex_string (data->tag, &tagexp); +if (data->aad) { + naad =unhex_string (data->aad, &aad); + } + out =g_new0 (uint8_t, nctext); + +/* Flip one ciphertext bit before decrypting. */ + ctext[0 ] ^=0x01 ; + + cipher =qcrypto_cipher_new (data->alg,QCRYPTO_CIPHER_MODE_GCM , + key, nkey, &error_abort); +g_assert (cipher !=NULL ); +g_assert (qcrypto_cipher_setiv (cipher, iv, niv, &error_abort) ==0 ); +if (naad) { +g_assert (qcrypto_cipher_setaad (cipher, aad, naad, &error_abort) ==0 ); + } +g_assert (qcrypto_cipher_decrypt (cipher, ctext, out, nctext, + &error_abort) ==0 ); +g_assert (qcrypto_cipher_gettag (cipher, tag,sizeof (tag), + &error_abort) ==0 ); +g_assert (memcmp (tag, tagexp, ntag) !=0 ); +qcrypto_cipher_free (cipher); +} +int main (int argc,char **argv) {size_t i;
@@ -828,6 +1052,22 @@ int main(int argc, char **argv) } } +for (i =0 ; i <G_N_ELEMENTS (gcm_test_data); i++) { +if (qcrypto_cipher_supports (gcm_test_data[i].alg, +QCRYPTO_CIPHER_MODE_GCM )) { + g_autofree char *tamper = g_strdup_printf("%s/tamper", + gcm_test_data[i].path); + +g_test_add_data_func (gcm_test_data[i].path, &gcm_test_data[i], + test_cipher_gcm); +g_test_add_data_func (tamper, &gcm_test_data[i], + test_cipher_gcm_tamper); + }else { +g_printerr ("# skip unsupported %s:gcm\n" , +QCryptoCipherAlgo_str (gcm_test_data[i].alg)); + } + } +if (qcrypto_cipher_supports (QCRYPTO_CIPHER_ALGO_AES_256 ,QCRYPTO_CIPHER_MODE_CBC )) { g_test_add_func("/crypto/cipher/null-iv",
Signature · 1 line
2.43.0
cover Support the ASPEED HACE crypto command
The ASPEED HACE model only emulated the hash command; the crypto (cipher) command was stubbed out. On the AST2700 the kernel runs the crypto driver self-tests at boot, so without crypto emulation booting floods the log with failures:
root@ast2700-default:~# dmesg | grep "self" [ 6.078446] alg: self-tests for ctr(des) using aspeed-ctr-des failed (rc=-22) [ 6.079295] alg: self-tests for ctr(des) using aspeed-ctr-des failed (rc=-22) [ 6.089924] alg: self-tests for ctr(des3_ede) using aspeed-ctr-tdes failed (rc=-22) [ 6.090629] alg: self-tests for ctr(des3_ede) using aspeed-ctr-tdes failed (rc=-22) [ 6.100134] alg: self-tests for cbc(des3_ede) using aspeed-cbc-tdes failed (rc=-22) [ 6.100416] alg: self-tests for ecb(des3_ede) using aspeed-ecb-tdes failed (rc=-22) [ 6.100576] alg: self-tests for cbc(des3_ede) using aspeed-cbc-tdes failed (rc=-22) [ 6.101411] alg: self-tests for ecb(des3_ede) using aspeed-ecb-tdes failed (rc=-22) [ 6.152954] alg: self-tests for ecb(des) using aspeed-ecb-des failed (rc=-22) [ 6.153701] alg: self-tests for ecb(des) using aspeed-ecb-des failed (rc=-22) [ 6.180101] alg: self-tests for cbc(des) using aspeed-cbc-des failed (rc=-22) [ 6.180747] alg: self-tests for cbc(des) using aspeed-cbc-des failed (rc=-22) [ 6.206437] alg: self-tests for ctr(aes) using aspeed-ctr-aes failed (rc=-22) [ 6.206910] alg: self-tests for ctr(aes) using aspeed-ctr-aes failed (rc=-22) [ 6.215313] alg: self-tests for cbc(aes) using aspeed-cbc-aes failed (rc=-22) [ 6.215698] alg: self-tests for cbc(aes) using aspeed-cbc-aes failed (rc=-22) [ 6.227173] alg: self-tests for ecb(aes) using aspeed-ecb-aes failed (rc=-22) [ 6.228027] alg: self-tests for ecb(aes) using aspeed-ecb-aes failed (rc=-22) [ 6.276878] alg: self-tests for gcm(aes) using aspeed-gcm-aes failed (rc=-22) [ 6.277541] alg: self-tests for gcm(aes) using aspeed-gcm-aes failed (rc=-22)
This series implements the HACE crypto command: AES/DES/3DES in ECB/CBC/CTR over direct and scatter-gather DMA, and AES-GCM with 64-bit DMA on the AST2700. It also adds GCM to the qcrypto cipher API (gcrypt backend) that the AST2700 path needs, plus qtest and unit-test coverage.
With crypto emulated the self-tests pass, the warnings above disappear, and the temporary 'cryptomgr.notests=1' boot workaround in the AST2700 functional tests is dropped.
v1:
1. Support the crypto command in direct access mode
2. Support scatter-gather mode for the crypto command
3. Support the AES-GCM mode for the crypto command
4. Support 64-bit DMA for the crypto command
5. Test the crypto command
6. Drop the AST2700 crypto self-test/model workaround
Jamin Lin (15):
hw/misc/aspeed_hace: Support the crypto command in direct access mode tests/qtest/aspeed-hace: Test the crypto command on the AST2500 hw/misc/aspeed_hace: Support scatter-gather mode for the crypto
command
hw/misc/aspeed_hace: Support the CTR mode for the crypto command tests/qtest/aspeed-hace: Test the crypto command on the AST2600 tests/qtest/aspeed-hace: Test the crypto command on the AST1030 crypto/cipher: Add GCM to QCryptoCipherMode
crypto/cipher: Add setaad/gettag for AEAD modes crypto/cipher-gcrypt: Implement AES-GCM tests/unit/test-crypto-cipher: Test AES-GCM mode hw/misc/aspeed_hace: Support 64-bit DMA for the crypto command hw/misc/aspeed_hace: Support the AES-GCM mode for the crypto command hw/misc/aspeed_hace: Enable the crypto command on the AST2700 tests/qtest/aspeed-hace: Test the crypto command on the AST2700 tests/functional/aarch64/test_aspeed_ast2700: Drop the AST2700 crypto
self-test workaround
qapi/crypto.json | 4 +- crypto/cipherpriv.h | 8 + include/crypto/cipher.h | 36 + include/hw/misc/aspeed_hace.h | 1 - tests/qtest/aspeed-hace-utils.h | 20 + crypto/cipher.c | 32 + hw/misc/aspeed_hace.c | 454 +++++++++++- tests/qtest/aspeed-hace-utils.c | 662 ++++++++++++++++++ tests/qtest/aspeed_hace-test.c | 18 + tests/qtest/ast2700-hace-test.c | 9 + tests/unit/test-crypto-cipher.c | 240 +++++++ crypto/cipher-gcrypt.c.inc | 101 +++ .../aarch64/test_aspeed_ast2700a1.py | 5 - .../aarch64/test_aspeed_ast2700a2.py | 5 - .../aarch64/test_aspeed_ast2700fc.py | 5 - 15 files changed, 1563 insertions(+), 37 deletions(-)
Signature · 1 line
2.43.0
PATCH 11/15 hw/misc/aspeed_hace: Support 64-bit DMA for the crypto command
The AST2700 crypto engine addresses DRAM with 64 bits, supplying the high half of the source, destination and context addresses through HACE80, HACE84 and HACE88. Add those registers and a crypt_get_addr() helper that combines the low and high halves when the SoC has 64-bit DMA, mirroring the hash engine. SoCs without 64-bit DMA (AST2500/AST2600/AST1030) ignore the high registers, so their behaviour is unchanged.
--- hw/misc/aspeed_hace.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-)
hw/misc/aspeed_hace.c +30 -3
@@ -58,6 +58,11 @@#define CRYPT_CTX_KEY_OFFSET 0x10#define CRYPT_CTX_SIZE 0x30 +/* AST2700 64-bit DMA high address registers for the crypto command */ +#define R_CRYPT_SRC_HI (0x80 / 4) +#define R_CRYPT_DEST_HI (0x84 / 4) +#define R_CRYPT_CONTEXT_HI (0x88 / 4) +#define R_STATUS (0x1c / 4)#define HASH_IRQ BIT(9)#define CRYPT_IRQ BIT(12)
@@ -672,6 +677,19 @@ static void crypt_be_add(uint8_t *ctr, size_t len, uint64_t add) } } +static uint64_t crypt_get_addr (AspeedHACEState *s,int reg,int reg_hi) +{ +AspeedHACEClass *ahc =ASPEED_HACE_GET_CLASS (s); +uint64_t addr; + + addr =deposit64 (0 ,0 ,32 , s->regs[reg]); +if (ahc->has_dma64) { + addr =deposit64 (addr,32 ,32 , s->regs[reg_hi]); + } + +return addr; +} +/* * Perform an AES/DES/3DES ECB/CBC operation. The source and destination are * either single contiguous buffers (direct access mode) or scatter-gather
@@ -712,7 +730,7 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd) }/* Fetch the IV and key from the context buffer in DRAM. */ - ctx_addr = s->regs[R_CRYPT_CONTEXT ]; + ctx_addr =crypt_get_addr (s,R_CRYPT_CONTEXT ,R_CRYPT_CONTEXT_HI );if (address_space_read (&s->dram_as, ctx_addr,MEMTXATTRS_UNSPECIFIED , ctx,sizeof (ctx))) { qemu_log_mask(LOG_GUEST_ERROR,
@@ -753,7 +771,7 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd) dst_buf =g_malloc0 (buf_len);/* Gather the source into the bounce buffer, per the selected mode. */ - src_addr = s->regs[R_CRYPT_SRC ]; + src_addr =crypt_get_addr (s,R_CRYPT_SRC ,R_CRYPT_SRC_HI );if (sg_mode) { status =crypt_prepare_sg (s, src_addr, src_buf, len, false); }else {
@@ -789,7 +807,7 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd) }/* Scatter the result back out, per the selected mode. */ - dst_addr = s->regs[R_CRYPT_DEST ]; + dst_addr =crypt_get_addr (s,R_CRYPT_DEST ,R_CRYPT_DEST_HI );if (sg_mode) { status =crypt_prepare_sg (s, dst_addr, dst_buf, len, true); }else {
@@ -953,6 +971,15 @@ static void aspeed_hace_write(void *opaque, hwaddr addr, uint64_t data,case R_HASH_KEY_BUFF_HI : data &= ahc->key_hi_mask;break ; +case R_CRYPT_SRC_HI : + data &= ahc->src_hi_mask; +break ; +case R_CRYPT_DEST_HI : + data &= ahc->dest_hi_mask; +break ; +case R_CRYPT_CONTEXT_HI : + data &= ahc->key_hi_mask; +break ;default :break ; }
Signature · 1 line
2.43.0
PATCH 2/15 tests/qtest/aspeed-hace: Test the crypto command on the AST2500
In reply to Jamin LinAdd a crypto known-answer test harness and exercise the AST2500, which uses the crypto engine's direct access mode. Each mode (AES/DES/3DES in ECB and CBC) is a separate test that checks the ciphertext, the plaintext round-trip and, for CBC, the chaining IV written back to the context buffer.
The key/IV/plaintext/ciphertext values are taken verbatim from the Linux kernel crypto self-test templates in crypto/testmgr.h.
--- tests/qtest/aspeed-hace-utils.h | 16 ++ tests/qtest/aspeed-hace-utils.c | 304 ++++++++++++++++++++++++++++++++ tests/qtest/aspeed_hace-test.c | 6 + 3 files changed, 326 insertions(+)
tests/qtest/aspeed-hace-utils.h +16 -0
@@ -79,5 +79,21 @@ void aspeed_test_sha512_accum(const char *machine, const uint32_t base,void aspeed_test_addresses (const char *machine,const uint32_t base,const struct AspeedMasks *expected); +/* +* Cipher modes a SoC's crypto engine supports, for aspeed_add_crypto_tests(). +*/ +enum { +CRYPT_MODE_ECB =1 <<0 , +CRYPT_MODE_CBC =1 <<1 , +}; + +/* +* Register the crypto known-answer tests that @modes selects (a mask of +* CRYPT_MODE_*) for the given machine. Each test is named +* "<prefix>/hace/crypto/<mode>". +*/ +void aspeed_add_crypto_tests (const char *prefix,const char *machine, +uint32_t base,uint64_t dram,uint32_t modes); +#endif /* TESTS_ASPEED_HACE_UTILS_H */
tests/qtest/aspeed-hace-utils.c +304 -0
307 lines not on this page · raw message
tests/qtest/aspeed_hace-test.c +6 -0
@@ -229,6 +229,12 @@ int main(int argc, char **argv)qtest_add_func ("ast2500/hace/sha256" , test_sha256_ast2500);qtest_add_func ("ast2500/hace/md5" , test_md5_ast2500); +/* +* The AST2500 crypto engine uses direct access mode and supports ECB/CBC. +*/ +aspeed_add_crypto_tests ("ast2500" ,"-machine ast2500-evb" ,0x1e6e3000 , +0x80000000 ,CRYPT_MODE_ECB |CRYPT_MODE_CBC ); +qtest_add_func ("ast2400/hace/addresses" , test_addresses_ast2400);qtest_add_func ("ast2400/hace/sha512" , test_sha512_ast2400);qtest_add_func ("ast2400/hace/sha256" , test_sha256_ast2400);
Signature · 1 line
2.43.0
PATCH 14/15 tests/qtest/aspeed-hace: Test the crypto command on the AST2700
In reply to Jamin LinCover the AST2700 crypto engine, which drives 64-bit scatter-gather DMA and adds AES-GCM on top of the ECB/CBC/CTR modes shared with the AST2600. Add AES-128 and AES-256 GCM known-answer vectors (GCM specification / NIST SP 800-38D, no associated data) and a dedicated GCM runner that programs the tag buffer and reads the tag back, checking it after both encryption and decryption. Register the AST2700 with all four modes.
--- tests/qtest/aspeed-hace-utils.h | 1 + tests/qtest/aspeed-hace-utils.c | 159 +++++++++++++++++++++++++++++++- tests/qtest/ast2700-hace-test.c | 9 ++ 3 files changed, 168 insertions(+), 1 deletion(-)
tests/qtest/aspeed-hace-utils.h +1 -0
@@ -86,6 +86,7 @@ enum {CRYPT_MODE_ECB =1 <<0 ,CRYPT_MODE_CBC =1 <<1 ,CRYPT_MODE_CTR =1 <<2 , + CRYPT_MODE_GCM = 1 << 3, };/*
tests/qtest/aspeed-hace-utils.c +158 -1
231 lines not on this page · raw message
tests/qtest/ast2700-hace-test.c +9 -0
@@ -94,5 +94,14 @@ int main(int argc, char **argv)qtest_add_func ("ast2700/hace/sha384_accum" , test_sha384_accum_ast2700);qtest_add_func ("ast2700/hace/sha256_accum" , test_sha256_accum_ast2700); +/* +* The AST2700 crypto engine uses scatter-gather with 64-bit DMA and adds +* AES-GCM on top of the ECB/CBC/CTR modes shared with the AST2600. +*/ +aspeed_add_crypto_tests ("ast2700" ,"-machine ast2700-evb" ,0x12070000 , +0x400000000 , +CRYPT_MODE_ECB |CRYPT_MODE_CBC |CRYPT_MODE_CTR | +CRYPT_MODE_GCM , true); +return g_test_run (); }
Signature · 1 line
2.43.0
PATCH 13/15 hw/misc/aspeed_hace: Enable the crypto command on the AST2700
In reply to Jamin LinWith direct/scatter-gather access, 64-bit DMA and AES-GCM all in place, the AST2700 crypto engine is now fully modelled. Drop its temporary interrupt-only workaround so the crypto command runs for real, like the other HACE variants.
--- include/hw/misc/aspeed_hace.h | 1 - hw/misc/aspeed_hace.c | 15 +-------------- 2 files changed, 1 insertion(+), 15 deletions(-)
include/hw/misc/aspeed_hace.h +0 -1
@@ -49,7 +49,6 @@ struct AspeedHACEClass {uint32_t key_mask;uint32_t hash_mask;uint64_t nr_regs; -bool raise_crypt_interrupt_workaround;uint32_t src_hi_mask;uint32_t dest_hi_mask;uint32_t key_hi_mask;
hw/misc/aspeed_hace.c +1 -14
@@ -998,14 +998,7 @@ static void aspeed_hace_write(void *opaque, hwaddr addr, uint64_t data,break ; }case R_CRYPT_CMD : -/* -* The AST2700 crypto engine needs 64-bit DMA and AES-GCM, which are -* added later; until then it keeps the temporary workaround of only -* raising the completion interrupt without running the command. -*/ -if (!ahc->raise_crypt_interrupt_workaround) { -do_crypt_operation (s, data); - } +do_crypt_operation (s, data);/* Hardware raises the crypt interrupt once the command finishes. */ s->regs[R_STATUS ] |=CRYPT_IRQ ;
@@ -1209,12 +1202,6 @@ static void aspeed_ast2700_hace_class_init(ObjectClass *klass, const void *data) ahc->dest_hi_mask =0x00000003 ; ahc->key_hi_mask =0x00000003 ; -/* -* Currently, it does not support the CRYPT command. Instead, it only -* sends an interrupt to notify the firmware that the crypt command -* has completed. It is a temporary workaround. -*/ - ahc->raise_crypt_interrupt_workaround = true; ahc->has_dma64 = true; }
Signature · 1 line
2.43.0
PATCH 6/15 tests/qtest/aspeed-hace: Test the crypto command on the AST1030
In reply to Jamin LinThe AST1030 reuses the AST2600 crypto engine, so it drives the same scatter-gather transfers and supports the same ECB/CBC/CTR modes. Reuse the crypto known-answer tests to cover it, registering the AST1030 with the same modes and scatter-gather flag as the AST2600.
--- tests/qtest/aspeed_hace-test.c | 6 ++++++ 1 file changed, 6 insertions(+)
tests/qtest/aspeed_hace-test.c +6 -0
@@ -210,6 +210,12 @@ int main(int argc, char **argv)qtest_add_func ("ast1030/hace/sha384_accum" , test_sha384_accum_ast1030);qtest_add_func ("ast1030/hace/sha256_accum" , test_sha256_accum_ast1030); +/* The AST1030 reuses the AST2600 crypto engine (scatter-gather, CTR). */ +aspeed_add_crypto_tests ("ast1030" ,"-machine ast1030-evb" ,0x7e6d0000 , +0x00000000 , +CRYPT_MODE_ECB |CRYPT_MODE_CBC |CRYPT_MODE_CTR , + true); +qtest_add_func ("ast2600/hace/addresses" , test_addresses_ast2600);qtest_add_func ("ast2600/hace/sha512" , test_sha512_ast2600);qtest_add_func ("ast2600/hace/sha384" , test_sha384_ast2600);
Signature · 1 line
2.43.0
PATCH 3/15 hw/misc/aspeed_hace: Support scatter-gather mode for the crypto command
In reply to Jamin LinThe AST2600 and later crypto engines drive the source and destination through scatter-gather lists (HACE10[18]/[19]) rather than the single contiguous buffers used by the AST2500 direct access mode. Each SG list entry is a length word (SG_LIST_LEN_LAST marks the final entry) followed by a DRAM address, matching the hash engine layout.
Add a crypt_prepare_sg() helper that gathers the source into / scatters the destination out of the bounce buffer by walking the SG list, and select it or the existing crypt_prepare_direct() from do_crypt_operation based on HACE10[18], mirroring the hash engine's direct/scatter-gather dispatch.
--- hw/misc/aspeed_hace.c | 70 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 6 deletions(-)
hw/misc/aspeed_hace.c +64 -6
@@ -610,13 +610,58 @@ static bool crypt_prepare_direct(AspeedHACEState *s, uint64_t addr, }/* -* Perform an AES/DES/3DES ECB/CBC operation in direct access mode: the source -* and destination are single contiguous buffers (HACE00/HACE04) and the IV/key -* come from the context buffer (HACE08). For CBC the resulting chaining IV is -* written back to the context buffer so the driver can continue the chain. +* Scatter-gather mode: the source/destination register points at an SG list +* whose entries are a length word (SG_LIST_LEN_LAST flags the final entry) +* followed by a DRAM address, matching the hash engine layout. Gather @len +* bytes into @buf, or scatter @buf back out when @to_dram is true. +* Returns true on success. +*/ +static bool crypt_prepare_sg (AspeedHACEState *s,uint64_t addr, +uint8_t *buf,uint32_t len,bool to_dram) +{ +uint32_t copied =0 ; +uint32_t sg_addr; +uint32_t sg_len; +uint32_t entry; +int i; + +for (i =0 ; i <ASPEED_HACE_MAX_SG && copied < len; i++) { + entry =address_space_ldl_le (&s->dram_as, addr, +MEMTXATTRS_UNSPECIFIED ,NULL ); + sg_addr =address_space_ldl_le (&s->dram_as, addr +SG_LIST_LEN_SIZE , +MEMTXATTRS_UNSPECIFIED ,NULL ); + sg_len = entry &SG_LIST_LEN_MASK ; + + sg_addr &=SG_LIST_ADDR_MASK ; + addr +=SG_LIST_ENTRY_SIZE ; + +if (sg_len > len - copied) { + sg_len = len - copied; + } +if (address_space_rw (&s->dram_as, sg_addr,MEMTXATTRS_UNSPECIFIED , + buf + copied, sg_len, to_dram)) { +return false; + } + copied += sg_len; + +if (entry &SG_LIST_LEN_LAST ) { +break ; + } + } + +return copied == len; +} + +/* +* Perform an AES/DES/3DES ECB/CBC operation. The source and destination are +* either single contiguous buffers (direct access mode) or scatter-gather +* lists (HACE10[18]/[19]), addressed by HACE00/HACE04; the IV/key come from +* the context buffer (HACE08). For CBC the resulting chaining IV is written +* back to the context buffer so the driver can continue the chain. */ static void do_crypt_operation (AspeedHACEState *s,uint32_t cmd) { +bool sg_mode = cmd &CRYPT_CMD_SRC_SG_CTRL ;uint32_t len = s->regs[R_CRYPT_DATA_LEN ];bool encrypt = cmd &CRYPT_CMD_ENCRYPT ;g_autoptr (QCryptoCipher) cipher =NULL ;
@@ -633,6 +678,7 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd)size_t iv_offset;size_t blocklen;size_t keylen; +bool status;if (len ==0 ) {return ;
@@ -679,8 +725,14 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd) src_buf =g_malloc0 (len); dst_buf =g_malloc0 (len); +/* Gather the source into the bounce buffer, per the selected mode. */ src_addr = s->regs[R_CRYPT_SRC ]; -if (!crypt_prepare_direct (s, src_addr, src_buf, len, false)) { +if (sg_mode) { + status =crypt_prepare_sg (s, src_addr, src_buf, len, false); + }else { + status =crypt_prepare_direct (s, src_addr, src_buf, len, false); + } +if (!status) {qemu_log_mask (LOG_GUEST_ERROR ,"%s: Failed to read src, addr=0x%" HWADDR_PRIx"\n" , __func__, src_addr);
@@ -709,8 +761,14 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd) } } +/* Scatter the result back out, per the selected mode. */ dst_addr = s->regs[R_CRYPT_DEST ]; -if (!crypt_prepare_direct (s, dst_addr, dst_buf, len, true)) { +if (sg_mode) { + status =crypt_prepare_sg (s, dst_addr, dst_buf, len, true); + }else { + status =crypt_prepare_direct (s, dst_addr, dst_buf, len, true); + } +if (!status) {qemu_log_mask (LOG_GUEST_ERROR ,"%s: Failed to write dst, addr=0x%" HWADDR_PRIx"\n" , __func__, dst_addr);
Signature · 1 line
2.43.0
PATCH 5/15 tests/qtest/aspeed-hace: Test the crypto command on the AST2600
In reply to Jamin LinExtend the crypto known-answer tests to cover the AST2600 crypto engine, which drives the source and destination through scatter-gather lists and adds CTR mode on top of the ECB/CBC modes shared with the AST2500.
Add a scatter-gather runner that describes each buffer with three non-adjacent fragments to exercise the gather/scatter path, add AES/DES/3DES CTR vectors (verifying the counter written back to the context buffer), and give aspeed_add_crypto_tests() a mode mask and a scatter-gather flag so each SoC registers exactly the modes and transfer method it supports. Register the AST2600 with ECB/CBC/CTR in scatter-gather mode.
--- tests/qtest/aspeed-hace-utils.h | 7 +- tests/qtest/aspeed-hace-utils.c | 211 +++++++++++++++++++++++++++++++- tests/qtest/aspeed_hace-test.c | 8 +- 3 files changed, 218 insertions(+), 8 deletions(-)
tests/qtest/aspeed-hace-utils.h +5 -2
@@ -85,15 +85,18 @@ void aspeed_test_addresses(const char *machine, const uint32_t base,enum {CRYPT_MODE_ECB =1 <<0 ,CRYPT_MODE_CBC =1 <<1 , +CRYPT_MODE_CTR =1 <<2 , };/* * Register the crypto known-answer tests that @modes selects (a mask of * CRYPT_MODE_*) for the given machine. Each test is named -* "<prefix>/hace/crypto/<mode>". +* "<prefix>/hace/crypto/<mode>". @sg selects scatter-gather mode (used by the +* AST2600 and later) instead of the AST2500 direct access mode. */ void aspeed_add_crypto_tests (const char *prefix,const char *machine, -uint32_t base,uint64_t dram,uint32_t modes); +uint32_t base,uint64_t dram,uint32_t modes, +bool sg);#endif /* TESTS_ASPEED_HACE_UTILS_H */
tests/qtest/aspeed-hace-utils.c +206 -5
277 lines not on this page · raw message
tests/qtest/aspeed_hace-test.c +7 -1
@@ -224,6 +224,12 @@ int main(int argc, char **argv)qtest_add_func ("ast2600/hace/sha384_accum" , test_sha384_accum_ast2600);qtest_add_func ("ast2600/hace/sha256_accum" , test_sha256_accum_ast2600); +/* The AST2600 crypto engine uses scatter-gather mode and adds CTR. */ +aspeed_add_crypto_tests ("ast2600" ,"-machine ast2600-evb" ,0x1e6d0000 , +0x80000000 , +CRYPT_MODE_ECB |CRYPT_MODE_CBC |CRYPT_MODE_CTR , + true); +qtest_add_func ("ast2500/hace/addresses" , test_addresses_ast2500);qtest_add_func ("ast2500/hace/sha512" , test_sha512_ast2500);qtest_add_func ("ast2500/hace/sha256" , test_sha256_ast2500);
@@ -233,7 +239,7 @@ int main(int argc, char **argv)* The AST2500 crypto engine uses direct access mode and supports ECB/CBC. */ aspeed_add_crypto_tests ("ast2500" ,"-machine ast2500-evb" ,0x1e6e3000 , -0x80000000 ,CRYPT_MODE_ECB |CRYPT_MODE_CBC ); +0x80000000 ,CRYPT_MODE_ECB |CRYPT_MODE_CBC , false);qtest_add_func ("ast2400/hace/addresses" , test_addresses_ast2400);qtest_add_func ("ast2400/hace/sha512" , test_sha512_ast2400);
Signature · 1 line
2.43.0
PATCH 1/15 hw/misc/aspeed_hace: Support the crypto command in direct access mode
In reply to Jamin LinThe crypt command register was previously stubbed out. Implement it for the direct access mode, where HACE00/HACE04 point directly at contiguous source and destination buffers. AES-128/192/256, DES and 3DES are supported in ECB and CBC modes via the qcrypto cipher API; the IV and key are read from the context buffer (HACE08) and, for CBC, the resulting chaining IV is written back to the context.
The completion interrupt is now raised for every HACE variant as the hardware does, which fixes the crypt command hang on the AST2500, AST2600 and AST1030. The AST2700 crypto engine still needs 64-bit DMA and AES-GCM, which are added later, so it keeps its temporary interrupt-only workaround until then.
For debugging, the context, source and destination buffers are dumped through the existing aspeed_hace_hexdump trace event (disabled by default). CTR mode, scatter-gather mode and AES-GCM are added separately.
--- hw/misc/aspeed_hace.c | 276 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 262 insertions(+), 14 deletions(-)
hw/misc/aspeed_hace.c +262 -14
@@ -18,11 +18,44 @@#include "qapi/error.h" #include "migration/vmstate.h" #include "crypto/hash.h" +#include "crypto/cipher.h" #include "hw/core/qdev-properties.h" #include "hw/core/irq.h" #include "trace.h" -#define R_CRYPT_CMD (0x10 / 4) +/* Crypto engine registers */ +#define R_CRYPT_SRC (0x00 / 4) +#define R_CRYPT_DEST (0x04 / 4) +#define R_CRYPT_CONTEXT (0x08 / 4) +#define R_CRYPT_DATA_LEN (0x0c / 4) +/* HACE0C[27:0] holds the crypto data length */ +#define CRYPT_DATA_LEN_MASK 0x0FFFFFFF +#define R_CRYPT_CMD (0x10 / 4) +/* Crypto engine command register (HACE10) bits */ +#define CRYPT_CMD_ENCRYPT BIT(7) +#define CRYPT_CMD_ISR_EN BIT(12) +#define CRYPT_CMD_DES_SELECT BIT(16) +#define CRYPT_CMD_TRIPLE_DES BIT(17) +#define CRYPT_CMD_SRC_SG_CTRL BIT(18) +#define CRYPT_CMD_DST_SG_CTRL BIT(19) +/* Operation mode HACE10[6:4] */ +#define CRYPT_CMD_OP_MODE_MASK (0x7 << 4) +#define CRYPT_CMD_ECB (0x0 << 4) +#define CRYPT_CMD_CBC (0x1 << 4) +/* AES key length HACE10[3:2] */ +#define CRYPT_CMD_AES_KEY_LEN_MASK (0x3 << 2) +#define CRYPT_CMD_AES256 (0x2 << 2) +#define CRYPT_CMD_AES192 (0x1 << 2) +#define CRYPT_CMD_AES128 (0x0 << 2) + +/* +* Crypto context buffer layout (HACE08). The IV is at the start of the buffer +* (DES places its 8 byte IV at offset 8) and the cipher key at offset 0x10. +*/ +#define CRYPT_CTX_IV_OFFSET 0x00 +#define CRYPT_CTX_DES_IV_OFFSET 0x08 +#define CRYPT_CTX_KEY_OFFSET 0x10 +#define CRYPT_CTX_SIZE 0x30#define R_STATUS (0x1c / 4)#define HASH_IRQ BIT(9)
@@ -501,6 +534,209 @@ static void do_hash_operation(AspeedHACEState *s, int algo, bool sg_mode, } } +static bool crypt_aes_alg (uint32_t cmd,QCryptoCipherAlgo *alg,size_t *keylen) +{ +switch (cmd &CRYPT_CMD_AES_KEY_LEN_MASK ) { +case CRYPT_CMD_AES128 : + *alg =QCRYPTO_CIPHER_ALGO_AES_128 ; + *keylen =16 ; +break ; +case CRYPT_CMD_AES192 : + *alg =QCRYPTO_CIPHER_ALGO_AES_192 ; + *keylen =24 ; +break ; +case CRYPT_CMD_AES256 : + *alg =QCRYPTO_CIPHER_ALGO_AES_256 ; + *keylen =32 ; +break ; +default : +return false; + } + +return true; +} + +/* +* Decode the crypto command register into a libqcrypto algorithm/mode pair +* and the block/IV geometry. Returns false for unsupported selections. +*/ +static bool crypt_decode_cmd (uint32_t cmd,QCryptoCipherAlgo *alg, +QCryptoCipherMode *mode,size_t *keylen, +size_t *blocklen,size_t *iv_offset) +{ +if (cmd &CRYPT_CMD_DES_SELECT ) { + *blocklen =8 ; + *iv_offset =CRYPT_CTX_DES_IV_OFFSET ; +if (cmd &CRYPT_CMD_TRIPLE_DES ) { + *alg =QCRYPTO_CIPHER_ALGO_3DES ; + *keylen =24 ; + }else { + *alg =QCRYPTO_CIPHER_ALGO_DES ; + *keylen =8 ; + } + }else { + *blocklen =16 ; + *iv_offset =CRYPT_CTX_IV_OFFSET ; +if (!crypt_aes_alg (cmd, alg, keylen)) { +return false; + } + } + +switch (cmd &CRYPT_CMD_OP_MODE_MASK ) { +case CRYPT_CMD_ECB : + *mode =QCRYPTO_CIPHER_MODE_ECB ; +break ; +case CRYPT_CMD_CBC : + *mode =QCRYPTO_CIPHER_MODE_CBC ; +break ; +default : +return false; + } + +return true; +} + +/* +* Direct access mode: the source/destination register (HACE00/HACE04) points +* at a single contiguous buffer in DRAM. Copy @len bytes between it and the +* bounce buffer @buf; when @to_dram is true @buf is written out, otherwise it +* is read in. Returns true on success. +*/ +static bool crypt_prepare_direct (AspeedHACEState *s,uint64_t addr, +uint8_t *buf,uint32_t len,bool to_dram) +{ +return !address_space_rw (&s->dram_as, addr,MEMTXATTRS_UNSPECIFIED , + buf, len, to_dram); +} + +/* +* Perform an AES/DES/3DES ECB/CBC operation in direct access mode: the source +* and destination are single contiguous buffers (HACE00/HACE04) and the IV/key +* come from the context buffer (HACE08). For CBC the resulting chaining IV is +* written back to the context buffer so the driver can continue the chain. +*/ +static void do_crypt_operation (AspeedHACEState *s,uint32_t cmd) +{ +uint32_t len = s->regs[R_CRYPT_DATA_LEN ]; +bool encrypt = cmd &CRYPT_CMD_ENCRYPT ; +g_autoptr (QCryptoCipher) cipher =NULL ; + g_autofree uint8_t *src_buf = NULL; + g_autofree uint8_t *dst_buf = NULL; +uint8_t ctx[CRYPT_CTX_SIZE ]; +Error *local_err =NULL ; +QCryptoCipherMode mode; +QCryptoCipherAlgo alg; +const uint8_t *next_iv; +uint64_t ctx_addr; +uint64_t src_addr; +uint64_t dst_addr; +size_t iv_offset; +size_t blocklen; +size_t keylen; + +if (len ==0 ) { +return ; + } + +if (!crypt_decode_cmd (cmd, &alg, &mode, &keylen, &blocklen, &iv_offset)) { +qemu_log_mask (LOG_UNIMP , +"%s: Unsupported crypt command 0x%x\n" , __func__, cmd); +return ; + } + +/* Fetch the IV and key from the context buffer in DRAM. */ + ctx_addr = s->regs[R_CRYPT_CONTEXT ]; +if (address_space_read (&s->dram_as, ctx_addr,MEMTXATTRS_UNSPECIFIED , + ctx,sizeof (ctx))) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Failed to read context, addr=0x%" HWADDR_PRIx"\n" , + __func__, ctx_addr); +return ; + } + +if (trace_event_get_state_backends (TRACE_ASPEED_HACE_HEXDUMP )) { +hace_hexdump ("context" , (char *)ctx,sizeof (ctx)); + } + + cipher =qcrypto_cipher_new (alg, mode, ctx +CRYPT_CTX_KEY_OFFSET , keylen, + &local_err); +if (cipher ==NULL ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: qcrypto cipher new failed: %s\n" , + __func__,error_get_pretty (local_err)); +error_free (local_err); +return ; + } + +if (mode !=QCRYPTO_CIPHER_MODE_ECB && +qcrypto_cipher_setiv (cipher, ctx + iv_offset, blocklen, + &local_err) <0 ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: qcrypto cipher setiv failed: %s\n" , + __func__,error_get_pretty (local_err)); +error_free (local_err); +return ; + } + + src_buf =g_malloc0 (len); + dst_buf =g_malloc0 (len); + + src_addr = s->regs[R_CRYPT_SRC ]; +if (!crypt_prepare_direct (s, src_addr, src_buf, len, false)) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Failed to read src, addr=0x%" HWADDR_PRIx"\n" , + __func__, src_addr); +return ; + } + +if (trace_event_get_state_backends (TRACE_ASPEED_HACE_HEXDUMP )) { +hace_hexdump ("src" , (char *)src_buf, len); + } + +if (encrypt) { +if (qcrypto_cipher_encrypt (cipher, src_buf, dst_buf, len, + &local_err) <0 ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: encrypt failed: %s\n" , + __func__,error_get_pretty (local_err)); +error_free (local_err); +return ; + } + }else { +if (qcrypto_cipher_decrypt (cipher, src_buf, dst_buf, len, + &local_err) <0 ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: decrypt failed: %s\n" , + __func__,error_get_pretty (local_err)); +error_free (local_err); +return ; + } + } + + dst_addr = s->regs[R_CRYPT_DEST ]; +if (!crypt_prepare_direct (s, dst_addr, dst_buf, len, true)) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Failed to write dst, addr=0x%" HWADDR_PRIx"\n" , + __func__, dst_addr); +return ; + } + +if (trace_event_get_state_backends (TRACE_ASPEED_HACE_HEXDUMP )) { +hace_hexdump ("dst" , (char *)dst_buf, len); + } + +if (mode ==QCRYPTO_CIPHER_MODE_CBC ) { +/* +* CBC chains on the last ciphertext block: the final block of the +* output when encrypting, or of the input when decrypting. Write it +* back as the IV for the next request. +*/ + next_iv = (encrypt ? dst_buf : src_buf) + len - blocklen; +if (address_space_write (&s->dram_as, ctx_addr + iv_offset, +MEMTXATTRS_UNSPECIFIED , next_iv, blocklen)) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Failed to write IV, addr=0x%" HWADDR_PRIx"\n" , + __func__, ctx_addr + iv_offset); + } + } +} +static uint64_t aspeed_hace_read (void *opaque,hwaddr addr,unsigned int size) {AspeedHACEState *s =ASPEED_HACE (opaque);
@@ -531,16 +767,22 @@ static void aspeed_hace_write(void *opaque, hwaddr addr, uint64_t data,qemu_irq_lower (s->irq); } } -if (ahc->raise_crypt_interrupt_workaround) { -if (data &CRYPT_IRQ ) { - data &= ~CRYPT_IRQ ; +if (data &CRYPT_IRQ ) { + data &= ~CRYPT_IRQ ; -if (s->regs[addr] &CRYPT_IRQ ) { -qemu_irq_lower (s->irq); - } +if (s->regs[addr] &CRYPT_IRQ ) { +qemu_irq_lower (s->irq); } }break ; +case R_CRYPT_SRC : +case R_CRYPT_DEST : +case R_CRYPT_CONTEXT : + data &= ahc->src_mask; +break ; +case R_CRYPT_DATA_LEN : + data &=CRYPT_DATA_LEN_MASK ; +break ;case R_HASH_SRC : data &= ahc->src_mask;break ;
@@ -589,13 +831,19 @@ static void aspeed_hace_write(void *opaque, hwaddr addr, uint64_t data,break ; }case R_CRYPT_CMD : -qemu_log_mask (LOG_UNIMP ,"%s: Crypt commands not implemented\n" , - __func__); -if (ahc->raise_crypt_interrupt_workaround) { - s->regs[R_STATUS ] |=CRYPT_IRQ ; -if (data &CRYPT_IRQ_EN ) { -qemu_irq_raise (s->irq); - } +/* +* The AST2700 crypto engine needs 64-bit DMA and AES-GCM, which are +* added later; until then it keeps the temporary workaround of only +* raising the completion interrupt without running the command. +*/ +if (!ahc->raise_crypt_interrupt_workaround) { +do_crypt_operation (s, data); + } + +/* Hardware raises the crypt interrupt once the command finishes. */ + s->regs[R_STATUS ] |=CRYPT_IRQ ; +if (data &CRYPT_CMD_ISR_EN ) { +qemu_irq_raise (s->irq); }break ;case R_HASH_SRC_HI :
Signature · 1 line
2.43.0
PATCH 12/15 hw/misc/aspeed_hace: Support the AES-GCM mode for the crypto command
In reply to Jamin LinImplement the AES-GCM mode (HACE10[6:4] = 0b101) used by the AST2700 crypto engine: decode the GCM selection, read the 96-bit IV from the context buffer, operate on the exact data length (GCM handles a partial final block itself), and write the 128-bit authentication tag to the tag buffer (HACE18/HACE8C). The hardware GCM path is only used without associated data (the driver falls back to software otherwise), so AAD is not modelled and a non-zero HACE14 is reported as unimplemented.
--- hw/misc/aspeed_hace.c | 70 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 8 deletions(-)
hw/misc/aspeed_hace.c +62 -8
@@ -31,6 +31,9 @@/* HACE0C[27:0] holds the crypto data length */ #define CRYPT_DATA_LEN_MASK 0x0FFFFFFF#define R_CRYPT_CMD (0x10 / 4) +/* AES-GCM associated data length (HACE14) and tag write buffer (HACE18) */ +#define R_CRYPT_GCM_ADD_LEN (0x14 / 4) +#define R_CRYPT_GCM_TAG (0x18 / 4)/* Crypto engine command register (HACE10) bits */ #define CRYPT_CMD_ENCRYPT BIT(7)#define CRYPT_CMD_ISR_EN BIT(12)
@@ -43,6 +46,7 @@#define CRYPT_CMD_ECB (0x0 << 4)#define CRYPT_CMD_CBC (0x1 << 4)#define CRYPT_CMD_CTR (0x4 << 4) +#define CRYPT_CMD_GCM (0x5 << 4)/* AES key length HACE10[3:2] */ #define CRYPT_CMD_AES_KEY_LEN_MASK (0x3 << 2)#define CRYPT_CMD_AES256 (0x2 << 2)
@@ -58,10 +62,15 @@#define CRYPT_CTX_KEY_OFFSET 0x10#define CRYPT_CTX_SIZE 0x30 +/* AES-GCM uses a 96-bit IV and a 128-bit authentication tag */ +#define CRYPT_GCM_IV_LEN 12 +#define CRYPT_GCM_TAG_LEN 16 +/* AST2700 64-bit DMA high address registers for the crypto command */ #define R_CRYPT_SRC_HI (0x80 / 4)#define R_CRYPT_DEST_HI (0x84 / 4)#define R_CRYPT_CONTEXT_HI (0x88 / 4) +#define R_CRYPT_GCM_TAG_HI (0x8c / 4)#define R_STATUS (0x1c / 4)#define HASH_IRQ BIT(9)
@@ -598,6 +607,9 @@ static bool crypt_decode_cmd(uint32_t cmd, QCryptoCipherAlgo *alg,case CRYPT_CMD_CTR : *mode =QCRYPTO_CIPHER_MODE_CTR ;break ; +case CRYPT_CMD_GCM : + *mode =QCRYPTO_CIPHER_MODE_GCM ; +break ;default :return false; }
@@ -691,11 +703,12 @@ static uint64_t crypt_get_addr(AspeedHACEState *s, int reg, int reg_hi) }/* -* Perform an AES/DES/3DES ECB/CBC operation. The source and destination are -* either single contiguous buffers (direct access mode) or scatter-gather -* lists (HACE10[18]/[19]), addressed by HACE00/HACE04; the IV/key come from -* the context buffer (HACE08). For CBC the resulting chaining IV is written -* back to the context buffer so the driver can continue the chain. +* Perform an AES/DES/3DES ECB/CBC/CTR or AES-GCM operation. The source and +* destination are either single contiguous buffers (direct access mode) or +* scatter-gather lists (HACE10[18]/[19]), addressed by HACE00/HACE04; the +* IV/key come from the context buffer (HACE08). For CBC and CTR the resulting +* chaining state is written back to the context buffer so the driver can +* continue; for GCM the authentication tag is written to the tag buffer. */ static void do_crypt_operation (AspeedHACEState *s,uint32_t cmd) {
@@ -705,6 +718,7 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd)g_autoptr (QCryptoCipher) cipher =NULL ; g_autofree uint8_t *src_buf = NULL; g_autofree uint8_t *dst_buf = NULL; +uint8_t tag[CRYPT_GCM_TAG_LEN ];uint8_t ctx[CRYPT_CTX_SIZE ];Error *local_err =NULL ;QCryptoCipherMode mode;
@@ -713,10 +727,13 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd)uint64_t ctx_addr;uint64_t src_addr;uint64_t dst_addr; +uint64_t tag_addr; +uint32_t aad_len;size_t iv_offset;size_t blocklen;size_t buf_len;size_t keylen; +size_t ivlen;bool status;if (len ==0 ) {
@@ -729,6 +746,20 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd)return ; } +/* GCM uses a 96-bit IV; the block modes use a full-block IV. */ + ivlen = (mode ==QCRYPTO_CIPHER_MODE_GCM ) ?CRYPT_GCM_IV_LEN : blocklen; + +/* +* The hardware GCM path is only exercised without associated data (the +* driver falls back to software when there is any), so AAD is not modelled. +*/ + aad_len = s->regs[R_CRYPT_GCM_ADD_LEN ] &CRYPT_DATA_LEN_MASK ; +if (mode ==QCRYPTO_CIPHER_MODE_GCM && aad_len !=0 ) { +qemu_log_mask (LOG_UNIMP , +"%s: GCM associated data is not implemented\n" , __func__); +return ; + } +/* Fetch the IV and key from the context buffer in DRAM. */ ctx_addr =crypt_get_addr (s,R_CRYPT_CONTEXT ,R_CRYPT_CONTEXT_HI ); if (address_space_read(&s->dram_as, ctx_addr, MEMTXATTRS_UNSPECIFIED,
@@ -753,7 +784,7 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd) }if (mode !=QCRYPTO_CIPHER_MODE_ECB && -qcrypto_cipher_setiv (cipher, ctx + iv_offset, blocklen, +qcrypto_cipher_setiv (cipher, ctx + iv_offset, ivlen, &local_err) <0 ) {qemu_log_mask (LOG_GUEST_ERROR ,"%s: qcrypto cipher setiv failed: %s\n" , __func__,error_get_pretty (local_err));
@@ -764,9 +795,11 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd)/* * Round the working buffers up to a whole block. Block modes are already * block-aligned; the stream-like CTR mode may leave a partial final block -* that the engine still processes a full block at a time. +* that the engine still processes a full block at a time. GCM handles a +* partial final block itself, so it operates on the exact length. */ - buf_len =QEMU_ALIGN_UP (len, blocklen); + buf_len = (mode ==QCRYPTO_CIPHER_MODE_GCM ) ? + len :QEMU_ALIGN_UP (len, blocklen); src_buf =g_malloc0 (buf_len); dst_buf =g_malloc0 (buf_len);
@@ -850,6 +883,24 @@ static void do_crypt_operation(AspeedHACEState *s, uint32_t cmd)"%s: Failed to write IV, addr=0x%" HWADDR_PRIx"\n" , __func__, ctx_addr + iv_offset); } + }else if (mode ==QCRYPTO_CIPHER_MODE_GCM ) { +/* +* GCM authenticates the message and writes the resulting tag to the +* dedicated tag buffer (HACE18/HACE8C). +*/ +if (qcrypto_cipher_gettag (cipher, tag,sizeof (tag), &local_err) <0 ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: qcrypto cipher gettag failed: " +"%s\n" , __func__,error_get_pretty (local_err)); +error_free (local_err); +return ; + } + tag_addr =crypt_get_addr (s,R_CRYPT_GCM_TAG ,R_CRYPT_GCM_TAG_HI ); +if (address_space_write (&s->dram_as, tag_addr,MEMTXATTRS_UNSPECIFIED , + tag,sizeof (tag))) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Failed to write tag, addr=0x%" HWADDR_PRIx"\n" , + __func__, tag_addr); + } } }
@@ -980,6 +1031,9 @@ static void aspeed_hace_write(void *opaque, hwaddr addr, uint64_t data,case R_CRYPT_CONTEXT_HI : data &= ahc->key_hi_mask;break ; +case R_CRYPT_GCM_TAG_HI : + data &= ahc->dest_hi_mask; +break ;default :break ; }
Signature · 1 line
2.43.0
PATCH 7/15 crypto/cipher: Add GCM to QCryptoCipherMode
In reply to Jamin LinIntroduce the GCM cipher mode so authenticated encryption can be built on top of the existing qcrypto_cipher API. GCM is an IV-based mode, so register it in mode_need_iv. No backend advertises it yet, so it stays unsupported until a backend and the AAD/tag helpers are added in the following patches.
--- qapi/crypto.json | 4 +++- crypto/cipher.c | 1 + 2 files changed, 4 insertions(+), 1 deletion(-)
qapi/crypto.json +3 -1
@@ -121,10 +121,12 @@ # # @ctr: Counter (Since 2.8) # +# @gcm: Galois/Counter Mode (Since 11.2) +# # Since: 2.6 ## { 'enum': 'QCryptoCipherMode', - 'data': ['ecb', 'cbc', 'xts', 'ctr']} + 'data': ['ecb', 'cbc', 'xts', 'ctr', 'gcm']} ## # @QCryptoIVGenAlgo:
crypto/cipher.c +1 -0
@@ -66,6 +66,7 @@ static const bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = { [QCRYPTO_CIPHER_MODE_CBC] = true, [QCRYPTO_CIPHER_MODE_XTS] = true, [QCRYPTO_CIPHER_MODE_CTR] = true, + [QCRYPTO_CIPHER_MODE_GCM] = true, };
Signature · 1 line
2.43.0
Re: [PATCH v1 07/15] crypto/cipher: Add GCM to QCryptoCipherMode
Quoted 11 lines · Jamin Lin
Introduce the GCM cipher mode so authenticated encryption can be built on top of the existing
qcrypto_cipherAPI. GCM is an IV-based mode, so register it inmode_need_iv. No backend advertises it yet, so it stays unsupported until a backend and the AAD/tag helpers are added in the following patches.Signed-off-by Jamin Lin <[email protected]>--- qapi/crypto.json | 4 +++- crypto/cipher.c | 1 + 2 files changed, 4 insertions(+), 1 deletion(-)
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
Re: [PATCH v1 08/15] crypto/cipher: Add setaad/gettag for AEAD modes
In reply to Jamin LinQuoted 7 lines · Jamin Lin
AEAD modes such as GCM authenticate optional associated data (AAD) and produce an authentication tag, which the block-cipher encrypt/decrypt interface cannot express. Add
qcrypto_cipher_setaad()andqcrypto_cipher_gettag()plus the matching backend driver hooks. The generic front-end reports an error when the selected mode's driver does not implement them, so calling them on a non-AEAD mode fails cleanly.
Quoted 6 lines
Signed-off-by Jamin Lin <[email protected]>--- crypto/cipherpriv.h | 8 ++++++++ include/crypto/cipher.h | 36 ++++++++++++++++++++++++++++++++++++ crypto/cipher.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+)
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
Re: [PATCH v1 09/15] crypto/cipher-gcrypt: Implement AES-GCM
In reply to Jamin LinQuoted 81 lines · Jamin Lin
Map
QCRYPTO_CIPHER_MODE_GCMtoGCRY_CIPHER_MODE_GCMand advertise it inqcrypto_cipher_supports()for 128-bit block ciphers. Add a GCM driver whose setiv accepts the (typically 96-bit) nonce, whose encrypt/decrypt do not require block-aligned lengths, and which implements setaad viagcry_cipher_authenticate()and gettag viagcry_cipher_gettag().Signed-off-by Jamin Lin <[email protected]>--- crypto/cipher-gcrypt.c.inc | 101 +++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+)crypto/cipher-gcrypt.c.inc +47
-0@@ -65,6 +65,8 @@ static int qcrypto_cipher_mode_to_gcry_mode(QCryptoCipherMode mode) return GCRY_CIPHER_MODE_CBC; case QCRYPTO_CIPHER_MODE_CTR: return GCRY_CIPHER_MODE_CTR; + case QCRYPTO_CIPHER_MODE_GCM: + return GCRY_CIPHER_MODE_GCM; default: return GCRY_CIPHER_MODE_NONE; }@@ -104,6 +106,10 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgo alg, case QCRYPTO_CIPHER_MODE_XTS: case QCRYPTO_CIPHER_MODE_CTR: return true; + case QCRYPTO_CIPHER_MODE_GCM: + /* GCM requires a 128-bit block cipher. */ + return gcry_cipher_get_algo_blklen( + qcrypto_cipher_alg_to_gcry_alg(alg)) == 16; default: return false; }@@ -228,6 +234,99 @@ static const struct QCryptoCipherDriver qcrypto_gcrypt_ctr_driver = { .cipher_free = qcrypto_gcrypt_ctx_free, }; +/* + * GCM is an AEAD stream mode: the IV/nonce need not match the block size, + * the message length need not be a multiple of the block size, associated + * data is fed with gcry_cipher_authenticate() and the authentication tag is + * read back with gcry_cipher_gettag(). + */ +static int qcrypto_gcrypt_gcm_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) +{ + QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base); + gcry_error_t err; + + gcry_cipher_reset(ctx->handle); + err = gcry_cipher_setiv(ctx->handle, iv, niv); + if (err != 0) { + error_setg(errp, "Cannot set IV: %s", gcry_strerror(err)); + return -1; + } + + return 0; +} +static int qcrypto_gcrypt_gcm_encrypt(QCryptoCipher *cipher, const void *in, + void *out, size_t len, Error **errp) +{ + QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base); + gcry_error_t err; + + err = gcry_cipher_encrypt(ctx->handle, out, len, in, len); + if (err != 0) { + error_setg(errp, "Cannot encrypt data: %s", gcry_strerror(err)); + return -1; + } + + return 0; +} + +static int qcrypto_gcrypt_gcm_decrypt(QCryptoCipher *cipher, const void *in, + void *out, size_t len, Error **errp) +{ + QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver qcrypto_gcrypt_ctr_driver = { · line 288
Quoted 10 lines
crypto/cipher-gcrypt.c.inc +10
-0+ gcry_error_t err; + + err = gcry_cipher_decrypt(ctx->handle, out, len, in, len); + if (err != 0) { + error_setg(errp, "Cannot decrypt data: %s", gcry_strerror(err)); + return -1; + } + + return 0; +}
Is there a reason you can't use the common qcrypto_gcrypt_setiv, qcrypto_gcrypt_decrypt and qcrypto_gcrypt_encrypt methods ?
Quoted 41 lines
crypto/cipher-gcrypt.c.inc +27
-0+ +static int qcrypto_gcrypt_gcm_gettag(QCryptoCipher *cipher, + uint8_t *tag, size_t len, Error **errp) +{ + QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base); + gcry_error_t err; + + err = gcry_cipher_gettag(ctx->handle, tag, len); + if (err != 0) { + error_setg(errp, "Cannot get tag: %s", gcry_strerror(err)); + return -1; + } + + return 0; +} + +static const struct QCryptoCipherDriver qcrypto_gcrypt_gcm_driver = { + .cipher_encrypt = qcrypto_gcrypt_gcm_encrypt, + .cipher_decrypt = qcrypto_gcrypt_gcm_decrypt, + .cipher_setiv = qcrypto_gcrypt_gcm_setiv, + .cipher_setaad = qcrypto_gcrypt_gcm_setaad, + .cipher_gettag = qcrypto_gcrypt_gcm_gettag, + .cipher_free = qcrypto_gcrypt_ctx_free, +}; + static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgo alg, QCryptoCipherMode mode, const uint8_t *key,@@ -259,6 +358,8 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgo alg, if (mode == QCRYPTO_CIPHER_MODE_CTR) { drv = &qcrypto_gcrypt_ctr_driver; + } else if (mode == QCRYPTO_CIPHER_MODE_GCM) { + drv = &qcrypto_gcrypt_gcm_driver; } else { drv = &qcrypto_gcrypt_driver; }Signature · 1 line
2.43.0
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
Quoted 4 lines · Jamin Lin
qapi/crypto.json | 4 +- crypto/cipherpriv.h | 8 + include/crypto/cipher.h | 36 + include/hw/misc/aspeed_hace.h | 1 -
Quoted 8 lines
tests/qtest/aspeed-hace-utils.h | 20 + crypto/cipher.c | 32 + hw/misc/aspeed_hace.c | 454 +++++++++++- tests/qtest/aspeed-hace-utils.c | 662 ++++++++++++++++++ tests/qtest/aspeed_hace-test.c | 18 + tests/qtest/ast2700-hace-test.c | 9 + tests/unit/test-crypto-cipher.c | 240 +++++++ crypto/cipher-gcrypt.c.inc | 101 +++
Both nettle and gnutls also support GCM mode, so can you impl those too.
Quoted 9 lines
.../aarch64/test_aspeed_ast2700a1.py | 5 - .../aarch64/test_aspeed_ast2700a2.py | 5 - .../aarch64/test_aspeed_ast2700fc.py | 5 - 15 files changed, 1563 insertions(+), 37 deletions(-)Signature · 1 line
2.43.0
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
RE: [PATCH v1 09/15] crypto/cipher-gcrypt: Implement AES-GCM
In reply to Daniel P. BerrangéHi Daniel
Quoted 97 lines
Subject: Re: [PATCH v1 09/15]
crypto/cipher-gcrypt: Implement AES-GCMQuoted 1 line · Jamin Lin
Map
QCRYPTO_CIPHER_MODE_GCMtoGCRY_CIPHER_MODE_GCMandadvertise it
crypto/cipher-gcrypt.c.inc static int
Quoted 18 lines
in
qcrypto_cipher_supports()for 128-bit block ciphers. Add a GCM driver whose setiv accepts the (typically 96-bit) nonce, whose encrypt/decrypt do not require block-aligned lengths, and which implements setaad viagcry_cipher_authenticate()and gettag viagcry_cipher_gettag().Signed-off-by Jamin Lin <[email protected]>--- crypto/cipher-gcrypt.c.inc | 101 +++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+)crypto/cipher-gcrypt.c.inc +0
-0@@ -65,6 +65,8 @@ static int
qcrypto_cipher_mode_to_gcry_mode(QCryptoCipherMode mode)crypto/cipher-gcrypt.c.inc bool qcrypto_cipher_supports(QCryptoCipherAlgo
Quoted 9 lines
crypto/cipher-gcrypt.c.inc +2
-0return GCRY_CIPHER_MODE_CBC; case QCRYPTO_CIPHER_MODE_CTR: return GCRY_CIPHER_MODE_CTR; + case QCRYPTO_CIPHER_MODE_GCM: + return GCRY_CIPHER_MODE_GCM; default: return GCRY_CIPHER_MODE_NONE; }@@ -104,6 +106,10 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgoalg,
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver
Quoted 11 lines
crypto/cipher-gcrypt.c.inc +4
-0case QCRYPTO_CIPHER_MODE_XTS: case QCRYPTO_CIPHER_MODE_CTR: return true; + case QCRYPTO_CIPHER_MODE_GCM: + /* GCM requires a 128-bit block cipher. */ + return gcry_cipher_get_algo_blklen( + qcrypto_cipher_alg_to_gcry_alg(alg)) == 16; default: return false; }@@ -228,6 +234,99 @@ static const struct QCryptoCipherDriver
qcrypto_gcrypt_ctr_driver= {Quoted 5 lines
crypto/cipher-gcrypt.c.inc +3
-0.cipher_free = qcrypto_gcrypt_ctx_free, }; +/* + * GCM is an AEAD stream mode: the IV/nonce need not match the block +size,crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver · line 248
Quoted 10 lines
crypto/cipher-gcrypt.c.inc +10
-0+ * the message length need not be a multiple of the block size, +associated + * data is fed with gcry_cipher_authenticate() and the authentication +tag is + * read back with gcry_cipher_gettag(). + */ +static int qcrypto_gcrypt_gcm_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) { + QCryptoCipherGcrypt *ctx = container_of(cipher,QCryptoCipherGcrypt, base);
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver qcrypto_gcrypt_ctr_driver = {
Quoted 13 lines
crypto/cipher-gcrypt.c.inc +12
-0+ gcry_error_t err; + + gcry_cipher_reset(ctx->handle); + err = gcry_cipher_setiv(ctx->handle, iv, niv); + if (err != 0) { + error_setg(errp, "Cannot set IV: %s", gcry_strerror(err)); + return -1; + } + + return 0; +} +static int qcrypto_gcrypt_gcm_encrypt(QCryptoCipher *cipher, const void*in,
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver · line 264
Quoted 3 lines
crypto/cipher-gcrypt.c.inc +3
-0+ void *out, size_t len, Error +**errp) { + QCryptoCipherGcrypt *ctx = container_of(cipher,QCryptoCipherGcrypt, base);
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver qcrypto_gcrypt_ctr_driver = {
Quoted 12 lines
crypto/cipher-gcrypt.c.inc +12
-0+ gcry_error_t err; + + err = gcry_cipher_encrypt(ctx->handle, out, len, in, len); + if (err != 0) { + error_setg(errp, "Cannot encrypt data: %s", gcry_strerror(err)); + return -1; + } + + return 0; +} + +static int qcrypto_gcrypt_gcm_decrypt(QCryptoCipher *cipher, const void*in,
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver · line 279
Quoted 3 lines
crypto/cipher-gcrypt.c.inc +3
-0+ void *out, size_t len, Error +**errp) { + QCryptoCipherGcrypt *ctx = container_of(cipher,QCryptoCipherGcrypt, base);
Quoted 14 lines
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver qcrypto_gcrypt_ctr_driver = { · line 304
Quoted 10 lines
crypto/cipher-gcrypt.c.inc +10
-0+ gcry_error_t err; + + err = gcry_cipher_decrypt(ctx->handle, out, len, in, len); + if (err != 0) { + error_setg(errp, "Cannot decrypt data: %s", gcry_strerror(err)); + return -1; + } + + return 0; +}Is there a reason you can't use the common
qcrypto_gcrypt_setiv,qcrypto_gcrypt_decryptandqcrypto_gcrypt_encryptmethods ?
Thanks for the review and suggestion.
The GCM path differs from the common methods in two input checks that GCM legitimately violates:
- qcrypto_gcrypt_setiv() rejects niv != blocksize, but GCM uses a
96-bit (12-byte) nonce.
- qcrypto_gcrypt_encrypt()/decrypt() reject a length that is not a multiple of the block size, but GCM must accept arbitrary lengths (e.g. the 60-byte NIST test vectors).
So reusing the common methods means relaxing those two checks for GCM, e.g.:
/* qcrypto_gcrypt_setiv() */
if (cipher->mode != QCRYPTO_CIPHER_MODE_GCM && niv != ctx->blocksize) {
error_setg(errp, "Expected IV size %zu not %zu", ctx->blocksize, niv);
return -1;
}
/* qcrypto_gcrypt_encrypt() / _decrypt() */
if (cipher->mode != QCRYPTO_CIPHER_MODE_GCM &&
(len & (ctx->blocksize - 1))) {
error_setg(errp, "Length %zu must be a multiple of block size %zu",
len, ctx->blocksize);
return -1;}
I kept them separate to avoid adding GCM special-cases to the shared block-cipher path, but I'm happy to switch the GCM driver to the common qcrypto_gcrypt_{setiv,encrypt,decrypt} (dropping the GCM-specific ones and keeping only the AEAD setaad/gettag hooks)
If you prefer. I'll make that change in v2 if that's the direction you'd like.
Thanks,
Jamin
Quoted 51 lines
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver · line 293
Quoted 4 lines
crypto/cipher-gcrypt.c.inc +4
-0+static int qcrypto_gcrypt_gcm_gettag(QCryptoCipher *cipher, + uint8_t *tag, size_t len, Error +**errp) { + QCryptoCipherGcrypt *ctx = container_of(cipher,QCryptoCipherGcrypt, base);
Quoted 11 lines
crypto/cipher-gcrypt.c.inc +11
-0+ gcry_error_t err; + + err = gcry_cipher_gettag(ctx->handle, tag, len); + if (err != 0) { + error_setg(errp, "Cannot get tag: %s", gcry_strerror(err)); + return -1; + } + + return 0; +} +crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver qcrypto_gcrypt_ctr_driver = {
Quoted 10 lines
crypto/cipher-gcrypt.c.inc +8
-0+static const struct QCryptoCipherDriver qcrypto_gcrypt_gcm_driver = { + .cipher_encrypt = qcrypto_gcrypt_gcm_encrypt, + .cipher_decrypt = qcrypto_gcrypt_gcm_decrypt, + .cipher_setiv = qcrypto_gcrypt_gcm_setiv, + .cipher_setaad = qcrypto_gcrypt_gcm_setaad, + .cipher_gettag = qcrypto_gcrypt_gcm_gettag, + .cipher_free = qcrypto_gcrypt_ctx_free, }; + static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgo alg,QCryptoCipherMode mode,
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver qcrypto_gcrypt_ctr_driver = { · line 332
Quoted 1 line
crypto/cipher-gcrypt.c.inc +0
-0const uint8_t *key,@@
Quoted 11 lines
-259,6 +358,8 @@ static QCryptoCipher
*qcrypto_cipher_ctx_new(QCryptoCipherAlgo alg,if (mode == QCRYPTO_CIPHER_MODE_CTR) { drv = &qcrypto_gcrypt_ctr_driver;crypto/cipher-gcrypt.c.inc +2
-1+ } else if (mode == QCRYPTO_CIPHER_MODE_GCM) { + drv = &qcrypto_gcrypt_gcm_driver; } else { drv = &qcrypto_gcrypt_driver; } --Quoted 3 lines
2.43.0
With regards,
Daniel
--
|: https://berrange.com ~~
https://hachyderm.io/@berrange :||: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
Re: [PATCH v1 09/15] crypto/cipher-gcrypt: Implement AES-GCM
Quoted 137 lines · Jamin Lin
Hi Daniel
Quoted 97 lines
Subject: Re: [PATCH v1 09/15]
crypto/cipher-gcrypt: Implement AES-GCMQuoted 1 line · Jamin Lin
Map
QCRYPTO_CIPHER_MODE_GCMtoGCRY_CIPHER_MODE_GCMandadvertise it
crypto/cipher-gcrypt.c.inc static int
Quoted 18 lines
in
qcrypto_cipher_supports()for 128-bit block ciphers. Add a GCM driver whose setiv accepts the (typically 96-bit) nonce, whose encrypt/decrypt do not require block-aligned lengths, and which implements setaad viagcry_cipher_authenticate()and gettag viagcry_cipher_gettag().Signed-off-by Jamin Lin <[email protected]>--- crypto/cipher-gcrypt.c.inc | 101 +++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+)crypto/cipher-gcrypt.c.inc +0
-0@@ -65,6 +65,8 @@ static int
qcrypto_cipher_mode_to_gcry_mode(QCryptoCipherMode mode)crypto/cipher-gcrypt.c.inc bool qcrypto_cipher_supports(QCryptoCipherAlgo
Quoted 9 lines
crypto/cipher-gcrypt.c.inc +2
-0return GCRY_CIPHER_MODE_CBC; case QCRYPTO_CIPHER_MODE_CTR: return GCRY_CIPHER_MODE_CTR; + case QCRYPTO_CIPHER_MODE_GCM: + return GCRY_CIPHER_MODE_GCM; default: return GCRY_CIPHER_MODE_NONE; }@@ -104,6 +106,10 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgoalg,
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver
Quoted 11 lines
crypto/cipher-gcrypt.c.inc +4
-0case QCRYPTO_CIPHER_MODE_XTS: case QCRYPTO_CIPHER_MODE_CTR: return true; + case QCRYPTO_CIPHER_MODE_GCM: + /* GCM requires a 128-bit block cipher. */ + return gcry_cipher_get_algo_blklen( + qcrypto_cipher_alg_to_gcry_alg(alg)) == 16; default: return false; }@@ -228,6 +234,99 @@ static const struct QCryptoCipherDriver
qcrypto_gcrypt_ctr_driver= {Quoted 5 lines
crypto/cipher-gcrypt.c.inc +3
-0.cipher_free = qcrypto_gcrypt_ctx_free, }; +/* + * GCM is an AEAD stream mode: the IV/nonce need not match the block +size,crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver · line 248
Quoted 10 lines
crypto/cipher-gcrypt.c.inc +10
-0+ * the message length need not be a multiple of the block size, +associated + * data is fed with gcry_cipher_authenticate() and the authentication +tag is + * read back with gcry_cipher_gettag(). + */ +static int qcrypto_gcrypt_gcm_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) { + QCryptoCipherGcrypt *ctx = container_of(cipher,QCryptoCipherGcrypt, base);
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver qcrypto_gcrypt_ctr_driver = {
Quoted 13 lines
crypto/cipher-gcrypt.c.inc +12
-0+ gcry_error_t err; + + gcry_cipher_reset(ctx->handle); + err = gcry_cipher_setiv(ctx->handle, iv, niv); + if (err != 0) { + error_setg(errp, "Cannot set IV: %s", gcry_strerror(err)); + return -1; + } + + return 0; +} +static int qcrypto_gcrypt_gcm_encrypt(QCryptoCipher *cipher, const void*in,
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver · line 264
Quoted 3 lines
crypto/cipher-gcrypt.c.inc +3
-0+ void *out, size_t len, Error +**errp) { + QCryptoCipherGcrypt *ctx = container_of(cipher,QCryptoCipherGcrypt, base);
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver qcrypto_gcrypt_ctr_driver = {
Quoted 12 lines
crypto/cipher-gcrypt.c.inc +12
-0+ gcry_error_t err; + + err = gcry_cipher_encrypt(ctx->handle, out, len, in, len); + if (err != 0) { + error_setg(errp, "Cannot encrypt data: %s", gcry_strerror(err)); + return -1; + } + + return 0; +} + +static int qcrypto_gcrypt_gcm_decrypt(QCryptoCipher *cipher, const void*in,
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver · line 279
Quoted 3 lines
crypto/cipher-gcrypt.c.inc +3
-0+ void *out, size_t len, Error +**errp) { + QCryptoCipherGcrypt *ctx = container_of(cipher,QCryptoCipherGcrypt, base);
Quoted 14 lines
crypto/cipher-gcrypt.c.inc static const struct QCryptoCipherDriver qcrypto_gcrypt_ctr_driver = { · line 304
Quoted 10 lines
crypto/cipher-gcrypt.c.inc +10
-0+ gcry_error_t err; + + err = gcry_cipher_decrypt(ctx->handle, out, len, in, len); + if (err != 0) { + error_setg(errp, "Cannot decrypt data: %s", gcry_strerror(err)); + return -1; + } + + return 0; +}Is there a reason you can't use the common
qcrypto_gcrypt_setiv,qcrypto_gcrypt_decryptandqcrypto_gcrypt_encryptmethods ?Thanks for the review and suggestion.
The GCM path differs from the common methods in two input checks that GCM legitimately violates:
-
qcrypto_gcrypt_setiv()rejects niv != blocksize, but GCM uses a96-bit (12-byte) nonce.- qcrypto_gcrypt_encrypt()/decrypt() reject a length that is not a multiple of the block size, but GCM must accept arbitrary lengths (e.g. the 60-byte NIST test vectors).
So reusing the common methods means relaxing those two checks for GCM, e.g.:
/*qcrypto_gcrypt_setiv()*/if (cipher->mode != QCRYPTO_CIPHER_MODE_GCM && niv != ctx->blocksize) { error_setg(errp, "Expected IV size %zu not %zu", ctx->blocksize, niv); return -1; } /* qcrypto_gcrypt_encrypt() / _decrypt() */ if (cipher->mode != QCRYPTO_CIPHER_MODE_GCM && (len & (ctx->blocksize - 1))) { error_setg(errp, "Length %zu must be a multiple of block size %zu", len, ctx->blocksize); return -1;
Quoted 8 lines
}
I kept them separate to avoid adding GCM special-cases to the shared block-cipher path, but I'm happy to switch the GCM driver to the common qcrypto_gcrypt_{setiv,encrypt,decrypt} (dropping the GCM-specific ones and keeping only the AEAD setaad/gettag hooks)
If you prefer. I'll make that change in v2 if that's the direction you'd like.
No, leave it as it is.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
Re: [PATCH v1 05/15] tests/qtest/aspeed-hace: Test the crypto command on the AST2600
In reply to Jamin LinQuoted 12 lines · Jamin Lin
Extend the crypto known-answer tests to cover the AST2600 crypto engine, which drives the source and destination through scatter-gather lists and adds CTR mode on top of the ECB/CBC modes shared with the AST2500.
Add a scatter-gather runner that describes each buffer with three non-adjacent fragments to exercise the gather/scatter path, add AES/DES/3DES CTR vectors (verifying the counter written back to the context buffer), and give
aspeed_add_crypto_tests()a mode mask and a scatter-gather flag so each SoC registers exactly the modes and transfer method it supports. Register the AST2600 with ECB/CBC/CTR in scatter-gather mode.
Quoted 6 lines
Signed-off-by Jamin Lin <[email protected]>--- tests/qtest/aspeed-hace-utils.h | 7 +- tests/qtest/aspeed-hace-utils.c | 211 +++++++++++++++++++++++++++++++- tests/qtest/aspeed_hace-test.c | 8 +- 3 files changed, 218 insertions(+), 8 deletions(-)
On my system, the build use the gnutls crypto backend (no CONFIG_GCRYPT or CONFIG_NETTLE) and the test fails.
I think a qcrypto_cipher_supports() call is required.
Thanks,
C.
Quoted 342 lines
tests/qtest/aspeed-hace-utils.h +5
-2@@ -85,15 +85,18 @@ void aspeed_test_addresses(const char *machine, const uint32_t base,enum {CRYPT_MODE_ECB =1 <<0 ,CRYPT_MODE_CBC =1 <<1 , +CRYPT_MODE_CTR =1 <<2 , };/* * Register the crypto known-answer tests that @modes selects (a mask of * CRYPT_MODE_*) for the given machine. Each test is named -* "<prefix>/hace/crypto/<mode>". +* "<prefix>/hace/crypto/<mode>". @sg selects scatter-gather mode (used by the +* AST2600 and later) instead of the AST2500 direct access mode. */ void aspeed_add_crypto_tests (const char *prefix,const char *machine, -uint32_t base,uint64_t dram,uint32_t modes); +uint32_t base,uint64_t dram,uint32_t modes, +bool sg);#endif /* TESTS_ASPEED_HACE_UTILS_H */ tests/qtest/aspeed-hace-utils.c +206
-5@@ -664,6 +664,7 @@ void aspeed_test_addresses(const char *machine, const uint32_t base,#define HACE_CMD_OP_MODE_MASK (0x7 << 4)#define HACE_CMD_ECB (0x0 << 4)#define HACE_CMD_CBC (0x1 << 4) +#define HACE_CMD_CTR (0x4 << 4)#define HACE_CMD_AES128 (0x0 << 2)/* Context buffer layout: IV (DES at +8), key at +0x10 */ @@ -749,6 +750,49 @@ static const uint8_t tdes_cbc_ptext[8] = {static const uint8_t tdes_cbc_ctext[8 ] = {0x0e ,0x2d ,0xb6 ,0x97 ,0x3c ,0x56 ,0x33 ,0xf4 }; +/* aes_ctr_tv_template[0] (NIST SP800-38A F.5.1), first block */ +static const uint8_t aes128_ctr_key[16 ] = { +0x2b ,0x7e ,0x15 ,0x16 ,0x28 ,0xae ,0xd2 ,0xa6 , +0xab ,0xf7 ,0x15 ,0x88 ,0x09 ,0xcf ,0x4f ,0x3c }; +static const uint8_t aes128_ctr_iv[16 ] = { +0xf0 ,0xf1 ,0xf2 ,0xf3 ,0xf4 ,0xf5 ,0xf6 ,0xf7 , +0xf8 ,0xf9 ,0xfa ,0xfb ,0xfc ,0xfd ,0xfe ,0xff }; +static const uint8_t aes128_ctr_ptext[16 ] = { +0x6b ,0xc1 ,0xbe ,0xe2 ,0x2e ,0x40 ,0x9f ,0x96 , +0xe9 ,0x3d ,0x7e ,0x11 ,0x73 ,0x93 ,0x17 ,0x2a }; +static const uint8_t aes128_ctr_ctext[16 ] = { +0x87 ,0x4d ,0x61 ,0x91 ,0xb6 ,0x20 ,0xe3 ,0x26 , +0x1b ,0xef ,0x68 ,0x64 ,0x99 ,0x0d ,0xb6 ,0xce }; +static const uint8_t aes128_ctr_ivout[16 ] = { +0xf0 ,0xf1 ,0xf2 ,0xf3 ,0xf4 ,0xf5 ,0xf6 ,0xf7 , +0xf8 ,0xf9 ,0xfa ,0xfb ,0xfc ,0xfd ,0xff ,0x00 }; + +/* des_ctr_tv_template[0] (Crypto++), first block */ +static const uint8_t des_ctr_key[8 ] = { +0xc9 ,0x83 ,0xa6 ,0xc9 ,0xec ,0x0f ,0x32 ,0x55 }; +static const uint8_t des_ctr_iv[8 ] = { +0xff ,0xff ,0xff ,0xff ,0xff ,0xff ,0xff ,0xfd }; +static const uint8_t des_ctr_ptext[8 ] = { +0x50 ,0xb9 ,0x22 ,0xae ,0x17 ,0x80 ,0x0c ,0x75 }; +static const uint8_t des_ctr_ctext[8 ] = { +0x2f ,0x96 ,0x06 ,0x0f ,0x50 ,0xc9 ,0x68 ,0x03 }; +static const uint8_t des_ctr_ivout[8 ] = { +0xff ,0xff ,0xff ,0xff ,0xff ,0xff ,0xff ,0xfe }; + +/* des3_ede_ctr_tv_template[0] (Crypto++), first block */ +static const uint8_t tdes_ctr_key[24 ] = { +0x9c ,0xd6 ,0xf3 ,0x9c ,0xb9 ,0x5a ,0x67 ,0x00 , +0x5a ,0x67 ,0x00 ,0x2d ,0xce ,0xeb ,0x2d ,0xce , +0xeb ,0xb4 ,0x51 ,0x72 ,0xb4 ,0x51 ,0x72 ,0x1f }; +static const uint8_t tdes_ctr_iv[8 ] = { +0xff ,0xff ,0xff ,0xff ,0xff ,0xff ,0xff ,0xff }; +static const uint8_t tdes_ctr_ptext[8 ] = { +0x05 ,0xec ,0x77 ,0xfb ,0x42 ,0xd5 ,0x59 ,0x20 }; +static const uint8_t tdes_ctr_ctext[8 ] = { +0x07 ,0xc2 ,0x08 ,0x20 ,0x72 ,0x1f ,0x49 ,0xef }; +static const uint8_t tdes_ctr_ivout[8 ] = { +0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 }; + typedef struct CryptTest { const uint8_t *ptext; const uint8_t *ctext;@@ -826,12 +870,59 @@ static const CryptTest crypt_tests[] = { .ctext = tdes_cbc_ctext, .len = sizeof(tdes_cbc_ptext), }, + { + .name = "aes128-ctr", + .cmd = HACE_CMD_AES128 | HACE_CMD_CTR, + .key = aes128_ctr_key, + .keylen = sizeof(aes128_ctr_key), + .iv = aes128_ctr_iv, + .ivlen = sizeof(aes128_ctr_iv), + .ptext = aes128_ctr_ptext, + .ctext = aes128_ctr_ctext, + .iv_out = aes128_ctr_ivout, + .len = sizeof(aes128_ctr_ptext), + }, + { + .name = "des-ctr", + .cmd = HACE_CMD_DES_SELECT | HACE_CMD_CTR, + .key = des_ctr_key, + .keylen = sizeof(des_ctr_key), + .iv = des_ctr_iv, + .ivlen = sizeof(des_ctr_iv), + .ptext = des_ctr_ptext, + .ctext = des_ctr_ctext, + .iv_out = des_ctr_ivout, + .len = sizeof(des_ctr_ptext), + }, + { + .name = "des3_ede-ctr", + .cmd = HACE_CMD_DES_SELECT | HACE_CMD_TRIPLE_DES | HACE_CMD_CTR, + .key = tdes_ctr_key, + .keylen = sizeof(tdes_ctr_key), + .iv = tdes_ctr_iv, + .ivlen = sizeof(tdes_ctr_iv), + .ptext = tdes_ctr_ptext, + .ctext = tdes_ctr_ctext, + .iv_out = tdes_ctr_ivout, + .len = sizeof(tdes_ctr_ptext), + }, }; /* DRAM offsets for the crypto test source, destination and context buffers. */ #define CRYPT_OFF_SRC 0x10000 #define CRYPT_OFF_DST 0x20000 #define CRYPT_OFF_CTX 0x30000 +/* Scatter-gather list offsets (each list has CRYPT_SG_FRAGS entries). */ +#define CRYPT_OFF_SRC_SG 0x40000 +#define CRYPT_OFF_DST_SG 0x50000 +/* + * The scatter-gather tests split each buffer into CRYPT_SG_FRAGS fragments, + * each placed CRYPT_SG_FRAG_STRIDE apart so the fragments never abut. The gaps + * make the test fail if the engine ignores the list and reads one contiguous + * block. + */ +#define CRYPT_SG_FRAGS 3 +#define CRYPT_SG_FRAG_STRIDE 0x1000 /* Describes one registered crypto test (qtest_add_data_func() data pointer). */ typedef struct AspeedCryptoTest {@@ -839,6 +930,7 @@ typedef struct AspeedCryptoTest {uint64_t dram;uint32_t base;int index; +bool sg; } AspeedCryptoTest;/* Map a command's operation mode (HACE10[6:4]) to a CRYPT_MODE_* flag. */ @@ -849,6 +941,8 @@ static uint32_t crypt_mode_flag(uint32_t cmd)return CRYPT_MODE_ECB ;case HACE_CMD_CBC :return CRYPT_MODE_CBC ; +case HACE_CMD_CTR : +return CRYPT_MODE_CTR ;default :return 0 ; }@@ -897,7 +991,104 @@ static void crypt_run_direct(QTestState *s, uint32_t base, uint64_t dram,qtest_memread (s, dst, out, t->len); } -static void aspeed_test_crypto_direct (const void *data) +/* +* Byte range [*frag_off, *frag_off + *frag_len) of fragment @index when an +* @len-byte buffer is split into CRYPT_SG_FRAGS pieces; the last piece takes +* the remainder of an uneven split. +*/ +static void crypt_frag_range (uint32_t len,int index, +uint32_t *frag_off,uint32_t *frag_len) +{ +uint32_t base = len /CRYPT_SG_FRAGS ; + + *frag_off = base * index; + *frag_len = (index ==CRYPT_SG_FRAGS -1 ) ? len - *frag_off : base; +} + +/* +* Scatter [in, len) across CRYPT_SG_FRAGS buffers based at @base_off and spaced +* CRYPT_SG_FRAG_STRIDE apart, then build the SG list describing them at @list. +* When @in is NULL only the list is built (used for the destination, which the +* engine fills in). +*/ +static void crypt_make_sg (QTestState *s,uint64_t dram,uint32_t base_off, +uint64_t list,const uint8_t *in,uint32_t len) +{ +struct AspeedSgList sg[CRYPT_SG_FRAGS ]; +uint32_t frag_off; +uint32_t frag_len; +uint64_t buf; +int i; + +for (i =0 ; i <CRYPT_SG_FRAGS ; i++) { +crypt_frag_range (len, i, &frag_off, &frag_len); + buf = dram + base_off + i *CRYPT_SG_FRAG_STRIDE ; + +if (in) { +qtest_memwrite (s, buf, in + frag_off, frag_len); + } + sg[i].len =cpu_to_le32 (frag_len | (i ==CRYPT_SG_FRAGS -1 ? +SG_LIST_LEN_LAST :0 )); + sg[i].addr =cpu_to_le32 ((uint32_t )buf); + } + +qtest_memwrite (s, list, sg,sizeof (sg)); +} + +/* Gather a scatter-gathered result back from the CRYPT_SG_FRAGS buffers. */ +static void crypt_gather_sg (QTestState *s,uint64_t dram,uint32_t base_off, +uint8_t *out,uint32_t len) +{ +uint32_t frag_off; +uint32_t frag_len; +int i; + +for (i =0 ; i <CRYPT_SG_FRAGS ; i++) { +crypt_frag_range (len, i, &frag_off, &frag_len); +qtest_memread (s, dram + base_off + i *CRYPT_SG_FRAG_STRIDE , + out + frag_off, frag_len); + } +} + +/* +* Run one block-cipher (ECB/CBC/CTR) operation in scatter-gather mode and read +* back the result. The source and destination are each split across +* CRYPT_SG_FRAGS non-adjacent DRAM buffers described by an SG list; the gaps +* ensure the test fails if the engine ignores the list and reads one +* contiguous block. +*/ +static void crypt_run_sg (QTestState *s,uint32_t base,uint64_t dram, +const CryptTest *t,bool encrypt,uint8_t *out) +{ +const uint8_t *in = encrypt ? t->ptext : t->ctext; +uint64_t src_sg = dram +CRYPT_OFF_SRC_SG ; +uint64_t dst_sg = dram +CRYPT_OFF_DST_SG ; +uint64_t ctx = dram +CRYPT_OFF_CTX ; +uint32_t cmd = t->cmd |HACE_CMD_ISR_EN |HACE_CMD_SRC_SG_CTRL | +HACE_CMD_DST_SG_CTRL ; + +if (encrypt) { + cmd |=HACE_CMD_ENCRYPT ; + } + +crypt_write_ctx (s, ctx, t); +crypt_make_sg (s, dram,CRYPT_OFF_SRC , src_sg, in, t->len); +crypt_make_sg (s, dram,CRYPT_OFF_DST , dst_sg,NULL , t->len); + +qtest_writel (s, base +HACE_CRYPTO_SRC , (uint32_t )src_sg); +qtest_writel (s, base +HACE_CRYPTO_DEST , (uint32_t )dst_sg); +qtest_writel (s, base +HACE_CRYPTO_CONTEXT , (uint32_t )ctx); +qtest_writel (s, base +HACE_CRYPTO_DATA_LEN , t->len); +qtest_writel (s, base +HACE_CRYPTO_CMD , cmd); + + g_assert_cmphex(qtest_readl(s, base + HACE_STS) & HACE_CRYPTO_ISR, ==, +HACE_CRYPTO_ISR ); +qtest_writel (s, base +HACE_STS ,HACE_CRYPTO_ISR ); + +crypt_gather_sg (s, dram,CRYPT_OFF_DST , out, t->len); +} + +static void aspeed_test_crypto (const void *data) {const AspeedCryptoTest *c = data;const CryptTest *t = &crypt_tests[c->index];@@ -909,7 +1100,11 @@ static void aspeed_test_crypto_direct(const void *data) g_assert_cmpuint(t->len, <=, sizeof(out));/* Encrypt: ptext -> ctext */ -crypt_run_direct (s, c->base, c->dram, t, true, out); +if (c->sg) { +crypt_run_sg (s, c->base, c->dram, t, true, out); + }else { +crypt_run_direct (s, c->base, c->dram, t, true, out); + }g_assert_cmpmem (out, t->len, t->ctext, t->len);if (t->iv_out) {@@ -919,14 +1114,19 @@ static void aspeed_test_crypto_direct(const void *data) }/* Decrypt: ctext -> ptext */ -crypt_run_direct (s, c->base, c->dram, t, false, out); +if (c->sg) { +crypt_run_sg (s, c->base, c->dram, t, false, out); + }else { +crypt_run_direct (s, c->base, c->dram, t, false, out); + }g_assert_cmpmem (out, t->len, t->ptext, t->len);qtest_quit (s); }void aspeed_add_crypto_tests (const char *prefix,const char *machine, -uint32_t base,uint64_t dram,uint32_t modes) +uint32_t base,uint64_t dram,uint32_t modes, +bool sg) {int i;@@ -945,7 +1145,8 @@ void aspeed_add_crypto_tests(const char *prefix, const char *machine, t->base = base; t->dram = dram; t->index = i; -qtest_add_data_func_full (path, t, aspeed_test_crypto_direct, g_free); + t->sg = sg; +qtest_add_data_func_full (path, t, aspeed_test_crypto, g_free); } }tests/qtest/aspeed_hace-test.c +7
-1@@ -224,6 +224,12 @@ int main(int argc, char **argv)qtest_add_func ("ast2600/hace/sha384_accum" , test_sha384_accum_ast2600);qtest_add_func ("ast2600/hace/sha256_accum" , test_sha256_accum_ast2600); +/* The AST2600 crypto engine uses scatter-gather mode and adds CTR. */ +aspeed_add_crypto_tests ("ast2600" ,"-machine ast2600-evb" ,0x1e6d0000 , +0x80000000 , +CRYPT_MODE_ECB |CRYPT_MODE_CBC |CRYPT_MODE_CTR , + true); +qtest_add_func ("ast2500/hace/addresses" , test_addresses_ast2500);qtest_add_func ("ast2500/hace/sha512" , test_sha512_ast2500);qtest_add_func ("ast2500/hace/sha256" , test_sha256_ast2500);@@ -233,7 +239,7 @@ int main(int argc, char **argv)* The AST2500 crypto engine uses direct access mode and supports ECB/CBC. */ aspeed_add_crypto_tests ("ast2500" ,"-machine ast2500-evb" ,0x1e6e3000 , -0x80000000 ,CRYPT_MODE_ECB |CRYPT_MODE_CBC ); +0x80000000 ,CRYPT_MODE_ECB |CRYPT_MODE_CBC , false);qtest_add_func ("ast2400/hace/addresses" , test_addresses_ast2400);qtest_add_func ("ast2400/hace/sha512" , test_sha512_ast2400);
RE: [PATCH v1 05/15] tests/qtest/aspeed-hace: Test the crypto command on the AST2600
Hi Cédric
Quoted 27 lines
Subject: Re: [PATCH v1 05/15] tests/qtest/aspeed-hace: Test the crypto command on the AST2600
Quoted 3 lines · Jamin Lin
Extend the crypto known-answer tests to cover the AST2600 crypto engine, which drives the source and destination through scatter-gather lists and adds CTR mode on top of the ECB/CBC modes shared with the
AST2500.
Quoted 9 lines
Add a scatter-gather runner that describes each buffer with three non-adjacent fragments to exercise the gather/scatter path, add AES/DES/3DES CTR vectors (verifying the counter written back to the context buffer), and give
aspeed_add_crypto_tests()a mode mask and a scatter-gather flag so each SoC registers exactly the modes and transfer method it supports. Register the AST2600 with ECB/CBC/CTR in scatter-gather mode.Quoted 4 lines
Signed-off-by Jamin Lin <[email protected]>--- tests/qtest/aspeed-hace-utils.h | 7 +- tests/qtest/aspeed-hace-utils.c | 211+++++++++++++++++++++++++++++++-
Quoted 2 lines
tests/qtest/aspeed_hace-test.c | 8 +- 3 files changed, 218 insertions(+), 8 deletions(-)
Quoted 5 lines
On my system, the build use the gnutls crypto backend (no
CONFIG_GCRYPTorCONFIG_NETTLE) and the test fails.I think a
qcrypto_cipher_supports()call is required.
The reason was that GnuTLS did not support CTR mode.
This commit adds a test case for CTR mode.
Thanks for the suggestion and review.
I'll use qcrypto_cipher_supports() to skip the test when the backend does not support the requested algorithm and mode. I'll also add the same check to the HACE model.
Thanks,
Jamin
Quoted 385 lines
Thanks,
C.
tests/qtest/aspeed-hace-utils.h void aspeed_test_addresses(const char *machine,
Quoted 6 lines
(quoted diff) +0
-0
b/tests/qtest/aspeed-hace-utils.hindex13feaa61e4..82b0b3f93d100644tests/qtest/aspeed-hace-utils.h +0
-0@@ -85,15 +85,18 @@ void aspeed_test_addresses(const char *machine,const
uint32_tbase,tests/qtest/aspeed-hace-utils.h void aspeed_test_addresses(const char *machine, const uint32_t base,
Quoted 8 lines
tests/qtest/aspeed-hace-utils.h +1
-0enum {CRYPT_MODE_ECB =1 <<0 ,CRYPT_MODE_CBC =1 <<1 , +CRYPT_MODE_CTR =1 <<2 , };/* * Register the crypto known-answer tests that @modes selects (a mask of
tests/qtest/aspeed-hace-utils.h void aspeed_test_addresses(const char *machine, const uint32_t base,
Quoted 8 lines
tests/qtest/aspeed-hace-utils.h +3
-2* CRYPT_MODE_*) for the given machine. Each test is named -* "<prefix>/hace/crypto/<mode>". +* "<prefix>/hace/crypto/<mode>". @sg selects scatter-gather mode +(used by the +* AST2600 and later) instead of the AST2500 direct access mode. */ void aspeed_add_crypto_tests(const char *prefix, const char *machine, - uint32_t base, uint64_t dram, uint32_tmodes);
tests/qtest/aspeed-hace-utils.h void aspeed_test_addresses(const char *machine, · line 99
Quoted 1 line
tests/qtest/aspeed-hace-utils.h +1
-0+ uint32_t base, uint64_t dram, uint32_tmodes,
tests/qtest/aspeed-hace-utils.c void aspeed_test_addresses(const char *machine,
Quoted 9 lines
tests/qtest/aspeed-hace-utils.h +1
-0+ bool sg);#endif /* TESTS_ASPEED_HACE_UTILS_H */ (quoted diff) +0
-0
b/tests/qtest/aspeed-hace-utils.cindex4deb88dbcc..0b91a4e61a100644tests/qtest/aspeed-hace-utils.c +0
-0@@ -664,6 +664,7 @@ void aspeed_test_addresses(const char *machine,const
uint32_tbase,Quoted 78 lines
tests/qtest/aspeed-hace-utils.c +62
-0#define HACE_CMD_OP_MODE_MASK (0x7 << 4)#define HACE_CMD_ECB (0x0 << 4)#define HACE_CMD_CBC (0x1 << 4) +#define HACE_CMD_CTR (0x4 << 4)#define HACE_CMD_AES128 (0x0 << 2) /* Context buffer layout: IV (DES at +8), key at +0x10 */ @@ -749,6 +750,49 @@ static const uint8_t tdes_cbc_ptext[8] = { static const uint8_t tdes_cbc_ctext[8] = {0x0e ,0x2d ,0xb6 ,0x97 ,0x3c ,0x56 ,0x33 ,0xf4 }; +/* aes_ctr_tv_template[0] (NIST SP800-38A F.5.1), first block */ +static const uint8_t aes128_ctr_key[16 ] = { +0x2b ,0x7e ,0x15 ,0x16 ,0x28 ,0xae ,0xd2 ,0xa6 , +0xab ,0xf7 ,0x15 ,0x88 ,0x09 ,0xcf ,0x4f ,0x3c };static const +uint8_t aes128_ctr_iv[16 ] = { +0xf0 ,0xf1 ,0xf2 ,0xf3 ,0xf4 ,0xf5 ,0xf6 ,0xf7 , +0xf8 ,0xf9 ,0xfa ,0xfb ,0xfc ,0xfd ,0xfe ,0xff };static const +uint8_t aes128_ctr_ptext[16 ] = { +0x6b ,0xc1 ,0xbe ,0xe2 ,0x2e ,0x40 ,0x9f ,0x96 , +0xe9 ,0x3d ,0x7e ,0x11 ,0x73 ,0x93 ,0x17 ,0x2a };static const +uint8_t aes128_ctr_ctext[16 ] = { +0x87 ,0x4d ,0x61 ,0x91 ,0xb6 ,0x20 ,0xe3 ,0x26 , +0x1b ,0xef ,0x68 ,0x64 ,0x99 ,0x0d ,0xb6 ,0xce };static const +uint8_t aes128_ctr_ivout[16 ] = { +0xf0 ,0xf1 ,0xf2 ,0xf3 ,0xf4 ,0xf5 ,0xf6 ,0xf7 , +0xf8 ,0xf9 ,0xfa ,0xfb ,0xfc ,0xfd ,0xff ,0x00 }; + +/* des_ctr_tv_template[0] (Crypto++), first block */ static const +uint8_t des_ctr_key[8 ] = { +0xc9 ,0x83 ,0xa6 ,0xc9 ,0xec ,0x0f ,0x32 ,0x55 };static const +uint8_t des_ctr_iv[8 ] = { +0xff ,0xff ,0xff ,0xff ,0xff ,0xff ,0xff ,0xfd };static const +uint8_t des_ctr_ptext[8 ] = { +0x50 ,0xb9 ,0x22 ,0xae ,0x17 ,0x80 ,0x0c ,0x75 };static const +uint8_t des_ctr_ctext[8 ] = { +0x2f ,0x96 ,0x06 ,0x0f ,0x50 ,0xc9 ,0x68 ,0x03 };static const +uint8_t des_ctr_ivout[8 ] = { +0xff ,0xff ,0xff ,0xff ,0xff ,0xff ,0xff ,0xfe }; + +/* des3_ede_ctr_tv_template[0] (Crypto++), first block */ static +const uint8_t tdes_ctr_key[24 ] = { +0x9c ,0xd6 ,0xf3 ,0x9c ,0xb9 ,0x5a ,0x67 ,0x00 , +0x5a ,0x67 ,0x00 ,0x2d ,0xce ,0xeb ,0x2d ,0xce , +0xeb ,0xb4 ,0x51 ,0x72 ,0xb4 ,0x51 ,0x72 ,0x1f };static const +uint8_t tdes_ctr_iv[8 ] = { +0xff ,0xff ,0xff ,0xff ,0xff ,0xff ,0xff ,0xff };static const +uint8_t tdes_ctr_ptext[8 ] = { +0x05 ,0xec ,0x77 ,0xfb ,0x42 ,0xd5 ,0x59 ,0x20 };static const +uint8_t tdes_ctr_ctext[8 ] = { +0x07 ,0xc2 ,0x08 ,0x20 ,0x72 ,0x1f ,0x49 ,0xef };static const +uint8_t tdes_ctr_ivout[8 ] = { +0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 }; + typedef struct CryptTest { const uint8_t *ptext; const uint8_t *ctext;@@ -826,12 +870,59 @@ static const CryptTest crypt_tests[] = { .ctext = tdes_cbc_ctext, .len = sizeof(tdes_cbc_ptext), }, + { + .name = "aes128-ctr", + .cmd = HACE_CMD_AES128 | HACE_CMD_CTR, + .key = aes128_ctr_key, + .keylen = sizeof(aes128_ctr_key), + .iv = aes128_ctr_iv, + .ivlen = sizeof(aes128_ctr_iv), + .ptext = aes128_ctr_ptext, + .ctext = aes128_ctr_ctext, + .iv_out = aes128_ctr_ivout, + .len = sizeof(aes128_ctr_ptext), + }, + { + .name = "des-ctr", + .cmd = HACE_CMD_DES_SELECT | HACE_CMD_CTR, + .key = des_ctr_key, + .keylen = sizeof(des_ctr_key),tests/qtest/aspeed-hace-utils.c static const CryptTest crypt_tests[] = { · line 899
Quoted 10 lines
tests/qtest/aspeed-hace-utils.c +10
-0+ .iv = des_ctr_iv, + .ivlen = sizeof(des_ctr_iv), + .ptext = des_ctr_ptext, + .ctext = des_ctr_ctext, + .iv_out = des_ctr_ivout, + .len = sizeof(des_ctr_ptext), + }, + { + .name = "des3_ede-ctr", + .cmd = HACE_CMD_DES_SELECT | HACE_CMD_TRIPLE_DES |
HACE_CMD_CTR,tests/qtest/aspeed-hace-utils.c static const CryptTest crypt_tests[] = {
Quoted 12 lines
tests/qtest/aspeed-hace-utils.c +9
-0+ .key = tdes_ctr_key, + .keylen = sizeof(tdes_ctr_key), + .iv = tdes_ctr_iv, + .ivlen = sizeof(tdes_ctr_iv), + .ptext = tdes_ctr_ptext, + .ctext = tdes_ctr_ctext, + .iv_out = tdes_ctr_ivout, + .len = sizeof(tdes_ctr_ptext), + }, };/* DRAM offsets for the crypto test source, destination and context buffers. */
Quoted 9 lines
tests/qtest/aspeed-hace-utils.c +6
-0#define CRYPT_OFF_SRC 0x10000#define CRYPT_OFF_DST 0x20000#define CRYPT_OFF_CTX 0x30000 +/* Scatter-gather list offsets (each list has CRYPT_SG_FRAGS +entries). */ #define CRYPT_OFF_SRC_SG 0x40000 #define +CRYPT_OFF_DST_SG 0x50000 +/* +* The scatter-gather tests split each buffer into CRYPT_SG_FRAGS +fragments, tests/qtest/aspeed-hace-utils.c static const CryptTest crypt_tests[] = {
Quoted 10 lines
tests/qtest/aspeed-hace-utils.c +8
-0+* each placed CRYPT_SG_FRAG_STRIDE apart so the fragments never +abut. The gaps +* make the test fail if the engine ignores the list and reads one +contiguous +* block. +*/ +#define CRYPT_SG_FRAGS 3 +#define CRYPT_SG_FRAG_STRIDE 0x1000/* Describes one registered crypto test (qtest_add_data_func() data pointer). */
tests/qtest/aspeed-hace-utils.c typedef struct AspeedCryptoTest { · line 936
Quoted 9 lines
typedef struct AspeedCryptoTest {tests/qtest/aspeed-hace-utils.c +1
-0@@ -839,6 +930,7 @@ typedef struct AspeedCryptoTest {uint64_t dram;uint32_t base;int index; +bool sg; } AspeedCryptoTest;/* Map a command's operation mode (HACE10[6:4]) to a CRYPT_MODE_*
Quoted 1 line
flag. */ @@ -849,6 +941,8 @@ static
uint32_tcrypt_mode_flag(uint32_tcmd)
tests/qtest/aspeed-hace-utils.c static void crypt_run_direct(QTestState *s,
Quoted 9 lines
return CRYPT_MODE_ECB; case HACE_CMD_CBC: return CRYPT_MODE_CBC;tests/qtest/aspeed-hace-utils.c +2
-0+case HACE_CMD_CTR : +return CRYPT_MODE_CTR ;default :return 0 ; }@@ -897,7 +991,104 @@ static void crypt_run_direct(QTestState *s,
uint32_tbase,uint64_tdram,Quoted 14 lines
tests/qtest/aspeed-hace-utils.c +10
-1qtest_memread (s, dst, out, t->len); } -static void aspeed_test_crypto_direct (const void *data) +/* +* Byte range [*frag_off, *frag_off + *frag_len) of fragment @index +when an +* @len-byte buffer is split into CRYPT_SG_FRAGS pieces; the last +piece takes +* the remainder of an uneven split. +*/ +static void crypt_frag_range (uint32_t len,int index, +uint32_t *frag_off,uint32_t *frag_len) +{tests/qtest/aspeed-hace-utils.c static void crypt_run_direct(QTestState *s, uint32_t base, uint64_t dram,
Quoted 10 lines
tests/qtest/aspeed-hace-utils.c +10
-0+uint32_t base = len /CRYPT_SG_FRAGS ; + + *frag_off = base * index; + *frag_len = (index ==CRYPT_SG_FRAGS -1 ) ? len - *frag_off : +base; } + +/* +* Scatter [in, len) across CRYPT_SG_FRAGS buffers based at @base_off +and spaced +* CRYPT_SG_FRAG_STRIDE apart, then build the SG list describing them at @list.
Quoted 12 lines
tests/qtest/aspeed-hace-utils.c +12
-0+* When @in is NULL only the list is built (used for the destination, +which the +* engine fills in). +*/ +static void crypt_make_sg (QTestState *s,uint64_t dram,uint32_t base_off, +uint64_t list,const uint8_t *in,uint32_t +len) { +struct AspeedSgList sg[CRYPT_SG_FRAGS ]; +uint32_t frag_off; +uint32_t frag_len; +uint64_t buf; +int i;tests/qtest/aspeed-hace-utils.c static void crypt_run_direct(QTestState *s, uint32_t base, uint64_t dram,
Quoted 10 lines
tests/qtest/aspeed-hace-utils.c +10
-0+ +for (i =0 ; i <CRYPT_SG_FRAGS ; i++) { +crypt_frag_range (len, i, &frag_off, &frag_len); + buf = dram + base_off + i *CRYPT_SG_FRAG_STRIDE ; + +if (in) { +qtest_memwrite (s, buf, in + frag_off, frag_len); + } + sg[i].len = cpu_to_le32(frag_len | (i == CRYPT_SG_FRAGS - 1 ? + SG_LIST_LEN_LAST :0));
tests/qtest/aspeed-hace-utils.c static void crypt_run_direct(QTestState *s, uint32_t base, uint64_t dram,
Quoted 7 lines
tests/qtest/aspeed-hace-utils.c +7
-0+ sg[i].addr =cpu_to_le32 ((uint32_t )buf); + } + +qtest_memwrite (s, list, sg,sizeof (sg)); } + +/* Gather a scatter-gathered result back from the CRYPT_SG_FRAGS +buffers. */ static void crypt_gather_sg(QTestState *s, uint64_t dram,
uint32_tbase_off,tests/qtest/aspeed-hace-utils.c static void crypt_run_direct(QTestState *s, uint32_t base, uint64_t dram,
Quoted 8 lines
tests/qtest/aspeed-hace-utils.c +8
-0+ uint8_t *out, uint32_t len) { + uint32_t frag_off; +uint32_t frag_len; +int i; + + for (i = 0; i < CRYPT_SG_FRAGS; i++) { + crypt_frag_range(len, i, &frag_off, &frag_len); + qtest_memread(s, dram + base_off + i *
CRYPT_SG_FRAG_STRIDE,Quoted 11 lines
tests/qtest/aspeed-hace-utils.c +11
-0+ out + frag_off, frag_len); + } +} + +/* +* Run one block-cipher (ECB/CBC/CTR) operation in scatter-gather +mode and read +* back the result. The source and destination are each split across +* CRYPT_SG_FRAGS non-adjacent DRAM buffers described by an SG list; +the gaps +* ensure the test fails if the engine ignores the list and reads one tests/qtest/aspeed-hace-utils.c static void crypt_run_direct(QTestState *s, uint32_t base, uint64_t dram,
Quoted 10 lines
tests/qtest/aspeed-hace-utils.c +10
-0+* contiguous block. +*/ +static void crypt_run_sg (QTestState *s,uint32_t base,uint64_t dram, +const CryptTest *t,bool encrypt,uint8_t +*out) { +const uint8_t *in = encrypt ? t->ptext : t->ctext; +uint64_t src_sg = dram +CRYPT_OFF_SRC_SG ; +uint64_t dst_sg = dram +CRYPT_OFF_DST_SG ; +uint64_t ctx = dram +CRYPT_OFF_CTX ; + uint32_t cmd = t->cmd | HACE_CMD_ISR_EN |
HACE_CMD_SRC_SG_CTRL|Quoted 7 lines
tests/qtest/aspeed-hace-utils.c +7
-0+HACE_CMD_DST_SG_CTRL ; + +if (encrypt) { + cmd |=HACE_CMD_ENCRYPT ; + } + +crypt_write_ctx (s, ctx, t);tests/qtest/aspeed-hace-utils.c static void crypt_run_direct(QTestState *s, uint32_t base, uint64_t dram,
Quoted 10 lines
tests/qtest/aspeed-hace-utils.c +10
-0+crypt_make_sg (s, dram,CRYPT_OFF_SRC , src_sg, in, t->len); +crypt_make_sg (s, dram,CRYPT_OFF_DST , dst_sg,NULL , t->len); + +qtest_writel (s, base +HACE_CRYPTO_SRC , (uint32_t )src_sg); +qtest_writel (s, base +HACE_CRYPTO_DEST , (uint32_t )dst_sg); +qtest_writel (s, base +HACE_CRYPTO_CONTEXT , (uint32_t )ctx); +qtest_writel (s, base +HACE_CRYPTO_DATA_LEN , t->len); +qtest_writel (s, base +HACE_CRYPTO_CMD , cmd); + + g_assert_cmphex(qtest_readl(s, base + HACE_STS) &
HACE_CRYPTO_ISR, ==,tests/qtest/aspeed-hace-utils.c static void aspeed_test_crypto_direct(const void
Quoted 23 lines
tests/qtest/aspeed-hace-utils.c +6
-0+ HACE_CRYPTO_ISR); +qtest_writel (s, base +HACE_STS ,HACE_CRYPTO_ISR ); + +crypt_gather_sg (s, dram,CRYPT_OFF_DST , out, t->len); } + +static void aspeed_test_crypto (const void *data) {const AspeedCryptoTest *c = data; const CryptTest *t = &crypt_tests[c->index]; @@ -909,7 +1100,11@@ static void
aspeed_test_crypto_direct(const void *data)g_assert_cmpuint(t->len, <=, sizeof(out)); /* Encrypt: ptext -> ctext */tests/qtest/aspeed-hace-utils.c +5
-1-crypt_run_direct (s, c->base, c->dram, t, true, out); +if (c->sg) { +crypt_run_sg (s, c->base, c->dram, t, true, out); + }else { +crypt_run_direct (s, c->base, c->dram, t, true, out); + }g_assert_cmpmem (out, t->len, t->ctext, t->len);if (t->iv_out) {@@ -919,14 +1114,19 @@ static void aspeed_test_crypto_direct(const void*data)
Quoted 6 lines
tests/qtest/aspeed-hace-utils.c +2
-1}/* Decrypt: ctext -> ptext */ -crypt_run_direct (s, c->base, c->dram, t, false, out); +if (c->sg) { +crypt_run_sg (s, c->base, c->dram, t, false, out);tests/qtest/aspeed-hace-utils.c static void aspeed_test_crypto_direct(const void *data)
Quoted 10 lines
tests/qtest/aspeed-hace-utils.c +3
-1+ }else { +crypt_run_direct (s, c->base, c->dram, t, false, out); + }g_assert_cmpmem (out, t->len, t->ptext, t->len);qtest_quit (s); } void aspeed_add_crypto_tests(const char *prefix, const char *machine, - uint32_t base, uint64_t dram, uint32_tmodes)
tests/qtest/aspeed-hace-utils.c static void aspeed_test_crypto_direct(const void · line 1128
Quoted 1 line
tests/qtest/aspeed-hace-utils.c +1
-0+ uint32_t base, uint64_t dram, uint32_tmodes,
tests/qtest/aspeed-hace-utils.c void aspeed_add_crypto_tests(const char *prefix,
Quoted 5 lines
tests/qtest/aspeed-hace-utils.c +1
-0+ bool sg) {int i;@@ -945,7 +1145,8 @@ void aspeed_add_crypto_tests(const char *prefix,const char *machine,
tests/qtest/aspeed-hace-utils.c void aspeed_add_crypto_tests(const char *prefix, const char *machine,
Quoted 4 lines
tests/qtest/aspeed-hace-utils.c +0
-1t->base = base; t->dram = dram; t->index = i; - qtest_add_data_func_full(path, t, aspeed_test_crypto_direct,
g_free);tests/qtest/aspeed_hace-test.c int main(int argc, char **argv) · line 224
Quoted 12 lines
tests/qtest/aspeed-hace-utils.c +3
-0+ t->sg = sg; +qtest_add_data_func_full (path, t, aspeed_test_crypto, + g_free); } }(quoted diff) +0
-0
b/tests/qtest/aspeed_hace-test.cindex4cb4c475e9..61a3e3feb5100644tests/qtest/aspeed_hace-test.c +0
-0@@ -224,6 +224,12 @@ int main(int argc, char **argv) qtest_add_func("ast2600/hace/sha384_accum",
test_sha384_accum_ast2600);tests/qtest/aspeed_hace-test.c int main(int argc, char **argv) · line 228
Quoted 4 lines
tests/qtest/aspeed_hace-test.c +1
-0qtest_add_func ("ast2600/hace/sha256_accum" , test_sha256_accum_ast2600); +/* The AST2600 crypto engine uses scatter-gather mode and adds CTR. */
tests/qtest/aspeed_hace-test.c int main(int argc, char **argv) · line 229
Quoted 1 line
tests/qtest/aspeed_hace-test.c +1
-0+ aspeed_add_crypto_tests("ast2600", "-machine ast2600-evb",
0x1e6d0000,tests/qtest/aspeed_hace-test.c int main(int argc, char **argv) · line 231
Quoted 2 lines
tests/qtest/aspeed_hace-test.c +2
-0+0x80000000 , +CRYPT_MODE_ECB |CRYPT_MODE_CBC |
CRYPT_MODE_CTR,Quoted 7 lines
tests/qtest/aspeed_hace-test.c +2
-0+ true); +qtest_add_func ("ast2500/hace/addresses" , test_addresses_ast2500);qtest_add_func ("ast2500/hace/sha512" , test_sha512_ast2500);qtest_add_func("ast2500/hace/sha256", test_sha256_ast2500); @@-233,7 +239,7 @@ int
main(int argc, char **argv)
* The AST2500 crypto engine uses direct access mode and supportsECB/CBC.
Quoted 2 lines
*/ aspeed_add_crypto_tests("ast2500", "-machine ast2500-evb",
0x1e6e3000,Quoted 1 line
-
0x80000000,CRYPT_MODE_ECB|
CRYPT_MODE_CBC);Quoted 5 lines
+
0x80000000,CRYPT_MODE_ECB|
+CRYPT_MODE_CBC, false);qtest_add_func("ast2400/hace/addresses", test_addresses_ast2400); qtest_add_func("ast2400/hace/sha512", test_sha512_ast2400);