CipherBuilderTrait.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace Aws\Crypto\Cipher;
  3. use Aws\Exception\CryptoException;
  4. trait CipherBuilderTrait
  5. {
  6. /**
  7. * Returns an identifier recognizable by `openssl_*` functions, such as
  8. * `aes-256-cbc` or `aes-128-ctr`.
  9. *
  10. * @param string $cipherName Name of the cipher being used for encrypting
  11. * or decrypting.
  12. * @param int $keySize Size of the encryption key, in bits, that will be
  13. * used.
  14. *
  15. * @return string
  16. */
  17. protected function getCipherOpenSslName($cipherName, $keySize)
  18. {
  19. return "aes-{$keySize}-{$cipherName}";
  20. }
  21. /**
  22. * Constructs a CipherMethod for the given name, initialized with the other
  23. * data passed for use in encrypting or decrypting.
  24. *
  25. * @param string $cipherName Name of the cipher to generate for encrypting.
  26. * @param string $iv Base Initialization Vector for the cipher.
  27. * @param int $keySize Size of the encryption key, in bits, that will be
  28. * used.
  29. *
  30. * @return CipherMethod
  31. *
  32. * @internal
  33. */
  34. protected function buildCipherMethod($cipherName, $iv, $keySize)
  35. {
  36. switch ($cipherName) {
  37. case 'cbc':
  38. return new Cbc(
  39. $iv,
  40. $keySize
  41. );
  42. default:
  43. return null;
  44. }
  45. }
  46. /**
  47. * Performs a reverse lookup to get the openssl_* cipher name from the
  48. * AESName passed in from the MetadataEnvelope.
  49. *
  50. * @param $aesName
  51. *
  52. * @return string
  53. *
  54. * @internal
  55. */
  56. protected function getCipherFromAesName($aesName)
  57. {
  58. switch ($aesName) {
  59. case 'AES/GCM/NoPadding':
  60. return 'gcm';
  61. case 'AES/CBC/PKCS5Padding':
  62. return 'cbc';
  63. default:
  64. throw new CryptoException('Unrecognized or unsupported'
  65. . ' AESName for reverse lookup.');
  66. }
  67. }
  68. }