No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / otp / otp_md.c
blob5e2703be1925f4db3b18b1a0f8a9c669faa85312
1 /*
2 * Copyright (c) 1995 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 __RCSID("$Heimdal: otp_md.c 12048 2003-04-16 16:19:33Z lha $"
37 "$NetBSD$");
38 #endif
39 #include "otp_locl.h"
41 #include "otp_md.h"
42 #include "crypto-headers.h"
45 * Compress len bytes from md into key
48 static void
49 compressmd (OtpKey key, unsigned char *md, size_t len)
51 u_char *p = key;
53 memset (p, 0, OTPKEYSIZE);
54 while(len) {
55 *p++ ^= *md++;
56 *p++ ^= *md++;
57 *p++ ^= *md++;
58 *p++ ^= *md++;
59 len -= 4;
60 if (p == key + OTPKEYSIZE)
61 p = key;
65 #ifdef HAVE_OLD_HASH_NAMES
66 static void
67 otp_md4_final (void *res, struct md4 *m)
69 MD4_Final(res, m);
71 #undef MD4_Final
72 #define MD4_Final otp_md4_final
74 static void
75 otp_md5_final (void *res, struct md5 *m)
77 MD5_Final(res, m);
79 #undef MD5_Final
80 #define MD5_Final otp_md5_final
81 #endif
83 static int
84 otp_md_init (OtpKey key,
85 const char *pwd,
86 const char *seed,
87 void (*init)(void *),
88 void (*update)(void *, const void *, size_t),
89 void (*final)(void *, void *),
90 void *arg,
91 unsigned char *res,
92 size_t ressz)
94 char *p;
95 int len;
97 len = strlen(pwd) + strlen(seed);
98 p = malloc (len + 1);
99 if (p == NULL)
100 return -1;
101 strlcpy (p, seed, len + 1);
102 strlwr (p);
103 strlcat (p, pwd, len + 1);
104 (*init)(arg);
105 (*update)(arg, p, len);
106 (*final)(res, arg);
107 free (p);
108 compressmd (key, res, ressz);
109 return 0;
112 static int
113 otp_md_next (OtpKey key,
114 void (*init)(void *),
115 void (*update)(void *, const void *, size_t),
116 void (*final)(void *, void *),
117 void *arg,
118 unsigned char *res,
119 size_t ressz)
121 (*init)(arg);
122 (*update)(arg, key, OTPKEYSIZE);
123 (*final)(res, arg);
124 compressmd (key, res, ressz);
125 return 0;
128 static int
129 otp_md_hash (const char *data,
130 size_t len,
131 void (*init)(void *),
132 void (*update)(void *, const void *, size_t),
133 void (*final)(void *, void *),
134 void *arg,
135 unsigned char *res,
136 size_t ressz)
138 (*init)(arg);
139 (*update)(arg, data, len);
140 (*final)(res, arg);
141 return 0;
145 otp_md4_init (OtpKey key, const char *pwd, const char *seed)
147 unsigned char res[16];
148 MD4_CTX md4;
150 return otp_md_init (key, pwd, seed,
151 (void (*)(void *))MD4_Init,
152 (void (*)(void *, const void *, size_t))MD4_Update,
153 (void (*)(void *, void *))MD4_Final,
154 &md4, res, sizeof(res));
158 otp_md4_hash (const char *data,
159 size_t len,
160 unsigned char *res)
162 MD4_CTX md4;
164 return otp_md_hash (data, len,
165 (void (*)(void *))MD4_Init,
166 (void (*)(void *, const void *, size_t))MD4_Update,
167 (void (*)(void *, void *))MD4_Final,
168 &md4, res, 16);
172 otp_md4_next (OtpKey key)
174 unsigned char res[16];
175 MD4_CTX md4;
177 return otp_md_next (key,
178 (void (*)(void *))MD4_Init,
179 (void (*)(void *, const void *, size_t))MD4_Update,
180 (void (*)(void *, void *))MD4_Final,
181 &md4, res, sizeof(res));
186 otp_md5_init (OtpKey key, const char *pwd, const char *seed)
188 unsigned char res[16];
189 MD5_CTX md5;
191 return otp_md_init (key, pwd, seed,
192 (void (*)(void *))MD5_Init,
193 (void (*)(void *, const void *, size_t))MD5_Update,
194 (void (*)(void *, void *))MD5_Final,
195 &md5, res, sizeof(res));
199 otp_md5_hash (const char *data,
200 size_t len,
201 unsigned char *res)
203 MD5_CTX md5;
205 return otp_md_hash (data, len,
206 (void (*)(void *))MD5_Init,
207 (void (*)(void *, const void *, size_t))MD5_Update,
208 (void (*)(void *, void *))MD5_Final,
209 &md5, res, 16);
213 otp_md5_next (OtpKey key)
215 unsigned char res[16];
216 MD5_CTX md5;
218 return otp_md_next (key,
219 (void (*)(void *))MD5_Init,
220 (void (*)(void *, const void *, size_t))MD5_Update,
221 (void (*)(void *, void *))MD5_Final,
222 &md5, res, sizeof(res));
226 * For histerical reasons, in the OTP definition it's said that the
227 * result from SHA must be stored in little-endian order. See
228 * draft-ietf-otp-01.txt.
231 static void
232 SHA1_Final_little_endian (void *res, SHA_CTX *m)
234 unsigned char tmp[20];
235 unsigned char *p = res;
236 int j;
238 SHA1_Final (tmp, m);
239 for (j = 0; j < 20; j += 4) {
240 p[j] = tmp[j+3];
241 p[j+1] = tmp[j+2];
242 p[j+2] = tmp[j+1];
243 p[j+3] = tmp[j];
248 otp_sha_init (OtpKey key, const char *pwd, const char *seed)
250 unsigned char res[20];
251 SHA_CTX sha1;
253 return otp_md_init (key, pwd, seed,
254 (void (*)(void *))SHA1_Init,
255 (void (*)(void *, const void *, size_t))SHA1_Update,
256 (void (*)(void *, void *))SHA1_Final_little_endian,
257 &sha1, res, sizeof(res));
261 otp_sha_hash (const char *data,
262 size_t len,
263 unsigned char *res)
265 SHA_CTX sha1;
267 return otp_md_hash (data, len,
268 (void (*)(void *))SHA1_Init,
269 (void (*)(void *, const void *, size_t))SHA1_Update,
270 (void (*)(void *, void *))SHA1_Final_little_endian,
271 &sha1, res, 20);
275 otp_sha_next (OtpKey key)
277 unsigned char res[20];
278 SHA_CTX sha1;
280 return otp_md_next (key,
281 (void (*)(void *))SHA1_Init,
282 (void (*)(void *, const void *, size_t))SHA1_Update,
283 (void (*)(void *, void *))SHA1_Final_little_endian,
284 &sha1, res, sizeof(res));