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
29 br_md5sha1_init(br_md5sha1_context *cc)
31 cc->vtable = &br_md5sha1_vtable;
32 memcpy(cc->val_md5, br_md5_IV, sizeof cc->val_md5);
33 memcpy(cc->val_sha1, br_sha1_IV, sizeof cc->val_sha1);
39 br_md5sha1_update(br_md5sha1_context *cc, const void *data, size_t len)
41 const unsigned char *buf;
45 ptr = (size_t)cc->count & 63;
53 memcpy(cc->buf + ptr, buf, clen);
57 cc->count += (uint64_t)clen;
59 br_md5_round(cc->buf, cc->val_md5);
60 br_sha1_round(cc->buf, cc->val_sha1);
68 br_md5sha1_out(const br_md5sha1_context *cc, void *dst)
70 unsigned char buf[64];
78 ptr = (size_t)count & 63;
79 memcpy(buf, cc->buf, ptr);
80 memcpy(val_md5, cc->val_md5, sizeof val_md5);
81 memcpy(val_sha1, cc->val_sha1, sizeof val_sha1);
84 memset(buf + ptr, 0, 64 - ptr);
85 br_md5_round(buf, val_md5);
86 br_sha1_round(buf, val_sha1);
89 memset(buf + ptr, 0, 56 - ptr);
92 br_enc64le(buf + 56, count);
93 br_md5_round(buf, val_md5);
94 br_enc64be(buf + 56, count);
95 br_sha1_round(buf, val_sha1);
97 br_range_enc32le(out, val_md5, 4);
98 br_range_enc32be(out + 16, val_sha1, 5);
103 br_md5sha1_state(const br_md5sha1_context *cc, void *dst)
108 br_range_enc32le(out, cc->val_md5, 4);
109 br_range_enc32be(out + 16, cc->val_sha1, 5);
115 br_md5sha1_set_state(br_md5sha1_context *cc, const void *stb, uint64_t count)
117 const unsigned char *buf;
120 br_range_dec32le(cc->val_md5, 4, buf);
121 br_range_dec32be(cc->val_sha1, 5, buf + 16);
126 const br_hash_class br_md5sha1_vtable = {
127 sizeof(br_md5sha1_context),
128 BR_HASHDESC_ID(br_md5sha1_ID)
129 | BR_HASHDESC_OUT(36)
130 | BR_HASHDESC_STATE(36)
131 | BR_HASHDESC_LBLEN(6),
132 (void (*)(const br_hash_class **))&br_md5sha1_init,
133 (void (*)(const br_hash_class **, const void *, size_t))
135 (void (*)(const br_hash_class *const *, void *))
137 (uint64_t (*)(const br_hash_class *const *, void *))
139 (void (*)(const br_hash_class **, const void *, uint64_t))
140 &br_md5sha1_set_state