1
1
mirror of https://github.com/henrydcase/pqc.git synced 2024-11-23 16:08:59 +00:00
pqcrypto/crypto_sign/rainbowIIIc-classic/clean/sign.c

75 lines
2.4 KiB
C
Raw Normal View History

2019-06-22 17:17:07 +01:00
/// @file sign.c
/// @brief the implementations for functions in api.h
///
///
#include "api.h"
#include "rainbow.h"
#include "rainbow_config.h"
#include "rainbow_keypair.h"
#include "randombytes.h"
#include "utils_hash.h"
#include <stdlib.h>
#include <string.h>
2019-07-24 09:15:48 +01:00
int PQCLEAN_RAINBOWIIICCLASSIC_CLEAN_crypto_sign_keypair(unsigned char *pk, unsigned char *sk) {
2019-06-22 17:17:07 +01:00
unsigned char sk_seed[LEN_SKSEED] = {0};
2019-07-24 09:15:48 +01:00
randombytes(sk_seed, LEN_SKSEED);
2019-06-22 17:17:07 +01:00
2019-07-24 09:15:48 +01:00
PQCLEAN_RAINBOWIIICCLASSIC_CLEAN_generate_keypair((pk_t *)pk, (sk_t *)sk, sk_seed);
2019-06-22 17:17:07 +01:00
return 0;
}
2019-07-24 09:15:48 +01:00
int PQCLEAN_RAINBOWIIICCLASSIC_CLEAN_crypto_sign(unsigned char *sm, size_t *smlen, const unsigned char *m, size_t mlen, const unsigned char *sk) {
2019-06-22 17:17:07 +01:00
unsigned char digest[_HASH_LEN];
2019-07-24 09:15:48 +01:00
PQCLEAN_RAINBOWIIICCLASSIC_CLEAN_hash_msg(digest, _HASH_LEN, m, mlen);
2019-06-22 17:17:07 +01:00
2019-07-24 09:15:48 +01:00
memcpy(sm, m, mlen);
2019-06-22 17:17:07 +01:00
smlen[0] = mlen + _SIGNATURE_BYTE;
2019-07-24 09:15:48 +01:00
return PQCLEAN_RAINBOWIIICCLASSIC_CLEAN_rainbow_sign(sm + mlen, (const sk_t *)sk, digest);
2019-06-22 17:17:07 +01:00
}
2019-07-24 09:15:48 +01:00
int PQCLEAN_RAINBOWIIICCLASSIC_CLEAN_crypto_sign_open(unsigned char *m, size_t *mlen, const unsigned char *sm, size_t smlen, const unsigned char *pk) {
2019-06-25 11:42:49 +01:00
int rc;
2019-07-24 09:15:48 +01:00
if (_SIGNATURE_BYTE > smlen) {
2019-06-25 11:42:49 +01:00
rc = -1;
} else {
*mlen = smlen - _SIGNATURE_BYTE;
2019-06-22 17:17:07 +01:00
2019-06-25 11:42:49 +01:00
unsigned char digest[_HASH_LEN];
PQCLEAN_RAINBOWIIICCLASSIC_CLEAN_hash_msg(digest, _HASH_LEN, sm, *mlen);
2019-06-22 17:17:07 +01:00
2019-06-25 11:42:49 +01:00
rc = PQCLEAN_RAINBOWIIICCLASSIC_CLEAN_rainbow_verify(digest, sm + mlen[0], (const pk_t *)pk);
}
if (!rc) {
2019-07-24 09:15:48 +01:00
memcpy(m, sm, smlen - _SIGNATURE_BYTE);
2019-06-25 11:42:49 +01:00
} else { // bad signature
*mlen = (size_t) -1;
memset(m, 0, smlen);
}
return rc;
2019-06-22 17:17:07 +01:00
}
int PQCLEAN_RAINBOWIIICCLASSIC_CLEAN_crypto_sign_signature(
uint8_t *sig, size_t *siglen,
const uint8_t *m, size_t mlen, const uint8_t *sk) {
unsigned char digest[_HASH_LEN];
2019-07-24 09:15:48 +01:00
PQCLEAN_RAINBOWIIICCLASSIC_CLEAN_hash_msg(digest, _HASH_LEN, m, mlen);
2019-06-22 17:17:07 +01:00
*siglen = _SIGNATURE_BYTE;
2019-07-24 09:15:48 +01:00
return PQCLEAN_RAINBOWIIICCLASSIC_CLEAN_rainbow_sign(sig, (const sk_t *)sk, digest);
2019-06-22 17:17:07 +01:00
}
int PQCLEAN_RAINBOWIIICCLASSIC_CLEAN_crypto_sign_verify(
const uint8_t *sig, size_t siglen,
const uint8_t *m, size_t mlen, const uint8_t *pk) {
if (siglen != _SIGNATURE_BYTE) {
return -1;
}
unsigned char digest[_HASH_LEN];
2019-07-24 09:15:48 +01:00
PQCLEAN_RAINBOWIIICCLASSIC_CLEAN_hash_msg(digest, _HASH_LEN, m, mlen);
return PQCLEAN_RAINBOWIIICCLASSIC_CLEAN_rainbow_verify(digest, sig, (const pk_t *)pk);
2019-06-22 17:17:07 +01:00
}