Skip to content

Commit 3525adf

Browse files
authored
SNOW-1850888 Fix secure random initialization (#1990)
1 parent 84deebc commit 3525adf

File tree

2 files changed

+9
-33
lines changed

2 files changed

+9
-33
lines changed

src/main/java/net/snowflake/client/jdbc/cloud/storage/EncryptionProvider.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public class EncryptionProvider {
4141
private static final String FILE_CIPHER = "AES/CBC/PKCS5Padding";
4242
private static final String KEY_CIPHER = "AES/ECB/PKCS5Padding";
4343
private static final int BUFFER_SIZE = 2 * 1024 * 1024; // 2 MB
44-
private static SecureRandom secRnd;
44+
private static ThreadLocal<SecureRandom> secRnd =
45+
new ThreadLocal<>().withInitial(SecureRandom::new);
4546

4647
/**
4748
* Decrypt a InputStream
@@ -165,11 +166,11 @@ public static CipherInputStream encrypt(
165166

166167
// Create IV
167168
ivData = new byte[blockSize];
168-
getSecRnd().nextBytes(ivData);
169+
secRnd.get().nextBytes(ivData);
169170
final IvParameterSpec iv = new IvParameterSpec(ivData);
170171

171172
// Create file key
172-
getSecRnd().nextBytes(fileKeyBytes);
173+
secRnd.get().nextBytes(fileKeyBytes);
173174
SecretKey fileKey = new SecretKeySpec(fileKeyBytes, 0, keySize, AES);
174175

175176
// Init cipher
@@ -199,18 +200,4 @@ public static CipherInputStream encrypt(
199200

200201
return cis;
201202
}
202-
203-
/*
204-
* getSecRnd
205-
* Gets a random number for encryption purposes.
206-
*/
207-
private static synchronized SecureRandom getSecRnd()
208-
throws NoSuchAlgorithmException, NoSuchProviderException {
209-
if (secRnd == null) {
210-
secRnd = SecureRandom.getInstance("SHA1PRNG");
211-
byte[] bytes = new byte[10];
212-
secRnd.nextBytes(bytes);
213-
}
214-
return secRnd;
215-
}
216203
}

src/main/java/net/snowflake/client/jdbc/cloud/storage/GcmEncryptionProvider.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,10 @@ class GcmEncryptionProvider {
3636
private static final String FILE_CIPHER = "AES/GCM/NoPadding";
3737
private static final String KEY_CIPHER = "AES/GCM/NoPadding";
3838
private static final int BUFFER_SIZE = 8 * 1024 * 1024; // 2 MB
39-
private static final int blockSize;
40-
private static final SecureRandom random;
39+
private static final ThreadLocal<SecureRandom> random =
40+
new ThreadLocal<>().withInitial(SecureRandom::new);
4141
private static final Base64.Decoder base64Decoder = Base64.getDecoder();
4242

43-
static {
44-
try {
45-
Cipher fileCipher = Cipher.getInstance(FILE_CIPHER);
46-
blockSize = fileCipher.getBlockSize();
47-
48-
random = SecureRandom.getInstance("SHA1PRNG");
49-
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
50-
throw new ExceptionInInitializerError(e);
51-
}
52-
}
53-
5443
static InputStream encrypt(
5544
StorageObjectMetadata meta,
5645
long originalContentLength,
@@ -86,9 +75,9 @@ static InputStream encrypt(
8675

8776
private static void initRandomIvsAndFileKey(
8877
byte[] dataIvData, byte[] fileKeyIvData, byte[] fileKeyBytes) {
89-
random.nextBytes(dataIvData);
90-
random.nextBytes(fileKeyIvData);
91-
random.nextBytes(fileKeyBytes);
78+
random.get().nextBytes(dataIvData);
79+
random.get().nextBytes(fileKeyIvData);
80+
random.get().nextBytes(fileKeyBytes);
9281
}
9382

9483
private static byte[] encryptKey(byte[] kekBytes, byte[] keyBytes, byte[] keyIvData, byte[] aad)

0 commit comments

Comments
 (0)