des-compat.c 5.46 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* des-compat.h
 *
 * The des block cipher, libdes/openssl-style interface.
 */

/* nettle, low-level cryptographics library
 *
 * Copyright (C) 2001 Niels Möller
 *  
 * 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.
 */

26 27 28 29 30 31 32
#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <string.h>
#include <assert.h>

Niels Möller's avatar
Niels Möller committed
33 34 35
#include "des-compat.h"

#include "cbc.h"
36
#include "macros.h"
Niels Möller's avatar
Niels Möller committed
37 38
#include "memxor.h"

39
struct des_compat_des3 { const struct des_ctx *keys[3]; }; 
40 41 42 43 44

static void
des_compat_des3_encrypt(struct des_compat_des3 *ctx,
			uint32_t length, uint8_t *dst, const uint8_t *src)
{
45 46 47
  nettle_des_encrypt(ctx->keys[0], length, dst, src);
  nettle_des_decrypt(ctx->keys[1], length, dst, dst);
  nettle_des_encrypt(ctx->keys[2], length, dst, dst);
48 49 50 51 52
}

static void
des_compat_des3_decrypt(struct des_compat_des3 *ctx,
			uint32_t length, uint8_t *dst, const uint8_t *src)
Niels Möller's avatar
Niels Möller committed
53
{
54 55 56
  nettle_des_decrypt(ctx->keys[2], length, dst, src);
  nettle_des_encrypt(ctx->keys[1], length, dst, dst);
  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 62 63
		 des_key_schedule k1,
		 des_key_schedule k2,
		 des_key_schedule k3, int enc)
64
{
65 66 67 68
  struct des_compat_des3 keys;
  keys.keys[0] = k1;
  keys.keys[1] = k2;
  keys.keys[2] = k3;
69 70

  ((enc == DES_ENCRYPT) ? des_compat_des3_encrypt : des_compat_des3_decrypt)
71
    (&keys, DES_BLOCK_SIZE, *dst, *src);
Niels Möller's avatar
Niels Möller committed
72 73
}

74
uint32_t
75
des_cbc_cksum(const uint8_t *src, des_cblock *dst,
76
	      long length, des_key_schedule ctx,
77
	      const_des_cblock *iv)
Niels Möller's avatar
Niels Möller committed
78 79 80 81 82
{
  /* FIXME: I'm not entirely sure how this function is supposed to
   * work, in particular what it should return, and if iv can be
   * modified. */
  uint8_t block[DES_BLOCK_SIZE];
83
  const uint8_t *p;
84 85

  memcpy(block, *iv, DES_BLOCK_SIZE);
86
  
Niels Möller's avatar
Niels Möller committed
87 88
  assert(!(length % DES_BLOCK_SIZE));
  
89
  for (p = src; length; length -= DES_BLOCK_SIZE, p += DES_BLOCK_SIZE)
Niels Möller's avatar
Niels Möller committed
90
    {
91
      memxor(block, p, DES_BLOCK_SIZE);
92
      nettle_des_encrypt(ctx, DES_BLOCK_SIZE, block, block);
Niels Möller's avatar
Niels Möller committed
93
    }
94
  memcpy(*dst, block, DES_BLOCK_SIZE);
95 96

  return LE_READ_UINT32(block + 4);
Niels Möller's avatar
Niels Möller committed
97 98
}

99
void
100
des_ncbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
101
                 des_key_schedule ctx, des_cblock *iv,
102 103 104 105 106
                 int enc)
{
  switch (enc)
    {
    case DES_ENCRYPT:
107
      nettle_cbc_encrypt(ctx, (nettle_crypt_func) des_encrypt,
108 109
			 DES_BLOCK_SIZE, *iv,
			 length, *dst, *src);
110 111
      break;
    case DES_DECRYPT:
112
      nettle_cbc_decrypt(ctx,
113
			 (nettle_crypt_func) des_decrypt,
114 115
			 DES_BLOCK_SIZE, *iv,
			 length, *dst, *src);
116 117 118 119 120
      break;
    default:
      abort();
    }
}
Niels Möller's avatar
Niels Möller committed
121 122

void
123 124
des_cbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
		des_key_schedule ctx, const_des_cblock *civ,
