Support the ASPEED HACE crypto command
PATCH v4 4/16 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
@@ -41,6 +41,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)
@@ -587,6 +588,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; }
@@ -650,6 +654,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
@@ -675,6 +695,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;
@@ -727,8 +748,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 ];
@@ -749,7 +776,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));
@@ -757,7 +784,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));
@@ -790,13 +817,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 v4 2/16 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 | 325 ++++++++++++++++++++++++++++++++ tests/qtest/aspeed_hace-test.c | 6 + tests/qtest/meson.build | 6 +- 4 files changed, 351 insertions(+), 2 deletions(-)
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 +325 -0
334 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);
tests/qtest/meson.build +4 -2
@@ -389,9 +389,11 @@ if get_option('replication').allowed() endif qtests = { - 'aspeed_hace-test': files('aspeed-hace-utils.c', 'aspeed_hace-test.c'), + 'aspeed_hace-test': [files('aspeed-hace-utils.c', 'aspeed_hace-test.c'), + crypto], 'aspeed_smc-test': files('aspeed-smc-utils.c', 'aspeed_smc-test.c'), - 'ast2700-hace-test': files('aspeed-hace-utils.c', 'ast2700-hace-test.c'), + 'ast2700-hace-test': [files('aspeed-hace-utils.c', 'ast2700-hace-test.c'), + crypto], 'ast2700-smc-test': files('aspeed-smc-utils.c', 'ast2700-smc-test.c'), 'bios-tables-test': [io, 'boot-sector.c', 'acpi-utils.c', 'tpm-emu.c'], 'cdrom-test': files('boot-sector.c'),
Signature · 1 line
2.43.0
PATCH v4 7/16 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
PATCH v4 16/16 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 | 163 +++++++++++++++++++++++++++++++- tests/qtest/ast2700-hace-test.c | 9 ++ 3 files changed, 172 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 +162 -1
234 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 v4 1/16 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 | 283 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 268 insertions(+), 15 deletions(-)
hw/misc/aspeed_hace.c +268 -15
@@ -18,11 +18,43 @@#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) +/* 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)
@@ -65,7 +97,6 @@/* Other cmd bits */ #define HASH_IRQ_EN BIT(9)#define HASH_SG_EN BIT(18) -#define CRYPT_IRQ_EN BIT(12)/* Scatter-gather data list */ #define SG_LIST_LEN_SIZE 4#define SG_LIST_LEN_MASK 0x0FFFFFFF
@@ -501,6 +532,216 @@ 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 ; + } + +if (!qcrypto_cipher_supports (alg, mode)) { +qemu_log_mask (LOG_UNIMP , +"%s: cipher mode not supported by the crypto backend\n" , + __func__); +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 +772,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 +836,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 v4 8/16 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 v4 5/16 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 | 217 +++++++++++++++++++++++++++++++- tests/qtest/aspeed_hace-test.c | 8 +- 3 files changed, 224 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 +212 -5
283 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 v4 11/16 crypto/cipher-gnutls: Implement AES-GCM
In reply to Jamin LinAdd the AES-GCM AEAD mode to the gnutls backend so it is available when QEMU is built with gnutls (neither gcrypt nor nettle). GCM uses the incremental gnutls_cipher_* API with the GNUTLS_CIPHER_AES_*_GCM
algorithms: gnutls_cipher_set_iv() sets the nonce, gnutls_cipher_add_auth() feeds the associated data, gnutls_cipher_encrypt2()/decrypt2() process the message, and gnutls_cipher_tag() reads back the authentication tag.
--- crypto/cipher-gnutls.c.inc | 154 +++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+)
crypto/cipher-gnutls.c.inc +154 -0
@@ -48,6 +48,15 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgo alg, default: return false; } + case QCRYPTO_CIPHER_MODE_GCM: + switch (alg) { + case QCRYPTO_CIPHER_ALGO_AES_128: + case QCRYPTO_CIPHER_ALGO_AES_192: + case QCRYPTO_CIPHER_ALGO_AES_256: + return true; + default: + return false; + } default: return false; }
@@ -223,6 +232,147 @@ static struct QCryptoCipherDriver gnutls_driver = { .cipher_free = qcrypto_gnutls_cipher_free, }; +/* + * GCM is an AEAD stream mode: the nonce need not match the block size, the + * message length need not be a multiple of the block size, associated data is + * fed with gnutls_cipher_add_auth() and the authentication tag is read back + * with gnutls_cipher_tag(). + */ +static int +qcrypto_gnutls_cipher_encrypt_gcm(QCryptoCipher *cipher, + const void *in, void *out, + size_t len, Error **errp) +{ + QCryptoCipherGnutls *ctx = container_of(cipher, QCryptoCipherGnutls, base); + int err; + + err = gnutls_cipher_encrypt2(ctx->handle, in, len, out, len); + if (err != 0) { + error_setg(errp, "Cannot encrypt data: %s", gnutls_strerror(err)); + return -1; + } + + return 0; +} + +static int +qcrypto_gnutls_cipher_decrypt_gcm(QCryptoCipher *cipher, + const void *in, void *out, + size_t len, Error **errp) +{ + QCryptoCipherGnutls *ctx = container_of(cipher, QCryptoCipherGnutls, base); + int err; + + err = gnutls_cipher_decrypt2(ctx->handle, in, len, out, len); + if (err != 0) { + error_setg(errp, "Cannot decrypt data: %s", gnutls_strerror(err)); + return -1; + } + + return 0; +} + +static int +qcrypto_gnutls_cipher_setiv_gcm(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) +{ + QCryptoCipherGnutls *ctx = container_of(cipher, QCryptoCipherGnutls, base); + + gnutls_cipher_set_iv(ctx->handle, (void *)iv, niv); + + return 0; +} + +static int +qcrypto_gnutls_cipher_setaad_gcm(QCryptoCipher *cipher, + const uint8_t *aad, size_t len, + Error **errp) +{ + QCryptoCipherGnutls *ctx = container_of(cipher, QCryptoCipherGnutls, base); + int err; + + err = gnutls_cipher_add_auth(ctx->handle, aad, len); + if (err != 0) { + error_setg(errp, "Cannot add associated data: %s", + gnutls_strerror(err)); + return -1; + } + + return 0; +} + +static int +qcrypto_gnutls_cipher_gettag_gcm(QCryptoCipher *cipher, + uint8_t *tag, size_t len, + Error **errp) +{ + QCryptoCipherGnutls *ctx = container_of(cipher, QCryptoCipherGnutls, base); + int err; + + err = gnutls_cipher_tag(ctx->handle, tag, len); + if (err != 0) { + error_setg(errp, "Cannot get authentication tag: %s", + gnutls_strerror(err)); + return -1; + } + + return 0; +} + +static struct QCryptoCipherDriver gnutls_gcm_driver = { + .cipher_encrypt = qcrypto_gnutls_cipher_encrypt_gcm, + .cipher_decrypt = qcrypto_gnutls_cipher_decrypt_gcm, + .cipher_setiv = qcrypto_gnutls_cipher_setiv_gcm, + .cipher_setaad = qcrypto_gnutls_cipher_setaad_gcm, + .cipher_gettag = qcrypto_gnutls_cipher_gettag_gcm, + .cipher_free = qcrypto_gnutls_cipher_free, +}; + +static QCryptoCipher * +qcrypto_gnutls_aes_gcm_ctx_new(QCryptoCipherAlgo alg, const uint8_t *key, + size_t nkey, Error **errp) +{ + gnutls_datum_t gkey = { (unsigned char *)key, nkey }; + gnutls_cipher_algorithm_t galg = GNUTLS_CIPHER_UNKNOWN; + QCryptoCipherGnutls *ctx; + int err; + + switch (alg) { + case QCRYPTO_CIPHER_ALGO_AES_128: + galg = GNUTLS_CIPHER_AES_128_GCM; + break; + case QCRYPTO_CIPHER_ALGO_AES_192: + galg = GNUTLS_CIPHER_AES_192_GCM; + break; + case QCRYPTO_CIPHER_ALGO_AES_256: + galg = GNUTLS_CIPHER_AES_256_GCM; + break; + default: + error_setg(errp, "Unsupported cipher algorithm %s with GCM mode", + QCryptoCipherAlgo_str(alg)); + return NULL; + } + + if (!qcrypto_cipher_validate_key_length(alg, QCRYPTO_CIPHER_MODE_GCM, + nkey, errp)) { + return NULL; + } + + ctx = g_new0(QCryptoCipherGnutls, 1); + ctx->base.driver = &gnutls_gcm_driver; + ctx->blocksize = 16; + + err = gnutls_cipher_init(&ctx->handle, galg, &gkey, NULL); + if (err != 0) { + error_setg(errp, "Cannot initialize cipher: %s", gnutls_strerror(err)); + g_free(ctx); + return NULL; + } + + return &ctx->base; +} + static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgo alg, QCryptoCipherMode mode, const uint8_t *key,
@@ -234,6 +384,10 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgo alg, gnutls_cipher_algorithm_t galg = GNUTLS_CIPHER_UNKNOWN; int err; + if (mode == QCRYPTO_CIPHER_MODE_GCM) { + return qcrypto_gnutls_aes_gcm_ctx_new(alg, key, nkey, errp); + } + switch (mode) { case QCRYPTO_CIPHER_MODE_XTS: switch (alg) {
Signature · 1 line
2.43.0
PATCH v4 3/16 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
@@ -608,13 +608,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 ;
@@ -631,6 +676,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 ;
@@ -684,8 +730,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);
@@ -714,8 +766,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 v4 14/16 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)
@@ -42,6 +45,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)
@@ -57,10 +61,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)
@@ -596,6 +605,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; }
@@ -689,11 +701,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) {
@@ -703,6 +716,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;
@@ -711,10 +725,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 ) {
@@ -734,6 +751,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 ]; +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,
@@ -758,7 +789,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));
@@ -769,9 +800,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);
@@ -855,6 +888,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); + } } }
@@ -899,9 +950,11 @@ static void aspeed_hace_write(void *opaque, hwaddr addr, uint64_t data,case R_CRYPT_SRC :case R_CRYPT_DEST :case R_CRYPT_CONTEXT : +case R_CRYPT_GCM_TAG : data &= ahc->src_mask;break ;case R_CRYPT_DATA_LEN : +case R_CRYPT_GCM_ADD_LEN : data &=CRYPT_DATA_LEN_MASK ;break ;case R_HASH_SRC :
@@ -980,6 +1033,7 @@ static void aspeed_hace_write(void *opaque, hwaddr addr, uint64_t data, data &= ahc->src_hi_mask;break ;case R_CRYPT_DEST_HI : +case R_CRYPT_GCM_TAG_HI : data &= ahc->dest_hi_mask;break ;case R_CRYPT_CONTEXT_HI :
Signature · 1 line
2.43.0
PATCH v4 15/16 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
@@ -1005,14 +1005,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 ;
@@ -1214,12 +1207,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 v4 10/16 crypto/cipher-nettle: Implement AES-GCM
In reply to Jamin LinAdd the AES-GCM AEAD mode to the nettle backend so it is available when QEMU is built with nettle instead of gcrypt. GCM is driven through nettle's generic gcm_* interface, using the AES encrypt function for both
directions: gcm_set_iv() sets the (typically 96-bit) nonce, gcm_update() feeds the associated data, gcm_encrypt()/gcm_decrypt() need not be block aligned, and gcm_digest() produces the authentication tag.
--- crypto/cipher-nettle.c.inc | 128 +++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+)
crypto/cipher-nettle.c.inc +128 -0
@@ -27,6 +27,7 @@ #include <nettle/twofish.h> #include <nettle/ctr.h> #include <nettle/xts.h> +#include <nettle/gcm.h> #ifdef CONFIG_CRYPTO_SM4 #include <nettle/sm4.h> #endif
@@ -410,6 +411,125 @@ DEFINE_ECB(qcrypto_nettle_sm4, sm4_encrypt_native, sm4_decrypt_native) #endif +/* + * GCM is an AEAD mode built on AES (128-bit block only). Drive it through the + * generic gcm_* interface, using the block cipher's encrypt function for both + * directions; associated data is fed with gcm_update() and the authentication + * tag is produced by gcm_digest(). + */ +typedef struct QCryptoNettleAESGCM { + QCryptoCipher base; + struct gcm_key gcm_key; + struct gcm_ctx gcm_ctx; + union { + struct aes128_ctx aes128; + struct aes192_ctx aes192; + struct aes256_ctx aes256; + } cipher; + nettle_cipher_func *encrypt; +} QCryptoNettleAESGCM; + +static int qcrypto_nettle_aes_gcm_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) +{ + QCryptoNettleAESGCM *ctx = container_of(cipher, QCryptoNettleAESGCM, base); + + gcm_set_iv(&ctx->gcm_ctx, &ctx->gcm_key, niv, iv); + return 0; +} + +static int qcrypto_nettle_aes_gcm_setaad(QCryptoCipher *cipher, + const uint8_t *aad, size_t len, + Error **errp) +{ + QCryptoNettleAESGCM *ctx = container_of(cipher, QCryptoNettleAESGCM, base); + + gcm_update(&ctx->gcm_ctx, &ctx->gcm_key, len, aad); + return 0; +} + +static int qcrypto_nettle_aes_gcm_encrypt(QCryptoCipher *cipher, + const void *in, void *out, + size_t len, Error **errp) +{ + QCryptoNettleAESGCM *ctx = container_of(cipher, QCryptoNettleAESGCM, base); + + gcm_encrypt(&ctx->gcm_ctx, &ctx->gcm_key, &ctx->cipher, ctx->encrypt, + len, out, in); + return 0; +} + +static int qcrypto_nettle_aes_gcm_decrypt(QCryptoCipher *cipher, + const void *in, void *out, + size_t len, Error **errp) +{ + QCryptoNettleAESGCM *ctx = container_of(cipher, QCryptoNettleAESGCM, base); + + gcm_decrypt(&ctx->gcm_ctx, &ctx->gcm_key, &ctx->cipher, ctx->encrypt, + len, out, in); + return 0; +} + +static int qcrypto_nettle_aes_gcm_gettag(QCryptoCipher *cipher, + uint8_t *tag, size_t len, + Error **errp) +{ + QCryptoNettleAESGCM *ctx = container_of(cipher, QCryptoNettleAESGCM, base); + + gcm_digest(&ctx->gcm_ctx, &ctx->gcm_key, &ctx->cipher, ctx->encrypt, + len, tag); + return 0; +} + +static const struct QCryptoCipherDriver qcrypto_nettle_aes_gcm_driver = { + .cipher_encrypt = qcrypto_nettle_aes_gcm_encrypt, + .cipher_decrypt = qcrypto_nettle_aes_gcm_decrypt, + .cipher_setiv = qcrypto_nettle_aes_gcm_setiv, + .cipher_setaad = qcrypto_nettle_aes_gcm_setaad, + .cipher_gettag = qcrypto_nettle_aes_gcm_gettag, + .cipher_free = qcrypto_cipher_ctx_free, +}; + +static QCryptoCipher *qcrypto_nettle_aes_gcm_ctx_new(QCryptoCipherAlgo alg, + const uint8_t *key, + size_t nkey, + Error **errp) +{ + QCryptoNettleAESGCM *ctx; + + if (!qcrypto_cipher_validate_key_length(alg, QCRYPTO_CIPHER_MODE_GCM, + nkey, errp)) { + return NULL; + } + + ctx = g_new0(QCryptoNettleAESGCM, 1); + ctx->base.driver = &qcrypto_nettle_aes_gcm_driver; + + switch (alg) { + case QCRYPTO_CIPHER_ALGO_AES_128: + aes128_set_encrypt_key(&ctx->cipher.aes128, key); + ctx->encrypt = aes128_encrypt_native; + break; + case QCRYPTO_CIPHER_ALGO_AES_192: + aes192_set_encrypt_key(&ctx->cipher.aes192, key); + ctx->encrypt = aes192_encrypt_native; + break; + case QCRYPTO_CIPHER_ALGO_AES_256: + aes256_set_encrypt_key(&ctx->cipher.aes256, key); + ctx->encrypt = aes256_encrypt_native; + break; + default: + error_setg(errp, "Unsupported cipher algorithm %s with GCM mode", + QCryptoCipherAlgo_str(alg)); + g_free(ctx); + return NULL; + } + + gcm_set_key(&ctx->gcm_key, &ctx->cipher, ctx->encrypt); + return &ctx->base; +} + bool qcrypto_cipher_supports(QCryptoCipherAlgo alg, QCryptoCipherMode mode) {
@@ -440,6 +560,10 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgo alg, case QCRYPTO_CIPHER_MODE_XTS: case QCRYPTO_CIPHER_MODE_CTR: return true; + case QCRYPTO_CIPHER_MODE_GCM: + return alg == QCRYPTO_CIPHER_ALGO_AES_128 || + alg == QCRYPTO_CIPHER_ALGO_AES_192 || + alg == QCRYPTO_CIPHER_ALGO_AES_256; default: return false; }
@@ -451,6 +575,10 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgo alg, size_t nkey, Error **errp) { + if (mode == QCRYPTO_CIPHER_MODE_GCM) { + return qcrypto_nettle_aes_gcm_ctx_new(alg, key, nkey, errp); + } + switch (mode) { case QCRYPTO_CIPHER_MODE_ECB: case QCRYPTO_CIPHER_MODE_CBC:
Signature · 1 line
2.43.0
v4 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.
Note: the GNUTLS crypto backend does not support AES-CTR mode.
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
v2:
1. Add AES-GCM to the nettle and gnutls crypto backends
2. Skip crypto tests for cipher modes the
compiled backend does not support, via qcrypto_cipher_supports()
3. Check qcrypto_cipher_supports() before running a
crypt command and report the mode as unimplemented when the backend lacks it
v3:
1. remove the duplicate CRYPT_IRQ_EN definition.
2. remove the unuse CRYPT_CMD_DST_SG_CTRL definition.
3. Drop "Drop the AST2700 crypto self-test workaround" patch because GNUTLS backend does not support AES-CTR mode.
v4:
1. Add GCM tag mask
Jamin Lin (16):
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 crypto/cipher-nettle: Implement AES-GCM crypto/cipher-gnutls: 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
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 | 461 ++++++++++++++++++++- tests/qtest/aspeed-hace-utils.c | 693 ++++++++++++++++++++++++++++++++ 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 +++++ crypto/cipher-gnutls.c.inc | 154 +++++++ crypto/cipher-nettle.c.inc | 128 ++++++ tests/qtest/meson.build | 6 +- 15 files changed, 1886 insertions(+), 25 deletions(-)
Signature · 1 line
2.43.0
PATCH v4 9/16 crypto/cipher-gcrypt: Implement AES-GCM
Map 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 v4 13/16 hw/misc/aspeed_hace: Support 64-bit DMA for the crypto command
In reply to Jamin LinThe 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
@@ -57,6 +57,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)
@@ -670,6 +675,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
@@ -717,7 +735,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,
@@ -758,7 +776,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 {
@@ -794,7 +812,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 {
@@ -958,6 +976,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 v4 12/16 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-gcm-128-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-gcm-128-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-gcm-128-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-gcm-256-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-gcm-256-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
PATCH v4 6/16 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
Quoted 81 lines · Jamin Lin
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.
Note: the GNUTLS crypto backend does not support AES-CTR mode.
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 workaroundv2:
1. Add AES-GCM to the nettle and gnutls crypto backends
2. Skip crypto tests for cipher modes the
compiled backend does not support, viaqcrypto_cipher_supports()
3. Checkqcrypto_cipher_supports()before running a
crypt command and report the mode as unimplemented when the backend lacks itv3:
1. remove the duplicateCRYPT_IRQ_ENdefinition.
2. remove the unuseCRYPT_CMD_DST_SG_CTRLdefinition.
3. Drop "Drop the AST2700 crypto self-test workaround" patch because GNUTLS backend does not support AES-CTR mode.v4:
1. Add GCM tag maskJamin Lin (16):
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 AST1030crypto/cipher: Add GCM to QCryptoCipherModecrypto/cipher: Add setaad/gettag for AEAD modescrypto/cipher-gcrypt: Implement AES-GCMcrypto/cipher-nettle: Implement AES-GCMcrypto/cipher-gnutls: 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
Quoted 17 lines
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 | 461 ++++++++++++++++++++- tests/qtest/aspeed-hace-utils.c | 693 ++++++++++++++++++++++++++++++++ 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 +++++ crypto/cipher-gnutls.c.inc | 154 +++++++ crypto/cipher-nettle.c.inc | 128 ++++++ tests/qtest/meson.build | 6 +- 15 files changed, 1886 insertions(+), 25 deletions(-)
Applied to
https://github.com/legoater/qemu aspeed-next
Thanks,
C.