Skip to content

Commit 19f08ce

Browse files
committed
Implement the rest of AES CT functions
1 parent 226592d commit 19f08ce

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

Classes/FCryptoAES.uc

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,27 @@ static final function AesCtOrtho(out array<int> Q)
341341

342342
static final function int SubWord(int X)
343343
{
344+
local array<int> Q;
345+
// local int I;
346+
347+
Q.Length = 8;
348+
// for (I = 0; I < 8; ++I)
349+
// {
350+
// Q[I] = X;
351+
// }
352+
Q[0] = X;
353+
Q[1] = X;
354+
Q[2] = X;
355+
Q[3] = X;
356+
Q[4] = X;
357+
Q[5] = X;
358+
Q[6] = X;
359+
Q[7] = X;
360+
361+
AesCtOrtho(Q);
362+
AesCtBitSliceSBox(Q);
363+
AesCtOrtho(Q);
364+
return Q[0];
344365
}
345366

346367
static final function int AesCtKeySched(
@@ -349,6 +370,76 @@ static final function int AesCtKeySched(
349370
int KeyLen
350371
)
351372
{
373+
local int NumRounds;
374+
local int I;
375+
local int J;
376+
local int K;
377+
local int Nk;
378+
local int Nkf;
379+
local int Tmp;
380+
local array<int> SKey;
381+
382+
SKey.Length = 120;
383+
384+
switch (KeyLen)
385+
{
386+
case 16:
387+
NumRounds = 10;
388+
break;
389+
case 24:
390+
NumRounds = 12;
391+
break;
392+
case 32:
393+
NumRounds = 14;
394+
break;
395+
default:
396+
// TODO: log error?
397+
return 0;
398+
}
399+
400+
Nk = KeyLen >>> 2;
401+
Nkf = (NumRounds + 1) << 2;
402+
Tmp = 0;
403+
for (I = 0; I < Nk; ++I)
404+
{
405+
// TODO: dedicated file for Enc/Dec functions?
406+
// Tmp = Dec32LE(Key, I << 2);
407+
SKey[(I << 1) ] = Tmp;
408+
SKey[(I << 1) + 1] = Tmp;
409+
}
410+
J = 0;
411+
K = 0;
412+
for (I = Nk; I < Nkf; ++I)
413+
{
414+
if (J == 0)
415+
{
416+
Tmp = (Tmp << 24) | (Tmp >>> 8);
417+
Tmp = SubWord(Tmp) ^ default.RCon[K];
418+
}
419+
else if (Nk > 6 && J == 4)
420+
{
421+
Tmp = SubWord(Tmp);
422+
}
423+
Tmp = Tmp ^ (SKey[(I - Nk) << 1]);
424+
SKey[(I << 1) ] = Tmp;
425+
SKey[(I << 1) + 1] = Tmp;
426+
if (++J == Nk)
427+
{
428+
J = 0;
429+
++K;
430+
}
431+
}
432+
for (I = 0; I < Nkf; I += 4)
433+
{
434+
AesCtOrtho(SKey + (I << 1));
435+
}
436+
J = 0;
437+
for (I = 0; I < Nkf; ++I)
438+
{
439+
CompSkey[I] = (SKey[J] & 0x55555555) | (SKey[J + 1] & 0xAAAAAAAA);
440+
J += 2;
441+
}
442+
return NumRounds;
352443
}
353444

354445
static final function AesCtSKeyExpand(
@@ -357,6 +448,24 @@ static final function AesCtSKeyExpand(
357448
const out array<int> CompSKey
358449
)
359450
{
451+
local int U;
452+
local int V;
453+
local int N;
454+
local int X;
455+
local int Y;
456+
457+
N = (NumRounds + 1) << 2;
458+
V = 0;
459+
for (U = 0; U < N; ++U)
460+
{
461+
X = CompSKey[U];
462+
Y = CompSKey[U];
463+
X = X & 0x55555555;
464+
SKey[V ] = X | (X << 1);
465+
Y = Y & 0xAAAAAAAA;
466+
SKey[V + 1] = Y | (Y >>> 1);
467+
V += 2;
468+
}
360469
}
361470

362471
DefaultProperties

0 commit comments

Comments
 (0)