Skip to content

Commit b666c97

Browse files
committed
Merge branch 'RM-3258' into 'master'
RM-3258: Restore static salt constant values from CDOC2* to CDOC20* See merge request cdoc2/cdoc2-java-ref-impl!9
2 parents a57fd6a + 7d41ec9 commit b666c97

File tree

7 files changed

+57
-21
lines changed

7 files changed

+57
-21
lines changed

cdoc2-lib/src/main/java/ee/cyber/cdoc2/container/Envelope.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static Header deserializeFBSHeader(byte[] buf) {
199199
public static byte[] getAdditionalData(byte[] header, byte[] headerHMAC) {
200200
Objects.requireNonNull(header);
201201
Objects.requireNonNull(headerHMAC);
202-
final byte[] cDoc2Payload = "CDOC2payload".getBytes(StandardCharsets.UTF_8);
202+
final byte[] cDoc2Payload = "CDOC20payload".getBytes(StandardCharsets.UTF_8);
203203
ByteBuffer bb = ByteBuffer.allocate(cDoc2Payload.length + header.length + headerHMAC.length);
204204
bb.put(cDoc2Payload);
205205
bb.put(header);

cdoc2-lib/src/main/java/ee/cyber/cdoc2/crypto/Crypto.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private static SecureRandom createSecureRandom() throws NoSuchAlgorithmException
9393
DrbgParameters.instantiation(
9494
256, // Required security strength
9595
PR_AND_RESEED, // configure algorithm to provide prediction resistance and reseeding facilities
96-
"CDOC2".getBytes() // personalization string, used to derive seed
96+
"CDOC20".getBytes() // personalization string, used to derive seed
9797
)
9898
);
9999
log.info("Initialized SecureRandom.");
@@ -103,17 +103,20 @@ private static SecureRandom createSecureRandom() throws NoSuchAlgorithmException
103103
public static byte[] generateFileMasterKey() throws NoSuchAlgorithmException {
104104
byte[] inputKeyingMaterial = new byte[64]; //spec says: ikm should be more than 32bytes of secure random
105105
getSecureRandom().nextBytes(inputKeyingMaterial);
106-
return HKDF.fromHmacSha256().extract("CDOC2salt".getBytes(StandardCharsets.UTF_8), inputKeyingMaterial);
106+
return HKDF.fromHmacSha256().extract("CDOC20salt".getBytes(StandardCharsets.UTF_8),
107+
inputKeyingMaterial);
107108
}
108109

109110
public static SecretKey deriveContentEncryptionKey(byte[] fmk) {
110111
byte[] cekBytes = HKDF.fromHmacSha256()
111-
.expand(fmk, "CDOC2cek".getBytes(StandardCharsets.UTF_8), CEK_LEN_BYTES);
112+
.expand(fmk, "CDOC20cek".getBytes(StandardCharsets.UTF_8), CEK_LEN_BYTES);
112113
return new SecretKeySpec(cekBytes, "ChaCha20");
113114
}
114115

115116
public static SecretKey deriveHeaderHmacKey(byte[] fmk) {
116-
byte[] hhk = HKDF.fromHmacSha256().expand(fmk, "CDOC2hmac".getBytes(StandardCharsets.UTF_8), HHK_LEN_BYTES);
117+
byte[] hhk = HKDF.fromHmacSha256().expand(
118+
fmk, "CDOC20hmac".getBytes(StandardCharsets.UTF_8), HHK_LEN_BYTES
119+
);
117120
return new SecretKeySpec(hhk, HMAC_SHA_256);
118121
}
119122

@@ -156,7 +159,7 @@ public static SecretKey deriveKeyEncryptionKey(
156159
final HKDF hkdf = HKDF.fromHmacSha256();
157160
byte[] kekPm = hkdf.extract(salt, preSharedSecretKey.getEncoded());
158161

159-
String info = "CDOC2kek" + fmkEncMethod + label;
162+
String info = "CDOC20kek" + fmkEncMethod + label;
160163
byte[] kek = hkdf.expand(kekPm, info.getBytes(StandardCharsets.UTF_8), FMK_LEN_BYTES);
161164

162165
return new SecretKeySpec(kek, FMKEncryptionMethod.name(FMKEncryptionMethod.XOR));
@@ -267,10 +270,10 @@ private static byte[] deriveKek(KeyPair ecKeyPair, ECPublicKey otherPublicKey, i
267270

268271
byte[] ecdhSharedSecret = calcEcDhSharedSecret(ecKeyPair.getPrivate(), otherPublicKey);
269272
byte[] kekPm = HKDF.fromHmacSha256()
270-
.extract("CDOC2kekpremaster".getBytes(StandardCharsets.UTF_8), ecdhSharedSecret);
273+
.extract("CDOC20kekpremaster".getBytes(StandardCharsets.UTF_8), ecdhSharedSecret);
271274

272275
ByteArrayOutputStream baos = new ByteArrayOutputStream();
273-
baos.writeBytes("CDOC2kek".getBytes(StandardCharsets.UTF_8));
276+
baos.writeBytes("CDOC20kek".getBytes(StandardCharsets.UTF_8));
274277
baos.writeBytes(FMKEncryptionMethod.name(FMKEncryptionMethod.XOR).getBytes(StandardCharsets.UTF_8));
275278

276279
if (isEncryptionMode) {
@@ -331,4 +334,5 @@ public static byte[] generateSaltForKey() throws NoSuchAlgorithmException {
331334
getSecureRandom().nextBytes(salt);
332335
return salt;
333336
}
337+
334338
}

cdoc2-lib/src/test/java/ee/cyber/cdoc2/crypto/CryptoTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ void deriveKeyEncryptionKeyFromSharedSecret() {
142142
byte[] kek = kekSecretKey.getEncoded();
143143
assertNotNull(kek);
144144
assertEquals(Crypto.FMK_LEN_BYTES, kek.length);
145-
// assertEquals("962b1d44a6e36e9d117136e972e2da0bff7b35fc29b3d8ec5bde246d2c145984",
146-
assertEquals("9698151f0a83fcd0947ecad13d2cd56d7e4d9ba2980b61f7474ddf58b3bcaedd",
145+
assertEquals("962b1d44a6e36e9d117136e972e2da0bff7b35fc29b3d8ec5bde246d2c145984",
147146
HexFormat.of().formatHex(kek));
148147
}
149148

test/bats/cdoc2_tests.bats

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ run_alias() {
6363
assert_output --partial '/bats-core/bin/bats'
6464
}
6565

66-
@test "preparing: assert TEST_VECTORS value exists" {
66+
@test "preparing: assert TEST_VECTORS package exists" {
6767
run ${TEST_VECTORS}
6868
assert_output --partial '/test/testvectors'
6969
}
@@ -82,13 +82,24 @@ run_alias() {
8282
assert_output --partial "Created $TEST_RESULTS_DIR/$cdoc_file"
8383

8484
# ensure encrypted container can be decrypted successfully
85-
run run_alias cdoc-cli decrypt -f $$TEST_RESULTS_DIR/$cdoc_file -k $CLI_KEYS_DIR/cdoc2client.pem -o $TEST_RESULTS_DIR
85+
run run_alias cdoc-cli decrypt -f $TEST_RESULTS_DIR/$cdoc_file -k $CLI_KEYS_DIR/cdoc2client.pem -o $TEST_RESULTS_DIR
8686
assertSuccessfulDecryption
8787

8888
rm -f $TEST_RESULTS_DIR/$cdoc_file
8989
}
9090

91-
@test "test2: successfully encrypt CDOC2 container with RSA" {
91+
@test "test2: assert EC decryption is compatible with earlier encrypted CDOC2" {
92+
local cdoc_file="ec_simple_old_version_DO_NOT_DELETE.cdoc"
93+
94+
echo "# Decrypting ${cdoc_file}">&3
95+
run run_alias cdoc-cli decrypt -f ${TEST_VECTORS}/${cdoc_file} -k $CLI_KEYS_DIR/cdoc2client.pem --output $TEST_RESULTS_DIR
96+
97+
assertSuccessfulExitCode
98+
assert_output --partial "Decrypting ${TEST_VECTORS}/${cdoc_file}"
99+
assertSuccessfulDecryption
100+
}
101+
102+
@test "test3: successfully encrypt CDOC2 container with RSA" {
92103
local cdoc_file="rsa_simple.cdoc"
93104
run run_alias cdoc-cli create -f $TEST_RESULTS_DIR/$cdoc_file \
94105
-p $CLI_KEYS_DIR/rsa_pub.pem $FILE_FOR_ENCRYPTION
@@ -104,13 +115,13 @@ run_alias() {
104115
rm -f $TEST_RESULTS_DIR/$cdoc_file
105116
}
106117

107-
@test "test3: successfully encrypt CDOC2 container with password" {
118+
@test "test4: successfully encrypt CDOC2 container with password" {
108119
run run_alias cdoc-cli create -f $CDOC2_CONTAINER -pw $PASSWORD_WITH_LABEL $FILE_FOR_ENCRYPTION
109120
assertSuccessfulExitCode
110121
assert_output --partial "Created $CDOC2_CONTAINER"
111122
}
112123

113-
@test "test4: successfully decrypt CDOC2 container from test1 with password" {
124+
@test "test5: successfully decrypt CDOC2 container from test1 with password" {
114125
run run_alias cdoc-cli decrypt -f $CDOC2_CONTAINER -pw $PASSWORD_WITH_LABEL --output $TEST_RESULTS_DIR
115126
assertSuccessfulExitCode
116127
assert_output --partial "Decrypting $CDOC2_CONTAINER"
@@ -119,20 +130,42 @@ run_alias() {
119130
removeEncryptedCdoc
120131
}
121132

122-
@test "test5: successfully encrypt CDOC2 container with few files" {
133+
@test "test6: assert password decryption is compatible with earlier encrypted CDOC2" {
134+
local earlier_encrypted_cdoc2_file="password_old_version_DO_NOT_DELETE.cdoc"
135+
136+
echo "# Decrypting ${earlier_encrypted_cdoc2_file}">&3
137+
run run_alias cdoc-cli decrypt -f ${TEST_VECTORS}/${earlier_encrypted_cdoc2_file} -pw $PASSWORD_WITH_LABEL --output $TEST_RESULTS_DIR
138+
139+
assertSuccessfulExitCode
140+
assert_output --partial "Decrypting ${TEST_VECTORS}/${earlier_encrypted_cdoc2_file}"
141+
assertSuccessfulDecryption
142+
}
143+
144+
@test "test7: assert decryption with symmetric key is compatible with earlier encrypted CDOC2" {
145+
local earlier_encrypted_cdoc2_file="symmetric_old_version_DO_NOT_DELETE.cdoc"
146+
147+
echo "# Decrypting ${earlier_encrypted_cdoc2_file}">&3
148+
run run_alias cdoc-cli decrypt -f ${TEST_VECTORS}/${earlier_encrypted_cdoc2_file} --secret $SECRET_WITH_LABEL --output $TEST_RESULTS_DIR
149+
150+
assertSuccessfulExitCode
151+
assert_output --partial "Decrypting ${TEST_VECTORS}/${earlier_encrypted_cdoc2_file}"
152+
assertSuccessfulDecryption
153+
}
154+
155+
@test "test8: successfully encrypt CDOC2 container with few files" {
123156
run run_alias cdoc-cli create -f $CDOC2_CONTAINER -pw $PASSWORD_WITH_LABEL $FILE_FOR_ENCRYPTION $FILE_FOR_ENCRYPTION2
124157
assertSuccessfulExitCode
125158

126159
removeEncryptedCdoc
127160
}
128161

129-
@test "test6: fail to encrypt CDOC2 container with password if it's validation has failed" {
162+
@test "test9: fail to encrypt CDOC2 container with password if it's validation has failed" {
130163
password="passwordlabel:short";
131164
run run_alias cdoc-cli create -f $CDOC2_CONTAINER -pw $password $FILE_FOR_ENCRYPTION
132165
assertFailure
133166
}
134167

135-
@test "test7: fail to decrypt CDOC2 container with wrong decryption key type" {
168+
@test "test10: fail to decrypt CDOC2 container with wrong decryption key type" {
136169
# encrypt with secret key
137170
run run_alias cdoc-cli create -f $CDOC2_CONTAINER --secret $SECRET_WITH_LABEL $FILE_FOR_ENCRYPTION
138171
assertSuccessfulExitCode
@@ -144,7 +177,7 @@ run_alias() {
144177
removeEncryptedCdoc
145178
}
146179

147-
@test "test8: successfully encrypt CDOC with two keys and decrypt with one of them" {
180+
@test "test11: successfully encrypt CDOC with two keys and decrypt with one of them" {
148181
# encrypt with secret key and password
149182
run run_alias cdoc-cli create -f $CDOC2_CONTAINER --secret $SECRET_WITH_LABEL -pw $PASSWORD_WITH_LABEL $FILE_FOR_ENCRYPTION
150183
assertSuccessfulExitCode
@@ -158,7 +191,7 @@ run_alias() {
158191
removeEncryptedCdoc
159192
}
160193

161-
@test "test9: successfully re-encrypt CDOC2 container" {
194+
@test "test12: successfully re-encrypt CDOC2 container" {
162195
# prepare encrypted container for further re-encryption
163196
run run_alias cdoc-cli create -f $CDOC2_CONTAINER --secret $SECRET_WITH_LABEL $FILE_FOR_ENCRYPTION
164197
assertSuccessfulExitCode
@@ -184,7 +217,7 @@ run_alias() {
184217
removeEncryptedCdoc
185218
}
186219

187-
@test "test10: fail re-encryption within the same directory" {
220+
@test "test13: fail re-encryption within the same directory" {
188221
run run_alias cdoc-cli create -f $CDOC2_CONTAINER --secret $SECRET_WITH_LABEL $FILE_FOR_ENCRYPTION
189222
assertSuccessfulExitCode
190223

3.66 KB
Binary file not shown.
4.85 KB
Binary file not shown.
4.79 KB
Binary file not shown.

0 commit comments

Comments
 (0)