github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/crypto/dsa/dsa_test.go (about) 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 var priv PrivateKey 28 params := &priv.Parameters 29 30 err := GenerateParameters(params, rand.Reader, sizes) 31 if err != nil { 32 t.Errorf("%d: %s", int(sizes), err) 33 return 34 } 35 36 if params.P.BitLen() != L { 37 t.Errorf("%d: params.BitLen got:%d want:%d", int(sizes), params.P.BitLen(), L) 38 } 39 40 if params.Q.BitLen() != N { 41 t.Errorf("%d: q.BitLen got:%d want:%d", int(sizes), params.Q.BitLen(), L) 42 } 43 44 one := new(big.Int) 45 one.SetInt64(1) 46 pm1 := new(big.Int).Sub(params.P, one) 47 quo, rem := new(big.Int).DivMod(pm1, params.Q, new(big.Int)) 48 if rem.Sign() != 0 { 49 t.Errorf("%d: p-1 mod q != 0", int(sizes)) 50 } 51 x := new(big.Int).Exp(params.G, quo, params.P) 52 if x.Cmp(one) == 0 { 53 t.Errorf("%d: invalid generator", int(sizes)) 54 } 55 56 err = GenerateKey(&priv, rand.Reader) 57 if err != nil { 58 t.Errorf("error generating key: %s", err) 59 return 60 } 61 62 testSignAndVerify(t, int(sizes), &priv) 63 } 64 65 func TestParameterGeneration(t *testing.T) { 66 if testing.Short() { 67 t.Skip("skipping parameter generation test in short mode") 68 } 69 70 testParameterGeneration(t, L1024N160, 1024, 160) 71 testParameterGeneration(t, L2048N224, 2048, 224) 72 testParameterGeneration(t, L2048N256, 2048, 256) 73 testParameterGeneration(t, L3072N256, 3072, 256) 74 } 75 76 func TestSignAndVerify(t *testing.T) { 77 var priv PrivateKey 78 priv.P, _ = new(big.Int).SetString("A9B5B793FB4785793D246BAE77E8FF63CA52F442DA763C440259919FE1BC1D6065A9350637A04F75A2F039401D49F08E066C4D275A5A65DA5684BC563C14289D7AB8A67163BFBF79D85972619AD2CFF55AB0EE77A9002B0EF96293BDD0F42685EBB2C66C327079F6C98000FBCB79AACDE1BC6F9D5C7B1A97E3D9D54ED7951FEF", 16) 79 priv.Q, _ = new(big.Int).SetString("E1D3391245933D68A0714ED34BBCB7A1F422B9C1", 16) 80 priv.G, _ = new(big.Int).SetString("634364FC25248933D01D1993ECABD0657CC0CB2CEED7ED2E3E8AECDFCDC4A25C3B15E9E3B163ACA2984B5539181F3EFF1A5E8903D71D5B95DA4F27202B77D2C44B430BB53741A8D59A8F86887525C9F2A6A5980A195EAA7F2FF910064301DEF89D3AA213E1FAC7768D89365318E370AF54A112EFBA9246D9158386BA1B4EEFDA", 16) 81 priv.Y, _ = new(big.Int).SetString("32969E5780CFE1C849A1C276D7AEB4F38A23B591739AA2FE197349AEEBD31366AEE5EB7E6C6DDB7C57D02432B30DB5AA66D9884299FAA72568944E4EEDC92EA3FBC6F39F53412FBCC563208F7C15B737AC8910DBC2D9C9B8C001E72FDC40EB694AB1F06A5A2DBD18D9E36C66F31F566742F11EC0A52E9F7B89355C02FB5D32D2", 16) 82 priv.X, _ = new(big.Int).SetString("5078D4D29795CBE76D3AACFE48C9AF0BCDBEE91A", 16) 83 84 testSignAndVerify(t, 0, &priv) 85 }