-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtesttsm.c
121 lines (94 loc) · 3.26 KB
/
testtsm.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*--------------------------------------------------------------------*/
/* testtsm.c */
/* Author: Gerry Wan */
/*--------------------------------------------------------------------*/
#include "keychain.h"
#include "sha256.h"
#include "tsm.h"
#include <stdlib.h>
//#include <string.h>
#include <assert.h>
#include <stdio.h>
#define ASSURE(i) assure(i, __LINE__)
/* If !iSuccessful, print a message to stdout indicating that the
test failed. */
static void assure(int iSuccessful, int iLineNum)
{
if (! iSuccessful)
{
printf("Test at line %d failed.\n", iLineNum);
fflush(stdout);
}
}
/*--------------------------------------------------------------------*/
static void print_hash(unsigned char hash[])
{
int idx;
for (idx=0; idx < 32; idx++)
printf("%02x",hash[idx]);
printf("\n");
}
/*--------------------------------------------------------------------*/
static void phex(unsigned char *str)
{
unsigned char i;
for (i = 0; i < 4; i++)
printf("%.2x", str[i]);
printf("\n");
}
/*--------------------------------------------------------------------*/
int main(void)
{
printf("Begin tests\n");
printf("------------------------------------------------------\n");
printf("An invalid key and data hash mismatch should appear here:\n");
int status;
unsigned long umk = 0xefcdab8967452301;
KeyChain_T oKeyChain;
oKeyChain = KeyChain_new(umk);
status = AddKeyToChain(oKeyChain, "0", "00", 0);
ASSURE(status);
sleep(1);
status = AddKeyToChain(oKeyChain, "0", "01", 1);
ASSURE(status);
sleep(1);
status = AddKeyToChain(oKeyChain, "00", "000", 1);
ASSURE(status);
sleep(1);
status = AddKeyToChain(oKeyChain, "00", "001", 1);
ASSURE(status);
// small text file
status = Encrypt("file.txt", "file.enc", oKeyChain, "001");
ASSURE(status);
status = Decrypt("file.enc", "file.dec", oKeyChain, "001");
ASSURE(status);
// image
status = Encrypt("elephant.jpg", "elephantenc.jpg", oKeyChain, "01");
ASSURE(status);
status = Decrypt("elephantenc.jpg", "elephantdec.jpg", oKeyChain, "01");
ASSURE(status);
// 8 byte multiple
status = Encrypt("file2.txt", "file2.enc", oKeyChain, "000");
ASSURE(status);
status = Decrypt("file2.enc", "file2.dec", oKeyChain, "000");
ASSURE(status);
status = DeleteKeyFromChain(oKeyChain, "001");
ASSURE(status);
// should fail, key revoked
status = Decrypt("file.enc", "file.dec", oKeyChain, "001");
ASSURE(!status);
sleep(1);
status = AddKeyToChain(oKeyChain, "0", "02", 1);
ASSURE(status);
// should fail, wrong key
status = Decrypt("file.enc", "file.dec", oKeyChain, "02");
ASSURE(!status);
// encrypt with new key
status = Encrypt("file.txt", "file.enc", oKeyChain, "02");
ASSURE(status);
status = Decrypt("file.enc", "file.dec", oKeyChain, "02");
ASSURE(status);
KeyChain_free(oKeyChain);
printf("------------------------------------------------------\n");
printf("End of tests\n");
}