Drop RCSID
[heimdal.git] / lib / otp / otp_md.c
blobd0736667520edb44a2a02a9459a50d0be042290c
1 /*
2 * Copyright (c) 1995 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "config.h"
36 #include "otp_locl.h"
38 #include "otp_md.h"
39 #include "crypto-headers.h"
42 * Compress len bytes from md into key
45 static void
46 compressmd (OtpKey key, unsigned char *md, size_t len)
48 u_char *p = key;
50 memset (p, 0, OTPKEYSIZE);
51 while(len) {
52 *p++ ^= *md++;
53 *p++ ^= *md++;
54 *p++ ^= *md++;
55 *p++ ^= *md++;
56 len -= 4;
57 if (p == key + OTPKEYSIZE)
58 p = key;
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.
68 static void
69 little_endian(unsigned char *res, size_t len)
71 unsigned char t;
72 size_t i;
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;
80 static int
81 otp_md_init (OtpKey key,
82 const char *pwd,
83 const char *seed,
84 const EVP_MD *md,
85 int le,
86 EVP_MD_CTX *arg,
87 unsigned char *res,
88 size_t ressz)
90 char *p;
91 int len;
93 len = strlen(pwd) + strlen(seed);
94 p = malloc (len + 1);
95 if (p == NULL)
96 return -1;
97 strlcpy (p, seed, len + 1);
98 strlwr (p);
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);
105 if (le)
106 little_endian(res, ressz);
108 free (p);
109 compressmd (key, res, ressz);
110 return 0;
113 static int
114 otp_md_next (OtpKey key,
115 const EVP_MD *md,
116 int le,
117 EVP_MD_CTX *arg,
118 unsigned char *res,
119 size_t ressz)
121 EVP_DigestInit_ex(arg, md, NULL);
122 EVP_DigestUpdate(arg, key, OTPKEYSIZE);
123 EVP_DigestFinal_ex(arg, res, NULL);
125 if (le)
126 little_endian(res, ressz);
128 compressmd (key, res, ressz);
129 return 0;
132 static int
133 otp_md_hash (const char *data,
134 size_t len,
135 const EVP_MD *md,
136 int le,
137 EVP_MD_CTX *arg,
138 unsigned char *res,
139 size_t ressz)
141 EVP_DigestInit_ex(arg, md, NULL);
142 EVP_DigestUpdate(arg, data, len);
143 EVP_DigestFinal_ex(arg, res, NULL);
145 if (le)
146 little_endian(res, ressz);
148 return 0;
152 otp_md4_init (OtpKey key, const char *pwd, const char *seed)
154 unsigned char res[16];
155 EVP_MD_CTX ctx;
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,
164 size_t len,
165 unsigned char *res)
167 EVP_MD_CTX ctx;
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];
178 EVP_MD_CTX ctx;
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];
190 EVP_MD_CTX ctx;
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,
199 size_t len,
200 unsigned char *res)
202 EVP_MD_CTX ctx;
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];
213 EVP_MD_CTX ctx;
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];
224 EVP_MD_CTX ctx;
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,
233 size_t len,
234 unsigned char *res)
236 EVP_MD_CTX ctx;
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];
247 EVP_MD_CTX ctx;
249 EVP_MD_CTX_init(&ctx);
251 return otp_md_next (key, EVP_sha1(), 1, &ctx, res, sizeof(res));