Source file src/crypto/dsa/dsa_test.go

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package dsa
     6  
     7  import (
     8  	"crypto/rand"
     9  	"math/big"
    10  	"testing"
    11  )
    12  
    13  func testSignAndVerify(t *testing.T, i int, priv *PrivateKey) {
    14  	hashed := []byte("testing")
    15  	r, s, err := Sign(rand.Reader, priv, hashed)
    16  	if err != nil {
    17  		t.Errorf("%d: error signing: %s", i, err)
    18  		return
    19  	}
    20  
    21  	if !Verify(&priv.PublicKey, hashed, r, s) {
    22  		t.Errorf("%d: Verify failed", i)
    23  	}
    24  }
    25  
    26  func testParameterGeneration(t *testing.T, sizes ParameterSizes, L, N int) {
    27  	t.Helper()
    28  	var priv PrivateKey
    29  	params := &priv.Parameters
    30  
    31  	err := GenerateParameters(params, rand.Reader, sizes)
    32  	if err != nil {
    33  		t.Errorf("%d: %s", int(sizes), err)
    34  		return
    35  	}
    36  
    37  	if params.P.BitLen() != L {
    38  		t.Errorf("%d: params.BitLen got:%d want:%d", int(sizes), params.P.BitLen(), L)
    39  	}
    40  
    41  	if params.Q.BitLen() != N {
    42  		t.Errorf("%d: q.BitLen got:%d want:%d", int(sizes), params.Q.BitLen(), L)
    43  	}
    44  
    45  	one := new(big.Int)
    46  	one.SetInt64(1)
    47  	pm1 := new(big.Int).Sub(params.P, one)
    48  	quo, rem := new(big.Int).DivMod(pm1, params.Q, new(big.Int))
    49  	if rem.Sign() != 0 {
    50  		t.Errorf("%d: p-1 mod q != 0", int(sizes))
    51  	}
    52  	x := new(big.Int).Exp(params.G, quo, params.P)
    53  	if x.Cmp(one) == 0 {
    54  		t.Errorf("%d: invalid generator", int(sizes))
    55  	}
    56  
    57  	err = GenerateKey(&priv, rand.Reader)
    58  	if err != nil {
    59  		t.Errorf("error generating key: %s", err)
    60  		return
    61  	}
    62  
    63  	testSignAndVerify(t, int(sizes), &priv)
    64  }
    65  
    66  func TestParameterGeneration(t *testing.T) {
    67  	if testing.Short() {
    68  		t.Skip("skipping parameter generation test in short mode")
    69  	}
    70  
    71  	testParameterGeneration(t, L1024N160, 1024, 160)
    72  	testParameterGeneration(t, L2048N224, 2048, 224)
    73  	testParameterGeneration(t, L2048N256, 2048, 256)
    74  	testParameterGeneration(t, L3072N256, 3072, 256)
    75  }
    76  
    77  func fromHex(s string) *big.Int {
    78  	result, ok := new(big.Int).SetString(s, 16)
    79  	if !ok {
    80  		panic(s)
    81  	}
    82  	return result
    83  }
    84  
    85  func TestSignAndVerify(t *testing.T) {
    86  	priv := PrivateKey{
    87  		PublicKey: PublicKey{
    88  			Parameters: Parameters{
    89  				P: fromHex("A9B5B793FB4785793D246BAE77E8FF63CA52F442DA763C440259919FE1BC1D6065A9350637A04F75A2F039401D49F08E066C4D275A5A65DA5684BC563C14289D7AB8A67163BFBF79D85972619AD2CFF55AB0EE77A9002B0EF96293BDD0F42685EBB2C66C327079F6C98000FBCB79AACDE1BC6F9D5C7B1A97E3D9D54ED7951FEF"),
    90  				Q: fromHex("E1D3391245933D68A0714ED34BBCB7A1F422B9C1"),
    91  				G: fromHex("634364FC25248933D01D1993ECABD0657CC0CB2CEED7ED2E3E8AECDFCDC4A25C3B15E9E3B163ACA2984B5539181F3EFF1A5E8903D71D5B95DA4F27202B77D2C44B430BB53741A8D59A8F86887525C9F2A6A5980A195EAA7F2FF910064301DEF89D3AA213E1FAC7768D89365318E370AF54A112EFBA9246D9158386BA1B4EEFDA"),
    92  			},
    93  			Y: fromHex("32969E5780CFE1C849A1C276D7AEB4F38A23B591739AA2FE197349AEEBD31366AEE5EB7E6C6DDB7C57D02432B30DB5AA66D9884299FAA72568944E4EEDC92EA3FBC6F39F53412FBCC563208F7C15B737AC8910DBC2D9C9B8C001E72FDC40EB694AB1F06A5A2DBD18D9E36C66F31F566742F11EC0A52E9F7B89355C02FB5D32D2"),
    94  		},
    95  		X: fromHex("5078D4D29795CBE76D3AACFE48C9AF0BCDBEE91A"),
    96  	}
    97  
    98  	testSignAndVerify(t, 0, &priv)
    99  }
   100  
   101  func TestSignAndVerifyWithBadPublicKey(t *testing.T) {
   102  	pub := PublicKey{
   103  		Parameters: Parameters{
   104  			P: fromHex("A9B5B793FB4785793D246BAE77E8FF63CA52F442DA763C440259919FE1BC1D6065A9350637A04F75A2F039401D49F08E066C4D275A5A65DA5684BC563C14289D7AB8A67163BFBF79D85972619AD2CFF55AB0EE77A9002B0EF96293BDD0F42685EBB2C66C327079F6C98000FBCB79AACDE1BC6F9D5C7B1A97E3D9D54ED7951FEF"),
   105  			Q: fromHex("FA"),
   106  			G: fromHex("634364FC25248933D01D1993ECABD0657CC0CB2CEED7ED2E3E8AECDFCDC4A25C3B15E9E3B163ACA2984B5539181F3EFF1A5E8903D71D5B95DA4F27202B77D2C44B430BB53741A8D59A8F86887525C9F2A6A5980A195EAA7F2FF910064301DEF89D3AA213E1FAC7768D89365318E370AF54A112EFBA9246D9158386BA1B4EEFDA"),
   107  		},
   108  		Y: fromHex("32969E5780CFE1C849A1C276D7AEB4F38A23B591739AA2FE197349AEEBD31366AEE5EB7E6C6DDB7C57D02432B30DB5AA66D9884299FAA72568944E4EEDC92EA3FBC6F39F53412FBCC563208F7C15B737AC8910DBC2D9C9B8C001E72FDC40EB694AB1F06A5A2DBD18D9E36C66F31F566742F11EC0A52E9F7B89355C02FB5D32D2"),
   109  	}
   110  
   111  	if Verify(&pub, []byte("testing"), fromHex("2"), fromHex("4")) {
   112  		t.Errorf("Verify unexpected success with non-existent mod inverse of Q")
   113  	}
   114  }
   115  
   116  func TestSigningWithDegenerateKeys(t *testing.T) {
   117  	// Signing with degenerate private keys should not cause an infinite
   118  	// loop.
   119  	badKeys := []struct {
   120  		p, q, g, y, x string
   121  	}{
   122  		{"00", "01", "00", "00", "00"},
   123  		{"01", "ff", "00", "00", "00"},
   124  	}
   125  
   126  	for i, test := range badKeys {
   127  		priv := PrivateKey{
   128  			PublicKey: PublicKey{
   129  				Parameters: Parameters{
   130  					P: fromHex(test.p),
   131  					Q: fromHex(test.q),
   132  					G: fromHex(test.g),
   133  				},
   134  				Y: fromHex(test.y),
   135  			},
   136  			X: fromHex(test.x),
   137  		}
   138  
   139  		hashed := []byte("testing")
   140  		if _, _, err := Sign(rand.Reader, &priv, hashed); err == nil {
   141  			t.Errorf("#%d: unexpected success", i)
   142  		}
   143  	}
   144  }
   145  

View as plain text