26
26
import javax .crypto .SecretKey ;
27
27
import javax .crypto .spec .GCMParameterSpec ;
28
28
import javax .crypto .spec .SecretKeySpec ;
29
+ import net .snowflake .client .core .SnowflakeJdbcInternalApi ;
29
30
import net .snowflake .client .jdbc .MatDesc ;
30
31
import net .snowflake .common .core .RemoteStoreFileEncryptionMaterial ;
31
32
32
- class GcmEncryptionProvider {
33
+ @ SnowflakeJdbcInternalApi
34
+ public class GcmEncryptionProvider {
33
35
private static final int TAG_LENGTH_IN_BITS = 128 ;
34
36
private static final int IV_LENGTH_IN_BYTES = 12 ;
35
37
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
38
private static final int BUFFER_SIZE = 8 * 1024 * 1024 ; // 2 MB
39
39
private static final ThreadLocal <SecureRandom > random =
40
40
ThreadLocal .withInitial (SecureRandom ::new );
@@ -85,7 +85,7 @@ private static byte[] encryptKey(byte[] kekBytes, byte[] keyBytes, byte[] keyIvD
85
85
BadPaddingException , NoSuchPaddingException , NoSuchAlgorithmException {
86
86
SecretKey kek = new SecretKeySpec (kekBytes , 0 , kekBytes .length , AES );
87
87
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec (TAG_LENGTH_IN_BITS , keyIvData );
88
- Cipher keyCipher = Cipher .getInstance (KEY_CIPHER );
88
+ Cipher keyCipher = Cipher .getInstance (JCE_CIPHER_NAME );
89
89
keyCipher .init (Cipher .ENCRYPT_MODE , kek , gcmParameterSpec );
90
90
if (aad != null ) {
91
91
keyCipher .updateAAD (aad );
@@ -99,7 +99,7 @@ private static CipherInputStream encryptContent(
99
99
NoSuchAlgorithmException {
100
100
SecretKey fileKey = new SecretKeySpec (keyBytes , 0 , keyBytes .length , AES );
101
101
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec (TAG_LENGTH_IN_BITS , dataIvBytes );
102
- Cipher fileCipher = Cipher .getInstance (FILE_CIPHER );
102
+ Cipher fileCipher = Cipher .getInstance (JCE_CIPHER_NAME );
103
103
fileCipher .init (Cipher .ENCRYPT_MODE , fileKey , gcmParameterSpec );
104
104
if (aad != null ) {
105
105
fileCipher .updateAAD (aad );
@@ -172,7 +172,7 @@ private static CipherInputStream decryptContentFromStream(
172
172
NoSuchAlgorithmException {
173
173
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec (TAG_LENGTH_IN_BITS , ivBytes );
174
174
SecretKey fileKey = new SecretKeySpec (fileKeyBytes , AES );
175
- Cipher fileCipher = Cipher .getInstance (FILE_CIPHER );
175
+ Cipher fileCipher = Cipher .getInstance (JCE_CIPHER_NAME );
176
176
fileCipher .init (Cipher .DECRYPT_MODE , fileKey , gcmParameterSpec );
177
177
if (aad != null ) {
178
178
fileCipher .updateAAD (aad );
@@ -187,7 +187,7 @@ private static void decryptContentFromFile(
187
187
SecretKey fileKey = new SecretKeySpec (fileKeyBytes , AES );
188
188
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec (TAG_LENGTH_IN_BITS , cekIvBytes );
189
189
byte [] buffer = new byte [BUFFER_SIZE ];
190
- Cipher fileCipher = Cipher .getInstance (FILE_CIPHER );
190
+ Cipher fileCipher = Cipher .getInstance (JCE_CIPHER_NAME );
191
191
fileCipher .init (Cipher .DECRYPT_MODE , fileKey , gcmParameterSpec );
192
192
if (aad != null ) {
193
193
fileCipher .updateAAD (aad );
@@ -215,7 +215,7 @@ private static byte[] decryptKey(byte[] kekBytes, byte[] ivBytes, byte[] keyByte
215
215
BadPaddingException , NoSuchPaddingException , NoSuchAlgorithmException {
216
216
SecretKey kek = new SecretKeySpec (kekBytes , 0 , kekBytes .length , AES );
217
217
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec (TAG_LENGTH_IN_BITS , ivBytes );
218
- Cipher keyCipher = Cipher .getInstance (KEY_CIPHER );
218
+ Cipher keyCipher = Cipher .getInstance (JCE_CIPHER_NAME );
219
219
keyCipher .init (Cipher .DECRYPT_MODE , kek , gcmParameterSpec );
220
220
if (aad != null ) {
221
221
keyCipher .updateAAD (aad );
0 commit comments