tfe: endian fixes and code optimization
authorAndrey Rys <rys@lynxlynx.ru>
Sat, 15 Jan 2022 15:20:53 +0000 (16:20 +0100)
committerAndrey Rys <rys@lynxlynx.ru>
Sat, 15 Jan 2022 15:20:53 +0000 (16:20 +0100)
VERSION
tfe.c
tfe.h

diff --git a/VERSION b/VERSION
index abdfb053e41e2af75ba7e11f82b4ef0c312566a7..b1e7d265fcc1d93320d7446fa1755892417f1f90 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-60
+61
diff --git a/tfe.c b/tfe.c
index eea50d36e46c45cc81b8e5ce0ac7be4bc8164c10..b25fc7da777ae97ebee751f640df92f416537893 100644 (file)
--- a/tfe.c
+++ b/tfe.c
@@ -7,7 +7,7 @@ void tfe_init_iv(struct tfe_stream *tfe, const void *key, const void *iv)
        memset(tfe, 0, sizeof(struct tfe_stream));
        memcpy(tfe->key, key, TF_KEY_SIZE);
        if (iv) memcpy(tfe->iv, iv, TF_BLOCK_SIZE);
-       tfe->carry_bytes = 0;
+       tfe->tidx = 0;
 }
 
 void tfe_init(struct tfe_stream *tfe, const void *key)
@@ -18,25 +18,27 @@ void tfe_init(struct tfe_stream *tfe, const void *key)
 void tfe_emit(void *dst, size_t szdst, struct tfe_stream *tfe)
 {
        TF_BYTE_TYPE *udst = dst;
-       size_t sz = szdst;
+       size_t sz = szdst, trem;
 
        if (!dst && szdst == 0) {
                memset(tfe, 0, sizeof(struct tfe_stream));
                return;
        }
 
-       if (tfe->carry_bytes > 0) {
-               if (tfe->carry_bytes > szdst) {
-                       memcpy(udst, tfe->carry_block, szdst);
-                       memmove(tfe->carry_block, tfe->carry_block+szdst, tfe->carry_bytes-szdst);
-                       tfe->carry_bytes -= szdst;
+       if (tfe->tidx > 0) {
+               trem = TF_BLOCK_SIZE-tfe->tidx;
+
+               if (szdst <= trem) {
+                       memcpy(udst, &tfe->tmp[tfe->tidx], szdst);
+                       tfe->tidx += szdst;
+                       if (tfe->tidx >= TF_BLOCK_SIZE) tfe->tidx = 0;
                        return;
                }
 
-               memcpy(udst, tfe->carry_block, tfe->carry_bytes);
-               udst += tfe->carry_bytes;
-               sz -= tfe->carry_bytes;
-               tfe->carry_bytes = 0;
+               memcpy(udst, &tfe->tmp[tfe->tidx], trem);
+               udst += trem;
+               sz -= trem;
+               tfe->tidx = 0;
        }
 
        if (sz >= TF_BLOCK_SIZE) {
@@ -49,15 +51,10 @@ void tfe_emit(void *dst, size_t szdst, struct tfe_stream *tfe)
        }
 
        if (sz) {
-               TF_UNIT_TYPE t[TF_NR_BLOCK_UNITS];
-
                tf_encrypt_rawblk(tfe->iv, tfe->iv, tfe->key);
-               memcpy(t, tfe->iv, TF_BLOCK_SIZE);
-               data_to_words(t, TF_BLOCK_SIZE);
-               memcpy(udst, t, sz);
-               memset(t, 0, TF_BLOCK_SIZE);
-               udst = (TF_BYTE_TYPE *)tfe->iv;
-               tfe->carry_bytes = TF_BLOCK_SIZE-sz;
-               memcpy(tfe->carry_block, udst+sz, tfe->carry_bytes);
+               memcpy(tfe->tmp, tfe->iv, TF_BLOCK_SIZE);
+               data_to_words(tfe->tmp, TF_BLOCK_SIZE);
+               memcpy(udst, tfe->tmp, sz);
+               tfe->tidx = sz;
        }
 }
diff --git a/tfe.h b/tfe.h
index f5793b1afd8fcd4836f1384f2975b33dddae57e3..e402260fb3d7fbd675d2103235e9dcd0e7f0d1d2 100644 (file)
--- a/tfe.h
+++ b/tfe.h
@@ -6,8 +6,8 @@
 struct tfe_stream {
        TF_UNIT_TYPE key[TF_NR_KEY_UNITS];
        TF_UNIT_TYPE iv[TF_NR_BLOCK_UNITS];
-       TF_BYTE_TYPE carry_block[TF_BLOCK_SIZE];
-       size_t carry_bytes;
+       TF_BYTE_TYPE tmp[TF_BLOCK_SIZE];
+       size_t tidx;
 };
 
 void tfe_init(struct tfe_stream *tfe, const void *key);