|
1 | 1 | package io.github.hapjava.server.impl.crypto; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | | -import org.bouncycastle.crypto.engines.ChaChaEngine; |
5 | | -import org.bouncycastle.crypto.generators.Poly1305KeyGenerator; |
| 4 | +import org.bouncycastle.crypto.InvalidCipherTextException; |
| 5 | +import org.bouncycastle.crypto.modes.ChaCha20Poly1305; |
| 6 | +import org.bouncycastle.crypto.params.AEADParameters; |
6 | 7 | import org.bouncycastle.crypto.params.KeyParameter; |
7 | | -import org.bouncycastle.crypto.params.ParametersWithIV; |
8 | 8 |
|
9 | 9 | public class ChachaEncoder { |
10 | 10 |
|
11 | | - private final ChaChaEngine encryptCipher; |
| 11 | + private final ChaCha20Poly1305 cipher; |
| 12 | + private final byte[] key; |
| 13 | + private final byte[] nonce; |
12 | 14 |
|
13 | 15 | public ChachaEncoder(byte[] key, byte[] nonce) throws IOException { |
| 16 | + this.key = key; |
| 17 | + // ChaCha20-Poly1305 requires exactly 12 bytes (96 bits) for nonce |
| 18 | + this.nonce = ensureNonceSize(nonce); |
| 19 | + this.cipher = new ChaCha20Poly1305(); |
| 20 | + } |
| 21 | + |
| 22 | + private byte[] ensureNonceSize(byte[] nonce) { |
| 23 | + if (nonce == null) { |
| 24 | + return new byte[12]; // Default to zero nonce if null |
| 25 | + } |
| 26 | + |
| 27 | + // For HomeKit pairing messages, handle Apple's string-based nonces |
| 28 | + if (nonce.length == 8) { |
| 29 | + // Apple's HomeKit implementation uses a specific nonce format |
| 30 | + // Based on RFC 7539 and Apple's implementation, the nonce should be: |
| 31 | + // - 4 bytes constant (0x00000000) |
| 32 | + // - 8 bytes nonce string |
| 33 | + // This matches ChaCha20's 96-bit nonce requirement and ChachaDecoder format |
| 34 | + byte[] adjustedNonce = new byte[12]; |
| 35 | + // Put the 8-byte nonce at the END (bytes 4-11), not at the beginning |
| 36 | + System.arraycopy(nonce, 0, adjustedNonce, 4, 8); |
| 37 | + // First 4 bytes remain zero (counter initialization) |
| 38 | + return adjustedNonce; |
| 39 | + } |
| 40 | + |
| 41 | + if (nonce.length == 12) { |
| 42 | + return nonce; // Already correct size |
| 43 | + } |
14 | 44 |
|
15 | | - this.encryptCipher = new ChaChaEngine(20); |
| 45 | + byte[] adjustedNonce = new byte[12]; |
| 46 | + if (nonce.length < 12) { |
| 47 | + // Pad with zeros if too short |
| 48 | + System.arraycopy(nonce, 0, adjustedNonce, 0, nonce.length); |
| 49 | + } else { |
| 50 | + // Truncate if too long |
| 51 | + System.arraycopy(nonce, 0, adjustedNonce, 0, 12); |
| 52 | + } |
| 53 | + return adjustedNonce; |
| 54 | + } |
16 | 55 |
|
17 | | - this.encryptCipher.init(true, new ParametersWithIV(new KeyParameter(key), nonce)); |
| 56 | + private static String bytesToHex(byte[] bytes) { |
| 57 | + StringBuilder result = new StringBuilder(); |
| 58 | + for (byte b : bytes) { |
| 59 | + result.append(String.format("%02x", b)); |
| 60 | + } |
| 61 | + return result.toString(); |
18 | 62 | } |
19 | 63 |
|
20 | 64 | public byte[] encodeCiphertext(byte[] plaintext) throws IOException { |
21 | 65 | return encodeCiphertext(plaintext, null); |
22 | 66 | } |
23 | 67 |
|
24 | 68 | public byte[] encodeCiphertext(byte[] plaintext, byte[] additionalData) throws IOException { |
25 | | - KeyParameter macKey = initRecordMAC(encryptCipher); |
26 | | - |
27 | | - byte[] ciphertext = new byte[plaintext.length]; |
28 | | - encryptCipher.processBytes(plaintext, 0, plaintext.length, ciphertext, 0); |
| 69 | + try { |
| 70 | + // Use the nonce provided during construction |
| 71 | + AEADParameters params = new AEADParameters(new KeyParameter(key), 128, nonce, additionalData); |
| 72 | + cipher.init(true, params); |
29 | 73 |
|
30 | | - byte[] calculatedMAC = PolyKeyCreator.create(macKey, additionalData, ciphertext); |
31 | | - |
32 | | - byte[] ret = new byte[ciphertext.length + 16]; |
33 | | - System.arraycopy(ciphertext, 0, ret, 0, ciphertext.length); |
34 | | - System.arraycopy(calculatedMAC, 0, ret, ciphertext.length, 16); |
35 | | - return ret; |
36 | | - } |
| 74 | + byte[] output = new byte[cipher.getOutputSize(plaintext.length)]; |
| 75 | + int len = cipher.processBytes(plaintext, 0, plaintext.length, output, 0); |
| 76 | + len += cipher.doFinal(output, len); |
37 | 77 |
|
38 | | - private KeyParameter initRecordMAC(ChaChaEngine cipher) { |
39 | | - byte[] firstBlock = new byte[64]; |
40 | | - cipher.processBytes(firstBlock, 0, firstBlock.length, firstBlock, 0); |
| 78 | + // Split the result into ciphertext and MAC |
| 79 | + byte[] ciphertext = new byte[plaintext.length]; |
| 80 | + byte[] mac = new byte[16]; |
| 81 | + System.arraycopy(output, 0, ciphertext, 0, plaintext.length); |
| 82 | + System.arraycopy(output, plaintext.length, mac, 0, 16); |
41 | 83 |
|
42 | | - // NOTE: The BC implementation puts 'r' after 'k' |
43 | | - System.arraycopy(firstBlock, 0, firstBlock, 32, 16); |
44 | | - KeyParameter macKey = new KeyParameter(firstBlock, 16, 32); |
45 | | - Poly1305KeyGenerator.clamp(macKey.getKey()); |
46 | | - return macKey; |
| 84 | + // Return combined ciphertext + MAC as expected by the original interface |
| 85 | + byte[] ret = new byte[ciphertext.length + 16]; |
| 86 | + System.arraycopy(ciphertext, 0, ret, 0, ciphertext.length); |
| 87 | + System.arraycopy(mac, 0, ret, ciphertext.length, 16); |
| 88 | + return ret; |
| 89 | + } catch (InvalidCipherTextException e) { |
| 90 | + throw new IOException("Encryption failed", e); |
| 91 | + } |
47 | 92 | } |
48 | 93 | } |
0 commit comments