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.
13 func testSignAndVerify(t *testing.T, i int, priv *PrivateKey) {
14 hashed := []byte("testing")
15 r, s, err := Sign(rand.Reader, priv, hashed)
17 t.Errorf("%d: error signing: %s", i, err)
21 if !Verify(&priv.PublicKey, hashed, r, s) {
22 t.Errorf("%d: Verify failed", i)
26 func testParameterGeneration(t *testing.T, sizes ParameterSizes, L, N int) {
28 params := &priv.Parameters
30 err := GenerateParameters(params, rand.Reader, sizes)
32 t.Errorf("%d: %s", int(sizes), err)
36 if params.P.BitLen() != L {
37 t.Errorf("%d: params.BitLen got:%d want:%d", int(sizes), params.P.BitLen(), L)
40 if params.Q.BitLen() != N {
41 t.Errorf("%d: q.BitLen got:%d want:%d", int(sizes), params.Q.BitLen(), L)
46 pm1 := new(big.Int).Sub(params.P, one)
47 quo, rem := new(big.Int).DivMod(pm1, params.Q, new(big.Int))
49 t.Errorf("%d: p-1 mod q != 0", int(sizes))
51 x := new(big.Int).Exp(params.G, quo, params.P)
53 t.Errorf("%d: invalid generator", int(sizes))
56 err = GenerateKey(&priv, rand.Reader)
58 t.Errorf("error generating key: %s", err)
62 testSignAndVerify(t, int(sizes), &priv)
65 func TestParameterGeneration(t *testing.T) {
66 // This test is too slow to run all the time.
69 testParameterGeneration(t, L1024N160, 1024, 160)
70 testParameterGeneration(t, L2048N224, 2048, 224)
71 testParameterGeneration(t, L2048N256, 2048, 256)
72 testParameterGeneration(t, L3072N256, 3072, 256)
75 func TestSignAndVerify(t *testing.T) {
77 priv.P, _ = new(big.Int).SetString("A9B5B793FB4785793D246BAE77E8FF63CA52F442DA763C440259919FE1BC1D6065A9350637A04F75A2F039401D49F08E066C4D275A5A65DA5684BC563C14289D7AB8A67163BFBF79D85972619AD2CFF55AB0EE77A9002B0EF96293BDD0F42685EBB2C66C327079F6C98000FBCB79AACDE1BC6F9D5C7B1A97E3D9D54ED7951FEF", 16)
78 priv.Q, _ = new(big.Int).SetString("E1D3391245933D68A0714ED34BBCB7A1F422B9C1", 16)
79 priv.G, _ = new(big.Int).SetString("634364FC25248933D01D1993ECABD0657CC0CB2CEED7ED2E3E8AECDFCDC4A25C3B15E9E3B163ACA2984B5539181F3EFF1A5E8903D71D5B95DA4F27202B77D2C44B430BB53741A8D59A8F86887525C9F2A6A5980A195EAA7F2FF910064301DEF89D3AA213E1FAC7768D89365318E370AF54A112EFBA9246D9158386BA1B4EEFDA", 16)
80 priv.Y, _ = new(big.Int).SetString("32969E5780CFE1C849A1C276D7AEB4F38A23B591739AA2FE197349AEEBD31366AEE5EB7E6C6DDB7C57D02432B30DB5AA66D9884299FAA72568944E4EEDC92EA3FBC6F39F53412FBCC563208F7C15B737AC8910DBC2D9C9B8C001E72FDC40EB694AB1F06A5A2DBD18D9E36C66F31F566742F11EC0A52E9F7B89355C02FB5D32D2", 16)
81 priv.X, _ = new(big.Int).SetString("5078D4D29795CBE76D3AACFE48C9AF0BCDBEE91A", 16)
83 testSignAndVerify(t, 0, &priv)