1*ebfedea0SLionel Sambuc /*-
2*ebfedea0SLionel Sambuc * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
3*ebfedea0SLionel Sambuc * All rights reserved.
4*ebfedea0SLionel Sambuc *
5*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
6*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
7*ebfedea0SLionel Sambuc * are met:
8*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
9*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
10*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
11*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
12*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
13*ebfedea0SLionel Sambuc *
14*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15*ebfedea0SLionel Sambuc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*ebfedea0SLionel Sambuc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*ebfedea0SLionel Sambuc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18*ebfedea0SLionel Sambuc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*ebfedea0SLionel Sambuc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*ebfedea0SLionel Sambuc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*ebfedea0SLionel Sambuc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*ebfedea0SLionel Sambuc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*ebfedea0SLionel Sambuc */
25*ebfedea0SLionel Sambuc #include <sys/types.h>
26*ebfedea0SLionel Sambuc #include <sys/stat.h>
27*ebfedea0SLionel Sambuc #include <sys/param.h>
28*ebfedea0SLionel Sambuc #include <sys/syslog.h>
29*ebfedea0SLionel Sambuc
30*ebfedea0SLionel Sambuc #ifdef _KERNEL
31*ebfedea0SLionel Sambuc # include <sys/md5.h>
32*ebfedea0SLionel Sambuc # include <sys/sha1.h>
33*ebfedea0SLionel Sambuc # include <sys/sha2.h>
34*ebfedea0SLionel Sambuc # include <sys/rmd160.h>
35*ebfedea0SLionel Sambuc # include <sys/kmem.h>
36*ebfedea0SLionel Sambuc #else
37*ebfedea0SLionel Sambuc # include <arpa/inet.h>
38*ebfedea0SLionel Sambuc # include <ctype.h>
39*ebfedea0SLionel Sambuc # include <inttypes.h>
40*ebfedea0SLionel Sambuc # include <md5.h>
41*ebfedea0SLionel Sambuc # include <rmd160.h>
42*ebfedea0SLionel Sambuc # include <sha1.h>
43*ebfedea0SLionel Sambuc # include <sha2.h>
44*ebfedea0SLionel Sambuc # include <stdarg.h>
45*ebfedea0SLionel Sambuc # include <stdio.h>
46*ebfedea0SLionel Sambuc # include <stdlib.h>
47*ebfedea0SLionel Sambuc # include <string.h>
48*ebfedea0SLionel Sambuc # include <time.h>
49*ebfedea0SLionel Sambuc # include <unistd.h>
50*ebfedea0SLionel Sambuc #endif
51*ebfedea0SLionel Sambuc
52*ebfedea0SLionel Sambuc #include "digest.h"
53*ebfedea0SLionel Sambuc
54*ebfedea0SLionel Sambuc static uint8_t prefix_md5[] = {
55*ebfedea0SLionel Sambuc 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
56*ebfedea0SLionel Sambuc 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10
57*ebfedea0SLionel Sambuc };
58*ebfedea0SLionel Sambuc
59*ebfedea0SLionel Sambuc static uint8_t prefix_sha1[] = {
60*ebfedea0SLionel Sambuc 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0E, 0x03, 0x02,
61*ebfedea0SLionel Sambuc 0x1A, 0x05, 0x00, 0x04, 0x14
62*ebfedea0SLionel Sambuc };
63*ebfedea0SLionel Sambuc
64*ebfedea0SLionel Sambuc static uint8_t prefix_sha256[] = {
65*ebfedea0SLionel Sambuc 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
66*ebfedea0SLionel Sambuc 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20
67*ebfedea0SLionel Sambuc };
68*ebfedea0SLionel Sambuc
69*ebfedea0SLionel Sambuc static uint64_t prefix_tiger[] = {
70*ebfedea0SLionel Sambuc 0x0123456789ABCDEFLL,
71*ebfedea0SLionel Sambuc 0xFEDCBA9876543210LL,
72*ebfedea0SLionel Sambuc 0xF096A5B4C3B2E187LL
73*ebfedea0SLionel Sambuc };
74*ebfedea0SLionel Sambuc
75*ebfedea0SLionel Sambuc static uint8_t prefix_rmd160[] = {
76*ebfedea0SLionel Sambuc 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24,
77*ebfedea0SLionel Sambuc 0x03, 0x02, 0x01, 0x05, 0x00, 0x04, 0x14
78*ebfedea0SLionel Sambuc };
79*ebfedea0SLionel Sambuc
80*ebfedea0SLionel Sambuc static uint8_t prefix_sha512[] = {
81*ebfedea0SLionel Sambuc 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
82*ebfedea0SLionel Sambuc 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40
83*ebfedea0SLionel Sambuc };
84*ebfedea0SLionel Sambuc
85*ebfedea0SLionel Sambuc #define V4_SIGNATURE 4
86*ebfedea0SLionel Sambuc
87*ebfedea0SLionel Sambuc /*************************************************************************/
88*ebfedea0SLionel Sambuc
89*ebfedea0SLionel Sambuc void
MD5_Init(MD5_CTX * context)90*ebfedea0SLionel Sambuc MD5_Init(MD5_CTX *context)
91*ebfedea0SLionel Sambuc {
92*ebfedea0SLionel Sambuc if (context) {
93*ebfedea0SLionel Sambuc MD5Init(context);
94*ebfedea0SLionel Sambuc }
95*ebfedea0SLionel Sambuc }
96*ebfedea0SLionel Sambuc
97*ebfedea0SLionel Sambuc void
MD5_Update(MD5_CTX * context,const unsigned char * data,unsigned int len)98*ebfedea0SLionel Sambuc MD5_Update(MD5_CTX *context, const unsigned char *data, unsigned int len)
99*ebfedea0SLionel Sambuc {
100*ebfedea0SLionel Sambuc if (context && data) {
101*ebfedea0SLionel Sambuc MD5Update(context, data, len);
102*ebfedea0SLionel Sambuc }
103*ebfedea0SLionel Sambuc }
104*ebfedea0SLionel Sambuc
105*ebfedea0SLionel Sambuc void
MD5_Final(unsigned char digest[16],MD5_CTX * context)106*ebfedea0SLionel Sambuc MD5_Final(unsigned char digest[16], MD5_CTX *context)
107*ebfedea0SLionel Sambuc {
108*ebfedea0SLionel Sambuc if (digest && context) {
109*ebfedea0SLionel Sambuc MD5Final(digest, context);
110*ebfedea0SLionel Sambuc }
111*ebfedea0SLionel Sambuc }
112*ebfedea0SLionel Sambuc
113*ebfedea0SLionel Sambuc void
SHA1_Init(SHA1_CTX * context)114*ebfedea0SLionel Sambuc SHA1_Init(SHA1_CTX *context)
115*ebfedea0SLionel Sambuc {
116*ebfedea0SLionel Sambuc if (context) {
117*ebfedea0SLionel Sambuc SHA1Init(context);
118*ebfedea0SLionel Sambuc }
119*ebfedea0SLionel Sambuc }
120*ebfedea0SLionel Sambuc
121*ebfedea0SLionel Sambuc void
SHA1_Update(SHA1_CTX * context,const unsigned char * data,unsigned int len)122*ebfedea0SLionel Sambuc SHA1_Update(SHA1_CTX *context, const unsigned char *data, unsigned int len)
123*ebfedea0SLionel Sambuc {
124*ebfedea0SLionel Sambuc if (context && data) {
125*ebfedea0SLionel Sambuc SHA1Update(context, data, len);
126*ebfedea0SLionel Sambuc }
127*ebfedea0SLionel Sambuc }
128*ebfedea0SLionel Sambuc
129*ebfedea0SLionel Sambuc void
SHA1_Final(unsigned char digest[20],SHA1_CTX * context)130*ebfedea0SLionel Sambuc SHA1_Final(unsigned char digest[20], SHA1_CTX *context)
131*ebfedea0SLionel Sambuc {
132*ebfedea0SLionel Sambuc if (digest && context) {
133*ebfedea0SLionel Sambuc SHA1Final(digest, context);
134*ebfedea0SLionel Sambuc }
135*ebfedea0SLionel Sambuc }
136*ebfedea0SLionel Sambuc
137*ebfedea0SLionel Sambuc void
RMD160_Init(RMD160_CTX * context)138*ebfedea0SLionel Sambuc RMD160_Init(RMD160_CTX *context)
139*ebfedea0SLionel Sambuc {
140*ebfedea0SLionel Sambuc if (context) {
141*ebfedea0SLionel Sambuc RMD160Init(context);
142*ebfedea0SLionel Sambuc }
143*ebfedea0SLionel Sambuc }
144*ebfedea0SLionel Sambuc
145*ebfedea0SLionel Sambuc void
RMD160_Update(RMD160_CTX * context,const unsigned char * data,unsigned int len)146*ebfedea0SLionel Sambuc RMD160_Update(RMD160_CTX *context, const unsigned char *data, unsigned int len)
147*ebfedea0SLionel Sambuc {
148*ebfedea0SLionel Sambuc if (context && data) {
149*ebfedea0SLionel Sambuc RMD160Update(context, data, len);
150*ebfedea0SLionel Sambuc }
151*ebfedea0SLionel Sambuc }
152*ebfedea0SLionel Sambuc
153*ebfedea0SLionel Sambuc void
RMD160_Final(unsigned char digest[20],RMD160_CTX * context)154*ebfedea0SLionel Sambuc RMD160_Final(unsigned char digest[20], RMD160_CTX *context)
155*ebfedea0SLionel Sambuc {
156*ebfedea0SLionel Sambuc if (context && digest) {
157*ebfedea0SLionel Sambuc RMD160Final(digest, context);
158*ebfedea0SLionel Sambuc }
159*ebfedea0SLionel Sambuc }
160*ebfedea0SLionel Sambuc
161*ebfedea0SLionel Sambuc
162*ebfedea0SLionel Sambuc /* algorithm size (raw) */
163*ebfedea0SLionel Sambuc int
digest_alg_size(unsigned alg)164*ebfedea0SLionel Sambuc digest_alg_size(unsigned alg)
165*ebfedea0SLionel Sambuc {
166*ebfedea0SLionel Sambuc switch(alg) {
167*ebfedea0SLionel Sambuc case MD5_HASH_ALG:
168*ebfedea0SLionel Sambuc return 16;
169*ebfedea0SLionel Sambuc case SHA1_HASH_ALG:
170*ebfedea0SLionel Sambuc return 20;
171*ebfedea0SLionel Sambuc case RIPEMD_HASH_ALG:
172*ebfedea0SLionel Sambuc return RMD160_DIGEST_LENGTH;
173*ebfedea0SLionel Sambuc case SHA256_HASH_ALG:
174*ebfedea0SLionel Sambuc return 32;
175*ebfedea0SLionel Sambuc case SHA512_HASH_ALG:
176*ebfedea0SLionel Sambuc return 64;
177*ebfedea0SLionel Sambuc case TIGER_HASH_ALG:
178*ebfedea0SLionel Sambuc case TIGER2_HASH_ALG:
179*ebfedea0SLionel Sambuc return TIGER_DIGEST_LENGTH;
180*ebfedea0SLionel Sambuc default:
181*ebfedea0SLionel Sambuc printf("hash_any: bad algorithm\n");
182*ebfedea0SLionel Sambuc return 0;
183*ebfedea0SLionel Sambuc }
184*ebfedea0SLionel Sambuc }
185*ebfedea0SLionel Sambuc
186*ebfedea0SLionel Sambuc /* initialise the hash structure */
187*ebfedea0SLionel Sambuc int
digest_init(digest_t * hash,const uint32_t hashalg)188*ebfedea0SLionel Sambuc digest_init(digest_t *hash, const uint32_t hashalg)
189*ebfedea0SLionel Sambuc {
190*ebfedea0SLionel Sambuc if (hash == NULL) {
191*ebfedea0SLionel Sambuc return 0;
192*ebfedea0SLionel Sambuc }
193*ebfedea0SLionel Sambuc switch(hash->alg = hashalg) {
194*ebfedea0SLionel Sambuc case MD5_HASH_ALG:
195*ebfedea0SLionel Sambuc MD5Init(&hash->u.md5ctx);
196*ebfedea0SLionel Sambuc hash->size = 16;
197*ebfedea0SLionel Sambuc hash->prefix = prefix_md5;
198*ebfedea0SLionel Sambuc hash->len = sizeof(prefix_md5);
199*ebfedea0SLionel Sambuc hash->ctx = &hash->u.md5ctx;
200*ebfedea0SLionel Sambuc return 1;
201*ebfedea0SLionel Sambuc case SHA1_HASH_ALG:
202*ebfedea0SLionel Sambuc SHA1Init(&hash->u.sha1ctx);
203*ebfedea0SLionel Sambuc hash->size = 20;
204*ebfedea0SLionel Sambuc hash->prefix = prefix_sha1;
205*ebfedea0SLionel Sambuc hash->len = sizeof(prefix_sha1);
206*ebfedea0SLionel Sambuc hash->ctx = &hash->u.sha1ctx;
207*ebfedea0SLionel Sambuc return 1;
208*ebfedea0SLionel Sambuc case RIPEMD_HASH_ALG:
209*ebfedea0SLionel Sambuc RMD160Init(&hash->u.rmd160ctx);
210*ebfedea0SLionel Sambuc hash->size = 20;
211*ebfedea0SLionel Sambuc hash->prefix = prefix_rmd160;
212*ebfedea0SLionel Sambuc hash->len = sizeof(prefix_rmd160);
213*ebfedea0SLionel Sambuc hash->ctx = &hash->u.rmd160ctx;
214*ebfedea0SLionel Sambuc return 1;
215*ebfedea0SLionel Sambuc case SHA256_HASH_ALG:
216*ebfedea0SLionel Sambuc SHA256_Init(&hash->u.sha256ctx);
217*ebfedea0SLionel Sambuc hash->size = 32;
218*ebfedea0SLionel Sambuc hash->prefix = prefix_sha256;
219*ebfedea0SLionel Sambuc hash->len = sizeof(prefix_sha256);
220*ebfedea0SLionel Sambuc hash->ctx = &hash->u.sha256ctx;
221*ebfedea0SLionel Sambuc return 1;
222*ebfedea0SLionel Sambuc case SHA512_HASH_ALG:
223*ebfedea0SLionel Sambuc SHA512_Init(&hash->u.sha512ctx);
224*ebfedea0SLionel Sambuc hash->size = 64;
225*ebfedea0SLionel Sambuc hash->prefix = prefix_sha512;
226*ebfedea0SLionel Sambuc hash->len = sizeof(prefix_sha512);
227*ebfedea0SLionel Sambuc hash->ctx = &hash->u.sha512ctx;
228*ebfedea0SLionel Sambuc return 1;
229*ebfedea0SLionel Sambuc case TIGER_HASH_ALG:
230*ebfedea0SLionel Sambuc TIGER_Init(&hash->u.tigerctx);
231*ebfedea0SLionel Sambuc hash->size = TIGER_DIGEST_LENGTH;
232*ebfedea0SLionel Sambuc hash->prefix = prefix_tiger;
233*ebfedea0SLionel Sambuc hash->len = sizeof(prefix_tiger);
234*ebfedea0SLionel Sambuc hash->ctx = &hash->u.tigerctx;
235*ebfedea0SLionel Sambuc return 1;
236*ebfedea0SLionel Sambuc case TIGER2_HASH_ALG:
237*ebfedea0SLionel Sambuc TIGER2_Init(&hash->u.tigerctx);
238*ebfedea0SLionel Sambuc hash->size = TIGER_DIGEST_LENGTH;
239*ebfedea0SLionel Sambuc hash->prefix = prefix_tiger;
240*ebfedea0SLionel Sambuc hash->len = sizeof(prefix_tiger);
241*ebfedea0SLionel Sambuc hash->ctx = &hash->u.tigerctx;
242*ebfedea0SLionel Sambuc return 1;
243*ebfedea0SLionel Sambuc default:
244*ebfedea0SLionel Sambuc printf("hash_any: bad algorithm\n");
245*ebfedea0SLionel Sambuc return 0;
246*ebfedea0SLionel Sambuc }
247*ebfedea0SLionel Sambuc }
248*ebfedea0SLionel Sambuc
249*ebfedea0SLionel Sambuc typedef struct rec_t {
250*ebfedea0SLionel Sambuc const char *s;
251*ebfedea0SLionel Sambuc const unsigned alg;
252*ebfedea0SLionel Sambuc } rec_t;
253*ebfedea0SLionel Sambuc
254*ebfedea0SLionel Sambuc static rec_t hashalgs[] = {
255*ebfedea0SLionel Sambuc { "md5", MD5_HASH_ALG },
256*ebfedea0SLionel Sambuc { "sha1", SHA1_HASH_ALG },
257*ebfedea0SLionel Sambuc { "ripemd", RIPEMD_HASH_ALG },
258*ebfedea0SLionel Sambuc { "sha256", SHA256_HASH_ALG },
259*ebfedea0SLionel Sambuc { "sha512", SHA512_HASH_ALG },
260*ebfedea0SLionel Sambuc { "tiger", TIGER_HASH_ALG },
261*ebfedea0SLionel Sambuc { "tiger2", TIGER2_HASH_ALG },
262*ebfedea0SLionel Sambuc { NULL, 0 }
263*ebfedea0SLionel Sambuc };
264*ebfedea0SLionel Sambuc
265*ebfedea0SLionel Sambuc /* initialise by string alg name */
266*ebfedea0SLionel Sambuc unsigned
digest_get_alg(const char * hashalg)267*ebfedea0SLionel Sambuc digest_get_alg(const char *hashalg)
268*ebfedea0SLionel Sambuc {
269*ebfedea0SLionel Sambuc rec_t *r;
270*ebfedea0SLionel Sambuc
271*ebfedea0SLionel Sambuc for (r = hashalgs ; hashalg && r->s ; r++) {
272*ebfedea0SLionel Sambuc if (strcasecmp(r->s, hashalg) == 0) {
273*ebfedea0SLionel Sambuc return r->alg;
274*ebfedea0SLionel Sambuc }
275*ebfedea0SLionel Sambuc }
276*ebfedea0SLionel Sambuc return 0;
277*ebfedea0SLionel Sambuc }
278*ebfedea0SLionel Sambuc
279*ebfedea0SLionel Sambuc int
digest_update(digest_t * hash,const uint8_t * data,size_t length)280*ebfedea0SLionel Sambuc digest_update(digest_t *hash, const uint8_t *data, size_t length)
281*ebfedea0SLionel Sambuc {
282*ebfedea0SLionel Sambuc if (hash == NULL || data == NULL) {
283*ebfedea0SLionel Sambuc return 0;
284*ebfedea0SLionel Sambuc }
285*ebfedea0SLionel Sambuc switch(hash->alg) {
286*ebfedea0SLionel Sambuc case MD5_HASH_ALG:
287*ebfedea0SLionel Sambuc MD5Update(hash->ctx, data, (unsigned)length);
288*ebfedea0SLionel Sambuc return 1;
289*ebfedea0SLionel Sambuc case SHA1_HASH_ALG:
290*ebfedea0SLionel Sambuc SHA1Update(hash->ctx, data, (unsigned)length);
291*ebfedea0SLionel Sambuc return 1;
292*ebfedea0SLionel Sambuc case RIPEMD_HASH_ALG:
293*ebfedea0SLionel Sambuc RMD160Update(hash->ctx, data, (unsigned)length);
294*ebfedea0SLionel Sambuc return 1;
295*ebfedea0SLionel Sambuc case SHA256_HASH_ALG:
296*ebfedea0SLionel Sambuc SHA256_Update(hash->ctx, data, length);
297*ebfedea0SLionel Sambuc return 1;
298*ebfedea0SLionel Sambuc case SHA512_HASH_ALG:
299*ebfedea0SLionel Sambuc SHA512_Update(hash->ctx, data, length);
300*ebfedea0SLionel Sambuc return 1;
301*ebfedea0SLionel Sambuc case TIGER_HASH_ALG:
302*ebfedea0SLionel Sambuc case TIGER2_HASH_ALG:
303*ebfedea0SLionel Sambuc TIGER_Update(hash->ctx, data, length);
304*ebfedea0SLionel Sambuc return 1;
305*ebfedea0SLionel Sambuc default:
306*ebfedea0SLionel Sambuc printf("hash_any: bad algorithm\n");
307*ebfedea0SLionel Sambuc return 0;
308*ebfedea0SLionel Sambuc }
309*ebfedea0SLionel Sambuc }
310*ebfedea0SLionel Sambuc
311*ebfedea0SLionel Sambuc unsigned
digest_final(uint8_t * out,digest_t * hash)312*ebfedea0SLionel Sambuc digest_final(uint8_t *out, digest_t *hash)
313*ebfedea0SLionel Sambuc {
314*ebfedea0SLionel Sambuc if (hash == NULL || out == NULL) {
315*ebfedea0SLionel Sambuc return 0;
316*ebfedea0SLionel Sambuc }
317*ebfedea0SLionel Sambuc switch(hash->alg) {
318*ebfedea0SLionel Sambuc case MD5_HASH_ALG:
319*ebfedea0SLionel Sambuc MD5Final(out, hash->ctx);
320*ebfedea0SLionel Sambuc break;
321*ebfedea0SLionel Sambuc case SHA1_HASH_ALG:
322*ebfedea0SLionel Sambuc SHA1Final(out, hash->ctx);
323*ebfedea0SLionel Sambuc break;
324*ebfedea0SLionel Sambuc case RIPEMD_HASH_ALG:
325*ebfedea0SLionel Sambuc RMD160Final(out, hash->ctx);
326*ebfedea0SLionel Sambuc break;
327*ebfedea0SLionel Sambuc case SHA256_HASH_ALG:
328*ebfedea0SLionel Sambuc SHA256_Final(out, hash->ctx);
329*ebfedea0SLionel Sambuc break;
330*ebfedea0SLionel Sambuc case SHA512_HASH_ALG:
331*ebfedea0SLionel Sambuc SHA512_Final(out, hash->ctx);
332*ebfedea0SLionel Sambuc break;
333*ebfedea0SLionel Sambuc case TIGER_HASH_ALG:
334*ebfedea0SLionel Sambuc TIGER_Final(out, hash->ctx);
335*ebfedea0SLionel Sambuc break;
336*ebfedea0SLionel Sambuc default:
337*ebfedea0SLionel Sambuc printf("hash_any: bad algorithm\n");
338*ebfedea0SLionel Sambuc return 0;
339*ebfedea0SLionel Sambuc }
340*ebfedea0SLionel Sambuc (void) memset(hash->ctx, 0x0, hash->size);
341*ebfedea0SLionel Sambuc return (unsigned)hash->size;
342*ebfedea0SLionel Sambuc }
343*ebfedea0SLionel Sambuc
344*ebfedea0SLionel Sambuc int
digest_length(digest_t * hash,unsigned hashedlen)345*ebfedea0SLionel Sambuc digest_length(digest_t *hash, unsigned hashedlen)
346*ebfedea0SLionel Sambuc {
347*ebfedea0SLionel Sambuc uint8_t trailer[6];
348*ebfedea0SLionel Sambuc
349*ebfedea0SLionel Sambuc if (hash == NULL) {
350*ebfedea0SLionel Sambuc return 0;
351*ebfedea0SLionel Sambuc }
352*ebfedea0SLionel Sambuc trailer[0] = V4_SIGNATURE;
353*ebfedea0SLionel Sambuc trailer[1] = 0xFF;
354*ebfedea0SLionel Sambuc trailer[2] = (uint8_t)((hashedlen >> 24) & 0xff);
355*ebfedea0SLionel Sambuc trailer[3] = (uint8_t)((hashedlen >> 16) & 0xff);
356*ebfedea0SLionel Sambuc trailer[4] = (uint8_t)((hashedlen >> 8) & 0xff);
357*ebfedea0SLionel Sambuc trailer[5] = (uint8_t)(hashedlen & 0xff);
358*ebfedea0SLionel Sambuc digest_update(hash, trailer, sizeof(trailer));
359*ebfedea0SLionel Sambuc return 1;
360*ebfedea0SLionel Sambuc }
361*ebfedea0SLionel Sambuc
362*ebfedea0SLionel Sambuc unsigned
digest_get_prefix(unsigned hashalg,uint8_t * prefix,size_t size)363*ebfedea0SLionel Sambuc digest_get_prefix(unsigned hashalg, uint8_t *prefix, size_t size)
364*ebfedea0SLionel Sambuc {
365*ebfedea0SLionel Sambuc if (prefix == NULL) {
366*ebfedea0SLionel Sambuc return 0;
367*ebfedea0SLionel Sambuc }
368*ebfedea0SLionel Sambuc switch (hashalg) {
369*ebfedea0SLionel Sambuc case MD5_HASH_ALG:
370*ebfedea0SLionel Sambuc memcpy(prefix, prefix_md5, sizeof(prefix_md5));
371*ebfedea0SLionel Sambuc return sizeof(prefix_md5);
372*ebfedea0SLionel Sambuc case SHA1_HASH_ALG:
373*ebfedea0SLionel Sambuc memcpy(prefix, prefix_sha1, sizeof(prefix_sha1));
374*ebfedea0SLionel Sambuc return sizeof(prefix_sha1);
375*ebfedea0SLionel Sambuc case SHA256_HASH_ALG:
376*ebfedea0SLionel Sambuc memcpy(prefix, prefix_sha256, sizeof(prefix_sha256));
377*ebfedea0SLionel Sambuc return sizeof(prefix_sha256);
378*ebfedea0SLionel Sambuc default:
379*ebfedea0SLionel Sambuc printf("digest_get_prefix: unknown hash algorithm: %d\n", hashalg);
380*ebfedea0SLionel Sambuc return 0;
381*ebfedea0SLionel Sambuc }
382*ebfedea0SLionel Sambuc }
383*ebfedea0SLionel Sambuc
384