| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Crypto\Cipher; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Exception\CryptoException; |
| 6 |
trait 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 |
* Constructs a CipherMethod for the given name, initialized with the other |
| 25 |
* data passed for use in encrypting or decrypting. |
| 26 |
* |
| 27 |
* @param string $cipherName Name of the cipher to generate for encrypting. |
| 28 |
* @param string $iv Base Initialization Vector for the cipher. |
| 29 |
* @param int $keySize Size of the encryption key, in bits, that will be |
| 30 |
* used. |
| 31 |
* |
| 32 |
* @return CipherMethod |
| 33 |
* |
| 34 |
* @internal |
| 35 |
*/ |
| 36 |
protected function buildCipherMethod($cipherName, $iv, $keySize) |
| 37 |
{ |
| 38 |
switch ($cipherName) { |
| 39 |
case 'cbc': |
| 40 |
return new Cbc($iv, $keySize); |
| 41 |
default: |
| 42 |
return null; |
| 43 |
} |
| 44 |
} |
| 45 |
/** |
| 46 |
* Performs a reverse lookup to get the openssl_* cipher name from the |
| 47 |
* AESName passed in from the MetadataEnvelope. |
| 48 |
* |
| 49 |
* @param $aesName |
| 50 |
* |
| 51 |
* @return string |
| 52 |
* |
| 53 |
* @internal |
| 54 |
*/ |
| 55 |
protected function getCipherFromAesName($aesName) |
| 56 |
{ |
| 57 |
switch ($aesName) { |
| 58 |
case 'AES/GCM/NoPadding': |
| 59 |
return 'gcm'; |
| 60 |
case 'AES/CBC/PKCS5Padding': |
| 61 |
return 'cbc'; |
| 62 |
default: |
| 63 |
throw new CryptoException('Unrecognized or unsupported' . ' AESName for reverse lookup.'); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|