aboutsummaryrefslogtreecommitdiff
path: root/sys/crypto/blake2/blake2-sw.c
blob: c25d4db50e29144306d5ede5c985d88396495134 (plain) (blame)
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
/* This file is in the public domain. */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <contrib/libb2/blake2.h>
#include <opencrypto/xform_auth.h>

extern int blake2b_init_ref(blake2b_state *S, size_t outlen);
extern int blake2b_init_param_ref(blake2b_state *S, const blake2b_param *P);
extern int blake2b_init_key_ref(blake2b_state *S, size_t outlen,
    const void *key, size_t keylen);
extern int blake2b_update_ref(blake2b_state *S, const uint8_t *in,
    size_t inlen);
extern int blake2b_final_ref(blake2b_state *S, uint8_t *out, size_t outlen);
extern int blake2b_ref(uint8_t *out, const void *in, const void *key,
    size_t outlen, size_t inlen, size_t keylen);

extern int blake2s_init_ref(blake2s_state *S, size_t outlen);
extern int blake2s_init_param_ref(blake2s_state *S, const blake2s_param *P);
extern int blake2s_init_key_ref(blake2s_state *S, size_t outlen,
    const void *key, size_t keylen);
extern int blake2s_update_ref(blake2s_state *S, const uint8_t *in,
    size_t inlen);
extern int blake2s_final_ref(blake2s_state *S, uint8_t *out, size_t outlen);
extern int blake2s_ref(uint8_t *out, const void *in, const void *key,
    size_t outlen, size_t inlen, size_t keylen);

struct blake2b_xform_ctx {
	blake2b_state state;
	uint8_t key[BLAKE2B_KEYBYTES];
	uint16_t klen;
};
CTASSERT(sizeof(union authctx) >= sizeof(struct blake2b_xform_ctx));

static void
blake2b_xform_init(void *vctx)
{
	struct blake2b_xform_ctx *ctx = vctx;
	int rc;

	if (ctx->klen > 0)
		rc = blake2b_init_key_ref(&ctx->state, BLAKE2B_OUTBYTES,
		    ctx->key, ctx->klen);
	else
		rc = blake2b_init_ref(&ctx->state, BLAKE2B_OUTBYTES);
	if (rc != 0)
		panic("blake2b_init_key: invalid arguments");
}

static void
blake2b_xform_setkey(void *vctx, const uint8_t *key, uint16_t klen)
{
	struct blake2b_xform_ctx *ctx = vctx;

	if (klen > sizeof(ctx->key))
		panic("invalid klen %u", (unsigned)klen);
	memcpy(ctx->key, key, klen);
	ctx->klen = klen;
}

static int
blake2b_xform_update(void *vctx, const uint8_t *data, uint16_t len)
{
	struct blake2b_xform_ctx *ctx = vctx;
	int rc;

	rc = blake2b_update_ref(&ctx->state, data, len);
	if (rc != 0)
		return (EINVAL);
	return (0);
}

static void
blake2b_xform_final(uint8_t *out, void *vctx)
{
	struct blake2b_xform_ctx *ctx = vctx;
	int rc;

	rc = blake2b_final_ref(&ctx->state, out, BLAKE2B_OUTBYTES);
	if (rc != 0)
		panic("blake2b_final: invalid");
}

struct auth_hash auth_hash_blake2b = {
	.type = CRYPTO_BLAKE2B,
	.name = "Blake2b",
	.keysize = BLAKE2B_KEYBYTES,
	.hashsize = BLAKE2B_OUTBYTES,
	.ctxsize = sizeof(struct blake2b_xform_ctx),
	.Setkey = blake2b_xform_setkey,
	.Init = blake2b_xform_init,
	.Update = blake2b_xform_update,
	.Final = blake2b_xform_final,
};

struct blake2s_xform_ctx {
	blake2s_state state;
	uint8_t key[BLAKE2S_KEYBYTES];
	uint16_t klen;
};
CTASSERT(sizeof(union authctx) >= sizeof(struct blake2s_xform_ctx));

static void
blake2s_xform_init(void *vctx)
{
	struct blake2s_xform_ctx *ctx = vctx;
	int rc;

	if (ctx->klen > 0)
		rc = blake2s_init_key_ref(&ctx->state, BLAKE2S_OUTBYTES,
		    ctx->key, ctx->klen);
	else
		rc = blake2s_init_ref(&ctx->state, BLAKE2S_OUTBYTES);
	if (rc != 0)
		panic("blake2s_init_key: invalid arguments");
}

static void
blake2s_xform_setkey(void *vctx, const uint8_t *key, uint16_t klen)
{
	struct blake2s_xform_ctx *ctx = vctx;

	if (klen > sizeof(ctx->key))
		panic("invalid klen %u", (unsigned)klen);
	memcpy(ctx->key, key, klen);
	ctx->klen = klen;
}

static int
blake2s_xform_update(void *vctx, const uint8_t *data, uint16_t len)
{
	struct blake2s_xform_ctx *ctx = vctx;
	int rc;

	rc = blake2s_update_ref(&ctx->state, data, len);
	if (rc != 0)
		return (EINVAL);
	return (0);
}

static void
blake2s_xform_final(uint8_t *out, void *vctx)
{
	struct blake2s_xform_ctx *ctx = vctx;
	int rc;

	rc = blake2s_final_ref(&ctx->state, out, BLAKE2S_OUTBYTES);
	if (rc != 0)
		panic("blake2s_final: invalid");
}

struct auth_hash auth_hash_blake2s = {
	.type = CRYPTO_BLAKE2S,
	.name = "Blake2s",
	.keysize = BLAKE2S_KEYBYTES,
	.hashsize = BLAKE2S_OUTBYTES,
	.ctxsize = sizeof(struct blake2s_xform_ctx),
	.Setkey = blake2s_xform_setkey,
	.Init = blake2s_xform_init,
	.Update = blake2s_xform_update,
	.Final = blake2s_xform_final,
};