Skip to content

Commit

Permalink
core/crypto: Switch to using ensure
Browse files Browse the repository at this point in the history
  • Loading branch information
Yawning committed Feb 7, 2025
1 parent 18059d7 commit 81c5094
Show file tree
Hide file tree
Showing 36 changed files with 188 additions and 363 deletions.
4 changes: 1 addition & 3 deletions core/crypto/_aes/ct64/ct64_enc.odin
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
package aes_ct64

add_round_key :: proc "contextless" (q: ^[8]u64, sk: []u64) #no_bounds_check {
if len(sk) < 8 {
panic_contextless("aes/ct64: invalid round key size")
}
ensure_contextless(len(sk) >=8, "aes/ct64: invalid round key size")

q[0] ~= sk[0]
q[1] ~= sk[1]
Expand Down
4 changes: 2 additions & 2 deletions core/crypto/_aes/ct64/ct64_keysched.odin
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ sub_word :: proc "contextless" (x: u32) -> u32 {
}

@(private, require_results)
keysched :: proc(comp_skey: []u64, key: []byte) -> int {
keysched :: proc "contextless" (comp_skey: []u64, key: []byte) -> int {
num_rounds, key_len := 0, len(key)
switch key_len {
case _aes.KEY_SIZE_128:
Expand All @@ -51,7 +51,7 @@ keysched :: proc(comp_skey: []u64, key: []byte) -> int {
case _aes.KEY_SIZE_256:
num_rounds = _aes.ROUNDS_256
case:
panic("crypto/aes: invalid AES key size")
panic_contextless("crypto/aes: invalid AES key size")
}

skey: [60]u32 = ---
Expand Down
5 changes: 2 additions & 3 deletions core/crypto/_aes/ct64/ghash.odin
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ rev64 :: proc "contextless" (x: u64) -> u64 {
// Note: `dst` is both an input and an output, to support easy implementation
// of GCM.
ghash :: proc "contextless" (dst, key, data: []byte) {
if len(dst) != _aes.GHASH_BLOCK_SIZE || len(key) != _aes.GHASH_BLOCK_SIZE {
panic_contextless("aes/ghash: invalid dst or key size")
}
ensure_contextless(len(dst) == _aes.GHASH_BLOCK_SIZE)
ensure_contextless(len(key) == _aes.GHASH_BLOCK_SIZE)

buf := data
l := len(buf)
Expand Down
24 changes: 6 additions & 18 deletions core/crypto/_aes/ct64/helpers.odin
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,39 @@ and_interleaved :: #force_inline proc "contextless" (a0, a1, b0, b1: u64) -> (u6
}

load_blockx1 :: proc "contextless" (q: ^[8]u64, src: []byte) {
if len(src) != _aes.BLOCK_SIZE {
panic_contextless("aes/ct64: invalid block size")
}
ensure_contextless(len(src) == _aes.BLOCK_SIZE, "aes/ct64: invalid block size")

q[0], q[4] = #force_inline load_interleaved(src)
orthogonalize(q)
}

store_blockx1 :: proc "contextless" (dst: []byte, q: ^[8]u64) {
if len(dst) != _aes.BLOCK_SIZE {
panic_contextless("aes/ct64: invalid block size")
}
ensure_contextless(len(dst) == _aes.BLOCK_SIZE, "aes/ct64: invalid block size")

orthogonalize(q)
#force_inline store_interleaved(dst, q[0], q[4])
}

load_blocks :: proc "contextless" (q: ^[8]u64, src: [][]byte) {
if n := len(src); n > STRIDE || n == 0 {
panic_contextless("aes/ct64: invalid block(s) size")
}
ensure_contextless(len(src) == 0 || len(src) <= STRIDE, "aes/ct64: invalid block(s) size")

for s, i in src {
if len(s) != _aes.BLOCK_SIZE {
panic_contextless("aes/ct64: invalid block size")
}
ensure_contextless(len(s) == _aes.BLOCK_SIZE, "aes/ct64: invalid block size")
q[i], q[i + 4] = #force_inline load_interleaved(s)
}
orthogonalize(q)
}

store_blocks :: proc "contextless" (dst: [][]byte, q: ^[8]u64) {
if n := len(dst); n > STRIDE || n == 0 {
panic_contextless("aes/ct64: invalid block(s) size")
}
ensure_contextless(len(dst) == 0 || len(dst) <= STRIDE, "aes/ct64: invalid block(s) size")

orthogonalize(q)
for d, i in dst {
// Allow storing [0,4] blocks.
if d == nil {
break
}
if len(d) != _aes.BLOCK_SIZE {
panic_contextless("aes/ct64: invalid block size")
}
ensure_contextless(len(d) == _aes.BLOCK_SIZE, "aes/ct64: invalid block size")
#force_inline store_interleaved(d, q[i], q[i + 4])
}
}
28 changes: 11 additions & 17 deletions core/crypto/_blake2/blake2.odin
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ BLAKE2S_SIZE :: 32
BLAKE2B_BLOCK_SIZE :: 128
BLAKE2B_SIZE :: 64

MAX_SIZE :: 255

Blake2s_Context :: struct {
h: [8]u32,
t: [2]u32,
Expand Down Expand Up @@ -82,16 +84,13 @@ BLAKE2B_IV := [8]u64 {
0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
}

init :: proc(ctx: ^$T, cfg: ^Blake2_Config) {
init :: proc "contextless" (ctx: ^$T, cfg: ^Blake2_Config) {
when T == Blake2s_Context {
max_size :: BLAKE2S_SIZE
} else when T == Blake2b_Context {
max_size :: BLAKE2B_SIZE
}

if cfg.size > max_size {
panic("blake2: requested output size exceeeds algorithm max")
}
ensure_contextless(cfg.size <= max_size, "blake2: requested output size exceeeds algorithm max")

// To save having to allocate a scratch buffer, use the internal
// data buffer (`ctx.x`), as it is exactly the correct size.
Expand Down Expand Up @@ -167,8 +166,8 @@ init :: proc(ctx: ^$T, cfg: ^Blake2_Config) {
ctx.is_initialized = true
}

update :: proc(ctx: ^$T, p: []byte) {
assert(ctx.is_initialized)
update :: proc "contextless" (ctx: ^$T, p: []byte) {
ensure_contextless(ctx.is_initialized)

p := p
when T == Blake2s_Context {
Expand All @@ -195,8 +194,8 @@ update :: proc(ctx: ^$T, p: []byte) {
ctx.nx += copy(ctx.x[ctx.nx:], p)
}

final :: proc(ctx: ^$T, hash: []byte, finalize_clone: bool = false) {
assert(ctx.is_initialized)
final :: proc "contextless" (ctx: ^$T, hash: []byte, finalize_clone: bool = false) {
ensure_contextless(ctx.is_initialized)

ctx := ctx
if finalize_clone {
Expand All @@ -206,24 +205,19 @@ final :: proc(ctx: ^$T, hash: []byte, finalize_clone: bool = false) {
}
defer(reset(ctx))

ensure_contextless(len(hash) >= int(ctx.size), "crypto/blake2: invalid destination digest size")
when T == Blake2s_Context {
if len(hash) < int(ctx.size) {
panic("crypto/blake2s: invalid destination digest size")
}
blake2s_final(ctx, hash)
} else when T == Blake2b_Context {
if len(hash) < int(ctx.size) {
panic("crypto/blake2b: invalid destination digest size")
}
blake2b_final(ctx, hash)
}
}

clone :: proc(ctx, other: ^$T) {
clone :: proc "contextless" (ctx, other: ^$T) {
ctx^ = other^
}

reset :: proc(ctx: ^$T) {
reset :: proc "contextless" (ctx: ^$T) {
if !ctx.is_initialized {
return
}
Expand Down
25 changes: 11 additions & 14 deletions core/crypto/_chacha20/chacha20.odin
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ Context :: struct {
// derivation is expected to be handled by the caller, so that the
// HChaCha call can be suitably accelerated.
init :: proc "contextless" (ctx: ^Context, key, iv: []byte, is_xchacha: bool) {
if len(key) != KEY_SIZE || len(iv) != IV_SIZE {
panic_contextless("chacha20: invalid key or IV size")
}
ensure_contextless(len(key) == KEY_SIZE, "chacha20: invalid key size")
ensure_contextless(len(iv) == IV_SIZE, "chacha20: invalid key size")

k, n := key, iv

Expand Down Expand Up @@ -75,12 +74,10 @@ init :: proc "contextless" (ctx: ^Context, key, iv: []byte, is_xchacha: bool) {

// seek seeks the (X)ChaCha20 stream counter to the specified block.
seek :: proc(ctx: ^Context, block_nr: u64) {
assert(ctx._is_initialized)
ensure(ctx._is_initialized)

if ctx._is_ietf_flavor {
if block_nr > MAX_CTR_IETF {
panic("crypto/chacha20: attempted to seek past maximum counter")
}
ensure(block_nr <= MAX_CTR_IETF, "crypto/chacha20: attempted to seek past maximum counter")
} else {
ctx._s[13] = u32(block_nr >> 32)
}
Expand All @@ -101,22 +98,22 @@ check_counter_limit :: proc(ctx: ^Context, nr_blocks: int) {
// Enforce the maximum consumed keystream per IV.
//
// While all modern "standard" definitions of ChaCha20 use
// the IETF 32-bit counter, for XChaCha20 most common
// the IETF 32-bit counter, for XChaCha20 historical
// implementations allow for a 64-bit counter.
//
// Honestly, the answer here is "use a MRAE primitive", but
// go with "common" practice in the case of XChaCha20.

ERR_CTR_EXHAUSTED :: "crypto/chacha20: maximum (X)ChaCha20 keystream per IV reached"

ctr_ok: bool
if ctx._is_ietf_flavor {
if u64(ctx._s[12]) + u64(nr_blocks) > MAX_CTR_IETF {
panic(ERR_CTR_EXHAUSTED)
}
ctr_ok = u64(ctx._s[12]) + u64(nr_blocks) <= MAX_CTR_IETF
} else {
ctr := (u64(ctx._s[13]) << 32) | u64(ctx._s[12])
if _, carry := bits.add_u64(ctr, u64(nr_blocks), 0); carry != 0 {
panic(ERR_CTR_EXHAUSTED)
}
_, carry := bits.add_u64(ctr, u64(nr_blocks), 0)
ctr_ok = carry == 0
}

ensure(ctr_ok, "crypto/chacha20: maximum (X)ChaCha20 keystream per IV reached")
}
8 changes: 2 additions & 6 deletions core/crypto/_edwards25519/edwards25519.odin
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ ge_set :: proc "contextless" (ge, a: ^Group_Element) {

@(require_results)
ge_set_bytes :: proc "contextless" (ge: ^Group_Element, b: []byte) -> bool {
if len(b) != 32 {
panic_contextless("edwards25519: invalid group element size")
}
ensure_contextless(len(b) == 32, "edwards25519: invalid group element size")
b_ := (^[32]byte)(raw_data(b))

// Do the work in a scratch element, so that ge is unchanged on
Expand Down Expand Up @@ -167,9 +165,7 @@ ge_set_bytes :: proc "contextless" (ge: ^Group_Element, b: []byte) -> bool {
}

ge_bytes :: proc "contextless" (ge: ^Group_Element, dst: []byte) {
if len(dst) != 32 {
panic_contextless("edwards25519: invalid group element size")
}
ensure_contextless(len(dst) == 32, "edwards25519: invalid group element size")
dst_ := (^[32]byte)(raw_data(dst))

// Convert the element to affine (x, y) representation.
Expand Down
8 changes: 2 additions & 6 deletions core/crypto/_edwards25519/edwards25519_scalar.odin
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ sc_set_u64 :: proc "contextless" (sc: ^Scalar, i: u64) {

@(require_results)
sc_set_bytes :: proc "contextless" (sc: ^Scalar, b: []byte) -> bool {
if len(b) != 32 {
panic_contextless("edwards25519: invalid scalar size")
}
ensure_contextless(len(b) == 32, "edwards25519: invalid scalar size")
b_ := (^[32]byte)(raw_data(b))
return field.fe_from_bytes(sc, b_)
}

sc_set_bytes_rfc8032 :: proc "contextless" (sc: ^Scalar, b: []byte) {
if len(b) != 32 {
panic_contextless("edwards25519: invalid scalar size")
}
ensure_contextless(len(b) == 32, "edwards25519: invalid scalar size")
b_ := (^[32]byte)(raw_data(b))
field.fe_from_bytes_rfc8032(sc, b_)
}
Expand Down
4 changes: 1 addition & 3 deletions core/crypto/_fiat/field_poly1305/field.odin
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ fe_from_bytes :: #force_inline proc "contextless" (
// makes implementing the actual MAC block processing considerably
// neater.

if len(arg1) != 16 {
panic_contextless("poly1305: invalid field element size")
}
ensure_contextless(len(arg1) == 16, "poly1305: invalid field element size")

// While it may be unwise to do deserialization here on our
// own when fiat-crypto provides equivalent functionality,
Expand Down
9 changes: 3 additions & 6 deletions core/crypto/_fiat/field_scalar25519/field.odin
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ fe_from_bytes_wide :: proc "contextless" (
@(private)
_fe_from_bytes_short :: proc "contextless" (out1: ^Montgomery_Domain_Field_Element, arg1: []byte) {
// INVARIANT: len(arg1) < 32.
if len(arg1) >= 32 {
panic_contextless("edwards25519: oversized short scalar")
}
ensure_contextless(len(arg1) < 32, "edwards25519: oversized short scalar")

tmp: [32]byte
copy(tmp[:], arg1)

Expand All @@ -105,9 +104,7 @@ _fe_from_bytes_short :: proc "contextless" (out1: ^Montgomery_Domain_Field_Eleme
}

fe_to_bytes :: proc "contextless" (out1: []byte, arg1: ^Montgomery_Domain_Field_Element) {
if len(out1) != 32 {
panic_contextless("edwards25519: oversized scalar output buffer")
}
ensure_contextless(len(out1) == 32, "edwards25519: oversized scalar output buffer")

tmp: Non_Montgomery_Domain_Field_Element
fe_from_montgomery(&tmp, arg1)
Expand Down
33 changes: 15 additions & 18 deletions core/crypto/_sha3/sha3.odin
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ keccakf :: proc "contextless" (st: ^[25]u64) {
}
}

init :: proc(ctx: ^Context) {
init :: proc "contextless" (ctx: ^Context) {
for i := 0; i < 25; i += 1 {
ctx.st.q[i] = 0
}
Expand All @@ -133,9 +133,9 @@ init :: proc(ctx: ^Context) {
ctx.is_finalized = false
}

update :: proc(ctx: ^Context, data: []byte) {
assert(ctx.is_initialized)
assert(!ctx.is_finalized)
update :: proc "contextless" (ctx: ^Context, data: []byte) {
ensure_contextless(ctx.is_initialized)
ensure_contextless(!ctx.is_finalized)

j := ctx.pt
for i := 0; i < len(data); i += 1 {
Expand All @@ -149,12 +149,9 @@ update :: proc(ctx: ^Context, data: []byte) {
ctx.pt = j
}

final :: proc(ctx: ^Context, hash: []byte, finalize_clone: bool = false) {
assert(ctx.is_initialized)

if len(hash) < ctx.mdlen {
panic("crypto/sha3: invalid destination digest size")
}
final :: proc "contextless" (ctx: ^Context, hash: []byte, finalize_clone: bool = false) {
ensure_contextless(ctx.is_initialized)
ensure_contextless(len(hash) >= ctx.mdlen, "crypto/sha3: invalid destination digest size")

ctx := ctx
if finalize_clone {
Expand All @@ -173,21 +170,21 @@ final :: proc(ctx: ^Context, hash: []byte, finalize_clone: bool = false) {
}
}

clone :: proc(ctx, other: ^Context) {
clone :: proc "contextless" (ctx, other: ^Context) {
ctx^ = other^
}

reset :: proc(ctx: ^Context) {
reset :: proc "contextless" (ctx: ^Context) {
if !ctx.is_initialized {
return
}

mem.zero_explicit(ctx, size_of(ctx^))
}

shake_xof :: proc(ctx: ^Context) {
assert(ctx.is_initialized)
assert(!ctx.is_finalized)
shake_xof :: proc "contextless" (ctx: ^Context) {
ensure_contextless(ctx.is_initialized)
ensure_contextless(!ctx.is_finalized)

ctx.st.b[ctx.pt] ~= ctx.dsbyte
ctx.st.b[ctx.rsiz - 1] ~= 0x80
Expand All @@ -197,9 +194,9 @@ shake_xof :: proc(ctx: ^Context) {
ctx.is_finalized = true // No more absorb, unlimited squeeze.
}

shake_out :: proc(ctx: ^Context, hash: []byte) {
assert(ctx.is_initialized)
assert(ctx.is_finalized)
shake_out :: proc "contextless" (ctx: ^Context, hash: []byte) {
ensure_contextless(ctx.is_initialized)
ensure_contextless(ctx.is_finalized)

j := ctx.pt
for i := 0; i < len(hash); i += 1 {
Expand Down
Loading

0 comments on commit 81c5094

Please sign in to comment.