Skip to content

Commit 19f880b

Browse files
committed
jets: rewrites aes-cbc to avoid using realloc
1 parent e77bd24 commit 19f880b

1 file changed

Lines changed: 23 additions & 24 deletions

File tree

pkg/noun/jets/e/aes_cbc.c

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,30 @@
1010
* the ECB functions, which truncate them, hence the raw u3r_bytes unpacking.
1111
*/
1212

13-
typedef int (*urcrypt_cbc)(c3_y**,
14-
size_t*,
15-
c3_y*,
16-
c3_y*,
17-
urcrypt_realloc_t);
13+
typedef int (*urcrypt_cbc)(c3_y*, size_t, c3_y*, c3_y*);
1814

1915
static u3_atom
2016
_cqea_cbc_help(c3_y* key_y, u3_atom iv, u3_atom msg, urcrypt_cbc low_f)
2117
{
22-
u3_atom ret;
23-
c3_w met_w;
24-
c3_y iv_y[16];
25-
c3_y* msg_y = u3r_bytes_all(&met_w, msg);
26-
size_t len = met_w;
18+
c3_y iv_y[16];
19+
// message length in 16-byte (bloq 7) blocks; cbc always processes at least
20+
// one block (the hoon pads an empty message to a single zero block)
21+
c3_d len_d = c3_max(1, u3r_met(7, msg));
22+
u3i_slab sab_u;
2723

2824
u3r_bytes(0, 16, iv_y, iv);
29-
if ( 0 != (*low_f)(&msg_y, &len, key_y, iv_y, &u3a_realloc) ) {
30-
ret = u3_none;
31-
}
32-
else {
33-
ret = u3i_bytes(len, msg_y);
34-
}
35-
u3a_free(msg_y);
3625

37-
return ret;
26+
// read/write buffer holding [msg] little-endian, zero-padded to a 16-byte
27+
// block boundary (bloq 7), passed to urcrypt's unsafe (no realloc)
28+
// interface, which operates in place.
29+
//
30+
u3i_slab_from(&sab_u, msg, 7, len_d);
31+
32+
// the only error is a non-block-aligned length, ruled out by construction
33+
//
34+
u3_assert( 0 == (*low_f)(sab_u.buf_y, (c3_z)sab_u.len_w << 2, key_y, iv_y) );
35+
36+
return u3i_slab_mint(&sab_u);
3837
}
3938

4039
static u3_atom
@@ -44,7 +43,7 @@ typedef int (*urcrypt_cbc)(c3_y**,
4443
{
4544
c3_y key_y[16];
4645
u3r_bytes(0, 16, key_y, key);
47-
return _cqea_cbc_help(key_y, iv, msg, &urcrypt_aes_cbca_en);
46+
return _cqea_cbc_help(key_y, iv, msg, &urcrypt_aes_cbca_en_unsafe);
4847
}
4948

5049
u3_noun
@@ -68,7 +67,7 @@ typedef int (*urcrypt_cbc)(c3_y**,
6867
{
6968
c3_y key_y[16];
7069
u3r_bytes(0, 16, key_y, key);
71-
return _cqea_cbc_help(key_y, iv, msg, &urcrypt_aes_cbca_de);
70+
return _cqea_cbc_help(key_y, iv, msg, &urcrypt_aes_cbca_de_unsafe);
7271
}
7372

7473
u3_noun
@@ -92,7 +91,7 @@ typedef int (*urcrypt_cbc)(c3_y**,
9291
{
9392
c3_y key_y[24];
9493
u3r_bytes(0, 24, key_y, key);
95-
return _cqea_cbc_help(key_y, iv, msg, &urcrypt_aes_cbcb_en);
94+
return _cqea_cbc_help(key_y, iv, msg, &urcrypt_aes_cbcb_en_unsafe);
9695
}
9796

9897
u3_noun
@@ -116,7 +115,7 @@ typedef int (*urcrypt_cbc)(c3_y**,
116115
{
117116
c3_y key_y[24];
118117
u3r_bytes(0, 24, key_y, key);
119-
return _cqea_cbc_help(key_y, iv, msg, &urcrypt_aes_cbcb_de);
118+
return _cqea_cbc_help(key_y, iv, msg, &urcrypt_aes_cbcb_de_unsafe);
120119
}
121120

122121
u3_noun
@@ -140,7 +139,7 @@ typedef int (*urcrypt_cbc)(c3_y**,
140139
{
141140
c3_y key_y[32];
142141
u3r_bytes(0, 32, key_y, key);
143-
return _cqea_cbc_help(key_y, iv, msg, &urcrypt_aes_cbcc_en);
142+
return _cqea_cbc_help(key_y, iv, msg, &urcrypt_aes_cbcc_en_unsafe);
144143
}
145144

146145
u3_noun
@@ -164,7 +163,7 @@ typedef int (*urcrypt_cbc)(c3_y**,
164163
{
165164
c3_y key_y[32];
166165
u3r_bytes(0, 32, key_y, key);
167-
return _cqea_cbc_help(key_y, iv, msg, &urcrypt_aes_cbcc_de);
166+
return _cqea_cbc_help(key_y, iv, msg, &urcrypt_aes_cbcc_de_unsafe);
168167
}
169168

170169
u3_noun

0 commit comments

Comments
 (0)