File Coverage

src/mac/hmac.c
Criterion Covered Total %
statement 49 50 98.0
branch 5 8 62.5
condition n/a
subroutine n/a
pod n/a
total 54 58 93.1


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             static inline size_t
28 9974           block_size(const br_hash_class *dig)
29             {
30             unsigned ls;
31            
32 9974           ls = (unsigned)(dig->desc >> BR_HASHDESC_LBLEN_OFF)
33             & BR_HASHDESC_LBLEN_MASK;
34 9974           return (size_t)1 << ls;
35             }
36              
37             static void
38 2524           process_key(const br_hash_class **hc, void *ks,
39             const void *key, size_t key_len, unsigned bb)
40             {
41             unsigned char tmp[256];
42             size_t blen, u;
43              
44 2524           blen = block_size(*hc);
45 2524           memcpy(tmp, key, key_len);
46 83502 100         for (u = 0; u < key_len; u ++) {
47 80978           tmp[u] ^= (unsigned char)bb;
48             }
49 2524           memset(tmp + key_len, bb, blen - key_len);
50 2524           (*hc)->init(hc);
51 2524           (*hc)->update(hc, tmp, blen);
52 2524           (*hc)->state(hc, ks);
53 2524           }
54              
55             /* see bearssl.h */
56             void
57 1262           br_hmac_key_init(br_hmac_key_context *kc,
58             const br_hash_class *dig, const void *key, size_t key_len)
59             {
60             br_hash_compat_context hc;
61             unsigned char kbuf[64];
62              
63 1262           kc->dig_vtable = dig;
64 1262           hc.vtable = dig;
65 1262 100         if (key_len > block_size(dig)) {
66 2           dig->init(&hc.vtable);
67 2           dig->update(&hc.vtable, key, key_len);
68 2           dig->out(&hc.vtable, kbuf);
69 2           key = kbuf;
70 2           key_len = br_digest_size(dig);
71             }
72 1262           process_key(&hc.vtable, kc->ksi, key, key_len, 0x36);
73 1262           process_key(&hc.vtable, kc->kso, key, key_len, 0x5C);
74 1262           }
75              
76             /* see bearssl.h */
77             void
78 3094           br_hmac_init(br_hmac_context *ctx,
79             const br_hmac_key_context *kc, size_t out_len)
80             {
81             const br_hash_class *dig;
82             size_t blen, hlen;
83              
84 3094           dig = kc->dig_vtable;
85 3094           blen = block_size(dig);
86 3094           dig->init(&ctx->dig.vtable);
87 3094           dig->set_state(&ctx->dig.vtable, kc->ksi, (uint64_t)blen);
88 3094           memcpy(ctx->kso, kc->kso, sizeof kc->kso);
89 3094           hlen = br_digest_size(dig);
90 3094 50         if (out_len > 0 && out_len < hlen) {
    0          
91 0           hlen = out_len;
92             }
93 3094           ctx->out_len = hlen;
94 3094           }
95              
96             /* see bearssl.h */
97             void
98 3810           br_hmac_update(br_hmac_context *ctx, const void *data, size_t len)
99             {
100 3810           ctx->dig.vtable->update(&ctx->dig.vtable, data, len);
101 3810           }
102              
103             /* see bearssl.h */
104             size_t
105 3094           br_hmac_out(const br_hmac_context *ctx, void *out)
106             {
107             const br_hash_class *dig;
108             br_hash_compat_context hc;
109             unsigned char tmp[64];
110             size_t blen, hlen;
111              
112 3094           dig = ctx->dig.vtable;
113 3094           dig->out(&ctx->dig.vtable, tmp);
114 3094           blen = block_size(dig);
115 3094           dig->init(&hc.vtable);
116 3094           dig->set_state(&hc.vtable, ctx->kso, (uint64_t)blen);
117 3094           hlen = br_digest_size(dig);
118 3094           dig->update(&hc.vtable, tmp, hlen);
119 3094           dig->out(&hc.vtable, tmp);
120 3094           memcpy(out, tmp, ctx->out_len);
121 3094           return ctx->out_len;
122             }