Skip to content

Commit 3ef8958

Browse files
Fix unit tests to work with FIPS certified Bouncy Castle (#132)
*Issue #, if available:* #99 *Description of changes:* These changes allow for the unit tests to pass when the FIPS validated Bouncy Castle provider is explicitly set. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. # Check any applicable: - [ ] Were any files moved? Moving files changes their URL, which breaks all hyperlinks to the files.
1 parent 713bcd2 commit 3ef8958

File tree

3 files changed

+55
-29
lines changed

3 files changed

+55
-29
lines changed

src/main/java/com/amazonaws/encryptionsdk/internal/RandomBytesGenerator.java

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
5+
* in compliance with the License. A copy of the License is located at
6+
*
7+
* http://aws.amazon.com/apache2.0
8+
*
9+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.amazonaws.encryptionsdk.internal;
15+
16+
import java.security.SecureRandom;
17+
18+
public class RandomBytesGenerator {
19+
private static final SecureRandom RND = new SecureRandom();
20+
21+
/* Some Providers (such as the FIPS certified Bouncy Castle) enforce a
22+
* maximum number of bytes that may be requested from SecureRandom. If
23+
* the requested len is larger than this value, the Secure Random will
24+
* be called multiple times to achieve the requested total length. */
25+
private static final int MAX_BYTES = 1 << 15;
26+
27+
/**
28+
* Generates a byte array of random data of the given length.
29+
*
30+
* @param len The length of the byte array.
31+
* @return The byte array.
32+
*/
33+
public static byte[] generate(final int len) {
34+
final byte[] result = new byte[len];
35+
int bytesGenerated = 0;
36+
37+
while (bytesGenerated < len) {
38+
final int requestSize = Math.min(MAX_BYTES, len - bytesGenerated);
39+
final byte[] request = new byte[requestSize];
40+
RND.nextBytes(request);
41+
System.arraycopy(request, 0, result, bytesGenerated, requestSize);
42+
bytesGenerated += requestSize;
43+
}
44+
45+
return result;
46+
}
47+
48+
}

src/test/java/com/amazonaws/encryptionsdk/internal/StaticMasterKey.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ public class StaticMasterKey extends MasterKey<StaticMasterKey> {
4949
/**
5050
* Encryption algorithm for the master key-pair
5151
*/
52-
private static final String MASTER_KEY_ENCRYPTION_ALGORITHM = "RSA";
52+
private static final String MASTER_KEY_ENCRYPTION_ALGORITHM = "RSA/ECB/PKCS1Padding";
53+
54+
/**
55+
* Encryption algorithm for the KeyFactory
56+
*/
57+
private static final String MASTER_KEY_ALGORITHM = "RSA";
5358

5459
/**
5560
* Encryption algorithm for the randomly generated data key
@@ -95,7 +100,7 @@ public StaticMasterKey(@Nonnull final String keyId) {
95100
this.keyId_ = Objects.requireNonNull(keyId);
96101

97102
try {
98-
KeyFactory keyFactory = KeyFactory.getInstance(MASTER_KEY_ENCRYPTION_ALGORITHM);
103+
KeyFactory keyFactory = KeyFactory.getInstance(MASTER_KEY_ALGORITHM);
99104
KeySpec publicKeySpec = new X509EncodedKeySpec(publicKey_v1);
100105
PublicKey pubKey = keyFactory.generatePublic(publicKeySpec);
101106
KeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKey_v1);

0 commit comments

Comments
 (0)