-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBatchSignAndVerifyEd25519Test.java
More file actions
135 lines (121 loc) · 5.92 KB
/
Copy pathBatchSignAndVerifyEd25519Test.java
File metadata and controls
135 lines (121 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright 2016-2022 chronicle.software
*
* https://chronicle.software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.salt;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesUtil;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.wire.TextWire;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.*;
@SuppressWarnings("rawtypes")
public class BatchSignAndVerifyEd25519Test {
static final BytesForTesting bft = new BytesForTesting();
@SuppressWarnings("unchecked")
public static Collection<Object[]> data() throws IOException {
String[] paramInput = {"test-vectors/ed25519-rfc-8032.yaml", "test-vectors/ed25519-python.yaml"};
ArrayList<Object[]> params = new ArrayList<>();
for (String paramFile : paramInput) {
TextWire textWire = new TextWire(BytesUtil.readFile(paramFile)).useTextDocuments();
List<Map<String, Object>> testData = (List<Map<String, Object>>) textWire.readMap().get("tests");
for (Map<String, Object> data : testData) {
String[] param = new String[5];
param[0] = data.get("SECRET KEY").toString();
param[1] = data.get("PUBLIC KEY").toString();
param[2] = data.get("MESSAGE").toString();
param[3] = data.get("SIGNATURE").toString();
param[4] = data.get("NAME").toString();
params.add(param);
}
}
return params;
}
@AfterAll
public static void teardownClass() {
bft.cleanup();
}
@ParameterizedTest(name = "{4}")
@MethodSource("data")
public void signAndVerify(String privateOrSecretKey, String publicKey, String message, String signExpected, String testName) {
assumeFalse(OS.isWindows());
Bytes<?> privateKeyBuffer = null;
Bytes<?> secretKeyBuffer = null;
Bytes<?> privateOrSecret = bft.fromHex(privateOrSecretKey);
if (privateOrSecret.readRemaining() == Ed25519.SECRET_KEY_LENGTH) {
secretKeyBuffer = privateOrSecret;
} else {
privateKeyBuffer = privateOrSecret;
}
Bytes<?> publicKeyBuffer = bft.fromHex(publicKey);
if (secretKeyBuffer == null) {
secretKeyBuffer = bft.bytesWithZeros(Ed25519.SECRET_KEY_LENGTH);
Bytes<?> tmpPublicKeyBuffer = bft.bytesWithZeros(Ed25519.PUBLIC_KEY_LENGTH);
Ed25519.privateToPublicAndSecret(tmpPublicKeyBuffer, secretKeyBuffer, privateKeyBuffer);
assertEquals(publicKeyBuffer.toHexString(), tmpPublicKeyBuffer.toHexString());
}
Bytes<?> messageBuffer = bft.fromHex(message);
Bytes<?> signExpectedBuffer;
if (signExpected.length() == 128) {
signExpectedBuffer = Bytes.wrapForRead(DatatypeConverter.parseHexBinary(signExpected + message));
} else {
signExpectedBuffer = Bytes.wrapForRead(DatatypeConverter.parseHexBinary(signExpected));
}
Bytes<?> signedMsgBuffer = bft.fromHex(Ed25519.SIGNATURE_LENGTH, message);
signedMsgBuffer.writePosition(0);
Ed25519.sign(signedMsgBuffer, messageBuffer, secretKeyBuffer);
assertEquals(signExpectedBuffer.toHexString(), signedMsgBuffer.toHexString());
signedMsgBuffer.readPosition(0);
publicKeyBuffer.readPositionRemaining(0, Ed25519.PUBLIC_KEY_LENGTH);
assertTrue(Ed25519.verify(signedMsgBuffer, publicKeyBuffer));
}
@ParameterizedTest(name = "{4}")
@MethodSource("data")
public void signAndVerifyDetached(String privateOrSecretKey, String publicKey, String message, String signExpected, String testName) {
assumeFalse(OS.isWindows());
Bytes<?> privateKeyBuffer = null;
Bytes<?> secretKeyBuffer = null;
Bytes<?> privateOrSecret = bft.fromHex(privateOrSecretKey);
if (privateOrSecret.readRemaining() == Ed25519.SECRET_KEY_LENGTH) {
secretKeyBuffer = privateOrSecret;
} else {
privateKeyBuffer = privateOrSecret;
}
Bytes<?> publicKeyBuffer = bft.fromHex(publicKey);
if (secretKeyBuffer == null) {
secretKeyBuffer = bft.bytesWithZeros(Ed25519.SECRET_KEY_LENGTH);
Bytes<?> tmpPublicKeyBuffer = bft.bytesWithZeros(Ed25519.PUBLIC_KEY_LENGTH);
Ed25519.privateToPublicAndSecret(tmpPublicKeyBuffer, secretKeyBuffer, privateKeyBuffer);
assertEquals(publicKeyBuffer.toHexString(), tmpPublicKeyBuffer.toHexString());
}
Bytes<?> messageBuffer = bft.fromHex(message);
Bytes<?> signExpectedBuffer = Bytes.wrapForRead(DatatypeConverter.parseHexBinary(signExpected.substring(0, 128)));
final int length = messageBuffer.length();
Ed25519.sign(messageBuffer, length, 0, length, secretKeyBuffer);
assertEquals(signExpectedBuffer.toHexString(), messageBuffer.subBytes(length, 64).bytesForRead().toHexString());
publicKeyBuffer.readPositionRemaining(0, Ed25519.PUBLIC_KEY_LENGTH);
assertTrue(Ed25519.verify(messageBuffer, length, 0, length, publicKeyBuffer));
}
}