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("$Heimdal: otp_md.c 12048 2003-04-16 16:19:33Z lha $"
42 #include "crypto-headers.h"
45 * Compress len bytes from md into key
49 compressmd (OtpKey key
, unsigned char *md
, size_t len
)
53 memset (p
, 0, OTPKEYSIZE
);
60 if (p
== key
+ OTPKEYSIZE
)
65 #ifdef HAVE_OLD_HASH_NAMES
67 otp_md4_final (void *res
, struct md4
*m
)
72 #define MD4_Final otp_md4_final
75 otp_md5_final (void *res
, struct md5
*m
)
80 #define MD5_Final otp_md5_final
84 otp_md_init (OtpKey key
,
88 void (*update
)(void *, const void *, size_t),
89 void (*final
)(void *, void *),
97 len
= strlen(pwd
) + strlen(seed
);
101 strlcpy (p
, seed
, len
+ 1);
103 strlcat (p
, pwd
, len
+ 1);
105 (*update
)(arg
, p
, len
);
108 compressmd (key
, res
, ressz
);
113 otp_md_next (OtpKey key
,
114 void (*init
)(void *),
115 void (*update
)(void *, const void *, size_t),
116 void (*final
)(void *, void *),
122 (*update
)(arg
, key
, OTPKEYSIZE
);
124 compressmd (key
, res
, ressz
);
129 otp_md_hash (const char *data
,
131 void (*init
)(void *),
132 void (*update
)(void *, const void *, size_t),
133 void (*final
)(void *, void *),
139 (*update
)(arg
, data
, len
);
145 otp_md4_init (OtpKey key
, const char *pwd
, const char *seed
)
147 unsigned char res
[16];
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
,
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
,
172 otp_md4_next (OtpKey key
)
174 unsigned char res
[16];
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];
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
,
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
,
213 otp_md5_next (OtpKey key
)
215 unsigned char res
[16];
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.
232 SHA1_Final_little_endian (void *res
, SHA_CTX
*m
)
234 unsigned char tmp
[20];
235 unsigned char *p
= res
;
239 for (j
= 0; j
< 20; j
+= 4) {
248 otp_sha_init (OtpKey key
, const char *pwd
, const char *seed
)
250 unsigned char res
[20];
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
,
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
,
275 otp_sha_next (OtpKey key
)
277 unsigned char res
[20];
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
));