summaryrefslogtreecommitdiffstats
path: root/libtomcrypt/math/rand_prime.c
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2010-09-06 17:20:33 +0200
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2010-09-06 17:26:58 +0200
commite6177630198eb1eea2def0374fae1196da0e40ec (patch)
tree704951804609999fb6ef7a956b04921b9f84c320 /libtomcrypt/math/rand_prime.c
parent943f9ab50c110133a5cd1118b5b19cb09301168f (diff)
downloadcryptodev-linux-e6177630198eb1eea2def0374fae1196da0e40ec.tar.gz
cryptodev-linux-e6177630198eb1eea2def0374fae1196da0e40ec.tar.xz
cryptodev-linux-e6177630198eb1eea2def0374fae1196da0e40ec.zip
Run Lindent on libtom(*)
Diffstat (limited to 'libtomcrypt/math/rand_prime.c')
-rw-r--r--libtomcrypt/math/rand_prime.c101
1 files changed, 49 insertions, 52 deletions
diff --git a/libtomcrypt/math/rand_prime.c b/libtomcrypt/math/rand_prime.c
index 5eff3a1..fb17bf5 100644
--- a/libtomcrypt/math/rand_prime.c
+++ b/libtomcrypt/math/rand_prime.c
@@ -10,70 +10,67 @@
*/
#include "tomcrypt.h"
-
/**
@file rand_prime.c
Generate a random prime, Tom St Denis
-*/
+*/
#define USE_BBS 1
-int rand_prime(mp_int *N, long len)
+int rand_prime(mp_int * 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);
+ 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);
+ zeromem(buf, len);
#endif
- XFREE(buf);
- return CRYPT_OK;
+ XFREE(buf);
+ return CRYPT_OK;
}
-
-
/* $Source: /cvs/libtom/libtomcrypt/src/math/rand_prime.c,v $ */
/* $Revision: 1.7 $ */