-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.c
67 lines (49 loc) · 1.64 KB
/
keys.c
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
/*
Michael Kochell
All round keys are calculated here
*/
#include "permutation.h"
#include "keys.h"
#include <stdio.h>
static const unsigned int mask = 0X0FFFFFFF; // used to mask keys (28 bits)
/*
gets all round keys and stores them in keyArray
takes the original key, the keyArray, and the number of rounds
*/
void obtainRoundKeys(unsigned long long key, unsigned long long* keyArray, int numRounds)
{
int currentRound;
unsigned long long roundKey;
// perform initial permutation for original key
unsigned long long initialKeyPerm = performLongToLongPermutation(key, keyPermInitial, keyPermInitialSize);
// prepare left and right bits
unsigned long long leftBits = initialKeyPerm >> 28;
unsigned long long rightBits = initialKeyPerm & mask;
unsigned long long c, d;
// find keys for each round
for(currentRound=0; currentRound<numRounds; currentRound++)
{
// do initial shifts for round key
c = shiftKeyLeft(leftBits, leftShiftsPerRound[currentRound]);
d = shiftKeyLeft(rightBits, leftShiftsPerRound[currentRound]);
// combine left and right side
roundKey = (c << 28) | d;
// get final permutation for round key
roundKey = performLongToLongPermutation(roundKey, keyPermFinal, keyPermFinalSize);
// store round key
keyArray[currentRound] = roundKey;
}
}
/*
"wraparound" left shift
*/
static const int inputSize = 28;
unsigned long long shiftKeyLeft(unsigned long long toShift, int numShifts)
{
unsigned long long result;
// collect bits that wrap around
result = toShift >> (inputSize - numShifts);
// collect bits that do not wrap around
result = result | ((toShift << numShifts) & mask);
return result;
}