home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / rpc / xcrypt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-15  |  3.8 KB  |  187 lines

  1. #ifndef lint
  2. static char sccsid[] = "@(#)xcrypt.c    2.2 88/08/10 4.0 RPCSRC";
  3. #endif
  4. /*
  5.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  6.  * unrestricted use provided that this legend is included on all tape
  7.  * media and as a part of the software program in whole or part.  Users
  8.  * may copy or modify Sun RPC without charge, but are not authorized
  9.  * to license or distribute it to anyone else except as part of a product or
  10.  * program developed by the user.
  11.  * 
  12.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  13.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  14.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  15.  * 
  16.  * Sun RPC is provided with no support and without any obligation on the
  17.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  18.  * modification or enhancement.
  19.  * 
  20.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  21.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  22.  * OR ANY PART THEREOF.
  23.  * 
  24.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  25.  * or profits or other special, indirect and consequential damages, even if
  26.  * Sun has been advised of the possibility of such damages.
  27.  * 
  28.  * Sun Microsystems, Inc.
  29.  * 2550 Garcia Avenue
  30.  * Mountain View, California  94043
  31.  */
  32. /*
  33.  * Hex encryption/decryption and utility routines
  34.  *
  35.  * Copyright (C) 1986, Sun Microsystems, Inc. 
  36.  */
  37.  
  38. #include <stdio.h>
  39. #include <des_crypt.h>
  40.  
  41. extern char *malloc();
  42.  
  43. extern char hex[];    /* forward */
  44. static char hexval();
  45.  
  46. static int hex2bin();
  47. static int bin2hex();
  48.  
  49. /*
  50.  * Encrypt a secret key given passwd
  51.  * The secret key is passed and returned in hex notation.
  52.  * Its length must be a multiple of 16 hex digits (64 bits).
  53.  */
  54. xencrypt(secret, passwd)
  55.     char *secret;
  56.     char *passwd;
  57. {
  58.     char key[8];
  59.     char ivec[8];
  60.     char *buf;
  61.     int err;
  62.     int len;
  63.  
  64.     len = strlen(secret) / 2;
  65.     buf = malloc((unsigned)len);
  66.  
  67.     hex2bin(len, secret, buf);
  68.     passwd2des(passwd, key);
  69.     bzero(ivec, 8);
  70.  
  71.     err = cbc_crypt(key, buf, len, DES_ENCRYPT | DES_HW, ivec);
  72.     if (DES_FAILED(err)) {    
  73.         free(buf);
  74.         return (0);
  75.     }
  76.     bin2hex(len, (unsigned char *) buf, secret);
  77.     free(buf);
  78.     return (1);
  79. }
  80.  
  81. /*
  82.  * Decrypt secret key using passwd
  83.  * The secret key is passed and returned in hex notation.
  84.  * Once again, the length is a multiple of 16 hex digits
  85.  */
  86. xdecrypt(secret, passwd)
  87.     char *secret;
  88.     char *passwd;
  89. {
  90.     char key[8];
  91.     char ivec[8];
  92.     char *buf;
  93.     int err;
  94.     int len;
  95.  
  96.     len = strlen(secret) / 2;
  97.     buf = malloc((unsigned)len);
  98.  
  99.     hex2bin(len, secret, buf);
  100.     passwd2des(passwd, key);    
  101.     bzero(ivec, 8);
  102.  
  103.     err = cbc_crypt(key, buf, len, DES_DECRYPT | DES_HW, ivec);
  104.     if (DES_FAILED(err)) {
  105.         free(buf);
  106.         return (0);
  107.     }
  108.     bin2hex(len, (unsigned char *) buf, secret);
  109.     free(buf);
  110.     return (1);
  111. }
  112.  
  113.  
  114. /*
  115.  * Turn password into DES key
  116.  */
  117. passwd2des(pw, key)
  118.     char *pw;
  119.     char *key;
  120. {
  121.     int i;
  122.  
  123.     bzero(key, 8);
  124.     for (i = 0; *pw; i = (i+1)%8) {
  125.         key[i] ^= *pw++ << 1;
  126.     }
  127.     des_setparity(key);
  128. }
  129.  
  130.  
  131.  
  132. /*
  133.  * Hex to binary conversion
  134.  */
  135. static
  136. hex2bin(len, hexnum, binnum)
  137.     int len;
  138.     char *hexnum;
  139.     char *binnum;
  140. {
  141.     int i;
  142.  
  143.     for (i = 0; i < len; i++) {
  144.         *binnum++ = 16 * hexval(hexnum[2*i]) + hexval(hexnum[2*i+1]);    
  145.     }
  146. }
  147.  
  148. /*
  149.  * Binary to hex conversion
  150.  */
  151. static
  152. bin2hex(len, binnum, hexnum)
  153.     int len;
  154.     unsigned char *binnum;
  155.     char *hexnum;
  156. {
  157.     int i;
  158.     unsigned val;
  159.  
  160.     for (i = 0; i < len; i++) {
  161.         val = binnum[i];
  162.         hexnum[i*2] = hex[val >> 4];
  163.         hexnum[i*2+1] = hex[val & 0xf];
  164.     }
  165.     hexnum[len*2] = 0;
  166. }
  167.  
  168. static char hex[16] = {
  169.     '0', '1', '2', '3', '4', '5', '6', '7',
  170.     '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
  171. };
  172.  
  173. static char
  174. hexval(c)
  175.     char c;
  176. {
  177.     if (c >= '0' && c <= '9') {
  178.         return (c - '0');
  179.     } else if (c >= 'a' && c <= 'z') {
  180.         return (c - 'a' + 10);
  181.     } else if (c >= 'A' && c <= 'Z') {
  182.         return (c - 'A' + 10);
  183.     } else {
  184.         return (-1);
  185.     }
  186. }
  187.