Make all apis use the uint8_t and size_t types

This commit is contained in:
Thom Wiggers 2019-03-05 14:14:47 +01:00
parent e74fcbecc0
commit 07b8c2ebe9
No known key found for this signature in database
GPG Key ID: 001BB0A7CE26E363
8 changed files with 142 additions and 123 deletions

View File

@ -1,6 +1,8 @@
#ifndef API_H #ifndef API_H
#define API_H #define API_H
#include <stdint.h>
#include "params.h" #include "params.h"
#define CRYPTO_SECRETKEYBYTES KYBER_SECRETKEYBYTES #define CRYPTO_SECRETKEYBYTES KYBER_SECRETKEYBYTES
@ -10,10 +12,10 @@
#define CRYPTO_ALGNAME "Kyber768" #define CRYPTO_ALGNAME "Kyber768"
int PQCLEAN_KYBER768_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk); int PQCLEAN_KYBER768_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk);
int PQCLEAN_KYBER768_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk); int PQCLEAN_KYBER768_CLEAN_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk);
int PQCLEAN_KYBER768_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk); int PQCLEAN_KYBER768_CLEAN_crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk);
#endif #endif

View File

@ -1,6 +1,9 @@
#ifndef API_H #ifndef API_H
#define API_H #define API_H
#include <stddef.h>
#include <stdint.h>
#define MODE 2 #define MODE 2
#define CRYPTO_PUBLICKEYBYTES 1472U #define CRYPTO_PUBLICKEYBYTES 1472U
@ -9,19 +12,19 @@
#define CRYPTO_ALGNAME "Dilithium-III" #define CRYPTO_ALGNAME "Dilithium-III"
int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign_keypair(unsigned char *pk, int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign_keypair(uint8_t *pk,
unsigned char *sk); uint8_t *sk);
int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign(unsigned char *sm, int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign(uint8_t *sm,
unsigned long long *smlen, size_t *smlen,
const unsigned char *msg, const uint8_t *msg,
unsigned long long len, size_t len,
const unsigned char *sk); const uint8_t *sk);
int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign_open(unsigned char *m, int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign_open(uint8_t *m,
unsigned long long *mlen, size_t *mlen,
const unsigned char *sm, const uint8_t *sm,
unsigned long long smlen, size_t smlen,
const unsigned char *pk); const uint8_t *pk);
#endif #endif

View File

