1*515aa502Stb /* $OpenBSD: des.h,v 1.23 2025/01/25 17:59:44 tb Exp $ */ 2913ec974Sbeck /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) 3913ec974Sbeck * All rights reserved. 4913ec974Sbeck * 5913ec974Sbeck * This package is an SSL implementation written 6913ec974Sbeck * by Eric Young (eay@cryptsoft.com). 7913ec974Sbeck * The implementation was written so as to conform with Netscapes SSL. 8913ec974Sbeck * 9913ec974Sbeck * This library is free for commercial and non-commercial use as long as 10913ec974Sbeck * the following conditions are aheared to. The following conditions 11913ec974Sbeck * apply to all code found in this distribution, be it the RC4, RSA, 12913ec974Sbeck * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13913ec974Sbeck * included with this distribution is covered by the same copyright terms 14913ec974Sbeck * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15913ec974Sbeck * 16913ec974Sbeck * Copyright remains Eric Young's, and as such any Copyright notices in 17913ec974Sbeck * the code are not to be removed. 18913ec974Sbeck * If this package is used in a product, Eric Young should be given attribution 19913ec974Sbeck * as the author of the parts of the library used. 20913ec974Sbeck * This can be in the form of a textual message at program startup or 21913ec974Sbeck * in documentation (online or textual) provided with the package. 22913ec974Sbeck * 23913ec974Sbeck * Redistribution and use in source and binary forms, with or without 24913ec974Sbeck * modification, are permitted provided that the following conditions 25913ec974Sbeck * are met: 26913ec974Sbeck * 1. Redistributions of source code must retain the copyright 27913ec974Sbeck * notice, this list of conditions and the following disclaimer. 28913ec974Sbeck * 2. Redistributions in binary form must reproduce the above copyright 29913ec974Sbeck * notice, this list of conditions and the following disclaimer in the 30913ec974Sbeck * documentation and/or other materials provided with the distribution. 31913ec974Sbeck * 3. All advertising materials mentioning features or use of this software 32913ec974Sbeck * must display the following acknowledgement: 33913ec974Sbeck * "This product includes cryptographic software written by 34913ec974Sbeck * Eric Young (eay@cryptsoft.com)" 35913ec974Sbeck * The word 'cryptographic' can be left out if the rouines from the library 36913ec974Sbeck * being used are not cryptographic related :-). 37913ec974Sbeck * 4. If you include any Windows specific code (or a derivative thereof) from 38913ec974Sbeck * the apps directory (application code) you must include an acknowledgement: 39913ec974Sbeck * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40913ec974Sbeck * 41913ec974Sbeck * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42913ec974Sbeck * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43913ec974Sbeck * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44913ec974Sbeck * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45913ec974Sbeck * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46913ec974Sbeck * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47913ec974Sbeck * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48913ec974Sbeck * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49913ec974Sbeck * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50913ec974Sbeck * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51913ec974Sbeck * SUCH DAMAGE. 52913ec974Sbeck * 53913ec974Sbeck * The licence and distribution terms for any publically available version or 54913ec974Sbeck * derivative of this code cannot be changed. i.e. this code cannot simply be 55913ec974Sbeck * copied and put under another distribution licence 56913ec974Sbeck * [including the GNU Public Licence.] 57913ec974Sbeck */ 58913ec974Sbeck 5997222eddSmiod #ifndef HEADER_NEW_DES_H 6097222eddSmiod #define HEADER_NEW_DES_H 61913ec974Sbeck 62b1f314f8Sjsing #include <openssl/opensslconf.h> 63b1f314f8Sjsing 64c109e398Sbeck #ifdef __cplusplus 65c109e398Sbeck extern "C" { 66c109e398Sbeck #endif 67c109e398Sbeck 68da347917Sbeck typedef unsigned char DES_cblock[8]; 69da347917Sbeck typedef /* const */ unsigned char const_DES_cblock[8]; 70da347917Sbeck /* With "const", gcc 2.8.1 on Solaris thinks that DES_cblock * 71da347917Sbeck * and const_DES_cblock * are incompatible pointer types. */ 72913ec974Sbeck 73aad5d5cdSbeck typedef struct DES_ks { 74aad5d5cdSbeck union { 75da347917Sbeck DES_cblock cblock; 76913ec974Sbeck /* make sure things are correct size on machines with 77913ec974Sbeck * 8 byte longs */ 78913ec974Sbeck DES_LONG deslong[2]; 79da347917Sbeck } ks[16]; 80da347917Sbeck } DES_key_schedule; 81913ec974Sbeck 82da347917Sbeck #define DES_KEY_SZ (sizeof(DES_cblock)) 83da347917Sbeck #define DES_SCHEDULE_SZ (sizeof(DES_key_schedule)) 84913ec974Sbeck 85913ec974Sbeck #define DES_ENCRYPT 1 86913ec974Sbeck #define DES_DECRYPT 0 87913ec974Sbeck 88913ec974Sbeck #define DES_CBC_MODE 0 89913ec974Sbeck #define DES_PCBC_MODE 1 90913ec974Sbeck 91da347917Sbeck #define DES_ecb2_encrypt(i,o,k1,k2,e) \ 92da347917Sbeck DES_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e)) 93913ec974Sbeck 94da347917Sbeck #define DES_ede2_cbc_encrypt(i,o,l,k1,k2,iv,e) \ 95da347917Sbeck DES_ede3_cbc_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(e)) 96913ec974Sbeck 97da347917Sbeck #define DES_ede2_cfb64_encrypt(i,o,l,k1,k2,iv,n,e) \ 98da347917Sbeck DES_ede3_cfb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n),(e)) 99913ec974Sbeck 100da347917Sbeck #define DES_ede2_ofb64_encrypt(i,o,l,k1,k2,iv,n) \ 101da347917Sbeck DES_ede3_ofb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n)) 102913ec974Sbeck 1033ef976c9Sjsing extern int DES_check_key; /* defaults to false */ 104913ec974Sbeck 1054fcf65c5Sdjm void DES_ecb3_encrypt(const_DES_cblock *input, DES_cblock *output, 106da347917Sbeck DES_key_schedule *ks1, DES_key_schedule *ks2, 107da347917Sbeck DES_key_schedule *ks3, int enc); 108da347917Sbeck DES_LONG DES_cbc_cksum(const unsigned char *input, DES_cblock *output, 109da347917Sbeck long length, DES_key_schedule *schedule, 110da347917Sbeck const_DES_cblock *ivec); 111da347917Sbeck /* DES_cbc_encrypt does not update the IV! Use DES_ncbc_encrypt instead. */ 112da347917Sbeck void DES_cbc_encrypt(const unsigned char *input, unsigned char *output, 113da347917Sbeck long length, DES_key_schedule *schedule, DES_cblock *ivec, 114913ec974Sbeck int enc); 115da347917Sbeck void DES_ncbc_encrypt(const unsigned char *input, unsigned char *output, 116da347917Sbeck long length, DES_key_schedule *schedule, DES_cblock *ivec, 117913ec974Sbeck int enc); 118da347917Sbeck void DES_xcbc_encrypt(const unsigned char *input, unsigned char *output, 119da347917Sbeck long length, DES_key_schedule *schedule, DES_cblock *ivec, 120da347917Sbeck const_DES_cblock *inw, const_DES_cblock *outw, int enc); 121da347917Sbeck void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits, 122da347917Sbeck long length, DES_key_schedule *schedule, DES_cblock *ivec, 123913ec974Sbeck int enc); 124da347917Sbeck void DES_ecb_encrypt(const_DES_cblock *input, DES_cblock *output, 125da347917Sbeck DES_key_schedule *ks, int enc); 126f6e3f262Sbeck 127f6e3f262Sbeck /* This is the DES encryption function that gets called by just about 128f6e3f262Sbeck every other DES routine in the library. You should not use this 129f6e3f262Sbeck function except to implement 'modes' of DES. I say this because the 130f6e3f262Sbeck functions that call this routine do the conversion from 'char *' to 131f6e3f262Sbeck long, and this needs to be done to make sure 'non-aligned' memory 132f6e3f262Sbeck access do not occur. The characters are loaded 'little endian'. 133f6e3f262Sbeck Data is a pointer to 2 unsigned long's and ks is the 134da347917Sbeck DES_key_schedule to use. enc, is non zero specifies encryption, 135f6e3f262Sbeck zero if decryption. */ 136da347917Sbeck void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc); 137f6e3f262Sbeck 138da347917Sbeck /* This functions is the same as DES_encrypt1() except that the DES 139f6e3f262Sbeck initial permutation (IP) and final permutation (FP) have been left 140da347917Sbeck out. As for DES_encrypt1(), you should not use this function. 141f6e3f262Sbeck It is used by the routines in the library that implement triple DES. 142da347917Sbeck IP() DES_encrypt2() DES_encrypt2() DES_encrypt2() FP() is the same 143da347917Sbeck as DES_encrypt1() DES_encrypt1() DES_encrypt1() except faster :-). */ 144da347917Sbeck void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc); 145f6e3f262Sbeck 146da347917Sbeck void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1, 147da347917Sbeck DES_key_schedule *ks2, DES_key_schedule *ks3); 148da347917Sbeck void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1, 149da347917Sbeck DES_key_schedule *ks2, DES_key_schedule *ks3); 150da347917Sbeck void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output, 151913ec974Sbeck long length, 152da347917Sbeck DES_key_schedule *ks1, DES_key_schedule *ks2, 153da347917Sbeck DES_key_schedule *ks3, DES_cblock *ivec, int enc); 154da347917Sbeck void DES_ede3_cbcm_encrypt(const unsigned char *in, unsigned char *out, 155913ec974Sbeck long length, 156da347917Sbeck DES_key_schedule *ks1, DES_key_schedule *ks2, 157da347917Sbeck DES_key_schedule *ks3, 158da347917Sbeck DES_cblock *ivec1, DES_cblock *ivec2, 159913ec974Sbeck int enc); 160da347917Sbeck void DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out, 161da347917Sbeck long length, DES_key_schedule *ks1, 162da347917Sbeck DES_key_schedule *ks2, DES_key_schedule *ks3, 163da347917Sbeck DES_cblock *ivec, int *num, int enc); 16440d8aef3Sdjm void DES_ede3_cfb_encrypt(const unsigned char *in, unsigned char *out, 16540d8aef3Sdjm int numbits, long length, DES_key_schedule *ks1, 16640d8aef3Sdjm DES_key_schedule *ks2, DES_key_schedule *ks3, 16740d8aef3Sdjm DES_cblock *ivec, int enc); 168da347917Sbeck void DES_ede3_ofb64_encrypt(const unsigned char *in, unsigned char *out, 169da347917Sbeck long length, DES_key_schedule *ks1, 170da347917Sbeck DES_key_schedule *ks2, DES_key_schedule *ks3, 171da347917Sbeck DES_cblock *ivec, int *num); 172da347917Sbeck char *DES_fcrypt(const char *buf, const char *salt, char *ret); 173da347917Sbeck char *DES_crypt(const char *buf, const char *salt); 174da347917Sbeck void DES_ofb_encrypt(const unsigned char *in, unsigned char *out, int numbits, 175da347917Sbeck long length, DES_key_schedule *schedule, DES_cblock *ivec); 176da347917Sbeck void DES_pcbc_encrypt(const unsigned char *input, unsigned char *output, 177da347917Sbeck long length, DES_key_schedule *schedule, DES_cblock *ivec, 178913ec974Sbeck int enc); 179da347917Sbeck DES_LONG DES_quad_cksum(const unsigned char *input, DES_cblock output[], 180da347917Sbeck long length, int out_count, DES_cblock *seed); 181da347917Sbeck int DES_random_key(DES_cblock *ret); 182da347917Sbeck void DES_set_odd_parity(DES_cblock *key); 183da347917Sbeck int DES_check_key_parity(const_DES_cblock *key); 184da347917Sbeck int DES_is_weak_key(const_DES_cblock *key); 185da347917Sbeck /* DES_set_key (= set_key = DES_key_sched = key_sched) calls 186da347917Sbeck * DES_set_key_checked if global variable DES_check_key is set, 187da347917Sbeck * DES_set_key_unchecked otherwise. */ 188da347917Sbeck int DES_set_key(const_DES_cblock *key, DES_key_schedule *schedule); 189da347917Sbeck int DES_key_sched(const_DES_cblock *key, DES_key_schedule *schedule); 190da347917Sbeck int DES_set_key_checked(const_DES_cblock *key, DES_key_schedule *schedule); 191da347917Sbeck void DES_set_key_unchecked(const_DES_cblock *key, DES_key_schedule *schedule); 192da347917Sbeck void DES_string_to_key(const char *str, DES_cblock *key); 193da347917Sbeck void DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2); 194da347917Sbeck void DES_cfb64_encrypt(const unsigned char *in, unsigned char *out, long length, 195da347917Sbeck DES_key_schedule *schedule, DES_cblock *ivec, int *num, 196913ec974Sbeck int enc); 197da347917Sbeck void DES_ofb64_encrypt(const unsigned char *in, unsigned char *out, long length, 198da347917Sbeck DES_key_schedule *schedule, DES_cblock *ivec, int *num); 199913ec974Sbeck 200da347917Sbeck #define DES_fixup_key_parity DES_set_odd_parity 201913ec974Sbeck 202913ec974Sbeck #ifdef __cplusplus 203913ec974Sbeck } 204913ec974Sbeck #endif 205913ec974Sbeck 206913ec974Sbeck #endif 207