commit bd80e5d9fe19153b1589b843ae234f0ee96a3dbd
parent 5fc1b70abe2624274d73c46bec114cc97c623e38
Author: Armin Preiml <apreiml@strohwolke.at>
Date: Sun, 21 Nov 2021 20:30:08 +0100
add crypto::cipher::stream interface
for stream modes on top of block ciphers like CTR and CFB
Signed-off-by: Armin Preiml <apreiml@strohwolke.at>
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
3 files changed, 27 insertions(+), 16 deletions(-)
diff --git a/crypto/cipher/stream.ha b/crypto/cipher/stream.ha
@@ -0,0 +1,12 @@
+// An abstract interface for implementing cipher streams
+export type stream = struct {
+ xor: *fn(s: *stream, dest: []u8, src: []u8) void,
+};
+
+// Applies xor of the key produced by the stream to src and writes the result
+// in dest. 'dest' and 'src' may be the same slice, and both slices must have
+// the same length.
+export fn stream_xor(s: *stream, dest: []u8, src: []u8) void = {
+ assert(len(dest) == len(src), "stream_xor: slices must have the same length");
+ s.xor(s, dest, src);
+};
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -225,8 +225,9 @@ crypto_blake2b() {
crypto_cipher() {
gen_srcs crypto::cipher \
- cbc.ha \
- cipher.ha
+ cipher.ha \
+ stream.ha \
+ cbc.ha
gen_ssa crypto::cipher bytes crypto::math
}
diff --git a/stdlib.mk b/stdlib.mk
@@ -679,8 +679,9 @@ $(HARECACHE)/crypto/blake2b/crypto_blake2b-any.ssa: $(stdlib_crypto_blake2b_any_
# crypto::cipher (+any)
stdlib_crypto_cipher_any_srcs= \
- $(STDLIB)/crypto/cipher/cbc.ha \
- $(STDLIB)/crypto/cipher/cipher.ha
+ $(STDLIB)/crypto/cipher/cipher.ha \
+ $(STDLIB)/crypto/cipher/stream.ha \
+ $(STDLIB)/crypto/cipher/cbc.ha
$(HARECACHE)/crypto/cipher/crypto_cipher-any.ssa: $(stdlib_crypto_cipher_any_srcs) $(stdlib_rt) $(stdlib_bytes_$(PLATFORM)) $(stdlib_crypto_math_$(PLATFORM))
@printf 'HAREC \t$@\n'
@@ -1508,15 +1509,13 @@ stdlib_strings_any_srcs= \
$(STDLIB)/strings/contains.ha \
$(STDLIB)/strings/cstrings.ha \
$(STDLIB)/strings/dup.ha \
- $(STDLIB)/strings/index.ha \
$(STDLIB)/strings/iter.ha \
- $(STDLIB)/strings/pad.ha \
- $(STDLIB)/strings/replace.ha \
$(STDLIB)/strings/sub.ha \
$(STDLIB)/strings/suffix.ha \
$(STDLIB)/strings/tokenize.ha \
- $(STDLIB)/strings/trim.ha \
- $(STDLIB)/strings/utf8.ha
+ $(STDLIB)/strings/utf8.ha \
+ $(STDLIB)/strings/index.ha \
+ $(STDLIB)/strings/trim.ha
$(HARECACHE)/strings/strings-any.ssa: $(stdlib_strings_any_srcs) $(stdlib_rt) $(stdlib_bytes_$(PLATFORM)) $(stdlib_encoding_utf8_$(PLATFORM)) $(stdlib_types_$(PLATFORM))
@printf 'HAREC \t$@\n'
@@ -2403,8 +2402,9 @@ $(TESTCACHE)/crypto/blake2b/crypto_blake2b-any.ssa: $(testlib_crypto_blake2b_any
# crypto::cipher (+any)
testlib_crypto_cipher_any_srcs= \
- $(STDLIB)/crypto/cipher/cbc.ha \
- $(STDLIB)/crypto/cipher/cipher.ha
+ $(STDLIB)/crypto/cipher/cipher.ha \
+ $(STDLIB)/crypto/cipher/stream.ha \
+ $(STDLIB)/crypto/cipher/cbc.ha
$(TESTCACHE)/crypto/cipher/crypto_cipher-any.ssa: $(testlib_crypto_cipher_any_srcs) $(testlib_rt) $(testlib_bytes_$(PLATFORM)) $(testlib_crypto_math_$(PLATFORM))
@printf 'HAREC \t$@\n'
@@ -3264,15 +3264,13 @@ testlib_strings_any_srcs= \
$(STDLIB)/strings/contains.ha \
$(STDLIB)/strings/cstrings.ha \
$(STDLIB)/strings/dup.ha \
- $(STDLIB)/strings/index.ha \
$(STDLIB)/strings/iter.ha \
- $(STDLIB)/strings/pad.ha \
- $(STDLIB)/strings/replace.ha \
$(STDLIB)/strings/sub.ha \
$(STDLIB)/strings/suffix.ha \
$(STDLIB)/strings/tokenize.ha \
- $(STDLIB)/strings/trim.ha \
- $(STDLIB)/strings/utf8.ha
+ $(STDLIB)/strings/utf8.ha \
+ $(STDLIB)/strings/index.ha \
+ $(STDLIB)/strings/trim.ha
$(TESTCACHE)/strings/strings-any.ssa: $(testlib_strings_any_srcs) $(testlib_rt) $(testlib_bytes_$(PLATFORM)) $(testlib_encoding_utf8_$(PLATFORM)) $(testlib_types_$(PLATFORM))
@printf 'HAREC \t$@\n'