[Title] Add packaging/nettle.spec to build nettle on OBS system
[external/nettle.git] / des-compat.h
1 /* des-compat.h
2  *
3  * The des block cipher, libdes/openssl-style interface.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2001 Niels Möller
9  *  
10  * The nettle library is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or (at your
13  * option) any later version.
14  * 
15  * The nettle library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18  * License for more details.
19  * 
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with the nettle library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  * MA 02111-1307, USA.
24  */
25
26 #ifndef NETTLE_DES_COMPAT_H_INCLUDED
27 #define NETTLE_DES_COMPAT_H_INCLUDED
28
29 /* According to Assar, des_set_key, des_set_key_odd_parity,
30  * des_is_weak_key, plus the encryption functions (des_*_encrypt and
31  * des_cbc_cksum) would be a pretty useful subset. */
32
33 /* NOTE: This is quite experimental, and not all functions are
34  * implemented. Contributions, in particular test cases are welcome. */
35
36 #include "des.h"
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 /* We use some name mangling, to avoid collisions with either other
43  * nettle functions or with libcrypto. */
44
45 #define des_ecb3_encrypt nettle_openssl_des_ecb3_encrypt
46 #define des_cbc_cksum nettle_openssl_des_cbc_cksum
47 #define des_ncbc_encrypt nettle_openssl_des_ncbc_encrypt
48 #define des_cbc_encrypt nettle_openssl_des_cbc_encrypt
49 #define des_ecb_encrypt nettle_openssl_des_ecb_encrypt
50 #define des_ede3_cbc_encrypt nettle_openssl_des_ede3_cbc_encrypt
51 #define des_set_odd_parity nettle_openssl_des_set_odd_parity
52 #define des_check_key nettle_openssl_des_check_key
53 #define des_key_sched nettle_openssl_des_key_sched
54 #define des_is_weak_key nettle_openssl_des_is_weak_key
55
56 /* An extra alias */
57 #undef des_set_key
58 #define des_set_key nettle_openssl_des_key_sched
59
60 enum { DES_DECRYPT = 0, DES_ENCRYPT = 1 };
61
62 /* Types */
63 typedef uint32_t DES_LONG;
64
65 /* Note: Typedef:ed arrays should be avoided, but they're used here
66  * for compatibility. */
67 typedef struct des_ctx des_key_schedule[1];
68
69 typedef uint8_t des_cblock[DES_BLOCK_SIZE];
70 /* Note: The proper definition,
71
72      typedef const uint8_t const_des_cblock[DES_BLOCK_SIZE];
73
74    would have worked, *if* all the prototypes had used arguments like
75    foo(const_des_cblock src, des_cblock dst), letting argument arrays
76    "decay" into pointers of type uint8_t * and const uint8_t *.
77
78    But since openssl's prototypes use *pointers* const_des_cblock *src,
79    des_cblock *dst, this ends up in type conflicts, and the workaround
80    is to not use const at all.
81 */
82 #define const_des_cblock des_cblock
83
84 /* Aliases */
85 #define des_ecb2_encrypt(i,o,k1,k2,e) \
86         des_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e))
87
88 #define des_ede2_cbc_encrypt(i,o,l,k1,k2,iv,e) \
89         des_ede3_cbc_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(e))
90
91 /* Global flag */
92 extern int des_check_key;
93
94 /* Prototypes */
95
96 /* Typing is a little confusing. Since both des_cblock and
97    des_key_schedule are typedef:ed arrays, it automatically decay to
98    a pointers.
99
100    But the functions are declared taking pointers to des_cblock, i.e.
101    pointers to arrays. And on the other hand, they take plain
102    des_key_schedule arguments, which is equivalent to pointers to
103    struct des_ctx.  */
104 void
105 des_ecb3_encrypt(const_des_cblock *src, des_cblock *dst,
106                  des_key_schedule k1,
107                  des_key_schedule k2,
108                  des_key_schedule k3, int enc);
109
110 /* des_cbc_cksum in libdes returns a 32 bit integer, representing the
111  * latter half of the output block, using little endian byte order. */
112 uint32_t
113 des_cbc_cksum(const uint8_t *src, des_cblock *dst,
114               long length, des_key_schedule ctx,
115               const_des_cblock *iv);
116
117 /* NOTE: Doesn't update iv. */
118 void
119 des_cbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
120                 des_key_schedule ctx, const_des_cblock *iv,
121                 int enc);
122
123 /* Similar, but updates iv. */
124 void
125 des_ncbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
126                  des_key_schedule ctx, des_cblock *iv,
127                  int enc);
128
129 void
130 des_ecb_encrypt(const_des_cblock *src, des_cblock *dst,
131                 des_key_schedule ctx, int enc);
132
133 void
134 des_ede3_cbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
135                      des_key_schedule k1,
136                      des_key_schedule k2,
137                      des_key_schedule k3,
138                      des_cblock *iv,
139                      int enc);
140
141 int
142 des_set_odd_parity(des_cblock *key);
143
144 int
145 des_key_sched(const_des_cblock *key, des_key_schedule ctx);
146
147 int
148 des_is_weak_key(const_des_cblock *key);
149
150 #ifdef __cplusplus
151 }
152 #endif
153
154 #endif /* NETTLE_DES_COMPAT_H_INCLUDED */