diff options
author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2010-07-06 13:00:59 +0200 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2010-07-06 13:00:59 +0200 |
commit | d12ecf68276ab0e57ea578d763f23b2143e57ed8 (patch) | |
tree | 9ddf4b21c9a918e4d97679a259ea6dcd9de742c7 /libtomcrypt/math/rand_prime.c | |
parent | b6d0f4da862e17344fca35db8d9ed0dce39e757f (diff) | |
download | cryptodev-linux-d12ecf68276ab0e57ea578d763f23b2143e57ed8.tar.gz cryptodev-linux-d12ecf68276ab0e57ea578d763f23b2143e57ed8.tar.xz cryptodev-linux-d12ecf68276ab0e57ea578d763f23b2143e57ed8.zip |
Added libtomcrypt
Diffstat (limited to 'libtomcrypt/math/rand_prime.c')
-rw-r--r-- | libtomcrypt/math/rand_prime.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/libtomcrypt/math/rand_prime.c b/libtomcrypt/math/rand_prime.c new file mode 100644 index 0000000..43c7603 --- /dev/null +++ b/libtomcrypt/math/rand_prime.c @@ -0,0 +1,79 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file rand_prime.c + Generate a random prime, Tom St Denis +*/ + +#define USE_BBS 1 + +int rand_prime(void *N, long len) +{ + int err, res, type; + unsigned char *buf; + + LTC_ARGCHK(N != NULL); + + /* get type */ + if (len < 0) { + type = USE_BBS; + len = -len; + } else { + type = 0; + } + + /* allow sizes between 2 and 512 bytes for a prime size */ + if (len < 2 || len > 512) { + return CRYPT_INVALID_PRIME_SIZE; + } + + /* allocate buffer to work with */ + buf = XCALLOC(1, len); + if (buf == NULL) { + return CRYPT_MEM; + } + + do { + /* generate value */ + get_random_bytes( buf, len); + + /* munge bits */ + buf[0] |= 0x80 | 0x40; + buf[len-1] |= 0x01 | ((type & USE_BBS) ? 0x02 : 0x00); + + /* load value */ + if ((err = mp_read_unsigned_bin(N, buf, len)) != CRYPT_OK) { + XFREE(buf); + return err; + } + + /* test */ + if ((err = mp_prime_is_prime(N, 8, &res)) != CRYPT_OK) { + XFREE(buf); + return err; + } + } while (res == LTC_MP_NO); + +#ifdef LTC_CLEAN_STACK + zeromem(buf, len); +#endif + + XFREE(buf); + return CRYPT_OK; +} + + + +/* $Source: /cvs/libtom/libtomcrypt/src/math/rand_prime.c,v $ */ +/* $Revision: 1.7 $ */ +/* $Date: 2006/12/28 01:27:23 $ */ |