-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ECDH-Curve25519 private key masking (#40)
- Loading branch information
Showing
4 changed files
with
71 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2019 ProtonTech AG. | ||
// | ||
|
||
package ecdh | ||
|
||
import ( | ||
"testing" | ||
"crypto/rand" | ||
) | ||
|
||
// Some OpenPGP implementations, such as gpg 2.2.12, do not accept ECDH private | ||
// keys if they're not masked. This is because they're not of the proper form, | ||
// cryptographically, and they don't mask input keys during crypto operations. | ||
// This test checks if the keys that this library stores or outputs are | ||
// properly masked. | ||
func TestGenerateMaskedPrivateKeyX25519(t *testing.T) { | ||
priv, _, err := x25519GenerateKeyPairBytes(rand.Reader) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Check masking | ||
// 3 lsb are 0 | ||
if priv[0]<<5 != 0 { | ||
t.Fatalf("Priv. key is not masked (3 lsb should be unset): %X", priv) | ||
} | ||
// MSB is 0 | ||
if priv[31]>>7 != 0 { | ||
t.Fatalf("Priv. key is not masked (MSB should be unset): %X", priv) | ||
} | ||
// Second-MSB is 1 | ||
if priv[31]>>6 != 1 { | ||
t.Fatalf("Priv. key is not masked (second MSB should be set): %X", priv) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters