00001 /* des.h --- DES cipher implementation. 00002 * Copyright (C) 2005, 2007 Free Software Foundation, Inc. 00003 * 00004 * This file is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as published 00006 * by the Free Software Foundation; either version 2.1, or (at your 00007 * option) any later version. 00008 * 00009 * This file is distributed in the hope that it will be useful, but 00010 * WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public License 00015 * along with this file; if not, write to the Free Software 00016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 * 02110-1301, USA. 00018 * 00019 */ 00020 00021 /* Adapted for gnulib by Simon Josefsson, based on Libgcrypt. */ 00022 00023 #ifndef DES_H 00024 # define DES_H 00025 00026 #include <stddef.h> 00027 #include <stdint.h> 00028 #include <stdbool.h> 00029 00030 /* 00031 * Encryption/Decryption context of DES 00032 */ 00033 typedef struct 00034 { 00035 uint32_t encrypt_subkeys[32]; 00036 uint32_t decrypt_subkeys[32]; 00037 } gl_des_ctx; 00038 00039 /* 00040 * Encryption/Decryption context of Triple-DES 00041 */ 00042 typedef struct 00043 { 00044 uint32_t encrypt_subkeys[96]; 00045 uint32_t decrypt_subkeys[96]; 00046 } gl_3des_ctx; 00047 00048 /* Check whether the 8 byte key is weak. Does not check the parity 00049 * bits of the key but simple ignore them. */ 00050 extern bool 00051 gl_des_is_weak_key (const char * key); 00052 00053 /* 00054 * DES 00055 * --- 00056 */ 00057 00058 /* Fill a DES context CTX with subkeys calculated from 64bit KEY. 00059 * Does not check parity bits, but simply ignore them. Does not check 00060 * for weak keys. */ 00061 extern void 00062 gl_des_setkey (gl_des_ctx *ctx, const char * key); 00063 00064 /* Fill a DES context CTX with subkeys calculated from 64bit KEY, with 00065 * weak key checking. Does not check parity bits, but simply ignore 00066 * them. */ 00067 extern bool 00068 gl_des_makekey (gl_des_ctx *ctx, const char * key, size_t keylen); 00069 00070 /* Electronic Codebook Mode DES encryption/decryption of data 00071 * according to 'mode'. */ 00072 extern void 00073 gl_des_ecb_crypt (gl_des_ctx *ctx, const char * from, char * to, int mode); 00074 00075 #define gl_des_ecb_encrypt(ctx, from, to) gl_des_ecb_crypt(ctx, from, to, 0) 00076 #define gl_des_ecb_decrypt(ctx, from, to) gl_des_ecb_crypt(ctx, from, to, 1) 00077 00078 /* Triple-DES 00079 * ---------- 00080 */ 00081 00082 /* Fill a Triple-DES context CTX with subkeys calculated from two 00083 * 64bit keys in KEY1 and KEY2. Does not check the parity bits of the 00084 * keys, but simply ignore them. Does not check for weak keys. */ 00085 extern void 00086 gl_3des_set2keys (gl_3des_ctx *ctx, 00087 const char * key1, 00088 const char * key2); 00089 00090 /* 00091 * Fill a Triple-DES context CTX with subkeys calculated from three 00092 * 64bit keys in KEY1, KEY2 and KEY3. Does not check the parity bits 00093 * of the keys, but simply ignore them. Does not check for weak 00094 * keys. */ 00095 extern void 00096 gl_3des_set3keys (gl_3des_ctx *ctx, 00097 const char * key1, 00098 const char * key2, 00099 const char * key3); 00100 00101 /* Fill a Triple-DES context CTX with subkeys calculated from three 00102 * concatenated 64bit keys in KEY, with weak key checking. Does not 00103 * check the parity bits of the keys, but simply ignore them. */ 00104 extern bool 00105 gl_3des_makekey (gl_3des_ctx *ctx, 00106 const char * key, 00107 size_t keylen); 00108 00109 /* Electronic Codebook Mode Triple-DES encryption/decryption of data 00110 * according to 'mode'. Sometimes this mode is named 'EDE' mode 00111 * (Encryption-Decryption-Encryption). */ 00112 extern void 00113 gl_3des_ecb_crypt (gl_3des_ctx *ctx, 00114 const char * from, 00115 char * to, 00116 int mode); 00117 00118 #define gl_3des_ecb_encrypt(ctx, from, to) gl_3des_ecb_crypt(ctx,from,to,0) 00119 #define gl_3des_ecb_decrypt(ctx, from, to) gl_3des_ecb_crypt(ctx,from,to,1) 00120 00121 #endif /* DES_H */