1 /* $NetBSD: otp_md.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $ */
4 * Copyright (c) 1995 - 2003 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #define HC_DEPRECATED_CRYPTO
43 #include "crypto-headers.h"
46 * Compress len bytes from md into key
50 compressmd (OtpKey key
, unsigned char *md
, size_t len
)
54 memset (p
, 0, OTPKEYSIZE
);
61 if (p
== key
+ OTPKEYSIZE
)
67 * For histerical reasons, in the OTP definition it's said that
68 * the result from SHA must be stored in little-endian order. See
69 * draft-ietf-otp-01.txt.
73 little_endian(unsigned char *res
, size_t len
)
78 for (i
= 0; i
< len
; i
+= 4) {
79 t
= res
[i
+ 0]; res
[i
+ 0] = res
[i
+ 3]; res
[i
+ 3] = t
;
80 t
= res
[i
+ 1]; res
[i
+ 1] = res
[i
+ 2]; res
[i
+ 2] = t
;
85 otp_md_init (OtpKey key
,
97 ctx
= EVP_MD_CTX_create();
99 len
= strlen(pwd
) + strlen(seed
);
100 p
= malloc (len
+ 1);
103 strlcpy (p
, seed
, len
+ 1);
105 strlcat (p
, pwd
, len
+ 1);
107 EVP_DigestInit_ex(ctx
, md
, NULL
);
108 EVP_DigestUpdate(ctx
, p
, len
);
109 EVP_DigestFinal_ex(ctx
, res
, NULL
);
111 EVP_MD_CTX_destroy(ctx
);
114 little_endian(res
, ressz
);
117 compressmd (key
, res
, ressz
);
122 otp_md_next (OtpKey key
,
130 ctx
= EVP_MD_CTX_create();
132 EVP_DigestInit_ex(ctx
, md
, NULL
);
133 EVP_DigestUpdate(ctx
, key
, OTPKEYSIZE
);
134 EVP_DigestFinal_ex(ctx
, res
, NULL
);
136 EVP_MD_CTX_destroy(ctx
);
139 little_endian(res
, ressz
);
141 compressmd (key
, res
, ressz
);
146 otp_md_hash (const char *data
,
154 ctx
= EVP_MD_CTX_create();
156 EVP_DigestInit_ex(ctx
, md
, NULL
);
157 EVP_DigestUpdate(ctx
, data
, len
);
158 EVP_DigestFinal_ex(ctx
, res
, NULL
);
160 EVP_MD_CTX_destroy(ctx
);
163 little_endian(res
, ressz
);
169 otp_md4_init (OtpKey key
, const char *pwd
, const char *seed
)
171 unsigned char res
[16];
172 return otp_md_init (key
, pwd
, seed
, EVP_md4(), 0, res
, sizeof(res
));
176 otp_md4_hash (const char *data
,
180 return otp_md_hash (data
, len
, EVP_md4(), 0, res
, 16);
184 otp_md4_next (OtpKey key
)
186 unsigned char res
[16];
187 return otp_md_next (key
, EVP_md4(), 0, res
, sizeof(res
));
192 otp_md5_init (OtpKey key
, const char *pwd
, const char *seed
)
194 unsigned char res
[16];
195 return otp_md_init (key
, pwd
, seed
, EVP_md5(), 0, res
, sizeof(res
));
199 otp_md5_hash (const char *data
,
203 return otp_md_hash (data
, len
, EVP_md5(), 0, res
, 16);
207 otp_md5_next (OtpKey key
)
209 unsigned char res
[16];
210 return otp_md_next (key
, EVP_md5(), 0, res
, sizeof(res
));
214 otp_sha_init (OtpKey key
, const char *pwd
, const char *seed
)
216 unsigned char res
[20];
217 return otp_md_init (key
, pwd
, seed
, EVP_sha1(), 1, res
, sizeof(res
));
221 otp_sha_hash (const char *data
,
225 return otp_md_hash (data
, len
, EVP_sha1(), 1, res
, 20);
229 otp_sha_next (OtpKey key
)
231 unsigned char res
[20];
232 return otp_md_next (key
, EVP_sha1(), 1, res
, sizeof(res
));