File: curve25519_test.go

package info (click to toggle)
golang-github-protonmail-go-crypto 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,932 kB
  • sloc: makefile: 10
file content (42 lines) | stat: -rw-r--r-- 1,154 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright 2019 ProtonTech AG.

// Package ecc implements a generic interface for ECDH, ECDSA, and EdDSA.
package ecc

import (
	"crypto/rand"
	"testing"
)

// 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) {
	c := NewCurve25519()
	_, secret, err := c.GenerateECDH(rand.Reader)
	if err != nil {
		t.Fatal(err)
	}

	encoded := c.MarshalByteSecret(secret)
	decoded := c.UnmarshalByteSecret(encoded)
	if decoded == nil {
		t.Fatal(err)
	}

	// Check masking
	// 3 lsb are 0
	if decoded[0]<<5 != 0 {
		t.Fatalf("Priv. key is not masked (3 lsb should be unset): %X", decoded)
	}
	// MSB is 0
	if decoded[31]>>7 != 0 {
		t.Fatalf("Priv. key is not masked (MSB should be unset): %X", decoded)
	}
	// Second-MSB is 1
	if decoded[31]>>6 != 1 {
		t.Fatalf("Priv. key is not masked (second MSB should be set): %X", decoded)
	}
}