...

Source file src/github.com/ProtonMail/go-crypto/openpgp/eddsa/eddsa_test.go

Documentation: github.com/ProtonMail/go-crypto/openpgp/eddsa

     1  // Package eddsa implements EdDSA signature, suitable for OpenPGP, as specified in
     2  // https://datatracker.ietf.org/doc/html/draft-ietf-openpgp-crypto-refresh-06#section-13.7
     3  package eddsa
     4  
     5  import (
     6  	"bytes"
     7  	"crypto/rand"
     8  	"io"
     9  	"testing"
    10  
    11  	"github.com/ProtonMail/go-crypto/openpgp/internal/ecc"
    12  )
    13  
    14  func TestCurves(t *testing.T) {
    15  	for _, curve := range ecc.Curves {
    16  		EdDSACurve, ok := curve.Curve.(ecc.EdDSACurve)
    17  		if !ok {
    18  			continue
    19  		}
    20  
    21  		t.Run(EdDSACurve.GetCurveName(), func(t *testing.T) {
    22  			testFingerprint := make([]byte, 20)
    23  			_, err := io.ReadFull(rand.Reader, testFingerprint[:])
    24  			if err != nil {
    25  				t.Fatal(err)
    26  			}
    27  
    28  			priv := testGenerate(t, EdDSACurve)
    29  			testSignVerify(t, priv)
    30  			testValidation(t, priv)
    31  			testMarshalUnmarshal(t, priv)
    32  		})
    33  	}
    34  }
    35  
    36  func testGenerate(t *testing.T, curve ecc.EdDSACurve) *PrivateKey {
    37  	priv, err := GenerateKey(rand.Reader, curve)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	return priv
    43  }
    44  
    45  func testSignVerify(t *testing.T, priv *PrivateKey) {
    46  	digest := make([]byte, 32)
    47  	_, err := io.ReadFull(rand.Reader, digest[:])
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  
    52  	r, s, err := Sign(priv, digest)
    53  	if err != nil {
    54  		t.Errorf("error signing: %s", err)
    55  	}
    56  
    57  	result := Verify(&priv.PublicKey, digest, r, s)
    58  
    59  	if !result {
    60  		t.Error("unable to verify message")
    61  	}
    62  }
    63  
    64  func testValidation(t *testing.T, priv *PrivateKey) {
    65  	if err := Validate(priv); err != nil {
    66  		t.Fatalf("valid key marked as invalid: %s", err)
    67  	}
    68  
    69  	priv.D[5] ^= 1
    70  	if err := Validate(priv); err == nil {
    71  		t.Fatal("failed to detect invalid key")
    72  	}
    73  }
    74  
    75  func testMarshalUnmarshal(t *testing.T, priv *PrivateKey) {
    76  	// Test correct zero padding
    77  	priv.X[0] = 0
    78  	priv.D[0] = 0
    79  
    80  	x := priv.MarshalPoint()
    81  	d := priv.MarshalByteSecret()
    82  
    83  	parsed := NewPrivateKey(*NewPublicKey(priv.GetCurve()))
    84  
    85  	if err := parsed.UnmarshalPoint(x); err != nil {
    86  		t.Fatalf("unable to unmarshal point: %s", err)
    87  	}
    88  
    89  	if err := parsed.UnmarshalByteSecret(d); err != nil {
    90  		t.Fatalf("unable to unmarshal integer: %s", err)
    91  	}
    92  
    93  	if !bytes.Equal(priv.X, parsed.X) || !bytes.Equal(priv.D, parsed.D) {
    94  		t.Fatal("failed to marshal/unmarshal correctly")
    95  	}
    96  }
    97  

View as plain text