xref: /minix3/crypto/external/bsd/heimdal/dist/lib/otp/otp_md.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: otp_md.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1995 - 2003 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #define HC_DEPRECATED_CRYPTO
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc #include "config.h"
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc #include "otp_locl.h"
41ebfedea0SLionel Sambuc 
42ebfedea0SLionel Sambuc #include "otp_md.h"
43ebfedea0SLionel Sambuc #include "crypto-headers.h"
44ebfedea0SLionel Sambuc 
45ebfedea0SLionel Sambuc /*
46ebfedea0SLionel Sambuc  * Compress len bytes from md into key
47ebfedea0SLionel Sambuc  */
48ebfedea0SLionel Sambuc 
49ebfedea0SLionel Sambuc static void
compressmd(OtpKey key,unsigned char * md,size_t len)50ebfedea0SLionel Sambuc compressmd (OtpKey key, unsigned char *md, size_t len)
51ebfedea0SLionel Sambuc {
52ebfedea0SLionel Sambuc     u_char *p = key;
53ebfedea0SLionel Sambuc 
54ebfedea0SLionel Sambuc     memset (p, 0, OTPKEYSIZE);
55ebfedea0SLionel Sambuc     while(len) {
56ebfedea0SLionel Sambuc 	*p++ ^= *md++;
57ebfedea0SLionel Sambuc 	*p++ ^= *md++;
58ebfedea0SLionel Sambuc 	*p++ ^= *md++;
59ebfedea0SLionel Sambuc 	*p++ ^= *md++;
60ebfedea0SLionel Sambuc 	len -= 4;
61ebfedea0SLionel Sambuc 	if (p == key + OTPKEYSIZE)
62ebfedea0SLionel Sambuc 	    p = key;
63ebfedea0SLionel Sambuc     }
64ebfedea0SLionel Sambuc }
65ebfedea0SLionel Sambuc 
66ebfedea0SLionel Sambuc /*
67ebfedea0SLionel Sambuc  * For histerical reasons, in the OTP definition it's said that
68ebfedea0SLionel Sambuc  * the result from SHA must be stored in little-endian order.  See
69ebfedea0SLionel Sambuc  * draft-ietf-otp-01.txt.
70ebfedea0SLionel Sambuc  */
71ebfedea0SLionel Sambuc 
72ebfedea0SLionel Sambuc static void
little_endian(unsigned char * res,size_t len)73ebfedea0SLionel Sambuc little_endian(unsigned char *res, size_t len)
74ebfedea0SLionel Sambuc {
75ebfedea0SLionel Sambuc     unsigned char t;
76ebfedea0SLionel Sambuc     size_t i;
77ebfedea0SLionel Sambuc 
78ebfedea0SLionel Sambuc     for (i = 0; i < len; i += 4) {
79ebfedea0SLionel Sambuc 	t = res[i + 0]; res[i + 0] = res[i + 3]; res[i + 3] = t;
80ebfedea0SLionel Sambuc 	t = res[i + 1]; res[i + 1] = res[i + 2]; res[i + 2] = t;
81ebfedea0SLionel Sambuc     }
82ebfedea0SLionel Sambuc }
83ebfedea0SLionel Sambuc 
84ebfedea0SLionel Sambuc static int
otp_md_init(OtpKey key,const char * pwd,const char * seed,const EVP_MD * md,int le,unsigned char * res,size_t ressz)85ebfedea0SLionel Sambuc otp_md_init (OtpKey key,
86ebfedea0SLionel Sambuc 	     const char *pwd,
87ebfedea0SLionel Sambuc 	     const char *seed,
88ebfedea0SLionel Sambuc 	     const EVP_MD *md,
89ebfedea0SLionel Sambuc 	     int le,
90ebfedea0SLionel Sambuc 	     unsigned char *res,
91ebfedea0SLionel Sambuc 	     size_t ressz)
92ebfedea0SLionel Sambuc {
93ebfedea0SLionel Sambuc     EVP_MD_CTX *ctx;
94ebfedea0SLionel Sambuc     char *p;
95ebfedea0SLionel Sambuc     int len;
96ebfedea0SLionel Sambuc 
97ebfedea0SLionel Sambuc     ctx = EVP_MD_CTX_create();
98ebfedea0SLionel Sambuc 
99ebfedea0SLionel Sambuc     len = strlen(pwd) + strlen(seed);
100ebfedea0SLionel Sambuc     p = malloc (len + 1);
101ebfedea0SLionel Sambuc     if (p == NULL)
102ebfedea0SLionel Sambuc 	return -1;
103ebfedea0SLionel Sambuc     strlcpy (p, seed, len + 1);
104ebfedea0SLionel Sambuc     strlwr (p);
105ebfedea0SLionel Sambuc     strlcat (p, pwd, len + 1);
106ebfedea0SLionel Sambuc 
107ebfedea0SLionel Sambuc     EVP_DigestInit_ex(ctx, md, NULL);
108ebfedea0SLionel Sambuc     EVP_DigestUpdate(ctx, p, len);
109ebfedea0SLionel Sambuc     EVP_DigestFinal_ex(ctx, res, NULL);
110ebfedea0SLionel Sambuc 
111ebfedea0SLionel Sambuc     EVP_MD_CTX_destroy(ctx);
112ebfedea0SLionel Sambuc 
113ebfedea0SLionel Sambuc     if (le)
114ebfedea0SLionel Sambuc     	little_endian(res, ressz);
115ebfedea0SLionel Sambuc 
116ebfedea0SLionel Sambuc     free (p);
117ebfedea0SLionel Sambuc     compressmd (key, res, ressz);
118ebfedea0SLionel Sambuc     return 0;
119ebfedea0SLionel Sambuc }
120ebfedea0SLionel Sambuc 
121ebfedea0SLionel Sambuc static int
otp_md_next(OtpKey key,const EVP_MD * md,int le,unsigned char * res,size_t ressz)122ebfedea0SLionel Sambuc otp_md_next (OtpKey key,
123ebfedea0SLionel Sambuc 	     const EVP_MD *md,
124ebfedea0SLionel Sambuc 	     int le,
125ebfedea0SLionel Sambuc 	     unsigned char *res,
126ebfedea0SLionel Sambuc 	     size_t ressz)
127ebfedea0SLionel Sambuc {
128ebfedea0SLionel Sambuc     EVP_MD_CTX *ctx;
129ebfedea0SLionel Sambuc 
130ebfedea0SLionel Sambuc     ctx = EVP_MD_CTX_create();
131ebfedea0SLionel Sambuc 
132ebfedea0SLionel Sambuc     EVP_DigestInit_ex(ctx, md, NULL);
133ebfedea0SLionel Sambuc     EVP_DigestUpdate(ctx, key, OTPKEYSIZE);
134ebfedea0SLionel Sambuc     EVP_DigestFinal_ex(ctx, res, NULL);
135ebfedea0SLionel Sambuc 
136ebfedea0SLionel Sambuc     EVP_MD_CTX_destroy(ctx);
137ebfedea0SLionel Sambuc 
138ebfedea0SLionel Sambuc     if (le)
139ebfedea0SLionel Sambuc 	little_endian(res, ressz);
140ebfedea0SLionel Sambuc 
141ebfedea0SLionel Sambuc     compressmd (key, res, ressz);
142ebfedea0SLionel Sambuc     return 0;
143ebfedea0SLionel Sambuc }
144ebfedea0SLionel Sambuc 
145ebfedea0SLionel Sambuc static int
otp_md_hash(const char * data,size_t len,const EVP_MD * md,int le,unsigned char * res,size_t ressz)146ebfedea0SLionel Sambuc otp_md_hash (const char *data,
147ebfedea0SLionel Sambuc 	     size_t len,
148ebfedea0SLionel Sambuc 	     const EVP_MD *md,
149ebfedea0SLionel Sambuc 	     int le,
150ebfedea0SLionel Sambuc 	     unsigned char *res,
151ebfedea0SLionel Sambuc 	     size_t ressz)
152ebfedea0SLionel Sambuc {
153ebfedea0SLionel Sambuc     EVP_MD_CTX *ctx;
154ebfedea0SLionel Sambuc     ctx = EVP_MD_CTX_create();
155ebfedea0SLionel Sambuc 
156ebfedea0SLionel Sambuc     EVP_DigestInit_ex(ctx, md, NULL);
157ebfedea0SLionel Sambuc     EVP_DigestUpdate(ctx, data, len);
158ebfedea0SLionel Sambuc     EVP_DigestFinal_ex(ctx, res, NULL);
159ebfedea0SLionel Sambuc 
160ebfedea0SLionel Sambuc     EVP_MD_CTX_destroy(ctx);
161ebfedea0SLionel Sambuc 
162ebfedea0SLionel Sambuc     if (le)
163ebfedea0SLionel Sambuc 	little_endian(res, ressz);
164ebfedea0SLionel Sambuc 
165ebfedea0SLionel Sambuc     return 0;
166ebfedea0SLionel Sambuc }
167ebfedea0SLionel Sambuc 
168ebfedea0SLionel Sambuc int
otp_md4_init(OtpKey key,const char * pwd,const char * seed)169ebfedea0SLionel Sambuc otp_md4_init (OtpKey key, const char *pwd, const char *seed)
170ebfedea0SLionel Sambuc {
171ebfedea0SLionel Sambuc   unsigned char res[16];
172ebfedea0SLionel Sambuc   return otp_md_init (key, pwd, seed, EVP_md4(), 0, res, sizeof(res));
173ebfedea0SLionel Sambuc }
174ebfedea0SLionel Sambuc 
175ebfedea0SLionel Sambuc int
otp_md4_hash(const char * data,size_t len,unsigned char * res)176ebfedea0SLionel Sambuc otp_md4_hash (const char *data,
177ebfedea0SLionel Sambuc 	      size_t len,
178ebfedea0SLionel Sambuc 	      unsigned char *res)
179ebfedea0SLionel Sambuc {
180ebfedea0SLionel Sambuc   return otp_md_hash (data, len, EVP_md4(), 0, res, 16);
181ebfedea0SLionel Sambuc }
182ebfedea0SLionel Sambuc 
183ebfedea0SLionel Sambuc int
otp_md4_next(OtpKey key)184ebfedea0SLionel Sambuc otp_md4_next (OtpKey key)
185ebfedea0SLionel Sambuc {
186ebfedea0SLionel Sambuc   unsigned char res[16];
187ebfedea0SLionel Sambuc   return otp_md_next (key, EVP_md4(), 0, res, sizeof(res));
188ebfedea0SLionel Sambuc }
189ebfedea0SLionel Sambuc 
190ebfedea0SLionel Sambuc 
191ebfedea0SLionel Sambuc int
otp_md5_init(OtpKey key,const char * pwd,const char * seed)192ebfedea0SLionel Sambuc otp_md5_init (OtpKey key, const char *pwd, const char *seed)
193ebfedea0SLionel Sambuc {
194ebfedea0SLionel Sambuc   unsigned char res[16];
195ebfedea0SLionel Sambuc   return otp_md_init (key, pwd, seed, EVP_md5(), 0, res, sizeof(res));
196ebfedea0SLionel Sambuc }
197ebfedea0SLionel Sambuc 
198ebfedea0SLionel Sambuc int
otp_md5_hash(const char * data,size_t len,unsigned char * res)199ebfedea0SLionel Sambuc otp_md5_hash (const char *data,
200ebfedea0SLionel Sambuc 	      size_t len,
201ebfedea0SLionel Sambuc 	      unsigned char *res)
202ebfedea0SLionel Sambuc {
203ebfedea0SLionel Sambuc   return otp_md_hash (data, len, EVP_md5(), 0, res, 16);
204ebfedea0SLionel Sambuc }
205ebfedea0SLionel Sambuc 
206ebfedea0SLionel Sambuc int
otp_md5_next(OtpKey key)207ebfedea0SLionel Sambuc otp_md5_next (OtpKey key)
208ebfedea0SLionel Sambuc {
209ebfedea0SLionel Sambuc   unsigned char res[16];
210ebfedea0SLionel Sambuc   return otp_md_next (key, EVP_md5(), 0, res, sizeof(res));
211ebfedea0SLionel Sambuc }
212ebfedea0SLionel Sambuc 
213ebfedea0SLionel Sambuc int
otp_sha_init(OtpKey key,const char * pwd,const char * seed)214ebfedea0SLionel Sambuc otp_sha_init (OtpKey key, const char *pwd, const char *seed)
215ebfedea0SLionel Sambuc {
216ebfedea0SLionel Sambuc   unsigned char res[20];
217ebfedea0SLionel Sambuc   return otp_md_init (key, pwd, seed, EVP_sha1(), 1, res, sizeof(res));
218ebfedea0SLionel Sambuc }
219ebfedea0SLionel Sambuc 
220ebfedea0SLionel Sambuc int
otp_sha_hash(const char * data,size_t len,unsigned char * res)221ebfedea0SLionel Sambuc otp_sha_hash (const char *data,
222ebfedea0SLionel Sambuc 	      size_t len,
223ebfedea0SLionel Sambuc 	      unsigned char *res)
224ebfedea0SLionel Sambuc {
225ebfedea0SLionel Sambuc   return otp_md_hash (data, len, EVP_sha1(), 1, res, 20);
226ebfedea0SLionel Sambuc }
227ebfedea0SLionel Sambuc 
228ebfedea0SLionel Sambuc int
otp_sha_next(OtpKey key)229ebfedea0SLionel Sambuc otp_sha_next (OtpKey key)
230ebfedea0SLionel Sambuc {
231ebfedea0SLionel Sambuc   unsigned char res[20];
232ebfedea0SLionel Sambuc   return otp_md_next (key, EVP_sha1(), 1, res, sizeof(res));
233ebfedea0SLionel Sambuc }
234