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