MaterialsProvider.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Aws\Crypto;
  3. abstract class MaterialsProvider implements MaterialsProviderInterface
  4. {
  5. private static $supportedKeySizes = [
  6. 128 => true,
  7. 192 => true,
  8. 256 => true,
  9. ];
  10. /**
  11. * Returns if the requested size is supported by AES.
  12. *
  13. * @param int $keySize Size of the requested key in bits.
  14. *
  15. * @return bool
  16. */
  17. public static function isSupportedKeySize($keySize)
  18. {
  19. return isset(self::$supportedKeySizes[$keySize]);
  20. }
  21. /**
  22. * Performs further initialization of the MaterialsProvider based on the
  23. * data inside the MetadataEnvelope.
  24. *
  25. * @param MetadataEnvelope $envelope A storage envelope for encryption
  26. * metadata to be read from.
  27. *
  28. * @return MaterialsProvider
  29. *
  30. * @throws \RuntimeException Thrown when there is an empty or improperly
  31. * formed materials description in the envelope.
  32. *
  33. * @internal
  34. */
  35. abstract public function fromDecryptionEnvelope(MetadataEnvelope $envelope);
  36. /**
  37. * Returns the material description for this Provider so it can be verified
  38. * by encryption mechanisms.
  39. *
  40. * @return string
  41. */
  42. abstract public function getMaterialsDescription();
  43. /**
  44. * Returns the wrap algorithm name for this Provider.
  45. *
  46. * @return string
  47. */
  48. abstract public function getWrapAlgorithmName();
  49. /**
  50. * Takes a content encryption key (CEK) and description to return an
  51. * encrypted key according to the Provider's specifications.
  52. *
  53. * @param string $unencryptedCek Key for use in encrypting other data
  54. * that itself needs to be encrypted by the
  55. * Provider.
  56. * @param string $materialDescription Material Description for use in
  57. * encrypting the $cek.
  58. *
  59. * @return string
  60. */
  61. abstract public function encryptCek($unencryptedCek, $materialDescription);
  62. /**
  63. * Takes an encrypted content encryption key (CEK) and material description
  64. * for use decrypting the key according to the Provider's specifications.
  65. *
  66. * @param string $encryptedCek Encrypted key to be decrypted by the Provider
  67. * for use decrypting other data.
  68. * @param string $materialDescription Material Description for use in
  69. * encrypting the $cek.
  70. *
  71. * @return string
  72. */
  73. abstract public function decryptCek($encryptedCek, $materialDescription);
  74. /**
  75. * @param string $keySize Length of a cipher key in bits for generating a
  76. * random content encryption key (CEK).
  77. *
  78. * @return string
  79. */
  80. public function generateCek($keySize)
  81. {
  82. return openssl_random_pseudo_bytes($keySize / 8);
  83. }
  84. /**
  85. * @param string $openSslName Cipher OpenSSL name to use for generating
  86. * an initialization vector.
  87. *
  88. * @return string
  89. */
  90. public function generateIv($openSslName)
  91. {
  92. return openssl_random_pseudo_bytes(
  93. openssl_cipher_iv_length($openSslName)
  94. );
  95. }
  96. }