2 * Copyright (c) 1995 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
36 RCSID("$Id: otp_md.c,v 1.18 2003/04/16 16:19:33 lha Exp $");
41 #include "crypto-headers.h"
44 * Compress len bytes from md into key
48 compressmd (OtpKey key, unsigned char *md, size_t len)
52 memset (p, 0, OTPKEYSIZE);
59 if (p == key + OTPKEYSIZE)
64 #ifdef HAVE_OLD_HASH_NAMES
66 otp_md4_final (void *res, struct md4 *m)
71 #define MD4_Final otp_md4_final
74 otp_md5_final (void *res, struct md5 *m)
79 #define MD5_Final otp_md5_final
83 otp_md_init (OtpKey key,
87 void (*update)(void *, const void *, size_t),
88 void (*final)(void *, void *),
96 len = strlen(pwd) + strlen(seed);
100 strlcpy (p, seed, len + 1);
102 strlcat (p, pwd, len + 1);
104 (*update)(arg, p, len);
107 compressmd (key, res, ressz);
112 otp_md_next (OtpKey key,
113 void (*init)(void *),
114 void (*update)(void *, const void *, size_t),
115 void (*final)(void *, void *),
121 (*update)(arg, key, OTPKEYSIZE);
123 compressmd (key, res, ressz);
128 otp_md_hash (const char *data,
130 void (*init)(void *),
131 void (*update)(void *, const void *, size_t),
132 void (*final)(void *, void *),
138 (*update)(arg, data, len);
144 otp_md4_init (OtpKey key, const char *pwd, const char *seed)
146 unsigned char res[16];
149 return otp_md_init (key, pwd, seed,
150 (void (*)(void *))MD4_Init,
151 (void (*)(void *, const void *, size_t))MD4_Update,
152 (void (*)(void *, void *))MD4_Final,
153 &md4, res, sizeof(res));
157 otp_md4_hash (const char *data,
163 return otp_md_hash (data, len,
164 (void (*)(void *))MD4_Init,
165 (void (*)(void *, const void *, size_t))MD4_Update,
166 (void (*)(void *, void *))MD4_Final,
171 otp_md4_next (OtpKey key)
173 unsigned char res[16];
176 return otp_md_next (key,
177 (void (*)(void *))MD4_Init,
178 (void (*)(void *, const void *, size_t))MD4_Update,
179 (void (*)(void *, void *))MD4_Final,
180 &md4, res, sizeof(res));
185 otp_md5_init (OtpKey key, const char *pwd, const char *seed)
187 unsigned char res[16];
190 return otp_md_init (key, pwd, seed,
191 (void (*)(void *))MD5_Init,
192 (void (*)(void *, const void *, size_t))MD5_Update,
193 (void (*)(void *, void *))MD5_Final,
194 &md5, res, sizeof(res));
198 otp_md5_hash (const char *data,
204 return otp_md_hash (data, len,
205 (void (*)(void *))MD5_Init,
206 (void (*)(void *, const void *, size_t))MD5_Update,
207 (void (*)(void *, void *))MD5_Final,
212 otp_md5_next (OtpKey key)
214 unsigned char res[16];
217 return otp_md_next (key,
218 (void (*)(void *))MD5_Init,
219 (void (*)(void *, const void *, size_t))MD5_Update,
220 (void (*)(void *, void *))MD5_Final,
221 &md5, res, sizeof(res));
225 * For histerical reasons, in the OTP definition it's said that the
226 * result from SHA must be stored in little-endian order. See
227 * draft-ietf-otp-01.txt.
231 SHA1_Final_little_endian (void *res, SHA_CTX *m)
233 unsigned char tmp[20];
234 unsigned char *p = res;
238 for (j = 0; j < 20; j += 4) {
247 otp_sha_init (OtpKey key, const char *pwd, const char *seed)
249 unsigned char res[20];
252 return otp_md_init (key, pwd, seed,
253 (void (*)(void *))SHA1_Init,
254 (void (*)(void *, const void *, size_t))SHA1_Update,
255 (void (*)(void *, void *))SHA1_Final_little_endian,
256 &sha1, res, sizeof(res));
260 otp_sha_hash (const char *data,
266 return otp_md_hash (data, len,
267 (void (*)(void *))SHA1_Init,
268 (void (*)(void *, const void *, size_t))SHA1_Update,
269 (void (*)(void *, void *))SHA1_Final_little_endian,
274 otp_sha_next (OtpKey key)
276 unsigned char res[20];
279 return otp_md_next (key,
280 (void (*)(void *))SHA1_Init,
281 (void (*)(void *, const void *, size_t))SHA1_Update,
282 (void (*)(void *, void *))SHA1_Final_little_endian,
283 &sha1, res, sizeof(res));