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