repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
tools/aes.java
Rawimport static java.lang.System.err;
import static java.lang.System.out;
import java.io.ByteArrayOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
class aes {
private static final byte[] MAGIC = new byte[] { 'D', 'F', 'A', 'E', 'S', '1' };
private static final int KEY_BYTES = 64;
private static final int AES_KEY_BYTES = 32;
private static final int IV_BYTES = 12;
private static final int GCM_TAG_BITS = 128;
private static final int HEADER_BYTES = MAGIC.length + IV_BYTES;
enum Command {
ENCRYPT, DECRYPT
}
public static void main(String[] arguments) throws Exception {
try {
run(arguments);
} catch (IllegalArgumentException problem) {
err.println(problem.getMessage());
System.exit(2);
} catch (Exception problem) {
err.println(problem.getMessage());
System.exit(1);
}
}
private static void run(String[] arguments) throws Exception {
if (arguments.length != 3 && arguments.length != 5) {
usage();
throw new IllegalArgumentException("wrong argument count");
}
var command = parseCommand(arguments[0]);
if (!"--key-file".equals(arguments[1])) {
usage();
throw new IllegalArgumentException("expected --key-file");
}
var key = readKey(Path.of(arguments[2]));
byte[] input;
if (arguments.length == 3) {
input = System.in.readAllBytes();
} else {
input = Files.readAllBytes(Path.of(arguments[3]));
}
byte[] output;
switch (command) {
case ENCRYPT:
output = encrypt(key, input);
break;
case DECRYPT:
output = decrypt(key, input);
break;
default:
throw new IllegalStateException("unhandled command: " + command);
}
if (arguments.length == 3) {
System.out.write(output);
} else {
var outputPath = Path.of(arguments[4]);
if (Files.exists(outputPath)) {
throw new IllegalArgumentException("output path already exists: " + outputPath);
}
try (var stream = Files.newOutputStream(outputPath)) {
stream.write(output);
}
}
}
static byte[] encrypt(byte[] key, byte[] plaintext) throws Exception {
if (startsWith(plaintext, MAGIC)) {
return plaintext;
}
var split = splitKey(key);
var iv = deterministicIv(split.macKey(), plaintext);
var header = header(iv);
var cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(split.encryptionKey(), "AES"), new GCMParameterSpec(GCM_TAG_BITS, iv));
cipher.updateAAD(header);
var ciphertext = cipher.doFinal(plaintext);
var output = new ByteArrayOutputStream(header.length + ciphertext.length);
output.write(header);
output.write(ciphertext);
return output.toByteArray();
}
static byte[] decrypt(byte[] key, byte[] encrypted) throws Exception {
if (!startsWith(encrypted, MAGIC)) {
throw new IllegalArgumentException("input is not a dotfiles AES blob");
}
if (encrypted.length < HEADER_BYTES + 1) {
throw new IllegalArgumentException("encrypted blob is truncated");
}
var iv = Arrays.copyOfRange(encrypted, MAGIC.length, HEADER_BYTES);
var header = Arrays.copyOfRange(encrypted, 0, HEADER_BYTES);
var ciphertext = Arrays.copyOfRange(encrypted, HEADER_BYTES, encrypted.length);
var split = splitKey(key);
var cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(split.encryptionKey(), "AES"), new GCMParameterSpec(GCM_TAG_BITS, iv));
cipher.updateAAD(header);
return cipher.doFinal(ciphertext);
}
static boolean startsWith(byte[] bytes, byte[] prefix) {
if (bytes.length < prefix.length) {
return false;
}
for (var index = 0; index < prefix.length; index++) {
if (bytes[index] != prefix[index]) {
return false;
}
}
return true;
}
private static Command parseCommand(String input) {
if ("encrypt".startsWith(input)) {
return Command.ENCRYPT;
}
if ("decrypt".startsWith(input)) {
return Command.DECRYPT;
}
throw new IllegalArgumentException("unknown command: " + input);
}
private static byte[] readKey(Path path) throws Exception {
if (!Files.exists(path)) {
throw new IllegalArgumentException("key file does not exist: " + path);
}
var text = Files.readString(path).trim();
var key = Base64.getDecoder().decode(text);
if (key.length != KEY_BYTES) {
throw new IllegalArgumentException("key file must contain " + KEY_BYTES + " base64-encoded bytes");
}
return key;
}
private static SplitKey splitKey(byte[] key) {
return new SplitKey(
Arrays.copyOfRange(key, 0, AES_KEY_BYTES),
Arrays.copyOfRange(key, AES_KEY_BYTES, KEY_BYTES));
}
private static byte[] deterministicIv(byte[] macKey, byte[] plaintext) throws Exception {
var mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(macKey, "HmacSHA256"));
return Arrays.copyOf(mac.doFinal(plaintext), IV_BYTES);
}
private static byte[] header(byte[] iv) throws Exception {
var output = new ByteArrayOutputStream(HEADER_BYTES);
output.write(MAGIC);
output.write(iv);
return output.toByteArray();
}
private static void usage() {
out.println("Symmetrically encrypts/decrypts dotfiles data.");
out.println("Usage for streams: java tools/aes.java <encrypt|decrypt> --key-file <path>");
out.println("Usage for files: java tools/aes.java <encrypt|decrypt> --key-file <path> <input> <output>");
}
static class SplitKey {
private final byte[] encryptionKey;
private final byte[] macKey;
SplitKey(byte[] encryptionKey, byte[] macKey) {
this.encryptionKey = encryptionKey;
this.macKey = macKey;
}
byte[] encryptionKey() {
return encryptionKey;
}
byte[] macKey() {
return macKey;
}
}
}