Skip to content

Commit 7e268ba

Browse files
authored
Merge pull request #456 from ColtonWilley/pkcs11_rsa_pss
Add RSA PSS signing to PKCS11 examples
2 parents d56158e + 3f13cc3 commit 7e268ba

2 files changed

Lines changed: 94 additions & 13 deletions

File tree

pkcs11/pkcs11_rsa.c

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ static const unsigned char client_keypub_der_2048[] =
187187
0x03, 0x01, 0x00, 0x01
188188
};
189189
static const int sizeof_client_keypub_der_2048 = sizeof(client_keypub_der_2048);
190+
WC_RNG rng;
190191

191192
static int decode_private_key(RsaKey* key, int devId)
192193
{
@@ -231,20 +232,21 @@ static int decode_public_key(RsaKey* key, int devId)
231232
static int rsa_sign_verify(int devId)
232233
{
233234
int ret = 0;
234-
byte hash[32], sig[2048/8];
235-
word32 hashSz, sigSz;
235+
byte hash[32], pt[32], sig[2048/8];
236+
word32 hashSz, ptSz, sigSz;
236237
RsaKey priv;
237238
RsaKey pub;
238239

239240
memset(hash, 9, sizeof(hash));
240241
hashSz = sizeof(hash);
241242
sigSz = sizeof(sig);
243+
ptSz = sizeof(pt);
242244

243245
ret = decode_private_key(&priv, devId);
244246
if (ret == 0) {
245247
fprintf(stderr, "Signing\n");
246248
sigSz = ret = wc_RsaSSL_Sign(hash, hashSz, sig, (int)sigSz, &priv,
247-
NULL);
249+
&rng);
248250
if (ret < 0)
249251
fprintf(stderr, "Failed to sign: %d\n", ret);
250252
else
@@ -257,7 +259,55 @@ static int rsa_sign_verify(int devId)
257259
ret = decode_public_key(&pub, devId);
258260
if (ret == 0) {
259261
fprintf(stderr, "Verifying\n");
260-
ret = wc_RsaSSL_Verify(sig, sigSz, hash, (int)hashSz, &pub);
262+
ret = wc_RsaSSL_Verify(sig, sigSz, pt, (int)ptSz, &pub);
263+
if (ret < 0)
264+
fprintf(stderr, "Failed to verify: %d\n", ret);
265+
266+
if (XMEMCMP(hash, pt, ret) != 0) {
267+
fprintf(stderr, "Failed to verify\n");
268+
}
269+
270+
wc_FreeRsaKey(&pub);
271+
ret = 0;
272+
}
273+
}
274+
275+
return ret;
276+
}
277+
278+
#ifdef WC_RSA_PSS
279+
static int rsa_sign_verify_pss(int devId)
280+
{
281+
int ret = 0;
282+
byte hash[32], pt[2048/8], sig[2048/8];
283+
word32 hashSz, ptSz, sigSz;
284+
RsaKey priv;
285+
RsaKey pub;
286+
287+
memset(hash, 9, sizeof(hash));
288+
hashSz = sizeof(hash);
289+
sigSz = sizeof(sig);
290+
ptSz = sizeof(pt);
291+
292+
ret = decode_private_key(&priv, devId);
293+
if (ret == 0) {
294+
fprintf(stderr, "PSS Signing\n");
295+
sigSz = ret = wc_RsaPSS_Sign(hash, hashSz, sig, (int)sigSz,
296+
WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &priv, &rng);
297+
if (ret < 0)
298+
fprintf(stderr, "Failed to sign: %d\n", ret);
299+
else
300+
ret = 0;
301+
302+
wc_FreeRsaKey(&priv);
303+
}
304+
305+
if (ret == 0) {
306+
ret = decode_public_key(&pub, devId);
307+
if (ret == 0) {
308+
fprintf(stderr, "PSS Verifying\n");
309+
ret = wc_RsaPSS_VerifyCheck(sig, sigSz, pt, ptSz, hash, hashSz,
310+
WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &pub);
261311
if (ret < 0)
262312
fprintf(stderr, "Failed to verify: %d\n", ret);
263313
else
@@ -269,7 +319,8 @@ static int rsa_sign_verify(int devId)
269319

270320
return ret;
271321
}
272-
#endif
322+
#endif /* ifdef WC_RSA_PSS */
323+
#endif /* ifndef NO_RSA */
273324

274325
int main(int argc, char* argv[])
275326
{
@@ -319,18 +370,32 @@ int main(int argc, char* argv[])
319370
fprintf(stderr, "Failed to register PKCS#11 token\n");
320371
ret = 2;
321372
}
373+
if (ret == 0) {
374+
ret = wc_InitRng(&rng);
375+
if (ret != 0) {
376+
fprintf(stderr, "Failed to initialize RNG: %d\n", ret);
377+
}
378+
}
322379
if (ret == 0) {
323380
#ifndef NO_RSA
324381
ret = rsa_sign_verify(devId);
325382
if (ret != 0)
326383
ret = 1;
384+
#ifdef WC_RSA_PSS
385+
if (ret == 0) {
386+
ret = rsa_sign_verify_pss(devId);
387+
if (ret != 0)
388+
ret = 1;
389+
}
390+
#endif
327391
#endif
328392
}
329393
wc_Pkcs11Token_Final(&token);
330394
}
331395
wc_Pkcs11_Finalize(&dev);
332396
}
333397

