本站源代码
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.

105 lines
2.7KB

  1. package packet
  2. import (
  3. "bytes"
  4. "io"
  5. "math/big"
  6. "github.com/keybase/go-crypto/openpgp/ecdh"
  7. "github.com/keybase/go-crypto/openpgp/errors"
  8. "github.com/keybase/go-crypto/openpgp/s2k"
  9. )
  10. // ECDHKdfParams generates KDF parameters sequence for given
  11. // PublicKey. See https://tools.ietf.org/html/rfc6637#section-8
  12. func ECDHKdfParams(pub *PublicKey) []byte {
  13. buf := new(bytes.Buffer)
  14. oid := pub.ec.oid
  15. buf.WriteByte(byte(len(oid)))
  16. buf.Write(oid)
  17. buf.WriteByte(18) // ECDH TYPE
  18. pub.ecdh.serialize(buf)
  19. buf.WriteString("Anonymous Sender ")
  20. buf.Write(pub.Fingerprint[:])
  21. return buf.Bytes()
  22. }
  23. func decryptKeyECDH(priv *PrivateKey, X, Y *big.Int, C []byte) (out []byte, err error) {
  24. ecdhpriv, ok := priv.PrivateKey.(*ecdh.PrivateKey)
  25. if !ok {
  26. return nil, errors.InvalidArgumentError("bad internal ECDH key")
  27. }
  28. Sx := ecdhpriv.DecryptShared(X, Y)
  29. kdfParams := ECDHKdfParams(&priv.PublicKey)
  30. hash, ok := s2k.HashIdToHash(byte(priv.ecdh.KdfHash))
  31. if !ok {
  32. return nil, errors.InvalidArgumentError("invalid hash id in private key")
  33. }
  34. key := ecdhpriv.KDF(Sx, kdfParams, hash)
  35. keySize := CipherFunction(priv.ecdh.KdfAlgo).KeySize()
  36. decrypted, err := ecdh.AESKeyUnwrap(key[:keySize], C)
  37. if err != nil {
  38. return nil, err
  39. }
  40. // We have to "read ahead" to discover real length of the
  41. // encryption key and properly unpad buffer.
  42. cipherFunc := CipherFunction(decrypted[0])
  43. // +3 bytes = 1-byte cipher id and checksum 2-byte checksum.
  44. out = ecdh.UnpadBuffer(decrypted, cipherFunc.KeySize()+3)
  45. if out == nil {
  46. return nil, errors.InvalidArgumentError("invalid padding while ECDH")
  47. }
  48. return out, nil
  49. }
  50. func serializeEncryptedKeyECDH(w io.Writer, rand io.Reader, header [10]byte, pub *PublicKey, keyBlock []byte) error {
  51. ecdhpub := pub.PublicKey.(*ecdh.PublicKey)
  52. kdfParams := ECDHKdfParams(pub)
  53. hash, ok := s2k.HashIdToHash(byte(pub.ecdh.KdfHash))
  54. if !ok {
  55. return errors.InvalidArgumentError("invalid hash id in private key")
  56. }
  57. kdfKeySize := CipherFunction(pub.ecdh.KdfAlgo).KeySize()
  58. Vx, Vy, C, err := ecdhpub.Encrypt(rand, kdfParams, keyBlock, hash, kdfKeySize)
  59. if err != nil {
  60. return err
  61. }
  62. mpis, mpiBitLen := ecdh.Marshal(ecdhpub.Curve, Vx, Vy)
  63. packetLen := len(header) /* header length in bytes */
  64. packetLen += 2 /* mpi length in bits */ + len(mpis)
  65. packetLen += 1 /* ciphertext size in bytes */ + len(C)
  66. err = serializeHeader(w, packetTypeEncryptedKey, packetLen)
  67. if err != nil {
  68. return err
  69. }
  70. _, err = w.Write(header[:])
  71. if err != nil {
  72. return err
  73. }
  74. _, err = w.Write([]byte{byte(mpiBitLen >> 8), byte(mpiBitLen)})
  75. if err != nil {
  76. return err
  77. }
  78. _, err = w.Write(mpis[:])
  79. if err != nil {
  80. return err
  81. }
  82. w.Write([]byte{byte(len(C))})
  83. w.Write(C[:])
  84. return nil
  85. }
上海开阖软件有限公司 沪ICP备12045867号-1