home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / pwgenera.zip / keycrnch.c < prev    next >
C/C++ Source or Header  |  1996-06-25  |  3KB  |  86 lines

  1. /* keycrunch.c: The opiekeycrunch() library function.
  2.  
  3. Portions of this software are Copyright 1996 by Craig Metz, All Rights
  4. Reserved. The Inner Net Copyright Notice and License Agreement applies to
  5. these portions of the software.
  6.  
  7. Portions of this software are Copyright 1995 by Randall Atkinson and Dan
  8. McDonald, All Rights Reserved. All Rights under this copyright are assigned
  9. to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
  10. License Agreement applies to this software.
  11.  
  12.         History:
  13.  
  14.     Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al.
  15.                Renamed MDx functions to prevent conflicts. Use unified 
  16.                context structure. Added changes to comply with OTP
  17.                spec: enforce sixteen char seed length limit, make seed
  18.                case insensitive by crunching as lower case. Don't allow
  19.                spaces in seed (shouldn't get here anyway). Check for
  20.                NULL arguments.
  21.         Created at NRL for OPIE 2.2 from opiesubr.c.
  22. */
  23. #include "opie_cfg.h"
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>    /* ANSI C standard library */
  28.  
  29. #include "opie.h"
  30.  
  31. /* Crunch a key:
  32.  * concatenate the seed and the password, run through MD4 or MD5 and
  33.  * collapse to 64 bits. This is defined as the user's starting key.
  34.  */
  35. int opiekeycrunch FUNCTION((algorithm, result, seed, passwd), unsigned algorithm AND char *result AND char *seed AND char *passwd)
  36. {
  37.   char *buf;
  38.   struct opiemdx_ctx ctx;
  39.   UINT4 results[4];
  40.   unsigned int buflen;
  41.  
  42. /* NOTE - we need to bound seed and password before this point.
  43.    "any length" can bite us... */
  44.   if (!seed || !seed[0] || !passwd || !result)
  45.     return 1;
  46.  
  47.   if ((buflen = strlen(seed)) > OPIE_SEED_MAX) 
  48.     return 1;
  49.  
  50.   {
  51.   int i;
  52.  
  53.   for (i = 0; seed[i]; i++)
  54.     if ((seed[i] >= 'A') && (seed[i] <= 'Z'))
  55.       seed[i] += 'a' - 'A';
  56.     else
  57.       if (seed[i] == ' ')
  58.         return 1;
  59.   } 
  60.  
  61.   buflen += strlen(passwd);
  62.   if ((buf = malloc(buflen + 1)) == NULL)
  63.     return -1;
  64.  
  65.   strcpy(buf, seed);
  66.   strcat(buf, passwd);
  67.  
  68.   /* Crunch the key through MD[45] */
  69.   if (4 == algorithm) {
  70.     opiemd4init(&ctx);
  71.     opiemd4update(&ctx, (unsigned char *) buf, buflen);
  72.     opiemd4final((unsigned char *) results, &ctx);
  73.   } else {
  74.     opiemd5init(&ctx);
  75.     opiemd5update(&ctx, (unsigned char *) buf, buflen);
  76.     opiemd5final((unsigned char *) results, &ctx);
  77.   }
  78.   free(buf);
  79.  
  80.   results[0] ^= results[2];
  81.   results[1] ^= results[3];
  82.  
  83.   memcpy(result, (char *) results, 8);
  84.   return 0;
  85. }
  86.