~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/seed.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*************************************************
2
 
* SEED Source File                               *
3
 
* (C) 1999-2007 The Botan Project                *
4
 
*************************************************/
5
 
 
6
 
#include <botan/seed.h>
7
 
#include <botan/bit_ops.h>
8
 
 
9
 
namespace Botan {
10
 
 
11
 
/*************************************************
12
 
* SEED G Function                                *
13
 
*************************************************/
14
 
u32bit SEED::G_FUNC::operator()(u32bit X) const
15
 
   {
16
 
   return (S0[get_byte(3, X)] ^ S1[get_byte(2, X)] ^
17
 
           S2[get_byte(1, X)] ^ S3[get_byte(0, X)]);
18
 
   }
19
 
 
20
 
/*************************************************
21
 
* SEED Encryption                                *
22
 
*************************************************/
23
 
void SEED::enc(const byte in[], byte out[]) const
24
 
   {
25
 
   u32bit B0 = make_u32bit(in[ 0], in[ 1], in[ 2], in[ 3]),
26
 
          B1 = make_u32bit(in[ 4], in[ 5], in[ 6], in[ 7]),
27
 
          B2 = make_u32bit(in[ 8], in[ 9], in[10], in[11]),
28
 
          B3 = make_u32bit(in[12], in[13], in[14], in[15]);
29
 
 
30
 
   G_FUNC G;
31
 
 
32
 
   for(u32bit j = 0; j != 16; j += 2)
33
 
      {
34
 
      u32bit T0, T1;
35
 
 
36
 
      T0 = B2 ^ K[2*j];
37
 
      T1 = G(T0 ^ B3 ^ K[2*j+1]);
38
 
      T0 = G(T1 + T0);
39
 
      T1 = G(T1 + T0);
40
 
      B1 ^= T1;
41
 
      B0 ^= T0 + T1;
42
 
 
43
 
      T0 = B0 ^ K[2*j+2];
44
 
      T1 = G(T0 ^ B1 ^ K[2*j+3]);
45
 
      T0 = G(T1 + T0);
46
 
      T1 = G(T1 + T0);
47
 
      B3 ^= T1;
48
 
      B2 ^= T0 + T1;
49
 
      }
50
 
 
51
 
   out[ 0] = get_byte(0, B2); out[ 1] = get_byte(1, B2);
52
 
   out[ 2] = get_byte(2, B2); out[ 3] = get_byte(3, B2);
53
 
   out[ 4] = get_byte(0, B3); out[ 5] = get_byte(1, B3);
54
 
   out[ 6] = get_byte(2, B3); out[ 7] = get_byte(3, B3);
55
 
   out[ 8] = get_byte(0, B0); out[ 9] = get_byte(1, B0);
56
 
   out[10] = get_byte(2, B0); out[11] = get_byte(3, B0);
57
 
   out[12] = get_byte(0, B1); out[13] = get_byte(1, B1);
58
 
   out[14] = get_byte(2, B1); out[15] = get_byte(3, B1);
59
 
   }
60
 
 
61
 
/*************************************************
62
 
* SEED Decryption                                *
63
 
*************************************************/
64
 
void SEED::dec(const byte in[], byte out[]) const
65
 
   {
66
 
   u32bit B0 = make_u32bit(in[ 0], in[ 1], in[ 2], in[ 3]),
67
 
          B1 = make_u32bit(in[ 4], in[ 5], in[ 6], in[ 7]),
68
 
          B2 = make_u32bit(in[ 8], in[ 9], in[10], in[11]),
69
 
          B3 = make_u32bit(in[12], in[13], in[14], in[15]);
70
 
 
71
 
   G_FUNC G;
72
 
 
73
 
   for(u32bit j = 0; j != 16; j += 2)
74
 
      {
75
 
      u32bit T0, T1;
76
 
 
77
 
      T0 = B2 ^ K[30-2*j];
78
 
      T1 = G(T0 ^ B3 ^ K[31-2*j]);
79
 
      T0 = G(T1 + T0);
80
 
      T1 = G(T1 + T0);
81
 
      B1 ^= T1;
82
 
      B0 ^= T0 + T1;
83
 
 
84
 
      T0 = B0 ^ K[28-2*j];
85
 
      T1 = G(T0 ^ B1 ^ K[29-2*j]);
86
 
      T0 = G(T1 + T0);
87
 
      T1 = G(T1 + T0);
88
 
      B3 ^= T1;
89
 
      B2 ^= T0 + T1;
90
 
      }
91
 
 
92
 
   out[ 0] = get_byte(0, B2); out[ 1] = get_byte(1, B2);
93
 
   out[ 2] = get_byte(2, B2); out[ 3] = get_byte(3, B2);
94
 
   out[ 4] = get_byte(0, B3); out[ 5] = get_byte(1, B3);
95
 
   out[ 6] = get_byte(2, B3); out[ 7] = get_byte(3, B3);
96
 
   out[ 8] = get_byte(0, B0); out[ 9] = get_byte(1, B0);
97
 
   out[10] = get_byte(2, B0); out[11] = get_byte(3, B0);
98
 
   out[12] = get_byte(0, B1); out[13] = get_byte(1, B1);
99
 
   out[14] = get_byte(2, B1); out[15] = get_byte(3, B1);
100
 
   }
101
 
 
102
 
/*************************************************
103
 
* SEED Key Schedule                              *
104
 
*************************************************/
105
 
void SEED::key(const byte key[], u32bit)
106
 
   {
107
 
   const u32bit RC[16] = {
108
 
      0x9E3779B9, 0x3C6EF373, 0x78DDE6E6, 0xF1BBCDCC,
109
 
      0xE3779B99, 0xC6EF3733, 0x8DDE6E67, 0x1BBCDCCF,
110
 
      0x3779B99E, 0x6EF3733C, 0xDDE6E678, 0xBBCDCCF1,
111
 
      0x779B99E3, 0xEF3733C6, 0xDE6E678D, 0xBCDCCF1B
112
 
   };
113
 
 
114
 
   SecureBuffer<u32bit, 4> WK;
115
 
 
116
 
   for(u32bit j = 0; j != 4; ++j)
117
 
      WK[j] = make_u32bit(key[4*j], key[4*j+1], key[4*j+2], key[4*j+3]);
118
 
 
119
 
   G_FUNC G;
120
 
 
121
 
   for(u32bit j = 0; j != 16; j += 2)
122
 
      {
123
 
      K[2*j  ] = G(WK[0] + WK[2] - RC[j]);
124
 
      K[2*j+1] = G(WK[1] - WK[3] + RC[j]);
125
 
 
126
 
      byte T = get_byte(3, WK[0]);
127
 
      WK[0] = (WK[0] >> 8) | (get_byte(3, WK[1]) << 24);
128
 
      WK[1] = (WK[1] >> 8) | (T << 24);
129
 
 
130
 
      K[2*j+2] = G(WK[0] + WK[2] - RC[j+1]);
131
 
      K[2*j+3] = G(WK[1] - WK[3] + RC[j+1]);
132
 
 
133
 
      T = get_byte(0, WK[3]);
134
 
      WK[3] = (WK[3] << 8) | get_byte(0, WK[2]);
135
 
      WK[2] = (WK[2] << 8) | T;
136
 
      }
137
 
   }
138
 
 
139
 
}