forked from skair39/milagro-crypto-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_rsa_sign_WWW_ZZZ.c
312 lines (264 loc) · 7.7 KB
/
test_rsa_sign_WWW_ZZZ.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* @file test_rsa_sign_WWW_ZZZ.c
* @author Kealan McCusker
* @brief Test RSA signature
*
* LICENSE
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/* Build executible after installation:
gcc -std=c99 -g ./test_rsa_sign.c -I/opt/amcl/include -L/opt/amcl/lib -lx509 -lrsa -lecdh -lamcl -o test_rsa_sign_WWW_ZZZ
*/
#include "rsa_WWW.h"
#include "ecdh_ZZZ.h"
#include "x509.h"
#include "utils.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "randapi.h"
typedef enum { false, true } bool;
#define RSA 2
#define H160 1
#define H256 2
#define H384 3
#define H512 4
#define LINE_LEN 10000
//#define DEBUG
#define MAXMODBYTES 66
#define MAXFFLEN 16
char sig[MAXMODBYTES*MAXFFLEN];
octet SIG= {0,sizeof(sig),sig};
char sig2[MAXMODBYTES*MAXFFLEN];
octet SIG2= {0,sizeof(sig2),sig2};
char r[MAXMODBYTES];
octet R= {0,sizeof(r),r};
char s[MAXMODBYTES];
octet S= {0,sizeof(s),s};
char cakey[MAXMODBYTES*MAXFFLEN];
octet CAKEY= {0,sizeof(cakey),cakey};
char certkey[MAXMODBYTES*MAXFFLEN];
octet CERTKEY= {0,sizeof(certkey),certkey};
char h[5000];
octet H= {0,sizeof(h),h};
char hh[5000];
octet HH= {0,sizeof(hh),hh};
int main(int argc, char** argv)
{
if (argc != 2)
{
printf("usage: ./test_rsa_sign_WWW_ZZZ [path to test vector file]\n");
exit(EXIT_FAILURE);
}
int sha;
pktype st,ca;
int rc;
FILE * fp = NULL;
char line[LINE_LEN];
char * linePtr = NULL;
int l1=0;
const char* CERTStr = "CERT = ";
fp = fopen(argv[1], "r");
if (fp == NULL)
{
printf("ERROR opening test vector file\n");
exit(EXIT_FAILURE);
}
rsa_public_key_WWW pub;
rsa_private_key_WWW priv;
// Prime p:
char p[RFS_WWW/2];
octet P= {sizeof(p),sizeof(p),p};
const char* PStr = "p = ";
// Prime q:
char q[RFS_WWW/2];
octet Q= {sizeof(q),sizeof(q),q};
const char* QStr = "q = ";
bool readLine = false;
int i=0;
while (fgets(line, LINE_LEN, fp) != NULL)
{
i++;
readLine = true;
if (!strncmp(line, PStr, strlen(PStr)))
{
#ifdef DEBUG
printf("line %d %s\n", i,line);
#endif
// Find hex value in string
linePtr = line + strlen(PStr);
// p binary value
l1 = strlen(linePtr)-1;
amcl_hex2bin(linePtr, P.val, l1);
}
if (!strncmp(line, QStr, strlen(QStr)))
{
#ifdef DEBUG
printf("line %d %s\n", i,line);
#endif
// Find hex value in string
linePtr = line + strlen(QStr);
// q binary value
l1 = strlen(linePtr)-1;
amcl_hex2bin(linePtr, Q.val, l1);
}
// Self-Signed CA cert
if (!strncmp(line, CERTStr, strlen(CERTStr)))
{
#ifdef DEBUG
printf("line %d %s\n", i,line);
#endif
// Find base64 value in string
char io[5000];
octet IO= {0,sizeof(io),io};
linePtr = line + strlen(CERTStr);
l1 = strlen(linePtr);
char* ca_b64 = (char*) calloc (l1,sizeof(char));
strncpy(ca_b64,linePtr,l1-1);
OCT_frombase64(&IO,ca_b64);
#ifdef DEBUG
printf("CA Self-Signed Cert= \n");
OCT_output(&IO);
printf("\n");
#endif
free(ca_b64);
ca_b64 = NULL;
// returns signature type
st=X509_extract_cert_sig(&IO,&SIG);
#ifdef DEBUG
printf("SIG= \n");
OCT_output(&SIG);
printf("\n");
#endif
// Extract Cert from signed Cert
X509_extract_cert(&IO,&H);
#ifdef DEBUG
printf("Cert= \n");
OCT_output(&H);
printf("\n");
int c;
c=X509_extract_cert(&IO,&H);
// Print email
int ic,len;
char en[9]= {0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x09,0x01};
octet EN= {9,sizeof(en),en};
printf("Issuer Details\n");
ic=X509_find_issuer(&H);
c=X509_find_entity_property(&H,&EN,ic,&len);
print_out("email=",&H,c,len);
printf("\n");
#endif
ca=X509_extract_public_key(&H,&CAKEY);
if (ca.type==0)
{
printf("Not supported by library\n");
exit(EXIT_FAILURE);
}
if (ca.type!=st.type)
{
printf("Not self-signed\n");
exit(EXIT_FAILURE);
}
#ifdef DEBUG
printf("EXTRACTED RSA PUBLIC KEY= \n");
OCT_output(&CAKEY);
printf("\n");
#endif
// Assign public key
pub.e=65537;
FF_WWW_fromOctet(pub.n,&CAKEY,FFLEN_WWW);
#ifdef DEBUG
printf("pub.n ");
FF_WWW_output(pub.n,FFLEN_WWW);
printf("\n");
#endif
// Checking Self-Signed Signature
sha=0;
if (st.hash==H256) sha=SHA256;
if (st.hash==H384) sha=SHA384;
if (st.hash==H512) sha=SHA512;
if (st.hash==0)
{
printf("Hash Function not supported\n");
exit(EXIT_FAILURE);
}
char mp[RFS_WWW];
octet MP= {0,sizeof(mp),mp};
PKCS15(sha,&H,&MP);
RSA_WWW_ENCRYPT(&pub,&SIG,&HH);
#ifdef DEBUG
printf("MP ");
OCT_output(&MP);
printf("\n");
#endif
rc = OCT_comp(&MP,&HH);
if (!rc)
{
printf("TEST RSA VERIFICATION FAILED LINE %d\n",i);
exit(EXIT_FAILURE);
}
// Generating public/private key pair from p amd q
RSA_WWW_KEY_PAIR(NULL,65537,&priv,&pub,&P,&Q);
#ifdef DEBUG
printf("priv.p ");
FF_WWW_output(priv.p,FFLEN_WWW/2);
printf("\n");
printf("priv.q ");
FF_WWW_output(priv.q,FFLEN_WWW/2);
printf("\n");
printf("priv.dp ");
FF_WWW_output(priv.dp,FFLEN_WWW/2);
printf("\n");
printf("priv.dq ");
FF_WWW_output(priv.dq,FFLEN_WWW/2);
printf("\n");
printf("priv.c ");
FF_WWW_output(priv.c,FFLEN_WWW/2);
printf("\n");
#endif
char hp[RFS_WWW];
octet HP= {0,sizeof(hp),hp};
// Sign message
PKCS15(sha,&H,&HP);
RSA_WWW_DECRYPT(&priv,&HP,&SIG2);
#ifdef DEBUG
printf("HP= ");
OCT_output(&HP);
printf("\r\n");
printf("SIG2= ");
OCT_output(&SIG2);
printf("\r\n");
#endif
rc = OCT_comp(&SIG,&SIG2);
if (!rc)
{
printf("TEST RSA SIGNING FAILED LINE %d\n",i);
exit(EXIT_FAILURE);
}
}
}
fclose(fp);
if (!readLine)
{
printf("ERROR Empty test vector file\n");
exit(EXIT_FAILURE);
}
printf("SUCCESS TEST RSA SIGNATURE PASSED\n");
exit(EXIT_SUCCESS);
}