remove COPYING* from .gitignore files
[platform/upstream/glibc.git] / sunrpc / 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 #if !defined(lint) && defined(SCCSID)
30 static char sccsid[] = "@(#)des_crypt.c 2.2 88/08/10 4.0 RPCSRC; from 1.13 88/02/08 SMI";
31 #endif
32 /*
33  * des_crypt.c, DES encryption library routines
34  * Copyright (C) 1986, Sun Microsystems, Inc.
35  */
36
37 #include <sys/types.h>
38 #include <rpc/des_crypt.h>
39 #include "des.h"
40
41 extern int _des_crypt (char *, unsigned, struct desparams *);
42
43 /*
44  * Copy 8 bytes
45  */
46 #define COPY8(src, dst) { \
47         register char *a = (char *) dst; \
48         register char *b = (char *) src; \
49         *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
50         *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
51 }
52
53 /*
54  * Copy multiple of 8 bytes
55  */
56 #define DESCOPY(src, dst, len) { \
57         register char *a = (char *) dst; \
58         register char *b = (char *) src; \
59         register int i; \
60         for (i = (int) len; i > 0; i -= 8) { \
61                 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
62                 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
63         } \
64 }
65
66 /*
67  * Common code to cbc_crypt() & ecb_crypt()
68  */
69 static int
70 common_crypt (char *key, char *buf, register unsigned len,
71               unsigned mode, register struct desparams *desp)
72 {
73   register int desdev;
74
75   if ((len % 8) != 0 || len > DES_MAXDATA)
76     return DESERR_BADPARAM;
77
78   desp->des_dir =
79     ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
80
81   desdev = mode & DES_DEVMASK;
82   COPY8 (key, desp->des_key);
83   /* 
84    * software
85    */
86   if (!_des_crypt (buf, len, desp))
87     return DESERR_HWERROR;
88
89   return desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE;
90 }
91
92 /*
93  * CBC mode encryption
94  */
95 int
96 cbc_crypt (char *key, char *buf, unsigned int len, unsigned int mode,
97            char *ivec)
98 {
99   int err;
100   struct desparams dp;
101
102   dp.des_mode = CBC;
103   COPY8 (ivec, dp.des_ivec);
104   err = common_crypt (key, buf, len, mode, &dp);
105   COPY8 (dp.des_ivec, ivec);
106   return err;
107 }
108 libc_hidden_def (cbc_crypt)
109
110 /*
111  * ECB mode encryption
112  */
113 int
114 ecb_crypt (char *key, char *buf, unsigned int len, unsigned int mode)
115 {
116   struct desparams dp;
117
118   dp.des_mode = ECB;
119   return common_crypt (key, buf, len, mode, &dp);
120 }
121 libc_hidden_def (ecb_crypt)