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