-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbftest.c
More file actions
89 lines (88 loc) · 2.13 KB
/
Copy pathbftest.c
File metadata and controls
89 lines (88 loc) · 2.13 KB
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
/********* BFtest.cc **********/
// From Schnier's blog. Author unknown.
// to compile and link this version, use
// g++ -Wall bftest.c Blowfish.c -Wwrite-strings -o bftest
//
//
//
//
//
//
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "Blowfish.h"
#define BUFF_SIZE 1048576 // 1MB
#define NUM_TRIALS 100
int Test(Blowfish *);
double Speed(Blowfish *);
using namespace std;
int main()
{
int result;
double speed;
Blowfish BF;
cout << "Blowfish verification: ";
if ((result = Test(&BF)))
{
cout << "\aFailed " << (result>0?"en":"de") << "crypting test vector " <<
abs(result) << endl;
return 0;
}
else
cout << "Passed" << endl;
printf("result variable is %i\n", result);
if ((speed = Speed(&BF)) <= 0)
cout << "Not enough time elapsed for the test, or something funny happend." << endl;
else
cout << "The throughput is " << speed << "MB/s" << endl;
return 1;
}
int Test(Blowfish *BF)
{
DWord Test_Vect;
char *Passwd[2] = {(char*)"abcdefghijklmnopqrstuvwxyz",(char*)"Who is John Galt?"};
unsigned int Clr0[2] = {0x424c4f57,0xfedcba98};
unsigned int Clr1[2] = {0x46495348,0x76543210};
unsigned int Crypt0[2] = {0x324ed0fe,0xcc91732b};
unsigned int Crypt1[2] = {0xf413a203,0x8022f684};
for (unsigned int i=0;i<2;i++)
{
Test_Vect.word0.word = Clr0[i];
Test_Vect.word1.word = Clr1[i];
BF->Set_Passwd(Passwd[i]);
BF->Encrypt((void *)&Test_Vect,8);
if ((Test_Vect.word0.word != Crypt0[i]) || (Test_Vect.word1.word != Crypt1[i]))
return (i+1);
BF->Decrypt((void *)&Test_Vect,8);
if ((Test_Vect.word0.word != Clr0[i]) || (Test_Vect.word1.word != Clr1[i]))
return -(i+1);
}
return 0;
}
double Speed(Blowfish *BF)
{
char *buff;
unsigned int i;
time_t begin,end;
buff = new char[BUFF_SIZE];
if (buff == NULL)
{
cerr << "\aRan out of memory for the test buffer\n";
return 0;
}
srand(0);
for (i=0;i<BUFF_SIZE;i++)
buff[i] = rand()%256;
BF->Set_Passwd((char*)"ianchan");
begin = time(NULL);
for (i=0;i<NUM_TRIALS;i++)
BF->Encrypt((void *)buff,BUFF_SIZE);
end = time(NULL);
delete []buff;
if (end-begin < 1)
return 0;
else
return double(NUM_TRIALS)/(end-begin);
}