Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5602f64

Browse files
authoredSep 19, 2024··
Merge pull request #39 from levitte/bugfix_gen_prime
Bugfixes to fp_prime_random_ex [reboot]
2 parents ab5814d + 060d65d commit 5602f64

File tree

2 files changed

+38
-37
lines changed

2 files changed

+38
-37
lines changed
 

‎src/headers/tfm.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@
318318

319319
typedef ulong64 fp_digit;
320320
#define SIZEOF_FP_DIGIT 8
321+
#define DIGIT_SHIFT 6
321322
typedef unsigned long fp_word __attribute__ ((mode(TI)));
322-
323323
#else
324324

325325
/* this is to make porting into LibTomCrypt easier :-) */
@@ -335,6 +335,7 @@
335335

336336
typedef unsigned int fp_digit;
337337
#define SIZEOF_FP_DIGIT 4
338+
#define DIGIT_SHIFT 5
338339
typedef ulong64 fp_word;
339340
#endif /* FP_64BIT */
340341

‎src/numtheory/fp_prime_random_ex.c

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
/* SPDX-License-Identifier: Unlicense */
33
#include <tfm_private.h>
44

5+
#define fp_on_bitnum(a, bitnum) \
6+
a->dp[(bitnum) >> DIGIT_SHIFT] |= (fp_digit)1 << ((bitnum) & (DIGIT_BIT-1))
7+
8+
#define fp_off_bitnum(a, bitnum) \
9+
a->dp[(bitnum) >> DIGIT_SHIFT] &= ~((fp_digit)1 << ((bitnum) & (DIGIT_BIT-1)))
10+
511
/* This is possibly the mother of all prime generation functions, muahahahahaha! */
612
int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat)
713
{
8-
unsigned char *tmp, maskAND, maskOR_msb, maskOR_lsb;
9-
int res, err, bsize, maskOR_msb_offset;
14+
fp_digit maskAND_msb, maskOR_lsb;
15+
int res, bsize, dsize;
16+
unsigned char buf[FP_SIZE * sizeof(fp_digit)];
1017

1118
/* sanity check the input */
1219
if (size <= 1 || cb == NULL || t <= 0 || t > FP_PRIME_SIZE) {
@@ -18,26 +25,13 @@ int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback
1825
flags |= TFM_PRIME_BBS;
1926
}
2027

21-
/* calc the byte size */
22-
bsize = (size>>3)+(size&7?1:0);
28+
/* calc the size in fp_digit */
29+
dsize = (size + DIGIT_BIT - 1) >> DIGIT_SHIFT;
30+
/* calc the size in bytes */
31+
bsize = (size + 7) >> 3;
2332

24-
/* we need a buffer of bsize bytes */
25-
tmp = malloc(bsize);
26-
if (tmp == NULL) {
27-
return FP_MEM;
28-
}
29-
30-
/* calc the maskAND value for the MSbyte*/
31-
maskAND = 0xFF >> ((8 - (size & 7)) & 7);
32-
33-
/* calc the maskOR_msb */
34-
maskOR_msb = 0;
35-
maskOR_msb_offset = (size - 2) >> 3;
36-
if (flags & TFM_PRIME_2MSB_ON) {
37-
maskOR_msb |= 1 << ((size - 2) & 7);
38-
} else if (flags & TFM_PRIME_2MSB_OFF) {
39-
maskAND &= ~(1 << ((size - 2) & 7));
40-
}
33+
/* calc the maskAND value for the MSbyte */
34+
maskAND_msb = FP_MASK >> ((DIGIT_BIT - size) & (DIGIT_BIT-1));
4135

4236
/* get the maskOR_lsb */
4337
maskOR_lsb = 1;
@@ -47,21 +41,30 @@ int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback
4741

4842
do {
4943
/* read the bytes */
50-
if (cb(tmp, bsize, dat) != bsize) {
51-
err = FP_VAL;
52-
goto error;
44+
if (cb(buf, bsize, dat) != bsize) {
45+
return FP_VAL;
5346
}
47+
fp_read_unsigned_bin(a, buf, bsize);
48+
49+
/* make sure the MSbyte has the required number of bits */
50+
a->dp[dsize-1] &= maskAND_msb;
5451

55-
/* work over the MSbyte */
56-
tmp[0] &= maskAND;
57-
tmp[0] |= 1 << ((size - 1) & 7);
52+
/* Force a->used as well, it could be smaller if the highest bits were
53+
generated as 0 by the callback. */
54+
a->used = dsize;
5855

59-
/* mix in the maskORs */
60-
tmp[maskOR_msb_offset] |= maskOR_msb;
61-
tmp[bsize-1] |= maskOR_lsb;
56+
/* modify the LSbyte as requested */
57+
a->dp[0] |= maskOR_lsb;
6258

63-
/* read it in */
64-
fp_read_unsigned_bin(a, tmp, bsize);
59+
/* turn on the MSbit to force the requested magnitude */
60+
fp_on_bitnum(a, size-1);
61+
62+
/* modify the 2nd MSBit */
63+
if (flags & TFM_PRIME_2MSB_ON) {
64+
fp_on_bitnum(a, size-2);
65+
} else if (flags & TFM_PRIME_2MSB_OFF) {
66+
fp_off_bitnum(a, size-2);
67+
}
6568

6669
/* is it prime? */
6770
res = fp_isprime_ex(a, t);
@@ -83,8 +86,5 @@ int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback
8386
fp_add_d(a, 1, a);
8487
}
8588

86-
err = FP_OKAY;
87-
error:
88-
free(tmp);
89-
return err;
89+
return FP_OKAY;
9090
}

0 commit comments

Comments
 (0)
Please sign in to comment.