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