2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2019-10-19 14:24:39 +00:00
|
|
|
#ifdef MP_PRIME_RAND_C
|
2019-04-07 13:29:11 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
2004-04-11 20:46:22 +00:00
|
|
|
|
|
|
|
/* makes a truly random prime of a given size (bits),
|
|
|
|
*
|
|
|
|
* Flags are as follows:
|
2017-08-30 03:51:11 +00:00
|
|
|
*
|
2019-05-07 07:45:38 +00:00
|
|
|
* MP_PRIME_BBS - make prime congruent to 3 mod 4
|
|
|
|
* MP_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies MP_PRIME_BBS)
|
|
|
|
* MP_PRIME_2MSB_ON - make the 2nd highest bit one
|
2004-04-11 20:46:22 +00:00
|
|
|
*
|
|
|
|
* You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
|
|
|
|
* have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
|
|
|
|
* so it can be NULL
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This is possibly the mother of all prime generation functions, muahahahahaha! */
|
2019-10-24 16:14:18 +00:00
|
|
|
mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)
|
2004-04-11 20:46:22 +00:00
|
|
|
{
|
|
|
|
unsigned char *tmp, maskAND, maskOR_msb, maskOR_lsb;
|
2019-05-12 22:22:18 +00:00
|
|
|
int bsize, maskOR_msb_offset;
|
2019-10-24 20:02:29 +00:00
|
|
|
bool res;
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err err;
|
2004-04-11 20:46:22 +00:00
|
|
|
|
|
|
|
/* sanity check the input */
|
2015-11-13 09:28:23 +00:00
|
|
|
if ((size <= 1) || (t <= 0)) {
|
2004-04-11 20:46:22 +00:00
|
|
|
return MP_VAL;
|
|
|
|
}
|
|
|
|
|
2019-05-07 07:45:38 +00:00
|
|
|
/* MP_PRIME_SAFE implies MP_PRIME_BBS */
|
|
|
|
if ((flags & MP_PRIME_SAFE) != 0) {
|
|
|
|
flags |= MP_PRIME_BBS;
|
2004-04-11 20:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* calc the byte size */
|
2004-12-23 02:40:37 +00:00
|
|
|
bsize = (size>>3) + ((size&7)?1:0);
|
2004-04-11 20:46:22 +00:00
|
|
|
|
|
|
|
/* we need a buffer of bsize bytes */
|
2019-04-09 09:08:26 +00:00
|
|
|
tmp = (unsigned char *) MP_MALLOC((size_t)bsize);
|
2004-04-11 20:46:22 +00:00
|
|
|
if (tmp == NULL) {
|
|
|
|
return MP_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calc the maskAND value for the MSbyte*/
|
2019-05-23 15:50:58 +00:00
|
|
|
maskAND = ((size&7) == 0) ? 0xFFu : (unsigned char)(0xFFu >> (8 - (size & 7)));
|
2004-04-11 20:46:22 +00:00
|
|
|
|
|
|
|
/* calc the maskOR_msb */
|
|
|
|
maskOR_msb = 0;
|
2005-02-12 08:40:15 +00:00
|
|
|
maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
|
2019-05-07 07:45:38 +00:00
|
|
|
if ((flags & MP_PRIME_2MSB_ON) != 0) {
|
2019-04-04 21:06:00 +00:00
|
|
|
maskOR_msb |= (unsigned char)(0x80 >> ((9 - size) & 7));
|
2017-08-30 03:51:11 +00:00
|
|
|
}
|
2004-04-11 20:46:22 +00:00
|
|
|
|
|
|
|
/* get the maskOR_lsb */
|
2019-05-23 15:50:58 +00:00
|
|
|
maskOR_lsb = 1u;
|
2019-05-07 07:45:38 +00:00
|
|
|
if ((flags & MP_PRIME_BBS) != 0) {
|
2019-05-23 15:50:58 +00:00
|
|
|
maskOR_lsb |= 3u;
|
2004-04-11 20:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* read the bytes */
|
2019-10-24 16:14:18 +00:00
|
|
|
if ((err = s_mp_rand_source(tmp, (size_t)bsize)) != MP_OKAY) {
|
|
|
|
goto LBL_ERR;
|
2004-04-11 20:46:22 +00:00
|
|
|
}
|
2017-08-30 03:51:11 +00:00
|
|
|
|
2004-04-11 20:46:22 +00:00
|
|
|
/* work over the MSbyte */
|
|
|
|
tmp[0] &= maskAND;
|
2019-04-04 21:06:00 +00:00
|
|
|
tmp[0] |= (unsigned char)(1 << ((size - 1) & 7));
|
2004-04-11 20:46:22 +00:00
|
|
|
|
|
|
|
/* mix in the maskORs */
|
|
|
|
tmp[maskOR_msb_offset] |= maskOR_msb;
|
|
|
|
tmp[bsize-1] |= maskOR_lsb;
|
|
|
|
|
|
|
|
/* read it in */
|
2019-09-24 22:29:19 +00:00
|
|
|
/* TODO: casting only for now until all lengths have been changed to the type "size_t"*/
|
|
|
|
if ((err = mp_from_ubin(a, tmp, (size_t)bsize)) != MP_OKAY) {
|
2019-10-24 16:14:18 +00:00
|
|
|
goto LBL_ERR;
|
2017-08-30 17:19:29 +00:00
|
|
|
}
|
2004-04-11 20:46:22 +00:00
|
|
|
|
|
|
|
/* is it prime? */
|
2017-10-04 16:57:25 +00:00
|
|
|
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
|
2019-10-24 16:14:18 +00:00
|
|
|
goto LBL_ERR;
|
2017-08-30 17:19:29 +00:00
|
|
|
}
|
2019-10-24 15:52:03 +00:00
|
|
|
if (!res) {
|
2004-10-29 22:07:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-04-11 20:46:22 +00:00
|
|
|
|
2019-05-07 07:45:38 +00:00
|
|
|
if ((flags & MP_PRIME_SAFE) != 0) {
|
2004-04-11 20:46:22 +00:00
|
|
|
/* see if (a-1)/2 is prime */
|
2017-10-04 16:57:25 +00:00
|
|
|
if ((err = mp_sub_d(a, 1uL, a)) != MP_OKAY) {
|
2019-10-24 16:14:18 +00:00
|
|
|
goto LBL_ERR;
|
2017-08-30 17:19:29 +00:00
|
|
|
}
|
2017-10-04 16:57:25 +00:00
|
|
|
if ((err = mp_div_2(a, a)) != MP_OKAY) {
|
2019-10-24 16:14:18 +00:00
|
|
|
goto LBL_ERR;
|
2017-08-30 17:19:29 +00:00
|
|
|
}
|
2017-08-30 03:51:11 +00:00
|
|
|
|
2004-04-11 20:46:22 +00:00
|
|
|
/* is it prime? */
|
2017-10-04 16:57:25 +00:00
|
|
|
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
|
2019-10-24 16:14:18 +00:00
|
|
|
goto LBL_ERR;
|
2017-08-30 17:19:29 +00:00
|
|
|
}
|
2004-04-11 20:46:22 +00:00
|
|
|
}
|
2019-10-24 15:52:03 +00:00
|
|
|
} while (!res);
|
2004-04-11 20:46:22 +00:00
|
|
|
|
2019-05-07 07:45:38 +00:00
|
|
|
if ((flags & MP_PRIME_SAFE) != 0) {
|
2004-04-11 20:46:22 +00:00
|
|
|
/* restore a to the original value */
|
2017-10-04 16:57:25 +00:00
|
|
|
if ((err = mp_mul_2(a, a)) != MP_OKAY) {
|
2019-10-24 16:14:18 +00:00
|
|
|
goto LBL_ERR;
|
2017-08-30 17:19:29 +00:00
|
|
|
}
|
2017-10-04 16:57:25 +00:00
|
|
|
if ((err = mp_add_d(a, 1uL, a)) != MP_OKAY) {
|
2019-10-24 16:14:18 +00:00
|
|
|
goto LBL_ERR;
|
2017-08-30 17:19:29 +00:00
|
|
|
}
|
2004-04-11 20:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = MP_OKAY;
|
2019-10-24 16:14:18 +00:00
|
|
|
LBL_ERR:
|
2019-05-11 08:22:20 +00:00
|
|
|
MP_FREE_BUFFER(tmp, (size_t)bsize);
|
2004-04-11 20:46:22 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|