dsa_test.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. package dsa
  5. import (
  6. "crypto/rand"
  7. "math/big"
  8. "testing"
  9. )
  10. func testSignAndVerify(t *testing.T, i int, priv *PrivateKey) {
  11. hashed := []byte("testing")
  12. r, s, err := Sign(rand.Reader, priv, hashed)
  13. if err != nil {
  14. t.Errorf("%d: error signing: %s", i, err)
  15. return
  16. }
  17. if !Verify(&priv.PublicKey, hashed, r, s) {
  18. t.Errorf("%d: Verify failed", i)
  19. }
  20. }
  21. func testParameterGeneration(t *testing.T, sizes ParameterSizes, L, N int) {
  22. var priv PrivateKey
  23. params := &priv.Parameters
  24. err := GenerateParameters(params, rand.Reader, sizes)
  25. if err != nil {
  26. t.Errorf("%d: %s", int(sizes), err)
  27. return
  28. }
  29. if params.P.BitLen() != L {
  30. t.Errorf("%d: params.BitLen got:%d want:%d", int(sizes), params.P.BitLen(), L)
  31. }
  32. if params.Q.BitLen() != N {
  33. t.Errorf("%d: q.BitLen got:%d want:%d", int(sizes), params.Q.BitLen(), L)
  34. }
  35. one := new(big.Int)
  36. one.SetInt64(1)
  37. pm1 := new(big.Int).Sub(params.P, one)
  38. quo, rem := new(big.Int).DivMod(pm1, params.Q, new(big.Int))
  39. if rem.Sign() != 0 {
  40. t.Errorf("%d: p-1 mod q != 0", int(sizes))
  41. }
  42. x := new(big.Int).Exp(params.G, quo, params.P)
  43. if x.Cmp(one) == 0 {
  44. t.Errorf("%d: invalid generator", int(sizes))
  45. }
  46. err = GenerateKey(&priv, rand.Reader)
  47. if err != nil {
  48. t.Errorf("error generating key: %s", err)
  49. return
  50. }
  51. testSignAndVerify(t, int(sizes), &priv)
  52. }
  53. func TestParameterGeneration(t *testing.T) {
  54. if testing.Short() {
  55. t.Skip("skipping parameter generation test in short mode")
  56. }
  57. testParameterGeneration(t, L1024N160, 1024, 160)
  58. testParameterGeneration(t, L2048N224, 2048, 224)
  59. testParameterGeneration(t, L2048N256, 2048, 256)
  60. testParameterGeneration(t, L3072N256, 3072, 256)
  61. }
  62. func TestSignAndVerify(t *testing.T) {
  63. var priv PrivateKey
  64. priv.P, _ = new(big.Int).SetString("A9B5B793FB4785793D246BAE77E8FF63CA52F442DA763C440259919FE1BC1D6065A9350637A04F75A2F039401D49F08E066C4D275A5A65DA5684BC563C14289D7AB8A67163BFBF79D85972619AD2CFF55AB0EE77A9002B0EF96293BDD0F42685EBB2C66C327079F6C98000FBCB79AACDE1BC6F9D5C7B1A97E3D9D54ED7951FEF", 16)
  65. priv.Q, _ = new(big.Int).SetString("E1D3391245933D68A0714ED34BBCB7A1F422B9C1", 16)
  66. priv.G, _ = new(big.Int).SetString("634364FC25248933D01D1993ECABD0657CC0CB2CEED7ED2E3E8AECDFCDC4A25C3B15E9E3B163ACA2984B5539181F3EFF1A5E8903D71D5B95DA4F27202B77D2C44B430BB53741A8D59A8F86887525C9F2A6A5980A195EAA7F2FF910064301DEF89D3AA213E1FAC7768D89365318E370AF54A112EFBA9246D9158386BA1B4EEFDA", 16)
  67. priv.Y, _ = new(big.Int).SetString("32969E5780CFE1C849A1C276D7AEB4F38A23B591739AA2FE197349AEEBD31366AEE5EB7E6C6DDB7C57D02432B30DB5AA66D9884299FAA72568944E4EEDC92EA3FBC6F39F53412FBCC563208F7C15B737AC8910DBC2D9C9B8C001E72FDC40EB694AB1F06A5A2DBD18D9E36C66F31F566742F11EC0A52E9F7B89355C02FB5D32D2", 16)
  68. priv.X, _ = new(big.Int).SetString("5078D4D29795CBE76D3AACFE48C9AF0BCDBEE91A", 16)
  69. testSignAndVerify(t, 0, &priv)
  70. }