Skip to content

Commit ef5c8dc

Browse files
committed
Add RSA PSS signing to PKCS11 examples
1 parent d56158e commit ef5c8dc

1 file changed

Lines changed: 67 additions & 4 deletions

File tree

pkcs11/pkcs11_rsa.c

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,15 @@ static int decode_public_key(RsaKey* key, int devId)
231231
static int rsa_sign_verify(int devId)
232232
{
233233
int ret = 0;
234-
byte hash[32], sig[2048/8];
235-
word32 hashSz, sigSz;
234+
byte hash[32], pt[32], sig[2048/8];
235+
word32 hashSz, ptSz, sigSz;
236236
RsaKey priv;
237237
RsaKey pub;
238238

239239
memset(hash, 9, sizeof(hash));
240240
hashSz = sizeof(hash);
241241
sigSz = sizeof(sig);
242+
ptSz = sizeof(pt);
242243

243244
ret = decode_private_key(&priv, devId);
244245
if (ret == 0) {
@@ -257,7 +258,61 @@ static int rsa_sign_verify(int devId)
257258
ret = decode_public_key(&pub, devId);
258259
if (ret == 0) {
259260
fprintf(stderr, "Verifying\n");
260-
ret = wc_RsaSSL_Verify(sig, sigSz, hash, (int)hashSz, &pub);
261+
ret = wc_RsaSSL_Verify(sig, sigSz, pt, (int)ptSz, &pub);
262+
if (ret < 0)
263+
fprintf(stderr, "Failed to verify: %d\n", ret);
264+
265+
if(XMEMCMP(hash, pt, ret) != 0) {
266+
fprintf(stderr, "Failed to verify\n");
267+
}
268+
269+
wc_FreeRsaKey(&pub);
270+
ret = 0;
271+
}
272+
}
273+
274+
return ret;
275+
}
276+
277+
#ifdef WC_RSA_PSS
278+
static int rsa_sign_verify_pss(int devId)
279+
{
280+
int ret = 0;
281+
byte hash[32], pt[2048/8], sig[2048/8];
282+
word32 hashSz, ptSz, sigSz;
283+
RsaKey priv;
284+
RsaKey pub;
285+
WC_RNG rng;
286+
287+
memset(hash, 9, sizeof(hash));
288+
hashSz = sizeof(hash);
289+
sigSz = sizeof(sig);
290+
ptSz = sizeof(pt);
291+
292+
ret = wc_InitRng(&rng);
293+
if (ret != 0) {
294+
fprintf(stderr, "Failed to initialize RNG: %d\n", ret);
295+
}
296+
297+
ret = decode_private_key(&priv, devId);
298+
if (ret == 0) {
299+
fprintf(stderr, "PSS Signing\n");
300+
sigSz = ret = wc_RsaPSS_Sign(hash, hashSz, sig, (int)sigSz,
301+
WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &priv, &rng);
302+
if (ret < 0)
303+
fprintf(stderr, "Failed to sign: %d\n", ret);
304+
else
305+
ret = 0;
306+
307+
wc_FreeRsaKey(&priv);
308+
}
309+
310+
if (ret == 0) {
311+
ret = decode_public_key(&pub, devId);
312+
if (ret == 0) {
313+
fprintf(stderr, "PSS Verifying\n");
314+
ret = wc_RsaPSS_VerifyCheck(sig, sigSz, pt, ptSz, hash, hashSz,
315+
WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &pub);
261316
if (ret < 0)
262317
fprintf(stderr, "Failed to verify: %d\n", ret);
263318
else
@@ -269,7 +324,8 @@ static int rsa_sign_verify(int devId)
269324

270325
return ret;
271326
}
272-
#endif
327+
#endif /* ifdef WC_RSA_PSS */
328+
#endif /* ifndef NO_RSA */
273329

274330
int main(int argc, char* argv[])
275331
{
@@ -324,6 +380,13 @@ int main(int argc, char* argv[])
324380
ret = rsa_sign_verify(devId);
325381
if (ret != 0)
326382
ret = 1;
383+
#ifdef WC_RSA_PSS
384+
if (ret == 0) {
385+
ret = rsa_sign_verify_pss(devId);
386+
if (ret != 0)
387+
ret = 1;
388+
}
389+
#endif
327390
#endif
328391
}
329392
wc_Pkcs11Token_Final(&token);

0 commit comments

Comments
 (0)