1515 */
1616package com .licel .jcardsim .crypto ;
1717
18+ import javacard .security .CryptoException ;
19+ import javacard .security .MessageDigest ;
1820import javacard .security .Signature ;
21+ import javacardx .crypto .Cipher ;
1922import org .junit .jupiter .api .Test ;
23+ import org .slf4j .Logger ;
24+ import org .slf4j .LoggerFactory ;
2025
2126import java .lang .reflect .Field ;
2227import java .util .ArrayList ;
2328import java .util .Arrays ;
2429import java .util .List ;
30+ import java .util .stream .Collectors ;
2531
2632public class SignatureProxyTest {
2733
34+ private static final Logger log = LoggerFactory .getLogger (SignatureProxyTest .class );
2835 // The deprecated signature algorithm list is created because JavaCard 3.0.5 API uses only javadoc annotation @deprecated
2936 // And not use the Java annotation @Deprecated, which can be read by java.lang.reflect.Field
3037 // https://docs.oracle.com/javacard/3.0.5/api/javacard/security/Signature.html
@@ -34,10 +41,10 @@ public class SignatureProxyTest {
3441 };
3542
3643 @ Test
37- public void testSupportSignatureForJavaCardv3_0_5 () throws ClassNotFoundException {
44+ public void testSupportSignatureForJavaCardv3_0_5 () {
3845 ArrayList <Field > signature_alg_fields = new ArrayList <>();
3946
40- for (Field field : Class . forName ( "javacard.security. Signature" ) .getDeclaredFields ()) {
47+ for (Field field : Signature . class .getDeclaredFields ()) {
4148 if (field .getName ().startsWith ("ALG_" )) {
4249 List <String > deprecated_list = Arrays .asList (SIGNATURE_DEPRECATED_ALG_JAVACARD_V3_0_5 );
4350 if (!deprecated_list .contains (field .getName ()))
@@ -48,8 +55,32 @@ public void testSupportSignatureForJavaCardv3_0_5() throws ClassNotFoundExceptio
4855 for (Field alg_field : signature_alg_fields ) {
4956 try {
5057 Signature sig = Signature .getInstance (alg_field .getByte (null ), false );
51- } catch (Throwable ex ) {
52- System .err .println (alg_field .getName () + " is not implemented." );
58+ } catch (CryptoException ex ) {
59+ if (ex .getReason () == CryptoException .NO_SUCH_ALGORITHM ) {
60+ log .warn ("Implemented: getInstance({})" , alg_field .getName ());
61+ } else {
62+ log .error ("Invalid implementation: {}" , alg_field .getName ());
63+ }
64+ } catch (IllegalAccessException e ) {
65+ throw new RuntimeException (e );
66+ }
67+ }
68+
69+ for (Field md : Arrays .stream (MessageDigest .class .getDeclaredFields ()).filter (n -> n .getName ().startsWith ("ALG_" )).collect (Collectors .toList ())) {
70+ for (Field ca : Arrays .stream (Signature .class .getDeclaredFields ()).filter (n -> n .getName ().startsWith ("SIG_CIPHER_" )).collect (Collectors .toList ())) {
71+ for (Field pad : Arrays .stream (Cipher .class .getDeclaredFields ()).filter (n -> n .getName ().startsWith ("PAD_" )).collect (Collectors .toList ())) {
72+ try {
73+ Signature sig = Signature .getInstance (md .getByte (null ), ca .getByte (null ), pad .getByte (null ), false );
74+ log .info ("Implemented: getInstance({}, {}, {})" , md .getName (), ca .getName (), pad .getName ());
75+ } catch (CryptoException e ) {
76+ if (e .getReason () != CryptoException .NO_SUCH_ALGORITHM ) {
77+ log .error ("Invalid implementation: {}, {}, {}" , md .getName (), ca .getName (), pad .getName ());
78+ }
79+ // No point in spamming the log.
80+ } catch (IllegalAccessException e ) {
81+ throw new RuntimeException (e );
82+ }
83+ }
5384 }
5485 }
5586 }
0 commit comments