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

  1. #ifndef lint
  2. static char sccsid[] = "@(#)des_crypt.c    2.2 88/08/10 4.0 RPCSRC; from 1.13 88/02/08 SMI";
  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.  * des_crypt.c, DES encryption library routines
  34.  * Copyright (C) 1986, Sun Microsystems, Inc.
  35.  */
  36.  
  37. #include <des_crypt.h>
  38.  
  39. #include "des.h"
  40.  
  41. static int common_crypt();    
  42.  
  43. #if 0
  44. /*
  45.  * To see if chip is installed 
  46.  */
  47. #define UNOPENED (-2)
  48. static int g_desfd = UNOPENED;
  49. #endif
  50.  
  51. /*
  52.  * Copy 8 bytes
  53.  */
  54. #define COPY8(src, dst) { \
  55.     register char *a = (char *) dst; \
  56.     register char *b = (char *) src; \
  57.     *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
  58.     *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
  59. }
  60.  
  61. /*
  62.  * Copy multiple of 8 bytes
  63.  */
  64. #define DESCOPY(src, dst, len) { \
  65.     register char *a = (char *) dst; \
  66.     register char *b = (char *) src; \
  67.     register int i; \
  68.     for (i = (int) len; i > 0; i -= 8) { \
  69.         *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
  70.         *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
  71.     } \
  72. }
  73.  
  74. /*
  75.  * CBC mode encryption
  76.  */
  77. int
  78. cbc_crypt(key, buf, len, mode, ivec)
  79.     char *key;
  80.     char *buf;
  81.     unsigned len;
  82.     unsigned mode;
  83.     char *ivec;    
  84. {
  85.     int err;
  86.     struct desparams dp;
  87.  
  88.     dp.des_mode = CBC;
  89.     COPY8(ivec, dp.des_ivec);
  90.     err = common_crypt(key, buf, len, mode, &dp);
  91.     COPY8(dp.des_ivec, ivec);
  92.     return(err);
  93. }
  94.  
  95.  
  96. /*
  97.  * ECB mode encryption
  98.  */
  99. int 
  100. ecb_crypt(key, buf, len, mode)
  101.     char *key;
  102.     char *buf;
  103.     unsigned len;
  104.     unsigned mode;
  105. {
  106.     struct desparams dp;
  107.  
  108.     dp.des_mode = ECB;    
  109.     return(common_crypt(key, buf, len, mode, &dp));
  110. }
  111.  
  112.  
  113.  
  114. /*
  115.  * Common code to cbc_crypt() & ecb_crypt()
  116.  */
  117. static int 
  118. common_crypt(key, buf, len, mode, desp)    
  119.     char *key;    
  120.     char *buf;
  121.     unsigned len;
  122.     unsigned mode;
  123.     struct desparams *desp;
  124. {
  125.     register int desdev;
  126.  
  127.     if ((len % 8) != 0 || len > DES_MAXDATA) {
  128.         return(DESERR_BADPARAM);
  129.     }
  130.     desp->des_dir =
  131.         ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
  132.  
  133.     desdev = mode & DES_DEVMASK;
  134.     COPY8(key, desp->des_key);
  135.     /* 
  136.      * software
  137.      */
  138.     if (!_des_crypt(buf, len, desp)) {
  139.         return (DESERR_HWERROR);
  140.     }
  141.     return(desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
  142. }
  143.