signify.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // signFile reads the contents of an input file and signs it (in armored format)
  17. // with the key provided, placing the signature into the output file.
  18. package signify
  19. import (
  20. "bytes"
  21. "crypto/ed25519"
  22. "encoding/base64"
  23. "errors"
  24. "fmt"
  25. "io/ioutil"
  26. "strings"
  27. "time"
  28. )
  29. var (
  30. errInvalidKeyHeader = errors.New("incorrect key header")
  31. errInvalidKeyLength = errors.New("invalid, key length != 104")
  32. )
  33. func parsePrivateKey(key string) (k ed25519.PrivateKey, header []byte, keyNum []byte, err error) {
  34. keydata, err := base64.StdEncoding.DecodeString(key)
  35. if err != nil {
  36. return nil, nil, nil, err
  37. }
  38. if len(keydata) != 104 {
  39. return nil, nil, nil, errInvalidKeyLength
  40. }
  41. if string(keydata[:2]) != "Ed" {
  42. return nil, nil, nil, errInvalidKeyHeader
  43. }
  44. return keydata[40:], keydata[:2], keydata[32:40], nil
  45. }
  46. // SignFile creates a signature of the input file.
  47. //
  48. // This accepts base64 keys in the format created by the 'signify' tool.
  49. // The signature is written to the 'output' file.
  50. func SignFile(input string, output string, key string, untrustedComment string, trustedComment string) error {
  51. // Pre-check comments and ensure they're set to something.
  52. if strings.IndexByte(untrustedComment, '\n') >= 0 {
  53. return errors.New("untrusted comment must not contain newline")
  54. }
  55. if strings.IndexByte(trustedComment, '\n') >= 0 {
  56. return errors.New("trusted comment must not contain newline")
  57. }
  58. if untrustedComment == "" {
  59. untrustedComment = "verify with " + input + ".pub"
  60. }
  61. if trustedComment == "" {
  62. trustedComment = fmt.Sprintf("timestamp:%d", time.Now().Unix())
  63. }
  64. filedata, err := ioutil.ReadFile(input)
  65. if err != nil {
  66. return err
  67. }
  68. skey, header, keyNum, err := parsePrivateKey(key)
  69. if err != nil {
  70. return err
  71. }
  72. // Create the main data signature.
  73. rawSig := ed25519.Sign(skey, filedata)
  74. var dataSig []byte
  75. dataSig = append(dataSig, header...)
  76. dataSig = append(dataSig, keyNum...)
  77. dataSig = append(dataSig, rawSig...)
  78. // Create the comment signature.
  79. var commentSigInput []byte
  80. commentSigInput = append(commentSigInput, rawSig...)
  81. commentSigInput = append(commentSigInput, []byte(trustedComment)...)
  82. commentSig := ed25519.Sign(skey, commentSigInput)
  83. // Create the output file.
  84. var out = new(bytes.Buffer)
  85. fmt.Fprintln(out, "untrusted comment:", untrustedComment)
  86. fmt.Fprintln(out, base64.StdEncoding.EncodeToString(dataSig))
  87. fmt.Fprintln(out, "trusted comment:", trustedComment)
  88. fmt.Fprintln(out, base64.StdEncoding.EncodeToString(commentSig))
  89. return ioutil.WriteFile(output, out.Bytes(), 0644)
  90. }