2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
30 * des_crypt.c, DES encryption library routines
31 * Copyright (C) 1986, Sun Microsystems, Inc.
34 #include <sys/types.h>
35 #include <rpc/des_crypt.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)des_crypt.c 2.2 88/08/10 4.0 RPCSRC; from 1.13 88/02/08 SMI";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 static int common_crypt( char *, char *, unsigned, unsigned, struct desparams * );
45 int (*__des_crypt_LOCAL)() = 0;
46 extern int _des_crypt_call(char *, int, struct desparams *);
50 #define COPY8(src, dst) { \
51 char *a = (char *) dst; \
52 char *b = (char *) src; \
53 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
54 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
58 * Copy multiple of 8 bytes
60 #define DESCOPY(src, dst, len) { \
61 char *a = (char *) dst; \
62 char *b = (char *) src; \
64 for (i = (int) len; i > 0; i -= 8) { \
65 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
66 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
74 cbc_crypt(key, buf, len, mode, ivec)
85 dp.UDES.UDES_buf = buf;
90 COPY8(ivec, dp.des_ivec);
91 err = common_crypt(key, buf, len, mode, &dp);
92 COPY8(dp.des_ivec, ivec);
101 ecb_crypt(key, buf, len, mode)
110 dp.UDES.UDES_buf = buf;
115 return(common_crypt(key, buf, len, mode, &dp));
121 * Common code to cbc_crypt() & ecb_crypt()
124 common_crypt(key, buf, len, mode, desp)
129 struct desparams *desp;
133 if ((len % 8) != 0 || len > DES_MAXDATA) {
134 return(DESERR_BADPARAM);
137 ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
139 desdev = mode & DES_DEVMASK;
140 COPY8(key, desp->des_key);
144 if (__des_crypt_LOCAL != NULL) {
145 if (!__des_crypt_LOCAL(buf, len, desp)) {
146 return (DESERR_HWERROR);
149 if (!_des_crypt_call(buf, len, desp)) {
150 return (DESERR_HWERROR);
153 return(desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);