aboutsummaryrefslogtreecommitdiff
path: root/core/crypto/aes/aes_ecb.odin
blob: cac62de5d442d8d444e9b28ce0d31acda8b2e9d9 (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
package aes

import "core:crypto/_aes/ct64"

// Context_ECB is a keyed AES-ECB instance.
//
// WARNING: Using ECB mode is strongly discouraged unless it is being
// used to implement higher level constructs.
Context_ECB :: struct {
	_impl:           Context_Impl,
	_is_initialized: bool,
}

// init_ecb initializes a Context_ECB with the provided key.
init_ecb :: proc(ctx: ^Context_ECB, key: []byte, impl := DEFAULT_IMPLEMENTATION) {
	init_impl(&ctx._impl, key, impl)
	ctx._is_initialized = true
}

// encrypt_ecb encrypts the BLOCK_SIZE buffer src, and writes the result to dst.
encrypt_ecb :: proc(ctx: ^Context_ECB, dst, src: []byte) {
	ensure(ctx._is_initialized)
	ensure(len(dst) == BLOCK_SIZE, "crypto/aes: invalid dst size")
	ensure(len(dst) == BLOCK_SIZE, "crypto/aes: invalid src size")

	switch &impl in ctx._impl {
	case ct64.Context:
		ct64.encrypt_block(&impl, dst, src)
	case Context_Impl_Hardware:
		encrypt_block_hw(&impl, dst, src)
	}
}

// decrypt_ecb decrypts the BLOCK_SIZE buffer src, and writes the result to dst.
decrypt_ecb :: proc(ctx: ^Context_ECB, dst, src: []byte) {
	ensure(ctx._is_initialized)
	ensure(len(dst) == BLOCK_SIZE, "crypto/aes: invalid dst size")
	ensure(len(dst) == BLOCK_SIZE, "crypto/aes: invalid src size")

	switch &impl in ctx._impl {
	case ct64.Context:
		ct64.decrypt_block(&impl, dst, src)
	case Context_Impl_Hardware:
		decrypt_block_hw(&impl, dst, src)
	}
}

// reset_ecb sanitizes the Context_ECB.  The Context_ECB must be
// re-initialized to be used again.
reset_ecb :: proc "contextless" (ctx: ^Context_ECB) {
	reset_impl(&ctx._impl)
	ctx._is_initialized = false
}