| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.store.fs.encrypt; |
| 7 |
|
| 8 |
import org.h2.security.BlockCipher; |
| 9 |
|
| 10 |
/** |
| 11 |
* An XTS implementation as described in |
| 12 |
* IEEE P1619 (Standard Architecture for Encrypted Shared Storage Media). |
| 13 |
* See also |
| 14 |
* http://axelkenzo.ru/downloads/1619-2007-NIST-Submission.pdf |
| 15 |
*/ |
| 16 |
class XTS { |
| 17 |
|
| 18 |
/** |
| 19 |
* Galois field feedback. |
| 20 |
*/ |
| 21 |
private static final int GF_128_FEEDBACK = 0x87; |
| 22 |
|
| 23 |
/** |
| 24 |
* The AES encryption block size. |
| 25 |
*/ |
| 26 |
private static final int CIPHER_BLOCK_SIZE = 16; |
| 27 |
|
| 28 |
private final BlockCipher cipher; |
| 29 |
|
| 30 |
XTS(BlockCipher cipher) { |
| 31 |
this.cipher = cipher; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Encrypt the data. |
| 36 |
* |
| 37 |
* @param id the (sector) id |
| 38 |
* @param len the number of bytes |
| 39 |
* @param data the data |
| 40 |
* @param offset the offset within the data |
| 41 |
*/ |
| 42 |
void encrypt(long id, int len, byte[] data, int offset) { |
| 43 |
byte[] tweak = initTweak(id); |
| 44 |
int i = 0; |
| 45 |
for (; i + CIPHER_BLOCK_SIZE <= len; i += CIPHER_BLOCK_SIZE) { |
| 46 |
if (i > 0) { |
| 47 |
updateTweak(tweak); |
| 48 |
} |
| 49 |
xorTweak(data, i + offset, tweak); |
| 50 |
cipher.encrypt(data, i + offset, CIPHER_BLOCK_SIZE); |
| 51 |
xorTweak(data, i + offset, tweak); |
| 52 |
} |
| 53 |
if (i < len) { |
| 54 |
updateTweak(tweak); |
| 55 |
swap(data, i + offset, i - CIPHER_BLOCK_SIZE + offset, len - i); |
| 56 |
xorTweak(data, i - CIPHER_BLOCK_SIZE + offset, tweak); |
| 57 |
cipher.encrypt(data, i - CIPHER_BLOCK_SIZE + offset, CIPHER_BLOCK_SIZE); |
| 58 |
xorTweak(data, i - CIPHER_BLOCK_SIZE + offset, tweak); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Decrypt the data. |
| 64 |
* |
| 65 |
* @param id the (sector) id |
| 66 |
* @param len the number of bytes |
| 67 |
* @param data the data |
| 68 |
* @param offset the offset within the data |
| 69 |
*/ |
| 70 |
void decrypt(long id, int len, byte[] data, int offset) { |
| 71 |
byte[] tweak = initTweak(id), tweakEnd = tweak; |
| 72 |
int i = 0; |
| 73 |
for (; i + CIPHER_BLOCK_SIZE <= len; i += CIPHER_BLOCK_SIZE) { |
| 74 |
if (i > 0) { |
| 75 |
updateTweak(tweak); |
| 76 |
if (i + CIPHER_BLOCK_SIZE + CIPHER_BLOCK_SIZE > len && |
| 77 |
i + CIPHER_BLOCK_SIZE < len) { |
| 78 |
tweakEnd = tweak.clone(); |
| 79 |
updateTweak(tweak); |
| 80 |
} |
| 81 |
} |
| 82 |
xorTweak(data, i + offset, tweak); |
| 83 |
cipher.decrypt(data, i + offset, CIPHER_BLOCK_SIZE); |
| 84 |
xorTweak(data, i + offset, tweak); |
| 85 |
} |
| 86 |
if (i < len) { |
| 87 |
swap(data, i, i - CIPHER_BLOCK_SIZE + offset, len - i + offset); |
| 88 |
xorTweak(data, i - CIPHER_BLOCK_SIZE + offset, tweakEnd); |
| 89 |
cipher.decrypt(data, i - CIPHER_BLOCK_SIZE + offset, CIPHER_BLOCK_SIZE); |
| 90 |
xorTweak(data, i - CIPHER_BLOCK_SIZE + offset, tweakEnd); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
private byte[] initTweak(long id) { |
| 95 |
byte[] tweak = new byte[CIPHER_BLOCK_SIZE]; |
| 96 |
for (int j = 0; j < CIPHER_BLOCK_SIZE; j++, id >>>= 8) { |
| 97 |
tweak[j] = (byte) (id & 0xff); |
| 98 |
} |
| 99 |
cipher.encrypt(tweak, 0, CIPHER_BLOCK_SIZE); |
| 100 |
return tweak; |
| 101 |
} |
| 102 |
|
| 103 |
private static void xorTweak(byte[] data, int pos, byte[] tweak) { |
| 104 |
for (int i = 0; i < CIPHER_BLOCK_SIZE; i++) { |
| 105 |
data[pos + i] ^= tweak[i]; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
private static void updateTweak(byte[] tweak) { |
| 110 |
byte ci = 0, co = 0; |
| 111 |
for (int i = 0; i < CIPHER_BLOCK_SIZE; i++) { |
| 112 |
co = (byte) ((tweak[i] >> 7) & 1); |
| 113 |
tweak[i] = (byte) (((tweak[i] << 1) + ci) & 255); |
| 114 |
ci = co; |
| 115 |
} |
| 116 |
if (co != 0) { |
| 117 |
tweak[0] ^= GF_128_FEEDBACK; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
private static void swap(byte[] data, int source, int target, int len) { |
| 122 |
for (int i = 0; i < len; i++) { |
| 123 |
byte temp = data[source + i]; |
| 124 |
data[source + i] = data[target + i]; |
| 125 |
data[target + i] = temp; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
} |