File: gcm.ha

package info (click to toggle)
hare 0.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,948 kB
  • sloc: asm: 1,264; makefile: 123; sh: 114; lisp: 101
file content (256 lines) | stat: -rw-r--r-- 6,642 bytes parent folder | download
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use bytes;
use crypto::math::{xor,eqslice};
use endian::{beputu64, beputu32, begetu32};
use errors;
use io;
use types;

def GCMBLOCKSZ: size = 16;

export def GCMTAGSZ: size = 16;

export type gcmstream = struct {
	stream: io::stream,
	block: nullable *block,
	handle: io::handle,
	tagbuf: [GCMBLOCKSZ]u8,
	xorbuf: [GCMBLOCKSZ]u8,
	cipherbuf: [GCMBLOCKSZ]u8,
	y0: [GCMBLOCKSZ]u8,
	h: [GCMBLOCKSZ]u8,
	y: u32,
	xorbufpos: size,
	adlen: u64,
	clen: u64,
};

const gcm_vtable: io::vtable = io::vtable {
	writer = &gcm_writer,
	reader = &gcm_reader,
	closer = &gcm_closer,
	...
};

// Creates a Galois Counter Mode (GCM) io::stream which can be used for
// encryption (by encrypting writes to the underlying handle) or decryption (or
// by decrypting reads from the underlying handle), but not both. [[gcm_init]]
// must be called to initialize the stream, before reading or writing. To
// authenticate the encrypted data an authentication tag must be created using
// [[gcm_seal]] after the encryption step. The authentication tag must be passed
// to [[gcm_verify]] after decryption to make sure that the encrypted and
// additional data were not modified. In case of a verification fail the
// decrypted data must not be trusted and hence discarded.
//
// A maximum of 2**36-32 bytes may be encrypted.
//
// The user must call [[io::close]] when they are done using the stream to
// securely erase secret information stored in the stream state. Close will
// also finish the 'block' provided by [[gcm_init]]. If the 'block' should
// not be finished, [[gcm_unlink_block]] must be called before close.
export fn gcm() gcmstream = {
	return gcmstream {
		stream = &gcm_vtable,
		handle = 0,
		...
	};
};

// Initialises the gcmstream. The data will be encrypted to or encrypted from
// the given 'handle' The implementation only supports a block cipher 'b' with a
// block size of 16 bytes. The initialization vector (nonce) 'iv' may have any
// size up to 2**61 bytes. 12 bytes is the recommended size, if efficiency is
// critical. The additional data 'ad' will be authenticated but not encrypted
// and may have a maximum length of 2**61 - 1 bytes. 'ad' will not be written to
// the underlying handle.
export fn gcm_init(
	s: *gcmstream,
	handle: io::handle,
	b: *block,
	iv: const []u8,
	ad: const []u8
) void = {
	assert(blocksz(b) == GCMBLOCKSZ);
	assert(len(iv): u64 <= (types::U64_MAX >> 3));

	s.handle = handle;
	s.block = b;
	s.adlen = len(ad);
	s.xorbufpos = GCMBLOCKSZ; // to force fill xorbuf at start

	encrypt(b, s.h, s.h);

	if (len(iv) == 12) {
		s.y0[..12] = iv[..];
		s.y0[15] |= 1;
	} else {
		let ivlen = s.tagbuf;
		beputu64(ivlen[8..], len(iv) << 3);
		ghash_ctmul64(s.y0, s.h, iv);
		ghash_ctmul64(s.y0, s.h, ivlen);
		bytes::zero(ivlen);
	};

	s.y = begetu32(s.y0[12..]) + 1;

	let ad = ad[..];
	for (len(ad) > 0) {
		const max = if (len(ad) >= GCMBLOCKSZ) {
			yield GCMBLOCKSZ;
		} else {
			yield len(ad);
		};

		ghash_ctmul64(s.tagbuf, s.h, ad[..max]);
		ad = ad[max..];
	};
};

