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