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
@@ -44,4 +49,114 @@ public static void beforeClass() {
4449 public void testRSANew () throws WolfSSLException {
4550 assertNotNull (new WolfCryptRSA ());
4651 }
52+
53+ /* A size larger than its backing direct buffer must be rejected */
54+ @ Test
55+ public void testDoSignRejectsOversizedSz () {
56+ WolfCryptRSA rsa = new WolfCryptRSA ();
57+ ByteBuffer in = ByteBuffer .allocateDirect (64 );
58+ ByteBuffer out = ByteBuffer .allocateDirect (256 );
59+ ByteBuffer key = ByteBuffer .allocateDirect (128 );
60+
61+ assertEquals (-1 , rsa .doSign (in , 65 , out , new int []{256 }, key , 128 ));
62+ assertEquals (-1 , rsa .doSign (in , 64 , out , new int []{257 }, key , 128 ));
63+ assertEquals (-1 ,
64+ rsa .doSign (in , 64 , out , new int []{256 }, key , 0x100000000L + 16 ));
65+ }
66+
67+ @ Test
68+ public void testDoEncRejectsOversizedSz () {
69+ WolfCryptRSA rsa = new WolfCryptRSA ();
70+ ByteBuffer in = ByteBuffer .allocateDirect (64 );
71+ ByteBuffer out = ByteBuffer .allocateDirect (256 );
72+ ByteBuffer key = ByteBuffer .allocateDirect (128 );
73+
74+ assertEquals (-1 , rsa .doEnc (in , 65 , out , new int []{256 }, key , 128 ));
75+ assertEquals (-1 , rsa .doEnc (in , 64 , out , new int []{257 }, key , 128 ));
76+ assertEquals (-1 ,
77+ rsa .doEnc (in , 64 , out , new int []{256 }, key , 0x100000000L + 16 ));
78+ }
79+
80+ @ Test
81+ public void testDoVerifyRejectsOversizedSz () {
82+ WolfCryptRSA rsa = new WolfCryptRSA ();
83+ ByteBuffer sig = ByteBuffer .allocateDirect (64 );
84+ ByteBuffer out = ByteBuffer .allocateDirect (256 );
85+ ByteBuffer key = ByteBuffer .allocateDirect (128 );
86+
87+ assertEquals (-1 , rsa .doVerify (sig , 65 , out , 256 , key , 128 ));
88+ assertEquals (-1 , rsa .doVerify (sig , 64 , out , 257 , key , 128 ));
89+ assertEquals (-1 ,
90+ rsa .doVerify (sig , 64 , out , 256 , key , 0x100000000L + 16 ));
91+ }
92+
93+ @ Test
94+ public void testDoDecRejectsOversizedSz () {
95+ WolfCryptRSA rsa = new WolfCryptRSA ();
96+ ByteBuffer in = ByteBuffer .allocateDirect (64 );
97+ ByteBuffer out = ByteBuffer .allocateDirect (256 );
98+ ByteBuffer key = ByteBuffer .allocateDirect (128 );
99+
100+ assertEquals (-1 , rsa .doDec (in , 65 , out , 256 , key , 128 ));
101+ assertEquals (-1 , rsa .doDec (in , 64 , out , 257 , key , 128 ));
102+ assertEquals (-1 ,
103+ rsa .doDec (in , 64 , out , 256 , key , 0x100000000L + 16 ));
104+ }
105+
106+ /* SHA-256 hash OID sum, which wolfSSL encodes two ways depending on the
107+ * build: current default, or the legacy WOLFSSL_OLD_OID_SUM value
108+ * (see wolfSSL oid_sum.h). */
109+ private static final int SHA256_OID = 0x7cb37afb ;
110+ private static final int SHA256_OID_OLD = 414 ;
111+
112+ /* Return a SHA-256 hash OID wc_OidGetHash accepts, or -1 if none
113+ * (PSS not compiled, or an OID-sum scheme we do not know). */
114+ private static int findPssHashOid (WolfCryptRSA rsa ) {
115+ ByteBuffer b = ByteBuffer .allocateDirect (64 );
116+ int [] candidates = { SHA256_OID , SHA256_OID_OLD };
117+ for (int oid : candidates ) {
118+ int ret = rsa .doPssVerify (b , 64 , b , 64 , oid , 0 , b , 64 );
119+ if (ret != -1 && ret != WolfSSL .NOT_COMPILED_IN ) {
120+ return oid ;
121+ }
122+ }
123+ return -1 ;
124+ }
125+
126+ @ Test
127+ public void testDoPssSignRejectsOversizedSz () {
128+ WolfCryptRSA rsa = new WolfCryptRSA ();
129+ int oid = findPssHashOid (rsa );
130+ Assume .assumeTrue ("PSS unavailable or unknown hash OID scheme" ,
131+ oid != -1 );
132+
133+ ByteBuffer in = ByteBuffer .allocateDirect (64 );
134+ ByteBuffer out = ByteBuffer .allocateDirect (256 );
135+ ByteBuffer key = ByteBuffer .allocateDirect (128 );
136+
137+ assertEquals (-1 ,
138+ rsa .doPssSign (in , 65 , out , new int []{256 }, oid , 0 , key , 128 ));
139+ assertEquals (-1 ,
140+ rsa .doPssSign (in , 64 , out , new int []{257 }, oid , 0 , key , 128 ));
141+ assertEquals (-1 ,
142+ rsa .doPssSign (in , 64 , out , new int []{256 }, oid , 0 , key ,
143+ 0x100000000L + 16 ));
144+ }
145+
146+ @ Test
147+ public void testDoPssVerifyRejectsOversizedSz () {
148+ WolfCryptRSA rsa = new WolfCryptRSA ();
149+ int oid = findPssHashOid (rsa );
150+ Assume .assumeTrue ("PSS unavailable or unknown hash OID scheme" ,
151+ oid != -1 );
152+
153+ ByteBuffer sig = ByteBuffer .allocateDirect (64 );
154+ ByteBuffer out = ByteBuffer .allocateDirect (256 );
155+ ByteBuffer key = ByteBuffer .allocateDirect (128 );
156+
157+ assertEquals (-1 , rsa .doPssVerify (sig , 65 , out , 256 , oid , 0 , key , 128 ));
158+ assertEquals (-1 , rsa .doPssVerify (sig , 64 , out , 257 , oid , 0 , key , 128 ));
159+ assertEquals (-1 ,
160+ rsa .doPssVerify (sig , 64 , out , 256 , oid , 0 , key , 0x100000000L + 16 ));
161+ }
47162}
0 commit comments