Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions goodkey/good_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,12 @@ func checkSmallPrimes(i *big.Int) bool {
// independently from a uniform random distribution. Basically, if we can factor
// the key this easily, so can anyone else.
func checkPrimeFactorsTooClose(n *big.Int, rounds int) error {
// Trivial case: n = p^2
sqrt := new(big.Int).Sqrt(n)
if new(big.Int).Mul(sqrt, sqrt).Cmp(n) == 0 {
return fmt.Errorf("public modulus n = pq factored in 1 rounds into p: %s and q: %s", sqrt, sqrt)
}

// Pre-allocate some big numbers that we'll use a lot down below.
one := big.NewInt(1)
bb := new(big.Int)
Expand All @@ -396,8 +402,7 @@ func checkPrimeFactorsTooClose(n *big.Int, rounds int) error {
// first integer greater than the square root of n. Unfortunately, big.Int's
// built-in square root function takes the floor, so we have to add one to get
// the ceil.
a := new(big.Int)
a.Sqrt(n).Add(a, one)
a := new(big.Int).Add(sqrt, one)

// We calculate b2 to see if it is a perfect square (i.e. b^2), and therefore
// b is an integer. Specifically, b2 = a^2 - n.
Expand Down
6 changes: 6 additions & 0 deletions goodkey/good_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ func TestCheckPrimeFactorsTooClose(t *testing.T) {
q: "155536235030272749691472293262418471207550926406427515178205576891522284497518443889075039382254334975506248481615035474816604875321501901699955105345417152355947783063521554077194367454070647740704883461064399268622437721385112646454393005862535727615809073410746393326688230040267160616554768771412289114113",
expectRounds: 1,
},
{
name: "p squared (2048 bit, 1 round)",
p: "155536235030272749691472293262418471207550926406427515178205576891522284497518443889075039382254334975506248481615035474816604875321501901699955105345417152355947783063521554077194367454070647740704883461064399268622437721385112646454393005862535727615809073410746393326688230040267160616554768771412289114449",
q: "155536235030272749691472293262418471207550926406427515178205576891522284497518443889075039382254334975506248481615035474816604875321501901699955105345417152355947783063521554077194367454070647740704883461064399268622437721385112646454393005862535727615809073410746393326688230040267160616554768771412289114449",
expectRounds: 1,
},
{
// These factors come from a real innsbruck printer in the wild with a
// broken key generation mechanism.
Expand Down
Loading