File Coverage

src/hash/multihash.c
Criterion Covered Total %
statement 50 51 98.0
branch 15 18 83.3
condition n/a
subroutine n/a
pod n/a
total 65 69 94.2


line stmt bran cond sub pod time code
1             /*
2             * Copyright (c) 2016 Thomas Pornin
3             *
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:
11             *
12             * The above copyright notice and this permission notice shall be
13             * included in all copies or substantial portions of the Software.
14             *
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
22             * SOFTWARE.
23             */
24              
25             #include "inner.h"
26              
27             /*
28             * An aggregate context that is large enough for all supported hash
29             * functions.
30             */
31             typedef union {
32             const br_hash_class *vtable;
33             br_md5_context md5;
34             br_sha1_context sha1;
35             br_sha224_context sha224;
36             br_sha256_context sha256;
37             br_sha384_context sha384;
38             br_sha512_context sha512;
39             } gen_hash_context;
40              
41             /*
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
44             * functions,
45             */
46             static size_t
47 185           get_state_offset(int id)
48             {
49 185 100         if (id >= 5) {
50             /*
51             * SHA-384 has id 5, and SHA-512 has id 6. Both use
52             * eight 64-bit words for their state.
53             */
54             return offsetof(br_multihash_context, val_64)
55 60           + ((size_t)(id - 5) * (8 * sizeof(uint64_t)));
56             } else {
57             /*
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
61             * and 8 for SHA-256).
62             */
63             unsigned x;
64              
65 125           x = id - 1;
66 125           x = ((x + (x & (x >> 1))) << 2) + (x >> 1);
67             return offsetof(br_multihash_context, val_32)
68 125           + x * sizeof(uint32_t);
69             }
70             }
71              
72             /* see bearssl_hash.h */
73             void
74 1           br_multihash_zero(br_multihash_context *ctx)
75             {
76             /*
77             * This is not standard, but yields very short and efficient code,
78             * and it works "everywhere".
79             */
80 1           memset(ctx, 0, sizeof *ctx);
81 1           }
82              
83             /* see bearssl_hash.h */
84             void
85 4           br_multihash_init(br_multihash_context *ctx)
86             {
87             int i;
88              
89 4           ctx->count = 0;
90 28 100         for (i = 1; i <= 6; i ++) {
91             const br_hash_class *hc;
92              
93 24           hc = ctx->impl[i - 1];
94 24 50         if (hc != NULL) {
95             gen_hash_context g;
96              
97 24           hc->init(&g.vtable);
98 24           hc->state(&g.vtable,
99 24           (unsigned char *)ctx + get_state_offset(i));
100             }
101             }
102 4           }
103              
104             /* see bearssl_hash.h */
105             void
106 897           br_multihash_update(br_multihash_context *ctx, const void *data, size_t len)
107             {
108             const unsigned char *buf;
109             size_t ptr;
110              
111 897           buf = data;
112 897           ptr = (size_t)ctx->count & 127;
113 1811 100         while (len > 0) {
114             size_t clen;
115              
116 914           clen = 128 - ptr;
117 914 100         if (clen > len) {
118 888           clen = len;
119             }
120 914           memcpy(ctx->buf + ptr, buf, clen);
121 914           ptr += clen;
122 914           buf += clen;
123 914           len -= clen;
124 914           ctx->count += (uint64_t)clen;
125 914 100         if (ptr == 128) {
126             int i;
127              
128 182 100         for (i = 1; i <= 6; i ++) {
129             const br_hash_class *hc;
130              
131 156           hc = ctx->impl[i - 1];
132 156 50         if (hc != NULL) {
133             gen_hash_context g;
134             unsigned char *state;
135              
136 156           state = (unsigned char *)ctx
137 156           + get_state_offset(i);
138 156           hc->set_state(&g.vtable,
139 156           state, ctx->count - 128);
140 156           hc->update(&g.vtable, ctx->buf, 128);
141 156           hc->state(&g.vtable, state);
142             }
143             }
144 26           ptr = 0;
145             }
146             }
147 897           }
148              
149             /* see bearssl_hash.h */
150             size_t
151 5           br_multihash_out(const br_multihash_context *ctx, int id, void *dst)
152             {
153             const br_hash_class *hc;
154             gen_hash_context g;
155             const unsigned char *state;
156              
157 5           hc = ctx->impl[id - 1];
158 5 50         if (hc == NULL) {
159 0           return 0;
160             }
161 5           state = (const unsigned char *)ctx + get_state_offset(id);
162 5           hc->set_state(&g.vtable, state, ctx->count & ~(uint64_t)127);
163 5           hc->update(&g.vtable, ctx->buf, ctx->count & (uint64_t)127);
164 5           hc->out(&g.vtable, dst);
165 5           return (hc->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
166             }