Commit f21f781b authored by Ruud de Rooij's avatar Ruud de Rooij Committed by Niels Möller

Initial twofish support

Rev: src/symmetric/generate_q.c:1.1
Rev: src/symmetric/include/twofish.h:1.1
Rev: src/symmetric/twofish.c:1.1
parent 3be59114
/*
* generate_q - Generates the permutations q0 and q1 for twofish.
* Copyright (C) 1999 Ruud de Rooij <ruud@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
typedef unsigned char byte;
#define ror4(x) (((x) >> 1) | (((x) & 1) << 3))
static byte q0(byte x)
{
static byte t0[16] = { 0x8, 0x1, 0x7, 0xD, 0x6, 0xF, 0x3, 0x2,
0x0, 0xB, 0x5, 0x9, 0xE, 0xC, 0xA, 0x4 };
static byte t1[16] = { 0xE, 0xC, 0xB, 0x8, 0x1, 0x2, 0x3, 0x5,
0xF, 0x4, 0xA, 0x6, 0x7, 0x0, 0x9, 0xD };
static byte t2[16] = { 0xB, 0xA, 0x5, 0xE, 0x6, 0xD, 0x9, 0x0,
0xC, 0x8, 0xF, 0x3, 0x2, 0x4, 0x7, 0x1 };
static byte t3[16] = { 0xD, 0x7, 0xF, 0x4, 0x1, 0x2, 0x6, 0xE,
0x9, 0xB, 0x3, 0x0, 0x8, 0x5, 0xC, 0xA };
byte a0 = x / 16;
byte b0 = x % 16;
byte a1 = a0 ^ b0;
byte b1 = a0 ^ ror4(b0) ^ ((8*a0) % 16);
byte a2 = t0[a1];
byte b2 = t1[b1];
byte a3 = a2 ^ b2;
byte b3 = a2 ^ ror4(b2) ^ ((8*a2) % 16);
byte a4 = t2[a3];
byte b4 = t3[b3];
byte y = 16*b4 + a4;
return y;
}
static byte q1(byte x)
{
static byte t0[16] = { 0x2, 0x8, 0xB, 0xD, 0xF, 0x7, 0x6, 0xE,
0x3, 0x1, 0x9, 0x4, 0x0, 0xA, 0xC, 0x5 };
static byte t1[16] = { 0x1, 0xE, 0x2, 0xB, 0x4, 0xC, 0x3, 0x7,
0x6, 0xD, 0xA, 0x5, 0xF, 0x9, 0x0, 0x8 };
static byte t2[16] = { 0x4, 0xC, 0x7, 0x5, 0x1, 0x6, 0x9, 0xA,
0x0, 0xE, 0xD, 0x8, 0x2, 0xB, 0x3, 0xF };
static byte t3[16] = { 0xB, 0x9, 0x5, 0x1, 0xC, 0x3, 0xD, 0xE,
0x6, 0x4, 0x7, 0xF, 0x2, 0x0, 0x8, 0xA };
byte a0 = x / 16;
byte b0 = x % 16;
byte a1 = a0 ^ b0;
byte b1 = a0 ^ ror4(b0) ^ ((8*a0) % 16);
byte a2 = t0[a1];
byte b2 = t1[b1];
byte a3 = a2 ^ b2;
byte b3 = a2 ^ ror4(b2) ^ ((8*a2) % 16);
byte a4 = t2[a3];
byte b4 = t3[b3];
byte y = 16*b4 + a4;
return y;
}
int
main(void)
{
int i, j;
printf("static byte q0[] = { ");
for (i = 0; i < 32; i++) {
for (j = 0; j < 8; j++)
printf("0x%02X, ", q0(i*8+j));
if (i == 31)
printf("};\n\n");
else
printf("\n ");
}
printf("static byte q1[] = { ");
for (i = 0; i < 32; i++) {
for (j = 0; j < 8; j++)
printf("0x%02X, ", q1(i*8+j));
if (i == 31)
printf("};\n");
else
printf("\n ");
}
return 0;
}
/*
* twofish - An implementation of the twofish cipher.
* Copyright (C) 1999 Ruud de Rooij <ruud@debian.org>
*
* Modifications for lsh
* Copyright (C) 1999 J.H.M. Dassen (Ray) <jdassen@wi.LeidenUniv.nl>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Twofish is a 128-bit block cipher that accepts a variable-length
* key up to 256 bits, designed by Bruce Schneier and others. See
* http://www.counterpane.com/twofish.html for details.
*/
#if !defined(TWOFISH_H)
#define TWOFISH_H
#include <stdlib.h> /* For size_t */
#include "crypto_types.h"
#define TWOFISH_BLOCKSIZE 16 /* bytes */
/* Other key lengths are possible, but in the context of the ssh protocols,
* 256 bits is the default. */
#define TWOFISH_KEYSIZE 32 /* bytes */
/* Allow keys of size 128 <= bits <= 256 */
#define TWOFISH_MIN_KEYSIZE 16 /* bytes */
#define TWOFISH_MAX_KEYSIZE 32 /* bytes */
typedef struct {
UINT32 keys[40];
UINT32 s_box[4][256];
} TWOFISH_context;
/* TWOFISH_context * twofish_setup(size_t keysize, const void * key);
*
* Set up internal tables required for twofish encryption and decryption.
*
* The key size is specified in bytes. Key sizes up to 32 bytes are
* supported. Larger key sizes are silently truncated. The function
* returns a pointer which must be passed as the first argument to
* twofish_encrypt() and twofish_decrypt(). When no more encryption or
* decryption with this key is to be performed, the storage for the tables
* can be reclaimed with the free() function.
* If no memory is available to store the tables, twofish_setup()
* returns NULL.
*/
TWOFISH_context * twofish_setup(size_t keysize, const void *key);
/* void twofish_encrypt(void * context,
* const void * plaintext,
* void * ciphertext);
*
* Encrypt 16 bytes of data with the twofish algorithm.
*
* Before this function can be used, twofish_setup() must be used in order to
* set up various tables required for the encryption algorithm.
* The first argument is the handle returned from twofish_setup().
* This function always encrypts 16 bytes of plaintext to 16 bytes of
* ciphertext. The memory areas of the plaintext and the ciphertext can
* overlap.
*/
void twofish_encrypt(void *context, const void *plaintext, void *ciphertext);
/* void twofish_decrypt(void * context,
* const void * ciphertext,
* void * plaintext);
*
* Decrypt 16 bytes of data with the twofish algorithm.
*
* Before this function can be used, twofish_setup() must be used in order to
* set up various tables required for the decryption algorithm.
* The first argument is the handle returned from twofish_setup().
* This function always decrypts 16 bytes of ciphertext to 16 bytes of
* plaintext. The memory areas of the plaintext and the ciphertext can
* overlap.
*/
void twofish_decrypt(void *context, const void *ciphertext, void *plaintext);
#endif
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment