Back to index

glibc  2.9
des_crypt.c
Go to the documentation of this file.
00001 /*
00002  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00003  * unrestricted use provided that this legend is included on all tape
00004  * media and as a part of the software program in whole or part.  Users
00005  * may copy or modify Sun RPC without charge, but are not authorized
00006  * to license or distribute it to anyone else except as part of a product or
00007  * program developed by the user.
00008  * 
00009  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00010  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00011  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00012  * 
00013  * Sun RPC is provided with no support and without any obligation on the
00014  * part of Sun Microsystems, Inc. to assist in its use, correction,
00015  * modification or enhancement.
00016  * 
00017  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00018  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00019  * OR ANY PART THEREOF.
00020  * 
00021  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00022  * or profits or other special, indirect and consequential damages, even if
00023  * Sun has been advised of the possibility of such damages.
00024  * 
00025  * Sun Microsystems, Inc.
00026  * 2550 Garcia Avenue
00027  * Mountain View, California  94043
00028  */
00029 #if !defined(lint) && defined(SCCSID)
00030 static char sccsid[] = "@(#)des_crypt.c   2.2 88/08/10 4.0 RPCSRC; from 1.13 88/02/08 SMI";
00031 #endif
00032 /*
00033  * des_crypt.c, DES encryption library routines
00034  * Copyright (C) 1986, Sun Microsystems, Inc.
00035  */
00036 
00037 #include <sys/types.h>
00038 #include <rpc/des_crypt.h>
00039 #include "des.h"
00040 
00041 extern int _des_crypt (char *, unsigned, struct desparams *);
00042 
00043 /*
00044  * Copy 8 bytes
00045  */
00046 #define COPY8(src, dst) { \
00047        register char *a = (char *) dst; \
00048        register char *b = (char *) src; \
00049        *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
00050        *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
00051 }
00052 
00053 /*
00054  * Copy multiple of 8 bytes
00055  */
00056 #define DESCOPY(src, dst, len) { \
00057        register char *a = (char *) dst; \
00058        register char *b = (char *) src; \
00059        register int i; \
00060        for (i = (int) len; i > 0; i -= 8) { \
00061               *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
00062               *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
00063        } \
00064 }
00065 
00066 /*
00067  * Common code to cbc_crypt() & ecb_crypt()
00068  */
00069 static int
00070 common_crypt (char *key, char *buf, register unsigned len,
00071              unsigned mode, register struct desparams *desp)
00072 {
00073   register int desdev;
00074 
00075   if ((len % 8) != 0 || len > DES_MAXDATA)
00076     return DESERR_BADPARAM;
00077 
00078   desp->des_dir =
00079     ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
00080 
00081   desdev = mode & DES_DEVMASK;
00082   COPY8 (key, desp->des_key);
00083   /* 
00084    * software
00085    */
00086   if (!_des_crypt (buf, len, desp))
00087     return DESERR_HWERROR;
00088 
00089   return desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE;
00090 }
00091 
00092 /*
00093  * CBC mode encryption
00094  */
00095 int
00096 cbc_crypt (char *key, char *buf, unsigned int len, unsigned int mode,
00097           char *ivec)
00098 {
00099   int err;
00100   struct desparams dp;
00101 
00102   dp.des_mode = CBC;
00103   COPY8 (ivec, dp.des_ivec);
00104   err = common_crypt (key, buf, len, mode, &dp);
00105   COPY8 (dp.des_ivec, ivec);
00106   return err;
00107 }
00108 libc_hidden_def (cbc_crypt)
00109 
00110 /*
00111  * ECB mode encryption
00112  */
00113 int
00114 ecb_crypt (char *key, char *buf, unsigned int len, unsigned int mode)
00115 {
00116   struct desparams dp;
00117 
00118   dp.des_mode = ECB;
00119   return common_crypt (key, buf, len, mode, &dp);
00120 }
00121 libc_hidden_def (ecb_crypt)