-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestAttack.c
82 lines (71 loc) · 2.39 KB
/
testAttack.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* \author H{\aa}vard Raddum
*
* Program for testing attack on Rubato with weak q.
* Modify code according to comments in code to test different variants.
*
**/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "rubato.h"
#include "attack.h"
int main(int argc, char *argv[]){
cipherState *K, *gK, *Km;
long i, j, *KS, *GS, *canNoise, nr;
double maxScore=0.0, sc;
FILE *fp;
/* Select parameters for Rubato */
v=6;//size of linear transfomations in Rubato, (v x v) matrices
nr=3;//number of rounds in Rubato
sigma=1.6356633496458739795537788457;//standard deviation for Gaussian sampler, taken from
// https://github.com/KAIST-CryptLab/RtF-Transciphering/blob/master/ckks_fv/fv_rubato.go
//OLDQ=Q=43976504;//26-bit number with 11 as factor
//OLDQ=Q=25937124;//25-bit number with factor 2^2 * 3
//OLDQ=Q=45890520;//26-bit number with 10 and 12 as factors
OLDQ=Q=25937125;//25-bit number with factor 5
K=newState();
for(i=0; i<v; ++i){
for(j=0; j<v; ++j)
K->matrix[i][j]=random()%Q;
}
initMDSmatrices();
initDGS();
KS=realKeyStream(K,nr);//KS is Rubato key stream, modulo Q
/* Select modulus for attack. The new value of Q must be a factor of OLDQ */
Q=5;
newModulus(KS);
fixI1andI2();
gK=newState();
Km=newState();
for(i=0; i<v; ++i){
for(j=0; j<v; ++j){
Km->matrix[i][j]=K->matrix[i][j]%Q;
}
}
//Km is correct key, modulo small factor
for(i=0; i<v; ++i){
for(j=0; j<v; ++j)
gK->matrix[i][j]=Km->matrix[i][j];
}//fixing correct guess on non-guessed key elements
fp=fopen("scoreValues.txt","w");//score values for all guessed keys will be written to this file
canNoise=(long *)malloc(length*sizeof(long));
/* Select how much to guess, and test the attack */
for(i=0; i<15625; ++i){//adjust number of guessed keys according to fixGuess function below
fixGuess128M(gK,i);//replace function according to Rubato variant (see bottom of attack.h)
GS=guessKeyStream(gK,nr);
for(j=0; j<length; ++j)
canNoise[j]=(KS[j]-GS[j]+Q)%Q;
free(GS);
sc=score(canNoise);
fprintf(fp,"%1.4f\n",sc);
if(sc>maxScore){
printf("Score %1.3f for guessed K\n",sc);
printState(gK);
maxScore=sc;
}
}
fclose(fp);
printf("** Real Rubato secret key **\n");
printState(K);//should match the last printed key modulo the small factor of OLDQ, when attack is successful
}