3 * The des block cipher, libdes/openssl-style interface.
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2001 Niels Möller
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston,
33 #include "des-compat.h"
39 struct des_compat_des3
{ const struct des_ctx
*keys
[3]; };
42 des_compat_des3_encrypt(struct des_compat_des3
*ctx
,
43 uint32_t length
, uint8_t *dst
, const uint8_t *src
)
45 nettle_des_encrypt(ctx
->keys
[0], length
, dst
, src
);
46 nettle_des_decrypt(ctx
->keys
[1], length
, dst
, dst
);
47 nettle_des_encrypt(ctx
->keys
[2], length
, dst
, dst
);
51 des_compat_des3_decrypt(struct des_compat_des3
*ctx
,
52 uint32_t length
, uint8_t *dst
, const uint8_t *src
)
54 nettle_des_decrypt(ctx
->keys
[2], length
, dst
, src
);
55 nettle_des_encrypt(ctx
->keys
[1], length
, dst
, dst
);
56 nettle_des_decrypt(ctx
->keys
[0], length
, dst
, dst
);
60 des_ecb3_encrypt(const_des_cblock
*src
, des_cblock
*dst
,
63 des_key_schedule k3
, int enc
)
65 struct des_compat_des3 keys
;
70 ((enc
== DES_ENCRYPT
) ? des_compat_des3_encrypt
: des_compat_des3_decrypt
)
71 (&keys
, DES_BLOCK_SIZE
, *dst
, *src
);
74 /* If input is not a integral number of blocks, the final block is
75 padded with zeros, no length field or anything like that. That's
76 pretty broken, since it means that "$100" and "$100\0" always have
77 the same checksum, but I think that's how it's supposed to work. */
79 des_cbc_cksum(const uint8_t *src
, des_cblock
*dst
,
80 long length
, des_key_schedule ctx
,
83 /* FIXME: I'm not entirely sure how this function is supposed to
84 * work, in particular what it should return, and if iv can be
86 uint8_t block
[DES_BLOCK_SIZE
];
88 memcpy(block
, *iv
, DES_BLOCK_SIZE
);
90 while (length
>= DES_BLOCK_SIZE
)
92 memxor(block
, src
, DES_BLOCK_SIZE
);
93 nettle_des_encrypt(ctx
, DES_BLOCK_SIZE
, block
, block
);
95 src
+= DES_BLOCK_SIZE
;
96 length
-= DES_BLOCK_SIZE
;
100 memxor(block
, src
, length
);
101 nettle_des_encrypt(ctx
, DES_BLOCK_SIZE
, block
, block
);
103 memcpy(*dst
, block
, DES_BLOCK_SIZE
);
105 return LE_READ_UINT32(block
+ 4);
109 des_ncbc_encrypt(const_des_cblock
*src
, des_cblock
*dst
, long length
,
110 des_key_schedule ctx
, des_cblock
*iv
,
116 nettle_cbc_encrypt(ctx
, (nettle_crypt_func
*) des_encrypt
,
121 nettle_cbc_decrypt(ctx
,
122 (nettle_crypt_func
*) des_decrypt
,
132 des_cbc_encrypt(const_des_cblock
*src
, des_cblock
*dst
, long length
,
133 des_key_schedule ctx
, const_des_cblock
*civ
,
138 memcpy(iv
, civ
, DES_BLOCK_SIZE
);
140 des_ncbc_encrypt(src
, dst
, length
, ctx
, &iv
, enc
);
145 des_ecb_encrypt(const_des_cblock
*src
, des_cblock
*dst
,
146 des_key_schedule ctx
,
149 ((enc
== DES_ENCRYPT
) ? nettle_des_encrypt
: nettle_des_decrypt
)
150 (ctx
, DES_BLOCK_SIZE
, *dst
, *src
);
154 des_ede3_cbc_encrypt(const_des_cblock
*src
, des_cblock
*dst
, long length
,
161 struct des_compat_des3 keys
;
169 nettle_cbc_encrypt(&keys
, (nettle_crypt_func
*) des_compat_des3_encrypt
,
174 nettle_cbc_decrypt(&keys
, (nettle_crypt_func
*) des_compat_des3_decrypt
,
184 des_set_odd_parity(des_cblock
*key
)
186 nettle_des_fix_parity(DES_KEY_SIZE
, *key
, *key
);
188 /* FIXME: What to return? */
193 /* If des_check_key is non-zero, returns
195 * 0 for ok, -1 for bad parity, and -2 for weak keys.
197 * If des_check_key is zero (the default), always returns zero.
200 int des_check_key
= 0;
203 des_key_sched(const_des_cblock
*key
, des_key_schedule ctx
)
205 if (des_check_key
&& !des_check_parity (DES_KEY_SIZE
, *key
))
209 if (!nettle_des_set_key(ctx
, *key
) && des_check_key
)
217 des_is_weak_key(const_des_cblock
*key
)
221 return !nettle_des_set_key(&ctx
, *key
);