-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.c
104 lines (91 loc) · 1.85 KB
/
gen.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
#include <string.h>
#include "gen.h"
//for the curious, the algorithm used is RSA
//(with hardcoded keys)
#define _gen_mod (323)
#define _gen_priv_ex (173)
#define _gen_pub_ex (5)
uint64_t ipowmod(uint64_t val, uint64_t pow, uint64_t mod)
{
if(pow == 0)
return 1;
if(pow == 1)
return val;
int res = val;
for(int i=0; i<(pow-1); i++)
res = (res * val) % mod;
return res;
}
uint16_t useKey(uint16_t val, uint16_t key, uint16_t mod)
{
uint64_t res = ipowmod( val, key, mod );
return res % mod;
}
uint16_t encrypt(uint8_t plain)
{
return useKey(plain, _gen_priv_ex, _gen_mod);
}
uint8_t decrypt(uint16_t cypher)
{
if(cypher > _gen_mod)
abort();
uint16_t result = useKey( cypher, _gen_pub_ex, _gen_mod );
if(result > 255)
abort();
return result;
}
void generate(void* ptr, uint32_t seed)
{
return generateFull(ptr, seed, 128);
}
int64_t verify(void* ptr)
{
return verifyFull(ptr, 128);
}
void generateFull(void* ptr, uint32_t seed, uint32_t step)
{
if(step == 0) abort();
uint32_t plain = seed;
uint8_t* plainPtr = (uint8_t*)&plain;
uint16_t* cypherPtr = (uint16_t*)ptr;
uint64_t i;
for(i=0; i<GEN_BLOCK_SIZE / 8; i+= step)
{
if(step > 1 && i != 0)
memset(cypherPtr, 0, (step - 1) * 4);
int j;
for(j=0; j<4; j++)
{
*cypherPtr = encrypt(plainPtr[j]);
cypherPtr++;
}
cypherPtr += (step - 1) * 4;
plain += step;
}
}
int64_t verifyFull(void* ptr, uint32_t step)
{
if(step == 0) abort();
uint32_t plain;
uint8_t* plainPtr = (uint8_t*)&plain;
uint16_t* cypherPtr = (uint16_t*)ptr;
uint32_t result;
uint32_t expected;
uint64_t i;
for(i=0; i<GEN_BLOCK_SIZE / 8; i += step)
{
int j;
for(j=0; j<4; j++)
{
plainPtr[j] = decrypt(*cypherPtr);
cypherPtr++;
}
cypherPtr += (step - 1) * 4;
if(i == 0)
result = expected = plain;
else if(plain != expected)
return -1;
expected += step;
}
return result;
}