| 1 |
package openpgp |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/hex" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
"strconv" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/ProtonMail/go-crypto/openpgp/armor" |
| 13 |
) |
| 14 |
|
| 15 |
var pqcDraftVectors = map[string]struct { |
| 16 |
armoredPrivateKeyPath string |
| 17 |
armoredPublicKeyPath string |
| 18 |
fingerprints []string |
| 19 |
armoredMessagePaths []string |
| 20 |
}{ |
| 21 |
"v6_MlDsa65_MlKem768": { |
| 22 |
"test_data/pqc/v6-mldsa-65-sample-sk.asc", |
| 23 |
"test_data/pqc/v6-mldsa-65-sample-pk.asc", |
| 24 |
[]string{"a3e2e14b6a493ff930fb27321f125e9a6880338be9fb7da3ae065ea65793242f", "7dae8fbce23022607167af72a002e774e0ca379a2d7ae072384e1e8fde3265e4"}, |
| 25 |
[]string{"test_data/pqc/v6-mldsa-65-sample-message.asc"}, |
| 26 |
}, |
| 27 |
"v4_eddsa_MlKem768": { |
| 28 |
"test_data/pqc/v4-eddsa-sample-sk.asc", |
| 29 |
"test_data/pqc/v4-eddsa-sample-pk.asc", |
| 30 |
[]string{"342e5db2de345215cb2c944f7102ffed3b9cf12d", "e51dbfea51936988b5428fffa4f95f985ed61a51"}, |
| 31 |
[]string{"test_data/pqc/v4-eddsa-sample-message-v1.asc", "test_data/pqc/v4-eddsa-sample-message-v1.asc"}, |
| 32 |
}, |
| 33 |
} |
| 34 |
|
| 35 |
func TestPqcDraftVectors(t *testing.T) { |
| 36 |
for name, test := range pqcDraftVectors { |
| 37 |
t.Run(name, func(t *testing.T) { |
| 38 |
// Read private key |
| 39 |
privateKeyBytes, err := os.ReadFile(test.armoredPrivateKeyPath) |
| 40 |
if err != nil { |
| 41 |
t.Fatalf("Failed to read private key file: %v", err) |
| 42 |
} |
| 43 |
|
| 44 |
// Read public key |
| 45 |
publicKeyBytes, err := os.ReadFile(test.armoredPublicKeyPath) |
| 46 |
if err != nil { |
| 47 |
t.Fatalf("Failed to read public key file: %v", err) |
| 48 |
} |
| 49 |
|
| 50 |
secretKey, err := ReadArmoredKeyRing(bytes.NewReader(privateKeyBytes)) |
| 51 |
if err != nil { |
| 52 |
t.Error(err) |
| 53 |
return |
| 54 |
} |
| 55 |
|
| 56 |
if len(secretKey) != 1 { |
| 57 |
t.Errorf("Expected 1 entity, found %d", len(secretKey)) |
| 58 |
} |
| 59 |
|
| 60 |
if len(test.fingerprints) > 0 && len(secretKey[0].Subkeys) != len(test.fingerprints)-1 { |
| 61 |
t.Errorf("Expected %d subkey, found %d", len(test.fingerprints)-1, len(secretKey[0].Subkeys)) |
| 62 |
} |
| 63 |
|
| 64 |
if len(test.fingerprints) > 0 && hex.EncodeToString(secretKey[0].PrimaryKey.Fingerprint) != test.fingerprints[0] { |
| 65 |
t.Errorf("Expected primary fingerprint %s, got %x", test.fingerprints[0], secretKey[0].PrimaryKey.Fingerprint) |
| 66 |
} |
| 67 |
|
| 68 |
for i, subkey := range secretKey[0].Subkeys { |
| 69 |
if len(test.fingerprints) > 0 && hex.EncodeToString(subkey.PublicKey.Fingerprint) != test.fingerprints[i+1] { |
| 70 |
t.Errorf("Expected subkey %d fingerprint %s, got %x", i, test.fingerprints[i+1], subkey.PublicKey.Fingerprint) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
var serializedArmoredPublic bytes.Buffer |
| 75 |
serializedPublic, err := armor.EncodeWithChecksumOption(&serializedArmoredPublic, PublicKeyType, nil, false) |
| 76 |
if err != nil { |
| 77 |
t.Fatalf("Failed to init armoring: %s", err) |
| 78 |
} |
| 79 |
|
| 80 |
if err = secretKey[0].Serialize(serializedPublic); err != nil { |
| 81 |
t.Fatalf("Failed to serialize entity: %s", err) |
| 82 |
} |
| 83 |
|
| 84 |
if err := serializedPublic.Close(); err != nil { |
| 85 |
t.Fatalf("Failed to close armoring: %s", err) |
| 86 |
} |
| 87 |
|
| 88 |
if serializedArmoredPublic.String() != strings.Trim(string(publicKeyBytes), "\r\n") { |
| 89 |
t.Error("Wrong serialized public key") |
| 90 |
} |
| 91 |
|
| 92 |
for i, armoredMessage := range test.armoredMessagePaths { |
| 93 |
t.Run("Decrypt_message_"+strconv.Itoa(i), func(t *testing.T) { |
| 94 |
msgData, err := os.ReadFile(armoredMessage) |
| 95 |
if err != nil { |
| 96 |
t.Fatalf("Failed to read message file: %v", err) |
| 97 |
} |
| 98 |
msgReader, err := armor.Decode(bytes.NewReader(msgData)) |
| 99 |
if err != nil { |
| 100 |
t.Error(err) |
| 101 |
return |
| 102 |
} |
| 103 |
|
| 104 |
md, err := ReadMessage(msgReader.Body, secretKey, nil, nil) |
| 105 |
if err != nil { |
| 106 |
t.Fatalf("Error in reading message: %s", err) |
| 107 |
return |
| 108 |
} |
| 109 |
contents, err := io.ReadAll(md.UnverifiedBody) |
| 110 |
if err != nil { |
| 111 |
t.Fatalf("Error in decrypting message: %s", err) |
| 112 |
return |
| 113 |
} |
| 114 |
|
| 115 |
if string(contents) != "Testing\n" { |
| 116 |
t.Fatalf("Decrypted message is wrong: %s", contents) |
| 117 |
} |
| 118 |
}) |
| 119 |
} |
| 120 |
}) |
| 121 |
} |
| 122 |
} |