Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / des-compat.c
bloba74386a5f449c47cb5e9d009497c030fbd33d1d0
1 /* des-compat.h
3 * The des block cipher, libdes/openssl-style interface.
4 */
6 /* nettle, low-level cryptographics library
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.
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,
23 * MA 02111-1301, USA.
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <string.h>
31 #include <assert.h>
33 #include "des-compat.h"
35 #include "cbc.h"
36 #include "macros.h"
37 #include "memxor.h"
39 struct des_compat_des3 { const struct des_ctx *keys[3]; };
41 static void
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);
50 static void
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);
59 void
60 des_ecb3_encrypt(const_des_cblock *src, des_cblock *dst,
61 des_key_schedule k1,
62 des_key_schedule k2,
63 des_key_schedule k3, int enc)
65 struct des_compat_des3 keys;
66 keys.keys[0] = k1;
67 keys.keys[1] = k2;
68 keys.keys[2] = k3;
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. */
78 uint32_t
79 des_cbc_cksum(const uint8_t *src, des_cblock *dst,
80 long length, des_key_schedule ctx,
81 const_des_cblock *iv)
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
85 * modified. */
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;
98 if (length > 0)
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);
108 void
109 des_ncbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
110 des_key_schedule ctx, des_cblock *iv,
111 int enc)
113 switch (enc)
115 case DES_ENCRYPT:
116 nettle_cbc_encrypt(ctx, (nettle_crypt_func *) des_encrypt,
117 DES_BLOCK_SIZE, *iv,
118 length, *dst, *src);
119 break;
120 case DES_DECRYPT:
121 nettle_cbc_decrypt(ctx,
122 (nettle_crypt_func *) des_decrypt,
123 DES_BLOCK_SIZE, *iv,
124 length, *dst, *src);
125 break;
126 default:
127 abort();
131 void
132 des_cbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
133 des_key_schedule ctx, const_des_cblock *civ,
134 int enc)
136 des_cblock iv;
138 memcpy(iv, civ, DES_BLOCK_SIZE);
140 des_ncbc_encrypt(src, dst, length, ctx, &iv, enc);
144 void
145 des_ecb_encrypt(const_des_cblock *src, des_cblock *dst,
146 des_key_schedule ctx,
147 int enc)
149 ((enc == DES_ENCRYPT) ? nettle_des_encrypt : nettle_des_decrypt)
150 (ctx, DES_BLOCK_SIZE, *dst, *src);
153 void
154 des_ede3_cbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
155 des_key_schedule k1,
156 des_key_schedule k2,
157 des_key_schedule k3,
158 des_cblock *iv,
159 int enc)
161 struct des_compat_des3 keys;
162 keys.keys[0] = k1;
163 keys.keys[1] = k2;
164 keys.keys[2] = k3;
166 switch (enc)
168 case DES_ENCRYPT:
169 nettle_cbc_encrypt(&keys, (nettle_crypt_func *) des_compat_des3_encrypt,
170 DES_BLOCK_SIZE, *iv,
171 length, *dst, *src);
172 break;
173 case DES_DECRYPT:
174 nettle_cbc_decrypt(&keys, (nettle_crypt_func *) des_compat_des3_decrypt,
175 DES_BLOCK_SIZE, *iv,
176 length, *dst, *src);
177 break;
178 default:
179 abort();
184 des_set_odd_parity(des_cblock *key)
186 nettle_des_fix_parity(DES_KEY_SIZE, *key, *key);
188 /* FIXME: What to return? */
189 return 0;
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))
206 /* Bad parity */
207 return -1;
209 if (!nettle_des_set_key(ctx, *key) && des_check_key)
210 /* Weak key */
211 return -2;
213 return 0;
217 des_is_weak_key(const_des_cblock *key)
219 struct des_ctx ctx;
221 return !nettle_des_set_key(&ctx, *key);