des-compat.c 3.51 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 31
#include "cbc.h"

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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
48
{
49 50 51 52 53 54 55 56 57 58 59 60 61 62
  des_decrypt(ctx->keys[0], length, dst, src);
  des_encrypt(ctx->keys[1], length, dst, dst);
  des_decrypt(ctx->keys[2], length, dst, dst);
}

void
des_ecb3_encrypt(const uint8_t *src, uint8_t *dst,
		 struct des_ctx *k1, struct des_ctx *k2,
		 struct des_ctx *k3, int enc)
{
  struct des_compat_des3 keys = { { k1, k2, k3 } };

  ((enc == DES_ENCRYPT) ? des_compat_des3_encrypt : des_compat_des3_decrypt)
    (&keys, DES_BLOCK_SIZE, dst, src);
Niels Möller's avatar
Niels Möller committed
63 64 65 66 67 68 69 70 71 72 73 74
}

uint32_t
des_cbc_cksum(const uint8_t *src, uint8_t dst,
	      long length, struct des_ctx *ctx,
	      uint8_t *iv);

void
des_cbc_encrypt(const uint8_t *src, uint8_t *dst, long length,
		struct des_ctx *ctx, uint8_t *iv,
		int enc)
{
75 76
  cbc_encrypt(ctx,
	      (cbc_crypt_func) ((enc == DES_ENCRYPT) ? des_encrypt : des_decrypt),
Niels Möller's avatar
Niels Möller committed
77 78 79 80 81 82
              DES_BLOCK_SIZE, iv,
              length, dst, src);
}

void
des_3cbc_encrypt(const uint8_t *src, uint8_t *dst, long length,
83
		 struct des_ctx * k1,struct des_ctx *k2,
Niels Möller's avatar
Niels Möller committed
84 85 86 87 88 89
		 /* What mode is this, two iv:s? */
		 uint8_t *iv1, uint8_t *iv2,
		 int enc);

void
des_ecb_encrypt(const uint8_t *src, uint8_t *dst, long length,
90
		struct des_ctx *ctx,
Niels Möller's avatar
Niels Möller committed
91 92
		int enc)
{
93
  ((enc == DES_ENCRYPT) ? des_encrypt : des_decrypt)(ctx, length, dst, src);
Niels Möller's avatar
Niels Möller committed
94
}
95

Niels Möller's avatar
Niels Möller committed
96 97
void
des_ede3_cbc_encrypt(const uint8_t *src, uint8_t *dst, long length,
98
		     struct des_ctx * k1, struct des_ctx *k2, struct des_ctx *k3,
Niels Möller's avatar
Niels Möller committed
99
		     uint8_t *iv,
100 101 102 103 104 105 106 107 108 109 110 111 112
		     int enc)
{
  struct des_compat_des3 keys = { { k1, k2, k3 } };

  if (enc == DES_ENCRYPT)
    cbc_encrypt(&keys, (cbc_crypt_func) des_compat_des3_encrypt,
		DES_BLOCK_SIZE, iv,
		length, dst, src);
  else
    cbc_decrypt(&keys, (cbc_crypt_func) des_compat_des3_decrypt,
		DES_BLOCK_SIZE, iv,
		length, dst, src);
}
Niels Möller's avatar
Niels Möller committed
113 114

int
115 116 117 118
des_set_odd_parity(uint8_t *key)
{
  des_fix_parity(DES_KEY_SIZE, key, key);
}
Niels Möller's avatar
Niels Möller committed
119 120

int
121 122 123 124
des_compat_set_key(const uint8_t *key, struct des_ctx *ctx)
{
  des_set_key(ctx, key);
}
Niels Möller's avatar
Niels Möller committed
125 126 127 128 129

int
des_key_sched(const uint8_t *key, struct des_ctx *ctx);

int
130 131 132 133 134 135
des_is_weak_key(const uint8_t *key)
{
  struct des_ctx ctx;

  return !des_set_key(&ctx, key);
}