int pgp_s2k_fill(PGP_S2K *s2k, int mode, int digest_algo, int count) { int res = 0; uint8 tmp; s2k->mode = mode; s2k->digest_algo = digest_algo; switch (s2k->mode) { case PGP_S2K_SIMPLE: break; case PGP_S2K_SALTED: res = px_get_random_bytes(s2k->salt, PGP_S2K_SALT); break; case PGP_S2K_ISALTED: res = px_get_random_bytes(s2k->salt, PGP_S2K_SALT); if (res < 0) break; res = px_get_random_bytes(&tmp, 1); if (res < 0) break; s2k->iter = decide_s2k_iter(tmp, count); break; default: res = PXE_PGP_BAD_S2K_MODE; } return res; }
int main (int argc, char** argv) { /* uint8 data[1024]; unsigned len = 18; for (int i=0;i<len;i++) { printf("%i\n",(int)data[i]); } return(0); } */ PX_MD *md; uint8 rnd[16]; char data[1024]; int i; strcpy(data,"This is a test"); if (px_get_random_bytes(rnd, 16) < 0) { printf("No random bytes\n"); exit(1); } if (px_find_digest("sha1", &md) < 0) { printf("No sha\n"); exit(1); } for (i=0;i<16;i++) { printf("%i ",rnd[i]); } printf("\n"); /* * Try to make the feeding unpredictable. * * Prefer data over keys, as it's rather likely that key is same in * several calls. */ if (data && rnd[0] >= 32) { add_block_entropy(md, data); } if (data && rnd[1] >= 160) { add_block_entropy(md, data); } if (data && rnd[2] >= 160) add_block_entropy(md, data); px_md_free(md); memset(rnd, 0, sizeof(rnd)); }
static void system_reseed(void) { uint8 buf[1024]; int n; time_t t; int skip = 1; t = time(NULL); if (seed_time == 0) skip = 0; else if ((t - seed_time) < SYSTEM_RESEED_MIN) skip = 1; else if ((t - seed_time) > SYSTEM_RESEED_MAX) skip = 0; else if (check_time == 0 || (t - check_time) > SYSTEM_RESEED_CHECK_TIME) { check_time = t; /* roll dice */ px_get_random_bytes(buf, 1); skip = buf[0] >= SYSTEM_RESEED_CHANCE; } /* clear 1 byte */ memset(buf, 0, sizeof(buf)); if (skip) return; n = px_acquire_system_randomness(buf); if (n > 0) fortuna_add_entropy(buf, n); seed_time = t; memset(buf, 0, sizeof(buf)); }
static int mp_px_rand(uint32 bits, mpz_t *res) { int err; unsigned bytes = (bits + 7) / 8; int last_bits = bits & 7; uint8 *buf; buf = px_alloc(bytes); err = px_get_random_bytes(buf, bytes); if (err < 0) { px_free(buf); return err; } /* clear unnecessary bits and set last bit to one */ if (last_bits) { buf[0] >>= 8 - last_bits; buf[0] |= 1 << (last_bits - 1); } else
/* * Mix user data into RNG. It is for user own interests to have * RNG state shuffled. */ static void add_entropy(text *data1, text *data2, text *data3) { PX_MD *md; uint8 rnd[3]; if (!data1 && !data2 && !data3) return; if (px_get_random_bytes(rnd, 3) < 0) return; if (px_find_digest("sha1", &md) < 0) return; /* * Try to make the feeding unpredictable. * * Prefer data over keys, as it's rather likely that key is same in * several calls. */ /* chance: 7/8 */ if (data1 && rnd[0] >= 32) add_block_entropy(md, data1); /* chance: 5/8 */ if (data2 && rnd[1] >= 160) add_block_entropy(md, data2); /* chance: 5/8 */ if (data3 && rnd[2] >= 160) add_block_entropy(md, data3); px_md_free(md); memset(rnd, 0, sizeof(rnd)); }
Datum pg_random_bytes(PG_FUNCTION_ARGS) { int err; int len = PG_GETARG_INT32(0); bytea *res; if (len < 1 || len > 1024) ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), errmsg("Length not in range"))); res = palloc(VARHDRSZ + len); SET_VARSIZE(res, VARHDRSZ + len); /* generate result */ err = px_get_random_bytes((uint8 *) VARDATA(res), len); if (err < 0) ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), errmsg("Random generator error: %s", px_strerror(err)))); PG_RETURN_BYTEA_P(res); }
/* * Use always strong randomness. */ int px_get_pseudo_random_bytes(uint8 *dst, unsigned count) { return px_get_random_bytes(dst, count); }