2121
2222/* This file is an example of signing and verifying an RSA signature.
2323 * The signature can be PKCS#1.5 formatted and PSS formatted.
24- *
24+ *
2525 * - PKCS#1.5
2626 * 1. hash -> encSig
27- * 2. encSig -> signature
27+ * 2. encSig -> signature
2828 * 3. signature -> decSig
29- *
29+ *
3030 * - PSS
31- * 1. hash -> signature
31+ * 1. hash -> signature
3232 * 2. signature -> decSig
33- *
33+ *
3434 * PKCS#1.5 is used for the Signature by default.
3535 * To turning on PSS, define PSS_PADDING
3636 */
4949/* RSA Key size bits */
5050#define RSA_KEY_SIZE 2048
5151
52-
53-
54-
55-
5652#define CHECK_RET (a , b , eLabel , msg ) { \
5753 if (a != b) { \
5854 printf("failed %s\n", msg); \
@@ -115,21 +111,21 @@ int sign(){
115111 ret = wc_MakeRsaKey (& key , RSA_KEY_SIZE , e , & rng );
116112 CHECK_RET (ret , 0 , finish , "wc_MakeRsaKey()" );
117113
118- /* Encode digest with algorithm information as per PKCS#1.5 */
119- encSigLen = wc_EncodeSignature (encSig , hash , sizeof (hash ), SHA256h );
120- if ((int )encSigLen < 0 )
121- ret = (int )encSigLen ;
122- CHECK_RET (ret , 0 , finish , "wc_EncodeSignature()" );
123-
124114#ifdef PSS_PADDING
125- sigLen = wc_RsaPSS_Sign (hash , sizeof (hash ), signature , sizeof (signature )\
126- , WC_HASH_TYPE_SHA256 , WC_MGF1SHA256 , & key , & rng );
115+ sigLen = wc_RsaPSS_Sign (hash , sizeof (hash ), signature , sizeof (signature ),
116+ WC_HASH_TYPE_SHA256 , WC_MGF1SHA256 , & key , & rng );
127117 if ((int )sigLen < 0 )
128118 ret = (int )sigLen ;
129119 CHECK_RET (ret , 0 , finish , "wc_RsaPSS_Sign()" );
130120
131121#else /* PKCS#1.5 */
132- sigLen = wc_RsaSSL_Sign (encSig , encSigLen , signature , sizeof (signature ),\
122+ /* Encode digest with algorithm information as per PKCS#1.5 */
123+ encSigLen = wc_EncodeSignature (encSig , hash , sizeof (hash ), SHA256h );
124+ if ((int )encSigLen < 0 )
125+ ret = (int )encSigLen ;
126+ CHECK_RET (ret , 0 , finish , "wc_EncodeSignature()" );
127+
128+ sigLen = wc_RsaSSL_Sign (encSig , encSigLen , signature , sigBuffLen ,
133129 & key , NULL );
134130 if ((int )sigLen < 0 )
135131 ret = (int )sigLen ;
@@ -182,7 +178,7 @@ int verify(){
182178 return -1 ;
183179 }
184180
185- #ifdef BENCHMARK
181+ #ifdef BENCHMARK
186182 count = 0 ;
187183 printf ("Running benchmark...\n" );
188184 printf ("Please Wait %.2f seconds\n" , (double )BENCH_TIME_SEC );
@@ -191,15 +187,16 @@ int verify(){
191187#endif
192188
193189 /* Verify the signature by decrypting the value. */
194-
190+
195191 #ifdef PSS_PADDING
196192 decSigLen = wc_RsaPSS_VerifyCheck (signature , sizeof (signature ),
197- decSig , sizeof (decSig ), hash , sizeof (hash ), WC_HASH_TYPE_SHA256 , WC_MGF1SHA256 , & key );
198-
193+ decSig , sizeof (decSig ), hash , sizeof (hash ),
194+ WC_HASH_TYPE_SHA256 , WC_MGF1SHA256 , & key );
195+
199196 if ((int )decSigLen < 0 )
200197 ret = (int )decSigLen ;
201198 CHECK_RET (ret , 0 , finish , "wc_RsaPSS_VerifyCheck()" );
202-
199+
203200 #else /* PKCS#1.5 */
204201 decSigLen = wc_RsaSSL_Verify (signature , sizeof (signature ),
205202 decSig , sizeof (decSig ), & key );
@@ -220,11 +217,11 @@ int verify(){
220217
221218 #endif
222219
223- #ifdef BENCHMARK
220+ #ifdef BENCHMARK
224221 count ++ ;
225222 }
226-
227- printf ( "Takes %1.2f Sec for %d times, %6.2f Cycles/sec\n" , total_time , count , count /total_time );
223+ printf ( "Takes %1.2f Sec for %d times, %6.2f Cycles/sec\n" ,
224+ total_time , count , count /total_time );
228225 printf ("Finished Benchmark \n" );
229226#elif defined(DEBUG_MEMORY )
230227
@@ -248,8 +245,6 @@ int verify(){
248245 return ret ;
249246}
250247
251-
252-
253248int main (){
254249 int ret = 0 ;
255250#ifdef BENCHMARK
@@ -270,7 +265,7 @@ int main(){
270265
271266#ifdef DEBUG_MEMORY
272267 ret = StackSizeCheck (NULL , (thread_func )sign );
273- #else
268+ #else
274269 ret = sign ();
275270#endif
276271
@@ -280,7 +275,7 @@ int main(){
280275
281276#ifdef DEBUG_MEMORY
282277 ret = StackSizeCheck (NULL , (thread_func )verify );
283- #else
278+ #else
284279 ret = verify ();
285280#endif
286281 return ret ;
0 commit comments