2 * Copyright (c) 1995 - 2001 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.14 2001/01/29 05:55:18 assar Exp $");
41 #ifdef HAVE_OPENSSL_MD4_H
42 #include <openssl/md4.h>
46 #ifdef HAVE_OPENSSL_MD5_H
47 #include <openssl/md5.h>
51 #ifdef HAVE_OPENSSL_SHA_H
52 #include <openssl/sha.h>
58 * Compress len bytes from md into key
62 compressmd (OtpKey key, unsigned char *md, size_t len)
66 memset (p, 0, OTPKEYSIZE);
73 if (p == key + OTPKEYSIZE)
79 otp_md_init (OtpKey key,
83 void (*update)(void *, const void *, size_t),
84 void (*final)(void *, void *),
92 len = strlen(pwd) + strlen(seed);
100 (*update)(arg, p, len);
103 compressmd (key, res, ressz);
108 otp_md_next (OtpKey key,
109 void (*init)(void *),
110 void (*update)(void *, const void *, size_t),
111 void (*final)(void *, void *),
117 (*update)(arg, key, OTPKEYSIZE);
119 compressmd (key, res, ressz);
124 otp_md_hash (const char *data,
126 void (*init)(void *),
127 void (*update)(void *, const void *, size_t),
128 void (*final)(void *, void *),
134 (*update)(arg, data, len);
140 otp_md4_init (OtpKey key, const char *pwd, const char *seed)
142 unsigned char res[16];
145 return otp_md_init (key, pwd, seed,
146 (void (*)(void *))MD4_Init,
147 (void (*)(void *, const void *, size_t))MD4_Update,
148 (void (*)(void *, void *))MD4_Final,
149 &md4, res, sizeof(res));
153 otp_md4_hash (const char *data,
159 return otp_md_hash (data, len,
160 (void (*)(void *))MD4_Init,
161 (void (*)(void *, const void *, size_t))MD4_Update,
162 (void (*)(void *, void *))MD4_Final,
167 otp_md4_next (OtpKey key)
169 unsigned char res[16];
172 return otp_md_next (key,
173 (void (*)(void *))MD4_Init,
174 (void (*)(void *, const void *, size_t))MD4_Update,
175 (void (*)(void *, void *))MD4_Final,
176 &md4, res, sizeof(res));
181 otp_md5_init (OtpKey key, const char *pwd, const char *seed)
183 unsigned char res[16];
186 return otp_md_init (key, pwd, seed,
187 (void (*)(void *))MD5_Init,
188 (void (*)(void *, const void *, size_t))MD5_Update,
189 (void (*)(void *, void *))MD5_Final,
190 &md5, res, sizeof(res));
194 otp_md5_hash (const char *data,
200 return otp_md_hash (data, len,
201 (void (*)(void *))MD5_Init,
202 (void (*)(void *, const void *, size_t))MD5_Update,
203 (void (*)(void *, void *))MD5_Final,
208 otp_md5_next (OtpKey key)
210 unsigned char res[16];
213 return otp_md_next (key,
214 (void (*)(void *))MD5_Init,
215 (void (*)(void *, const void *, size_t))MD5_Update,
216 (void (*)(void *, void *))MD5_Final,
217 &md5, res, sizeof(res));
221 * For histerical reasons, in the OTP definition it's said that the
222 * result from SHA must be stored in little-endian order. See
223 * draft-ietf-otp-01.txt.
227 SHA1_Final_little_endian (void *res, SHA_CTX *m)
229 unsigned char tmp[20];
230 unsigned char *p = res;
234 for (j = 0; j < 20; j += 4) {
243 otp_sha_init (OtpKey key, const char *pwd, const char *seed)
245 unsigned char res[20];
248 return otp_md_init (key, pwd, seed,
249 (void (*)(void *))SHA1_Init,
250 (void (*)(void *, const void *, size_t))SHA1_Update,
251 (void (*)(void *, void *))SHA1_Final_little_endian,
252 &sha1, res, sizeof(res));
256 otp_sha_hash (const char *data,
262 return otp_md_hash (data, len,
263 (void (*)(void *))SHA1_Init,
264 (void (*)(void *, const void *, size_t))SHA1_Update,
265 (void (*)(void *, void *))SHA1_Final_little_endian,
270 otp_sha_next (OtpKey key)
272 unsigned char res[20];
275 return otp_md_next (key,
276 (void (*)(void *))SHA1_Init,
277 (void (*)(void *, const void *, size_t))SHA1_Update,
278 (void (*)(void *, void *))SHA1_Final_little_endian,
279 &sha1, res, sizeof(res));