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 #ifndef DIGEST_H_
26*ebfedea0SLionel Sambuc #define DIGEST_H_	20100108
27*ebfedea0SLionel Sambuc 
28*ebfedea0SLionel Sambuc #include <sys/types.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 #else
36*ebfedea0SLionel Sambuc # include <md5.h>
37*ebfedea0SLionel Sambuc # include <sha1.h>
38*ebfedea0SLionel Sambuc # include <sha2.h>
39*ebfedea0SLionel Sambuc # include <rmd160.h>
40*ebfedea0SLionel Sambuc # include <inttypes.h>
41*ebfedea0SLionel Sambuc #endif
42*ebfedea0SLionel Sambuc 
43*ebfedea0SLionel Sambuc #include "tiger.h"
44*ebfedea0SLionel Sambuc 
45*ebfedea0SLionel Sambuc #ifndef __BEGIN_DECLS
46*ebfedea0SLionel Sambuc #  if defined(__cplusplus)
47*ebfedea0SLionel Sambuc #  define __BEGIN_DECLS           extern "C" {
48*ebfedea0SLionel Sambuc #  define __END_DECLS             }
49*ebfedea0SLionel Sambuc #  else
50*ebfedea0SLionel Sambuc #  define __BEGIN_DECLS
51*ebfedea0SLionel Sambuc #  define __END_DECLS
52*ebfedea0SLionel Sambuc #  endif
53*ebfedea0SLionel Sambuc #endif
54*ebfedea0SLionel Sambuc 
55*ebfedea0SLionel Sambuc __BEGIN_DECLS
56*ebfedea0SLionel Sambuc 
57*ebfedea0SLionel Sambuc #define MD5_HASH_ALG		1
58*ebfedea0SLionel Sambuc #define SHA1_HASH_ALG		2
59*ebfedea0SLionel Sambuc #define RIPEMD_HASH_ALG		3
60*ebfedea0SLionel Sambuc #define TIGER_HASH_ALG		6	/* from rfc2440 */
61*ebfedea0SLionel Sambuc #define SHA256_HASH_ALG		8
62*ebfedea0SLionel Sambuc #define SHA384_HASH_ALG		9
63*ebfedea0SLionel Sambuc #define SHA512_HASH_ALG		10
64*ebfedea0SLionel Sambuc #define SHA224_HASH_ALG		11
65*ebfedea0SLionel Sambuc #define TIGER2_HASH_ALG		100	/* private/experimental from rfc4880 */
66*ebfedea0SLionel Sambuc 
67*ebfedea0SLionel Sambuc /* structure to describe digest methods */
68*ebfedea0SLionel Sambuc typedef struct digest_t {
69*ebfedea0SLionel Sambuc 	uint32_t		 alg;		/* algorithm */
70*ebfedea0SLionel Sambuc 	size_t			 size;		/* size */
71*ebfedea0SLionel Sambuc 	union {
72*ebfedea0SLionel Sambuc 		MD5_CTX		 md5ctx;	/* MD5 */
73*ebfedea0SLionel Sambuc 		SHA1_CTX	 sha1ctx;	/* SHA1 */
74*ebfedea0SLionel Sambuc 		RMD160_CTX	 rmd160ctx;	/* RIPEMD */
75*ebfedea0SLionel Sambuc 		SHA256_CTX	 sha256ctx;	/* SHA256 */
76*ebfedea0SLionel Sambuc 		SHA512_CTX	 sha512ctx;	/* SHA512 */
77*ebfedea0SLionel Sambuc 		TIGER_CTX	 tigerctx;	/* TIGER/TIGER2 */
78*ebfedea0SLionel Sambuc 	} u;
79*ebfedea0SLionel Sambuc 	void			*prefix;	/* points to specific prefix */
80*ebfedea0SLionel Sambuc 	uint32_t		 len;		/* prefix length */
81*ebfedea0SLionel Sambuc 	void			*ctx;		/* pointer to context array */
82*ebfedea0SLionel Sambuc } digest_t;
83*ebfedea0SLionel Sambuc 
84*ebfedea0SLionel Sambuc unsigned digest_get_alg(const char */*hashalg*/);
85*ebfedea0SLionel Sambuc 
86*ebfedea0SLionel Sambuc int digest_init(digest_t */*digest*/, const uint32_t /*hashalg*/);
87*ebfedea0SLionel Sambuc 
88*ebfedea0SLionel Sambuc int digest_update(digest_t */*digest*/, const uint8_t */*data*/, size_t /*size*/);
89*ebfedea0SLionel Sambuc unsigned digest_final(uint8_t */*out*/, digest_t */*digest*/);
90*ebfedea0SLionel Sambuc int digest_alg_size(unsigned /*alg*/);
91*ebfedea0SLionel Sambuc int digest_length(digest_t */*hash*/, unsigned /*hashedlen*/);
92*ebfedea0SLionel Sambuc 
93*ebfedea0SLionel Sambuc void MD5_Init(MD5_CTX */*context*/);
94*ebfedea0SLionel Sambuc void MD5_Update(MD5_CTX */*context*/, const unsigned char */*data*/, unsigned int /*len*/);
95*ebfedea0SLionel Sambuc void MD5_Final(unsigned char /*digest*/[16], MD5_CTX */*context*/);
96*ebfedea0SLionel Sambuc 
97*ebfedea0SLionel Sambuc void SHA1_Init(SHA1_CTX */*context*/);
98*ebfedea0SLionel Sambuc void SHA1_Update(SHA1_CTX */*context*/, const unsigned char */*data*/, unsigned int /*len*/);
99*ebfedea0SLionel Sambuc void SHA1_Final(unsigned char /*digest*/[20], SHA1_CTX */*context*/);
100*ebfedea0SLionel Sambuc 
101*ebfedea0SLionel Sambuc void RMD160_Init(RMD160_CTX */*ctx*/);
102*ebfedea0SLionel Sambuc void RMD160_Update(RMD160_CTX */*ctx*/, const unsigned char */*data*/, uint32_t /*len*/);
103*ebfedea0SLionel Sambuc void RMD160_Final(unsigned char /*digest*/[RMD160_DIGEST_LENGTH], RMD160_CTX */*ctx*/);
104*ebfedea0SLionel Sambuc 
105*ebfedea0SLionel Sambuc unsigned digest_get_prefix(unsigned /*hashalg*/, uint8_t */*prefix*/, size_t /*size*/);
106*ebfedea0SLionel Sambuc 
107*ebfedea0SLionel Sambuc __END_DECLS
108*ebfedea0SLionel Sambuc 
109*ebfedea0SLionel Sambuc #endif
110