]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/opie/libopie/hashlen.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / opie / libopie / hashlen.c
1 /* hashlen.c: The opiehashlen() library function.
2
3 %%% copyright-cmetz-96
4 This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
5 The Inner Net License Version 3 applies to this software.
6 You should have received a copy of the license with this software. If
7 you didn't get a copy, you may request one from <license@inner.net>.
8
9         History:
10
11         Modified by cmetz for OPIE 2.4. Use struct opie_otpkey, isolate variables.
12         Created by cmetz for OPIE 2.3.
13
14 $FreeBSD$
15 */
16
17 #include <sys/endian.h>
18
19 #include "opie_cfg.h"
20 #include "opie.h"
21
22 #include <sha.h>
23 #include <md4.h>
24 #include <md5.h>
25
26 VOIDRET opiehashlen FUNCTION((algorithm, in, out, n), int algorithm AND
27 VOIDPTR in AND struct opie_otpkey *out AND int n)
28 {
29   UINT4 *results = (UINT4 *)out;
30   UINT4 mdx_tmp[4];
31
32   switch(algorithm) {
33     case 3: {
34       SHA_CTX sha;
35       UINT4 digest[5];
36       SHA1_Init(&sha);
37       SHA1_Update(&sha, (unsigned char *)in, n);
38       SHA1_Final((unsigned char *)digest, &sha);
39       results[0] = digest[0] ^ digest[2] ^ digest[4];
40       results[1] = digest[1] ^ digest[3];
41
42       /*
43        * RFC2289 mandates that we convert SHA1 digest from big-endian to little
44        * see Appendix A.
45        */
46       results[0] = bswap32(results[0]);
47       results[1] = bswap32(results[1]);
48       break;
49     }
50     case 4: {
51       MD4_CTX mdx;
52       MD4Init(&mdx);
53       MD4Update(&mdx, (unsigned char *)in, n);
54       MD4Final((unsigned char *)mdx_tmp, &mdx);
55       results[0] = mdx_tmp[0] ^ mdx_tmp[2];
56       results[1] = mdx_tmp[1] ^ mdx_tmp[3];
57       break;
58     }
59     case 5: {
60       MD5_CTX mdx;
61       MD5Init(&mdx);
62       MD5Update(&mdx, (unsigned char *)in, n);
63       MD5Final((unsigned char *)mdx_tmp, &mdx);
64       results[0] = mdx_tmp[0] ^ mdx_tmp[2];
65       results[1] = mdx_tmp[1] ^ mdx_tmp[3];
66       break;
67     }
68   }
69 }