commit 7ff4dde820c04056022d05e37aac34b30a89ce56
parent d7d3c83a7dc97452441de7e5387fb778f8f4e4c2
Author: Bor Grošelj Simić <bgs@turminal.net>
Date: Sat, 29 Apr 2023 16:12:13 +0200
crypto::bcrypt: allocate blowfish state on the heap
Makes the binary function size go from 15kb to <1kb
Signed-off-by: Bor Grošelj Simić <bgs@turminal.net>
Diffstat:
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/crypto/bcrypt/bcrypt.ha b/crypto/bcrypt/bcrypt.ha
@@ -162,9 +162,10 @@ fn bcrypt(password: []u8, salt: []u8, cost: uint) ([]u8 | errors::invalid) = {
defer free(state);
let bf = expensive_blowfish(password, salt, cost)?;
+ defer free(bf);
for (let i = 0; i < 24; i += 8) {
for (let j = 0; j < 64; j += 1) {
- cipher::encrypt(&bf, state[i..i+8], state[i..i+8]);
+ cipher::encrypt(bf, state[i..i+8], state[i..i+8]);
};
};
@@ -177,7 +178,7 @@ fn expensive_blowfish(
key: []u8,
salt: []u8,
cost: uint,
-) (blowfish::state | errors::invalid) = {
+) (*blowfish::state | errors::invalid) = {
let csalt = b64_decode(salt)?;
defer free(csalt);
@@ -187,13 +188,13 @@ fn expensive_blowfish(
append(ckey, 0);
defer free(ckey);
- const bf = blowfish::new();
- blowfish::init_salt(&bf, ckey, csalt);
+ const bf = alloc(blowfish::new());
+ blowfish::init_salt(bf, ckey, csalt);
let rounds: u64 = 1u64 << cost;
for (let i = 0u64; i < rounds; i += 1) {
- blowfish::init(&bf, ckey);
- blowfish::init(&bf, csalt);
+ blowfish::init(bf, ckey);
+ blowfish::init(bf, csalt);
};
return bf;