des-compat.h 4.89 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* des-compat.h
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

   The des block cipher, old libdes/openssl-style interface.

   Copyright (C) 2001 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/
Niels Möller's avatar
Niels Möller committed
33 34 35 36 37 38 39 40

#ifndef NETTLE_DES_COMPAT_H_INCLUDED
#define NETTLE_DES_COMPAT_H_INCLUDED

/* According to Assar, des_set_key, des_set_key_odd_parity,
 * des_is_weak_key, plus the encryption functions (des_*_encrypt and
 * des_cbc_cksum) would be a pretty useful subset. */

Niels Möller's avatar
Niels Möller committed
41 42 43
/* NOTE: This is quite experimental, and not all functions are
 * implemented. Contributions, in particular test cases are welcome. */

44 45
#include "des.h"

Niels Möller's avatar
Niels Möller committed
46 47 48 49
#ifdef __cplusplus
extern "C" {
#endif

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/* We use some name mangling, to avoid collisions with either other
 * nettle functions or with libcrypto. */

#define des_ecb3_encrypt nettle_openssl_des_ecb3_encrypt
#define des_cbc_cksum nettle_openssl_des_cbc_cksum
#define des_ncbc_encrypt nettle_openssl_des_ncbc_encrypt
#define des_cbc_encrypt nettle_openssl_des_cbc_encrypt
#define des_ecb_encrypt nettle_openssl_des_ecb_encrypt
#define des_ede3_cbc_encrypt nettle_openssl_des_ede3_cbc_encrypt
#define des_set_odd_parity nettle_openssl_des_set_odd_parity
#define des_check_key nettle_openssl_des_check_key
#define des_key_sched nettle_openssl_des_key_sched
#define des_is_weak_key nettle_openssl_des_is_weak_key

/* An extra alias */
65
#undef des_set_key
66
#define des_set_key nettle_openssl_des_key_sched
67 68 69

enum { DES_DECRYPT = 0, DES_ENCRYPT = 1 };

70 71 72
/* Types */
typedef uint32_t DES_LONG;

73 74
/* Note: Typedef:ed arrays should be avoided, but they're used here
 * for compatibility. */
75 76 77
typedef struct des_ctx des_key_schedule[1];

typedef uint8_t des_cblock[DES_BLOCK_SIZE];
78 79 80 81 82 83 84 85
/* Note: The proper definition,

     typedef const uint8_t const_des_cblock[DES_BLOCK_SIZE];

   would have worked, *if* all the prototypes had used arguments like
   foo(const_des_cblock src, des_cblock dst), letting argument arrays
   "decay" into pointers of type uint8_t * and const uint8_t *.

Niels Möller's avatar
Niels Möller committed
86
   But since openssl's prototypes use *pointers* const_des_cblock *src,
87 88 89 90
   des_cblock *dst, this ends up in type conflicts, and the workaround
   is to not use const at all.
*/
#define const_des_cblock des_cblock
91 92 93 94 95 96 97 98 99 100 101 102

/* Aliases */
#define des_ecb2_encrypt(i,o,k1,k2,e) \
	des_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e))

#define des_ede2_cbc_encrypt(i,o,l,k1,k2,iv,e) \
	des_ede3_cbc_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(e))

/* Global flag */
extern int des_check_key;

/* Prototypes */
103 104 105 106 107 108 109 110 111

/* Typing is a little confusing. Since both des_cblock and
   des_key_schedule are typedef:ed arrays, it automatically decay to
   a pointers.

   But the functions are declared taking pointers to des_cblock, i.e.
   pointers to arrays. And on the other hand, they take plain
   des_key_schedule arguments, which is equivalent to pointers to
   struct des_ctx.  */
Niels Möller's avatar
Niels Möller committed
112
void
113
des_ecb3_encrypt(const_des_cblock *src, des_cblock *dst,
114 115 116
		 des_key_schedule k1,
		 des_key_schedule k2,
		 des_key_schedule k3, int enc);
117

118 119 120
/* des_cbc_cksum in libdes returns a 32 bit integer, representing the
 * latter half of the output block, using little endian byte order. */
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 128
des_cbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
		des_key_schedule ctx, const_des_cblock *iv,
129 130
		int enc);

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 144 145
		     des_key_schedule k1,
		     des_key_schedule k2,
		     des_key_schedule k3,
146
		     des_cblock *iv,
147 148 149
		     int enc);

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

Niels Möller's avatar
Niels Möller committed
158 159 160 161
#ifdef __cplusplus
}
#endif

Niels Möller's avatar
Niels Möller committed
162
#endif /* NETTLE_DES_COMPAT_H_INCLUDED */