summaryrefslogtreecommitdiff
path: root/usual/crypto/sha3.c
blob: b6e45a43b86e8af8c8e5cb06c91cfd4e13c63d3e (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
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
/*
 * SHA3 implementation.
 *
 * Copyright (c) 2014  Marko Kreen
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <usual/crypto/sha3.h>
#include <usual/crypto/digest.h>

#define PAD_SHA3	0x06
#define PAD_SHAKE	0x1f

void sha3_224_reset(struct SHA3Context *ctx)
{
	keccak_init(&ctx->kctx, SHA3_224_CAPACITY);
	ctx->padded = 0;
	ctx->obytes = SHA3_224_DIGEST_LENGTH;
	ctx->pad = PAD_SHA3;
}

void sha3_256_reset(struct SHA3Context *ctx)
{
	keccak_init(&ctx->kctx, SHA3_256_CAPACITY);
	ctx->padded = 0;
	ctx->obytes = SHA3_256_DIGEST_LENGTH;
	ctx->pad = PAD_SHA3;
}

void sha3_384_reset(struct SHA3Context *ctx)
{
	keccak_init(&ctx->kctx, SHA3_384_CAPACITY);
	ctx->padded = 0;
	ctx->obytes = SHA3_384_DIGEST_LENGTH;
	ctx->pad = PAD_SHA3;
}

void sha3_512_reset(struct SHA3Context *ctx)
{
	keccak_init(&ctx->kctx, SHA3_512_CAPACITY);
	ctx->padded = 0;
	ctx->obytes = SHA3_512_DIGEST_LENGTH;
	ctx->pad = PAD_SHA3;
}

void shake128_reset(struct SHA3Context *ctx)
{
	keccak_init(&ctx->kctx, SHAKE128_CAPACITY);
	ctx->padded = 0;
	ctx->obytes = SHAKE128_DIGEST_LENGTH;
	ctx->pad = PAD_SHAKE;
}

void shake256_reset(struct SHA3Context *ctx)
{
	keccak_init(&ctx->kctx, SHAKE256_CAPACITY);
	ctx->padded = 0;
	ctx->obytes = SHAKE256_DIGEST_LENGTH;
	ctx->pad = PAD_SHAKE;
}

void sha3_update(struct SHA3Context *ctx, const void *ptr, unsigned len)
{
	keccak_absorb(&ctx->kctx, ptr, len);
}

void sha3_final(struct SHA3Context *ctx, void *dst)
{
	if (!ctx->padded) {
		keccak_pad(&ctx->kctx, &ctx->pad, 1);
		ctx->padded = 1;
	}
	keccak_squeeze(&ctx->kctx, dst, ctx->obytes);
}

void shake_update(struct SHA3Context *ctx, const void *ptr, unsigned len)
{
	keccak_absorb(&ctx->kctx, ptr, len);
}

void shake_extract(struct SHA3Context *ctx, void *dst, unsigned count)
{
	if (!ctx->padded) {
		keccak_pad(&ctx->kctx, &ctx->pad, 1);
		ctx->padded = 1;
	}
	keccak_squeeze(&ctx->kctx, dst, count);
}

/*
 * DigestInfo
 */

static const struct DigestInfo sha3_224_info = {
	(DigestInitFunc *)sha3_224_reset,
	(DigestUpdateFunc *)sha3_update,
	(DigestFinalFunc *)sha3_final,
	sizeof(struct SHA3Context),
	SHA3_224_DIGEST_LENGTH,
	SHA3_224_BLOCK_SIZE
};

static const struct DigestInfo sha3_256_info = {
	(DigestInitFunc *)sha3_256_reset,
	(DigestUpdateFunc *)sha3_update,
	(DigestFinalFunc *)sha3_final,
	sizeof(struct SHA3Context),
	SHA3_256_DIGEST_LENGTH,
	SHA3_256_BLOCK_SIZE
};

static const struct DigestInfo sha3_384_info = {
	(DigestInitFunc *)sha3_384_reset,
	(DigestUpdateFunc *)sha3_update,
	(DigestFinalFunc *)sha3_final,
	sizeof(struct SHA3Context),
	SHA3_384_DIGEST_LENGTH,
	SHA3_384_BLOCK_SIZE
};

static const struct DigestInfo sha3_512_info = {
	(DigestInitFunc *)sha3_512_reset,
	(DigestUpdateFunc *)sha3_update,
	(DigestFinalFunc *)sha3_final,
	sizeof(struct SHA3Context),
	SHA3_512_DIGEST_LENGTH,
	SHA3_512_BLOCK_SIZE
};

static const struct DigestInfo shake128_info = {
	(DigestInitFunc *)shake128_reset,
	(DigestUpdateFunc *)sha3_update,
	(DigestFinalFunc *)sha3_final,
	sizeof(struct SHA3Context),
	SHAKE128_DIGEST_LENGTH,
	SHAKE128_BLOCK_SIZE
};

static const struct DigestInfo shake256_info = {
	(DigestInitFunc *)shake256_reset,
	(DigestUpdateFunc *)sha3_update,
	(DigestFinalFunc *)sha3_final,
	sizeof(struct SHA3Context),
	SHAKE256_DIGEST_LENGTH,
	SHAKE256_BLOCK_SIZE
};

const struct DigestInfo *digest_SHA3_224(void)
{
	return &sha3_224_info;
}

const struct DigestInfo *digest_SHA3_256(void)
{
	return &sha3_256_info;
}

const struct DigestInfo *digest_SHA3_384(void)
{
	return &sha3_384_info;
}

const struct DigestInfo *digest_SHA3_512(void)
{
	return &sha3_512_info;
}

const struct DigestInfo *digest_SHAKE128(void)
{
	return &shake128_info;
}

const struct DigestInfo *digest_SHAKE256(void)
{
	return &shake256_info;
}