11void MARS::Base::UncheckedSetKey(
const byte *userKey,
unsigned int length,
const NameValuePairs &)
13 AssertValidKeyLength(length);
18 T[length/4] = length/4;
20 for (
unsigned int j=0; j<4; j++)
25 T[i] = T[i] ^ rotlConstant<3>(T[(i + 8) % 15] ^ T[(i + 13) % 15]) ^ (4 * i + j);
28 for (
unsigned int k=0; k<4; k++)
30 T[i] = rotlConstant<9>(T[i] + Sbox[T[(i + 14) % 15] % 512]);
34 m_k[10*j+i] = T[4*i%15];
38 for(
unsigned int i = 5; i < 37; i += 2)
41 m = (~w ^ (w<<1)) & (~w ^ (w>>1)) & 0x7ffffffe;
42 m &= m>>1; m &= m>>2; m &= m>>4;
43 m |= m<<1; m |= m<<2; m |= m<<4;
45 w ^=
rotlMod(Sbox[265 + (m_k[i] & 3)], m_k[i-1]) & m;
50#define S(a) Sbox[(a)&0x1ff]
51#define S0(a) Sbox[(a)&0xff]
52#define S1(a) Sbox[((a)&0xff) + 256]
56void MARS::Enc::ProcessAndXorBlock(
const byte *inBlock,
const byte *xorBlock,
byte *outBlock)
const
59 word32 a, b, c, d, l, m, r, t;
62 Block::Get(inBlock)(a)(b)(c)(d);
64 a += k[0]; b += k[1]; c += k[2]; d += k[3];
68 b = (b ^ S0(a)) + S1(a>>8);
70 a = rotrConstant<24>(a);
72 a += (i%4==0) ? d : 0;
73 a += (i%4==1) ? b : 0;
74 t = a; a = b; b = c; c = d; d = t;
79 t = rotlConstant<13>(a);
80 r = rotlConstant<10>(t * k[2 * i + 5]);
82 l =
rotlMod((S(m) ^ rotrConstant<5>(r) ^ r), r);
83 c +=
rotlMod(m, rotrConstant<5>(r));
86 a = b; b = c; c = d; d = t;
91 a -= (i%4==2) ? d : 0;
92 a -= (i%4==3) ? b : 0;
95 t = rotlConstant<24>(a);
96 d = (d - S1(a>>16)) ^ S0(t);
97 a = b; b = c; c = d; d = t;
100 a -= k[36]; b -= k[37]; c -= k[38]; d -= k[39];
105void MARS::Dec::ProcessAndXorBlock(
const byte *inBlock,
const byte *xorBlock,
byte *outBlock)
const
108 word32 a, b, c, d, l, m, r, t;
111 Block::Get(inBlock)(d)(c)(b)(a);
113 d += k[36]; c += k[37]; b += k[38]; a += k[39];
117 b = (b ^ S0(a)) + S1(a>>8);
119 a = rotrConstant<24>(a);
121 a += (i%4==0) ? d : 0;
122 a += (i%4==1) ? b : 0;
123 t = a; a = b; b = c; c = d; d = t;
128 t = rotrConstant<13>(a);
129 r = rotlConstant<10>(a * k[35 - 2 * i]);
131 l =
rotlMod((S(m) ^ rotrConstant<5>(r) ^ r), r);
132 c -=
rotlMod(m, rotrConstant<5>(r));
135 a = b; b = c; c = d; d = t;
140 a -= (i%4==2) ? d : 0;
141 a -= (i%4==3) ? b : 0;
144 t = rotlConstant<24>(a);
145 d = (d - S1(a>>16)) ^ S0(t);
146 a = b; b = c; c = d; d = t;
149 d -= k[0]; c -= k[1]; b -= k[2]; a -= k[3];
Fixed size stack-based SecBlock.
Interface for retrieving values given their names.
Access a block of memory.
unsigned int word32
32-bit unsigned datatype
@ LITTLE_ENDIAN_ORDER
byte order is little-endian
Classes for the MARS block cipher (IBM AES submission)
Utility functions for the Crypto++ library.
void GetUserKey(ByteOrder order, T *out, size_t outlen, const byte *in, size_t inlen)
Copy bytes in a buffer to an array of elements in big-endian order.
T rotlMod(T x, unsigned int y)
Performs a left rotate.
Crypto++ library namespace.
Access a block of memory.