* Removed the __P macros from lib/
[games.git] / lib / libc / rpc / des_crypt.c
1 /*
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.
8  * 
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.
12  * 
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.
16  * 
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.
20  * 
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.
24  * 
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)des_crypt.c      2.2 88/08/10 4.0 RPCSRC; from 1.13 88/02/08 SMI
30  * $FreeBSD: src/lib/libc/rpc/des_crypt.c,v 1.3 1999/08/28 00:00:38 peter Exp $
31  * $DragonFly: src/lib/libc/rpc/des_crypt.c,v 1.3 2003/11/12 20:21:25 eirikn Exp $
32  */
33 /*
34  * des_crypt.c, DES encryption library routines
35  * Copyright (C) 1986, Sun Microsystems, Inc.
36  */
37
38 #include <sys/types.h>
39 #include <rpc/des_crypt.h>
40 #include <rpc/des.h>
41
42 static int common_crypt ( char *, char *, register unsigned, unsigned, struct desparams * );
43 int (*__des_crypt_LOCAL)() = 0;
44 extern _des_crypt_call ( char *, int, struct desparams * );
45 /*
46  * Copy 8 bytes
47  */
48 #define COPY8(src, dst) { \
49         register char *a = (char *) dst; \
50         register char *b = (char *) src; \
51         *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
52         *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
53 }
54  
55 /*
56  * Copy multiple of 8 bytes
57  */
58 #define DESCOPY(src, dst, len) { \
59         register char *a = (char *) dst; \
60         register char *b = (char *) src; \
61         register int i; \
62         for (i = (int) len; i > 0; i -= 8) { \
63                 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
64                 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
65         } \
66 }
67
68 /*
69  * CBC mode encryption
70  */
71 int
72 cbc_crypt(key, buf, len, mode, ivec)
73         char *key;
74         char *buf;
75         unsigned len;
76         unsigned mode;
77         char *ivec;     
78 {
79         int err;
80         struct desparams dp;
81
82 #ifdef BROKEN_DES
83         dp.UDES.UDES_buf = buf;
84         dp.des_mode = ECB;
85 #else
86         dp.des_mode = CBC;
87 #endif
88         COPY8(ivec, dp.des_ivec);
89         err = common_crypt(key, buf, len, mode, &dp);
90         COPY8(dp.des_ivec, ivec);
91         return(err);
92 }
93
94
95 /*
96  * ECB mode encryption
97  */
98 int
99 ecb_crypt(key, buf, len, mode)
100         char *key;
101         char *buf;
102         unsigned len;
103         unsigned mode;
104 {
105         struct desparams dp;
106
107 #ifdef BROKEN_DES
108         dp.UDES.UDES_buf = buf;
109         dp.des_mode = CBC;
110 #else
111         dp.des_mode = ECB;
112 #endif
113         return(common_crypt(key, buf, len, mode, &dp));
114 }
115
116
117
118 /*
119  * Common code to cbc_crypt() & ecb_crypt()
120  */
121 static int
122 common_crypt(key, buf, len, mode, desp) 
123         char *key;      
124         char *buf;
125         register unsigned len;
126         unsigned mode;
127         register struct desparams *desp;
128 {
129         register int desdev;
130
131         if ((len % 8) != 0 || len > DES_MAXDATA) {
132                 return(DESERR_BADPARAM);
133         }
134         desp->des_dir =
135                 ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
136
137         desdev = mode & DES_DEVMASK;
138         COPY8(key, desp->des_key);
139         /* 
140          * software
141          */
142         if (__des_crypt_LOCAL != NULL) {
143                 if (!__des_crypt_LOCAL(buf, len, desp)) {
144                         return (DESERR_HWERROR);
145                 }
146         } else {
147                 if (!_des_crypt_call(buf, len, desp)) {
148                         return (DESERR_HWERROR);
149                 }
150         }
151         return(desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
152 }