]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Change CBC-AES interface
authorNiels Möller <nisse@lysator.liu.se>
Tue, 14 Sep 2021 12:40:35 +0000 (14:40 +0200)
committerNiels Möller <nisse@lysator.liu.se>
Tue, 14 Sep 2021 12:40:35 +0000 (14:40 +0200)
* cbc.h (cbc_aes128_encrypt, cbc_aes192_encrypt)
(cbc_aes256_encrypt): Change interface, take cipher context
pointer and iv as separate arguments. Update C and x86_64
implementations and corresponding glue code.

ChangeLog
cbc-aes128-encrypt.c
cbc-aes192-encrypt.c
cbc-aes256-encrypt.c
cbc.h
fat-setup.h
fat-x86_64.c
nettle-internal.c
x86_64/aesni/cbc-aes128-encrypt.asm
x86_64/aesni/cbc-aes192-encrypt.asm
x86_64/aesni/cbc-aes256-encrypt.asm

index fbe076ad3c02324cde6c06829449f01dab07a72f..08adcf283c42d7543ef158b46190a6571af37432 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2021-09-14  Niels Möller  <nisse@lysator.liu.se>
 
+       * cbc.h (cbc_aes128_encrypt, cbc_aes192_encrypt)
+       (cbc_aes256_encrypt): Change interface, take cipher context
+       pointer and iv as separate arguments. Update C and x86_64
+       implementations and corresponding glue code.
+
        * testsuite/testutils.c (test_aead): Test encrypt/decrypt with
        message split into pieces.
 
index 358a3d7048084f48e8462edb9cab0bac2b65fab5..b13c744c471191c4f4ac3677fa404aaf630355cb 100644 (file)
 /* For fat builds */
 #if HAVE_NATIVE_cbc_aes128_encrypt
 void
-_nettle_cbc_aes128_encrypt_c(struct cbc_aes128_ctx *ctx,
+_nettle_cbc_aes128_encrypt_c(const struct aes128_ctx *ctx, uint8_t *iv,
                             size_t length, uint8_t *dst,
                             const uint8_t *src);
 # define nettle_cbc_aes128_encrypt _nettle_cbc_aes128_encrypt_c
 #endif
 
 void
-cbc_aes128_encrypt(struct cbc_aes128_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src)
+cbc_aes128_encrypt(const struct aes128_ctx *ctx, uint8_t *iv,
+                  size_t length, uint8_t *dst, const uint8_t *src)
 {
-  CBC_ENCRYPT(ctx, aes128_encrypt, length, dst, src);
+  cbc_encrypt(ctx, (nettle_cipher_func *) aes128_encrypt,
+             AES_BLOCK_SIZE, iv, length, dst, src);
 }
index fa9d10b4934b7c656ef981abd9a6686854a446e2..c23192d2b3d764953cdb01b858e685a90a5e058d 100644 (file)
 /* For fat builds */
 #if HAVE_NATIVE_cbc_aes192_encrypt
 void
-_nettle_cbc_aes192_encrypt_c(struct cbc_aes192_ctx *ctx,
+_nettle_cbc_aes192_encrypt_c(const struct aes192_ctx *ctx, uint8_t *iv,
                             size_t length, uint8_t *dst,
                             const uint8_t *src);
 # define nettle_cbc_aes192_encrypt _nettle_cbc_aes192_encrypt_c
 #endif
 
 void
-cbc_aes192_encrypt(struct cbc_aes192_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src)
+cbc_aes192_encrypt(const struct aes192_ctx *ctx, uint8_t *iv,
+                  size_t length, uint8_t *dst, const uint8_t *src)
 {
-  CBC_ENCRYPT(ctx, aes192_encrypt, length, dst, src);
+  cbc_encrypt(ctx, (nettle_cipher_func *) aes192_encrypt,
+             AES_BLOCK_SIZE, iv,  length, dst, src);
 }
index 72dd7d83c8c77c4dfcd1091d55b14de1ae0b0479..14c6d9f093b6bc11cd4077fa8fd11ba91aed6fe9 100644 (file)
 /* For fat builds */
 #if HAVE_NATIVE_cbc_aes256_encrypt
 void
-_nettle_cbc_aes256_encrypt_c(struct cbc_aes256_ctx *ctx,
+_nettle_cbc_aes256_encrypt_c(const struct aes256_ctx *ctx, uint8_t *iv,
                             size_t length, uint8_t *dst,
                             const uint8_t *src);
 # define nettle_cbc_aes256_encrypt _nettle_cbc_aes256_encrypt_c
 #endif
 
 void
-cbc_aes256_encrypt(struct cbc_aes256_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src)
+cbc_aes256_encrypt(const struct aes256_ctx *ctx, uint8_t *iv,
+                  size_t length, uint8_t *dst, const uint8_t *src)
 {
-  CBC_ENCRYPT(ctx, aes256_encrypt, length, dst, src);
+  cbc_encrypt(ctx, (nettle_cipher_func *) aes256_encrypt,
+             AES_BLOCK_SIZE, iv, length, dst, src);
 }
diff --git a/cbc.h b/cbc.h
index 5fa670f3c5fa39c8b6c42baccdd653f262dc476a..cf62e028b62a9ba469497ceee915f719fd68728a 100644 (file)
--- a/cbc.h
+++ b/cbc.h
@@ -83,17 +83,17 @@ memcpy((ctx)->iv, (data), sizeof((ctx)->iv))
                 sizeof((self)->iv), (self)->iv,        \
                 (length), (dst), (src)))
 
-struct cbc_aes128_ctx CBC_CTX(struct aes128_ctx, AES_BLOCK_SIZE);
 void
-cbc_aes128_encrypt(struct cbc_aes128_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src);
+cbc_aes128_encrypt(const struct aes128_ctx *ctx, uint8_t *iv,
+                  size_t length, uint8_t *dst, const uint8_t *src);
 
-struct cbc_aes192_ctx CBC_CTX(struct aes192_ctx, AES_BLOCK_SIZE);
 void
-cbc_aes192_encrypt(struct cbc_aes192_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src);
+cbc_aes192_encrypt(const struct aes192_ctx *ctx, uint8_t *iv,
+                  size_t length, uint8_t *dst, const uint8_t *src);
 
-struct cbc_aes256_ctx CBC_CTX(struct aes256_ctx, AES_BLOCK_SIZE);
 void
-cbc_aes256_encrypt(struct cbc_aes256_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src);
+cbc_aes256_encrypt(const struct aes256_ctx *ctx, uint8_t *iv,
+                  size_t length, uint8_t *dst, const uint8_t *src);
 
 #ifdef __cplusplus
 }
