Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / des-compat.c
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., 51 Franklin Street, Fifth Floor, Boston,
23  * MA 02111-1301, USA.
24  */
25
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <string.h>
31 #include <assert.h>
32
33 #include "des-compat.h"
34
35 #include "cbc.h"
36 #include "macros.h"
37 #include "memxor.h"
38
39 struct des_compat_des3 { const struct des_ctx *keys[3]; }; 
40
41 static void
42 des_compat_des3_encrypt(struct des_compat_des3 *ctx,
43                         uint32_t length, uint8_t *dst, const uint8_t *src)
44 {
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);
48 }
49
50 static void
51 des_compat_des3_decrypt(struct des_compat_des3 *ctx,
52                         uint32_t length, uint8_t *dst, const uint8_t *src)
53 {
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);
57 }
58
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)
64 {
65   struct des_compat_des3 keys;
66   keys.keys[0] = k1;
67   keys.keys[1] = k2;
68   keys.keys[2] = k3;
69
70   ((enc == DES_ENCRYPT) ? des_compat_des3_encrypt : des_compat_des3_decrypt)
71     (&keys, DES_BLOCK_SIZE, *dst, *src);
72 }
73
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)
82 {
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];
87
88   memcpy(block, *iv, DES_BLOCK_SIZE);
89
90   while (length >= DES_BLOCK_SIZE)
91     {
92       memxor(block, src, DES_BLOCK_SIZE);
93       nettle_des_encrypt(ctx, DES_BLOCK_SIZE, block, block);
94
95       src += DES_BLOCK_SIZE;
96       length -= DES_BLOCK_SIZE;   
97     }
98   if (length > 0)
99     {
100       memxor(block, src, length);
101       nettle_des_encrypt(ctx, DES_BLOCK_SIZE, block, block);      
102     }
103   memcpy(*dst, block, DES_BLOCK_SIZE);
104
105   return LE_READ_UINT32(block + 4);
106 }
107
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)
112 {
113   switch (enc)
114     {
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();
128     }
129 }
130
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)
135 {
136   des_cblock iv;
137
138   memcpy(iv, civ, DES_BLOCK_SIZE);
139
140   des_ncbc_encrypt(src, dst, length, ctx, &iv, enc);
141 }
142
143
144 void
145 des_ecb_encrypt(const_des_cblock *src, des_cblock *dst,
146                 des_key_schedule ctx,
147                 int enc)
148 {
149   ((enc == DES_ENCRYPT) ? nettle_des_encrypt : nettle_des_decrypt)
150     (ctx, DES_BLOCK_SIZE, *dst, *src);
151 }
152
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)
160 {
161   struct des_compat_des3 keys;
162   keys.keys[0] = k1;
163   keys.keys[1] = k2;
164   keys.keys[2] = k3;
165
166   switch (enc)
167     {
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();
180     }
181 }
182
183 int
184 des_set_odd_parity(des_cblock *key)
185 {
186   nettle_des_fix_parity(DES_KEY_SIZE, *key, *key);
187
188   /* FIXME: What to return? */
189   return 0;
190 }
191
192
193 /* If des_check_key is non-zero, returns
194  *
195  *   0 for ok, -1 for bad parity, and -2 for weak keys.
196  *
197  * If des_check_key is zero (the default), always returns zero.
198  */
199
200 int des_check_key = 0;
201
202 int
203 des_key_sched(const_des_cblock *key, des_key_schedule ctx)
204 {
205   if (des_check_key && !des_check_parity (DES_KEY_SIZE, *key))
206     /* Bad parity */
207     return -1;
208   
209   if (!nettle_des_set_key(ctx, *key) && des_check_key)
210     /* Weak key */
211     return -2;
212
213   return 0;
214 }
215
216 int
217 des_is_weak_key(const_des_cblock *key)
218 {
219   struct des_ctx ctx;
220
221   return !nettle_des_set_key(&ctx, *key);
222 }