des-compat.c 5.09 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 26 27
/* 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.
 */

#include "des-compat.h"

28 29
#undef des_set_key

Niels Möller's avatar
Niels Möller committed
30
#include "cbc.h"
31
#include "macros.h"
Niels Möller's avatar
Niels Möller committed
32 33 34 35
#include "memxor.h"

#include <string.h>
#include <assert.h>
Niels Möller's avatar
Niels Möller committed
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
struct des_compat_des3 { struct des_ctx *keys[3]; }; 

typedef void (*cbc_crypt_func)(void *, uint32_t, uint8_t *, const uint8_t *);

static void
des_compat_des3_encrypt(struct des_compat_des3 *ctx,
			uint32_t length, uint8_t *dst, const uint8_t *src)
{
  des_encrypt(ctx->keys[0], length, dst, src);
  des_decrypt(ctx->keys[1], length, dst, dst);
  des_encrypt(ctx->keys[2], length, dst, dst);
}

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
  des_decrypt(ctx->keys[2], length, dst, src);
55
  des_encrypt(ctx->keys[1], length, dst, dst);
56
  des_decrypt(ctx->keys[0], length, dst, dst);
57 58 59
}

void
60 61 62
des_ecb3_encrypt(des_cblock *src, des_cblock *dst,
		 des_key_schedule k1, des_key_schedule k2,
		 des_key_schedule k3, int enc)
63 64 65 66
{
  struct des_compat_des3 keys = { { k1, k2, k3 } };

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

70 71 72 73
uint32_t
des_cbc_cksum(des_cblock *src, des_cblock *dst,
	      long length, des_key_schedule ctx,
	      des_cblock *iv)
Niels Möller's avatar
Niels Möller committed
74 75 76 77 78
{
  /* 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];
79
  memcpy(block, *iv, DES_BLOCK_SIZE);
Niels Möller's avatar
Niels Möller committed
80

81 82
  uint8_t *p;
  
Niels Möller's avatar
Niels Möller committed
83 84
  assert(!(length % DES_BLOCK_SIZE));
  
85
  for (p = *src; length; length -= DES_BLOCK_SIZE, p += DES_BLOCK_SIZE)
Niels Möller's avatar
Niels Möller committed
86
    {
87
      memxor(block, p, DES_BLOCK_SIZE);
Niels Möller's avatar
Niels Möller committed
88 89 90
      des_encrypt(ctx, DES_BLOCK_SIZE, block, block);
    }
  memcpy(dst, block, DES_BLOCK_SIZE);
91 92

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

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
void
des_ncbc_encrypt(des_cblock *src, des_cblock *dst, long length,
                 des_key_schedule ctx, des_cblock *iv,
                 int enc)
{
  switch (enc)
    {
    case DES_ENCRYPT:
      cbc_encrypt(ctx, (cbc_crypt_func) des_encrypt,
		  DES_BLOCK_SIZE, *iv,
		  length, *dst, *src);
      break;
    case DES_DECRYPT:
      cbc_decrypt(ctx,
		  (cbc_crypt_func) des_decrypt,
		  DES_BLOCK_SIZE, *iv,
		  length, *dst, *src);
      break;
    default:
      abort();
    }
}
Niels Möller's avatar
Niels Möller committed
117 118

void
119 120
des_cbc_encrypt(des_cblock *src, des_cblock *dst, long length,
		des_key_schedule ctx, des_cblock *civ,
Niels Möller's avatar
Niels Möller committed
121 122
		int enc)
{
123
  des_cblock iv;
124 125

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

127
  des_ncbc_encrypt(src, dst, length, ctx, &iv, enc);
128
}
Niels Möller's avatar
Niels Möller committed
129

130

Niels Möller's avatar
Niels Möller committed
131
void
132 133
des_ecb_encrypt(des_cblock *src, des_cblock *dst,
		des_key_schedule ctx,
Niels Möller's avatar
Niels Möller committed
134 135
		int enc)
{
136
  ((enc == DES_ENCRYPT) ? des_encrypt : des_decrypt)(ctx, DES_BLOCK_SIZE, *dst, *src);
Niels Möller's avatar
Niels Möller committed
137
}
138

Niels Möller's avatar
Niels Möller committed
139
void
140 141 142
des_ede3_cbc_encrypt(des_cblock *src, des_cblock *dst, long length,
		     des_key_schedule  k1, des_key_schedule k2, des_key_schedule k3,
		     des_cblock *iv,
143 144 145 146
		     int enc)
{
  struct des_compat_des3 keys = { { k1, k2, k3 } };

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  switch (enc)
    {
    case DES_ENCRYPT:
      cbc_encrypt(&keys, (cbc_crypt_func) des_compat_des3_encrypt,
		  DES_BLOCK_SIZE, *iv,
		  length, *dst, *src);
      break;
    case DES_DECRYPT:
      cbc_decrypt(&keys, (cbc_crypt_func) des_compat_des3_decrypt,
		  DES_BLOCK_SIZE, *iv,
		  length, *dst, *src);
      break;
    default:
      abort();
    }
162
}
Niels Möller's avatar
Niels Möller committed
163 164

int
165
des_set_odd_parity(des_cblock *key)
166
{
167
  des_fix_parity(DES_KEY_SIZE, *key, *key);
Niels Möller's avatar
Niels Möller committed
168 169 170

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

173 174 175 176 177 178 179 180 181 182

/* 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
183
int
184
des_key_sched(des_cblock *key, des_key_schedule ctx)
185
{
186 187 188 189 190
  if (!des_check_key)
    /* Fix the parity */
    des_set_odd_parity(key);
  
  if (des_set_key(ctx, *key))
Niels Möller's avatar
Niels Möller committed
191 192 193 194
    return 0;
  else switch(ctx->status)
    {
    case DES_BAD_PARITY:
195 196 197 198 199
      if (des_check_key)
        return -1;
      else
        /* We fixed the parity above */
        abort();
Niels Möller's avatar
Niels Möller committed
200
    case DES_WEAK_KEY:
201 202 203 204 205 206 207
      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
208 209 210
    default:
      abort();
    }
211
}
Niels Möller's avatar
Niels Möller committed
212 213

int
214
des_is_weak_key(des_cblock *key)
215 216 217
{
  struct des_ctx ctx;

218
  return !des_set_key(&ctx, *key);
219
}