mirror of
https://github.com/coder/coder.git
synced 2026-11-06 00:03:36 +08:00
Modify the RSA key generation algorithm to check that GCD(e, p-1) = 1 and GCD(e, q-1) = 1 when selecting prime numbers, ensuring that e and φ(n) are coprime. This prevents ModInverse from returning nil, which would cause private key generation to fail and result in a panic when `Precompute` is called. Change-Id: I0a453e1e1f8c638e40e7a4b87a6d0d7299e1cb5d Signed-off-by: Thomas Kosiewski <tk@coder.com>
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package agentrsa_test
|
|
|
|
import (
|
|
"crypto/rsa"
|
|
"math/rand/v2"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/coder/coder/v2/agent/agentrsa"
|
|
)
|
|
|
|
func TestGenerateDeterministicKey(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
key1 := agentrsa.GenerateDeterministicKey(1234)
|
|
key2 := agentrsa.GenerateDeterministicKey(1234)
|
|
|
|
assert.Equal(t, key1, key2)
|
|
assert.EqualExportedValues(t, key1, key2)
|
|
}
|
|
|
|
var result *rsa.PrivateKey
|
|
|
|
func BenchmarkGenerateDeterministicKey(b *testing.B) {
|
|
var r *rsa.PrivateKey
|
|
|
|
for range b.N {
|
|
// always record the result of DeterministicPrivateKey to prevent
|
|
// the compiler eliminating the function call.
|
|
r = agentrsa.GenerateDeterministicKey(rand.Int64())
|
|
}
|
|
|
|
// always store the result to a package level variable
|
|
// so the compiler cannot eliminate the Benchmark itself.
|
|
result = r
|
|
}
|
|
|
|
func FuzzGenerateDeterministicKey(f *testing.F) {
|
|
testcases := []int64{0, 1234, 1010101010}
|
|
for _, tc := range testcases {
|
|
f.Add(tc) // Use f.Add to provide a seed corpus
|
|
}
|
|
f.Fuzz(func(t *testing.T, seed int64) {
|
|
key1 := agentrsa.GenerateDeterministicKey(seed)
|
|
key2 := agentrsa.GenerateDeterministicKey(seed)
|
|
assert.Equal(t, key1, key2)
|
|
assert.EqualExportedValues(t, key1, key2)
|
|
})
|
|
}
|