forked from dazjo/nxtool
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkeyset.cpp
246 lines (192 loc) · 5.49 KB
/
keyset.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <stdio.h>
#include "keyset.h"
#include "utils.h"
#include "tinyxml/tinyxml.h"
static void keyset_set_key128(key128* key, unsigned char* keydata);
static void keyset_parse_key128(key128* key, char* keytext, int keylen);
static int keyset_parse_key(const char* text, unsigned int textlen, unsigned char* key, unsigned int size, int* valid);
static int keyset_load_rsakey(TiXmlElement* elem, rsakey* key);
static int keyset_load_rsakey2048(TiXmlElement* elem, rsakey* key);
static int keyset_load_rsakey4096(TiXmlElement* elem, rsakey* key);
static int keyset_load_key128(TiXmlHandle node, key128* key);
static int keyset_load_key(TiXmlHandle node, unsigned char* key, unsigned int maxsize, int* valid);
static int ishex(char c)
{
if (c >= '0' && c <= '9')
return 1;
if (c >= 'A' && c <= 'F')
return 1;
if (c >= 'a' && c <= 'f')
return 1;
return 0;
}
static unsigned char hextobin(char c)
{
if (c >= '0' && c <= '9')
return c-'0';
if (c >= 'A' && c <= 'F')
return c-'A'+0xA;
if (c >= 'a' && c <= 'f')
return c-'a'+0xA;
return 0;
}
void keyset_init(keyset* keys)
{
memset(keys, 0, sizeof(keyset));
}
int keyset_load_key(TiXmlHandle node, unsigned char* key, unsigned int size, int* valid)
{
TiXmlElement* elem = node.ToElement();
if (valid)
*valid = 0;
if (!elem)
return 0;
const char* text = elem->GetText();
unsigned int textlen = strlen(text);
int status = keyset_parse_key(text, textlen, key, size, valid);
if (status == KEY_ERR_LEN_MISMATCH)
{
fprintf(stderr, "Error size mismatch for key \"%s/%s\"\n", elem->Parent()->Value(), elem->Value());
return 0;
}
return 1;
}
int keyset_parse_key(const char* text, unsigned int textlen, unsigned char* key, unsigned int size, int* valid)
{
unsigned int i, j;
unsigned int hexcount = 0;
if (valid)
*valid = 0;
for(i=0; i<textlen; i++)
{
if (ishex(text[i]))
hexcount++;
}
if (hexcount != size*2)
{
fprintf(stdout, "Error, expected %d hex characters when parsing text \"", size*2);
for(i=0; i<textlen; i++)
fprintf(stdout, "%c", text[i]);
fprintf(stdout, "\"\n");
return KEY_ERR_LEN_MISMATCH;
}
for(i=0, j=0; i<textlen; i++)
{
if (ishex(text[i]))
{
if ( (j&1) == 0 )
key[j/2] = hextobin(text[i])<<4;
else
key[j/2] |= hextobin(text[i]);
j++;
}
}
if (valid)
*valid = 1;
return KEY_OK;
}
int keyset_load_key128(TiXmlHandle node, key128* key)
{
return keyset_load_key(node, key->data, sizeof(key->data), &key->valid);
}
int keyset_load_xtskey128(TiXmlHandle node, xtskey128* key)
{
key->valid = 0;
if (!keyset_load_key(node.FirstChild("TWEAK"), key->tweak, sizeof(key->tweak), 0))
goto clean;
if (!keyset_load_key(node.FirstChild("CRYPT"), key->crypt, sizeof(key->crypt), 0))
goto clean;
key->valid = 1;
clean:
return (key->valid != 0);
}
int keyset_load_rsakey(TiXmlHandle node, rsakey* key)
{
key->keytype = RSAKEY_INVALID;
if (!keyset_load_key(node.FirstChild("N"), key->n, sizeof(key->n) / key->keysize, 0))
goto clean;
if (!keyset_load_key(node.FirstChild("E"), key->e, sizeof(key->e), 0))
goto clean;
key->keytype = RSAKEY_PUB;
if (!keyset_load_key(node.FirstChild("D"), key->d, sizeof(key->d) / key->keysize, 0))
goto clean;
if (!keyset_load_key(node.FirstChild("P"), key->p, sizeof(key->p) / key->keysize, 0))
goto clean;
if (!keyset_load_key(node.FirstChild("Q"), key->q, sizeof(key->q) / key->keysize, 0))
goto clean;
if (!keyset_load_key(node.FirstChild("DP"), key->dp, sizeof(key->dp) / key->keysize, 0))
goto clean;
if (!keyset_load_key(node.FirstChild("DQ"), key->dq, sizeof(key->dq) / key->keysize, 0))
goto clean;
if (!keyset_load_key(node.FirstChild("QP"), key->qp, sizeof(key->qp) / key->keysize, 0))
goto clean;
key->keytype = RSAKEY_PRIV;
clean:
return (key->keytype != RSAKEY_INVALID);
}
int keyset_load_rsakey2048(TiXmlHandle node, rsakey* key)
{
key->keysize = RSASIZE_2048;
return keyset_load_rsakey(node, key);
}
int keyset_load_rsakey4096(TiXmlHandle node, rsakey* key)
{
key->keysize = RSASIZE_4096;
return keyset_load_rsakey(node, key);
}
int keyset_load(keyset* keys, const char* fname, int verbose)
{
TiXmlDocument doc(fname);
bool loadOkay = doc.LoadFile();
if (!loadOkay)
{
if (verbose)
fprintf(stderr, "Could not load keyset file \"%s\", error: %s.\n", fname, doc.ErrorDesc() );
return 0;
}
TiXmlHandle root = doc.FirstChild("keys");
keyset_load_rsakey2048(root.FirstChild("NrrRsaKey"), &keys->nrrrsakey);
return 1;
}
void keyset_merge(keyset* keys, keyset* src)
{
return;
}
void keyset_set_key128(key128* key, unsigned char* keydata)
{
memcpy(key->data, keydata, 16);
key->valid = 1;
}
void keyset_parse_key128(key128* key, char* keytext, int keylen)
{
keyset_parse_key(keytext, keylen, key->data, 16, &key->valid);
}
void keyset_dump_rsakey(rsakey* key, const char* keytitle)
{
if (key->keytype == RSAKEY_INVALID)
return;
fprintf(stdout, "%s\n", keytitle);
memdump(stdout, "Modulus: ", key->n, sizeof(key->n) / key->keysize);
memdump(stdout, "Exponent: ", key->e, sizeof(key->e));
if (key->keytype == RSAKEY_PRIV)
{
memdump(stdout, "P: ", key->p, sizeof(key->p) / key->keysize);
memdump(stdout, "Q: ", key->q, sizeof(key->q) / key->keysize);
}
fprintf(stdout, "\n");
}
void keyset_dump_key128(key128* key, const char* keytitle)
{
if (key->valid)
{
fprintf(stdout, "%s\n", keytitle);
memdump(stdout, "", key->data, 16);
fprintf(stdout, "\n");
}
}
void keyset_dump(keyset* keys)
{
fprintf(stdout, "Current keyset: \n");
keyset_dump_rsakey(&keys->nrrrsakey, "NRR RSA KEY");
fprintf(stdout, "\n");
}