AbstractCryptoClientV2.php 3.9 KB

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