2121
2222package com .wolfssl .test ;
2323
24+ import org .junit .Assume ;
2425import org .junit .BeforeClass ;
2526import org .junit .Rule ;
2627import org .junit .Test ;
2728import org .junit .rules .TestRule ;
2829import static org .junit .Assert .assertNotNull ;
30+ import static org .junit .Assert .assertEquals ;
2931
32+ import java .nio .ByteBuffer ;
33+
34+ import com .wolfssl .WolfSSL ;
3035import com .wolfssl .WolfSSLException ;
3136import com .wolfssl .WolfCryptRSA ;
3237
@@ -36,12 +41,129 @@ public class WolfCryptRSATest {
3641 public TestRule testWatcher = TimedTestWatcher .create ();
3742
3843 @ BeforeClass
39- public static void beforeClass () {
44+ public static void beforeClass () throws WolfSSLException {
4045 System .out .println ("WolfCryptRSA Class" );
46+ WolfSSL .loadLibrary ();
4147 }
4248
4349 @ Test
4450 public void testRSANew () throws WolfSSLException {
4551 assertNotNull (new WolfCryptRSA ());
4652 }
53+
54+ /* A size larger than its backing direct buffer must be rejected */
55+ @ Test
56+ public void testDoSignRejectsOversizedSz () {
57+ Assume .assumeTrue (WolfSSL .RsaEnabled ());
58+ WolfCryptRSA rsa = new WolfCryptRSA ();
59+ ByteBuffer in = ByteBuffer .allocateDirect (64 );
60+ ByteBuffer out = ByteBuffer .allocateDirect (256 );
61+ ByteBuffer key = ByteBuffer .allocateDirect (128 );
62+
63+ assertEquals (-1 , rsa .doSign (in , 65 , out , new int []{256 }, key , 128 ));
64+ assertEquals (-1 , rsa .doSign (in , 64 , out , new int []{257 }, key , 128 ));
65+ assertEquals (-1 ,
66+ rsa .doSign (in , 64 , out , new int []{256 }, key , 0x100000000L + 16 ));
67+ }
68+
69+ @ Test
70+ public void testDoEncRejectsOversizedSz () {
71+ Assume .assumeTrue (WolfSSL .RsaEnabled ());
72+ WolfCryptRSA rsa = new WolfCryptRSA ();
73+ ByteBuffer in = ByteBuffer .allocateDirect (64 );
74+ ByteBuffer out = ByteBuffer .allocateDirect (256 );
75+ ByteBuffer key = ByteBuffer .allocateDirect (128 );
76+
77+ assertEquals (-1 , rsa .doEnc (in , 65 , out , new int []{256 }, key , 128 ));
78+ assertEquals (-1 , rsa .doEnc (in , 64 , out , new int []{257 }, key , 128 ));
79+ assertEquals (-1 ,
80+ rsa .doEnc (in , 64 , out , new int []{256 }, key , 0x100000000L + 16 ));
81+ }
82+
83+ @ Test
84+ public void testDoVerifyRejectsOversizedSz () {
85+ Assume .assumeTrue (WolfSSL .RsaEnabled ());
86+ WolfCryptRSA rsa = new WolfCryptRSA ();
87+ ByteBuffer sig = ByteBuffer .allocateDirect (64 );
88+ ByteBuffer out = ByteBuffer .allocateDirect (256 );
89+ ByteBuffer key = ByteBuffer .allocateDirect (128 );
90+
91+ assertEquals (-1 , rsa .doVerify (sig , 65 , out , 256 , key , 128 ));
92+ assertEquals (-1 , rsa .doVerify (sig , 64 , out , 257 , key , 128 ));
93+ assertEquals (-1 ,
94+ rsa .doVerify (sig , 64 , out , 256 , key , 0x100000000L + 16 ));
95+ }
96+
97+ @ Test
98+ public void testDoDecRejectsOversizedSz () {
99+ Assume .assumeTrue (WolfSSL .RsaEnabled ());
100+ WolfCryptRSA rsa = new WolfCryptRSA ();
101+ ByteBuffer in = ByteBuffer .allocateDirect (64 );
102+ ByteBuffer out = ByteBuffer .allocateDirect (256 );
103+ ByteBuffer key = ByteBuffer .allocateDirect (128 );
104+
105+ assertEquals (-1 , rsa .doDec (in , 65 , out , 256 , key , 128 ));
106+ assertEquals (-1 , rsa .doDec (in , 64 , out , 257 , key , 128 ));
107+ assertEquals (-1 ,
108+ rsa .doDec (in , 64 , out , 256 , key , 0x100000000L + 16 ));
109+ }
110+
111+ /* SHA-256 hash OID sum, which wolfSSL encodes two ways depending on the
112+ * build: current default, or the legacy WOLFSSL_OLD_OID_SUM value
113+ * (see wolfSSL oid_sum.h). */
114+ private static final int SHA256_OID = 0x7cb37afb ;
115+ private static final int SHA256_OID_OLD = 414 ;
116+
117+ /* Return a SHA-256 hash OID wc_OidGetHash accepts, or -1 if none
118+ * (PSS not compiled, or an OID-sum scheme we do not know). */
119+ private static int findPssHashOid (WolfCryptRSA rsa ) {
120+ ByteBuffer b = ByteBuffer .allocateDirect (64 );
121+ int [] candidates = { SHA256_OID , SHA256_OID_OLD };
122+ for (int oid : candidates ) {
123+ int ret = rsa .doPssVerify (b , 64 , b , 64 , oid , 0 , b , 64 );
124+ if (ret != -1 && ret != WolfSSL .NOT_COMPILED_IN ) {
125+ return oid ;
126+ }
127+ }
128+ return -1 ;
129+ }
130+
131+ @ Test
132+ public void testDoPssSignRejectsOversizedSz () {
133+ Assume .assumeTrue (WolfSSL .RsaEnabled ());
134+ WolfCryptRSA rsa = new WolfCryptRSA ();
135+ int oid = findPssHashOid (rsa );
136+ Assume .assumeTrue ("PSS unavailable or unknown hash OID scheme" ,
137+ oid != -1 );
138+
139+ ByteBuffer in = ByteBuffer .allocateDirect (64 );
140+ ByteBuffer out = ByteBuffer .allocateDirect (256 );
141+ ByteBuffer key = ByteBuffer .allocateDirect (128 );
142+
143+ assertEquals (-1 ,
144+ rsa .doPssSign (in , 65 , out , new int []{256 }, oid , 0 , key , 128 ));
145+ assertEquals (-1 ,
146+ rsa .doPssSign (in , 64 , out , new int []{257 }, oid , 0 , key , 128 ));
147+ assertEquals (-1 ,
148+ rsa .doPssSign (in , 64 , out , new int []{256 }, oid , 0 , key ,
149+ 0x100000000L + 16 ));
150+ }
151+
152+ @ Test
153+ public void testDoPssVerifyRejectsOversizedSz () {
154+ Assume .assumeTrue (WolfSSL .RsaEnabled ());
155+ WolfCryptRSA rsa = new WolfCryptRSA ();
156+ int oid = findPssHashOid (rsa );
157+ Assume .assumeTrue ("PSS unavailable or unknown hash OID scheme" ,
158+ oid != -1 );
159+
160+ ByteBuffer sig = ByteBuffer .allocateDirect (64 );
161+ ByteBuffer out = ByteBuffer .allocateDirect (256 );
162+ ByteBuffer key = ByteBuffer .allocateDirect (128 );
163+
164+ assertEquals (-1 , rsa .doPssVerify (sig , 65 , out , 256 , oid , 0 , key , 128 ));
165+ assertEquals (-1 , rsa .doPssVerify (sig , 64 , out , 257 , oid , 0 , key , 128 ));
166+ assertEquals (-1 ,
167+ rsa .doPssVerify (sig , 64 , out , 256 , oid , 0 , key , 0x100000000L + 16 ));
168+ }
47169}
0 commit comments