block.ha (1383B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use bytes; 5 use crypto::cipher; 6 7 // The block size used by the AES algorithm. 8 export def BLOCKSZ: size = 16; 9 10 // Size of the buffer used for [[crypto::cipher::cbc_encryptor]] and 11 // [[crypto::cipher::cbc_decryptor]]. 12 export def CBC_BUFSZ: size = BLOCKSZ * 2; 13 14 // Size of the buffer used for [[crypto::cipher::ctr]]. 15 export def CTR_BUFSZ: size = BLOCKSZ * (MAXNPARALLEL + 1); 16 17 export type block = struct { 18 vtable: cipher::block, 19 rounds: u32, 20 expkey: [MAXEXPKEYSZ]u8, 21 }; 22 23 // Returns an AES [[crypto::cipher::block]] cipher implementation that has 24 // hardware support if possible. Check [[hwsupport]] to see if it is available. 25 // 26 // The caller must call [[init]] to add a key to the cipher before using 27 // the cipher, and must call [[crypto::cipher::finish]] when they are finished 28 // using the cipher to securely erase any secret data stored in the cipher 29 // state. 30 export fn aes() block = block { 31 vtable = rtvtable, 32 ... 33 }; 34 35 36 let hwsup: bool = false; 37 38 // Checks whether hardware AES support is available. 39 export fn hwsupport() bool = hwsup; 40 41 type initfunc = fn(b: *block, key: []u8) void; 42 43 // Initializes the AES block with an encryption key. 44 export fn init(b: *block, key: []u8) void = initfuncptr(b, key); 45 46 fn block_finish(b: *cipher::block) void = { 47 let b = b: *block; 48 bytes::zero(b.expkey); 49 };