aboutsummaryrefslogtreecommitdiff
path: root/core/crypto/_aes/ct64/helpers.odin
blob: 7eec5bdc4330c583e7e51b780fcba655396d1e18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package aes_ct64

import "core:crypto/_aes"
import "core:encoding/endian"

@(require_results)
load_interleaved :: proc "contextless" (src: []byte) -> (u64, u64) #no_bounds_check {
	w0 := endian.unchecked_get_u32le(src[0:])
	w1 := endian.unchecked_get_u32le(src[4:])
	w2 := endian.unchecked_get_u32le(src[8:])
	w3 := endian.unchecked_get_u32le(src[12:])
	return interleave_in(w0, w1, w2, w3)
}

store_interleaved :: proc "contextless" (dst: []byte, a0, a1: u64) #no_bounds_check {
	w0, w1, w2, w3 := interleave_out(a0, a1)
	endian.unchecked_put_u32le(dst[0:], w0)
	endian.unchecked_put_u32le(dst[4:], w1)
	endian.unchecked_put_u32le(dst[8:], w2)
	endian.unchecked_put_u32le(dst[12:], w3)
}

@(require_results)
xor_interleaved :: #force_inline proc "contextless" (a0, a1, b0, b1: u64) -> (u64, u64) {
	return a0 ~ b0, a1 ~ b1
}

@(require_results)
and_interleaved :: #force_inline proc "contextless" (a0, a1, b0, b1: u64) -> (u64, u64) {
	return a0 & b0, a1 & b1
}

load_blockx1 :: proc "contextless" (q: ^[8]u64, src: []byte) {
	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) {
	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) {
	ensure_contextless(len(src) == 0 || len(src) <= STRIDE, "aes/ct64: invalid block(s) size")

	for s, i in src {
		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) {
	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
		}
		ensure_contextless(len(d) == _aes.BLOCK_SIZE, "aes/ct64: invalid block size")
		#force_inline store_interleaved(d, q[i], q[i + 4])
	}
}