ReactOS 0.4.16-dev-297-gc569aee
des_crypt.c
Go to the documentation of this file.
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 <wintirpc.h>
34#include <sys/types.h>
35#include <rpc/types.h>
36#include <rpc/des_crypt.h>
37#include <rpc/des.h>
38#if 0
39#ifndef lint
40static char sccsid[] = "@(#)des_crypt.c 2.2 88/08/10 4.0 RPCSRC; from 1.13 88/02/08 SMI";
41#endif
42#endif
43//#include <sys/cdefs.h>
44
45static int common_crypt( char *, char *, unsigned, unsigned, struct desparams * );
47extern int _des_crypt_call(char *, int, struct desparams *);
48/*
49 * Copy 8 bytes
50 */
51#define COPY8(src, dst) { \
52 char *a = (char *) dst; \
53 char *b = (char *) src; \
54 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
55 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
56}
57
58/*
59 * Copy multiple of 8 bytes
60 */
61#define DESCOPY(src, dst, len) { \
62 char *a = (char *) dst; \
63 char *b = (char *) src; \
64 int i; \
65 for (i = (int) len; i > 0; i -= 8) { \
66 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
67 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
68 } \
69}
70
71/*
72 * CBC mode encryption
73 */
74int
76 char *key;
77 char *buf;
78 unsigned len;
79 unsigned mode;
80 char *ivec;
81{
82 int err;
83 struct desparams dp;
84
85#ifdef BROKEN_DES
86 dp.UDES.UDES_buf = buf;
87 dp.des_mode = ECB;
88#else
89 dp.des_mode = CBC;
90#endif
91 COPY8(ivec, dp.des_ivec);
92 err = common_crypt(key, buf, len, mode, &dp);
93 COPY8(dp.des_ivec, ivec);
94 return(err);
95}
96
97
98/*
99 * ECB mode encryption
100 */
101int
103 char *key;
104 char *buf;
105 unsigned len;
106 unsigned mode;
107{
108 struct desparams dp;
109
110#ifdef BROKEN_DES
111 dp.UDES.UDES_buf = buf;
112 dp.des_mode = CBC;
113#else
114 dp.des_mode = ECB;
115#endif
116 return(common_crypt(key, buf, len, mode, &dp));
117}
118
119
120
121/*
122 * Common code to cbc_crypt() & ecb_crypt()
123 */
124static int
126 char *key;
127 char *buf;
128 unsigned len;
129 unsigned mode;
130 struct desparams *desp;
131{
132 int desdev;
133
134 if ((len % 8) != 0 || len > DES_MAXDATA) {
135 return(DESERR_BADPARAM);
136 }
137 desp->des_dir =
139
140 desdev = mode & DES_DEVMASK;
141 COPY8(key, desp->des_key);
142 /*
143 * software
144 */
145 if (__des_crypt_LOCAL != NULL) {
146 if (!__des_crypt_LOCAL(buf, len, desp)) {
147 return (DESERR_HWERROR);
148 }
149 } else {
150 if (!_des_crypt_call(buf, len, desp)) {
151 return (DESERR_HWERROR);
152 }
153 }
154 return(desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
155}
static char sccsid[]
Definition: cmds.c:27
static int common_crypt(char *, char *, unsigned, unsigned, struct desparams *)
Definition: des_crypt.c:125
int(* __des_crypt_LOCAL)()=0
Definition: des_crypt.c:46
int _des_crypt_call(char *, int, struct desparams *)
Definition: crypt_client.c:46
int cbc_crypt(char *key, char *buf, unsigned len, unsigned mode, char *ivec)
Definition: des_crypt.c:75
#define COPY8(src, dst)
Definition: des_crypt.c:51
int ecb_crypt(char *key, char *buf, unsigned len, unsigned mode)
Definition: des_crypt.c:102
#define DESERR_NONE
Definition: des_crypt.h:60
#define DES_SW
Definition: des_crypt.h:57
#define DESERR_NOHWDEVICE
Definition: des_crypt.h:61
#define DES_DIRMASK
Definition: des_crypt.h:50
#define DES_ENCRYPT
Definition: des_crypt.h:51
#define DES_MAXDATA
Definition: des_crypt.h:49
#define DES_DEVMASK
Definition: des_crypt.h:55
#define DESERR_HWERROR
Definition: des_crypt.h:62
#define DESERR_BADPARAM
Definition: des_crypt.h:63
@ DECRYPT
Definition: des.h:39
@ ENCRYPT
Definition: des.h:39
@ CBC
Definition: des.h:40
@ ECB
Definition: des.h:40
#define NULL
Definition: types.h:112
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
GLenum mode
Definition: glext.h:6217
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLenum GLsizei len
Definition: glext.h:6722
#define err(...)
Definition: des.h:45
u_char des_ivec[8]
Definition: des.h:49
enum desmode des_mode
Definition: des.h:48
u_char * UDES_buf
Definition: des.h:53
u_char des_key[8]
Definition: des.h:46
union desparams::@193 UDES
enum desdir des_dir
Definition: des.h:47
Definition: copy.c:22