fn gcm_writer(s: *io::stream, buf: const []u8) (size | io::error) = {
	let s = s: *gcmstream;
	if (len(buf) == 0) {
		return 0z;
	};

	if (s.xorbufpos == GCMBLOCKSZ) {
		// current key block is depleted, prepare the next one
		fillxorbuf(s);
	};

	let buf = buf[..];

	let n: size = 0;
	const max = if (s.xorbufpos + len(buf) > len(s.cipherbuf)) {
		yield len(s.cipherbuf) - s.xorbufpos;
	} else {
		yield len(buf);
	};

	let cipher = s.cipherbuf[s.xorbufpos..s.xorbufpos + max];
	let key = s.xorbuf[s.xorbufpos..s.xorbufpos + max];
	xor(cipher, key, buf[..max]);

	const n = io::write(s.handle, cipher)?;
	s.xorbufpos += n;
	s.clen += n;

	if (s.xorbufpos == GCMBLOCKSZ) {
		ghash_ctmul64(s.tagbuf, s.h, s.cipherbuf);
	};

	return n;
};

fn fillxorbuf(s: *gcmstream) void = {
	let y: [GCMBLOCKSZ]u8 = [0...];
	s.xorbuf[..] = s.y0[..];
	beputu32(s.xorbuf[12..], s.y);
	encrypt(s.block as *block, s.xorbuf, s.xorbuf);
	s.y += 1;
	s.xorbufpos = 0;
};

fn gcm_reader(s: *io::stream, buf: []u8) (size | io::EOF | io::error) = {
	let s = s: *gcmstream;

	const n = match (io::read(s.handle, buf)?) {
	case io::EOF =>
		return io::EOF;
	case let s: size =>
		yield s;
	};

	for (let i = n; i > 0) {
		if (s.xorbufpos == GCMBLOCKSZ) {
			fillxorbuf(s);
		};

		const max = if (s.xorbufpos + i > GCMBLOCKSZ) {
			yield len(s.cipherbuf) - s.xorbufpos;
		} else {
			yield i;
		};

		let cipher = s.cipherbuf[s.xorbufpos..s.xorbufpos + max];
		let key = s.xorbuf[s.xorbufpos..s.xorbufpos + max];

		cipher[..] = buf[..max];
		xor(buf[..max], buf[..max], key);

		buf = buf[max..];
		i -= max;

		s.xorbufpos += max;
		s.clen += max;

		if (s.xorbufpos == len(s.cipherbuf)) {
			ghash_ctmul64(s.tagbuf, s.h, s.cipherbuf);
		};
	};

	return n;
};

// Finishes encryption and returns the authentication tag. After calling seal,
// the user must not write any more data to the stream.
export fn gcm_seal(s: *gcmstream, tag: []u8) void = {
	assert(len(tag) == GCMTAGSZ);
	if (s.xorbufpos > 0 && s.xorbufpos < GCMBLOCKSZ) {
		// last block was is not full, therefore the content was not
		// hashed yet.
		ghash_ctmul64(s.tagbuf, s.h, s.cipherbuf[..s.xorbufpos]);
	};

	beputu64(tag, s.adlen << 3);
	beputu64(tag[8..], s.clen << 3);
	ghash_ctmul64(s.tagbuf, s.h, tag);

	// use tmp to store the resulting tag
	encrypt(s.block as *block, tag, s.y0);
	xor(tag, tag, s.tagbuf);
};

// Verifies the authentication tag against the decrypted data. Must be called
// after reading all data from the stream to ensure that the data was not
// modified. If the data was modified, [[errors::invalid]] will be returned and
// the data must not be trusted.
export fn gcm_verify(s: *gcmstream, tag: []u8) (void | errors::invalid) = {
	assert(len(tag) == GCMTAGSZ);
	if (s.xorbufpos > 0 && s.xorbufpos < GCMBLOCKSZ) {
		ghash_ctmul64(s.tagbuf, s.h, s.cipherbuf[..s.xorbufpos]);
	};

	let tmp: [16]u8 = [0...];
	beputu64(tmp, s.adlen << 3);
	beputu64(tmp[8..], s.clen << 3);

	ghash_ctmul64(s.tagbuf, s.h, tmp);

	encrypt(s.block as *block, tmp, s.y0);
	xor(tmp, tmp, s.tagbuf);

	if (eqslice(tag, tmp) == 0) {
		return errors::invalid;
	};
};

// Unlinks the 'block' provided by [[gcm_init]] to avoid finishing it when
// calling [[io::close]] on the stream.
export fn gcm_unlink_block(s: *gcmstream) void = {
	s.block = null;
};

fn gcm_closer(s: *io::stream) (void | io::error) = {
	let s = s: *gcmstream;
	bytes::zero(s.tagbuf);
	bytes::zero(s.xorbuf);
	bytes::zero(s.cipherbuf);
	bytes::zero(s.y0);
	bytes::zero(s.h);

	if (s.block is *block) {
		finish(s.block as *block);
	};
};