home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / os2 / pgp263.arj / PGP263I.SRC / PGP263II.ZIP / src / passwd.c < prev    next >
C/C++ Source or Header  |  1996-01-02  |  3KB  |  109 lines

  1. /*    passwd.c - Password reading/hashing routines
  2.     Implemented in Microsoft C.
  3.     Routines for getting a pass phrase from the user's console.
  4.  
  5.     (c) Copyright 1990-1996 by Philip Zimmermann.  All rights reserved.
  6.     The author assumes no liability for damages resulting from the use
  7.     of this software, even if the damage results from defects in this
  8.     software.  No warranty is expressed or implied.
  9.  
  10.     Note that while most PGP source modules bear Philip Zimmermann's
  11.     copyright notice, many of them have been revised or entirely written
  12.     by contributors who frequently failed to put their names in their
  13.     code.  Code that has been incorporated into PGP from other authors
  14.     was either originally published in the public domain or is used with
  15.     permission from the various authors.
  16.  
  17.     PGP is available for free to the public under certain restrictions.
  18.     See the PGP User's Guide (included in the release package) for
  19.     important information about licensing, patent restrictions on
  20.     certain algorithms, trademarks, copyrights, and export controls.
  21. */
  22.  
  23. #include    <stdio.h>    /* for fprintf() */
  24. #include    <ctype.h>    /* for isdigit(), toupper(), etc. */
  25. #include    <string.h>    /* for strlen() */
  26.  
  27. #include    "random.h"    /* for getstring() */
  28. #include    "md5.h"
  29. #include    "language.h"
  30. #include    "pgp.h"
  31. #include        "charset.h"
  32.  
  33. #ifdef AMIGA
  34. #       include "system.h"
  35. #endif
  36.  
  37. #define MAXKEYLEN 254    /* max byte length of pass phrase */
  38.  
  39. boolean showpass = FALSE;
  40.  
  41. /*
  42. **    hashpass - Hash pass phrase down to 128 bits (16 bytes).
  43. **  keylen must be less than 1024.
  44. **    Use the MD5 algorithm.
  45. */
  46. void hashpass (char *keystring, int keylen, byte *hash)
  47. {
  48.     struct MD5Context mdContext;
  49.  
  50.     /* Calculate the hash */
  51.     MD5Init(&mdContext);
  52.     MD5Update(&mdContext, (unsigned char *) keystring, keylen);
  53.     MD5Final(hash, &mdContext);
  54. } /* hashpass */
  55.  
  56.  
  57. /*
  58. **    GetHashedPassPhrase - get pass phrase from user,
  59.          hashes it to an IDEA key.
  60.     Parameters:
  61.         returns char *keystring as the pass phrase itself
  62.         return char *hash as the 16-byte hash of the pass phrase
  63.                 using MD5.
  64.         byte noecho:  
  65.             0=ask once, echo. 
  66.             1=ask once, no echo. 
  67.             2=ask twice, no echo.
  68.     Return 0 if no characters are input, else return 1.
  69.     If we return 0, the hashed key will not be useful.
  70. */
  71. int GetHashedPassPhrase(char *hash, boolean noecho)
  72. {    char keystr1[MAXKEYLEN+2], keystr2[MAXKEYLEN+2];
  73.     int len;
  74.  
  75.     if (showpass)
  76.         noecho = 0;
  77.     for (;;) {
  78.         fprintf(pgpout,LANG("\nEnter pass phrase: "));
  79. #ifdef AMIGA
  80.                 requesterdesc=LANG("\nEnter pass phrase: ");
  81. #endif
  82.         getstring(keystr1,MAXKEYLEN-1,!noecho);
  83.         if (noecho<2)    /* no need to ask again if user can see it */
  84.             break;
  85.         fprintf(pgpout,LANG("\nEnter same pass phrase again: "));
  86. #ifdef AMIGA
  87.                 requesterdesc=LANG("\nEnter same pass phrase again: ");
  88. #endif
  89.         getstring(keystr2,MAXKEYLEN-1,!noecho);
  90.         if (strcmp(keystr1,keystr2)==0)
  91.             break;
  92.         fprintf(pgpout,
  93. LANG("\n\007Error: Pass phrases were different.  Try again."));
  94.         memset(keystr2, 0, sizeof(keystr2));
  95.     }
  96.     if (noecho && (filter_mode || quietmode))
  97.         putc('\n', pgpout);
  98.  
  99.     len = strlen(keystr1);
  100.     if (len == 0)
  101.         return 0;
  102.     CONVERT_TO_CANONICAL_CHARSET(keystr1);
  103.     hashpass (keystr1, strlen(keystr1), (byte *) hash);
  104.     memset(keystr1, 0, sizeof(keystr1));
  105.     memset(keystr2, 0, sizeof(keystr2));
  106.     return 1;
  107. } /* GetHashedPassPhrase */
  108.  
  109.