bda4e7595d47986f03c189cdf0c4a069551a99a1
[platform/upstream/nettle.git] / des-compat.h
1 /* des-compat.h
2
3    The des block cipher, old libdes/openssl-style interface.
4
5    Copyright (C) 2001 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
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.
15
16    or
17
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.
21
22    or both in parallel, as here.
23
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.
28
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/.
32 */
33
34 #ifndef NETTLE_DES_COMPAT_H_INCLUDED
35 #define NETTLE_DES_COMPAT_H_INCLUDED
36
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. */
40
41 /* NOTE: This is quite experimental, and not all functions are
42  * implemented. Contributions, in particular test cases are welcome. */
43
44 #include "des.h"
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 /* We use some name mangling, to avoid collisions with either other
51  * nettle functions or with libcrypto. */
52
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
63
64 /* An extra alias */
65 #undef des_set_key
66 #define des_set_key nettle_openssl_des_key_sched
67
68 enum { DES_DECRYPT = 0, DES_ENCRYPT = 1 };
69
70 /* Types */
71 typedef uint32_t DES_LONG;
72
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];
76
77 typedef uint8_t des_cblock[DES_BLOCK_SIZE];
78 /* Note: The proper definition,
79
80      typedef const uint8_t const_des_cblock[DES_BLOCK_SIZE];
81
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 *.
85
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.
89 */
90 #define const_des_cblock des_cblock
91
92 /* Aliases */
93 #define des_ecb2_encrypt(i,o,k1,k2,e) \
94         des_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e))
95
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))
98
99 /* Global flag */
100 extern int des_check_key;
101
102 /* Prototypes */
103
104 /* Typing is a little confusing. Since both des_cblock and
105    des_key_schedule are typedef:ed arrays, it automatically decay to
106    a pointers.
107
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
111    struct des_ctx.  */
112 void
113 des_ecb3_encrypt(const_des_cblock *src, des_cblock *dst,
114                  des_key_schedule k1,
115                  des_key_schedule k2,
116                  des_key_schedule k3, int enc);
117
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. */
120 uint32_t
121 des_cbc_cksum(const uint8_t *src, des_cblock *dst,
122               long length, des_key_schedule ctx,
123               const_des_cblock *iv);
124
125 /* NOTE: Doesn't update iv. */
126 void
127 des_cbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
128                 des_key_schedule ctx, const_des_cblock *iv,
129                 int enc);
130
131 /* Similar, but updates iv. */
132 void
133 des_ncbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
134                  des_key_schedule ctx, des_cblock *iv,
135                  int enc);
136
137 void
138 des_ecb_encrypt(const_des_cblock *src, des_cblock *dst,
139                 des_key_schedule ctx, int enc);
140
141 void
142 des_ede3_cbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
143                      des_key_schedule k1,
144                      des_key_schedule k2,
145                      des_key_schedule k3,
146                      des_cblock *iv,
147                      int enc);
148
149 int
150 des_set_odd_parity(des_cblock *key);
151
152 int
153 des_key_sched(const_des_cblock *key, des_key_schedule ctx);
154
155 int
156 des_is_weak_key(const_des_cblock *key);
157
158 #ifdef __cplusplus
159 }
160 #endif
161
162 #endif /* NETTLE_DES_COMPAT_H_INCLUDED */