Skip to content

Commit 77d3a6b

Browse files
committed
[CRITICAL CHANGE] Remove message at the end of signature
CKB fits detached message mode of SPHINCS+, since the signing messages will be calculated based on enclosing CKB transactions.
1 parent 9e02bb4 commit 77d3a6b

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

contracts/c-sphincs-all-in-one-lock/ckb-sphincsplus.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@
1818
enum SphincsPlusError {
1919
SphincsPlusError_Params = 200,
2020
SphincsPlusError_Verify,
21-
SphincsPlusError_Verify_MsgLen,
22-
SphincsPlusError_Verify_MsgCmp,
21+
SphincsPlusError_OutputSignLength,
2322
};
2423

2524
#ifndef CKB_VM
2625

27-
// TODO: switch the code to use crypto_sign_signature / crypto_sign_verify,
28-
// so we don't need to append message to the end of the signature.
29-
3026
#include <stdlib.h>
3127

3228
int sphincs_plus_generate_keypair(uint8_t *pk, uint8_t *sk) {
@@ -35,11 +31,14 @@ int sphincs_plus_generate_keypair(uint8_t *pk, uint8_t *sk) {
3531

3632
int sphincs_plus_sign(const uint8_t *message, const uint8_t *sk,
3733
uint8_t *out_sign) {
38-
unsigned long long out_sign_len = SPHINCS_PLUS_SIGN_SIZE;
39-
int ret = crypto_sign(out_sign, (unsigned long long *)&out_sign_len, message,
40-
SPX_MLEN, sk);
34+
size_t out_sign_len = 0;
35+
int ret =
36+
crypto_sign_signature(out_sign, &out_sign_len, message, SPX_MLEN, sk);
37+
if (ret != 0) {
38+
return ret;
39+
}
4140
if ((uint32_t)out_sign_len != SPHINCS_PLUS_SIGN_SIZE) {
42-
return 1;
41+
return SphincsPlusError_OutputSignLength;
4342
}
4443
return ret;
4544
}
@@ -58,21 +57,12 @@ int sphincs_plus_verify(const uint8_t *sign, uint32_t sign_size,
5857
pubkey_size != SPHINCS_PLUS_PK_SIZE) {
5958
return SphincsPlusError_Params;
6059
}
61-
unsigned char mout[SPX_BYTES + SPX_MLEN];
62-
unsigned long long mlen = 0;
6360

64-
int err = crypto_sign_open(mout, &mlen, sign, SPHINCS_PLUS_SIGN_SIZE, pubkey);
61+
int err = crypto_sign_verify(sign, SPHINCS_PLUS_SIGN_SIZE, message,
62+
message_size, pubkey);
6563
if (err != 0) {
6664
return SphincsPlusError_Verify;
6765
}
6866

69-
if (mlen != SPX_MLEN) {
70-
return SphincsPlusError_Verify_MsgLen;
71-
}
72-
73-
if (memcmp(mout, message, SPX_MLEN) != 0) {
74-
return SphincsPlusError_Verify_MsgCmp;
75-
}
76-
7767
return 0;
7868
}

contracts/c-sphincs-all-in-one-lock/ckb-sphincsplus.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#define SPHINCS_PLUS_PK_SIZE SPX_PK_BYTES
99
#define SPHINCS_PLUS_SK_SIZE SPX_SK_BYTES
10-
#define SPHINCS_PLUS_SIGN_SIZE (SPX_BYTES + SPX_MLEN)
10+
#define SPHINCS_PLUS_SIGN_SIZE SPX_BYTES
1111

1212
#ifndef CKB_VM
1313
int sphincs_plus_generate_keypair(uint8_t *pk, uint8_t *sk);

tests/sphincsplus/test-ckb-sphincs-plus.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#include <assert.h>
33
#include <stdio.h>
4+
#include <stdlib.h>
45
#include <string.h>
56
#include <time.h>
67

@@ -14,7 +15,7 @@
1415
#define ASSERT(c) \
1516
if (!(c)) { \
1617
printf("Assert: %s:%d", __FILE__, __LINE__); \
17-
((void)0); \
18+
exit(-1); \
1819
}
1920
#endif // ASSERT
2021

@@ -52,8 +53,13 @@ int main() {
5253
clock_t start, end;
5354
start = clock();
5455

56+
/*
57+
* G_TEST_DATA_SIGN was prepared at a time where signature is followed
58+
* by message. We remove this behavior later, so sizeof(G_TEST_DATA_SIGN)
59+
* will not give correct result here.
60+
*/
5561
int ret =
56-
sphincs_plus_verify(G_TEST_DATA_SIGN, sizeof(G_TEST_DATA_SIGN),
62+
sphincs_plus_verify(G_TEST_DATA_SIGN, SPHINCS_PLUS_SIGN_SIZE,
5763
G_TEST_DATA_MSG, sizeof(G_TEST_DATA_MSG),
5864
G_TEST_DATA_PUB_KEY, sizeof(G_TEST_DATA_PUB_KEY));
5965
ASSERT(ret == 0);

0 commit comments

Comments
 (0)