Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/des_crypt.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2009, Sun Microsystems, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 /*
29 * des_crypt.c, DES encryption library routines
30 * Copyright (C) 1986, Sun Microsystems, Inc.
31 */
32
33 #include <sys/types.h>
34 #include <rpc/types.h>
35 #include <rpc/des_crypt.h>
36 #include <rpc/des.h>
37 #if 0
38 #ifndef lint
39 static char sccsid[] = "@(#)des_crypt.c 2.2 88/08/10 4.0 RPCSRC; from 1.13 88/02/08 SMI";
40 #endif
41 #endif
42
43 static int common_crypt( char *, char *, unsigned, unsigned, struct desparams * );
44 /*
45 * Copy 8 bytes
46 */
47 #define COPY8(src, dst) { \
48 char *a = (char *) dst; \
49 char *b = (char *) src; \
50 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
51 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
52 }
53
54 /*
55 * Copy multiple of 8 bytes
56 */
57 #define DESCOPY(src, dst, len) { \
58 char *a = (char *) dst; \
59 char *b = (char *) src; \
60 int i; \
61 for (i = (int) len; i > 0; i -= 8) { \
62 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
63 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
64 } \
65 }
66
67 /*
68 * CBC mode encryption
69 */
70 int
71 cbc_crypt(key, buf, len, mode, ivec)
72 char *key;
73 char *buf;
74 unsigned len;
75 unsigned mode;
76 char *ivec;
77 {
78 int err;
79 struct desparams dp;
80
81 #ifdef BROKEN_DES
82 dp.UDES.UDES_buf = buf;
83 dp.des_mode = ECB;
84 #else
85 dp.des_mode = CBC;
86 #endif
87 COPY8(ivec, dp.des_ivec);
88 err = common_crypt(key, buf, len, mode, &dp);
89 COPY8(dp.des_ivec, ivec);
90 return(err);
91 }
92
93
94 /*
95 * ECB mode encryption
96 */
97 int
98 ecb_crypt(key, buf, len, mode)
99 char *key;
100 char *buf;
101 unsigned len;
102 unsigned mode;
103 {
104 struct desparams dp;
105
106 #ifdef BROKEN_DES
107 dp.UDES.UDES_buf = buf;
108 dp.des_mode = CBC;
109 #else
110 dp.des_mode = ECB;
111 #endif
112 return(common_crypt(key, buf, len, mode, &dp));
113 }
114
115
116
117 /*
118 * Common code to cbc_crypt() & ecb_crypt()
119 */
120 static int
121 common_crypt(key, buf, len, mode, desp)
122 char *key;
123 char *buf;
124 unsigned len;
125 unsigned mode;
126 struct desparams *desp;
127 {
128 int desdev;
129
130 if ((len % 8) != 0 || len > DES_MAXDATA) {
131 return(DESERR_BADPARAM);
132 }
133 desp->des_dir =
134 ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
135
136 desdev = mode & DES_DEVMASK;
137 COPY8(key, desp->des_key);
138 /*
139 * software
140 */
141 if (!_des_crypt(buf, len, desp)) {
142 return (DESERR_HWERROR);
143 }
144
145 return(desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
146 }