File: impl.ha

package info (click to toggle)
hare 0.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,352 kB
  • sloc: asm: 1,374; makefile: 123; sh: 117; lisp: 101
file content (31 lines) | stat: -rw-r--r-- 819 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
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

// TODO: At least use mlock or something
use bytes;
use errors;

export type key = []u8;

// Creates a new secure key. The caller should clear the secret buffer with
// [[bytes::zero]] after initialization.
export fn newkey(buf: []u8, name: str) (key | nomem | errors::error) = {
	return alloc(buf...)?: []u8: key;
};

// Destroys a secure key.
export fn destroy(key: key) void = {
	bytes::zero(key[..]);
	free(key);
};

// Reads secret data from a secure key. When the caller is done using the secret
// buffer, they should use [[bytes::zero]] to securely wipe the buffer memory.
export fn read(key: key, buf: []u8) size = {
	let amt = len(buf);
	if (len(key) < len(buf)) {
		amt = len(key);
	};
	buf[..amt] = key[..amt];
	return amt;
};