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 * @(#)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 $ 30 */ 31 /* 32 * des_crypt.c, DES encryption library routines 33 * Copyright (C) 1986, Sun Microsystems, Inc. 34 */ 35 36 #include <sys/types.h> 37 #include <rpc/des_crypt.h> 38 #include <rpc/des.h> 39 40 static int common_crypt ( char *, char *, unsigned, unsigned, struct desparams * ); 41 int (*__des_crypt_LOCAL)(char *, int, struct desparams *) = NULL; 42 extern int _des_crypt_call( char *, int, struct desparams * ); 43 /* 44 * Copy 8 bytes 45 */ 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++; \ 51 } 52 53 /* 54 * Copy multiple of 8 bytes 55 */ 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 } \ 64 } 65 66 /* 67 * CBC mode encryption 68 */ 69 int 70 cbc_crypt(char *key, char *buf, unsigned len, unsigned mode, char *ivec) 71 { 72 int err; 73 struct desparams dp; 74 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); 85 } 86 87 88 /* 89 * ECB mode encryption 90 */ 91 int 92 ecb_crypt(char *key, char *buf, unsigned len, unsigned mode) 93 { 94 struct desparams dp; 95 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)); 103 } 104 105 106 107 /* 108 * Common code to cbc_crypt() & ecb_crypt() 109 */ 110 static int 111 common_crypt(char *key, char *buf, unsigned len, unsigned mode, 112 struct desparams *desp) 113 { 114 int desdev; 115 116 if ((len % 8) != 0 || len > DES_MAXDATA) { 117 return(DESERR_BADPARAM); 118 } 119 desp->des_dir = 120 ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT; 121 122 desdev = mode & DES_DEVMASK; 123 COPY8(key, desp->des_key); 124 /* 125 * software 126 */ 127 if (__des_crypt_LOCAL != NULL) { 128 if (!__des_crypt_LOCAL(buf, len, desp)) { 129 return (DESERR_HWERROR); 130 } 131 } else { 132 if (!_des_crypt_call(buf, len, desp)) { 133 return (DESERR_HWERROR); 134 } 135 } 136 return(desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE); 137 } 138