aboutsummaryrefslogtreecommitdiff
path: root/core/crypto/aead/doc.odin
blob: 811dd564dad629ae2e51c6fd37357a8d3b72fce1 (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
/*
A generic interface to Authenticated Encryption with Associated Data (`AEAD`) algorithms.

Both a one-shot and context based interface are provided, with similar
usage.  If multiple messages are to be sealed/opened via the same key,
the context based interface may be more efficient, depending on the
algorithm.

WARNING: Reusing the same key + iv to seal (encrypt) multiple messages
results in catastrophic loss of security for most algorithms.

Example:
	package aead_example

	import "core:bytes"
	import "core:crypto"
	import "core:crypto/aead"

	main :: proc() {
		algo := aead.Algorithm.XCHACHA20POLY1305

		// The example added associated data, and plaintext.
		aad_str := "Get your ass in gear boys."
		pt_str := "They're immanetizing the Eschaton."

		aad := transmute([]byte)aad_str
		plaintext := transmute([]byte)pt_str
		pt_len := len(plaintext)

		// Generate a random key for the purposes of illustration.
		key := make([]byte, aead.KEY_SIZES[algo])
		defer delete(key)
		crypto.rand_bytes(key)

		// `ciphertext || tag`, is a common way data is transmitted, so
		// demonstrate that.
		buf := make([]byte, pt_len + aead.TAG_SIZES[algo])
		defer delete(buf)
		ciphertext, tag := buf[:pt_len], buf[pt_len:]

		// Seal the AAD + Plaintext.
		iv := make([]byte, aead.IV_SIZES[algo])
		defer delete(iv)
		crypto.rand_bytes(iv) // Random IVs are safe with XChaCha20-Poly1305.
		aead.seal(algo, ciphertext, tag, key, iv, aad, plaintext)

		// Open the AAD + Ciphertext.
		opened_pt := buf[:pt_len]
		if ok := aead.open(algo, opened_pt, key, iv, aad, ciphertext, tag); !ok {
			panic("aead example: failed to open")
		}

		assert(bytes.equal(opened_pt, plaintext))
	}
*/
package aead