forked from usbarmory/tamago-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecdsa.go
45 lines (36 loc) · 1.06 KB
/
ecdsa.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// Adapted from go/src/crypto/ecdsa/ecdsa_test.go
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"log"
"time"
)
func testSignAndVerify(c elliptic.Curve, tag string) {
start := time.Now()
log.Printf("ECDSA sign and verify with p%d ... ", c.Params().BitSize)
priv, _ := ecdsa.GenerateKey(c, rand.Reader)
hashed := []byte("testing")
r, s, err := ecdsa.Sign(rand.Reader, priv, hashed)
if err != nil {
log.Printf("%s: error signing: %s", tag, err)
return
}
if !ecdsa.Verify(&priv.PublicKey, hashed, r, s) {
log.Printf("%s: Verify failed", tag)
}
hashed[0] ^= 0xff
if ecdsa.Verify(&priv.PublicKey, hashed, r, s) {
log.Printf("%s: Verify always works!", tag)
}
log.Printf("ECDSA sign and verify with p%d took %s", c.Params().BitSize, time.Since(start))
}
func TestSignAndVerify() {
testSignAndVerify(elliptic.P224(), "p224")
testSignAndVerify(elliptic.P256(), "p256")
}