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
39 #include "crypto-headers.h"
42 * Compress len bytes from md into key
46 compressmd (OtpKey key
, unsigned char *md
, size_t len
)
50 memset (p
, 0, OTPKEYSIZE
);
57 if (p
== key
+ OTPKEYSIZE
)
63 * For histerical reasons, in the OTP definition it's said that
64 * the result from SHA must be stored in little-endian order. See
65 * draft-ietf-otp-01.txt.
69 little_endian(unsigned char *res
, size_t len
)
74 for (i
= 0; i
< len
; i
+= 4) {
75 t
= res
[i
+ 0]; res
[i
+ 0] = res
[i
+ 3]; res
[i
+ 3] = t
;
76 t
= res
[i
+ 1]; res
[i
+ 1] = res
[i
+ 2]; res
[i
+ 2] = t
;
81 otp_md_init (OtpKey key
,
93 len
= strlen(pwd
) + strlen(seed
);
97 strlcpy (p
, seed
, len
+ 1);
99 strlcat (p
, pwd
, len
+ 1);
101 EVP_DigestInit_ex(arg
, md
, NULL
);
102 EVP_DigestUpdate(arg
, p
, len
);
103 EVP_DigestFinal_ex(arg
, res
, NULL
);
106 little_endian(res
, ressz
);
109 compressmd (key
, res
, ressz
);
114 otp_md_next (OtpKey key
,
121 EVP_DigestInit_ex(arg
, md
, NULL
);
122 EVP_DigestUpdate(arg
, key
, OTPKEYSIZE
);
123 EVP_DigestFinal_ex(arg
, res
, NULL
);
126 little_endian(res
, ressz
);
128 compressmd (key
, res
, ressz
);
133 otp_md_hash (const char *data
,
141 EVP_DigestInit_ex(arg
, md
, NULL
);
142 EVP_DigestUpdate(arg
, data
, len
);
143 EVP_DigestFinal_ex(arg
, res
, NULL
);
146 little_endian(res
, ressz
);
152 otp_md4_init (OtpKey key
, const char *pwd
, const char *seed
)
154 unsigned char res
[16];
157 EVP_MD_CTX_init(&ctx
);
159 return otp_md_init (key
, pwd
, seed
, EVP_md4(), 0, &ctx
, res
, sizeof(res
));
163 otp_md4_hash (const char *data
,
169 EVP_MD_CTX_init(&ctx
);
171 return otp_md_hash (data
, len
, EVP_md4(), 0, &ctx
, res
, 16);
175 otp_md4_next (OtpKey key
)
177 unsigned char res
[16];
180 EVP_MD_CTX_init(&ctx
);
182 return otp_md_next (key
, EVP_md4(), 0, &ctx
, res
, sizeof(res
));
187 otp_md5_init (OtpKey key
, const char *pwd
, const char *seed
)
189 unsigned char res
[16];
192 EVP_MD_CTX_init(&ctx
);
194 return otp_md_init (key
, pwd
, seed
, EVP_md5(), 0, &ctx
, res
, sizeof(res
));
198 otp_md5_hash (const char *data
,
204 EVP_MD_CTX_init(&ctx
);
206 return otp_md_hash (data
, len
, EVP_md5(), 0, &ctx
, res
, 16);
210 otp_md5_next (OtpKey key
)
212 unsigned char res
[16];
215 EVP_MD_CTX_init(&ctx
);
217 return otp_md_next (key
, EVP_md5(), 0, &ctx
, res
, sizeof(res
));
221 otp_sha_init (OtpKey key
, const char *pwd
, const char *seed
)
223 unsigned char res
[20];
226 EVP_MD_CTX_init(&ctx
);
228 return otp_md_init (key
, pwd
, seed
, EVP_sha1(), 1, &ctx
, res
, sizeof(res
));
232 otp_sha_hash (const char *data
,
238 EVP_MD_CTX_init(&ctx
);
240 return otp_md_hash (data
, len
, EVP_sha1(), 1, &ctx
, res
, 20);
244 otp_sha_next (OtpKey key
)
246 unsigned char res
[20];
249 EVP_MD_CTX_init(&ctx
);
251 return otp_md_next (key
, EVP_sha1(), 1, &ctx
, res
, sizeof(res
));