| 1 | // Copyright 2019 ProtonTech AG. |
| 2 | |
| 3 | // Package ecc implements a generic interface for ECDH, ECDSA, and EdDSA. |
| 4 | package eccscip-go gomod github.com/ProtonMail/go-crypto . `github.com/ProtonMail/go-crypto/openpgp/internal/ecc`/ |
| 5 | |
| 6 | import ( |
| 7 | "crypto/randscip-go gomod github.com/golang/go/src go1.23.0 `crypto/rand`/" |
| 8 | "testingscip-go gomod github.com/golang/go/src go1.23.0 testing/" |
| 9 | ) |
| 10 | |
| 11 | // Some OpenPGP implementations, such as gpg 2.2.12, do not accept ECDH private |
| 12 | // keys if they're not masked. This is because they're not of the proper form, |
| 13 | // cryptographically, and they don't mask input keys during crypto operations. |
| 14 | // This test checks if the keys that this library stores or outputs are |
| 15 | // properly masked. |
| 16 | func TestGenerateMaskedPrivateKeyX25519scip-go gomod github.com/ProtonMail/go-crypto . `github.com/ProtonMail/go-crypto/openpgp/internal/ecc`/TestGenerateMaskedPrivateKeyX25519().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 17 | c := NewCurve25519scip-go gomod github.com/ProtonMail/go-crypto . `github.com/ProtonMail/go-crypto/openpgp/internal/ecc`/NewCurve25519().() |
| 18 | _, secret, err := c.GenerateECDHscip-go gomod github.com/ProtonMail/go-crypto . `github.com/ProtonMail/go-crypto/openpgp/internal/ecc`/curve25519#GenerateECDH().(randscip-go gomod github.com/golang/go/src go1.23.0 `crypto/rand`/.Readerscip-go gomod github.com/golang/go/src go1.23.0 `crypto/rand`/Reader.) |
| 19 | if err != nil { |
| 20 | t.Fatalscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatal().(err) |
| 21 | } |
| 22 | |
| 23 | encoded := c.MarshalByteSecretscip-go gomod github.com/ProtonMail/go-crypto . `github.com/ProtonMail/go-crypto/openpgp/internal/ecc`/curve25519#MarshalByteSecret().(secret) |
| 24 | decoded := c.UnmarshalByteSecretscip-go gomod github.com/ProtonMail/go-crypto . `github.com/ProtonMail/go-crypto/openpgp/internal/ecc`/curve25519#UnmarshalByteSecret().(encoded) |
| 25 | if decoded == nil { |
| 26 | t.Fatalscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatal().(err) |
| 27 | } |
| 28 | |
| 29 | // Check masking |
| 30 | // 3 lsb are 0 |
| 31 | if decoded[0]<<5 != 0 { |
| 32 | t.Fatalfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatalf().("Priv. key is not masked (3 lsb should be unset): %X", decoded) |
| 33 | } |
| 34 | // MSB is 0 |
| 35 | if decoded[31]>>7 != 0 { |
| 36 | t.Fatalfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatalf().("Priv. key is not masked (MSB should be unset): %X", decoded) |
| 37 | } |
| 38 | // Second-MSB is 1 |
| 39 | if decoded[31]>>6 != 1 { |
| 40 | t.Fatalfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatalf().("Priv. key is not masked (second MSB should be set): %X", decoded) |
| 41 | } |
| 42 | } |