Skip to content

Commit 7969bdf

Browse files
committed
Added test cases for Bouncy Castle block cipher modes
This commit also reorganizes the Bouncy Castle test cases into separate sub-directories for signature and cipher modes.
1 parent f97be14 commit 7969bdf

27 files changed

+368
-1
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import java.security.SecureRandom;
2+
import java.security.Security;
3+
import java.util.Arrays;
4+
import org.bouncycastle.crypto.engines.AESEngine;
5+
import org.bouncycastle.crypto.modes.CBCBlockCipher;
6+
import org.bouncycastle.crypto.paddings.PKCS7Padding;
7+
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
8+
import org.bouncycastle.crypto.params.KeyParameter;
9+
import org.bouncycastle.crypto.params.ParametersWithIV;
10+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
11+
12+
/**
13+
* Example of AES-CBC encryption and decryption using Bouncy Castle's low-level API.
14+
*/
15+
public class AESCBCEncryption {
16+
byte[] encrypt(byte[] plaintext, byte[] key, byte[] iv) throws Exception {
17+
AESEngine engine = new AESEngine();
18+
CBCBlockCipher mode = new CBCBlockCipher(engine);
19+
PKCS7Padding padding = new PKCS7Padding();
20+
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(mode, padding);
21+
22+
KeyParameter keyParam = new KeyParameter(key);
23+
ParametersWithIV params = new ParametersWithIV(keyParam, iv);
24+
25+
cipher.init(true, params); // true for encryption
26+
byte[] ciphertext = new byte[cipher.getOutputSize(plaintext.length)];
27+
int outputLen = cipher.processBytes(plaintext, 0, plaintext.length, ciphertext, 0);
28+
outputLen += cipher.doFinal(ciphertext, outputLen);
29+
30+
if (outputLen != ciphertext.length) {
31+
ciphertext = Arrays.copyOf(ciphertext, outputLen);
32+
}
33+
return ciphertext;
34+
}
35+
byte[] decrypt(byte[] ciphertext, byte[] key, byte[] iv) throws Exception {
36+
AESEngine engine = new AESEngine();
37+
CBCBlockCipher mode = new CBCBlockCipher(engine);
38+
PKCS7Padding padding = new PKCS7Padding();
39+
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(mode, padding);
40+
41+
KeyParameter keyParam = new KeyParameter(key);
42+
ParametersWithIV params = new ParametersWithIV(keyParam, iv);
43+
44+
cipher.init(false, params); // false for decryption
45+
byte[] plaintext = new byte[cipher.getOutputSize(ciphertext.length)];
46+
int outputLen = cipher.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0);
47+
outputLen += cipher.doFinal(plaintext, outputLen);
48+
49+
if (outputLen != plaintext.length) {
50+
plaintext = Arrays.copyOf(plaintext, outputLen);
51+
}
52+
return plaintext;
53+
}
54+
public static void main(String[] args) {
55+
Security.addProvider(new BouncyCastleProvider());
56+
57+
try {
58+
SecureRandom random = new SecureRandom();
59+
byte[] key = new byte[32];
60+
random.nextBytes(key);
61+
byte[] iv = new byte[16];
62+
random.nextBytes(iv);
63+
64+
byte[] message = "Hello AES-CBC mode!".getBytes("UTF-8");
65+
byte[] ciphertext = new AESCBCEncryption().encrypt(message, key, iv);
66+
byte[] plaintext = new AESCBCEncryption().decrypt(ciphertext, key, iv);
67+
} catch (Exception e) {
68+
e.printStackTrace();
69+
}
70+
}
71+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.security.SecureRandom;
2+
import org.bouncycastle.crypto.engines.AESEngine;
3+
import org.bouncycastle.crypto.modes.GCMBlockCipher;
4+
import org.bouncycastle.crypto.params.AEADParameters;
5+
import org.bouncycastle.crypto.params.KeyParameter;
6+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
7+
import java.security.Security;
8+
import java.util.Arrays;
9+
10+
/**
11+
* Example of AES-GCM encryption and decryption using Bouncy Castle's low-level API.
12+
*/
13+
public class AESGCMEncryption {
14+
byte[] encrypt(byte[] plaintext, byte[] key, byte[] nonce, byte[] aad) throws Exception {
15+
AESEngine engine = new AESEngine();
16+
GCMBlockCipher cipher = new GCMBlockCipher(engine);
17+
AEADParameters params = new AEADParameters(
18+
new KeyParameter(key),
19+
128, // Authentication tag size in bits
20+
nonce,
21+
aad);
22+
23+
cipher.init(true, params); // true for encryption
24+
byte[] ciphertext = new byte[cipher.getOutputSize(plaintext.length)];
25+
int outputLen = cipher.processBytes(plaintext, 0, plaintext.length, ciphertext, 0);
26+
outputLen += cipher.doFinal(ciphertext, outputLen);
27+
28+
if (outputLen != ciphertext.length) {
29+
ciphertext = Arrays.copyOf(ciphertext, outputLen);
30+
}
31+
return ciphertext;
32+
}
33+
byte[] decrypt(byte[] ciphertext, byte[] key, byte[] nonce, byte[] aad) throws Exception {
34+
AESEngine engine = new AESEngine();
35+
GCMBlockCipher cipher = new GCMBlockCipher(engine);
36+
AEADParameters params = new AEADParameters(
37+
new KeyParameter(key),
38+
128, // Authentication tag size in bits
39+
nonce,
40+
aad);
41+
42+
cipher.init(false, params); // false for decryption
43+
byte[] plaintext = new byte[cipher.getOutputSize(ciphertext.length)];
44+
int outputLen = cipher.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0);
45+
outputLen += cipher.doFinal(plaintext, outputLen);
46+
47+
if (outputLen != plaintext.length) {
48+
plaintext = Arrays.copyOf(plaintext, outputLen);
49+
}
50+
return plaintext;
51+
}
52+
public static void main(String[] args) {
53+
Security.addProvider(new BouncyCastleProvider());
54+
55+
try {
56+
SecureRandom random = new SecureRandom();
57+
byte[] key = new byte[32];
58+
random.nextBytes(key);
59+
byte[] nonce = new byte[12];
60+
random.nextBytes(nonce);
61+
62+
byte[] message = "This is a message to be encrypted.".getBytes("UTF-8");
63+
byte[] aad = "This is additional authenticated data".getBytes("UTF-8");
64+
byte[] ciphertext = new AESGCMEncryption().encrypt(message, key, nonce, aad);
65+
byte[] plaintext = new AESGCMEncryption().decrypt(ciphertext, key, nonce, aad);
66+
} catch (Exception e) {
67+
e.printStackTrace();
68+
}
69+
}
70+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
| AESCBCEncryption.java:28:22:28:58 | EncryptOperation | AESCBCEncryption.java:17:28:17:42 | KeyOperationAlgorithm | AESCBCEncryption.java:22:50:22:52 | Key | AESCBCEncryption.java:23:66:23:67 | Nonce | AESCBCEncryption.java:27:45:27:53 | Message | AESCBCEncryption.java:27:77:27:86 | KeyOperationOutput |
2+
| AESCBCEncryption.java:28:22:28:58 | EncryptOperation | AESCBCEncryption.java:17:28:17:42 | KeyOperationAlgorithm | AESCBCEncryption.java:22:50:22:52 | Key | AESCBCEncryption.java:23:66:23:67 | Nonce | AESCBCEncryption.java:27:45:27:53 | Message | AESCBCEncryption.java:28:37:28:46 | KeyOperationOutput |
3+
| AESCBCEncryption.java:47:22:47:57 | DecryptOperation | AESCBCEncryption.java:36:28:36:42 | KeyOperationAlgorithm | AESCBCEncryption.java:41:50:41:52 | Key | AESCBCEncryption.java:42:66:42:67 | Nonce | AESCBCEncryption.java:46:45:46:54 | Message | AESCBCEncryption.java:46:79:46:87 | KeyOperationOutput |
4+
| AESCBCEncryption.java:47:22:47:57 | DecryptOperation | AESCBCEncryption.java:36:28:36:42 | KeyOperationAlgorithm | AESCBCEncryption.java:41:50:41:52 | Key | AESCBCEncryption.java:42:66:42:67 | Nonce | AESCBCEncryption.java:46:45:46:54 | Message | AESCBCEncryption.java:47:37:47:45 | KeyOperationOutput |
5+
| AESGCMEncryption.java:26:22:26:58 | EncryptOperation | AESGCMEncryption.java:15:28:15:42 | KeyOperationAlgorithm | AESGCMEncryption.java:18:34:18:36 | Key | AESGCMEncryption.java:20:17:20:21 | Nonce | AESGCMEncryption.java:25:45:25:53 | Message | AESGCMEncryption.java:25:77:25:86 | KeyOperationOutput |
6+
| AESGCMEncryption.java:26:22:26:58 | EncryptOperation | AESGCMEncryption.java:15:28:15:42 | KeyOperationAlgorithm | AESGCMEncryption.java:18:34:18:36 | Key | AESGCMEncryption.java:20:17:20:21 | Nonce | AESGCMEncryption.java:25:45:25:53 | Message | AESGCMEncryption.java:26:37:26:46 | KeyOperationOutput |
7+
| AESGCMEncryption.java:45:22:45:57 | DecryptOperation | AESGCMEncryption.java:34:28:34:42 | KeyOperationAlgorithm | AESGCMEncryption.java:37:34:37:36 | Key | AESGCMEncryption.java:39:17:39:21 | Nonce | AESGCMEncryption.java:44:45:44:54 | Message | AESGCMEncryption.java:44:79:44:87 | KeyOperationOutput |
8+
| AESGCMEncryption.java:45:22:45:57 | DecryptOperation | AESGCMEncryption.java:34:28:34:42 | KeyOperationAlgorithm | AESGCMEncryption.java:37:34:37:36 | Key | AESGCMEncryption.java:39:17:39:21 | Nonce | AESGCMEncryption.java:44:45:44:54 | Message | AESGCMEncryption.java:45:37:45:45 | KeyOperationOutput |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import java
2+
import experimental.quantum.Language
3+
4+
from Crypto::CipherOperationNode n
5+
select n, n.getAKnownAlgorithm(), n.getAKey(), n.getANonce(), n.getAnInputArtifact(),
6+
n.getAnOutputArtifact()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| AESCBCEncryption.java:60:30:60:32 | RandomNumberGeneration | AESCBCEncryption.java:22:50:22:52 | Key |
2+
| AESCBCEncryption.java:60:30:60:32 | RandomNumberGeneration | AESCBCEncryption.java:41:50:41:52 | Key |
3+
| AESGCMEncryption.java:58:30:58:32 | RandomNumberGeneration | AESGCMEncryption.java:18:34:18:36 | Key |
4+
| AESGCMEncryption.java:58:30:58:32 | RandomNumberGeneration | AESGCMEncryption.java:37:34:37:36 | Key |
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
//semmle-extractor-options: --javac-args -cp ${testdir}/../../../../stubs/bcprov-lts8on-2.73.7
1+
//semmle-extractor-options: --javac-args -cp ${testdir}/../../../../../stubs/bcprov-lts8on-2.73.7
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
| AESCBCEncryption.java:22:50:22:52 | Key |
2+
| AESCBCEncryption.java:23:66:23:67 | Nonce |
3+
| AESCBCEncryption.java:41:50:41:52 | Key |
4+
| AESCBCEncryption.java:42:66:42:67 | Nonce |
5+
| AESGCMEncryption.java:18:34:18:36 | Key |
6+
| AESGCMEncryption.java:20:17:20:21 | Nonce |
7+
| AESGCMEncryption.java:37:34:37:36 | Key |
8+
| AESGCMEncryption.java:39:17:39:21 | Nonce |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import java
2+
import experimental.quantum.Language
3+
4+
from Crypto::ArtifactNode n
5+
where any(SecureRandomnessInstance rng).flowsTo(n.asElement())
6+
select n
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//semmle-extractor-options: --javac-args -cp ${testdir}/../../../../../stubs/bcprov-lts8on-2.73.7

java/ql/test/stubs/bcprov-lts8on-2.73.7/org/bouncycastle/crypto/engines/AESEngine.java

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/ql/test/stubs/bcprov-lts8on-2.73.7/org/bouncycastle/crypto/modes/CBCBlockCipher.java

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/ql/test/stubs/bcprov-lts8on-2.73.7/org/bouncycastle/crypto/modes/GCMBlockCipher.java

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/ql/test/stubs/bcprov-lts8on-2.73.7/org/bouncycastle/crypto/paddings/PKCS7Padding.java

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/ql/test/stubs/bcprov-lts8on-2.73.7/org/bouncycastle/crypto/paddings/PaddedBufferedBlockCipher.java

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/ql/test/stubs/bcprov-lts8on-2.73.7/org/bouncycastle/crypto/params/AEADParameters.java

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/ql/test/stubs/bcprov-lts8on-2.73.7/org/bouncycastle/crypto/params/KeyParameter.java

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/ql/test/stubs/bcprov-lts8on-2.73.7/org/bouncycastle/crypto/params/ParametersWithIV.java

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)