interface.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package keyenc
  2. import (
  3. "crypto/rsa"
  4. "hash"
  5. "github.com/lestrrat-go/jwx/jwa"
  6. "github.com/lestrrat-go/jwx/jwe/internal/keygen"
  7. )
  8. // Encrypter is an interface for things that can encrypt keys
  9. type Encrypter interface {
  10. Algorithm() jwa.KeyEncryptionAlgorithm
  11. Encrypt([]byte) (keygen.ByteSource, error)
  12. // KeyID returns the key id for this Encrypter. This exists so that
  13. // you can pass in a Encrypter to MultiEncrypt, you can rest assured
  14. // that the generated key will have the proper key ID.
  15. KeyID() string
  16. SetKeyID(string)
  17. }
  18. // Decrypter is an interface for things that can decrypt keys
  19. type Decrypter interface {
  20. Algorithm() jwa.KeyEncryptionAlgorithm
  21. Decrypt([]byte) ([]byte, error)
  22. }
  23. type Noop struct {
  24. alg jwa.KeyEncryptionAlgorithm
  25. keyID string
  26. sharedkey []byte
  27. }
  28. // AES encrypts content encryption keys using AES key wrap.
  29. // Contrary to what the name implies, it also decrypt encrypted keys
  30. type AES struct {
  31. alg jwa.KeyEncryptionAlgorithm
  32. keyID string
  33. sharedkey []byte
  34. }
  35. // AESGCM encrypts content encryption keys using AES-GCM key wrap.
  36. type AESGCMEncrypt struct {
  37. algorithm jwa.KeyEncryptionAlgorithm
  38. keyID string
  39. sharedkey []byte
  40. }
  41. // ECDHESEncrypt encrypts content encryption keys using ECDH-ES.
  42. type ECDHESEncrypt struct {
  43. algorithm jwa.KeyEncryptionAlgorithm
  44. keyID string
  45. generator keygen.Generator
  46. }
  47. // ECDHESDecrypt decrypts keys using ECDH-ES.
  48. type ECDHESDecrypt struct {
  49. keyalg jwa.KeyEncryptionAlgorithm
  50. contentalg jwa.ContentEncryptionAlgorithm
  51. apu []byte
  52. apv []byte
  53. privkey interface{}
  54. pubkey interface{}
  55. }
  56. // RSAOAEPEncrypt encrypts keys using RSA OAEP algorithm
  57. type RSAOAEPEncrypt struct {
  58. alg jwa.KeyEncryptionAlgorithm
  59. pubkey *rsa.PublicKey
  60. keyID string
  61. }
  62. // RSAOAEPDecrypt decrypts keys using RSA OAEP algorithm
  63. type RSAOAEPDecrypt struct {
  64. alg jwa.KeyEncryptionAlgorithm
  65. privkey *rsa.PrivateKey
  66. }
  67. // RSAPKCS15Decrypt decrypts keys using RSA PKCS1v15 algorithm
  68. type RSAPKCS15Decrypt struct {
  69. alg jwa.KeyEncryptionAlgorithm
  70. privkey *rsa.PrivateKey
  71. generator keygen.Generator
  72. }
  73. // RSAPKCSEncrypt encrypts keys using RSA PKCS1v15 algorithm
  74. type RSAPKCSEncrypt struct {
  75. alg jwa.KeyEncryptionAlgorithm
  76. pubkey *rsa.PublicKey
  77. keyID string
  78. }
  79. // DirectDecrypt does no encryption (Note: Unimplemented)
  80. type DirectDecrypt struct {
  81. Key []byte
  82. }
  83. // PBES2Encrypt encrypts keys with PBES2 / PBKDF2 password
  84. type PBES2Encrypt struct {
  85. algorithm jwa.KeyEncryptionAlgorithm
  86. hashFunc func() hash.Hash
  87. keylen int
  88. keyID string
  89. password []byte
  90. }