forked from open-quantum-safe/liboqs-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignature.java
More file actions
346 lines (312 loc) · 12.8 KB
/
Signature.java
File metadata and controls
346 lines (312 loc) · 12.8 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package org.openquantumsafe;
import java.util.Arrays;
/**
* \brief Signature Mechanisms
*/
public class Signature {
/**
* \brief Signature algorithm details
*/
class SignatureDetails {
String method_name;
String alg_version;
byte claimed_nist_level;
boolean is_euf_cma;
long length_public_key;
long length_secret_key;
long max_length_signature;
/**
* \brief Print Signature algorithm details
*/
void printSignature() {
System.out.println("Signature Details:" +
"\n Name: " + this.method_name +
"\n Version: " + this.alg_version +
"\n Claimed NIST level: " + this.claimed_nist_level +
"\n Is IND-CCA: " + this.is_euf_cma +
"\n Length public key (bytes): " + this.length_public_key +
"\n Length secret key (bytes): " + this.length_secret_key +
"\n Maximum length signature (bytes): " + this.max_length_signature
);
}
}
/**
* Keep native pointers for Java to remember which C memory it is managing.
*/
private long native_sig_handle_;
private byte[] public_key_;
private byte[] secret_key_;
/**
* Private object that has the signature details.
*/
private SignatureDetails alg_details_;
/**
* \brief Constructs an instance of oqs::Signature
* \param alg_name Cryptographic algorithm method_name
*/
public Signature(String alg_name) throws RuntimeException {
this(alg_name, null);
}
/**
* \brief Constructs an instance of oqs::Signature
* \param alg_name Cryptographic algorithm method_name
* \param secret_key Secret key
*/
public Signature(String alg_name, byte[] secret_key)
throws RuntimeException {
// signature not enabled
if (!Sigs.is_sig_enabled(alg_name)) {
// perhaps it's supported
if (Sigs.is_sig_supported(alg_name)) {
throw new MechanismNotEnabledError(alg_name);
} else {
throw new MechanismNotSupportedError(alg_name);
}
}
create_sig_new(alg_name);
alg_details_ = get_sig_details();
// initialize keys
if (secret_key != null) {
this.secret_key_ = Arrays.copyOf(secret_key, secret_key.length);
} else {
this.secret_key_ = new byte[(int) alg_details_.length_secret_key];
}
this.public_key_ = new byte[(int) alg_details_.length_public_key];
}
/**
* \brief Wrapper for OQS_API OQS_SIG *OQS_SIG_new(const char *method_name);
* Calls OQS_SIG_new and stores return value to native_sig_handle_.
*/
public native void create_sig_new(String method_name);
/**
* \brief Wrapper for OQS_API void OQS_SIG_free(OQS_SIG *sig);
* Frees an OQS_SIG object that was constructed by OQS_SIG_new.
*/
private native void free_sig();
/**
* \brief Initialize and fill a SignatureDetails object from the native
* C struct pointed by native_sig_handle_.
*/
private native SignatureDetails get_sig_details();
/**
* \brief Wrapper for OQS_API OQS_STATUS OQS_SIG_keypair(const OQS_SIG *sig,
* uint8_t *public_key, uint8_t *secret_key);
* \param Public key
* \param Secret key
* \return Status
*/
private native int generate_keypair(byte[] public_key, byte[] secret_key);
/**
* \brief Wrapper for OQS_API OQS_STATUS OQS_SIG_sign(const OQS_SIG *sig,
* uint8_t *signature,
* size_t *signature_len,
* const uint8_t *message,
* size_t message_len,
* const uint8_t *secret_key);
* \param signature
* \param signature_len_ret
* \param message
* \param message_len
* \param secret_key
* \return Status
*/
private native int sign(byte[] signature, Mutable<Long> signature_len_ret,
byte[] message, long message_len, byte[] secret_key);
/**
* \brief Wrapper for OQS_API OQS_STATUS OQS_SIG_verify(const OQS_SIG *sig,
* const uint8_t *message,
* size_t message_len,
* const uint8_t *signature,
* size_t signature_len,
* const uint8_t *public_key);
* \param message
* \param message_len
* \param signature
* \param signature_len
* \param public_key
* \return True if the signature is valid, false otherwise
*/
private native boolean verify(byte[] message, long message_len,
byte[] signature, long signature_len,
byte[] public_key);
/**
* \brief Wrapper for OQS_API OQS_STATUS OQS_SIG_sign_with_ctx_str(const OQS_SIG *sig,
* uint8_t *signature,
* size_t *signature_len,
* const uint8_t *message,
* size_t message_len,
* const uint8_t *ctx,
* size_t ctx_len,
* const uint8_t *secret_key);
* \param signature
* \param signature_len_ret
* \param message
* \param message_len
* \param ctx
* \param ctx_len
* \param secret_key
* \return Status
*/
private native int sign_with_ctx_str(byte[] signature, Mutable<Long> signature_len_ret,
byte[] message, long message_len, byte[] ctx, long ctx_len,
byte[] secret_key);
/**
* \brief Wrapper for OQS_API OQS_STATUS OQS_SIG_verify_with_ctx_str(const OQS_SIG *sig,
* const uint8_t *message,
* size_t message_len,
* const uint8_t *signature,
* size_t signature_len,
* const uint8_t *ctx,
* size_t ctx_len,
* const uint8_t *public_key);
* \param message
* \param message_len
* \param signature
* \param signature_len
* \param ctx
* \param ctx_len
* \param public_key
* \return True if the signature is valid, false otherwise
*/
private native boolean verify_with_ctx_str(byte[] message, long message_len,
byte[] signature, long signature_len,
byte[] ctx, long ctx_len,
byte[] public_key);
/**
* \brief Invoke native free_sig
*/
public void dispose_sig() {
Common.wipe(this.secret_key_);
free_sig();
}
/**
* \brief Invoke native generate_keypair method using the PK and SK lengths
* from alg_details_. Check return value and if != 0 throw RuntimeException.
*/
public byte[] generate_keypair() throws RuntimeException {
int rv_ = generate_keypair(this.public_key_, this.secret_key_);
if (rv_ != 0) throw new RuntimeException("Cannot generate keypair");
return this.public_key_;
}
/**
* \brief Return public key
*/
public byte[] export_public_key() {
return this.public_key_;
}
/**
* \brief Return secret key
*/
public byte[] export_secret_key() {
return this.secret_key_;
}
/**
* \brief Invoke native sign method
* \param message
* \return signature
*/
public byte[] sign(byte[] message) throws RuntimeException {
if (this.secret_key_.length != alg_details_.length_secret_key) {
throw new RuntimeException("Incorrect secret key length, " +
"make sure you specify one in the " +
"constructor or run generate_keypair()");
}
byte[] signature = new byte[(int) alg_details_.max_length_signature];
Mutable<Long> signature_len_ret = new Mutable<>();
int rv_= sign(signature, signature_len_ret,
message, message.length, this.secret_key_);
long actual_signature_len = signature_len_ret.value;
byte[] actual_signature = new byte[(int) actual_signature_len];
System.arraycopy(signature, 0,
actual_signature, 0, (int) actual_signature_len);
if (rv_ != 0) throw new RuntimeException("Cannot sign message");
return actual_signature;
}
/**
* \brief Invoke native verify method
* \param message
* \param signature
* \param public_key
* \return True if the signature is valid, false otherwise
*/
public boolean verify(byte[] message, byte[] signature, byte[] public_key)
throws RuntimeException {
if (public_key.length != alg_details_.length_public_key) {
throw new RuntimeException("Incorrect public key length");
}
if (signature.length > alg_details_.max_length_signature) {
throw new RuntimeException("Incorrect signature length");
}
return verify(message, message.length, signature, signature.length, public_key);
}
/**
* \brief Invoke native sign method
* \param message
* \param ctx
* \return signature
*/
public byte[] sign(byte[] message, byte[] ctx) throws RuntimeException {
if (this.secret_key_.length != alg_details_.length_secret_key) {
throw new RuntimeException("Incorrect secret key length, " +
"make sure you specify one in the " +
"constructor or run generate_keypair()");
}
byte[] signature = new byte[(int) alg_details_.max_length_signature];
Mutable<Long> signature_len_ret = new Mutable<>();
int ctx_len = (ctx == null) ? 0 : ctx.length;
int rv_= sign_with_ctx_str(signature, signature_len_ret,
message, message.length,
ctx, ctx_len,
this.secret_key_);
long actual_signature_len = signature_len_ret.value;
byte[] actual_signature = new byte[(int) actual_signature_len];
System.arraycopy(signature, 0,
actual_signature, 0, (int) actual_signature_len);
if (rv_ != 0) throw new RuntimeException("Cannot sign message");
return actual_signature;
}
/**
* \brief Invoke native verify method
* \param message
* \param signature
* \param ctx
* \param public_key
* \return True if the signature is valid, false otherwise
*/
public boolean verify(byte[] message, byte[] signature, byte[] ctx, byte[] public_key)
throws RuntimeException {
if (public_key.length != alg_details_.length_public_key) {
throw new RuntimeException("Incorrect public key length");
}
if (signature.length > alg_details_.max_length_signature) {
throw new RuntimeException("Incorrect signature length");
}
int ctx_len = (ctx == null) ? 0 : ctx.length;
return verify_with_ctx_str(message, message.length, signature, signature.length, ctx, ctx_len, public_key);
}
/**
* \brief Print Signature. If a SignatureDetails object is not
* initialized, initialize it and fill it using native C code.
*/
public void print_signature() {
if (alg_details_ == null) {
alg_details_ = get_sig_details();
}
System.out.println("Signature: " + alg_details_.method_name);
}
/**
* \brief print signature algorithm details. If a SignatureDetails object is
* not initialized, initialize it and fill it using native C code.
*/
public void print_details() {
if (alg_details_ == null) {
alg_details_ = get_sig_details();
}
alg_details_.printSignature();
}
public static class Mutable<T> {
T value;
public void setValue(T t) { this.value = t; }
public T getValue() { return this.value; }
}
}