void test4(const char *name) { unsigned char *a, *b, *c; long long i; alloc_setlimit(1024000); for (i = 0; i < LOOPS; ++i) { a = alloc(10); if (!a) err(name, "unable to allocate memory"); fastrandombytes(a, 10); } b = alloc(10); if (!b) err(name, "unable to allocate memory"); for (i = 0; i < 1000; ++i) { a = alloc(10); if (!a) err(name, "unable to allocate memory"); fastrandombytes(a, 10); } c = alloc(10); if (!c) err(name, "unable to allocate memory"); for (i = 0; i < 1000; ++i) { a = alloc(10); if (!a) err(name, "unable to allocate memory"); fastrandombytes(a, 10); } alloc_free(c); alloc_free(b); alloc_freeall(); if (alloc_getallocated() != 0) err(name, "alloc_freeall doesn't work"); }
unsigned generate_random_bit() { unsigned char rnd[1]; _Pragma(STRINGIFY(omp critical)) { fastrandombytes(rnd, 1); } return rnd[0] & 1; }
void sample_y(double mat_y[PARAM_N]) { int32_t val; unsigned char buf[3*PARAM_N+68]; int pos=0, i=0; fastrandombytes(buf,3*PARAM_N+68); do { if(pos == 3*PARAM_N+66) { fastrandombytes(buf,3*PARAM_N+68); pos = 0; } val = (*(int32_t *)(buf+pos)) & 0x7fffff; if(val < 0x7fffff) mat_y[i++] = val-PARAM_B; pos+=3; } while(i< PARAM_N); }
void test6(const char *name) { struct astruct *s; long long i; s = alloc(sizeof(struct astruct)); if (!s) err(name, "unable to allocate memory"); for (i = 0; i < LOOPS; ++i) { s->a[i]= alloc(10); if (!s->a[i]) err(name, "unable to allocate memory"); } fastrandombytes((unsigned char *)s, sizeof(struct astruct)); alloc_freeall(); if (alloc_getallocated() != 0) err(name, "alloc_freeall doesn't work"); }
void test5(const char *name) { unsigned char *x[LOOPS]; long long i, j; for (i = 0; i < LOOPS; ++i) { x[i] = alloc(10); if (!x[i]) err(name, "unable to allocate memory"); fastrandombytes(x[i], 10); } for (i = 0; i < LOOPS; ++i) { for (j = 0; j < LOOPS; ++j) { alloc_free(x[j]); } } }
void test2(const char *name) { long long i, j; unsigned char *x; long long l[4] = {1, 10, 100, 1000}; alloc_setlimit(10240); for (i = 0; i < LOOPS; ++i) { for(j = 0; j < 4; ++j) { x = alloc(l[j] + i); if (!x) err(name, "unable to allocate memory"); fastrandombytes(x, l[j] + i); alloc_free(x); } } if (alloc_getallocated() != 0) err(name, "alloc_free doesn't work"); }
void generate_random(mpz_class &z, unsigned bits) { if (bits == 0) { z = 0; return; } size_t nbchar = (bits) / (sizeof(unsigned char) * CHAR_BIT) + 1; unsigned char *rnd = new unsigned char[nbchar]; mpz_class mask(2); mask <<= bits; mask -= 1; _Pragma(STRINGIFY(omp critical)) { fastrandombytes(rnd, nbchar); } mpz_import (z.get_mpz_t(), nbchar, 1, sizeof(unsigned char), 0, 0, rnd); delete[] rnd; z &= mask; }
unsigned generate_random(unsigned n) { if (n == 0) { return 0; } size_t nbchar = sizeof(unsigned) / sizeof(unsigned char); unsigned char *rnd = new unsigned char[nbchar]; unsigned mask = 1; for (unsigned i = n; i > 0; i >>= 1) { mask <<= 1; } mask -= 1; unsigned result; do { _Pragma(STRINGIFY(omp critical)) { fastrandombytes(rnd, nbchar); } result = 0; for (unsigned i = 0; i < nbchar; i++) { result <<= CHAR_BIT; result &= rnd[i]; } result &= mask; } while (result >= n); delete[] rnd; return result; }
void generate_random(mpz_class &z, const mpz_class &n) { if (n == 0) { z = 0; return; } size_t nbchar = mpz_sizeinbase(n.get_mpz_t(), 2) / (sizeof(unsigned char) * CHAR_BIT) + 1; unsigned char *rnd = new unsigned char[nbchar]; mpz_class mask(2); mask <<= mpz_sizeinbase(n.get_mpz_t(), 2); mask -= 1; do { _Pragma(STRINGIFY(omp critical)) { fastrandombytes(rnd, nbchar); } mpz_import (z.get_mpz_t(), nbchar, 1, sizeof(unsigned char), 0, 0, rnd); z &= mask; } while (z >= n); delete[] rnd; }
static int testPack(PQ_PARAM_SET_ID id) { int i; int T; int rc; PQ_PARAM_SET *P; if(!(P = pq_get_param_set_by_id(id))) { return -1; } unsigned char *scratch; uint16_t *iF; uint16_t *oF; uint16_t *ig; uint16_t *og; int64_t *iginv; int64_t *oginv; int64_t *ih; int64_t *oh; int64_t *isig; int64_t *osig; unsigned char *priv_blob; unsigned char *pub_blob; unsigned char *sig_blob; size_t prod = 2*(P->d1 + P->d2 + P->d3)*sizeof(uint16_t); size_t full = P->N*sizeof(int64_t); size_t priv_blob_len = PRIVKEY_PACKED_BYTES(P); size_t pub_blob_len = PUBKEY_PACKED_BYTES(P); size_t sig_len = SIGNATURE_BYTES(P); size_t offset; scratch = malloc(4*prod + 6*full + priv_blob_len + pub_blob_len + sig_len); offset = 0; iF = (uint16_t*)(scratch + offset); offset += prod; oF = (uint16_t*)(scratch + offset); offset += prod; ig = (uint16_t*)(scratch + offset); offset += prod; og = (uint16_t*)(scratch + offset); offset += prod; iginv = (int64_t*)(scratch + offset); offset += full; oginv = (int64_t*)(scratch + offset); offset += full; isig = (int64_t*)(scratch + offset); offset += full; osig = (int64_t*)(scratch + offset); offset += full; ih = (int64_t*)(scratch + offset); offset += full; oh = (int64_t*)(scratch + offset); offset += full; priv_blob = (unsigned char*)(scratch + offset); offset += priv_blob_len; pub_blob = (unsigned char*)(scratch + offset); offset += pub_blob_len; sig_blob = (unsigned char*)(scratch + offset); offset += sig_len; for(T=0; T<TIMES; T++) { fastrandombytes(scratch, 4*prod + 6*full + priv_blob_len + pub_blob_len + sig_len); for(i = 0; i < prod; i++) { iF[i] = iF[i] % P->N; ig[i] = ig[i] % P->N; } for(i=0; i < P->N; i++) { iginv[i] = cmod(iginv[i], P->p); ih[i] = cmod(ih[i], P->q); isig[i] = isig[i] % P->q; isig[i] = (isig[i] + P->q) % P->q; isig[i] -= isig[i] % P->p; isig[i] /= P->p; } rc = pack_private_key(P, iF, ig, iginv, priv_blob_len, priv_blob); if(PQNTRU_ERROR == rc) { printf("Private key pack error\n"); return -1; } rc = unpack_private_key(P, oF, og, oginv, priv_blob_len, priv_blob); if(PQNTRU_ERROR == rc) { printf("Private key unpack error\n"); return -1; } rc = pack_public_key(P, ih, pub_blob_len, pub_blob); if(PQNTRU_ERROR == rc) { printf("Public key pack error\n"); return -1; } rc = unpack_public_key(P, oh, pub_blob_len, pub_blob); if(PQNTRU_ERROR == rc) { printf("Public key unpack error\n"); return -1; } rc = pack_signature(P, isig, sig_len, sig_blob); if(PQNTRU_ERROR == rc) { printf("Signature pack error\n"); return -1; } rc = unpack_signature(P, osig, sig_len, sig_blob); if(PQNTRU_ERROR == rc) { printf("Signature unpack error\n"); return -1; } for(i=0; i<2*(P->d1 + P->d2 + P->d3); i++) { if(iF[i] != oF[i] || ig[i] != og[i]) { printf("product form keys not equal\n"); break; } } for(i=0; i<P->N; i++) { oh[i] = cmod(oh[i], P->q); oginv[i] = cmod(oginv[i], P->p); if(ih[i] != oh[i] || iginv[i] != oginv[i]) { printf("%d %ld %ld %ld %ld\n", i, ih[i], oh[i], iginv[i], oginv[i]); printf("public key or iginv not equal\n"); return -1; } if(isig[i] != osig[i]) { printf("%d %ld %ld\n", i, isig[i], osig[i]); printf("signatures not equal\n"); return -1; } } } }
static int testSet(PQ_PARAM_SET_ID id) { uint16_t i; PQ_PARAM_SET *P; size_t privkey_blob_len; size_t pubkey_blob_len; unsigned char *privkey_blob; unsigned char *pubkey_blob; unsigned char *sigs; uint16_t msg_len = 256; unsigned char *msg; int result = 0; if(!(P = pq_get_param_set_by_id(id))) { return -1; } fprintf(stderr, "Testing parameter set %s", P->name); fflush(stderr); pq_gen_key(P, &privkey_blob_len, NULL, &pubkey_blob_len, NULL); privkey_blob = malloc(TIMES * privkey_blob_len); pubkey_blob = malloc(TIMES * pubkey_blob_len); msg = malloc(TIMES * msg_len * sizeof(int64_t)); memset(msg, 0, TIMES*msg_len*sizeof(int64_t)); for(i=0; i<TIMES; i++) { if(PQNTRU_ERROR == pq_gen_key(P, &privkey_blob_len, privkey_blob + (i*privkey_blob_len), &pubkey_blob_len, pubkey_blob + (i*pubkey_blob_len))) { result = -1; fprintf(stderr, "\t fail in keygen\n"); goto exit_kg; } } size_t packed_sig_len; pq_sign(&packed_sig_len, NULL, privkey_blob_len, privkey_blob, pubkey_blob_len, pubkey_blob, 0, NULL); sigs = malloc(TIMES * packed_sig_len); for(i=0; i<TIMES; i++) { fastrandombytes(msg+(i*msg_len), msg_len); if(PQNTRU_ERROR == pq_sign( &packed_sig_len, sigs + (i*packed_sig_len), privkey_blob_len, privkey_blob + (i*privkey_blob_len), pubkey_blob_len, pubkey_blob + (i*pubkey_blob_len), msg_len, msg + (i*msg_len))) { result = -1; fprintf(stderr, "\t fail in sign\n"); goto exit; } } for(i=0; i<TIMES; i++) { if(PQNTRU_ERROR == pq_verify( packed_sig_len, sigs + (i*packed_sig_len), pubkey_blob_len, pubkey_blob + (i*pubkey_blob_len), msg_len, msg + (i*msg_len))) { result = -1; fprintf(stderr, "\t fail in verify\n"); goto exit; } } fprintf(stderr, "\t good\n"); exit: free(sigs); exit_kg: free(msg); free(privkey_blob); free(pubkey_blob); return result; }