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.

ecdsa.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package jwt
  2. import (
  3. "crypto"
  4. "crypto/ecdsa"
  5. "crypto/rand"
  6. "errors"
  7. "math/big"
  8. )
  9. var (
  10. // Sadly this is missing from crypto/ecdsa compared to crypto/rsa
  11. ErrECDSAVerification = errors.New("crypto/ecdsa: verification error")
  12. )
  13. // Implements the ECDSA family of signing methods signing methods
  14. type SigningMethodECDSA struct {
  15. Name string
  16. Hash crypto.Hash
  17. KeySize int
  18. CurveBits int
  19. }
  20. // Specific instances for EC256 and company
  21. var (
  22. SigningMethodES256 *SigningMethodECDSA
  23. SigningMethodES384 *SigningMethodECDSA
  24. SigningMethodES512 *SigningMethodECDSA
  25. )
  26. func init() {
  27. // ES256
  28. SigningMethodES256 = &SigningMethodECDSA{"ES256", crypto.SHA256, 32, 256}
  29. RegisterSigningMethod(SigningMethodES256.Alg(), func() SigningMethod {
  30. return SigningMethodES256
  31. })
  32. // ES384
  33. SigningMethodES384 = &SigningMethodECDSA{"ES384", crypto.SHA384, 48, 384}
  34. RegisterSigningMethod(SigningMethodES384.Alg(), func() SigningMethod {
  35. return SigningMethodES384
  36. })
  37. // ES512
  38. SigningMethodES512 = &SigningMethodECDSA{"ES512", crypto.SHA512, 66, 521}
  39. RegisterSigningMethod(SigningMethodES512.Alg(), func() SigningMethod {
  40. return SigningMethodES512
  41. })
  42. }
  43. func (m *SigningMethodECDSA) Alg() string {
  44. return m.Name
  45. }
  46. // Implements the Verify method from SigningMethod
  47. // For this verify method, key must be an ecdsa.PublicKey struct
  48. func (m *SigningMethodECDSA) Verify(signingString, signature string, key interface{}) error {
  49. var err error
  50. // Decode the signature
  51. var sig []byte
  52. if sig, err = DecodeSegment(signature); err != nil {
  53. return err
  54. }
  55. // Get the key
  56. var ecdsaKey *ecdsa.PublicKey
  57. switch k := key.(type) {
  58. case *ecdsa.PublicKey:
  59. ecdsaKey = k
  60. default:
  61. return ErrInvalidKeyType
  62. }
  63. if len(sig) != 2*m.KeySize {
  64. return ErrECDSAVerification
  65. }
  66. r := big.NewInt(0).SetBytes(sig[:m.KeySize])
  67. s := big.NewInt(0).SetBytes(sig[m.KeySize:])
  68. // Create hasher
  69. if !m.Hash.Available() {
  70. return ErrHashUnavailable
  71. }
  72. hasher := m.Hash.New()
  73. hasher.Write([]byte(signingString))
  74. // Verify the signature
  75. if verifystatus := ecdsa.Verify(ecdsaKey, hasher.Sum(nil), r, s); verifystatus == true {
  76. return nil
  77. } else {
  78. return ErrECDSAVerification
  79. }
  80. }
  81. // Implements the Sign method from SigningMethod
  82. // For this signing method, key must be an ecdsa.PrivateKey struct
  83. func (m *SigningMethodECDSA) Sign(signingString string, key interface{}) (string, error) {
  84. // Get the key
  85. var ecdsaKey *ecdsa.PrivateKey
  86. switch k := key.(type) {
  87. case *ecdsa.PrivateKey:
  88. ecdsaKey = k
  89. default:
  90. return "", ErrInvalidKeyType
  91. }
  92. // Create the hasher
  93. if !m.Hash.Available() {
  94. return "", ErrHashUnavailable
  95. }
  96. hasher := m.Hash.New()
  97. hasher.Write([]byte(signingString))
  98. // Sign the string and return r, s
  99. if r, s, err := ecdsa.Sign(rand.Reader, ecdsaKey, hasher.Sum(nil)); err == nil {
  100. curveBits := ecdsaKey.Curve.Params().BitSize
  101. if m.CurveBits != curveBits {
  102. return "", ErrInvalidKey
  103. }
  104. keyBytes := curveBits / 8
  105. if curveBits%8 > 0 {
  106. keyBytes += 1
  107. }
  108. // We serialize the outpus (r and s) into big-endian byte arrays and pad
  109. // them with zeros on the left to make sure the sizes work out. Both arrays
  110. // must be keyBytes long, and the output must be 2*keyBytes long.
  111. rBytes := r.Bytes()
  112. rBytesPadded := make([]byte, keyBytes)
  113. copy(rBytesPadded[keyBytes-len(rBytes):], rBytes)
  114. sBytes := s.Bytes()
  115. sBytesPadded := make([]byte, keyBytes)
  116. copy(sBytesPadded[keyBytes-len(sBytes):], sBytes)
  117. out := append(rBytesPadded, sBytesPadded...)
  118. return EncodeSegment(out), nil
  119. } else {
  120. return "", err
  121. }
  122. }