/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 2009, Sun Microsystems, Inc.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8* - Redistributions of source code must retain the above copyright notice,9* this list of conditions and the following disclaimer.10* - Redistributions in binary form must reproduce the above copyright notice,11* this list of conditions and the following disclaimer in the documentation12* and/or other materials provided with the distribution.13* - Neither the name of Sun Microsystems, Inc. nor the names of its14* contributors may be used to endorse or promote products derived15* from this software without specific prior written permission.16*17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"18* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE21* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS24* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN25* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)26* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGE.28*/29/*30* des_crypt.c, DES encryption library routines31* Copyright (C) 1986, Sun Microsystems, Inc.32*/3334#include <sys/types.h>35#include <rpc/des_crypt.h>36#include <rpc/des.h>3738static int common_crypt( char *, char *, unsigned, unsigned, struct desparams * );39int (*__des_crypt_LOCAL)(char *, unsigned, struct desparams *) = 0;40extern int _des_crypt_call(char *, int, struct desparams *);41/*42* Copy 8 bytes43*/44#define COPY8(src, dst) { \45char *a = (char *) dst; \46char *b = (char *) src; \47*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \48*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \49}5051/*52* Copy multiple of 8 bytes53*/54#define DESCOPY(src, dst, len) { \55char *a = (char *) dst; \56char *b = (char *) src; \57int i; \58for (i = (int) len; i > 0; i -= 8) { \59*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \60*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \61} \62}6364/*65* CBC mode encryption66*/67int68cbc_crypt(char *key, char *buf, unsigned len, unsigned mode, char *ivec)69{70int err;71struct desparams dp;7273#ifdef BROKEN_DES74dp.UDES.UDES_buf = buf;75dp.des_mode = ECB;76#else77dp.des_mode = CBC;78#endif79COPY8(ivec, dp.des_ivec);80err = common_crypt(key, buf, len, mode, &dp);81COPY8(dp.des_ivec, ivec);82return(err);83}848586/*87* ECB mode encryption88*/89int90ecb_crypt(char *key, char *buf, unsigned len, unsigned mode)91{92struct desparams dp;9394#ifdef BROKEN_DES95dp.UDES.UDES_buf = buf;96dp.des_mode = CBC;97#else98dp.des_mode = ECB;99#endif100return(common_crypt(key, buf, len, mode, &dp));101}102103104105/*106* Common code to cbc_crypt() & ecb_crypt()107*/108static int109common_crypt(char *key, char *buf, unsigned len, unsigned mode,110struct desparams *desp)111{112int desdev;113114if ((len % 8) != 0 || len > DES_MAXDATA) {115return(DESERR_BADPARAM);116}117desp->des_dir =118((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;119120desdev = mode & DES_DEVMASK;121COPY8(key, desp->des_key);122/*123* software124*/125if (__des_crypt_LOCAL != NULL) {126if (!__des_crypt_LOCAL(buf, len, desp)) {127return (DESERR_HWERROR);128}129} else {130if (!_des_crypt_call(buf, len, desp)) {131return (DESERR_HWERROR);132}133}134return(desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);135}136137138