98 lines
4.7 KiB
Java
98 lines
4.7 KiB
Java
package cloud.molberg.mgutility;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.File;
|
|
import java.io.InputStreamReader;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
/** Verifies that the car decrypts exactly what the phone page's WebCrypto code produces. */
|
|
public final class HermesPairingCryptoTest {
|
|
private static String script;
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
script = new File(args[0]).getAbsolutePath();
|
|
base64RoundTrip();
|
|
hkdfVector();
|
|
phoneToCar();
|
|
tamperingRejected();
|
|
wrongCodeRejected();
|
|
System.out.println("HermesPairingCryptoTest: all tests passed");
|
|
}
|
|
|
|
private static void base64RoundTrip() {
|
|
for (int n = 0; n < 70; n++) {
|
|
byte[] data = new byte[n];
|
|
for (int i = 0; i < n; i++) data[i] = (byte) (i * 37 + n);
|
|
String text = HermesPairingCrypto.base64Url(data);
|
|
check(java.util.Arrays.equals(data, HermesPairingCrypto.base64UrlDecode(text)), "base64url round trip " + n);
|
|
check(text.equals(java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(data)), "base64url matches JDK " + n);
|
|
}
|
|
}
|
|
|
|
/** RFC 5869 test case 3 (empty salt and info). */
|
|
private static void hkdfVector() throws Exception {
|
|
byte[] ikm = new byte[22];
|
|
java.util.Arrays.fill(ikm, (byte) 0x0b);
|
|
byte[] okm = HermesPairingCrypto.hkdfSha256(ikm, new byte[0], new byte[0], 42);
|
|
check(hex(okm).equals("8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8"), "HKDF RFC 5869 #3");
|
|
}
|
|
|
|
private static void phoneToCar() throws Exception {
|
|
HermesPairingCrypto car = new HermesPairingCrypto();
|
|
check(car.publicKey().length() == 87, "public key is 87 chars");
|
|
String plain = "{\"url\":\"https://hermes.example.com\",\"key\":\"sk-test-ÆØÅ-123\"}";
|
|
String sealed = phone("ABCD-EF23", car.publicKey(), plain);
|
|
String opened = car.open("ABCD-EF23", field(sealed, "epk"), field(sealed, "iv"), field(sealed, "ct"));
|
|
check(opened.equals(plain), "car decrypts phone payload");
|
|
check(field(sealed, "fingerprint").equals(car.fingerprint()), "phone and car show the same check code");
|
|
}
|
|
|
|
private static void tamperingRejected() throws Exception {
|
|
HermesPairingCrypto car = new HermesPairingCrypto();
|
|
String sealed = phone("ABCD-EF23", car.publicKey(), "{\"url\":\"https://x\",\"key\":\"k\"}");
|
|
String ct = field(sealed, "ct");
|
|
char flipped = ct.charAt(3) == 'A' ? 'B' : 'A';
|
|
String tampered = ct.substring(0, 3) + flipped + ct.substring(4);
|
|
expectFailure(car, "ABCD-EF23", field(sealed, "epk"), field(sealed, "iv"), tampered, "tampered ciphertext rejected");
|
|
HermesPairingCrypto other = new HermesPairingCrypto();
|
|
expectFailure(other, "ABCD-EF23", field(sealed, "epk"), field(sealed, "iv"), ct, "other car cannot decrypt");
|
|
}
|
|
|
|
private static void wrongCodeRejected() throws Exception {
|
|
HermesPairingCrypto car = new HermesPairingCrypto();
|
|
String sealed = phone("ABCD-EF23", car.publicKey(), "{\"url\":\"https://x\",\"key\":\"k\"}");
|
|
expectFailure(car, "ABCD-EF24", field(sealed, "epk"), field(sealed, "iv"), field(sealed, "ct"), "payload bound to pairing code");
|
|
}
|
|
|
|
private static void expectFailure(HermesPairingCrypto car, String code, String epk, String iv, String ct, String name) {
|
|
try { car.open(code, epk, iv, ct); } catch (Exception expected) { return; }
|
|
throw new AssertionError(name);
|
|
}
|
|
|
|
private static String phone(String code, String key, String plain) throws Exception {
|
|
Process process = new ProcessBuilder("node", script, code, key, plain).redirectErrorStream(true).start();
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
|
|
StringBuilder out = new StringBuilder();
|
|
String line;
|
|
while ((line = reader.readLine()) != null) out.append(line);
|
|
if (process.waitFor() != 0) throw new AssertionError("node encrypt failed: " + out);
|
|
return out.toString();
|
|
}
|
|
|
|
private static String field(String json, String name) {
|
|
Matcher m = Pattern.compile("\"" + name + "\":\"([^\"]*)\"").matcher(json);
|
|
if (!m.find()) throw new AssertionError("missing " + name + " in " + json);
|
|
return m.group(1);
|
|
}
|
|
|
|
private static String hex(byte[] bytes) {
|
|
StringBuilder out = new StringBuilder();
|
|
for (byte b : bytes) out.append(String.format("%02x", b & 0xff));
|
|
return out.toString();
|
|
}
|
|
|
|
private static void check(boolean condition, String name) { if (!condition) throw new AssertionError(name); }
|
|
}
|