xref: /freebsd/contrib/bearssl/src/hash/md5sha1.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 /* see bearssl.h */
28*0957b409SSimon J. Gerraty void
br_md5sha1_init(br_md5sha1_context * cc)29*0957b409SSimon J. Gerraty br_md5sha1_init(br_md5sha1_context *cc)
30*0957b409SSimon J. Gerraty {
31*0957b409SSimon J. Gerraty 	cc->vtable = &br_md5sha1_vtable;
32*0957b409SSimon J. Gerraty 	memcpy(cc->val_md5, br_md5_IV, sizeof cc->val_md5);
33*0957b409SSimon J. Gerraty 	memcpy(cc->val_sha1, br_sha1_IV, sizeof cc->val_sha1);
34*0957b409SSimon J. Gerraty 	cc->count = 0;
35*0957b409SSimon J. Gerraty }
36*0957b409SSimon J. Gerraty 
37*0957b409SSimon J. Gerraty /* see bearssl.h */
38*0957b409SSimon J. Gerraty void
br_md5sha1_update(br_md5sha1_context * cc,const void * data,size_t len)39*0957b409SSimon J. Gerraty br_md5sha1_update(br_md5sha1_context *cc, const void *data, size_t len)
40*0957b409SSimon J. Gerraty {
41*0957b409SSimon J. Gerraty 	const unsigned char *buf;
42*0957b409SSimon J. Gerraty 	size_t ptr;
43*0957b409SSimon J. Gerraty 
44*0957b409SSimon J. Gerraty 	buf = data;
45*0957b409SSimon J. Gerraty 	ptr = (size_t)cc->count & 63;
46*0957b409SSimon J. Gerraty 	while (len > 0) {
47*0957b409SSimon J. Gerraty 		size_t clen;
48*0957b409SSimon J. Gerraty 
49*0957b409SSimon J. Gerraty 		clen = 64 - ptr;
50*0957b409SSimon J. Gerraty 		if (clen > len) {
51*0957b409SSimon J. Gerraty 			clen = len;
52*0957b409SSimon J. Gerraty 		}
53*0957b409SSimon J. Gerraty 		memcpy(cc->buf + ptr, buf, clen);
54*0957b409SSimon J. Gerraty 		ptr += clen;
55*0957b409SSimon J. Gerraty 		buf += clen;
56*0957b409SSimon J. Gerraty 		len -= clen;
57*0957b409SSimon J. Gerraty 		cc->count += (uint64_t)clen;
58*0957b409SSimon J. Gerraty 		if (ptr == 64) {
59*0957b409SSimon J. Gerraty 			br_md5_round(cc->buf, cc->val_md5);
60*0957b409SSimon J. Gerraty 			br_sha1_round(cc->buf, cc->val_sha1);
61*0957b409SSimon J. Gerraty 			ptr = 0;
62*0957b409SSimon J. Gerraty 		}
63*0957b409SSimon J. Gerraty 	}
64*0957b409SSimon J. Gerraty }
65*0957b409SSimon J. Gerraty 
66*0957b409SSimon J. Gerraty /* see bearssl.h */
67*0957b409SSimon J. Gerraty void
br_md5sha1_out(const br_md5sha1_context * cc,void * dst)68*0957b409SSimon J. Gerraty br_md5sha1_out(const br_md5sha1_context *cc, void *dst)
69*0957b409SSimon J. Gerraty {
70*0957b409SSimon J. Gerraty 	unsigned char buf[64];
71*0957b409SSimon J. Gerraty 	uint32_t val_md5[4];
72*0957b409SSimon J. Gerraty 	uint32_t val_sha1[5];
73*0957b409SSimon J. Gerraty 	size_t ptr;
74*0957b409SSimon J. Gerraty 	unsigned char *out;
75*0957b409SSimon J. Gerraty 	uint64_t count;
76*0957b409SSimon J. Gerraty 
77*0957b409SSimon J. Gerraty 	count = cc->count;
78*0957b409SSimon J. Gerraty 	ptr = (size_t)count & 63;
79*0957b409SSimon J. Gerraty 	memcpy(buf, cc->buf, ptr);
80*0957b409SSimon J. Gerraty 	memcpy(val_md5, cc->val_md5, sizeof val_md5);
81*0957b409SSimon J. Gerraty 	memcpy(val_sha1, cc->val_sha1, sizeof val_sha1);
82*0957b409SSimon J. Gerraty 	buf[ptr ++] = 0x80;
83*0957b409SSimon J. Gerraty 	if (ptr > 56) {
84*0957b409SSimon J. Gerraty 		memset(buf + ptr, 0, 64 - ptr);
85*0957b409SSimon J. Gerraty 		br_md5_round(buf, val_md5);
86*0957b409SSimon J. Gerraty 		br_sha1_round(buf, val_sha1);
87*0957b409SSimon J. Gerraty 		memset(buf, 0, 56);
88*0957b409SSimon J. Gerraty 	} else {
89*0957b409SSimon J. Gerraty 		memset(buf + ptr, 0, 56 - ptr);
90*0957b409SSimon J. Gerraty 	}
91*0957b409SSimon J. Gerraty 	count <<= 3;
92*0957b409SSimon J. Gerraty 	br_enc64le(buf + 56, count);
93*0957b409SSimon J. Gerraty 	br_md5_round(buf, val_md5);
94*0957b409SSimon J. Gerraty 	br_enc64be(buf + 56, count);
95*0957b409SSimon J. Gerraty 	br_sha1_round(buf, val_sha1);
96*0957b409SSimon J. Gerraty 	out = dst;
97*0957b409SSimon J. Gerraty 	br_range_enc32le(out, val_md5, 4);
98*0957b409SSimon J. Gerraty 	br_range_enc32be(out + 16, val_sha1, 5);
99*0957b409SSimon J. Gerraty }
100*0957b409SSimon J. Gerraty 
101*0957b409SSimon J. Gerraty /* see bearssl.h */
102*0957b409SSimon J. Gerraty uint64_t
br_md5sha1_state(const br_md5sha1_context * cc,void * dst)103*0957b409SSimon J. Gerraty br_md5sha1_state(const br_md5sha1_context *cc, void *dst)
104*0957b409SSimon J. Gerraty {
105*0957b409SSimon J. Gerraty 	unsigned char *out;
106*0957b409SSimon J. Gerraty 
107*0957b409SSimon J. Gerraty 	out = dst;
108*0957b409SSimon J. Gerraty 	br_range_enc32le(out, cc->val_md5, 4);
109*0957b409SSimon J. Gerraty 	br_range_enc32be(out + 16, cc->val_sha1, 5);
110*0957b409SSimon J. Gerraty 	return cc->count;
111*0957b409SSimon J. Gerraty }
112*0957b409SSimon J. Gerraty 
113*0957b409SSimon J. Gerraty /* see bearssl.h */
114*0957b409SSimon J. Gerraty void
br_md5sha1_set_state(br_md5sha1_context * cc,const void * stb,uint64_t count)115*0957b409SSimon J. Gerraty br_md5sha1_set_state(br_md5sha1_context *cc, const void *stb, uint64_t count)
116*0957b409SSimon J. Gerraty {
117*0957b409SSimon J. Gerraty 	const unsigned char *buf;
118*0957b409SSimon J. Gerraty 
119*0957b409SSimon J. Gerraty 	buf = stb;
120*0957b409SSimon J. Gerraty 	br_range_dec32le(cc->val_md5, 4, buf);
121*0957b409SSimon J. Gerraty 	br_range_dec32be(cc->val_sha1, 5, buf + 16);
122*0957b409SSimon J. Gerraty 	cc->count = count;
123*0957b409SSimon J. Gerraty }
124*0957b409SSimon J. Gerraty 
125*0957b409SSimon J. Gerraty /* see bearssl.h */
126*0957b409SSimon J. Gerraty const br_hash_class br_md5sha1_vtable = {
127*0957b409SSimon J. Gerraty 	sizeof(br_md5sha1_context),
128*0957b409SSimon J. Gerraty 	BR_HASHDESC_ID(br_md5sha1_ID)
129*0957b409SSimon J. Gerraty 		| BR_HASHDESC_OUT(36)
130*0957b409SSimon J. Gerraty 		| BR_HASHDESC_STATE(36)
131*0957b409SSimon J. Gerraty 		| BR_HASHDESC_LBLEN(6),
132*0957b409SSimon J. Gerraty 	(void (*)(const br_hash_class **))&br_md5sha1_init,
133*0957b409SSimon J. Gerraty 	(void (*)(const br_hash_class **, const void *, size_t))
134*0957b409SSimon J. Gerraty 		&br_md5sha1_update,
135*0957b409SSimon J. Gerraty 	(void (*)(const br_hash_class *const *, void *))
136*0957b409SSimon J. Gerraty 		&br_md5sha1_out,
137*0957b409SSimon J. Gerraty 	(uint64_t (*)(const br_hash_class *const *, void *))
138*0957b409SSimon J. Gerraty 		&br_md5sha1_state,
139*0957b409SSimon J. Gerraty 	(void (*)(const br_hash_class **, const void *, uint64_t))
140*0957b409SSimon J. Gerraty 		&br_md5sha1_set_state
141*0957b409SSimon J. Gerraty };
142