commit c2c525983094f4b99e29fb9eda56f7a55b2840ec
parent 06b3f5b77a6aa1247dfbf2972b7f2238493b2c8f
Author: Armin Preiml <apreiml@strohwolke.at>
Date: Fri, 12 Nov 2021 14:32:50 +0100
introduce crypto::cipher::block
Required for ciphers that operate on fixed blocks like AES.
Signed-off-by: Armin Preiml <apreiml@strohwolke.at>
Diffstat:
4 files changed, 63 insertions(+), 0 deletions(-)
diff --git a/crypto/cipher/README b/crypto/cipher/README
@@ -0,0 +1 @@
+Block encryption modes
diff --git a/crypto/cipher/cipher.ha b/crypto/cipher/cipher.ha
@@ -0,0 +1,23 @@
+// An abstract interface for implementing block ciphers.
+export type block = struct {
+ sz: size,
+
+ nparallel: size,
+
+ encrypt: *fn(b: *block, dst: []u8, src: []u8) void,
+ decrypt: *fn(b: *block, dst: []u8, src: []u8) void,
+};
+
+ // Returns the blocksize in bytes
+export fn sz(b: *block) size = b.sz;
+
+// The number of blocks that can be processed at once at encrypt or decrypt
+export fn nparallel(b: *block) size = b.nparallel;
+
+// Encrypt up to nparallel blocks of size sz of src data and write it to dst
+export fn encrypt(b: *block, dst: []u8, src: []u8) void =
+ b.encrypt(b, dst, src);
+
+// Decrypt up to nparallel blocks of size sz of src data and write it to dst
+export fn decrypt(b: *block, dst: []u8, src: []u8) void =
+ b.decrypt(b, dst, src);
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -206,6 +206,12 @@ crypto_blake2b() {
crypto::math endian
}
+crypto_cipher() {
+ gen_srcs crypto::cipher \
+ cipher.ha
+ gen_ssa crypto::cipher
+}
+
crypto_math() {
gen_srcs crypto::math \
bits.ha
@@ -1067,6 +1073,7 @@ bytes
compress::flate
compress::zlib
crypto::blake2b
+crypto::cipher
crypto::math
crypto::random linux freebsd
crypto::md5
diff --git a/stdlib.mk b/stdlib.mk
@@ -154,6 +154,12 @@ stdlib_deps_any+=$(stdlib_crypto_blake2b_any)
stdlib_crypto_blake2b_linux=$(stdlib_crypto_blake2b_any)
stdlib_crypto_blake2b_freebsd=$(stdlib_crypto_blake2b_any)
+# gen_lib crypto::cipher (any)
+stdlib_crypto_cipher_any=$(HARECACHE)/crypto/cipher/crypto_cipher-any.o
+stdlib_deps_any+=$(stdlib_crypto_cipher_any)
+stdlib_crypto_cipher_linux=$(stdlib_crypto_cipher_any)
+stdlib_crypto_cipher_freebsd=$(stdlib_crypto_cipher_any)
+
# gen_lib crypto::math (any)
stdlib_crypto_math_any=$(HARECACHE)/crypto/math/crypto_math-any.o
stdlib_deps_any+=$(stdlib_crypto_math_any)
@@ -649,6 +655,16 @@ $(HARECACHE)/crypto/blake2b/crypto_blake2b-any.ssa: $(stdlib_crypto_blake2b_any_
@HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Ncrypto::blake2b \
-t$(HARECACHE)/crypto/blake2b/crypto_blake2b.td $(stdlib_crypto_blake2b_any_srcs)
+# crypto::cipher (+any)
+stdlib_crypto_cipher_any_srcs= \
+ $(STDLIB)/crypto/cipher/cipher.ha
+
+$(HARECACHE)/crypto/cipher/crypto_cipher-any.ssa: $(stdlib_crypto_cipher_any_srcs) $(stdlib_rt)
+ @printf 'HAREC \t$@\n'
+ @mkdir -p $(HARECACHE)/crypto/cipher
+ @HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Ncrypto::cipher \
+ -t$(HARECACHE)/crypto/cipher/crypto_cipher.td $(stdlib_crypto_cipher_any_srcs)
+
# crypto::math (+any)
stdlib_crypto_math_any_srcs= \
$(STDLIB)/crypto/math/bits.ha
@@ -1822,6 +1838,12 @@ testlib_deps_any+=$(testlib_crypto_blake2b_any)
testlib_crypto_blake2b_linux=$(testlib_crypto_blake2b_any)
testlib_crypto_blake2b_freebsd=$(testlib_crypto_blake2b_any)
+# gen_lib crypto::cipher (any)
+testlib_crypto_cipher_any=$(TESTCACHE)/crypto/cipher/crypto_cipher-any.o
+testlib_deps_any+=$(testlib_crypto_cipher_any)
+testlib_crypto_cipher_linux=$(testlib_crypto_cipher_any)
+testlib_crypto_cipher_freebsd=$(testlib_crypto_cipher_any)
+
# gen_lib crypto::math (any)
testlib_crypto_math_any=$(TESTCACHE)/crypto/math/crypto_math-any.o
testlib_deps_any+=$(testlib_crypto_math_any)
@@ -2320,6 +2342,16 @@ $(TESTCACHE)/crypto/blake2b/crypto_blake2b-any.ssa: $(testlib_crypto_blake2b_any
@HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Ncrypto::blake2b \
-t$(TESTCACHE)/crypto/blake2b/crypto_blake2b.td $(testlib_crypto_blake2b_any_srcs)
+# crypto::cipher (+any)
+testlib_crypto_cipher_any_srcs= \
+ $(STDLIB)/crypto/cipher/cipher.ha
+
+$(TESTCACHE)/crypto/cipher/crypto_cipher-any.ssa: $(testlib_crypto_cipher_any_srcs) $(testlib_rt)
+ @printf 'HAREC \t$@\n'
+ @mkdir -p $(TESTCACHE)/crypto/cipher
+ @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Ncrypto::cipher \
+ -t$(TESTCACHE)/crypto/cipher/crypto_cipher.td $(testlib_crypto_cipher_any_srcs)
+
# crypto::math (+any)
testlib_crypto_math_any_srcs= \
$(STDLIB)/crypto/math/bits.ha