-
Notifications
You must be signed in to change notification settings - Fork 26
/
crypto_sign.c.in
128 lines (104 loc) · 3.46 KB
/
crypto_sign.c.in
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
/*
* This file is part of the optimized implementation of the Picnic signature scheme.
* See the accompanying documentation for complete details.
*
* The code is provided under the MIT license, see LICENSE for
* more details.
* SPDX-License-Identifier: MIT
*/
#ifdef SUPERCOP
#include "crypto_sign.h"
#else
#include "api.h"
#endif
#include "endian_compat.h"
#include "picnic.h"
#include <string.h>
// This file provides the implementation of the SUPERCOP / NIST competition API for a specific
// Picnic instance. For this implementation, we read/write the keys directly from the user provider
// memory areas.
//
// As those interfaces require the "signature" to include message, the signature is serialized as
// * 4-byte signature length (little endian)
// * the message message
// * the actual signature
int crypto_sign_keypair(unsigned char* pk, unsigned char* sk) {
picnic_publickey_t ppk;
picnic_privatekey_t psk;
int ret = picnic_keygen(PICNIC_INSTANCE, &ppk, &psk);
if (ret) {
return ret;
}
ret = picnic_write_public_key(&ppk, pk, PICNIC_PUBLIC_KEY_SIZE(PICNIC_INSTANCE));
if (ret < 0) {
picnic_clear_private_key(&psk);
return ret;
}
ret = picnic_write_private_key(&psk, sk, PICNIC_PRIVATE_KEY_SIZE(PICNIC_INSTANCE));
picnic_clear_private_key(&psk);
if (ret < 0) {
return ret;
}
return 0;
}
int crypto_sign(unsigned char* sm, unsigned long long* smlen, const unsigned char* m,
unsigned long long mlen, const unsigned char* sk) {
// The first byte encodes the parameter set and is public.
picnic_declassify(&sk[0], sizeof(unsigned char));
if (sk[0] != PICNIC_INSTANCE) {
return -3;
}
size_t signature_len = PICNIC_SIGNATURE_SIZE(PICNIC_INSTANCE);
uint32_t len = 0;
picnic_privatekey_t psk;
int ret = picnic_read_private_key(&psk, sk, PICNIC_PRIVATE_KEY_SIZE(PICNIC_INSTANCE));
if (ret < 0) {
picnic_clear_private_key(&psk);
return ret;
}
ret = picnic_sign(&psk, m, mlen, sm + sizeof(len) + mlen, &signature_len);
picnic_clear_private_key(&psk);
if (ret) {
return ret;
}
len = htole32(signature_len);
*smlen = sizeof(len) + mlen + signature_len;
// Move the message first in case m and sm overlap.
memmove(sm + sizeof(len), m, mlen);
memcpy(sm, &len, sizeof(len));
return 0;
}
int crypto_sign_open(unsigned char* m, unsigned long long* mlen, const unsigned char* sm,
unsigned long long smlen, const unsigned char* pk) {
if (pk[0] != PICNIC_INSTANCE) {
return -3;
}
uint32_t signature_len = 0;
// The signature is too short to hold the signature length.
if (smlen < sizeof(signature_len)) {
return -2;
}
memcpy(&signature_len, sm, sizeof(signature_len));
signature_len = le32toh(signature_len);
// The signature is too short to hold the signature
if (signature_len + sizeof(signature_len) > smlen) {
return -2;
}
picnic_publickey_t ppk;
int ret = picnic_read_public_key(&ppk, pk, PICNIC_PUBLIC_KEY_SIZE(PICNIC_INSTANCE));
if (ret < 0) {
return ret;
}
const size_t message_len = smlen - signature_len - sizeof(signature_len);
const uint8_t* message = sm + sizeof(signature_len);
const uint8_t* sig = sm + sizeof(signature_len) + message_len;
ret = picnic_verify(&ppk, message, message_len, sig, signature_len);
if (ret) {
return ret;
}
// The signature is valid, so copy the message to its destination.
memmove(m, message, message_len);
*mlen = message_len;
return 0;
}
// vim: ft=c