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