File: twofishdata.c

package info (click to toggle)
nettle 3.5.1+really3.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,012 kB
  • sloc: ansic: 51,527; asm: 12,157; sh: 3,632; makefile: 846; cpp: 71; awk: 7
file content (127 lines) | stat: -rw-r--r-- 3,300 bytes parent folder | download | duplicates (4)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* twofishdata.c

   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., 51 Franklin Street, Fifth Floor, Boston, MA
   02111-1301  USA.
 */

#include <stdio.h>

#define ror4(x) (((x) >> 1) | (((x) & 1) << 3))

static unsigned char q0(unsigned char x)
{
    static const unsigned char t0[16] = {
      0x8, 0x1, 0x7, 0xD, 0x6, 0xF, 0x3, 0x2,
      0x0, 0xB, 0x5, 0x9, 0xE, 0xC, 0xA, 0x4
    };
    static const unsigned char t1[16] = {
      0xE, 0xC, 0xB, 0x8, 0x1, 0x2, 0x3, 0x5,
      0xF, 0x4, 0xA, 0x6, 0x7, 0x0, 0x9, 0xD
    };
    static const unsigned char t2[16] = {
      0xB, 0xA, 0x5, 0xE, 0x6, 0xD, 0x9, 0x0,
      0xC, 0x8, 0xF, 0x3, 0x2, 0x4, 0x7, 0x1
    };
    static const unsigned char t3[16] = {
      0xD, 0x7, 0xF, 0x4, 0x1, 0x2, 0x6, 0xE,
      0x9, 0xB, 0x3, 0x0, 0x8, 0x5, 0xC, 0xA
    };

    unsigned char a0 = x / 16;
    unsigned char b0 = x % 16;

    unsigned char a1 = a0 ^ b0;
    unsigned char b1 = a0 ^ ror4(b0) ^ ((8*a0) % 16);

    unsigned char a2 = t0[a1];
    unsigned char b2 = t1[b1];

    unsigned char a3 = a2 ^ b2;
    unsigned char b3 = a2 ^ ror4(b2) ^ ((8*a2) % 16);

    unsigned char a4 = t2[a3];
    unsigned char b4 = t3[b3];

    unsigned char y = 16*b4 + a4;

    return y;
}

static unsigned char q1(unsigned char x)
{
  static const unsigned char t0[16] = {
    0x2, 0x8, 0xB, 0xD, 0xF, 0x7, 0x6, 0xE,
    0x3, 0x1, 0x9, 0x4, 0x0, 0xA, 0xC, 0x5
  };
  static const unsigned char t1[16] = {
    0x1, 0xE, 0x2, 0xB, 0x4, 0xC, 0x3, 0x7,
    0x6, 0xD, 0xA, 0x5, 0xF, 0x9, 0x0, 0x8
  };
  static const unsigned char t2[16] = {
    0x4, 0xC, 0x7, 0x5, 0x1, 0x6, 0x9, 0xA,
    0x0, 0xE, 0xD, 0x8, 0x2, 0xB, 0x3, 0xF
  };
  static const unsigned char t3[16] = {
    0xB, 0x9, 0x5, 0x1, 0xC, 0x3, 0xD, 0xE,
    0x6, 0x4, 0x7, 0xF, 0x2, 0x0, 0x8, 0xA
  };

  unsigned char a0 = x / 16;
  unsigned char b0 = x % 16;

  unsigned char a1 = a0 ^ b0;
  unsigned char b1 = a0 ^ ror4(b0) ^ ((8*a0) % 16);

  unsigned char a2 = t0[a1];
  unsigned char b2 = t1[b1];

  unsigned char a3 = a2 ^ b2;
  unsigned char b3 = a2 ^ ror4(b2) ^ ((8*a2) % 16);

  unsigned char a4 = t2[a3];
  unsigned char b4 = t3[b3];

  unsigned char y = 16*b4 + a4;

  return y;
}

int
main(void)
{
  unsigned i;

  printf("static const uint8_t q0[256] = {");
  for (i = 0; i < 256; i++) {
    if ( (i % 8) == 0)
      printf("\n  ");
    printf("0x%02X,", q0(i));
  }
  printf("\n};\n\n");

  printf("static const uint8_t q1[256] = {");
  for (i = 0; i < 256; i++) {
    if ( (i % 8) == 0)
      printf("\n  ");
    printf("0x%02X,", q1(i));
  }
  printf("\n};\n");

  return 0;
}