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