Niels Möller's avatar
Niels Möller committed
125 126
		int enc)
{
127
  des_cblock iv;
128 129

  memcpy(iv, civ, DES_BLOCK_SIZE);
Niels Möller's avatar
Niels Möller committed
130

131
  des_ncbc_encrypt(src, dst, length, ctx, &iv, enc);
132
}
Niels Möller's avatar
Niels Möller committed
133

134

Niels Möller's avatar
Niels Möller committed
135
void
136
des_ecb_encrypt(const_des_cblock *src, des_cblock *dst,
137
		des_key_schedule ctx,
Niels Möller's avatar
Niels Möller committed
138 139
		int enc)
{
140 141
  ((enc == DES_ENCRYPT) ? nettle_des_encrypt : nettle_des_decrypt)
    (ctx, DES_BLOCK_SIZE, *dst, *src);
Niels Möller's avatar
Niels Möller committed
142
}
143

Niels Möller's avatar
Niels Möller committed
144
void
145
des_ede3_cbc_encrypt(const_des_cblock *src, des_cblock *dst, long length,
146 147 148
		     des_key_schedule k1,
		     des_key_schedule k2,
		     des_key_schedule k3,
149
		     des_cblock *iv,
150 151
		     int enc)
{
152 153 154 155
  struct des_compat_des3 keys;
  keys.keys[0] = k1;
  keys.keys[1] = k2;
  keys.keys[2] = k3;
156

157 158 159
  switch (enc)
    {
    case DES_ENCRYPT:
160
      nettle_cbc_encrypt(&keys, (nettle_crypt_func) des_compat_des3_encrypt,
161 162
			 DES_BLOCK_SIZE, *iv,
			 length, *dst, *src);
163 164
      break;
    case DES_DECRYPT:
165
      nettle_cbc_decrypt(&keys, (nettle_crypt_func) des_compat_des3_decrypt,
166 167
			 DES_BLOCK_SIZE, *iv,
			 length, *dst, *src);
168 169 170 171
      break;
    default:
      abort();
    }
172
}
Niels Möller's avatar
Niels Möller committed
173 174

int
175
des_set_odd_parity(des_cblock *key)
176
{
177
  nettle_des_fix_parity(DES_KEY_SIZE, *key, *key);
Niels Möller's avatar
Niels Möller committed
178 179 180

  /* FIXME: What to return? */
  return 0;
181
}
Niels Möller's avatar
Niels Möller committed
182

183 184 185 186 187 188 189 190 191 192

/* If des_check_key is non-zero, returns
 *
 *   0 for ok, -1 for bad parity, and -2 for weak keys.
 *
 * If des_check_key is zero (the default), always returns zero.
 */

int des_check_key = 0;

Niels Möller's avatar
Niels Möller committed
193
int
194
des_key_sched(const_des_cblock *key, des_key_schedule ctx)
195
{
196 197
  des_cblock nkey;
  const uint8_t *pkey;
198
  
199 200 201 202 203 204 205 206 207 208
  if (des_check_key)
    pkey = *key;
  else
    {
      /* Fix the parity */
      nettle_des_fix_parity(DES_KEY_SIZE, nkey, *key);
      pkey = nkey;
    }
  
  if (nettle_des_set_key(ctx, pkey))
Niels Möller's avatar
Niels Möller committed
209 210 211 212
    return 0;
  else switch(ctx->status)
    {
    case DES_BAD_PARITY:
213 214 215 216 217
      if (des_check_key)
        return -1;
      else
        /* We fixed the parity above */
        abort();
Niels Möller's avatar
Niels Möller committed
218
    case DES_WEAK_KEY:
219 220 221 222 223 224 225
      if (des_check_key)
        return -2;

      /* Pretend the key was good */
      ctx->status = DES_OK;
      return 0;
      
Niels Möller's avatar
Niels Möller committed
226 227 228
    default:
      abort();
    }
229
}
Niels Möller's avatar
Niels Möller committed
230 231

int
232
des_is_weak_key(const_des_cblock *key)
233 234 235
{
  struct des_ctx ctx;

236
  return !nettle_des_set_key(&ctx, *key);
237
}