Skip to content

Commit 11faf70

Browse files
committedDec 4, 2015
coverity_scan: fixed defects, using proper random number generator
1 parent 87493a4 commit 11faf70

12 files changed

+33
-27
lines changed
 

‎.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ addons:
3535
- libboost-program-options1.55-dev
3636
- libboost-serialization1.55-dev
3737
- libboost-iostreams1.55-dev
38+
- libboost-random1.55-dev
3839
coverity_scan:
3940
project:
4041
name: "ph4r05/Whitebox-crypto-AES"

‎CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
55

66
set(SOURCE_FILES
77
base.h
8+
base.cpp
89
BGEAttack.cpp
910
BGEAttack.h
1011
BGEAttack_test.cpp
@@ -43,7 +44,7 @@ include_directories(${NTL_INCLUDE_PATH})
4344
set(Boost_USE_STATIC_LIBS OFF)
4445
set(Boost_USE_MULTITHREADED ON)
4546
set(Boost_USE_STATIC_RUNTIME OFF)
46-
find_package(Boost REQUIRED COMPONENTS program_options serialization iostreams)
47+
find_package(Boost REQUIRED COMPONENTS program_options serialization iostreams random)
4748
include_directories(${Boost_INCLUDE_DIRS})
4849

4950
# Linking

‎GenericAES.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ int GenericAES::testMixColumn(){
561561
}
562562

563563
void GenericAES::generateA1A2Relations(vec_GF2E& A1, vec_GF2E& A2){
564-
generateA1A2Relations(A1, A2, (rand() % 255) + 1, rand() % 8);
564+
generateA1A2Relations(A1, A2, (phrand() % 255) + 1, phrand() % 8);
565565
}
566566

567567
void GenericAES::generateA1A2Relations(vec_GF2E& A1, vec_GF2E& A2, int a, int q){

‎LinearAffineEq.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ int LinearAffineEq::findLinearEquivalences(bsetElem * S1, bsetElem * S1inv,
613613
// Chose new X and pick value for it
614614
// Keep in mind linearity of mapping, so avoid duplicities.
615615
if (randomizeXGuess){
616-
int rnd = rand() % Ua.size();
616+
int rnd = phrand() % Ua.size();
617617
it1 = Ua.begin(); for(i=0; i<rnd; ++i, ++it1);
618618
x = *it1; Ua.erase(it1);
619619
Na.insert(x);

‎MixingBijections.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ long generateInvertiblePM(mat_GF2& M, int p){
3030
// Fill matrix with random values and then compute determinant.
3131
for(i=0; i<p; i++){
3232
for(j=0; j<p; j++){
33-
M.put(i,j,rand()%2);
33+
M.put(i,j,phrand()%2);
3434
}
3535
}
3636

@@ -371,7 +371,7 @@ int generateMixingBijection(mat_GF2& RES, int t, int p){
371371

372372
// 1. X matrix - p x t matrix, generated from M matrix using some row
373373
X.SetDims(p, curT);
374-
tmp = rand() % pBlocksInM; // current row
374+
tmp = phrand() % pBlocksInM; // current row
375375
for(i=p*tmp,k=0; k<p; i++, k++){
376376
for(j=0; j<curT; j++){
377377
X.put(k,j, M.get(i,j));
@@ -380,7 +380,7 @@ int generateMixingBijection(mat_GF2& RES, int t, int p){
380380

381381
// 2. Y matrix - t x p matrix, generated from M matrix using some column
382382
Y.SetDims(curT, p);
383-
tmp = rand() % pBlocksInM;
383+
tmp = phrand() % pBlocksInM;
384384
for(i=0; i<curT; i++){
385385
for(j=p*tmp,k=0; k<p; j++, k++){
386386
Y.put(i,k, M.get(i,j));
@@ -460,7 +460,7 @@ int generateRandomBijection(vec_GF2X& bijection, vec_GF2X& inverse, int size, in
460460
// yes, we start from second element on purpose, to produce uniform distribution
461461
for(i=1; i<size; i++){
462462
// rnd is index from interval [0, i]
463-
int rnd = rand() % (i+1);
463+
int rnd = phrand() % (i+1);
464464
swap(inverse[getLong(bijection[i])], inverse[getLong(bijection[rnd])]);
465465
swap(bijection[i], bijection[rnd]);
466466
}

‎MixingBijections.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ template<typename T> int randomPermutationT(T * bijection, int size, int init){
8585
// yes, we start from second element on purpose, to produce uniform distribution
8686
for(i=1; i<size; i++){
8787
// rnd is index from interval [0, i]
88-
int rnd = rand() % (i+1);
88+
int rnd = phrand() % (i+1);
8989

9090
// swap values
9191
T tmp = bijection[rnd];
@@ -108,7 +108,7 @@ template<typename T> int generateRandomBijectionT(T * bijection, T * inverse, in
108108
// yes, we start from second element on purpose, to produce uniform distribution
109109
for(i=1; i<size; i++){
110110
// rnd is index from interval [0, i]
111-
int rnd = rand() % (i+1);
111+
int rnd = phrand() % (i+1);
112112

113113
// swap indexes
114114
T idx = inverse[bijection[rnd]];

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Dependencies
4141
* boost_iostreams 1.55+
4242
* boost_serialization 1.55+
4343
* boost_program_options 1.55+
44+
* boost_random 1.55+
4445

4546
Description:
4647
* [NTL] math library is used for computation in finite fields & algebra. NTL is licensed under GPL thus this implementation also has to be GPL.

‎WBAESGenerator.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,10 @@ void WBAESGenerator::generateTables(BYTE *key, enum keySize ksize, WBAES * genAE
416416
defaultAES.expandKey(expandedKey, defaultKey, ksize); // key schedule for default AES
417417
backupKey = expandedKey; // backup default AES expanded key for test routine
418418
for(i=0; i<N_ROUNDS * N_SECTIONS; i++){
419-
int rndPolynomial = useDualAESIdentity ? 0 : rand() % AES_IRRED_POLYNOMIALS;
420-
int rndGenerator = useDualAESIdentity ? 0 : rand() % AES_GENERATORS;
421-
genA[i] = useDualAESARelationsIdentity ? 1 : (rand() % 255) + 1;
422-
genI[i] = useDualAESARelationsIdentity ? 0 : rand() % 8;
419+
int rndPolynomial = useDualAESIdentity ? 0 : phrand() % AES_IRRED_POLYNOMIALS;
420+
int rndGenerator = useDualAESIdentity ? 0 : phrand() % AES_GENERATORS;
421+
genA[i] = useDualAESARelationsIdentity ? 1 : (phrand() % 255) + 1;
422+
genI[i] = useDualAESARelationsIdentity ? 0 : phrand() % 8;
423423
if (useDualAESSimpeAlternate && !useDualAESIdentity){
424424
rndPolynomial = (i)%2 == 0 ? 0: AES_IRRED_POLYNOMIALS-1;
425425
rndGenerator = (i)%2 == 0 ? 0: AES_GENERATORS-1;

‎base.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// Created by Dusan Klinec on 04.12.15.
3+
//
4+
5+
#include "base.h"
6+
#include <boost/random/random_device.hpp>
7+
#include <boost/random/uniform_int_distribution.hpp>
8+
9+
int phrand(){
10+
static boost::random_device rd;
11+
static boost::random::uniform_int_distribution<int> dis;
12+
return dis(rd);
13+
}

‎base.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
#define WBAES_BOOST_SERIALIZATION 1
1313
#define FORCE_DETERMINISM 1
1414

15+
int phrand();
1516

1617
#endif /* BASE_H_ */

‎main.cpp

+2-8
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ int tryMain(int argc, const char * argv[]) {
7474
unsigned char keyFromString[AES_BYTES];
7575
unsigned char * keyToUse = GenericAES::testVect128_key;
7676

77-
// very poor PRNG seeding, but just for now
78-
srand((unsigned)time(0));
7977
GF2X defaultModulus = GF2XFromLong(0x11B, 9);
8078
GF2E::init(defaultModulus);
8179

@@ -140,7 +138,7 @@ int tryMain(int argc, const char * argv[]) {
140138
randomKey = vm["create-random"].as<bool>();
141139
if (randomKey){
142140
for(int i=0; i<AES_BYTES; i++){
143-
keyFromString[i] = rand() % 0x100;
141+
keyFromString[i] = phrand() % 0x100;
144142
}
145143

146144
keyToUse = keyFromString;
@@ -440,8 +438,6 @@ int tryMain(int argc, const char * argv[]) {
440438
}
441439

442440
int A1A2relationsGenerator(void){
443-
// very poor PRNG seeding, but just for now
444-
srand((unsigned)time(0));
445441
GF2X defaultModulus = GF2XFromLong(0x11B, 9);
446442
GF2E::init(defaultModulus);
447443

@@ -515,8 +511,6 @@ int A1A2relationsGenerator(void){
515511
}
516512

517513
int dualAESTest(void){
518-
// very poor PRNG seeding, but just for now
519-
srand((unsigned)time(0));
520514
GF2X defaultModulus = GF2XFromLong(0x11B, 9);
521515
GF2E::init(defaultModulus);
522516

@@ -586,7 +580,7 @@ int dualAESTest(void){
586580

587581
vec_GF2E A1;
588582
vec_GF2E A2;
589-
dualAES.generateA1A2Relations(A1, A2, 1+(rand() % 0xfe), rand() % 7);
583+
dualAES.generateA1A2Relations(A1, A2, 1+(phrand() % 0xfe), phrand() % 7);
590584
cout << "Testing relations A1 A2: Problems = " << dualAES.testA1A2Relations(A1, A2) << endl;
591585

592586
cout << "A1: " << endl;

‎testing.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ int main(void) {
2828
// very poor PRNG seeding, but just for now
2929
time_t start, end;
3030

31-
srand((unsigned)time(0));
3231
GF2X defaultModulus = GF2XFromLong(0x11B, 9);
3332
GF2E::init(defaultModulus);
3433

@@ -111,8 +110,6 @@ int main(void) {
111110
}
112111

113112
int A1A2relationsGenerator(void){
114-
// very poor PRNG seeding, but just for now
115-
srand((unsigned)time(0));
116113
GF2X defaultModulus = GF2XFromLong(0x11B, 9);
117114
GF2E::init(defaultModulus);
118115

@@ -298,8 +295,6 @@ int A1A2relationsGenerator(void){
298295
}
299296

300297
int dualAESTest(void){
301-
// very poor PRNG seeding, but just for now
302-
srand((unsigned)time(0));
303298
GF2X defaultModulus = GF2XFromLong(0x11B, 9);
304299
GF2E::init(defaultModulus);
305300

@@ -375,7 +370,7 @@ int dualAESTest(void){
375370

376371
vec_GF2E A1;
377372
vec_GF2E A2;
378-
dualAES.generateA1A2Relations(A1, A2, 1+(rand() % 0xfe), rand() % 7);
373+
dualAES.generateA1A2Relations(A1, A2, 1+(phrand() % 0xfe), phrand() % 7);
379374
cout << "Testing relations A1 A2: Problems = " << dualAES.testA1A2Relations(A1, A2) << endl;
380375

381376
cout << "A1: " << endl;

0 commit comments

Comments
 (0)
Please sign in to comment.