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

import "core:crypto/_aes/ct64"
import "core:mem"
import "core:reflect"

@(private)
Context_Impl :: union {
	ct64.Context,
	Context_Impl_Hardware,
}

// DEFAULT_IMPLEMENTATION is the implementation that will be used by
// default if possible.
DEFAULT_IMPLEMENTATION :: Implementation.Hardware

// Implementation is an AES implementation.  Most callers will not need
// to use this as the package will automatically select the most performant
// implementation available (See `is_hardware_accelerated()`).
Implementation :: enum {
	Portable,
	Hardware,
}

@(private)
init_impl :: proc(ctx: ^Context_Impl, key: []byte, impl: Implementation) {
	impl := impl
	if !is_hardware_accelerated() {
		impl = .Portable
	}

	switch impl {
	case .Portable:
		reflect.set_union_variant_typeid(ctx^, typeid_of(ct64.Context))
		ct64.init(&ctx.(ct64.Context), key)
	case .Hardware:
		reflect.set_union_variant_typeid(ctx^, typeid_of(Context_Impl_Hardware))
		init_impl_hw(&ctx.(Context_Impl_Hardware), key)
	}
}

@(private)
reset_impl :: proc "contextless" (ctx: ^Context_Impl) {
	mem.zero_explicit(ctx, size_of(Context_Impl))
}