2023-06-01 00:00:00 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An example to demonstrate how to use SQIsign with the NIST API.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-02-06 00:00:00 +00:00
|
|
|
#include <inttypes.h>
|
2023-06-01 00:00:00 +00:00
|
|
|
#include <mem.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
2025-02-06 00:00:00 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
#include <api.h>
|
|
|
|
|
#include <rng.h>
|
|
|
|
|
#include <bench_test_arguments.h>
|
|
|
|
|
#if defined(TARGET_BIG_ENDIAN)
|
|
|
|
|
#include <tutil.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static uint32_t rand_u32()
|
|
|
|
|
{
|
|
|
|
|
unsigned char buf[4];
|
|
|
|
|
if (randombytes(buf, sizeof(buf)))
|
|
|
|
|
abort();
|
|
|
|
|
return ((uint32_t) buf[3] << 24)
|
|
|
|
|
| ((uint32_t) buf[2] << 16)
|
|
|
|
|
| ((uint32_t) buf[1] << 8)
|
|
|
|
|
| ((uint32_t) buf[0] << 0);
|
|
|
|
|
}
|
2023-06-01 00:00:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Example for SQIsign variant:
|
|
|
|
|
* - crypto_sign_keypair
|
|
|
|
|
* - crypto_sign
|
|
|
|
|
* - crypto_sign_open
|
2025-02-06 00:00:00 +00:00
|
|
|
*
|
2023-06-01 00:00:00 +00:00
|
|
|
* @return int return code
|
|
|
|
|
*/
|
2025-02-06 00:00:00 +00:00
|
|
|
static int
|
|
|
|
|
example_sqisign(void)
|
|
|
|
|
{
|
2023-06-01 00:00:00 +00:00
|
|
|
|
2025-02-06 00:00:00 +00:00
|
|
|
unsigned long long msglen = rand_u32() % 100;
|
2023-06-01 00:00:00 +00:00
|
|
|
unsigned long long smlen = CRYPTO_BYTES + msglen;
|
|
|
|
|
|
2025-02-06 00:00:00 +00:00
|
|
|
unsigned char *sk = calloc(CRYPTO_SECRETKEYBYTES, 1);
|
|
|
|
|
unsigned char *pk = calloc(CRYPTO_PUBLICKEYBYTES, 1);
|
2023-06-01 00:00:00 +00:00
|
|
|
|
2025-02-06 00:00:00 +00:00
|
|
|
unsigned char *sm = calloc(smlen, 1);
|
2023-06-01 00:00:00 +00:00
|
|
|
|
2025-02-06 00:00:00 +00:00
|
|
|
unsigned char msg[msglen], msg2[msglen];
|
2023-06-01 00:00:00 +00:00
|
|
|
|
|
|
|
|
printf("Example with %s\n", CRYPTO_ALGNAME);
|
|
|
|
|
|
|
|
|
|
printf("crypto_sign_keypair -> ");
|
|
|
|
|
int res = crypto_sign_keypair(pk, sk);
|
|
|
|
|
if (res) {
|
|
|
|
|
printf("FAIL\n");
|
|
|
|
|
goto err;
|
|
|
|
|
} else {
|
|
|
|
|
printf("OK\n");
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-06 00:00:00 +00:00
|
|
|
// choose a random message
|
|
|
|
|
for (size_t i = 0; i < msglen; ++i)
|
|
|
|
|
msg[i] = rand_u32();
|
|
|
|
|
|
2023-06-01 00:00:00 +00:00
|
|
|
printf("crypto_sign -> ");
|
2025-02-06 00:00:00 +00:00
|
|
|
res = crypto_sign(sm, &smlen, msg, msglen, sk);
|
2023-06-01 00:00:00 +00:00
|
|
|
if (res) {
|
|
|
|
|
printf("FAIL\n");
|
|
|
|
|
goto err;
|
|
|
|
|
} else {
|
|
|
|
|
printf("OK\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("crypto_sign_open (with correct signature) -> ");
|
2025-02-06 00:00:00 +00:00
|
|
|
res = crypto_sign_open(msg2, &msglen, sm, smlen, pk);
|
|
|
|
|
if (res || msglen != sizeof(msg) || memcmp(msg, msg2, msglen)) {
|
|
|
|
|
printf("FAIL\n"); // signature was not accepted!?
|
2023-06-01 00:00:00 +00:00
|
|
|
goto err;
|
|
|
|
|
} else {
|
|
|
|
|
printf("OK\n");
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-06 00:00:00 +00:00
|
|
|
|
|
|
|
|
// fill with random bytes
|
|
|
|
|
for (size_t i = 0; i < msglen; ++i)
|
|
|
|
|
msg2[i] = rand_u32();
|
|
|
|
|
|
|
|
|
|
// let's try a single bit flip
|
|
|
|
|
size_t pos = rand_u32() % smlen;
|
|
|
|
|
sm[pos / 8] ^= 1 << pos % 8;
|
|
|
|
|
|
|
|
|
|
res = crypto_sign_open(msg2, &msglen, sm, smlen, pk);
|
|
|
|
|
|
2023-06-01 00:00:00 +00:00
|
|
|
printf("crypto_sign_open (with altered signature) -> ");
|
2025-02-06 00:00:00 +00:00
|
|
|
if (!res) {
|
|
|
|
|
printf("FAIL\n"); // signature was accepted anyway!?
|
2023-06-01 00:00:00 +00:00
|
|
|
res = -1;
|
|
|
|
|
goto err;
|
2025-02-06 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2023-06-01 00:00:00 +00:00
|
|
|
printf("OK\n");
|
2025-02-06 00:00:00 +00:00
|
|
|
res = 0;
|
|
|
|
|
|
|
|
|
|
if (msglen)
|
|
|
|
|
printf("WARNING: verification failed but the message length was returned nonzero; misuse-prone API\n");
|
|
|
|
|
|
|
|
|
|
unsigned char any = 0;
|
|
|
|
|
for (size_t i = 0; i < msglen; ++i)
|
|
|
|
|
any |= msg2[i];
|
|
|
|
|
if (any)
|
|
|
|
|
printf("WARNING: verification failed but the message buffer was not zeroed out; misuse-prone API\n");
|
2023-06-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err:
|
|
|
|
|
sqisign_secure_free(sk, CRYPTO_SECRETKEYBYTES);
|
2025-02-06 00:00:00 +00:00
|
|
|
free(pk);
|
|
|
|
|
free(sm);
|
|
|
|
|
|
2023-06-01 00:00:00 +00:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-06 00:00:00 +00:00
|
|
|
int
|
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
uint32_t seed[12] = { 0 };
|
|
|
|
|
int help = 0;
|
|
|
|
|
int seed_set = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
|
if (!help && strcmp(argv[i], "--help") == 0) {
|
|
|
|
|
help = 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!seed_set && !parse_seed(argv[i], seed)) {
|
|
|
|
|
seed_set = 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (help) {
|
|
|
|
|
printf("Usage: %s [--seed=<seed>]\n", argv[0]);
|
|
|
|
|
printf("Where <seed> is the random seed to be used; if not present, a random seed is "
|
|
|
|
|
"generated\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!seed_set) {
|
|
|
|
|
randombytes_select((unsigned char *)seed, sizeof(seed));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print_seed(seed);
|
|
|
|
|
|
|
|
|
|
#if defined(TARGET_BIG_ENDIAN)
|
|
|
|
|
for (int i = 0; i < 12; i++) {
|
|
|
|
|
seed[i] = BSWAP32(seed[i]);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
randombytes_init((unsigned char *)seed, NULL, 256);
|
|
|
|
|
|
2023-06-01 00:00:00 +00:00
|
|
|
return example_sqisign();
|
|
|
|
|
}
|