3 The des block cipher, old libdes/openssl-style interface.
5 Copyright (C) 2001 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
34 #ifndef NETTLE_DES_COMPAT_H_INCLUDED
35 #define NETTLE_DES_COMPAT_H_INCLUDED
37 /* According to Assar, des_set_key, des_set_key_odd_parity,
38 * des_is_weak_key, plus the encryption functions (des_*_encrypt and
39 * des_cbc_cksum) would be a pretty useful subset. */
41 /* NOTE: This is quite experimental, and not all functions are
42 * implemented. Contributions, in particular test cases are welcome. */
50 /* We use some name mangling, to avoid collisions with either other
51 * nettle functions or with libcrypto. */
53 #define des_ecb3_encrypt nettle_openssl_des_ecb3_encrypt
54 #define des_cbc_cksum nettle_openssl_des_cbc_cksum
55 #define des_ncbc_encrypt nettle_openssl_des_ncbc_encrypt
56 #define des_cbc_encrypt nettle_openssl_des_cbc_encrypt
57 #define des_ecb_encrypt nettle_openssl_des_ecb_encrypt
58 #define des_ede3_cbc_encrypt nettle_openssl_des_ede3_cbc_encrypt
59 #define des_set_odd_parity nettle_openssl_des_set_odd_parity
60 #define des_check_key nettle_openssl_des_check_key
61 #define des_key_sched nettle_openssl_des_key_sched
62 #define des_is_weak_key nettle_openssl_des_is_weak_key
66 #define des_set_key nettle_openssl_des_key_sched
68 enum { DES_DECRYPT = 0, DES_ENCRYPT = 1 };
71 typedef uint32_t DES_LONG;
73 /* Note: Typedef:ed arrays should be avoided, but they're used here
74 * for compatibility. */
75 typedef struct des_ctx des_key_schedule[1];
77 typedef uint8_t des_cblock[DES_BLOCK_SIZE];
78 /* Note: The proper definition,
80 typedef const uint8_t const_des_cblock[DES_BLOCK_SIZE];
82 would have worked, *if* all the prototypes had used arguments like
83 foo(const_des_cblock src, des_cblock dst), letting argument arrays
84 "decay" into pointers of type uint8_t * and const uint8_t *.
86 But since openssl's prototypes use *pointers* const_des_cblock *src,
87 des_cblock *dst, this ends up in type conflicts, and the workaround
88 is to not use const at all.
90 #define const_des_cblock des_cblock
93 #define des_ecb2_encrypt(i,o,k1,k2,e) \
94 des_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e))
96 #define des_ede2_cbc_encrypt(i,o,l,k1,k2,iv,e) \
97 des_ede3_cbc_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(e))
100 extern int des_check_key;
104 /* Typing is a little confusing. Since both des_cblock and
105 des_key_schedule are typedef:ed arrays, it automatically decay to
108 But the functions are declared taking pointers to des_cblock, i.e.
109 pointers to arrays. And on the other hand, they take plain
110 des_key_schedule arguments, which is equivalent to pointers to
113 des_ecb3_encrypt(const_des_cblock *src, des_cblock *dst,
116 des_key_schedule k3, int enc);
118 /* des_cbc_cksum in libdes returns a 32 bit integer, representing the
119 * latter half of the output block, using little endian byte order. */
121 des_cbc_cksum(const uint8_t *src, des_cblock *dst,
122 long length, des_key_schedule ctx,
123 const_des_cblock *iv);
125 /* NOTE: Doesn't update iv. */
127 des_cbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
128 des_key_schedule ctx, const_des_cblock *iv,
131 /* Similar, but updates iv. */
133 des_ncbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
134 des_key_schedule ctx, des_cblock *iv,
138 des_ecb_encrypt(const_des_cblock *src, des_cblock *dst,
139 des_key_schedule ctx, int enc);
142 des_ede3_cbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
150 des_set_odd_parity(des_cblock *key);
153 des_key_sched(const_des_cblock *key, des_key_schedule ctx);
156 des_is_weak_key(const_des_cblock *key);
162 #endif /* NETTLE_DES_COMPAT_H_INCLUDED */