xref: /freebsd/contrib/bearssl/src/hash/multihash.c (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty  * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3*0957b409SSimon J. Gerraty  *
4*0957b409SSimon J. Gerraty  * Permission is hereby granted, free of charge, to any person obtaining
5*0957b409SSimon J. Gerraty  * a copy of this software and associated documentation files (the
6*0957b409SSimon J. Gerraty  * "Software"), to deal in the Software without restriction, including
7*0957b409SSimon J. Gerraty  * without limitation the rights to use, copy, modify, merge, publish,
8*0957b409SSimon J. Gerraty  * distribute, sublicense, and/or sell copies of the Software, and to
9*0957b409SSimon J. Gerraty  * permit persons to whom the Software is furnished to do so, subject to
10*0957b409SSimon J. Gerraty  * the following conditions:
11*0957b409SSimon J. Gerraty  *
12*0957b409SSimon J. Gerraty  * The above copyright notice and this permission notice shall be
13*0957b409SSimon J. Gerraty  * included in all copies or substantial portions of the Software.
14*0957b409SSimon J. Gerraty  *
15*0957b409SSimon J. Gerraty  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*0957b409SSimon J. Gerraty  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*0957b409SSimon J. Gerraty  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*0957b409SSimon J. Gerraty  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*0957b409SSimon J. Gerraty  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*0957b409SSimon J. Gerraty  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*0957b409SSimon J. Gerraty  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*0957b409SSimon J. Gerraty  * SOFTWARE.
23*0957b409SSimon J. Gerraty  */
24*0957b409SSimon J. Gerraty 
25*0957b409SSimon J. Gerraty #include "inner.h"
26*0957b409SSimon J. Gerraty 
27*0957b409SSimon J. Gerraty /*
28*0957b409SSimon J. Gerraty  * An aggregate context that is large enough for all supported hash
29*0957b409SSimon J. Gerraty  * functions.
30*0957b409SSimon J. Gerraty  */
31*0957b409SSimon J. Gerraty typedef union {
32*0957b409SSimon J. Gerraty 	const br_hash_class *vtable;
33*0957b409SSimon J. Gerraty 	br_md5_context md5;
34*0957b409SSimon J. Gerraty 	br_sha1_context sha1;
35*0957b409SSimon J. Gerraty 	br_sha224_context sha224;
36*0957b409SSimon J. Gerraty 	br_sha256_context sha256;
37*0957b409SSimon J. Gerraty 	br_sha384_context sha384;
38*0957b409SSimon J. Gerraty 	br_sha512_context sha512;
39*0957b409SSimon J. Gerraty } gen_hash_context;
40*0957b409SSimon J. Gerraty 
41*0957b409SSimon J. Gerraty /*
42*0957b409SSimon J. Gerraty  * Get the offset to the state for a specific hash function within the
43*0957b409SSimon J. Gerraty  * context structure. This shall be called only for the supported hash
44*0957b409SSimon J. Gerraty  * functions,
45*0957b409SSimon J. Gerraty  */
46*0957b409SSimon J. Gerraty static size_t
get_state_offset(int id)47*0957b409SSimon J. Gerraty get_state_offset(int id)
48*0957b409SSimon J. Gerraty {
49*0957b409SSimon J. Gerraty 	if (id >= 5) {
50*0957b409SSimon J. Gerraty 		/*
51*0957b409SSimon J. Gerraty 		 * SHA-384 has id 5, and SHA-512 has id 6. Both use
52*0957b409SSimon J. Gerraty 		 * eight 64-bit words for their state.
53*0957b409SSimon J. Gerraty 		 */
54*0957b409SSimon J. Gerraty 		return offsetof(br_multihash_context, val_64)
55*0957b409SSimon J. Gerraty 			+ ((size_t)(id - 5) * (8 * sizeof(uint64_t)));
56*0957b409SSimon J. Gerraty 	} else {
57*0957b409SSimon J. Gerraty 		/*
58*0957b409SSimon J. Gerraty 		 * MD5 has id 1, SHA-1 has id 2, SHA-224 has id 3 and
59*0957b409SSimon J. Gerraty 		 * SHA-256 has id 4. They use 32-bit words for their
60*0957b409SSimon J. Gerraty 		 * states (4 words for MD5, 5 for SHA-1, 8 for SHA-224
61*0957b409SSimon J. Gerraty 		 * and 8 for SHA-256).
62*0957b409SSimon J. Gerraty 		 */
63*0957b409SSimon J. Gerraty 		unsigned x;
64*0957b409SSimon J. Gerraty 
65*0957b409SSimon J. Gerraty 		x = id - 1;
66*0957b409SSimon J. Gerraty 		x = ((x + (x & (x >> 1))) << 2) + (x >> 1);
67*0957b409SSimon J. Gerraty 		return offsetof(br_multihash_context, val_32)
68*0957b409SSimon J. Gerraty 			+ x * sizeof(uint32_t);
69*0957b409SSimon J. Gerraty 	}
70*0957b409SSimon J. Gerraty }
71*0957b409SSimon J. Gerraty 
72*0957b409SSimon J. Gerraty /* see bearssl_hash.h */
73*0957b409SSimon J. Gerraty void
br_multihash_zero(br_multihash_context * ctx)74*0957b409SSimon J. Gerraty br_multihash_zero(br_multihash_context *ctx)
75*0957b409SSimon J. Gerraty {
76*0957b409SSimon J. Gerraty 	/*
77*0957b409SSimon J. Gerraty 	 * This is not standard, but yields very short and efficient code,
78*0957b409SSimon J. Gerraty 	 * and it works "everywhere".
79*0957b409SSimon J. Gerraty 	 */
80*0957b409SSimon J. Gerraty 	memset(ctx, 0, sizeof *ctx);
81*0957b409SSimon J. Gerraty }
82*0957b409SSimon J. Gerraty 
83*0957b409SSimon J. Gerraty /* see bearssl_hash.h */
84*0957b409SSimon J. Gerraty void
br_multihash_init(br_multihash_context * ctx)85*0957b409SSimon J. Gerraty br_multihash_init(br_multihash_context *ctx)
86*0957b409SSimon J. Gerraty {
87*0957b409SSimon J. Gerraty 	int i;
88*0957b409SSimon J. Gerraty 
89*0957b409SSimon J. Gerraty 	ctx->count = 0;
90*0957b409SSimon J. Gerraty 	for (i = 1; i <= 6; i ++) {
91*0957b409SSimon J. Gerraty 		const br_hash_class *hc;
92*0957b409SSimon J. Gerraty 
93*0957b409SSimon J. Gerraty 		hc = ctx->impl[i - 1];
94*0957b409SSimon J. Gerraty 		if (hc != NULL) {
95*0957b409SSimon J. Gerraty 			gen_hash_context g;
96*0957b409SSimon J. Gerraty 
97*0957b409SSimon J. Gerraty 			hc->init(&g.vtable);
98*0957b409SSimon J. Gerraty 			hc->state(&g.vtable,
99*0957b409SSimon J. Gerraty 				(unsigned char *)ctx + get_state_offset(i));
100*0957b409SSimon J. Gerraty 		}
101*0957b409SSimon J. Gerraty 	}
102*0957b409SSimon J. Gerraty }
103*0957b409SSimon J. Gerraty 
104*0957b409SSimon J. Gerraty /* see bearssl_hash.h */
105*0957b409SSimon J. Gerraty void
br_multihash_update(br_multihash_context * ctx,const void * data,size_t len)106*0957b409SSimon J. Gerraty br_multihash_update(br_multihash_context *ctx, const void *data, size_t len)
107*0957b409SSimon J. Gerraty {
108*0957b409SSimon J. Gerraty 	const unsigned char *buf;
109*0957b409SSimon J. Gerraty 	size_t ptr;
110*0957b409SSimon J. Gerraty 
111*0957b409SSimon J. Gerraty 	buf = data;
112*0957b409SSimon J. Gerraty 	ptr = (size_t)ctx->count & 127;
113*0957b409SSimon J. Gerraty 	while (len > 0) {
114*0957b409SSimon J. Gerraty 		size_t clen;
115*0957b409SSimon J. Gerraty 
116*0957b409SSimon J. Gerraty 		clen = 128 - ptr;
117*0957b409SSimon J. Gerraty 		if (clen > len) {
118*0957b409SSimon J. Gerraty 			clen = len;
119*0957b409SSimon J. Gerraty 		}
120*0957b409SSimon J. Gerraty 		memcpy(ctx->buf + ptr, buf, clen);
121*0957b409SSimon J. Gerraty 		ptr += clen;
122*0957b409SSimon J. Gerraty 		buf += clen;
123*0957b409SSimon J. Gerraty 		len -= clen;
124*0957b409SSimon J. Gerraty 		ctx->count += (uint64_t)clen;
125*0957b409SSimon J. Gerraty 		if (ptr == 128) {
126*0957b409SSimon J. Gerraty 			int i;
127*0957b409SSimon J. Gerraty 
128*0957b409SSimon J. Gerraty 			for (i = 1; i <= 6; i ++) {
129*0957b409SSimon J. Gerraty 				const br_hash_class *hc;
130*0957b409SSimon J. Gerraty 
131*0957b409SSimon J. Gerraty 				hc = ctx->impl[i - 1];
132*0957b409SSimon J. Gerraty 				if (hc != NULL) {
133*0957b409SSimon J. Gerraty 					gen_hash_context g;
134*0957b409SSimon J. Gerraty 					unsigned char *state;
135*0957b409SSimon J. Gerraty 
136*0957b409SSimon J. Gerraty 					state = (unsigned char *)ctx
137*0957b409SSimon J. Gerraty 						+ get_state_offset(i);
138*0957b409SSimon J. Gerraty 					hc->set_state(&g.vtable,
139*0957b409SSimon J. Gerraty 						state, ctx->count - 128);
140*0957b409SSimon J. Gerraty 					hc->update(&g.vtable, ctx->buf, 128);
141*0957b409SSimon J. Gerraty 					hc->state(&g.vtable, state);
142*0957b409SSimon J. Gerraty 				}
143*0957b409SSimon J. Gerraty 			}
144*0957b409SSimon J. Gerraty 			ptr = 0;
145*0957b409SSimon J. Gerraty 		}
146*0957b409SSimon J. Gerraty 	}
147*0957b409SSimon J. Gerraty }
148*0957b409SSimon J. Gerraty 
149*0957b409SSimon J. Gerraty /* see bearssl_hash.h */
150*0957b409SSimon J. Gerraty size_t
br_multihash_out(const br_multihash_context * ctx,int id,void * dst)151*0957b409SSimon J. Gerraty br_multihash_out(const br_multihash_context *ctx, int id, void *dst)
152*0957b409SSimon J. Gerraty {
153*0957b409SSimon J. Gerraty 	const br_hash_class *hc;
154*0957b409SSimon J. Gerraty 	gen_hash_context g;
155*0957b409SSimon J. Gerraty 	const unsigned char *state;
156*0957b409SSimon J. Gerraty 
157*0957b409SSimon J. Gerraty 	hc = ctx->impl[id - 1];
158*0957b409SSimon J. Gerraty 	if (hc == NULL) {
159*0957b409SSimon J. Gerraty 		return 0;
160*0957b409SSimon J. Gerraty 	}
161*0957b409SSimon J. Gerraty 	state = (const unsigned char *)ctx + get_state_offset(id);
162*0957b409SSimon J. Gerraty 	hc->set_state(&g.vtable, state, ctx->count & ~(uint64_t)127);
163*0957b409SSimon J. Gerraty 	hc->update(&g.vtable, ctx->buf, ctx->count & (uint64_t)127);
164*0957b409SSimon J. Gerraty 	hc->out(&g.vtable, dst);
165*0957b409SSimon J. Gerraty 	return (hc->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
166*0957b409SSimon J. Gerraty }
167