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
34 #define HC_DEPRECATED_CRYPTO
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
)
65 * For histerical reasons, in the OTP definition it's said that
66 * the result from SHA must be stored in little-endian order. See
67 * draft-ietf-otp-01.txt.
71 little_endian(unsigned char *res
, size_t len
)
76 for (i
= 0; i
< len
; i
+= 4) {
77 t
= res
[i
+ 0]; res
[i
+ 0] = res
[i
+ 3]; res
[i
+ 3] = t
;
78 t
= res
[i
+ 1]; res
[i
+ 1] = res
[i
+ 2]; res
[i
+ 2] = t
;
83 otp_md_init (OtpKey key
,
95 ctx
= EVP_MD_CTX_create();
97 len
= strlen(pwd
) + strlen(seed
);
101 strlcpy (p
, seed
, len
+ 1);
103 strlcat (p
, pwd
, len
+ 1);
105 EVP_DigestInit_ex(ctx
, md
, NULL
);
106 EVP_DigestUpdate(ctx
, p
, len
);
107 EVP_DigestFinal_ex(ctx
, res
, NULL
);
109 EVP_MD_CTX_destroy(ctx
);
112 little_endian(res
, ressz
);
115 compressmd (key
, res
, ressz
);
120 otp_md_next (OtpKey key
,
128 ctx
= EVP_MD_CTX_create();
130 EVP_DigestInit_ex(ctx
, md
, NULL
);
131 EVP_DigestUpdate(ctx
, key
, OTPKEYSIZE
);
132 EVP_DigestFinal_ex(ctx
, res
, NULL
);
134 EVP_MD_CTX_destroy(ctx
);
137 little_endian(res
, ressz
);
139 compressmd (key
, res
, ressz
);
144 otp_md_hash (const char *data
,
152 ctx
= EVP_MD_CTX_create();
154 EVP_DigestInit_ex(ctx
, md
, NULL
);
155 EVP_DigestUpdate(ctx
, data
, len
);
156 EVP_DigestFinal_ex(ctx
, res
, NULL
);
158 EVP_MD_CTX_destroy(ctx
);
161 little_endian(res
, ressz
);
167 otp_md4_init (OtpKey key
, const char *pwd
, const char *seed
)
169 unsigned char res
[16];
170 return otp_md_init (key
, pwd
, seed
, EVP_md4(), 0, res
, sizeof(res
));
174 otp_md4_hash (const char *data
,
178 return otp_md_hash (data
, len
, EVP_md4(), 0, res
, 16);
182 otp_md4_next (OtpKey key
)
184 unsigned char res
[16];
185 return otp_md_next (key
, EVP_md4(), 0, res
, sizeof(res
));
190 otp_md5_init (OtpKey key
, const char *pwd
, const char *seed
)
192 unsigned char res
[16];
193 return otp_md_init (key
, pwd
, seed
, EVP_md5(), 0, res
, sizeof(res
));
197 otp_md5_hash (const char *data
,
201 return otp_md_hash (data
, len
, EVP_md5(), 0, res
, 16);
205 otp_md5_next (OtpKey key
)
207 unsigned char res
[16];
208 return otp_md_next (key
, EVP_md5(), 0, res
, sizeof(res
));
212 otp_sha_init (OtpKey key
, const char *pwd
, const char *seed
)
214 unsigned char res
[20];
215 return otp_md_init (key
, pwd
, seed
, EVP_sha1(), 1, res
, sizeof(res
));
219 otp_sha_hash (const char *data
,
223 return otp_md_hash (data
, len
, EVP_sha1(), 1, res
, 20);
227 otp_sha_next (OtpKey key
)
229 unsigned char res
[20];
230 return otp_md_next (key
, EVP_sha1(), 1, res
, sizeof(res
));