kqueue: Use wakeup_one based on # of threads sleep on kqueue
[dragonfly.git] / lib / libc / rpc / des_crypt.c
blobc6270a4d3bef95d4f19fba3ec4068d61ee80bc3c
1 /*-
2 * Copyright (c) 2009, Sun Microsystems, Inc.
3 * All rights reserved.
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>
38 #include <rpc/des.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 * );
44 * Copy 8 bytes
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; \
59 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 } \
67 * CBC mode encryption
69 int
70 cbc_crypt(char *key, char *buf, unsigned len, unsigned mode, char *ivec)
72 int err;
73 struct desparams dp;
75 #ifdef BROKEN_DES
76 dp.UDES.UDES_buf = buf;
77 dp.des_mode = ECB;
78 #else
79 dp.des_mode = CBC;
80 #endif
81 COPY8(ivec, dp.des_ivec);
82 err = common_crypt(key, buf, len, mode, &dp);
83 COPY8(dp.des_ivec, ivec);
84 return(err);
89 * ECB mode encryption
91 int
92 ecb_crypt(char *key, char *buf, unsigned len, unsigned mode)
94 struct desparams dp;
96 #ifdef BROKEN_DES
97 dp.UDES.UDES_buf = buf;
98 dp.des_mode = CBC;
99 #else
100 dp.des_mode = ECB;
101 #endif
102 return(common_crypt(key, buf, len, mode, &dp));
108 * Common code to cbc_crypt() & ecb_crypt()
110 static int
111 common_crypt(char *key, char *buf, unsigned len, unsigned mode,
112 struct desparams *desp)
114 int desdev;
116 if ((len % 8) != 0 || len > DES_MAXDATA) {
117 return(DESERR_BADPARAM);
119 desp->des_dir =
120 ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
122 desdev = mode & DES_DEVMASK;
123 COPY8(key, desp->des_key);
125 * software
127 if (__des_crypt_LOCAL != NULL) {
128 if (!__des_crypt_LOCAL(buf, len, desp)) {
129 return (DESERR_HWERROR);
131 } else {
132 if (!_des_crypt_call(buf, len, desp)) {
133 return (DESERR_HWERROR);
136 return(desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);