-
Notifications
You must be signed in to change notification settings - Fork 6
/
EEA3.cpp
97 lines (81 loc) · 1.49 KB
/
EEA3.cpp
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
#include "ZUC.h"
#include "EEA3.h"
#include <string.h>
void RevertData(u8 *Data, u32 Len)
{
u32 i = 0;
u8 tData = 0;
for (i=0; i<Len/2; i++)
{
tData = Data[i];
Data[i] = Data[Len-i-1];
Data[Len-i-1] = tData;
}
}
bool IsLittleEndian()
{
u32 t1 = 0x31;
if ((*(u8*)(&t1)) == 0x31)
{
return true;
}
return false;
}
/*Length of COUNT is 4*/
void EEA3(u8 *CK, u8 *COUNT, u8 BEARER, u8 DIRECTION, u8 *M, u32 LENGTH, u8 *CM)
{
u8 IV[16] = {0};
u32 i = 0, j = 0, tKeyStreamU32 = 0;
u8 *tKeyStreamU8 = (u8*)&tKeyStreamU32;
u32 bLENGTH = 0; //Byte LENGTH
u32 lbLENGTH = 0; //Left Byte LENGTH
u8 bMask = 0, bFlag = 0;
IV[0] = COUNT[0];
IV[1] = COUNT[1];
IV[2] = COUNT[2];
IV[3] = COUNT[3];
IV[4] = ((BEARER << 3) | ((DIRECTION & 1)<<2)) & 0xFC;
IV[5] = IV[6] = IV[7] = 0;
IV[8] = IV[0];
IV[9] = IV[1];
IV[10] = IV[2];
IV[11] = IV[3];
IV[12] = IV[4];
IV[13] = IV[5];
IV[14] = IV[6];
IV[15] = IV[7];
bLENGTH = LENGTH / 8;
lbLENGTH = LENGTH % 8;
Initialization(CK, IV);
for (i=0; i<bLENGTH; i++)
{
if ((i%4) == 0)
{
GenerateKeyStream(&tKeyStreamU32, 1);
if (IsLittleEndian())
{
RevertData(tKeyStreamU8, 4);
}
}
CM[i] = tKeyStreamU8[i%4] ^ M[i];
}
if (lbLENGTH)
{
if ((i%4) == 0)
{
GenerateKeyStream(&tKeyStreamU32, 1);
if (IsLittleEndian())
{
RevertData(tKeyStreamU8, 4);
}
}
bMask = 0;
bFlag = 0x80;
for (j=0; j<lbLENGTH; j++)
{
bMask |= bFlag;
bFlag >>= 1;
}
CM[i] = ((tKeyStreamU8[i%4] ^ M[i]) & bMask);
}
}