@@ -187,6 +187,7 @@ static const unsigned char client_keypub_der_2048[] =
187187 0x03 , 0x01 , 0x00 , 0x01
188188};
189189static const int sizeof_client_keypub_der_2048 = sizeof (client_keypub_der_2048 );
190+ WC_RNG rng ;
190191
191192static int decode_private_key (RsaKey * key , int devId )
192193{
@@ -231,20 +232,21 @@ static int decode_public_key(RsaKey* key, int devId)
231232static 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
274325int 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 )
0 commit comments