398+
wc_FreeRng(&rng);
334399
wolfCrypt_Cleanup();
335400

336401
if (ret == 0)

pkcs11/pkcs11_test.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,10 @@ int rsaenc_test(RsaKey* key)
429429
outSz = sizeof(out);
430430
decSz = sizeof(dec);
431431

432+
#ifdef WC_RSA_BLINDING
433+
ret = wc_RsaSetRNG(key, &rng);
434+
#endif
435+
432436
if (ret == 0) {
433437
outSz = ret = wc_RsaPublicEncrypt_ex(plain, plainSz, out, (int)outSz,
434438
key, &rng, WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL,
@@ -460,27 +464,39 @@ int rsaenc_test(RsaKey* key)
460464
int rsasig_test(RsaKey* key)
461465
{
462466
int ret = 0;
463-
byte plain[128], out[2048/8];
464-
word32 plainSz, outSz;
467+
byte plain[128], sig[2048/8], pt[2048/8];
468+
word32 plainSz, sigSz, ptSz;
465469

466470
memset(plain, 9, sizeof(plain));
467471
plainSz = sizeof(plain);
468-
outSz = sizeof(out);
472+
sigSz = sizeof(sig);
473+
ptSz = sizeof(pt);
469474

470475
if (ret == 0) {
471-
outSz = ret = wc_RsaSSL_Sign(plain, plainSz, out, (int)outSz, key,
472-
NULL);
476+
sigSz = ret = wc_RsaSSL_Sign(plain, plainSz, sig, (int)sigSz, key,
477+
&rng);
473478
if (ret < 0)
474479
fprintf(stderr, "Failed to sign: %d\n", ret);
475480
else
476481
ret = 0;
477482
}
478483
if (ret == 0) {
479-
ret = wc_RsaSSL_Verify(out, outSz, plain, (int)plainSz, key);
484+
ret = wc_RsaSSL_Verify(sig, sigSz, pt, (int)ptSz, key);
480485
if (ret < 0)
481486
fprintf(stderr, "Failed to verify: %d\n", ret);
482-
else
483-
ret = 0;
487+
488+
if (ret != plainSz) {
489+
fprintf(stderr, "Failed to verify: %d\n", ret);
490+
ret = -1;
491+
}
492+
if (ret > 0) {
493+
if (XMEMCMP(plain, pt, ret) != 0) {
494+
ret = -1;
495+
}
496+
else {
497+
ret = 0;
498+
}
499+
}
484500
}
485501

486502
return ret;

0 commit comments

Comments
 (0)