security(lib): enforce single-use encryption contexts
This commit is contained in:
@@ -154,7 +154,7 @@ public final class Guard {
|
||||
final Option OPT_TAG_BITS = Option.builder().longOpt("tag-bits").hasArg().argName("96..128")
|
||||
.desc("AES-GCM tag length in bits (default 128)").get();
|
||||
final Option OPT_NONCE_HEX = Option.builder().longOpt("nonce-hex").hasArg().argName("hex")
|
||||
.desc("ChaCha nonce (12-byte hex)").get();
|
||||
.desc("ChaCha decryption nonce (12-byte hex; rejected for encryption)").get();
|
||||
final Option OPT_INIT_CTR = Option.builder().longOpt("init-ctr").hasArg().argName("int")
|
||||
.desc("ChaCha stream initial counter (default 1)").get();
|
||||
final Option OPT_CTR = Option.builder().longOpt("ctr").hasArg().argName("int")
|
||||
@@ -303,7 +303,7 @@ public final class Guard {
|
||||
chacha.withHeader();
|
||||
}
|
||||
if (chachaNonce != null) {
|
||||
chacha.withNonce(chachaNonce);
|
||||
chacha.withDecryptionNonce(chachaNonce);
|
||||
}
|
||||
if (ctrOverride != null || initCtr != null) {
|
||||
// providing counters together with AAD would be conflicting; builder enforces
|
||||
@@ -323,7 +323,7 @@ public final class Guard {
|
||||
chacha.withHeader();
|
||||
}
|
||||
if (chachaNonce != null) {
|
||||
chacha.withNonce(chachaNonce);
|
||||
chacha.withDecryptionNonce(chachaNonce);
|
||||
}
|
||||
if (initCtr != null) {
|
||||
chacha.initialCounter(initCtr);
|
||||
|
||||
@@ -181,7 +181,7 @@ public final class Kem { // NOPMD
|
||||
|
||||
/** AES IV: --aes-iv <hex> */
|
||||
public static final Option OPT_AES_IV = Option.builder().longOpt("aes-iv").hasArg().argName("hex")
|
||||
.desc("AES IV/nonce (hex)").get();
|
||||
.desc("AES decryption IV/nonce (hex; rejected for encryption)").get();
|
||||
|
||||
/** AES tag bits: --aes-tag-bits <int> */
|
||||
public static final Option OPT_AES_TAG_BITS = Option.builder().longOpt("aes-tag-bits").hasArg().argName("int")
|
||||
@@ -189,7 +189,7 @@ public final class Kem { // NOPMD
|
||||
|
||||
/** ChaCha nonce: --chacha-nonce <hex> */
|
||||
public static final Option OPT_CHACHA_NONCE = Option.builder().longOpt("chacha-nonce").hasArg().argName("hex")
|
||||
.desc("ChaCha nonce (hex, usually 12 bytes)").get();
|
||||
.desc("ChaCha decryption nonce (hex, usually 12 bytes; rejected for encryption)").get();
|
||||
|
||||
/** ChaCha counter value: --chacha-counter <int> */
|
||||
public static final Option OPT_CHACHA_COUNTER = Option.builder().longOpt("chacha-counter").hasArg().argName("int")
|
||||
@@ -283,7 +283,7 @@ public final class Kem { // NOPMD
|
||||
}
|
||||
byte[] iv = parseHexOpt(cmd, OPT_AES_IV);
|
||||
if (iv != null) {
|
||||
aes = aes.withIv(iv);
|
||||
aes = aes.withDecryptionIv(iv);
|
||||
}
|
||||
if (aad != null && aad.length > 0) {
|
||||
aes = aes.withAad(aad);
|
||||
@@ -299,7 +299,7 @@ public final class Kem { // NOPMD
|
||||
ChaChaDataContentBuilder cc = ChaChaDataContentBuilder.builder(session);
|
||||
byte[] nonce = parseHexOpt(cmd, OPT_CHACHA_NONCE);
|
||||
if (nonce != null) {
|
||||
cc = cc.withNonce(nonce);
|
||||
cc = cc.withDecryptionNonce(nonce);
|
||||
}
|
||||
// counter is an integer, not bytes; use typed parsed option
|
||||
Integer counter = parsedIntOpt(cmd, OPT_CHACHA_COUNTER);
|
||||
|
||||
@@ -196,11 +196,10 @@ public class GuardTest {
|
||||
final int tagBits = 128;
|
||||
final String aadAes = "010203";
|
||||
final String aadCha = "D00DFEED";
|
||||
final String chNonce = "00112233445566778899AABB";
|
||||
|
||||
System.out.println(method);
|
||||
System.out.println("...params: sizeAes=" + sizeAes + " sizeCha=" + sizeCha + " tagBits=" + tagBits + " aadAes="
|
||||
+ aadAes + " aadCha=" + aadCha + " chNonce=" + chNonce);
|
||||
+ aadAes + " aadCha=" + aadCha);
|
||||
|
||||
// Prepare keyring with RSA pair
|
||||
Path ring = tmp.resolve("ring-rsa.txt");
|
||||
@@ -237,13 +236,13 @@ public class GuardTest {
|
||||
Path dec = tmp.resolve("rsa-pt-ch.bin.dec");
|
||||
|
||||
String[] encArgs = { "--encrypt", in.toString(), "--output", enc.toString(), "--keyring", ring.toString(),
|
||||
"--to-alias", rsa.pub, "--alg", "chacha-aead", "--aad-hex", aadCha, "--nonce-hex", chNonce };
|
||||
"--to-alias", rsa.pub, "--alg", "chacha-aead", "--aad-hex", aadCha };
|
||||
System.out.println("...ChaCha encrypt: " + Arrays.toString(encArgs));
|
||||
int e = Guard.main(encArgs, new Options());
|
||||
assertEquals(0, e, "... ChaCha encrypt rc");
|
||||
|
||||
String[] decArgs = { "--decrypt", enc.toString(), "--output", dec.toString(), "--keyring", ring.toString(),
|
||||
"--priv-alias", rsa.prv, "--alg", "chacha-aead", "--aad-hex", aadCha, "--nonce-hex", chNonce };
|
||||
"--priv-alias", rsa.prv, "--alg", "chacha-aead", "--aad-hex", aadCha };
|
||||
System.out.println("...ChaCha decrypt: " + Arrays.toString(decArgs));
|
||||
int d = Guard.main(decArgs, new Options());
|
||||
assertEquals(0, d, "... ChaCha decrypt rc");
|
||||
|
||||
@@ -142,11 +142,10 @@ public class KemTest {
|
||||
final int gcmTagBits = 128;
|
||||
final String aadAes = "A1B2C3";
|
||||
final String aadChaCha = "DEADBEEF";
|
||||
final String nonceChaCha = "00112233445566778899AABB";
|
||||
|
||||
System.out.println(method);
|
||||
System.out.println("...params: aesSize=" + aesSize + " chachaSize=" + chachaSize + " gcmTagBits=" + gcmTagBits
|
||||
+ " aesAAD=" + aadAes + " chachaAAD=" + aadChaCha + " chachaNonce=" + nonceChaCha);
|
||||
+ " aesAAD=" + aadAes + " chachaAAD=" + aadChaCha);
|
||||
|
||||
// Discover KEM ids via the CLI (ensures we use exactly the ids users will see).
|
||||
List<String> kemIds = listKemsViaCli();
|
||||
@@ -209,14 +208,14 @@ public class KemTest {
|
||||
System.out.println("...[" + kemId + "] ChaCha encrypt");
|
||||
int e = Kem.main(new String[] { "--encrypt", plain.toString(), "--output", enc.toString(),
|
||||
"--keyring", ring.toString(), "--pub", aliases.pub, "--kem", kemId, "--chacha",
|
||||
"--chacha-nonce", nonceChaCha, "--aad", aadChaCha, "--header" }, new Options());
|
||||
"--aad", aadChaCha, "--header" }, new Options());
|
||||
if (e != 0) {
|
||||
throw new IllegalStateException("ChaCha encrypt rc=" + e);
|
||||
}
|
||||
System.out.println("...[" + kemId + "] ChaCha decrypt");
|
||||
int d = Kem.main(new String[] { "--decrypt", enc.toString(), "--output", dec.toString(),
|
||||
"--keyring", ring.toString(), "--priv", aliases.prv, "--kem", kemId, "--chacha",
|
||||
"--chacha-nonce", nonceChaCha, "--aad", aadChaCha, "--header" }, new Options());
|
||||
"--aad", aadChaCha, "--header" }, new Options());
|
||||
if (d != 0) {
|
||||
throw new IllegalStateException("ChaCha decrypt rc=" + d);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user