Remove building with NOCRYPTO option
[minix.git] / crypto / external / bsd / heimdal / dist / lib / otp / otp_md.c
blob6708a9e7ad693ae0636863c217a737f0340e04fc
1 /* $NetBSD: otp_md.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $ */
3 /*
4 * Copyright (c) 1995 - 2003 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
33 * SUCH DAMAGE.
36 #define HC_DEPRECATED_CRYPTO
38 #include "config.h"
40 #include "otp_locl.h"
42 #include "otp_md.h"
43 #include "crypto-headers.h"
46 * Compress len bytes from md into key
49 static void
50 compressmd (OtpKey key, unsigned char *md, size_t len)
52 u_char *p = key;
54 memset (p, 0, OTPKEYSIZE);
55 while(len) {
56 *p++ ^= *md++;
57 *p++ ^= *md++;
58 *p++ ^= *md++;
59 *p++ ^= *md++;
60 len -= 4;
61 if (p == key + OTPKEYSIZE)
62 p = key;
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.
72 static void
73 little_endian(unsigned char *res, size_t len)
75 unsigned char t;
76 size_t i;
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;
84 static int
85 otp_md_init (OtpKey key,
86 const char *pwd,
87 const char *seed,
88 const EVP_MD *md,
89 int le,
90 unsigned char *res,
91 size_t ressz)
93 EVP_MD_CTX *ctx;
94 char *p;
95 int len;
97 ctx = EVP_MD_CTX_create();
99 len = strlen(pwd) + strlen(seed);
100 p = malloc (len + 1);
101 if (p == NULL)
102 return -1;
103 strlcpy (p, seed, len + 1);
104 strlwr (p);
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);
113 if (le)
114 little_endian(res, ressz);
116 free (p);
117 compressmd (key, res, ressz);
118 return 0;
121 static int
122 otp_md_next (OtpKey key,
123 const EVP_MD *md,
124 int le,
125 unsigned char *res,
126 size_t ressz)
128 EVP_MD_CTX *ctx;
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);
138 if (le)
139 little_endian(res, ressz);
141 compressmd (key, res, ressz);
142 return 0;
145 static int
146 otp_md_hash (const char *data,
147 size_t len,
148 const EVP_MD *md,
149 int le,
150 unsigned char *res,
151 size_t ressz)
153 EVP_MD_CTX *ctx;
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);
162 if (le)
163 little_endian(res, ressz);
165 return 0;
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,
177 size_t len,
178 unsigned char *res)
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,
200 size_t len,
201 unsigned char *res)
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,
222 size_t len,
223 unsigned char *res)
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));