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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // SigningMethodECDSA implements the ECDSA family of signing methods.
  14. // Expects *ecdsa.PrivateKey for signing and *ecdsa.PublicKey for verification
  15. type SigningMethodECDSA struct {
  16. Name string
  17. Hash crypto.Hash
  18. KeySize int
  19. CurveBits int
  20. }
  21. // Specific instances for EC256 and company
  22. var (
  23. SigningMethodES256 *SigningMethodECDSA
  24. SigningMethodES384 *SigningMethodECDSA
  25. SigningMethodES512 *SigningMethodECDSA
  26. )
  27. func init() {
  28. // ES256
  29. SigningMethodES256 = &SigningMethodECDSA{"ES256", crypto.SHA256, 32, 256}
  30. RegisterSigningMethod(SigningMethodES256.Alg(), func() SigningMethod {
  31. return SigningMethodES256
  32. })
  33. // ES384
  34. SigningMethodES384 = &SigningMethodECDSA{"ES384", crypto.SHA384, 48, 384}
  35. RegisterSigningMethod(SigningMethodES384.Alg(), func() SigningMethod {
  36. return SigningMethodES384
  37. })
  38. // ES512
  39. SigningMethodES512 = &SigningMethodECDSA{"ES512", crypto.SHA512, 66, 521}
  40. RegisterSigningMethod(SigningMethodES512.Alg(), func() SigningMethod {
  41. return SigningMethodES512
  42. })
  43. }
  44. func (m *SigningMethodECDSA) Alg() string {
  45. return m.Name
  46. }
  47. // Verify implements token verification for the SigningMethod.
  48. // For this verify method, key must be an ecdsa.PublicKey struct
  49. func (m *SigningMethodECDSA) Verify(signingString string, sig []byte, key interface{}) error {
  50. // Get the key
  51. var ecdsaKey *ecdsa.PublicKey
  52. switch k := key.(type) {
  53. case *ecdsa.PublicKey:
  54. ecdsaKey = k
  55. default:
  56. return newError("ECDSA verify expects *ecsda.PublicKey", ErrInvalidKeyType)
  57. }
  58. if len(sig) != 2*m.KeySize {
  59. return ErrECDSAVerification
  60. }
  61. r := big.NewInt(0).SetBytes(sig[:m.KeySize])
  62. s := big.NewInt(0).SetBytes(sig[m.KeySize:])
  63. // Create hasher
  64. if !m.Hash.Available() {
  65. return ErrHashUnavailable
  66. }
  67. hasher := m.Hash.New()
  68. hasher.Write([]byte(signingString))
  69. // Verify the signature
  70. if verifystatus := ecdsa.Verify(ecdsaKey, hasher.Sum(nil), r, s); verifystatus {
  71. return nil
  72. }
  73. return ErrECDSAVerification
  74. }
  75. // Sign implements token signing for the SigningMethod.
  76. // For this signing method, key must be an ecdsa.PrivateKey struct
  77. func (m *SigningMethodECDSA) Sign(signingString string, key interface{}) ([]byte, error) {
  78. // Get the key
  79. var ecdsaKey *ecdsa.PrivateKey
  80. switch k := key.(type) {
  81. case *ecdsa.PrivateKey:
  82. ecdsaKey = k
  83. default:
  84. return nil, newError("ECDSA sign expects *ecsda.PrivateKey", ErrInvalidKeyType)
  85. }
  86. // Create the hasher
  87. if !m.Hash.Available() {
  88. return nil, ErrHashUnavailable
  89. }
  90. hasher := m.Hash.New()
  91. hasher.Write([]byte(signingString))
  92. // Sign the string and return r, s
  93. if r, s, err := ecdsa.Sign(rand.Reader, ecdsaKey, hasher.Sum(nil)); err == nil {
  94. curveBits := ecdsaKey.Curve.Params().BitSize
  95. if m.CurveBits != curveBits {
  96. return nil, ErrInvalidKey
  97. }
  98. keyBytes := curveBits / 8
  99. if curveBits%8 > 0 {
  100. keyBytes += 1
  101. }
  102. // We serialize the outputs (r and s) into big-endian byte arrays
  103. // padded with zeros on the left to make sure the sizes work out.
  104. // Output must be 2*keyBytes long.
  105. out := make([]byte, 2*keyBytes)
  106. r.FillBytes(out[0:keyBytes]) // r is assigned to the first half of output.
  107. s.FillBytes(out[keyBytes:]) // s is assigned to the second half of output.
  108. return out, nil
  109. } else {
  110. return nil, err
  111. }
  112. }