-
Notifications
You must be signed in to change notification settings - Fork 9
/
trivium.m
90 lines (73 loc) · 3.51 KB
/
trivium.m
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
function bit=trivium(n,Key)
% Expresar los arrays sin comas! (es lo mismo...)
Key= [1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
IV=[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
Comp_A = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
Comp_B = [0, 0, 0, 0];
%n = 12;
output_array = [];
%n = str2num(n);
%Convierto la clave pasaada como argumento desde java en binario (primero
%cada char en su representación decimal en ASCII y luego en binario)
%long=length(Key_string)
%for i= 1:long
% binary=dec2bin('i',8);
% Keys= [Keys binary];
%end
%Key=Keys
% Armo los shifter concatenandoles los complementos
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%2.2 Key and IV setup
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
shifter_A=cat(2,Key, Comp_A);
shifter_B=cat(2,IV, Comp_B);
shifter_C=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1];
shifter_A_aux=shifter_A;
shifter_B_aux=shifter_B;
shifter_C_aux=shifter_C;
%bitxor(Key,IV)
%length(shifter_A)
%length(shifter_B)
%length(shifter_C)
%NOTA: El primer elemento en un array en matlab es el 1 (a diferencia de bash que es el 0)
%
tic;
for i = 1:(4*288)
s=cat(2,shifter_A_aux, shifter_B_aux, shifter_C_aux);
t1 = bitxor((bitxor((bitxor(s(66),(bitand(s(91),s(92))))),s(93))),s(171));
t2 = bitxor((bitxor((bitxor((bitand(s(175),s(176))),s(162))),s(177))),s(264));
t3 = bitxor((bitxor((bitxor((bitand(s(286),s(287))),s(243))),s(288))),s(69));
shifter_A_aux = [ t3 shifter_A_aux(1:end-1) ];
shifter_B_aux = [ t1 shifter_B_aux(1:end-1) ];
shifter_C_aux = [ t2 shifter_C_aux(1:end-1) ];
end
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%2.1 Key stream generation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%shifter_A = [0 0 1 1 0 1 1 1 0 0 0 0 1 0 1 0 1 1 1 0 0 1 1 1 1 0 1 0 1 0 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 0 1 1 1 1 1 1 0 1 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1]
%shifter_B = [0 1 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 1 0 1 1 1 1 0 1 1 0 1 0 0 1 0 1 0 1 1 0 1 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 0 1 1 1 0 1 1 1 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 0 0 0 1 1 0 0 1]
%shifter_C = [1 1 0 1 0 1 1 0 0 1 1 1 1 0 0 1 1 0 1 1 0 0 1 0 0 1 1 0 1 1 1 0 1 1 1 0 0 1 0 1 1 1 0 1 0 0 0 1 0 1 0 1 0 0 0 0 1 0 0 0 1 1 1 0 1 1 1 1 1 1 0 0 0 1 0 0 1 1 0 0 1 1 0 1 0 1 0 1 0 1 1 0 0 1 1 1 1 0 1 1 1 0 1 0 1 0 1 1 1 1 1]
%output_array = zeros(1, n);
for i = 1:(n)
s=cat(2,shifter_A_aux, shifter_B_aux, shifter_C_aux);
t1 = bitxor(s(66),s(93));
t2 = bitxor(s(162),s(177));
t3 = bitxor(s(243),s(288));
zi = bitxor((bitxor(t1,t2)),t3);
output_array(end+1) = zi;
t1 = bitxor((bitxor((bitand(s(91),s(92))),s(171))),t1);
t2 = bitxor((bitxor((bitand(s(175),s(176))),s(264))),t2);
t3 = bitxor((bitxor((bitand(s(286),s(287))),s(69))),t3);
shifter_A_aux = [ t3 shifter_A_aux(1:end-1) ];
shifter_B_aux = [ t1 shifter_B_aux(1:end-1) ];
shifter_C_aux = [ t2 shifter_C_aux(1:end-1) ];
end
%disp(output_array)
%fprintf('%g\t\n',output_array)
fprintf('%d',output_array)
time = toc;
disp(time)
%h=histogram(output_array)
bit = output_array;
end