2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * An aggregate context that is large enough for all supported hash
32 const br_hash_class
*vtable
;
35 br_sha224_context sha224
;
36 br_sha256_context sha256
;
37 br_sha384_context sha384
;
38 br_sha512_context sha512
;
42 * Get the offset to the state for a specific hash function within the
43 * context structure. This shall be called only for the supported hash
47 get_state_offset(int id
)
51 * SHA-384 has id 5, and SHA-512 has id 6. Both use
52 * eight 64-bit words for their state.
54 return offsetof(br_multihash_context
, val_64
)
55 + ((size_t)(id
- 5) * (8 * sizeof(uint64_t)));
58 * MD5 has id 1, SHA-1 has id 2, SHA-224 has id 3 and
59 * SHA-256 has id 4. They use 32-bit words for their
60 * states (4 words for MD5, 5 for SHA-1, 8 for SHA-224
66 x
= ((x
+ (x
& (x
>> 1))) << 2) + (x
>> 1);
67 return offsetof(br_multihash_context
, val_32
)
68 + x
* sizeof(uint32_t);
72 /* see bearssl_hash.h */
74 br_multihash_zero(br_multihash_context
*ctx
)
77 * This is not standard, but yields very short and efficient code,
78 * and it works "everywhere".
80 memset(ctx
, 0, sizeof *ctx
);
83 /* see bearssl_hash.h */
85 br_multihash_init(br_multihash_context
*ctx
)
90 for (i
= 1; i
<= 6; i
++) {
91 const br_hash_class
*hc
;
93 hc
= ctx
->impl
[i
- 1];
99 (unsigned char *)ctx
+ get_state_offset(i
));
104 /* see bearssl_hash.h */
106 br_multihash_update(br_multihash_context
*ctx
, const void *data
, size_t len
)
108 const unsigned char *buf
;
112 ptr
= (size_t)ctx
->count
& 127;
120 memcpy(ctx
->buf
+ ptr
, buf
, clen
);
124 ctx
->count
+= (uint64_t)clen
;
128 for (i
= 1; i
<= 6; i
++) {
129 const br_hash_class
*hc
;
131 hc
= ctx
->impl
[i
- 1];
134 unsigned char *state
;
136 state
= (unsigned char *)ctx
137 + get_state_offset(i
);
138 hc
->set_state(&g
.vtable
,
139 state
, ctx
->count
- 128);
140 hc
->update(&g
.vtable
, ctx
->buf
, 128);
141 hc
->state(&g
.vtable
, state
);
149 /* see bearssl_hash.h */
151 br_multihash_out(const br_multihash_context
*ctx
, int id
, void *dst
)
153 const br_hash_class
*hc
;
155 const unsigned char *state
;
157 hc
= ctx
->impl
[id
- 1];
161 state
= (const unsigned char *)ctx
+ get_state_offset(id
);
162 hc
->set_state(&g
.vtable
, state
, ctx
->count
& ~(uint64_t)127);
163 hc
->update(&g
.vtable
, ctx
->buf
, ctx
->count
& (uint64_t)127);
164 hc
->out(&g
.vtable
, dst
);
165 return (hc
->desc
>> BR_HASHDESC_OUT_OFF
) & BR_HASHDESC_OUT_MASK
;