1+ package org .cryptomator .siv ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .BeforeEach ;
5+ import org .junit .jupiter .api .Test ;
6+ import org .junit .jupiter .params .ParameterizedTest ;
7+ import org .junit .jupiter .params .provider .ValueSource ;
8+
9+ import javax .crypto .BadPaddingException ;
10+ import javax .crypto .Cipher ;
11+ import javax .crypto .IllegalBlockSizeException ;
12+ import javax .crypto .NoSuchPaddingException ;
13+ import javax .crypto .SecretKey ;
14+ import javax .crypto .ShortBufferException ;
15+ import javax .crypto .spec .SecretKeySpec ;
16+ import java .nio .charset .StandardCharsets ;
17+ import java .security .InvalidKeyException ;
18+ import java .security .NoSuchAlgorithmException ;
19+
20+ class SivCipherTest {
21+
22+ private SivCipher cipher ;
23+ private SecretKey key ;
24+
25+ @ BeforeEach
26+ public void setUp () {
27+ this .cipher = new SivCipher ();
28+ this .key = new SecretKeySpec (new byte [64 ], "AES" );
29+ }
30+
31+ @ Test
32+ public void testEngineGetBlockSize () {
33+ Assertions .assertEquals (16 , cipher .engineGetBlockSize ());
34+ }
35+
36+ @ ParameterizedTest
37+ @ ValueSource (strings = {"SIV" , "siv" , "sIv" })
38+ public void testEngineSetModeValid (String mode ) {
39+ Assertions .assertDoesNotThrow (() -> cipher .engineSetMode (mode ));
40+ }
41+
42+ @ ParameterizedTest
43+ @ ValueSource (strings = {"CBC" , "GCM" , "invalid" })
44+ public void testEngineSetModeInvalid (String mode ) {
45+ Assertions .assertThrows (NoSuchAlgorithmException .class , () -> cipher .engineSetMode (mode ));
46+ }
47+
48+ @ ParameterizedTest
49+ @ ValueSource (strings = {"NoPadding" , "nopadding" })
50+ public void testEngineSetPaddingValid (String padding ) {
51+ Assertions .assertDoesNotThrow (() -> cipher .engineSetPadding (padding ));
52+ }
53+
54+ @ ParameterizedTest
55+ @ ValueSource (strings = {"PKCS5Padding" , "PKCS7Padding" , "invalid" })
56+ public void testEngineSetPaddingInvalid (String padding ) {
57+ Assertions .assertThrows (NoSuchPaddingException .class , () -> cipher .engineSetPadding (padding ));
58+ }
59+
60+ @ Test
61+ public void testEngineGetIV () {
62+ Assertions .assertNull (cipher .engineGetIV ());
63+ }
64+
65+ @ Test
66+ public void testEngineGetParameters () {
67+ Assertions .assertNull (cipher .engineGetParameters ());
68+ }
69+
70+ @ ParameterizedTest
71+ @ ValueSource (ints = {32 , 48 , 64 })
72+ public void testEngineInitWithValidKeySize (int keysize ) {
73+ SecretKeySpec key = new SecretKeySpec (new byte [keysize ], "AES" );
74+ Assertions .assertDoesNotThrow (() -> cipher .engineInit (Cipher .ENCRYPT_MODE , key , null ));
75+ }
76+
77+ @ ParameterizedTest
78+ @ ValueSource (ints = {16 , 24 , 1337 })
79+ public void testEngineInitWithInvalidKeySize (int keysize ) {
80+ SecretKeySpec key = new SecretKeySpec (new byte [keysize ], "AES" );
81+ Assertions .assertThrows (InvalidKeyException .class ,() -> cipher .engineInit (Cipher .ENCRYPT_MODE , key , null ));
82+ }
83+
84+ @ Test
85+ public void testEngineInitWithAlgorithmParameterSpec () {
86+ Assertions .assertDoesNotThrow (() -> cipher .engineInit (Cipher .ENCRYPT_MODE , key , (java .security .spec .AlgorithmParameterSpec ) null , null ));
87+ }
88+
89+ @ Test
90+ public void testEngineInitWithAlgorithmParameters () {
91+ Assertions .assertDoesNotThrow (() -> cipher .engineInit (Cipher .ENCRYPT_MODE , key , (java .security .AlgorithmParameters ) null , null ));
92+ }
93+
94+ @ ParameterizedTest
95+ @ ValueSource (ints = {0 , 1 , 15 , 16 , 17 , 100 })
96+ public void testEngineGetOutputSizeEncryptMode (int inLen ) throws InvalidKeyException {
97+ cipher .engineInit (Cipher .ENCRYPT_MODE , key , null );
98+ Assertions .assertEquals (16 + inLen , cipher .engineGetOutputSize (inLen ));
99+ }
100+
101+ @ ParameterizedTest
102+ @ ValueSource (ints = {16 , 17 , 100 })
103+ public void testEngineGetOutputSizeDecryptMode (int inLen ) throws InvalidKeyException {
104+ cipher .engineInit (Cipher .DECRYPT_MODE , key , null );
105+ Assertions .assertEquals (inLen - 16 , cipher .engineGetOutputSize (inLen ));
106+ }
107+
108+ @ Test
109+ public void testWrapAndUnwrap () throws InvalidKeyException , ShortBufferException , IllegalBlockSizeException , BadPaddingException {
110+ // wrap:
111+ cipher .engineInit (Cipher .WRAP_MODE , key , null );
112+ cipher .engineUpdateAAD (StandardCharsets .UTF_8 .encode ("aad1" ));
113+ cipher .engineUpdateAAD ("aad2" .getBytes (StandardCharsets .UTF_8 ), 1 , 2 );
114+ byte [] wrapped1 = cipher .engineUpdate ("hello" .getBytes (StandardCharsets .UTF_8 ), 0 , "hello" .length ());
115+ byte [] wrapped2 = new byte [0 ];
116+ int wrapped2Len = cipher .engineUpdate ("world" .getBytes (StandardCharsets .UTF_8 ), 0 , "world" .length (), wrapped2 , 0 );
117+ Assertions .assertEquals (0 , wrapped1 .length );
118+ Assertions .assertEquals (0 , wrapped2Len );
119+ byte [] wrapped = cipher .engineDoFinal ("!" .getBytes (StandardCharsets .UTF_8 ), 0 , 1 );
120+
121+ // unwrap:
122+ cipher .engineInit (Cipher .UNWRAP_MODE , key , null );
123+ cipher .engineUpdateAAD (StandardCharsets .UTF_8 .encode ("aad1" ));
124+ cipher .engineUpdateAAD ("aad2" .getBytes (StandardCharsets .UTF_8 ), 1 , 2 );
125+ byte [] unwrapped1 = cipher .engineUpdate (wrapped , 0 , 5 );
126+ byte [] unwrapped2 = new byte [0 ];
127+ int unwrapped2Len = cipher .engineUpdate (wrapped , 5 , 5 , unwrapped2 , 0 );
128+ Assertions .assertEquals (0 , unwrapped1 .length );
129+ Assertions .assertEquals (0 , unwrapped2Len );
130+ byte [] unwrapped = cipher .engineDoFinal (wrapped , 10 , wrapped .length - 10 );
131+
132+ // compare:
133+ Assertions .assertEquals ("helloworld!" , new String (unwrapped , StandardCharsets .UTF_8 ));
134+ }
135+ }
0 commit comments