des_crypt.c revision 258578
1193326Sed/*- 2193326Sed * Copyright (c) 2009, Sun Microsystems, Inc. 3193326Sed * All rights reserved. 4193326Sed * 5193326Sed * Redistribution and use in source and binary forms, with or without 6193326Sed * modification, are permitted provided that the following conditions are met: 7193326Sed * - Redistributions of source code must retain the above copyright notice, 8193326Sed * this list of conditions and the following disclaimer. 9193326Sed * - Redistributions in binary form must reproduce the above copyright notice, 10193326Sed * this list of conditions and the following disclaimer in the documentation 11193326Sed * and/or other materials provided with the distribution. 12193326Sed * - Neither the name of Sun Microsystems, Inc. nor the names of its 13193326Sed * contributors may be used to endorse or promote products derived 14193326Sed * from this software without specific prior written permission. 15193326Sed * 16193326Sed * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17193326Sed * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18193326Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19193326Sed * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20193326Sed * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21193326Sed * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22193326Sed * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23193326Sed * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24193326Sed * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25193326Sed * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26193326Sed * POSSIBILITY OF SUCH DAMAGE. 27193326Sed */ 28193326Sed/* 29193326Sed * des_crypt.c, DES encryption library routines 30193326Sed * Copyright (C) 1986, Sun Microsystems, Inc. 31193326Sed */ 32193326Sed 33193326Sed#include <sys/types.h> 34193326Sed#include <rpc/des_crypt.h> 35193326Sed#include <rpc/des.h> 36193326Sed 37193326Sed#if defined(LIBC_SCCS) && !defined(lint) 38193326Sedstatic char sccsid[] = "@(#)des_crypt.c 2.2 88/08/10 4.0 RPCSRC; from 1.13 88/02/08 SMI"; 39193326Sed#endif 40193326Sed#include <sys/cdefs.h> 41193326Sed__FBSDID("$FreeBSD: head/lib/libc/rpc/des_crypt.c 258578 2013-11-25 19:04:36Z hrs $"); 42193326Sed 43193326Sedstatic int common_crypt( char *, char *, unsigned, unsigned, struct desparams * ); 44193326Sedint (*__des_crypt_LOCAL)() = 0; 45193326Sedextern int _des_crypt_call(char *, int, struct desparams *); 46198092Srdivacky/* 47193326Sed * Copy 8 bytes 48193326Sed */ 49193326Sed#define COPY8(src, dst) { \ 50193326Sed char *a = (char *) dst; \ 51193326Sed char *b = (char *) src; \ 52193326Sed *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \ 53193326Sed *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \ 54198092Srdivacky} 55193326Sed 56193326Sed/* 57193326Sed * Copy multiple of 8 bytes 58198092Srdivacky */ 59193326Sed#define DESCOPY(src, dst, len) { \ 60193326Sed char *a = (char *) dst; \ 61193326Sed char *b = (char *) src; \ 62193326Sed int i; \ 63193326Sed for (i = (int) len; i > 0; i -= 8) { \ 64193326Sed *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \ 65193326Sed *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \ 66193326Sed } \ 67193326Sed} 68193326Sed 69193326Sed/* 70193326Sed * CBC mode encryption 71193326Sed */ 72193326Sedint 73193326Sedcbc_crypt(key, buf, len, mode, ivec) 74193326Sed char *key; 75193326Sed char *buf; 76198092Srdivacky unsigned len; 77193326Sed unsigned mode; 78193326Sed char *ivec; 79193326Sed{ 80193326Sed int err; 81193326Sed struct desparams dp; 82193326Sed 83193326Sed#ifdef BROKEN_DES 84198092Srdivacky dp.UDES.UDES_buf = buf; 85198092Srdivacky dp.des_mode = ECB; 86193326Sed#else 87193326Sed dp.des_mode = CBC; 88198092Srdivacky#endif 89198092Srdivacky COPY8(ivec, dp.des_ivec); 90198092Srdivacky err = common_crypt(key, buf, len, mode, &dp); 91193326Sed COPY8(dp.des_ivec, ivec); 92193326Sed return(err); 93198092Srdivacky} 94198092Srdivacky 95198092Srdivacky 96198092Srdivacky/* 97193326Sed * ECB mode encryption 98193326Sed */ 99193326Sedint 100193326Sedecb_crypt(key, buf, len, mode) 101193326Sed char *key; 102193326Sed char *buf; 103193326Sed unsigned len; 104193326Sed unsigned mode; 105193326Sed{ 106198092Srdivacky struct desparams dp; 107198092Srdivacky 108198092Srdivacky#ifdef BROKEN_DES 109193326Sed dp.UDES.UDES_buf = buf; 110193326Sed dp.des_mode = CBC; 111193326Sed#else 112193326Sed dp.des_mode = ECB; 113193326Sed#endif 114198092Srdivacky return(common_crypt(key, buf, len, mode, &dp)); 115198092Srdivacky} 116193326Sed 117193326Sed 118193326Sed 119193326Sed/* 120193326Sed * Common code to cbc_crypt() & ecb_crypt() 121193326Sed */ 122194613Sedstatic int 123193326Sedcommon_crypt(key, buf, len, mode, desp) 124193326Sed char *key; 125193326Sed char *buf; 126198092Srdivacky unsigned len; 127193326Sed unsigned mode; 128193326Sed struct desparams *desp; 129193326Sed{ 130193326Sed int desdev; 131193326Sed 132193326Sed if ((len % 8) != 0 || len > DES_MAXDATA) { 133193326Sed return(DESERR_BADPARAM); 134198092Srdivacky } 135193326Sed desp->des_dir = 136193326Sed ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT; 137193326Sed 138193326Sed desdev = mode & DES_DEVMASK; 139193326Sed COPY8(key, desp->des_key); 140193326Sed /* 141193326Sed * software 142198092Srdivacky */ 143198092Srdivacky if (__des_crypt_LOCAL != NULL) { 144193326Sed if (!__des_crypt_LOCAL(buf, len, desp)) { 145193326Sed return (DESERR_HWERROR); 146193326Sed } 147193326Sed } else { 148193326Sed if (!_des_crypt_call(buf, len, desp)) { 149198092Srdivacky return (DESERR_HWERROR); 150198092Srdivacky } 151193326Sed } 152193326Sed return(desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE); 153193326Sed} 154193326Sed