122347Spst/* hashlen.c: The opiehashlen() library function.
222347Spst
329967Sache%%% copyright-cmetz-96
492914SmarkmThis software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
592914SmarkmThe Inner Net License Version 3 applies to this software.
622347SpstYou should have received a copy of the license with this software. If
722347Spstyou didn't get a copy, you may request one from <license@inner.net>.
822347Spst
922347Spst        History:
1022347Spst
1192914Smarkm	Modified by cmetz for OPIE 2.4. Use struct opie_otpkey, isolate variables.
1222347Spst	Created by cmetz for OPIE 2.3.
1359121Skris
1459121Skris$FreeBSD$
1522347Spst*/
1622347Spst
17239169Sdelphij#include <sys/endian.h>
18239169Sdelphij
1922347Spst#include "opie_cfg.h"
2022347Spst#include "opie.h"
2122347Spst
2279711Smarkm#include <sha.h>
2322371Spst#include <md4.h>
2422371Spst#include <md5.h>
2522371Spst
2692914SmarkmVOIDRET opiehashlen FUNCTION((algorithm, in, out, n), int algorithm AND
2792914SmarkmVOIDPTR in AND struct opie_otpkey *out AND int n)
2822347Spst{
2922347Spst  UINT4 *results = (UINT4 *)out;
3022347Spst  UINT4 mdx_tmp[4];
3122347Spst
3222347Spst  switch(algorithm) {
3379711Smarkm    case 3: {
3479711Smarkm      SHA_CTX sha;
3579711Smarkm      UINT4 digest[5];
3679711Smarkm      SHA1_Init(&sha);
3779711Smarkm      SHA1_Update(&sha, (unsigned char *)in, n);
3879711Smarkm      SHA1_Final((unsigned char *)digest, &sha);
3979711Smarkm      results[0] = digest[0] ^ digest[2] ^ digest[4];
4079711Smarkm      results[1] = digest[1] ^ digest[3];
41239169Sdelphij
42239169Sdelphij      /*
43239169Sdelphij       * RFC2289 mandates that we convert SHA1 digest from big-endian to little
44239169Sdelphij       * see Appendix A.
45239169Sdelphij       */
46239169Sdelphij      results[0] = bswap32(results[0]);
47239169Sdelphij      results[1] = bswap32(results[1]);
4822347Spst      break;
4979711Smarkm    }
5022371Spst    case 4: {
5122371Spst      MD4_CTX mdx;
5222371Spst      MD4Init(&mdx);
5322371Spst      MD4Update(&mdx, (unsigned char *)in, n);
5422371Spst      MD4Final((unsigned char *)mdx_tmp, &mdx);
5522347Spst      results[0] = mdx_tmp[0] ^ mdx_tmp[2];
5622347Spst      results[1] = mdx_tmp[1] ^ mdx_tmp[3];
5722347Spst      break;
5822371Spst    }
5922371Spst    case 5: {
6022371Spst      MD5_CTX mdx;
6122371Spst      MD5Init(&mdx);
6222371Spst      MD5Update(&mdx, (unsigned char *)in, n);
6322371Spst      MD5Final((unsigned char *)mdx_tmp, &mdx);
6422347Spst      results[0] = mdx_tmp[0] ^ mdx_tmp[2];
6522347Spst      results[1] = mdx_tmp[1] ^ mdx_tmp[3];
6622347Spst      break;
6722371Spst    }
6822347Spst  }
6922347Spst}
70