1 /*-
2 * Copyright (c) 2003-2005 MAEKAWA Masahide <maekawa@cvsync.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 * 3. Neither the name of the author nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/types.h>
31
32 #include <stdlib.h>
33
34 #include "compat_stdbool.h"
35 #include "compat_stdint.h"
36 #include "compat_inttypes.h"
37
38 #include "hash.h"
39 #include "hash_native.h"
40
41 bool cvsync_MD5_init(void **);
42 void cvsync_MD5_update(void *, const void *, size_t);
43 void cvsync_MD5_final(void *, uint8_t *);
44 #if defined(HAVE_RIPEMD160)
45 bool cvsync_RIPEMD160_init(void **);
46 void cvsync_RIPEMD160_update(void *, const void *, size_t);
47 void cvsync_RIPEMD160_final(void *, uint8_t *);
48 #endif /* defined(HAVE_RIPEMD160) */
49 #if defined(HAVE_SHA1)
50 bool cvsync_SHA1_init(void **);
51 void cvsync_SHA1_update(void *, const void *, size_t);
52 void cvsync_SHA1_final(void *, uint8_t *);
53 #endif /* defined(HAVE_SHA1) */
54 void cvsync_native_destroy(void *);
55
56 const struct hash_args MD5_args = {
57 cvsync_MD5_init, cvsync_MD5_update, cvsync_MD5_final,
58 cvsync_native_destroy,
59 16
60 };
61
62 #if defined(HAVE_RIPEMD160)
63 const struct hash_args RIPEMD160_args = {
64 cvsync_RIPEMD160_init, cvsync_RIPEMD160_update, cvsync_RIPEMD160_final,
65 cvsync_native_destroy,
66 20
67 };
68 #endif /* defined(HAVE_RIPEMD160) */
69
70 #if defined(HAVE_SHA1)
71 const struct hash_args SHA1_args = {
72 cvsync_SHA1_init, cvsync_SHA1_update, cvsync_SHA1_final,
73 cvsync_native_destroy,
74 20
75 };
76 #endif /* defined(HAVE_SHA1) */
77
78 bool
cvsync_MD5_init(void ** ctx)79 cvsync_MD5_init(void **ctx)
80 {
81 MD5_CTX *md5ctx;
82
83 if ((md5ctx = malloc(sizeof(*md5ctx))) == NULL)
84 return (false);
85 MD5Init(md5ctx);
86
87 *ctx = md5ctx;
88
89 return (true);
90 }
91
92 void
cvsync_MD5_update(void * ctx,const void * buffer,size_t bufsize)93 cvsync_MD5_update(void *ctx, const void *buffer, size_t bufsize)
94 {
95 MD5Update(ctx, buffer, bufsize);
96 }
97
98 void
cvsync_MD5_final(void * ctx,uint8_t * buffer)99 cvsync_MD5_final(void *ctx, uint8_t *buffer)
100 {
101 MD5Final(buffer, ctx);
102 free(ctx);
103 }
104
105 #if defined(HAVE_RIPEMD160)
106 bool
cvsync_RIPEMD160_init(void ** ctx)107 cvsync_RIPEMD160_init(void **ctx)
108 {
109 RMD160_CTX *ripemd160ctx;
110
111 if ((ripemd160ctx = malloc(sizeof(*ripemd160ctx))) == NULL)
112 return (false);
113 RMD160Init(ripemd160ctx);
114
115 *ctx = ripemd160ctx;
116
117 return (true);
118 }
119
120 void
cvsync_RIPEMD160_update(void * ctx,const void * buffer,size_t bufsize)121 cvsync_RIPEMD160_update(void *ctx, const void *buffer, size_t bufsize)
122 {
123 RMD160Update(ctx, buffer, bufsize);
124 }
125
126 void
cvsync_RIPEMD160_final(void * ctx,uint8_t * buffer)127 cvsync_RIPEMD160_final(void *ctx, uint8_t *buffer)
128 {
129 RMD160Final(buffer, ctx);
130 free(ctx);
131 }
132 #endif /* defined(HAVE_SHA1) */
133
134 #if defined(HAVE_SHA1)
135 bool
cvsync_SHA1_init(void ** ctx)136 cvsync_SHA1_init(void **ctx)
137 {
138 SHA1_CTX *sha1ctx;
139
140 if ((sha1ctx = malloc(sizeof(*sha1ctx))) == NULL)
141 return (false);
142 SHA1Init(sha1ctx);
143
144 *ctx = sha1ctx;
145
146 return (true);
147 }
148
149 void
cvsync_SHA1_update(void * ctx,const void * buffer,size_t bufsize)150 cvsync_SHA1_update(void *ctx, const void *buffer, size_t bufsize)
151 {
152 SHA1Update(ctx, buffer, bufsize);
153 }
154
155 void
cvsync_SHA1_final(void * ctx,uint8_t * buffer)156 cvsync_SHA1_final(void *ctx, uint8_t *buffer)
157 {
158 SHA1Final(buffer, ctx);
159 free(ctx);
160 }
161 #endif /* defined(HAVE_SHA1) */
162
163 void
cvsync_native_destroy(void * ctx)164 cvsync_native_destroy(void *ctx)
165 {
166 free(ctx);
167 }
168