AbstractCryptoClient.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Aws\Crypto;
  3. use Aws\Crypto\Cipher\CipherMethod;
  4. use Aws\Crypto\Cipher\Cbc;
  5. use GuzzleHttp\Psr7\Stream;
  6. /**
  7. * Legacy abstract encryption client. New workflows should use
  8. * AbstractCryptoClientV2.
  9. *
  10. * @deprecated
  11. * @internal
  12. */
  13. abstract class AbstractCryptoClient
  14. {
  15. public static $supportedCiphers = ['cbc', 'gcm'];
  16. public static $supportedKeyWraps = [
  17. KmsMaterialsProvider::WRAP_ALGORITHM_NAME
  18. ];
  19. /**
  20. * Returns if the passed cipher name is supported for encryption by the SDK.
  21. *
  22. * @param string $cipherName The name of a cipher to verify is registered.
  23. *
  24. * @return bool If the cipher passed is in our supported list.
  25. */
  26. public static function isSupportedCipher($cipherName)
  27. {
  28. return in_array($cipherName, self::$supportedCiphers);
  29. }
  30. /**
  31. * Returns an identifier recognizable by `openssl_*` functions, such as
  32. * `aes-256-cbc` or `aes-128-ctr`.
  33. *
  34. * @param string $cipherName Name of the cipher being used for encrypting
  35. * or decrypting.
  36. * @param int $keySize Size of the encryption key, in bits, that will be
  37. * used.
  38. *
  39. * @return string
  40. */
  41. abstract protected function getCipherOpenSslName($cipherName, $keySize);
  42. /**
  43. * Constructs a CipherMethod for the given name, initialized with the other
  44. * data passed for use in encrypting or decrypting.
  45. *
  46. * @param string $cipherName Name of the cipher to generate for encrypting.
  47. * @param string $iv Base Initialization Vector for the cipher.
  48. * @param int $keySize Size of the encryption key, in bits, that will be
  49. * used.
  50. *
  51. * @return CipherMethod
  52. *
  53. * @internal
  54. */
  55. abstract protected function buildCipherMethod($cipherName, $iv, $keySize);
  56. /**
  57. * Performs a reverse lookup to get the openssl_* cipher name from the
  58. * AESName passed in from the MetadataEnvelope.
  59. *
  60. * @param $aesName
  61. *
  62. * @return string
  63. *
  64. * @internal
  65. */
  66. abstract protected function getCipherFromAesName($aesName);
  67. /**
  68. * Dependency to provide an interface for building an encryption stream for
  69. * data given cipher details, metadata, and materials to do so.
  70. *
  71. * @param Stream $plaintext Plain-text data to be encrypted using the
  72. * materials, algorithm, and data provided.
  73. * @param array $cipherOptions Options for use in determining the cipher to
  74. * be used for encrypting data.
  75. * @param MaterialsProvider $provider A provider to supply and encrypt
  76. * materials used in encryption.
  77. * @param MetadataEnvelope $envelope A storage envelope for encryption
  78. * metadata to be added to.
  79. *
  80. * @return AesStreamInterface
  81. *
  82. * @internal
  83. */
  84. abstract public function encrypt(
  85. Stream $plaintext,
  86. array $cipherOptions,
  87. MaterialsProvider $provider,
  88. MetadataEnvelope $envelope
  89. );
  90. /**
  91. * Dependency to provide an interface for building a decryption stream for
  92. * cipher text given metadata and materials to do so.
  93. *
  94. * @param string $cipherText Plain-text data to be decrypted using the
  95. * materials, algorithm, and data provided.
  96. * @param MaterialsProviderInterface $provider A provider to supply and encrypt
  97. * materials used in encryption.
  98. * @param MetadataEnvelope $envelope A storage envelope for encryption
  99. * metadata to be read from.
  100. * @param array $cipherOptions Additional verification options.
  101. *
  102. * @return AesStreamInterface
  103. *
  104. * @internal
  105. */
  106. abstract public function decrypt(
  107. $cipherText,
  108. MaterialsProviderInterface $provider,
  109. MetadataEnvelope $envelope,
  110. array $cipherOptions = []
  111. );
  112. }