Debian Codegraph @

src/main/org/h2/security/SecureFileStore.java

raw
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.security;
7
8 import org.h2.engine.Constants;
9 import org.h2.store.DataHandler;
10 import org.h2.store.FileStore;
11 import org.h2.util.Bits;
12 import org.h2.util.MathUtils;
13
14 /**
15 * A file store that encrypts all data before writing, and decrypts all data
16 * after reading. Areas that were never written to (for example after calling
17 * setLength to enlarge the file) are not encrypted (contains 0 bytes).
18 */
19 public class SecureFileStore extends FileStore {
20
21 private byte[] key;
22 private final BlockCipher cipher;
23 private final BlockCipher cipherForInitVector;
24 private byte[] buffer = new byte[4];
25 private long pos;
26 private final byte[] bufferForInitVector;
27 private final int keyIterations;
28
29 public SecureFileStore(DataHandler handler, String name, String mode,
30 String cipher, byte[] key, int keyIterations) {
31 super(handler, name, mode);
32 this.key = key;
33 this.cipher = CipherFactory.getBlockCipher(cipher);
34 this.cipherForInitVector = CipherFactory.getBlockCipher(cipher);
35 this.keyIterations = keyIterations;
36 bufferForInitVector = new byte[Constants.FILE_BLOCK_SIZE];
37 }
38
39 @Override
40 protected byte[] generateSalt() {
41 return MathUtils.secureRandomBytes(Constants.FILE_BLOCK_SIZE);
42 }
43
44 @Override
45 protected void initKey(byte[] salt) {
46 key = SHA256.getHashWithSalt(key, salt);
47 for (int i = 0; i < keyIterations; i++) {
48 key = SHA256.getHash(key, true);
49 }
50 cipher.setKey(key);
51 key = SHA256.getHash(key, true);
52 cipherForInitVector.setKey(key);
53 }
54
55 @Override
56 protected void writeDirect(byte[] b, int off, int len) {
57 super.write(b, off, len);
58 pos += len;
59 }
60
61 @Override
62 public void write(byte[] b, int off, int len) {
63 if (buffer.length < b.length) {
64 buffer = new byte[len];
65 }
66 System.arraycopy(b, off, buffer, 0, len);
67 xorInitVector(buffer, 0, len, pos);
68 cipher.encrypt(buffer, 0, len);
69 super.write(buffer, 0, len);
70 pos += len;
71 }
72
73 @Override
74 public void readFullyDirect(byte[] b, int off, int len) {
75 super.readFully(b, off, len);
76 pos += len;
77 }
78
79 @Override
80 public void readFully(byte[] b, int off, int len) {
81 super.readFully(b, off, len);
82 for (int i = 0; i < len; i++) {
83 if (b[i] != 0) {
84 cipher.decrypt(b, off, len);
85 xorInitVector(b, off, len, pos);
86 break;
87 }
88 }
89 pos += len;
90 }
91
92 @Override
93 public void seek(long x) {
94 this.pos = x;
95 super.seek(x);
96 }
97
98 private void xorInitVector(byte[] b, int off, int len, long p) {
99 byte[] iv = bufferForInitVector;
100 while (len > 0) {
101 for (int i = 0; i < Constants.FILE_BLOCK_SIZE; i += 8) {
102 Bits.writeLong(iv, i, (p + i) >>> 3);
103 }
104 cipherForInitVector.encrypt(iv, 0, Constants.FILE_BLOCK_SIZE);
105 for (int i = 0; i < Constants.FILE_BLOCK_SIZE; i++) {
106 b[off + i] ^= iv[i];
107 }
108 p += Constants.FILE_BLOCK_SIZE;
109 off += Constants.FILE_BLOCK_SIZE;
110 len -= Constants.FILE_BLOCK_SIZE;
111 }
112 }
113
114 }