use crypto::aes;
use crypto::cipher;
use io;
type cipherinit = fn(src: io::handle, key: []u8, iv: []u8) *io::stream;
type cipher = struct {
name: str,
bufsiz: size,
keylen: size,
ivlen: size,
authlen: size,
init: *cipherinit,
};
const ciphers: [_]cipher = [
cipher {
name = "aes256-ctr",
bufsiz = aes::CTR_BUFSZ,
keylen = 32,
ivlen = 16,
authlen = 0,
init = &aes256ctr_init,
},
];
fn getcipher(name: str) (const *cipher | badcipher) = {
for (let i = 0z; i < len(ciphers); i += 1) {
if (ciphers[i].name == name) {
return &ciphers[i];
};
};
return badcipher;
};
type aes256ctr = struct {
st: cipher::ctr_stream,
block: aes::block,
buf: [aes::CTR_BUFSZ]u8,
};
fn aes256ctr_init(handle: io::handle, key: []u8, iv: []u8) *io::stream = {
let state = alloc(aes256ctr {
st = *(&([0...]: [size(cipher::ctr_stream)]u8): *cipher::ctr_stream),
block = aes::aes(),
...
});
aes::init(&state.block, key);
state.st = cipher::ctr(handle, &state.block, iv, state.buf);
return state;
};
fn cipher_free(cipher: *io::stream) void = {
io::close(cipher)!;
free(cipher);
};