You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

75 lines
2.5 KiB

  1. // Copyright 2018 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 sign
  5. import (
  6. "bytes"
  7. "crypto/rand"
  8. "encoding/hex"
  9. "testing"
  10. )
  11. var testSignedMessage, _ = hex.DecodeString("26a0a47f733d02ddb74589b6cbd6f64a7dab1947db79395a1a9e00e4c902c0f185b119897b89b248d16bab4ea781b5a3798d25c2984aec833dddab57e0891e0d68656c6c6f20776f726c64")
  12. var testMessage = testSignedMessage[Overhead:]
  13. var testPublicKey [32]byte
  14. var testPrivateKey = [64]byte{
  15. 0x98, 0x3c, 0x6a, 0xa6, 0x21, 0xcc, 0xbb, 0xb2, 0xa7, 0xe8, 0x97, 0x94, 0xde, 0x5f, 0xf8, 0x11,
  16. 0x8a, 0xf3, 0x33, 0x1a, 0x03, 0x5c, 0x43, 0x99, 0x03, 0x13, 0x2d, 0xd7, 0xb4, 0xc4, 0x8b, 0xb0,
  17. 0xf6, 0x33, 0x20, 0xa3, 0x34, 0x8b, 0x7b, 0xe2, 0xfe, 0xb4, 0xe7, 0x3a, 0x54, 0x08, 0x2d, 0xd7,
  18. 0x0c, 0xb7, 0xc0, 0xe3, 0xbf, 0x62, 0x6c, 0x55, 0xf0, 0x33, 0x28, 0x52, 0xf8, 0x48, 0x7d, 0xfd,
  19. }
  20. func init() {
  21. copy(testPublicKey[:], testPrivateKey[32:])
  22. }
  23. func TestSign(t *testing.T) {
  24. signedMessage := Sign(nil, testMessage, &testPrivateKey)
  25. if !bytes.Equal(signedMessage, testSignedMessage) {
  26. t.Fatalf("signed message did not match, got\n%x\n, expected\n%x", signedMessage, testSignedMessage)
  27. }
  28. }
  29. func TestOpen(t *testing.T) {
  30. message, ok := Open(nil, testSignedMessage, &testPublicKey)
  31. if !ok {
  32. t.Fatalf("valid signed message not successfully verified")
  33. }
  34. if !bytes.Equal(message, testMessage) {
  35. t.Fatalf("message did not match, got\n%x\n, expected\n%x", message, testMessage)
  36. }
  37. message, ok = Open(nil, testSignedMessage[1:], &testPublicKey)
  38. if ok {
  39. t.Fatalf("invalid signed message successfully verified")
  40. }
  41. badMessage := make([]byte, len(testSignedMessage))
  42. copy(badMessage, testSignedMessage)
  43. badMessage[5] ^= 1
  44. if _, ok := Open(nil, badMessage, &testPublicKey); ok {
  45. t.Fatalf("Open succeeded with a corrupt message")
  46. }
  47. var badPublicKey [32]byte
  48. copy(badPublicKey[:], testPublicKey[:])
  49. badPublicKey[5] ^= 1
  50. if _, ok := Open(nil, testSignedMessage, &badPublicKey); ok {
  51. t.Fatalf("Open succeeded with a corrupt public key")
  52. }
  53. }
  54. func TestGenerateSignOpen(t *testing.T) {
  55. publicKey, privateKey, _ := GenerateKey(rand.Reader)
  56. signedMessage := Sign(nil, testMessage, privateKey)
  57. message, ok := Open(nil, signedMessage, publicKey)
  58. if !ok {
  59. t.Fatalf("failed to verify signed message")
  60. }
  61. if !bytes.Equal(message, testMessage) {
  62. t.Fatalf("verified message does not match signed messge, got\n%x\n, expected\n%x", message, testMessage)
  63. }
  64. }