2019-05-13 14:06:54 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io/ioutil"
|
2019-12-27 19:35:43 +01:00
|
|
|
"math/big"
|
2019-05-14 16:08:25 +00:00
|
|
|
"strings"
|
2019-12-27 19:35:43 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/crypto/ed25519"
|
|
|
|
|
"golang.org/x/crypto/openpgp/ecdh"
|
|
|
|
|
"golang.org/x/crypto/rsa"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-05-13 14:06:54 +00:00
|
|
|
)
|
|
|
|
|
|
2019-12-27 19:35:43 +01:00
|
|
|
const testTime = 1557754627 // 2019-05-13T13:37:07+00:00
|
2019-05-13 14:06:54 +00:00
|
|
|
|
2019-05-14 16:08:25 +00:00
|
|
|
func readTestFile(name string, trimNewlines bool) string {
|
2019-12-27 19:35:43 +01:00
|
|
|
data, err := ioutil.ReadFile("testdata/" + name) //nolint
|
2019-05-13 14:06:54 +00:00
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2019-05-14 16:08:25 +00:00
|
|
|
if trimNewlines {
|
|
|
|
|
return strings.TrimRight(string(data), "\n")
|
|
|
|
|
}
|
2019-05-13 14:06:54 +00:00
|
|
|
return string(data)
|
|
|
|
|
}
|
2019-12-27 19:35:43 +01:00
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
UpdateTime(testTime) // 2019-05-13T13:37:07+00:00
|
|
|
|
|
|
|
|
|
|
initGenerateKeys()
|
|
|
|
|
initArmoredKeys()
|
|
|
|
|
initKeyRings()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func assertBigIntCleared(t *testing.T, x *big.Int) {
|
|
|
|
|
w := x.Bits()
|
|
|
|
|
for k := range w {
|
|
|
|
|
assert.Exactly(t, big.Word(0x00), w[k])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func assertMemCleared(t *testing.T, b []byte) {
|
|
|
|
|
for k := range b {
|
|
|
|
|
assert.Exactly(t, uint8(0x00), b[k])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func assertRSACleared(t *testing.T, rsaPriv *rsa.PrivateKey) {
|
|
|
|
|
assertBigIntCleared(t, rsaPriv.D)
|
|
|
|
|
for idx := range rsaPriv.Primes {
|
|
|
|
|
assertBigIntCleared(t, rsaPriv.Primes[idx])
|
|
|
|
|
}
|
|
|
|
|
assertBigIntCleared(t, rsaPriv.Precomputed.Qinv)
|
|
|
|
|
assertBigIntCleared(t, rsaPriv.Precomputed.Dp)
|
|
|
|
|
assertBigIntCleared(t, rsaPriv.Precomputed.Dq)
|
|
|
|
|
|
|
|
|
|
for idx := range rsaPriv.Precomputed.CRTValues {
|
|
|
|
|
assertBigIntCleared(t, rsaPriv.Precomputed.CRTValues[idx].Exp)
|
|
|
|
|
assertBigIntCleared(t, rsaPriv.Precomputed.CRTValues[idx].Coeff)
|
|
|
|
|
assertBigIntCleared(t, rsaPriv.Precomputed.CRTValues[idx].R)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func assertEdDSACleared(t *testing.T, priv ed25519.PrivateKey) {
|
|
|
|
|
assertMemCleared(t, priv)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func assertECDHCleared(t *testing.T, priv *ecdh.PrivateKey) {
|
|
|
|
|
assertMemCleared(t, priv.D)
|
|
|
|
|
}
|