index 9ef5c22d195ef09a4c63c89fd928d13fc7301688..64b272440fde181902a80422f85cd809d1ddc425 100644 (file)
@@ -214,12 +214,9 @@ typedef void aes256_invert_key_func (struct aes256_ctx *dst, const struct aes256
 typedef void aes256_crypt_func (const struct aes256_ctx *ctx, size_t length, uint8_t *dst,
               const uint8_t *src);
 
-struct cbc_aes128_ctx;
-typedef void cbc_aes128_encrypt_func (struct cbc_aes128_ctx *ctx,
+typedef void cbc_aes128_encrypt_func (const struct aes128_ctx *ctx, uint8_t *iv,
                                      size_t length, uint8_t *dst, const uint8_t *src);
-struct cbc_aes192_ctx;
-typedef void cbc_aes192_encrypt_func (struct cbc_aes192_ctx *ctx,
+typedef void cbc_aes192_encrypt_func (const struct aes192_ctx *ctx, uint8_t *iv,
                                      size_t length, uint8_t *dst, const uint8_t *src);
-struct cbc_aes256_ctx;
-typedef void cbc_aes256_encrypt_func (struct cbc_aes256_ctx *ctx,
+typedef void cbc_aes256_encrypt_func (const struct aes256_ctx *ctx, uint8_t *iv,
                                      size_t length, uint8_t *dst, const uint8_t *src);
index b5da39a1a44ba50c8bc22fea64157b5c2757ec10..30551cb29d927a665e202057691fc005461b3f7f 100644 (file)
@@ -262,17 +262,17 @@ DEFINE_FAT_FUNC(nettle_aes256_decrypt, void,
  (ctx, length, dst, src))
 
 DEFINE_FAT_FUNC(nettle_cbc_aes128_encrypt, void,
- (struct cbc_aes128_ctx *ctx,
+ (const struct aes128_ctx *ctx, uint8_t *iv,
   size_t length, uint8_t *dst, const uint8_t *src),
- (ctx, length, dst, src))
+ (ctx, iv, length, dst, src))
 DEFINE_FAT_FUNC(nettle_cbc_aes192_encrypt, void,
- (struct cbc_aes192_ctx *ctx,
+ (const struct aes192_ctx *ctx, uint8_t *iv,
   size_t length, uint8_t *dst, const uint8_t *src),
- (ctx, length, dst, src))
+ (ctx, iv, length, dst, src))
 DEFINE_FAT_FUNC(nettle_cbc_aes256_encrypt, void,
- (struct cbc_aes256_ctx *ctx,
+ (const struct aes256_ctx *ctx, uint8_t *iv,
   size_t length, uint8_t *dst, const uint8_t *src),
- (ctx, length, dst, src))
+ (ctx, iv, length, dst, src))
 
 DEFINE_FAT_FUNC(nettle_memxor, void *,
                (void *dst, const void *src, size_t n),
index e2422a5b2baed4c77c34f8c4fa073a27e4fc7710..dd2932274466e765ca963b152fa5be4185fc6e36 100644 (file)
@@ -152,6 +152,7 @@ nettle_salsa20r12 = {
   NULL,
 };
 
+struct cbc_aes128_ctx CBC_CTX(struct aes128_ctx, AES_BLOCK_SIZE);
 static void
 cbc_aes128_set_encrypt_key(struct cbc_aes128_ctx *ctx, const uint8_t *key)
 {
@@ -162,6 +163,14 @@ cbc_aes128_set_iv(struct cbc_aes128_ctx *ctx, const uint8_t *iv)
 {
   CBC_SET_IV(ctx, iv);
 }
+static void
+cbc_aes128_encrypt_wrapper(struct cbc_aes128_ctx *ctx,
+                          size_t length, uint8_t *dst,
+                          const uint8_t *src)
+{
+  cbc_aes128_encrypt(&ctx->ctx, ctx->iv, length, dst, src);
+}
+
 const struct nettle_aead
 nettle_cbc_aes128 = {
   "cbc_aes128", sizeof(struct cbc_aes128_ctx),
@@ -171,11 +180,12 @@ nettle_cbc_aes128 = {
   NULL,
   (nettle_set_key_func*) cbc_aes128_set_iv,
   NULL,
-  (nettle_crypt_func *) cbc_aes128_encrypt,
+  (nettle_crypt_func *) cbc_aes128_encrypt_wrapper,
   NULL,
   NULL,
 };
 
+struct cbc_aes192_ctx CBC_CTX(struct aes192_ctx, AES_BLOCK_SIZE);
 static void
 cbc_aes192_set_encrypt_key(struct cbc_aes192_ctx *ctx, const uint8_t *key)
 {
@@ -186,6 +196,13 @@ cbc_aes192_set_iv(struct cbc_aes192_ctx *ctx, const uint8_t *iv)
 {
   CBC_SET_IV(ctx, iv);
 }
+static void
+cbc_aes192_encrypt_wrapper(struct cbc_aes192_ctx *ctx,
+                          size_t length, uint8_t *dst,
+                          const uint8_t *src)
+{
+  cbc_aes192_encrypt(&ctx->ctx, ctx->iv, length, dst, src);
+}
 const struct nettle_aead
 nettle_cbc_aes192 = {
   "cbc_aes192", sizeof(struct cbc_aes192_ctx),
@@ -195,11 +212,12 @@ nettle_cbc_aes192 = {
   NULL,
   (nettle_set_key_func*) cbc_aes192_set_iv,
   NULL,
-  (nettle_crypt_func *) cbc_aes192_encrypt,
+  (nettle_crypt_func *) cbc_aes192_encrypt_wrapper,
   NULL,
   NULL,
 };
 
+struct cbc_aes256_ctx CBC_CTX(struct aes256_ctx, AES_BLOCK_SIZE);
 static void
 cbc_aes256_set_encrypt_key(struct cbc_aes256_ctx *ctx, const uint8_t *key)
 {
@@ -210,6 +228,13 @@ cbc_aes256_set_iv(struct cbc_aes256_ctx *ctx, const uint8_t *iv)
 {
   CBC_SET_IV(ctx, iv);
 }
+static void
+cbc_aes256_encrypt_wrapper(struct cbc_aes256_ctx *ctx,
+                          size_t length, uint8_t *dst,
+                          const uint8_t *src)
+{
+  cbc_aes256_encrypt(&ctx->ctx, ctx->iv, length, dst, src);
+}
 const struct nettle_aead
 nettle_cbc_aes256 = {
   "cbc_aes256", sizeof(struct cbc_aes256_ctx),
@@ -219,7 +244,7 @@ nettle_cbc_aes256 = {
   NULL,
   (nettle_set_key_func*) cbc_aes256_set_iv,
   NULL,
-  (nettle_crypt_func *) cbc_aes256_encrypt,
+  (nettle_crypt_func *) cbc_aes256_encrypt_wrapper,
   NULL,
   NULL,
 };
index 7375daddbce28ad67ea263a5464bf53e7ba640fb..c780b35e5ba8674bc41eace5e5fb2a73a2277696 100644 (file)
@@ -32,9 +32,10 @@ ifelse(`
 
 C Input argument
 define(`CTX',  `%rdi')
-define(`LENGTH',`%rsi')
-define(`DST',  `%rdx')
-define(`SRC',  `%rcx')
+define(`IV',   `%rsi')
+define(`LENGTH',`%rdx')
+define(`DST',  `%rcx')
+define(`SRC',  `%r8')
 
 define(`KEY0', `%xmm0')
 define(`KEY1', `%xmm1')
@@ -59,7 +60,7 @@ define(`BLOCK', `%xmm12')
        .text
        ALIGN(16)
 PROLOGUE(nettle_cbc_aes128_encrypt)
-       W64_ENTRY(4, 13)
+       W64_ENTRY(5, 13)
        shr     $4, LENGTH
        test    LENGTH, LENGTH
        jz      .Lend
@@ -75,7 +76,7 @@ PROLOGUE(nettle_cbc_aes128_encrypt)
        movups  128(CTX), KEY8
        movups  144(CTX), KEY9
        movups  160(CTX), KEY10
-       movups  176(CTX), X     C Load IV
+       movups  (IV), X
 
 .Lblock_loop:
        movups  (SRC), BLOCK    C Cleartext block
@@ -99,10 +100,9 @@ PROLOGUE(nettle_cbc_aes128_encrypt)
        dec     LENGTH
        jnz     .Lblock_loop
 
-       C Save IV
-       movups  X, 176(CTX)
+       movups  X, (IV)
 
 .Lend:
-       W64_EXIT(4, 13)
+       W64_EXIT(5, 13)
        ret
 EPILOGUE(nettle_cbc_aes128_encrypt)
index 2438d91f1e99a50e3c935981de41c0731598204c..138251620797560351f98ef955772f2ad4167f8d 100644 (file)
@@ -32,9 +32,10 @@ ifelse(`
 
 C Input argument
 define(`CTX',  `%rdi')
-define(`LENGTH',`%rsi')
-define(`DST',  `%rdx')
-define(`SRC',  `%rcx')
+define(`IV',   `%rsi')
+define(`LENGTH',`%rdx')
+define(`DST',  `%rcx')
+define(`SRC',  `%r8')
 
 define(`KEY0', `%xmm0')
 define(`KEY1', `%xmm1')
@@ -61,7 +62,7 @@ define(`BLOCK', `%xmm14')
        .text
        ALIGN(16)
 PROLOGUE(nettle_cbc_aes192_encrypt)
-       W64_ENTRY(4, 15)
+       W64_ENTRY(5, 15)
        shr     $4, LENGTH
        test    LENGTH, LENGTH
        jz      .Lend
@@ -79,7 +80,7 @@ PROLOGUE(nettle_cbc_aes192_encrypt)
        movups  160(CTX), KEY10
        movups  176(CTX), KEY11
        movups  192(CTX), KEY12
-       movups  208(CTX), X     C Load IV
+       movups  (IV), X
 
 .Lblock_loop:
        movups  (SRC), BLOCK    C Cleartext block
@@ -105,10 +106,9 @@ PROLOGUE(nettle_cbc_aes192_encrypt)
        dec     LENGTH
        jnz     .Lblock_loop
 
-       C Save IV
-       movups  X, 208(CTX)
+       movups  X, (IV)
 
 .Lend:
-       W64_EXIT(4, 15)
+       W64_EXIT(5, 15)
        ret
 EPILOGUE(nettle_cbc_aes192_encrypt)
index 6b289c704d2441f97450e1fced8e28034ed3d39a..17428f194c45de00005df8233deec0b4dde58b82 100644 (file)
@@ -32,9 +32,10 @@ ifelse(`
 
 C Input argument
 define(`CTX',  `%rdi')
-define(`LENGTH',`%rsi')
-define(`DST',  `%rdx')
-define(`SRC',  `%rcx')
+define(`IV',   `%rsi')
+define(`LENGTH',`%rdx')
+define(`DST',  `%rcx')
+define(`SRC',  `%r8')
 
 define(`KEY0_7', `%xmm0')
 define(`KEY1', `%xmm1')
@@ -63,7 +64,7 @@ define(`BLOCK', `%xmm15')
        .text
        ALIGN(16)
 PROLOGUE(nettle_cbc_aes256_encrypt)
-       W64_ENTRY(4, 16)
+       W64_ENTRY(5, 16)
        shr     $4, LENGTH
        test    LENGTH, LENGTH
        jz      .Lend
@@ -82,7 +83,7 @@ PROLOGUE(nettle_cbc_aes256_encrypt)
        movups  192(CTX), KEY12
        movups  208(CTX), KEY13
        movups  224(CTX), KEY14
-       movups  240(CTX), X     C Load IV
+       movups  (IV), X
 
 .Lblock_loop:
        movups  (SRC), BLOCK    C Cleartext block
@@ -112,10 +113,9 @@ PROLOGUE(nettle_cbc_aes256_encrypt)
        dec     LENGTH
        jnz     .Lblock_loop
 
-       C Save IV
-       movups  X, 240(CTX)
+       movups  X, (IV)
 
 .Lend:
-       W64_EXIT(4, 16)
+       W64_EXIT(5, 16)
        ret
 EPILOGUE(nettle_cbc_aes256_encrypt)