1 /*-
2  * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #ifndef DIGEST_H_
26 #define DIGEST_H_	20100108
27 
28 #include <sys/types.h>
29 
30 #ifdef _KERNEL
31 # include <sys/md5.h>
32 # include <sys/sha1.h>
33 # include <sys/sha2.h>
34 # include <sys/rmd160.h>
35 #else
36 # include <md5.h>
37 # include <sha1.h>
38 # include <sha2.h>
39 # include <rmd160.h>
40 # include <inttypes.h>
41 #endif
42 
43 #include "tiger.h"
44 
45 #ifndef __BEGIN_DECLS
46 #  if defined(__cplusplus)
47 #  define __BEGIN_DECLS           extern "C" {
48 #  define __END_DECLS             }
49 #  else
50 #  define __BEGIN_DECLS
51 #  define __END_DECLS
52 #  endif
53 #endif
54 
55 __BEGIN_DECLS
56 
57 #define MD5_HASH_ALG		1
58 #define SHA1_HASH_ALG		2
59 #define RIPEMD_HASH_ALG		3
60 #define TIGER_HASH_ALG		6	/* from rfc2440 */
61 #define SHA256_HASH_ALG		8
62 #define SHA384_HASH_ALG		9
63 #define SHA512_HASH_ALG		10
64 #define SHA224_HASH_ALG		11
65 #define TIGER2_HASH_ALG		100	/* private/experimental from rfc4880 */
66 
67 /* structure to describe digest methods */
68 typedef struct digest_t {
69 	uint32_t		 alg;		/* algorithm */
70 	size_t			 size;		/* size */
71 	union {
72 		MD5_CTX		 md5ctx;	/* MD5 */
73 		SHA1_CTX	 sha1ctx;	/* SHA1 */
74 		RMD160_CTX	 rmd160ctx;	/* RIPEMD */
75 		SHA256_CTX	 sha256ctx;	/* SHA256 */
76 		SHA512_CTX	 sha512ctx;	/* SHA512 */
77 		TIGER_CTX	 tigerctx;	/* TIGER/TIGER2 */
78 	} u;
79 	void			*prefix;	/* points to specific prefix */
80 	uint32_t		 len;		/* prefix length */
81 	void			*ctx;		/* pointer to context array */
82 } digest_t;
83 
84 unsigned digest_get_alg(const char */*hashalg*/);
85 
86 int digest_init(digest_t */*digest*/, const uint32_t /*hashalg*/);
87 
88 int digest_update(digest_t */*digest*/, const uint8_t */*data*/, size_t /*size*/);
89 unsigned digest_final(uint8_t */*out*/, digest_t */*digest*/);
90 int digest_alg_size(unsigned /*alg*/);
91 int digest_length(digest_t */*hash*/, unsigned /*hashedlen*/);
92 
93 void MD5_Init(MD5_CTX */*context*/);
94 void MD5_Update(MD5_CTX */*context*/, const unsigned char */*data*/, unsigned int /*len*/);
95 void MD5_Final(unsigned char /*digest*/[16], MD5_CTX */*context*/);
96 
97 void SHA1_Init(SHA1_CTX */*context*/);
98 void SHA1_Update(SHA1_CTX */*context*/, const unsigned char */*data*/, unsigned int /*len*/);
99 void SHA1_Final(unsigned char /*digest*/[20], SHA1_CTX */*context*/);
100 
101 void RMD160_Init(RMD160_CTX */*ctx*/);
102 void RMD160_Update(RMD160_CTX */*ctx*/, const unsigned char */*data*/, uint32_t /*len*/);
103 void RMD160_Final(unsigned char /*digest*/[RMD160_DIGEST_LENGTH], RMD160_CTX */*ctx*/);
104 
105 unsigned digest_get_prefix(unsigned /*hashalg*/, uint8_t */*prefix*/, size_t /*size*/);
106 
107 __END_DECLS
108 
109 #endif
110