Skip to content

Commit abeb314

Browse files
committed
Crypto: Formatting test cases, more removal of non-ascii
1 parent 96f6832 commit abeb314

26 files changed

+5167
-5415
lines changed

java/ql/test/experimental/library-tests/quantum/jca/AesWrapAndPBEWith.java

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.example.crypto.algorithms;
22

33
//import org.bouncycastle.jce.provider.BouncyCastleProvider;
4-
54
import java.security.*;
65
import javax.crypto.Cipher;
76
import javax.crypto.KeyGenerator;
@@ -20,29 +19,23 @@
2019
*
2120
* This file includes:
2221
*
23-
* 1. AESWrap Examples:
24-
* - secureAESWrap(): Uses a randomly generated wrapping key.
25-
* - insecureAESWrap(): Uses a fixed, hard-coded wrapping key.
22+
* 1. AESWrap Examples: - secureAESWrap(): Uses a randomly generated wrapping
23+
* key. - insecureAESWrap(): Uses a fixed, hard-coded wrapping key.
2624
*
27-
* 2. PBEWith Examples:
28-
* - insecurePBEExample(): Uses the legacy PBEWithMD5AndDES.
29-
* - securePBEExample(): Uses PBKDF2WithHmacSHA256.
30-
* - additionalPBEExample(): Uses PBEWithSHA256And128BitAES-CBC-BC.
31-
* - additionalPBEExample2(): Uses PBEWithSHA1And128BitAES-CBC-BC.
25+
* 2. PBEWith Examples: - insecurePBEExample(): Uses the legacy
26+
* PBEWithMD5AndDES. - securePBEExample(): Uses PBKDF2WithHmacSHA256. -
27+
* additionalPBEExample(): Uses PBEWithSHA256And128BitAES-CBC-BC. -
28+
* additionalPBEExample2(): Uses PBEWithSHA1And128BitAES-CBC-BC.
3229
*
33-
* 3. Dynamic PBE Encryption:
34-
* - dynamicPBEEncryption(): Chooses the PBE transformation based on a
35-
* configuration string.
30+
* 3. Dynamic PBE Encryption: - dynamicPBEEncryption(): Chooses the PBE
31+
* transformation based on a configuration string.
3632
*
37-
* Best Practices:
38-
* - Use secure random keys and salts.
39-
* - Avoid legacy algorithms like PBEWithMD5AndDES.
40-
* - Prefer modern KDFs (PBKDF2WithHmacSHA256) and secure provider-specific PBE
41-
* transformations.
33+
* Best Practices: - Use secure random keys and salts. - Avoid legacy algorithms
34+
* like PBEWithMD5AndDES. - Prefer modern KDFs (PBKDF2WithHmacSHA256) and secure
35+
* provider-specific PBE transformations.
4236
*
43-
* SAST/CBOM Notes:
44-
* - Insecure examples (PBEWithMD5AndDES, fixed keys) should be flagged.
45-
* - Secure examples use random salt, high iteration counts, and strong
37+
* SAST/CBOM Notes: - Insecure examples (PBEWithMD5AndDES, fixed keys) should be
38+
* flagged. - Secure examples use random salt, high iteration counts, and strong
4639
* algorithms.
4740
*/
4841
public class AesWrapAndPBEWith {
@@ -51,14 +44,12 @@ public class AesWrapAndPBEWith {
5144
// // Register BouncyCastle as a provider.
5245
// Security.addProvider(new BouncyCastleProvider());
5346
// }
54-
5547
// ===========================
5648
// 1. AESWrap Examples
5749
// ===========================
58-
5950
/**
60-
* Secure AES key wrapping.
61-
* Generates a random 256-bit wrapping key to wrap a target AES key.
51+
* Secure AES key wrapping. Generates a random 256-bit wrapping key to wrap
52+
* a target AES key.
6253
*
6354
* @return The wrapped key (Base64-encoded).
6455
* @throws Exception if an error occurs.
@@ -79,8 +70,7 @@ public String secureAESWrap() throws Exception {
7970
}
8071

8172
/**
82-
* Insecure AES key wrapping.
83-
* Uses a fixed (hard-coded) wrapping key.
73+
* Insecure AES key wrapping. Uses a fixed (hard-coded) wrapping key.
8474
*
8575
* @return The wrapped key (Base64-encoded).
8676
* @throws Exception if an error occurs.
@@ -104,7 +94,6 @@ public String insecureAESWrap() throws Exception {
10494
// ===========================
10595
// 2. PBEWith Examples
10696
// ===========================
107-
10897
/**
10998
* Insecure PBE example using PBEWithMD5AndDES.
11099
*
@@ -141,7 +130,7 @@ public String securePBEExample(String password) throws Exception {
141130
/**
142131
* Additional PBE example using PBEWithSHA256And128BitAES-CBC-BC.
143132
*
144-
* @param password The input password.
133+
* @param password The input password.
145134
* @param plaintext The plaintext to encrypt.
146135
* @return The IV concatenated with ciphertext (Base64-encoded).
147136
* @throws Exception if key derivation or encryption fails.
@@ -165,11 +154,10 @@ public String additionalPBEExample(String password, String plaintext) throws Exc
165154
}
166155

167156
/**
168-
* Additional PBE example using PBEWithSHA1And128BitAES-CBC-BC.
169-
* This is less preferred than PBKDF2WithHmacSHA256 but demonstrates another
170-
* variant.
157+
* Additional PBE example using PBEWithSHA1And128BitAES-CBC-BC. This is less
158+
* preferred than PBKDF2WithHmacSHA256 but demonstrates another variant.
171159
*
172-
* @param password The input password.
160+
* @param password The input password.
173161
* @param plaintext The plaintext to encrypt.
174162
* @return The IV concatenated with ciphertext (Base64-encoded).
175163
* @throws Exception if key derivation or encryption fails.
@@ -195,18 +183,16 @@ public String additionalPBEExample2(String password, String plaintext) throws Ex
195183
// ===========================
196184
// 3. Dynamic PBE Encryption
197185
// ===========================
198-
199186
/**
200187
* Dynamically selects a PBE transformation based on a configuration string.
201188
*
202-
* Acceptable values:
203-
* - "PBKDF2": Uses PBKDF2WithHmacSHA256.
204-
* - "SHA256AES": Uses PBEWithSHA256And128BitAES-CBC-BC.
205-
* - "SHA1AES": Uses PBEWithSHA1And128BitAES-CBC-BC.
206-
* - Otherwise, falls back to insecure PBEWithMD5AndDES.
189+
* Acceptable values: - "PBKDF2": Uses PBKDF2WithHmacSHA256. - "SHA256AES":
190+
* Uses PBEWithSHA256And128BitAES-CBC-BC. - "SHA1AES": Uses
191+
* PBEWithSHA1And128BitAES-CBC-BC. - Otherwise, falls back to insecure
192+
* PBEWithMD5AndDES.
207193
*
208-
* @param config The configuration string.
209-
* @param password The input password.
194+
* @param config The configuration string.
195+
* @param password The input password.
210196
* @param plaintext The plaintext to encrypt.
211197
* @return The Base64-encoded encrypted output.
212198
* @throws Exception if an error occurs.
@@ -227,7 +213,6 @@ public String dynamicPBEEncryption(String config, String password, String plaint
227213
// ===========================
228214
// Helper Methods
229215
// ===========================
230-
231216
/**
232217
* Concatenates two byte arrays.
233218
*/

java/ql/test/experimental/library-tests/quantum/jca/AsymmetricEncryptionMacHybridCryptosystem.java

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,52 @@
22

33
// import org.bouncycastle.jce.provider.BouncyCastleProvider;
44
// import org.bouncycastle.pqc.jcajce.provider.BouncyCastlePQCProvider;
5-
5+
import java.security.*;
6+
import java.security.spec.ECGenParameterSpec;
7+
import java.util.Arrays;
8+
import java.util.Base64;
69
import javax.crypto.Cipher;
710
import javax.crypto.KeyAgreement;
811
import javax.crypto.KeyGenerator;
912
import javax.crypto.Mac;
1013
import javax.crypto.SecretKey;
1114
import javax.crypto.spec.GCMParameterSpec;
1215
import javax.crypto.spec.SecretKeySpec;
13-
import java.security.*;
14-
import java.security.spec.ECGenParameterSpec;
15-
import java.util.Arrays;
16-
import java.util.Base64;
1716

1817
/**
19-
* AsymmetricEncryptionMacHybridCryptosystem demonstrates hybrid
20-
* cryptosystems that combine asymmetric encryption with a MAC.
18+
* AsymmetricEncryptionMacHybridCryptosystem demonstrates hybrid cryptosystems
19+
* that combine asymmetric encryption with a MAC.
2120
*
22-
* Flows:
23-
* 1. RSA-OAEP + HMAC:
24-
* - Secure Flow: Uses 2048-bit RSA-OAEP (with SHA256andMGF1Padding) to
25-
* encapsulate a freshly generated AES key;
26-
* then encrypts using AES-GCM with a random nonce and computes HMAC-SHA256 over
27-
* the ciphertext.
28-
* - Insecure Flow: Uses 1024-bit RSA (RSA/ECB/PKCS1Padding), AES-GCM with a
29-
* fixed IV, and HMAC-SHA1.
21+
* Flows: 1. RSA-OAEP + HMAC: - Secure Flow: Uses 2048-bit RSA-OAEP (with
22+
* SHA256andMGF1Padding) to encapsulate a freshly generated AES key; then
23+
* encrypts using AES-GCM with a random nonce and computes HMAC-SHA256 over the
24+
* ciphertext. - Insecure Flow: Uses 1024-bit RSA (RSA/ECB/PKCS1Padding),
25+
* AES-GCM with a fixed IV, and HMAC-SHA1.
3026
*
31-
* 2. ECIES + HMAC:
32-
* - Secure Flow: Uses ephemeral ECDH key pairs (secp256r1); derives a shared
33-
* secret and applies a simple KDF (SHA-256)
34-
* to derive a 128-bit AES key; then uses AES-GCM with a random nonce and
35-
* computes HMAC-SHA256.
36-
* - Insecure Flow: Reuses a static EC key pair, directly truncates the shared
37-
* secret without a proper KDF,
38-
* uses a fixed IV, and computes HMAC-SHA1.
27+
* 2. ECIES + HMAC: - Secure Flow: Uses ephemeral ECDH key pairs (secp256r1);
28+
* derives a shared secret and applies a simple KDF (SHA-256) to derive a
29+
* 128-bit AES key; then uses AES-GCM with a random nonce and computes
30+
* HMAC-SHA256. - Insecure Flow: Reuses a static EC key pair, directly truncates
31+
* the shared secret without a proper KDF, uses a fixed IV, and computes
32+
* HMAC-SHA1.
3933
*
40-
* 3. Dynamic Hybrid Selection:
41-
* - Chooses between flows based on a configuration string.
34+
* 3. Dynamic Hybrid Selection: - Chooses between flows based on a configuration
35+
* string.
4236
*
43-
* SAST/CBOM Notes:
44-
* - Secure flows use proper ephemeral key generation, secure key sizes, KDF
45-
* usage, and random nonces/IVs.
46-
* - Insecure flows (static key reuse, fixed nonces, weak key sizes, raw shared
47-
* secret truncation, and deprecated algorithms)
48-
* should be flagged.
37+
* SAST/CBOM Notes: - Secure flows use proper ephemeral key generation, secure
38+
* key sizes, KDF usage, and random nonces/IVs. - Insecure flows (static key
39+
* reuse, fixed nonces, weak key sizes, raw shared secret truncation, and
40+
* deprecated algorithms) should be flagged.
4941
*/
5042
public class AsymmetricEncryptionMacHybridCryptosystem {
5143

5244
// static {
5345
// Security.addProvider(new BouncyCastleProvider());
5446
// Security.addProvider(new BouncyCastlePQCProvider());
5547
// }
56-
5748
// ---------- Result Class ----------
5849
public static class HybridResult {
50+
5951
private final byte[] encapsulatedKey;
6052
private final byte[] ciphertext;
6153
private final byte[] mac;
@@ -79,14 +71,13 @@ public byte[] getMac() {
7971
}
8072

8173
public String toBase64String() {
82-
return "EncapsulatedKey: " + Base64.getEncoder().encodeToString(encapsulatedKey) +
83-
"\nCiphertext: " + Base64.getEncoder().encodeToString(ciphertext) +
84-
"\nMAC: " + Base64.getEncoder().encodeToString(mac);
74+
return "EncapsulatedKey: " + Base64.getEncoder().encodeToString(encapsulatedKey)
75+
+ "\nCiphertext: " + Base64.getEncoder().encodeToString(ciphertext)
76+
+ "\nMAC: " + Base64.getEncoder().encodeToString(mac);
8577
}
8678
}
8779

8880
// ---------- Helper Methods ----------
89-
9081
/**
9182
* Generates an ephemeral ECDH key pair on secp256r1.
9283
*/
@@ -107,10 +98,10 @@ public KeyPair generateX25519KeyPair() throws Exception {
10798

10899
/**
109100
* Derives a shared secret using the provided key agreement algorithm.
110-
*
101+
*
111102
* @param privateKey The private key.
112-
* @param publicKey The corresponding public key.
113-
* @param algorithm The key agreement algorithm (e.g., "ECDH" or "X25519").
103+
* @param publicKey The corresponding public key.
104+
* @param algorithm The key agreement algorithm (e.g., "ECDH" or "X25519").
114105
* @return The shared secret.
115106
*/
116107
public byte[] deriveSharedSecret(PrivateKey privateKey, PublicKey publicKey, String algorithm) throws Exception {
@@ -123,8 +114,8 @@ public byte[] deriveSharedSecret(PrivateKey privateKey, PublicKey publicKey, Str
123114
/**
124115
* A simple KDF that hashes the input with SHA-256 and returns the first
125116
* numBytes.
126-
*
127-
* @param input The input byte array.
117+
*
118+
* @param input The input byte array.
128119
* @param numBytes The desired number of output bytes.
129120
* @return The derived key material.
130121
*/
@@ -147,7 +138,6 @@ public byte[] concatenate(byte[] a, byte[] b) {
147138
// =====================================================
148139
// 1. RSA-OAEP + HMAC Hybrid Cryptosystem
149140
// =====================================================
150-
151141
/**
152142
* Generates a secure 2048-bit RSA key pair.
153143
*/
@@ -216,7 +206,6 @@ public HybridResult insecureRSAHybridEncryption(byte[] plaintext) throws Excepti
216206
// =====================================================
217207
// 2. ECIES + HMAC Hybrid Cryptosystem
218208
// =====================================================
219-
220209
/**
221210
* Secure hybrid encryption using ECIES (via ECDH) + HMAC-SHA256.
222211
*/
@@ -268,16 +257,15 @@ public HybridResult insecureECIESHybridEncryption(byte[] plaintext) throws Excep
268257
// =====================================================
269258
// 3. Dynamic Hybrid Selection
270259
// =====================================================
271-
272260
/**
273261
* Dynamically selects a hybrid encryption flow based on configuration.
274262
* SAST: Dynamic selection introduces risk if insecure defaults are chosen.
275263
*
276-
* @param config The configuration string ("secureRSA", "insecureRSA",
277-
* "secureECIES", "insecureECIES").
264+
* @param config The configuration string ("secureRSA", "insecureRSA",
265+
* "secureECIES", "insecureECIES").
278266
* @param plaintext The plaintext to encrypt.
279267
* @return A Base64-encoded string representation of the hybrid encryption
280-
* result.
268+
* result.
281269
* @throws Exception if an error occurs.
282270
*/
283271
public String dynamicHybridEncryption(String config, byte[] plaintext) throws Exception {
@@ -300,10 +288,8 @@ public String dynamicHybridEncryption(String config, byte[] plaintext) throws Ex
300288
// =====================================================
301289
// 4. Helper Methods for HMAC and Symmetric Encryption
302290
// =====================================================
303-
304291
/**
305-
* Secure HMAC using HMAC-SHA256.
306-
* SAST: HMAC-SHA256 is secure.
292+
* Secure HMAC using HMAC-SHA256. SAST: HMAC-SHA256 is secure.
307293
*/
308294
public byte[] secureHMACSHA256(String message, byte[] key) throws Exception {
309295
Mac mac = Mac.getInstance("HmacSHA256", "BC");
@@ -313,8 +299,8 @@ public byte[] secureHMACSHA256(String message, byte[] key) throws Exception {
313299
}
314300

315301
/**
316-
* Insecure HMAC using HMAC-SHA1.
317-
* SAST: HMAC-SHA1 is deprecated and insecure.
302+
* Insecure HMAC using HMAC-SHA1. SAST: HMAC-SHA1 is deprecated and
303+
* insecure.
318304
*/
319305
public byte[] insecureHMACSHA1(String message, byte[] key) throws Exception {
320306
Mac mac = Mac.getInstance("HmacSHA1", "BC");
@@ -326,10 +312,9 @@ public byte[] insecureHMACSHA1(String message, byte[] key) throws Exception {
326312
// =====================================================
327313
// 5. Helper Methods for Key/Nonce Generation
328314
// =====================================================
329-
330315
/**
331-
* Generates a secure 256-bit AES key.
332-
* SAST: Uses SecureRandom for key generation.
316+
* Generates a secure 256-bit AES key. SAST: Uses SecureRandom for key
317+
* generation.
333318
*/
334319
public SecretKey generateAESKey() throws Exception {
335320
KeyGenerator kg = KeyGenerator.getInstance("AES");

java/ql/test/experimental/library-tests/quantum/jca/ChainedEncryptionTest.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
package com.example.crypto.algorithms;
22

33
// import org.bouncycastle.jce.provider.BouncyCastleProvider;
4-
54
import java.security.*;
6-
5+
import java.util.Arrays;
76
import javax.crypto.Cipher;
87
import javax.crypto.KeyGenerator;
98
import javax.crypto.SecretKey;
109
import javax.crypto.spec.GCMParameterSpec;
1110
import javax.crypto.spec.IvParameterSpec;
1211

13-
import java.util.Arrays;
14-
import java.util.Base64;
15-
1612
public class ChainedEncryptionTest {
1713

1814
// static {
1915
// Security.addProvider(new BouncyCastleProvider());
2016
// }
21-
2217
// Encrypts using AES-GCM. Returns IV concatenated with ciphertext.
2318
public static byte[] encryptAESGCM(SecretKey key, byte[] plaintext) throws Exception {
2419
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
@@ -68,10 +63,10 @@ private static byte[] concat(byte[] a, byte[] b) {
6863
}
6964

7065
/**
71-
* Performs chained encryption and decryption in one function.
72-
* First, plaintext is encrypted with AES-GCM (inner layer),
73-
* then that ciphertext is encrypted with ChaCha20-Poly1305 (outer layer).
74-
* The decryption process reverses these steps.
66+
* Performs chained encryption and decryption in one function. First,
67+
* plaintext is encrypted with AES-GCM (inner layer), then that ciphertext
68+
* is encrypted with ChaCha20-Poly1305 (outer layer). The decryption process
69+
* reverses these steps.
7570
*
7671
* @param plaintext The input plaintext.
7772
* @return The decrypted plaintext as a String.
@@ -148,4 +143,4 @@ public static void main(String[] args) throws Exception {
148143
System.out.println("Decrypted: " + new String(decryptedPlaintext));
149144
}
150145

151-
}
146+
}

0 commit comments

Comments
 (0)