Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / opie / libopie / hash.c
1 /* hash.c: The opiehash() 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 for binary arg. 
12         Modified by cmetz for OPIE 2.31. Added SHA support (which may
13               not be correct). Backed out previous optimizations as
14               they killed thread-safety.
15         Created by cmetz for OPIE 2.3 using the old hash.c as a guide.
16
17 $FreeBSD: src/contrib/opie/libopie/hash.c,v 1.3.6.2 2002/07/15 14:48:47 des Exp $
18 $DragonFly: src/contrib/opie/libopie/hash.c,v 1.2 2003/06/17 04:24:05 dillon Exp $
19 */
20
21 #include "opie_cfg.h"
22 #include "opie.h"
23
24 #include <sha.h>
25 #include <md4.h>
26 #include <md5.h>
27
28 VOIDRET opiehash FUNCTION((x, algorithm), struct opie_otpkey *x AND
29 unsigned algorithm)
30 {
31   UINT4 *results = (UINT4 *)x;
32
33   switch(algorithm) {
34     case 3:
35       {
36       SHA_CTX sha;
37       UINT4 digest[5];
38       SHA1_Init(&sha);
39       SHA1_Update(&sha, (unsigned char *)x, 8);
40       SHA1_Final((unsigned char *)digest, &sha);
41       results[0] = digest[0] ^ digest[2] ^ digest[4];
42       results[1] = digest[1] ^ digest[3];
43       };
44       break;
45     case 4:
46       {
47       MD4_CTX mdx;
48       UINT4 mdx_tmp[4];
49
50       MD4Init(&mdx);
51       MD4Update(&mdx, (unsigned char *)x, 8);
52       MD4Final((unsigned char *)mdx_tmp, &mdx);
53       results[0] = mdx_tmp[0] ^ mdx_tmp[2];
54       results[1] = mdx_tmp[1] ^ mdx_tmp[3];
55       };
56       break;
57     case 5:
58       {
59       MD5_CTX mdx;
60       UINT4 mdx_tmp[4];
61
62       MD5Init(&mdx);
63       MD5Update(&mdx, (unsigned char *)x, 8);
64       MD5Final((unsigned char *)mdx_tmp, &mdx);
65       results[0] = mdx_tmp[0] ^ mdx_tmp[2];
66       results[1] = mdx_tmp[1] ^ mdx_tmp[3];
67       };
68       break;
69   }
70 }