ecdh_es.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*-
  2. * Copyright 2014 Square Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package josecipher
  17. import (
  18. "bytes"
  19. "crypto"
  20. "crypto/ecdsa"
  21. "crypto/elliptic"
  22. "encoding/binary"
  23. )
  24. // DeriveECDHES derives a shared encryption key using ECDH/ConcatKDF as described in JWE/JWA.
  25. // It is an error to call this function with a private/public key that are not on the same
  26. // curve. Callers must ensure that the keys are valid before calling this function. Output
  27. // size may be at most 1<<16 bytes (64 KiB).
  28. func DeriveECDHES(alg string, apuData, apvData []byte, priv *ecdsa.PrivateKey, pub *ecdsa.PublicKey, size int) []byte {
  29. if size > 1<<16 {
  30. panic("ECDH-ES output size too large, must be less than or equal to 1<<16")
  31. }
  32. // algId, partyUInfo, partyVInfo inputs must be prefixed with the length
  33. algID := lengthPrefixed([]byte(alg))
  34. ptyUInfo := lengthPrefixed(apuData)
  35. ptyVInfo := lengthPrefixed(apvData)
  36. // suppPubInfo is the encoded length of the output size in bits
  37. supPubInfo := make([]byte, 4)
  38. binary.BigEndian.PutUint32(supPubInfo, uint32(size)*8)
  39. if !priv.PublicKey.Curve.IsOnCurve(pub.X, pub.Y) {
  40. panic("public key not on same curve as private key")
  41. }
  42. z, _ := priv.Curve.ScalarMult(pub.X, pub.Y, priv.D.Bytes())
  43. zBytes := z.Bytes()
  44. // Note that calling z.Bytes() on a big.Int may strip leading zero bytes from
  45. // the returned byte array. This can lead to a problem where zBytes will be
  46. // shorter than expected which breaks the key derivation. Therefore we must pad
  47. // to the full length of the expected coordinate here before calling the KDF.
  48. octSize := dSize(priv.Curve)
  49. if len(zBytes) != octSize {
  50. zBytes = append(bytes.Repeat([]byte{0}, octSize-len(zBytes)), zBytes...)
  51. }
  52. reader := NewConcatKDF(crypto.SHA256, zBytes, algID, ptyUInfo, ptyVInfo, supPubInfo, []byte{})
  53. key := make([]byte, size)
  54. // Read on the KDF will never fail
  55. _, _ = reader.Read(key)
  56. return key
  57. }
  58. // dSize returns the size in octets for a coordinate on a elliptic curve.
  59. func dSize(curve elliptic.Curve) int {
  60. order := curve.Params().P
  61. bitLen := order.BitLen()
  62. size := bitLen / 8
  63. if bitLen%8 != 0 {
  64. size++
  65. }
  66. return size
  67. }
  68. func lengthPrefixed(data []byte) []byte {
  69. out := make([]byte, len(data)+4)
  70. binary.BigEndian.PutUint32(out, uint32(len(data)))
  71. copy(out[4:], data)
  72. return out
  73. }