|
| 1 | +package org.cryptomator.siv; |
| 2 | + |
| 3 | +import javax.crypto.BadPaddingException; |
| 4 | +import javax.crypto.Cipher; |
| 5 | +import javax.crypto.IllegalBlockSizeException; |
| 6 | +import javax.crypto.MacSpi; |
| 7 | +import javax.crypto.NoSuchPaddingException; |
| 8 | +import javax.crypto.spec.SecretKeySpec; |
| 9 | +import java.security.InvalidKeyException; |
| 10 | +import java.security.Key; |
| 11 | +import java.security.MessageDigest; |
| 12 | +import java.security.NoSuchAlgorithmException; |
| 13 | +import java.security.spec.AlgorithmParameterSpec; |
| 14 | +import java.util.Arrays; |
| 15 | + |
| 16 | +/** |
| 17 | + * AES-CMAC (Cipher-based Message Authentication Code). |
| 18 | + * Specs: <a href="https://www.rfc-editor.org/rfc/rfc4493.html">RFC 4493</a>. |
| 19 | + */ |
| 20 | +class CMac extends MacSpi { |
| 21 | + |
| 22 | + private static final int BLOCK_SIZE = 16; // 128 bits for AES |
| 23 | + private static final String AES_ALGORITHM = "AES"; |
| 24 | + private static final String AES_ECB_NO_PADDING = "AES/ECB/NoPadding"; |
| 25 | + |
| 26 | + // MAC keys: |
| 27 | + private Cipher cipher; |
| 28 | + private byte[] k1; |
| 29 | + private byte[] k2; |
| 30 | + |
| 31 | + // MAC state: |
| 32 | + private int bufferPos = 0; |
| 33 | + private byte[] buffer = new byte[BLOCK_SIZE]; |
| 34 | + private byte[] x = new byte[BLOCK_SIZE]; // X := const_Zero; |
| 35 | + private byte[] y = new byte[BLOCK_SIZE]; |
| 36 | + private int msgLen = 0; |
| 37 | + |
| 38 | + @Override |
| 39 | + protected int engineGetMacLength() { |
| 40 | + return BLOCK_SIZE; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException { |
| 45 | + try { |
| 46 | + this.cipher = Cipher.getInstance(AES_ECB_NO_PADDING); |
| 47 | + } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { |
| 48 | + throw new AssertionError("Every implementation of the Java platform is required to support [...] AES/ECB/NoPadding", e); |
| 49 | + } |
| 50 | + cipher.init(Cipher.ENCRYPT_MODE, key); |
| 51 | + |
| 52 | + // init subkeys K1 and K2 |
| 53 | + // see https://www.rfc-editor.org/rfc/rfc4493.html#section-2.3 |
| 54 | + byte[] L = new byte[BLOCK_SIZE]; |
| 55 | + try { |
| 56 | + // L = AES_encrypt(K, const_Zero) |
| 57 | + L = encryptBlock(cipher, L); |
| 58 | + this.k1 = SivMode.dbl(L); |
| 59 | + this.k2 = SivMode.dbl(k1); |
| 60 | + } finally { |
| 61 | + Arrays.fill(L, (byte) 0); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected void engineUpdate(byte input) { |
| 67 | + if (bufferPos == BLOCK_SIZE) { // buffer is full |
| 68 | + processBlock(); |
| 69 | + } |
| 70 | + assert bufferPos < BLOCK_SIZE; |
| 71 | + buffer[bufferPos++] = input; |
| 72 | + msgLen++; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected void engineUpdate(byte[] input, int offset, int len) { |
| 77 | + assert bufferPos < BLOCK_SIZE; |
| 78 | + for (int i = offset; i < offset + len; ) { |
| 79 | + if (bufferPos == BLOCK_SIZE) { // buffer is full |
| 80 | + processBlock(); |
| 81 | + } |
| 82 | + int required = offset + len - i; |
| 83 | + int available = BLOCK_SIZE - bufferPos; |
| 84 | + int m = Math.min(required, available); |
| 85 | + System.arraycopy(input, i, buffer, bufferPos, m); |
| 86 | + bufferPos += m; |
| 87 | + i += m; |
| 88 | + } |
| 89 | + msgLen += len; |
| 90 | + } |
| 91 | + |
| 92 | + // https://www.rfc-editor.org/rfc/rfc4493.html#section-2.4 Step 6 |
| 93 | + private void processBlock() { |
| 94 | + y = SivMode.xor(x, buffer); // Y := X XOR M_i; |
| 95 | + x = encryptBlock(cipher, y); // X := AES-128(K,Y); |
| 96 | + bufferPos = 0; |
| 97 | + } |
| 98 | + |
| 99 | + // https://www.rfc-editor.org/rfc/rfc4493.html#section-2.4 |
| 100 | + @Override |
| 101 | + protected byte[] engineDoFinal() { |
| 102 | + // Step 3: |
| 103 | + boolean flag = msgLen > 0 && bufferPos % BLOCK_SIZE == 0; // denoting if last block is complete or not |
| 104 | + |
| 105 | + // Step 4: |
| 106 | + byte[] m_last; |
| 107 | + if (flag) { |
| 108 | + // M_last := M_n XOR K1; |
| 109 | + m_last = SivMode.xor(buffer, k1); |
| 110 | + } else { |
| 111 | + // M_last := padding(M_n) XOR K2; |
| 112 | + // |
| 113 | + // [...] padding(x) is the concatenation of x and a single '1', |
| 114 | + // followed by the minimum number of '0's, so that the total length is |
| 115 | + // equal to 128 bits. |
| 116 | + buffer[bufferPos] = (byte) 0x80; // single '1' bit |
| 117 | + if (bufferPos + 1 < BLOCK_SIZE) { |
| 118 | + Arrays.fill(buffer, bufferPos + 1, BLOCK_SIZE, (byte) 0x00); // followed by '0' bits |
| 119 | + } |
| 120 | + m_last = SivMode.xor(buffer, k2); |
| 121 | + } |
| 122 | + |
| 123 | + // Step 7: |
| 124 | + y = SivMode.xor(m_last, x); // Y := M_last XOR X; |
| 125 | + try { |
| 126 | + return encryptBlock(cipher, y); // T := AES-128(K,Y); |
| 127 | + } finally { |
| 128 | + engineReset(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + protected void engineReset() { |
| 134 | + bufferPos = 0; |
| 135 | + msgLen = 0; |
| 136 | + Arrays.fill(buffer, (byte) 0); |
| 137 | + Arrays.fill(x, (byte) 0); |
| 138 | + Arrays.fill(y, (byte) 0); |
| 139 | + } |
| 140 | + |
| 141 | + // TODO make instance method, remove cipher param? |
| 142 | + private static byte[] encryptBlock(Cipher cipher, byte[] block) { |
| 143 | + try { |
| 144 | + return cipher.doFinal(block); |
| 145 | + } catch (IllegalBlockSizeException e) { |
| 146 | + throw new IllegalArgumentException(e); |
| 147 | + } catch (BadPaddingException e) { |
| 148 | + throw new AssertionError("Not in decrypt mode", e); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Create a new CMAC instance for incremental message processing |
| 154 | + */ |
| 155 | + public static CMac create(byte[] key) { |
| 156 | + if (key.length != 16 && key.length != 24 && key.length != 32) { |
| 157 | + throw new IllegalArgumentException("Invalid key length. Must be 16, 24, or 32 bytes"); |
| 158 | + } |
| 159 | + try { |
| 160 | + SecretKeySpec keySpec = new SecretKeySpec(key, AES_ALGORITHM); |
| 161 | + |
| 162 | + CMac mac = new CMac(); |
| 163 | + mac.engineInit(keySpec, null); |
| 164 | + return mac; |
| 165 | + } catch (InvalidKeyException e) { |
| 166 | + throw new IllegalArgumentException("Invalid key", e); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * One-shot CMAC computation |
| 172 | + */ |
| 173 | + public static byte[] tag(byte[] key, byte[] message) { |
| 174 | + CMac cmac = create(key); |
| 175 | + cmac.engineUpdate(message, 0, message.length); |
| 176 | + return cmac.engineDoFinal(); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Verify CMAC tag |
| 181 | + */ |
| 182 | + public static boolean verify(byte[] key, byte[] message, byte[] tag) { |
| 183 | + byte[] computedTag = tag(key, message); |
| 184 | + return MessageDigest.isEqual(computedTag, tag); |
| 185 | + } |
| 186 | +} |
0 commit comments