Skip to content

Commit 7805496

Browse files
committed
Update PKCS11 examples to set RNG for RSA operations
1 parent ef5c8dc commit 7805496

2 files changed

Lines changed: 33 additions & 15 deletions

File tree

pkcs11/pkcs11_rsa.c

Lines changed: 9 additions & 7 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
{
@@ -245,7 +246,7 @@ static int rsa_sign_verify(int devId)
245246
if (ret == 0) {
246247
fprintf(stderr, "Signing\n");
247248
sigSz = ret = wc_RsaSSL_Sign(hash, hashSz, sig, (int)sigSz, &priv,
248-
NULL);
249+
&rng);
249250
if (ret < 0)
250251
fprintf(stderr, "Failed to sign: %d\n", ret);
251252
else
@@ -282,18 +283,12 @@ static int rsa_sign_verify_pss(int devId)
282283
word32 hashSz, ptSz, sigSz;
283284
RsaKey priv;
284285
RsaKey pub;
285-
WC_RNG rng;
286286

287287
memset(hash, 9, sizeof(hash));
288288
hashSz = sizeof(hash);
289289
sigSz = sizeof(sig);
290290
ptSz = sizeof(pt);
291291

292-
ret = wc_InitRng(&rng);
293-
if (ret != 0) {
294-
fprintf(stderr, "Failed to initialize RNG: %d\n", ret);
295-
}
296-
297292
ret = decode_private_key(&priv, devId);
298293
if (ret == 0) {
299294
fprintf(stderr, "PSS Signing\n");
@@ -375,6 +370,12 @@ int main(int argc, char* argv[])
375370
fprintf(stderr, "Failed to register PKCS#11 token\n");
376371
ret = 2;
377372
}
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+
}
378379
if (ret == 0) {
379380
#ifndef NO_RSA
380381
ret = rsa_sign_verify(devId);
@@ -394,6 +395,7 @@ int main(int argc, char* argv[])
394395
wc_Pkcs11_Finalize(&dev);
395396
}
396397

398+
wc_FreeRng(&rng);
397399
wolfCrypt_Cleanup();
398400

399401
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)