3
3
*/
4
4
package net .snowflake .client .jdbc .cloud .storage ;
5
5
6
- import static java .nio .file .StandardOpenOption .CREATE ;
7
- import static java .nio .file .StandardOpenOption .READ ;
6
+ import net .snowflake .client .core .SnowflakeJdbcInternalApi ;
7
+ import net .snowflake .client .jdbc .MatDesc ;
8
+ import net .snowflake .common .core .RemoteStoreFileEncryptionMaterial ;
8
9
10
+ import javax .crypto .BadPaddingException ;
11
+ import javax .crypto .Cipher ;
12
+ import javax .crypto .CipherInputStream ;
13
+ import javax .crypto .IllegalBlockSizeException ;
14
+ import javax .crypto .NoSuchPaddingException ;
15
+ import javax .crypto .SecretKey ;
16
+ import javax .crypto .spec .GCMParameterSpec ;
17
+ import javax .crypto .spec .SecretKeySpec ;
9
18
import java .io .File ;
10
19
import java .io .FileOutputStream ;
11
20
import java .io .IOException ;
18
27
import java .security .NoSuchAlgorithmException ;
19
28
import java .security .SecureRandom ;
20
29
import java .util .Base64 ;
21
- import javax .crypto .BadPaddingException ;
22
- import javax .crypto .Cipher ;
23
- import javax .crypto .CipherInputStream ;
24
- import javax .crypto .IllegalBlockSizeException ;
25
- import javax .crypto .NoSuchPaddingException ;
26
- import javax .crypto .SecretKey ;
27
- import javax .crypto .spec .GCMParameterSpec ;
28
- import javax .crypto .spec .SecretKeySpec ;
29
- import net .snowflake .client .jdbc .MatDesc ;
30
- import net .snowflake .common .core .RemoteStoreFileEncryptionMaterial ;
31
30
32
- class GcmEncryptionProvider {
31
+ import static java .nio .file .StandardOpenOption .CREATE ;
32
+ import static java .nio .file .StandardOpenOption .READ ;
33
+
34
+ @ SnowflakeJdbcInternalApi
35
+ public class GcmEncryptionProvider {
33
36
private static final int TAG_LENGTH_IN_BITS = 128 ;
34
37
private static final int IV_LENGTH_IN_BYTES = 12 ;
35
38
private static final String AES = "AES" ;
36
- private static final String FILE_CIPHER = "AES/GCM/NoPadding" ;
37
- private static final String KEY_CIPHER = "AES/GCM/NoPadding" ;
38
39
private static final int BUFFER_SIZE = 8 * 1024 * 1024 ; // 2 MB
39
40
private static final ThreadLocal <SecureRandom > random =
40
41
ThreadLocal .withInitial (SecureRandom ::new );
@@ -85,7 +86,7 @@ private static byte[] encryptKey(byte[] kekBytes, byte[] keyBytes, byte[] keyIvD
85
86
BadPaddingException , NoSuchPaddingException , NoSuchAlgorithmException {
86
87
SecretKey kek = new SecretKeySpec (kekBytes , 0 , kekBytes .length , AES );
87
88
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec (TAG_LENGTH_IN_BITS , keyIvData );
88
- Cipher keyCipher = Cipher .getInstance (KEY_CIPHER );
89
+ Cipher keyCipher = Cipher .getInstance (JCE_CIPHER_NAME );
89
90
keyCipher .init (Cipher .ENCRYPT_MODE , kek , gcmParameterSpec );
90
91
if (aad != null ) {
91
92
keyCipher .updateAAD (aad );
@@ -99,7 +100,7 @@ private static CipherInputStream encryptContent(
99
100
NoSuchAlgorithmException {
100
101
SecretKey fileKey = new SecretKeySpec (keyBytes , 0 , keyBytes .length , AES );
101
102
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec (TAG_LENGTH_IN_BITS , dataIvBytes );
102
- Cipher fileCipher = Cipher .getInstance (FILE_CIPHER );
103
+ Cipher fileCipher = Cipher .getInstance (JCE_CIPHER_NAME );
103
104
fileCipher .init (Cipher .ENCRYPT_MODE , fileKey , gcmParameterSpec );
104
105
if (aad != null ) {
105
106
fileCipher .updateAAD (aad );
@@ -172,7 +173,7 @@ private static CipherInputStream decryptContentFromStream(
172
173
NoSuchAlgorithmException {
173
174
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec (TAG_LENGTH_IN_BITS , ivBytes );
174
175
SecretKey fileKey = new SecretKeySpec (fileKeyBytes , AES );
175
- Cipher fileCipher = Cipher .getInstance (FILE_CIPHER );
176
+ Cipher fileCipher = Cipher .getInstance (JCE_CIPHER_NAME );
176
177
fileCipher .init (Cipher .DECRYPT_MODE , fileKey , gcmParameterSpec );
177
178
if (aad != null ) {
178
179
fileCipher .updateAAD (aad );
@@ -187,7 +188,7 @@ private static void decryptContentFromFile(
187
188
SecretKey fileKey = new SecretKeySpec (fileKeyBytes , AES );
188
189
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec (TAG_LENGTH_IN_BITS , cekIvBytes );
189
190
byte [] buffer = new byte [BUFFER_SIZE ];
190
- Cipher fileCipher = Cipher .getInstance (FILE_CIPHER );
191
+ Cipher fileCipher = Cipher .getInstance (JCE_CIPHER_NAME );
191
192
fileCipher .init (Cipher .DECRYPT_MODE , fileKey , gcmParameterSpec );
192
193
if (aad != null ) {
193
194
fileCipher .updateAAD (aad );
@@ -215,7 +216,7 @@ private static byte[] decryptKey(byte[] kekBytes, byte[] ivBytes, byte[] keyByte
215
216
BadPaddingException , NoSuchPaddingException , NoSuchAlgorithmException {
216
217
SecretKey kek = new SecretKeySpec (kekBytes , 0 , kekBytes .length , AES );
217
218
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec (TAG_LENGTH_IN_BITS , ivBytes );
218
- Cipher keyCipher = Cipher .getInstance (KEY_CIPHER );
219
+ Cipher keyCipher = Cipher .getInstance (JCE_CIPHER_NAME );
219
220
keyCipher .init (Cipher .DECRYPT_MODE , kek , gcmParameterSpec );
220
221
if (aad != null ) {
221
222
keyCipher .updateAAD (aad );
0 commit comments