2 * Copyright (c) 2009, Sun Microsystems, Inc.
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.
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.
28 * @(#)des_crypt.c 2.2 88/08/10 4.0 RPCSRC; from 1.13 88/02/08 SMI
29 * $FreeBSD: src/lib/libc/rpc/des_crypt.c,v 1.3 1999/08/28 00:00:38 peter Exp $
32 * des_crypt.c, DES encryption library routines
33 * Copyright (C) 1986, Sun Microsystems, Inc.
36 #include <sys/types.h>
37 #include <rpc/des_crypt.h>
40 static int common_crypt ( char *, char *, unsigned, unsigned, struct desparams
* );
41 int (*__des_crypt_LOCAL
)() = NULL
;
42 extern int _des_crypt_call( char *, int, struct desparams
* );
46 #define COPY8(src, dst) { \
47 char *a = (char *) dst; \
48 char *b = (char *) src; \
49 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
50 *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
54 * Copy multiple of 8 bytes
56 #define DESCOPY(src, dst, len) { \
57 char *a = (char *) dst; \
58 char *b = (char *) src; \
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++; \
70 cbc_crypt(char *key
, char *buf
, unsigned len
, unsigned mode
, char *ivec
)
76 dp
.UDES
.UDES_buf
= buf
;
81 COPY8(ivec
, dp
.des_ivec
);
82 err
= common_crypt(key
, buf
, len
, mode
, &dp
);
83 COPY8(dp
.des_ivec
, ivec
);
92 ecb_crypt(char *key
, char *buf
, unsigned len
, unsigned mode
)
97 dp
.UDES
.UDES_buf
= buf
;
102 return(common_crypt(key
, buf
, len
, mode
, &dp
));
108 * Common code to cbc_crypt() & ecb_crypt()
111 common_crypt(char *key
, char *buf
, unsigned len
, unsigned mode
,
112 struct desparams
*desp
)
116 if ((len
% 8) != 0 || len
> DES_MAXDATA
) {
117 return(DESERR_BADPARAM
);
120 ((mode
& DES_DIRMASK
) == DES_ENCRYPT
) ? ENCRYPT
: DECRYPT
;
122 desdev
= mode
& DES_DEVMASK
;
123 COPY8(key
, desp
->des_key
);
127 if (__des_crypt_LOCAL
!= NULL
) {
128 if (!__des_crypt_LOCAL(buf
, len
, desp
)) {
129 return (DESERR_HWERROR
);
132 if (!_des_crypt_call(buf
, len
, desp
)) {
133 return (DESERR_HWERROR
);
136 return(desdev
== DES_SW
? DESERR_NONE
: DESERR_NOHWDEVICE
);