des-compat.h 4.77 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2 3 4 5 6 7
/* des-compat.h
 *
 * The des block cipher, libdes/openssl-style interface.
 */

/* nettle, low-level cryptographics library
 *
Niels Möller's avatar
Niels Möller committed
8
 * Copyright (C) 2001 Niels Möller
Niels Möller's avatar
Niels Möller committed
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 nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * The nettle library 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 Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

#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
33 34 35
/* NOTE: This is quite experimental, and not all functions are
 * implemented. Contributions, in particular test cases are welcome. */

36 37
#include "des.h"

Niels Möller's avatar
Niels Möller committed
38 39 40 41
#ifdef __cplusplus
extern "C" {
#endif

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/* 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 */
57
#undef des_set_key
58
#define des_set_key nettle_openssl_des_key_sched
59 60 61

enum { DES_DECRYPT = 0, DES_ENCRYPT = 1 };

62 63 64
/* Types */
typedef uint32_t DES_LONG;

65 66
/* Note: Typedef:ed arrays should be avoided, but they're used here
 * for compatibility. */
67 68 69
typedef struct des_ctx des_key_schedule[1];

typedef uint8_t des_cblock[DES_BLOCK_SIZE];
70 71 72 73 74 75 76 77
/* 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
78
   But since openssl's prototypes use *pointers* const_des_cblock *src,
79 80 81 82
   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
83 84 85 86 87 88 89 90 91 92 93 94

/* 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 */
95 96 97 98 99 100 101 102 103

/* 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
104
void
105
des_ecb3_encrypt(const_des_cblock *src, des_cblock *dst,
106 107 108
		 des_key_schedule k1,
		 des_key_schedule k2,
		 des_key_schedule k3, int enc);
109

110 111 112
/* 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
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 120
des_cbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
		des_key_schedule ctx, const_des_cblock *iv,
121 122
		int enc);

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 136 137
		     des_key_schedule k1,
		     des_key_schedule k2,
		     des_key_schedule k3,
138
		     des_cblock *iv,
139 140 141
		     int enc);

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

Niels Möller's avatar
Niels Möller committed
150 151 152 153
#ifdef __cplusplus
}
#endif

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