|
| 1 | +// Copyright 2011 The Go Authors. All rights reserved. |
| 2 | +// Copyright 2011 ThePiachu. All rights reserved. |
| 3 | +// Use of this source code is governed by a BSD-style |
| 4 | +// license that can be found in the LICENSE file. |
| 5 | + |
| 6 | +// Package ecdsa implements the Elliptic Curve Digital Signature Algorithm, as |
| 7 | +// defined in FIPS 186-3. |
| 8 | +package bitecdsa |
| 9 | + |
| 10 | +// References: |
| 11 | +// [NSA]: Suite B implementor's guide to FIPS 186-3, |
| 12 | +// http://www.nsa.gov/ia/_files/ecdsa.pdf |
| 13 | +// [SECG]: SECG, SEC1 |
| 14 | +// http://www.secg.org/download/aid-780/sec1-v2.pdf |
| 15 | + |
| 16 | +import ( |
| 17 | + "io" |
| 18 | + "math/big" |
| 19 | + |
| 20 | + "../bitelliptic" |
| 21 | +) |
| 22 | + |
| 23 | +// PublicKey represents an ECDSA public key. |
| 24 | +type PublicKey struct { |
| 25 | + *bitelliptic.BitCurve |
| 26 | + X, Y *big.Int |
| 27 | +} |
| 28 | + |
| 29 | +// PrivateKey represents a ECDSA private key. |
| 30 | +type PrivateKey struct { |
| 31 | + PublicKey |
| 32 | + D *big.Int |
| 33 | +} |
| 34 | + |
| 35 | +var one = new(big.Int).SetInt64(1) |
| 36 | + |
| 37 | +// randFieldElement returns a random element of the field underlying the given |
| 38 | +// curve using the procedure given in [NSA] A.2.1. |
| 39 | +func randFieldElement(c *bitelliptic.BitCurve, rand io.Reader) (k *big.Int, err error) { |
| 40 | + b := make([]byte, c.BitSize/8+8) |
| 41 | + _, err = io.ReadFull(rand, b) |
| 42 | + if err != nil { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + k = new(big.Int).SetBytes(b) |
| 47 | + n := new(big.Int).Sub(c.N, one) |
| 48 | + k.Mod(k, n) |
| 49 | + k.Add(k, one) |
| 50 | + return |
| 51 | +} |
| 52 | + |
| 53 | +// GenerateKey generates a public&private key pair. |
| 54 | +func GenerateKey(c *bitelliptic.BitCurve, rand io.Reader) (priv *PrivateKey, err error) { |
| 55 | + k, err := randFieldElement(c, rand) |
| 56 | + if err != nil { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + priv = new(PrivateKey) |
| 61 | + priv.PublicKey.BitCurve = c |
| 62 | + priv.D = k |
| 63 | + priv.PublicKey.X, priv.PublicKey.Y = c.ScalarBaseMult(k.Bytes()) |
| 64 | + return |
| 65 | +} |
| 66 | + |
| 67 | +// hashToInt converts a hash value to an integer. There is some disagreement |
| 68 | +// about how this is done. [NSA] suggests that this is done in the obvious |
| 69 | +// manner, but [SECG] truncates the hash to the bit-length of the curve order |
| 70 | +// first. We follow [SECG] because that's what OpenSSL does. |
| 71 | +func hashToInt(hash []byte, c *bitelliptic.BitCurve) *big.Int { |
| 72 | + orderBits := c.N.BitLen() |
| 73 | + orderBytes := (orderBits + 7) / 8 |
| 74 | + if len(hash) > orderBytes { |
| 75 | + hash = hash[:orderBytes] |
| 76 | + } |
| 77 | + |
| 78 | + ret := new(big.Int).SetBytes(hash) |
| 79 | + excess := orderBytes*8 - orderBits |
| 80 | + if excess > 0 { |
| 81 | + ret.Rsh(ret, uint(excess)) |
| 82 | + } |
| 83 | + return ret |
| 84 | +} |
| 85 | + |
| 86 | +// Sign signs an arbitrary length hash (which should be the result of hashing a |
| 87 | +// larger message) using the private key, priv. It returns the signature as a |
| 88 | +// pair of integers. The security of the private key depends on the entropy of |
| 89 | +// rand. |
| 90 | +func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error) { |
| 91 | + // See [NSA] 3.4.1 |
| 92 | + c := priv.PublicKey.BitCurve |
| 93 | + |
| 94 | + var k, kInv *big.Int |
| 95 | + for { |
| 96 | + for { |
| 97 | + k, err = randFieldElement(c, rand) |
| 98 | + if err != nil { |
| 99 | + r = nil |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + kInv = new(big.Int).ModInverse(k, c.N) |
| 104 | + r, _ = priv.BitCurve.ScalarBaseMult(k.Bytes()) |
| 105 | + r.Mod(r, priv.BitCurve.N) |
| 106 | + if r.Sign() != 0 { |
| 107 | + break |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + e := hashToInt(hash, c) |
| 112 | + s = new(big.Int).Mul(priv.D, r) |
| 113 | + s.Add(s, e) |
| 114 | + s.Mul(s, kInv) |
| 115 | + s.Mod(s, priv.PublicKey.BitCurve.N) |
| 116 | + if s.Sign() != 0 { |
| 117 | + break |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return |
| 122 | +} |
| 123 | + |
| 124 | +// Verify verifies the signature in r, s of hash using the public key, pub. It |
| 125 | +// returns true iff the signature is valid. |
| 126 | +func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool { |
| 127 | + // See [NSA] 3.4.2 |
| 128 | + c := pub.BitCurve |
| 129 | + |
| 130 | + if r.Sign() == 0 || s.Sign() == 0 { |
| 131 | + return false |
| 132 | + } |
| 133 | + if r.Cmp(c.N) >= 0 || s.Cmp(c.N) >= 0 { |
| 134 | + return false |
| 135 | + } |
| 136 | + e := hashToInt(hash, c) |
| 137 | + w := new(big.Int).ModInverse(s, c.N) |
| 138 | + |
| 139 | + u1 := e.Mul(e, w) |
| 140 | + u2 := w.Mul(r, w) |
| 141 | + |
| 142 | + x1, y1 := c.ScalarBaseMult(u1.Bytes()) |
| 143 | + x2, y2 := c.ScalarMult(pub.X, pub.Y, u2.Bytes()) |
| 144 | + if x1.Cmp(x2) == 0 { |
| 145 | + return false |
| 146 | + } |
| 147 | + x, _ := c.Add(x1, y1, x2, y2) |
| 148 | + x.Mod(x, c.N) |
| 149 | + return x.Cmp(r) == 0 |
| 150 | +} |
0 commit comments