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 <openssl/md5.h>
35 #include <openssl/ripemd.h>
36 #include <openssl/sha.h>
37 
38 #include "compat_stdbool.h"
39 #include "compat_stdint.h"
40 #include "compat_inttypes.h"
41 
42 #include "hash.h"
43 
44 bool cvsync_MD5_init(void **);
45 void cvsync_MD5_update(void *, const void *, size_t);
46 void cvsync_MD5_final(void *, uint8_t *);
47 bool cvsync_RIPEMD160_init(void **);
48 void cvsync_RIPEMD160_update(void *, const void *, size_t);
49 void cvsync_RIPEMD160_final(void *, uint8_t *);
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 void cvsync_openssl_destroy(void *);
54 
55 const struct hash_args MD5_args = {
56 	cvsync_MD5_init, cvsync_MD5_update, cvsync_MD5_final,
57 	cvsync_openssl_destroy,
58 	16
59 };
60 
61 const struct hash_args RIPEMD160_args = {
62 	cvsync_RIPEMD160_init, cvsync_RIPEMD160_update, cvsync_RIPEMD160_final,
63 	cvsync_openssl_destroy,
64 	20
65 };
66 
67 const struct hash_args SHA1_args = {
68 	cvsync_SHA1_init, cvsync_SHA1_update, cvsync_SHA1_final,
69 	cvsync_openssl_destroy,
70 	20
71 };
72 
73 bool
cvsync_MD5_init(void ** ctx)74 cvsync_MD5_init(void **ctx)
75 {
76 	MD5_CTX *md5ctx;
77 
78 	if ((md5ctx = malloc(sizeof(*md5ctx))) == NULL)
79 		return (false);
80 	MD5_Init(md5ctx);
81 
82 	*ctx = md5ctx;
83 
84 	return (true);
85 }
86 
87 void
cvsync_MD5_update(void * ctx,const void * buffer,size_t bufsize)88 cvsync_MD5_update(void *ctx, const void *buffer, size_t bufsize)
89 {
90 	MD5_Update(ctx, buffer, (unsigned long)bufsize);
91 }
92 
93 void
cvsync_MD5_final(void * ctx,uint8_t * buffer)94 cvsync_MD5_final(void *ctx, uint8_t *buffer)
95 {
96 	MD5_Final(buffer, ctx);
97 	free(ctx);
98 }
99 
100 bool
cvsync_RIPEMD160_init(void ** ctx)101 cvsync_RIPEMD160_init(void **ctx)
102 {
103 	RIPEMD160_CTX *ripemd160ctx;
104 
105 	if ((ripemd160ctx = malloc(sizeof(*ripemd160ctx))) == NULL)
106 		return (false);
107 	RIPEMD160_Init(ripemd160ctx);
108 
109 	*ctx = ripemd160ctx;
110 
111 	return (true);
112 }
113 
114 void
cvsync_RIPEMD160_update(void * ctx,const void * buffer,size_t bufsize)115 cvsync_RIPEMD160_update(void *ctx, const void *buffer, size_t bufsize)
116 {
117 	RIPEMD160_Update(ctx, buffer, (unsigned long)bufsize);
118 }
119 
120 void
cvsync_RIPEMD160_final(void * ctx,uint8_t * buffer)121 cvsync_RIPEMD160_final(void *ctx, uint8_t *buffer)
122 {
123 	RIPEMD160_Final(buffer, ctx);
124 	free(ctx);
125 }
126 
127 bool
cvsync_SHA1_init(void ** ctx)128 cvsync_SHA1_init(void **ctx)
129 {
130 	SHA_CTX *sha1ctx;
131 
132 	if ((sha1ctx = malloc(sizeof(*sha1ctx))) == NULL)
133 		return (false);
134 	SHA1_Init(sha1ctx);
135 
136 	*ctx = sha1ctx;
137 
138 	return (true);
139 }
140 
141 void
cvsync_SHA1_update(void * ctx,const void * buffer,size_t bufsize)142 cvsync_SHA1_update(void *ctx, const void *buffer, size_t bufsize)
143 {
144 	SHA1_Update(ctx, buffer, (unsigned long)bufsize);
145 }
146 
147 void
cvsync_SHA1_final(void * ctx,uint8_t * buffer)148 cvsync_SHA1_final(void *ctx, uint8_t *buffer)
149 {
150 	SHA1_Final(buffer, ctx);
151 	free(ctx);
152 }
153 
154 void
cvsync_openssl_destroy(void * ctx)155 cvsync_openssl_destroy(void *ctx)
156 {
157 	free(ctx);
158 }
159