@ -102,15 +102,15 @@ void PQCLEAN_DILITHIUMIII_CLEAN_challenge(poly *c, const unsigned char mu[CRHBYT
* *
* Description: Generates public and private key. * Description: Generates public and private key.
* *
* Arguments: - unsigned char *pk: pointer to output public key (allocated * Arguments: - uint8_t *pk: pointer to output public key (allocated
* array of CRYPTO_PUBLICKEYBYTES bytes) * array of CRYPTO_PUBLICKEYBYTES bytes)
* - unsigned char *sk: pointer to output private key (allocated * - uint8_t *sk: pointer to output private key (allocated
* array of CRYPTO_SECRETKEYBYTES bytes) * array of CRYPTO_SECRETKEYBYTES bytes)
* *
* Returns 0 (success) * Returns 0 (success)
**************************************************/ **************************************************/
int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign_keypair(unsigned char *pk, int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign_keypair(uint8_t *pk,
unsigned char *sk) { uint8_t *sk) {
unsigned int i; unsigned int i;
unsigned char seedbuf[3 * SEEDBYTES]; unsigned char seedbuf[3 * SEEDBYTES];
unsigned char tr[CRHBYTES]; unsigned char tr[CRHBYTES];
@ -168,26 +168,25 @@ int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign_keypair(unsigned char *pk,
* *
* Description: Compute signed message. * Description: Compute signed message.
* *
* Arguments: - unsigned char *sm: pointer to output signed message (allocated * Arguments: - uint8_t *sm: pointer to output signed message (allocated
* array with CRYPTO_BYTES + mlen bytes), * array with CRYPTO_BYTES + mlen bytes),
* can be equal to m * can be equal to m
* - unsigned long long *smlen: pointer to output length of signed * - size_t *smlen: pointer to output length of signed
* message * message
* - const unsigned char *m: pointer to message to be signed * - const uint8_t *m: pointer to message to be signed
* - unsigned long long mlen: length of message * - size_t mlen: length of message
* - const unsigned char *sk: pointer to bit-packed secret key * - const uint8_t *sk: pointer to bit-packed secret key
* *
* Returns 0 (success) * Returns 0 (success)
**************************************************/ **************************************************/
int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign(unsigned char *sm, int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign(uint8_t *sm,
unsigned long long *smlen, size_t *smlen,
const unsigned char *m, const uint8_t *m,
unsigned long long mlen, size_t mlen,
const unsigned char *sk) { const uint8_t *sk) {
unsigned long long i, j; unsigned long long i, j;
unsigned int n; unsigned int n;
unsigned char unsigned char seedbuf[2 * SEEDBYTES + CRHBYTES]; // TODO(thom): nonce in seedbuf (2x)
seedbuf[2 * SEEDBYTES + CRHBYTES]; // TODO(thom): nonce in seedbuf (2x)
unsigned char tr[CRHBYTES]; unsigned char tr[CRHBYTES];
unsigned char *rho, *key, *mu; unsigned char *rho, *key, *mu;
uint16_t nonce = 0; uint16_t nonce = 0;
@ -307,20 +306,20 @@ rej:
* *
* Description: Verify signed message. * Description: Verify signed message.
* *
* Arguments: - unsigned char *m: pointer to output message (allocated * Arguments: - uint8_t *m: pointer to output message (allocated
* array with smlen bytes), can be equal to sm * array with smlen bytes), can be equal to sm
* - unsigned long long *mlen: pointer to output length of message * - size_t *mlen: pointer to output length of message
* - const unsigned char *sm: pointer to signed message * - const uint8_t *sm: pointer to signed message
* - unsigned long long smlen: length of signed message * - size_t smlen: length of signed message
* - const unsigned char *sk: pointer to bit-packed public key * - const uint8_t *sk: pointer to bit-packed public key
* *
* Returns 0 if signed message could be verified correctly and -1 otherwise * Returns 0 if signed message could be verified correctly and -1 otherwise
**************************************************/ **************************************************/
int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign_open(unsigned char *m, int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign_open(uint8_t *m,
unsigned long long *mlen, size_t *mlen,
const unsigned char *sm, const uint8_t *sm,
unsigned long long smlen, size_t smlen,
const unsigned char *pk) { const uint8_t *pk) {
unsigned long long i; unsigned long long i;
unsigned char rho[SEEDBYTES]; unsigned char rho[SEEDBYTES];
unsigned char mu[CRHBYTES]; unsigned char mu[CRHBYTES];
@ -394,7 +393,7 @@ int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign_open(unsigned char *m,
/* Signature verification failed */ /* Signature verification failed */
badsig: badsig:
*mlen = (unsigned long long) -1; *mlen = 0;
for (i = 0; i < smlen; ++i) { for (i = 0; i < smlen; ++i) {
m[i] = 0; m[i] = 0;
} }

View File

@ -1,28 +1,30 @@
#ifndef SIGN_H #ifndef SIGN_H
#define SIGN_H #define SIGN_H
#include <stddef.h>
#include <stdint.h>
#include "params.h" #include "params.h"
#include "poly.h" #include "poly.h"
#include "polyvec.h" #include "polyvec.h"
void PQCLEAN_DILITHIUMIII_CLEAN_expand_mat(polyvecl mat[K], void PQCLEAN_DILITHIUMIII_CLEAN_expand_mat(polyvecl mat[K],
const unsigned char rho[SEEDBYTES]); const uint8_t rho[SEEDBYTES]);
void PQCLEAN_DILITHIUMIII_CLEAN_challenge(poly *c, const unsigned char mu[CRHBYTES], void PQCLEAN_DILITHIUMIII_CLEAN_challenge(poly *c, const uint8_t mu[CRHBYTES],
const polyveck *w1); const polyveck *w1);
int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign_keypair(unsigned char *pk, int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign_keypair(uint8_t *pk,
unsigned char *sk); uint8_t *sk);
int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign(unsigned char *sm, int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign(uint8_t *sm,
unsigned long long *smlen, size_t *smlen,
const unsigned char *m, const uint8_t *m,
unsigned long long mlen, size_t mlen,
const unsigned char *sk); const uint8_t *sk);
int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign_open(unsigned char *m, int PQCLEAN_DILITHIUMIII_CLEAN_crypto_sign_open(uint8_t *m,
unsigned long long *mlen, size_t *mlen,
const unsigned char *sm, const uint8_t *sm,
unsigned long long smlen, size_t smlen,
const unsigned char *pk); const uint8_t *pk);
#endif #endif

View File

@ -1,28 +1,31 @@
#include "api.h" #include <stdint.h>
#include "randombytes.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "api.h"
#include "randombytes.h"
#define NTESTS 10 #define NTESTS 10
const unsigned char canary[8] = { const uint8_t canary[8] = {
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
}; };
/* allocate a bit more for all keys and messages and /* allocate a bit more for all keys and messages and
* make sure it is not touched by the implementations. * make sure it is not touched by the implementations.
*/ */
static void write_canary(unsigned char *d) { static void write_canary(uint8_t *d) {
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
d[i] = canary[i]; d[i] = canary[i];
} }
} }
static int check_canary(const unsigned char *d) { static int check_canary(const uint8_t *d) {
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
if (d[i] != canary[i]) if (d[i] != canary[i]) {
return -1; return -1;
} }
}
return 0; return 0;
} }
@ -47,11 +50,11 @@ static int test_keys(void) {
* 16 extra bytes for canary * 16 extra bytes for canary
* 1 extra byte for unalignment * 1 extra byte for unalignment
*/ */
unsigned char key_a_aligned[CRYPTO_BYTES + 16 + 1]; uint8_t key_a_aligned[CRYPTO_BYTES + 16 + 1];
unsigned char key_b_aligned[CRYPTO_BYTES + 16 + 1]; uint8_t key_b_aligned[CRYPTO_BYTES + 16 + 1];
unsigned char pk_aligned[CRYPTO_PUBLICKEYBYTES + 16 + 1]; uint8_t pk_aligned[CRYPTO_PUBLICKEYBYTES + 16 + 1];
unsigned char sendb_aligned[CRYPTO_CIPHERTEXTBYTES + 16 + 1]; uint8_t sendb_aligned[CRYPTO_CIPHERTEXTBYTES + 16 + 1];
unsigned char sk_a_aligned[CRYPTO_SECRETKEYBYTES + 16 + 1]; uint8_t sk_a_aligned[CRYPTO_SECRETKEYBYTES + 16 + 1];
/* /*
* Make sure all pointers are odd. * Make sure all pointers are odd.
@ -59,11 +62,11 @@ static int test_keys(void) {
* data alignment. For example this would catch if an implementation * data alignment. For example this would catch if an implementation
* directly uses these pointers to load into vector registers using movdqa. * directly uses these pointers to load into vector registers using movdqa.
*/ */
unsigned char *key_a = (unsigned char *) ((uintptr_t) key_a_aligned|(uintptr_t) 1); uint8_t *key_a = (uint8_t *) ((uintptr_t) key_a_aligned|(uintptr_t) 1);
unsigned char *key_b = (unsigned char *) ((uintptr_t) key_b_aligned|(uintptr_t) 1); uint8_t *key_b = (uint8_t *) ((uintptr_t) key_b_aligned|(uintptr_t) 1);
unsigned char *pk = (unsigned char *) ((uintptr_t) pk_aligned|(uintptr_t) 1); uint8_t *pk = (uint8_t *) ((uintptr_t) pk_aligned|(uintptr_t) 1);
unsigned char *sendb = (unsigned char *) ((uintptr_t) sendb_aligned|(uintptr_t) 1); uint8_t *sendb = (uint8_t *) ((uintptr_t) sendb_aligned|(uintptr_t) 1);
unsigned char *sk_a = (unsigned char *) ((uintptr_t) sk_a_aligned|(uintptr_t) 1); uint8_t *sk_a = (uint8_t *) ((uintptr_t) sk_a_aligned|(uintptr_t) 1);
/* /*
* Write 8 byte canary before and after the actual memory regions. * Write 8 byte canary before and after the actual memory regions.
@ -114,10 +117,10 @@ static int test_keys(void) {
} }
static int test_invalid_sk_a(void) { static int test_invalid_sk_a(void) {
unsigned char sk_a[CRYPTO_SECRETKEYBYTES]; uint8_t sk_a[CRYPTO_SECRETKEYBYTES];
unsigned char key_a[CRYPTO_BYTES], key_b[CRYPTO_BYTES]; uint8_t key_a[CRYPTO_BYTES], key_b[CRYPTO_BYTES];
unsigned char pk[CRYPTO_PUBLICKEYBYTES]; uint8_t pk[CRYPTO_PUBLICKEYBYTES];
unsigned char sendb[CRYPTO_CIPHERTEXTBYTES]; uint8_t sendb[CRYPTO_CIPHERTEXTBYTES];
int i; int i;
int returncode; int returncode;
@ -149,16 +152,16 @@ static int test_invalid_sk_a(void) {
} }
static int test_invalid_ciphertext(void) { static int test_invalid_ciphertext(void) {
unsigned char sk_a[CRYPTO_SECRETKEYBYTES]; uint8_t sk_a[CRYPTO_SECRETKEYBYTES];
unsigned char key_a[CRYPTO_BYTES], key_b[CRYPTO_BYTES]; uint8_t key_a[CRYPTO_BYTES], key_b[CRYPTO_BYTES];
unsigned char pk[CRYPTO_PUBLICKEYBYTES]; uint8_t pk[CRYPTO_PUBLICKEYBYTES];
unsigned char sendb[CRYPTO_CIPHERTEXTBYTES]; uint8_t sendb[CRYPTO_CIPHERTEXTBYTES];
int i; int i;
size_t pos; size_t pos;
int returncode; int returncode;
for (i = 0; i < NTESTS; i++) { for (i = 0; i < NTESTS; i++) {
randombytes((unsigned char *)&pos, sizeof(size_t)); randombytes((uint8_t *)&pos, sizeof(size_t));
// Alice generates a public key // Alice generates a public key
RETURNS_ZERO(crypto_kem_keypair(pk, sk_a)); RETURNS_ZERO(crypto_kem_keypair(pk, sk_a));

View File

@ -1,12 +1,15 @@
#include "api.h" #include <stddef.h>
#include "randombytes.h" #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "api.h"
#include "randombytes.h"
#define NTESTS 100 #define NTESTS 100
static void printbytes(const unsigned char *x, unsigned long long xlen) { static void printbytes(const uint8_t *x, size_t xlen) {
unsigned long long i; size_t i;
for (i = 0; i < xlen; i++) { for (i = 0; i < xlen; i++) {
printf("%02x", x[i]); printf("%02x", x[i]);
} }
@ -23,10 +26,10 @@ static void printbytes(const unsigned char *x, unsigned long long xlen) {
#define crypto_kem_dec NAMESPACE(crypto_kem_dec) #define crypto_kem_dec NAMESPACE(crypto_kem_dec)
int main(void) { int main(void) {
unsigned char key_a[CRYPTO_BYTES], key_b[CRYPTO_BYTES]; uint8_t key_a[CRYPTO_BYTES], key_b[CRYPTO_BYTES];
unsigned char pk[CRYPTO_PUBLICKEYBYTES]; uint8_t pk[CRYPTO_PUBLICKEYBYTES];
unsigned char sendb[CRYPTO_CIPHERTEXTBYTES]; uint8_t sendb[CRYPTO_CIPHERTEXTBYTES];
unsigned char sk_a[CRYPTO_SECRETKEYBYTES]; uint8_t sk_a[CRYPTO_SECRETKEYBYTES];
int i, j; int i, j;
for (i = 0; i < NTESTS; i++) { for (i = 0; i < NTESTS; i++) {
// Key-pair generation // Key-pair generation

View File

@ -1,29 +1,33 @@
#include "api.h" #include <stddef.h>
#include "randombytes.h" #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "api.h"
#include "randombytes.h"
#define NTESTS 15 #define NTESTS 15
#define MLEN 32 #define MLEN 32
const unsigned char canary[8] = { const uint8_t canary[8] = {
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
}; };
/* allocate a bit more for all keys and messages and /* allocate a bit more for all keys and messages and
* make sure it is not touched by the implementations. * make sure it is not touched by the implementations.
*/ */
static void write_canary(unsigned char *d) { static void write_canary(uint8_t *d) {
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
d[i] = canary[i]; d[i] = canary[i];
} }
} }
static int check_canary(const unsigned char *d) { static int check_canary(const uint8_t *d) {
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
if (d[i] != canary[i]) if (d[i] != canary[i]) {
return -1; return -1;
} }
}
return 0; return 0;
} }
@ -48,10 +52,10 @@ static int test_sign(void) {
* 16 extra bytes for canary * 16 extra bytes for canary
* 1 extra byte for unalignment * 1 extra byte for unalignment
*/ */
unsigned char pk_aligned[CRYPTO_PUBLICKEYBYTES + 16 + 1]; uint8_t pk_aligned[CRYPTO_PUBLICKEYBYTES + 16 + 1];
unsigned char sk_aligned[CRYPTO_SECRETKEYBYTES + 16 + 1]; uint8_t sk_aligned[CRYPTO_SECRETKEYBYTES + 16 + 1];
unsigned char sm_aligned[MLEN + CRYPTO_BYTES + 16 + 1]; uint8_t sm_aligned[MLEN + CRYPTO_BYTES + 16 + 1];
unsigned char m_aligned[MLEN + 16 + 1]; uint8_t m_aligned[MLEN + 16 + 1];
/* /*
* Make sure all pointers are odd. * Make sure all pointers are odd.
@ -59,13 +63,13 @@ static int test_sign(void) {
* data alignment. For example this would catch if an implementation * data alignment. For example this would catch if an implementation
* directly uses these pointers to load into vector registers using movdqa. * directly uses these pointers to load into vector registers using movdqa.
*/ */
unsigned char *pk = (unsigned char *) ((uintptr_t) pk_aligned|(uintptr_t) 1); uint8_t *pk = (uint8_t *) ((uintptr_t) pk_aligned|(uintptr_t) 1);
unsigned char *sk = (unsigned char *) ((uintptr_t) sk_aligned|(uintptr_t) 1); uint8_t *sk = (uint8_t *) ((uintptr_t) sk_aligned|(uintptr_t) 1);
unsigned char *sm = (unsigned char *) ((uintptr_t) sm_aligned|(uintptr_t) 1); uint8_t *sm = (uint8_t *) ((uintptr_t) sm_aligned|(uintptr_t) 1);
unsigned char *m = (unsigned char *) ((uintptr_t) m_aligned|(uintptr_t) 1); uint8_t *m = (uint8_t *) ((uintptr_t) m_aligned|(uintptr_t) 1);
unsigned long long mlen; size_t mlen;
unsigned long long smlen; size_t smlen;
int returncode; int returncode;
int i; int i;
@ -114,14 +118,14 @@ static int test_sign(void) {
} }
static int test_wrong_pk(void) { static int test_wrong_pk(void) {
unsigned char pk[CRYPTO_PUBLICKEYBYTES]; uint8_t pk[CRYPTO_PUBLICKEYBYTES];
unsigned char pk2[CRYPTO_PUBLICKEYBYTES]; uint8_t pk2[CRYPTO_PUBLICKEYBYTES];
unsigned char sk[CRYPTO_SECRETKEYBYTES]; uint8_t sk[CRYPTO_SECRETKEYBYTES];
unsigned char sm[MLEN + CRYPTO_BYTES]; uint8_t sm[MLEN + CRYPTO_BYTES];
unsigned char m[MLEN]; uint8_t m[MLEN];
unsigned long long mlen; size_t mlen;
unsigned long long smlen; size_t smlen;
int returncode; int returncode;

View File

@ -1,13 +1,16 @@
#include "api.h" #include <stddef.h>
#include "randombytes.h" #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "api.h"
#include "randombytes.h"
#define NTESTS 100 #define NTESTS 100
#define MAXMLEN 2048 #define MAXMLEN 2048
static void printbytes(const unsigned char *x, unsigned long long xlen) { static void printbytes(const uint8_t *x, size_t xlen) {
unsigned long long i; size_t i;
for (i = 0; i < xlen; i++) { for (i = 0; i < xlen; i++) {
printf("%02x", x[i]); printf("%02x", x[i]);
} }
@ -24,16 +27,16 @@ static void printbytes(const unsigned char *x, unsigned long long xlen) {
#define crypto_sign_open NAMESPACE(crypto_sign_open) #define crypto_sign_open NAMESPACE(crypto_sign_open)
int main(void) { int main(void) {
unsigned char sk[CRYPTO_SECRETKEYBYTES]; uint8_t sk[CRYPTO_SECRETKEYBYTES];
unsigned char pk[CRYPTO_PUBLICKEYBYTES]; uint8_t pk[CRYPTO_PUBLICKEYBYTES];
unsigned char mi[MAXMLEN]; uint8_t mi[MAXMLEN];
unsigned char sm[MAXMLEN + CRYPTO_BYTES]; uint8_t sm[MAXMLEN + CRYPTO_BYTES];
unsigned long long smlen; size_t smlen;
unsigned long long mlen; size_t mlen;
int r; int r;
unsigned long long i, k; size_t i, k;
for (i = 0; i < MAXMLEN; i = (i == 0) ? i + 1 : i << 1) { for (i = 0; i < MAXMLEN; i = (i == 0) ? i + 1 : i << 1) {
randombytes(mi, i); randombytes(mi, i);