| 1 |
<?php |
| 2 |
namespace Aws\Crypto; |
| 3 |
|
| 4 |
use Aws\S3\Crypto\S3EncryptionClientV3; |
| 5 |
|
| 6 |
enum AlgorithmSuite: int |
| 7 |
{ |
| 8 |
case ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY = 0x0073; |
| 9 |
case ALG_AES_256_GCM_IV12_TAG16_NO_KDF = 0x0072; |
| 10 |
case ALG_AES_256_CBC_IV16_NO_KDF = 0x0070; |
| 11 |
|
| 12 |
public function getId(): int |
| 13 |
{ |
| 14 |
return $this->value; |
| 15 |
} |
| 16 |
|
| 17 |
public function isLegacy(): bool |
| 18 |
{ |
| 19 |
return match ($this) { |
| 20 |
self::ALG_AES_256_CBC_IV16_NO_KDF => true, |
| 21 |
default => false, |
| 22 |
}; |
| 23 |
} |
| 24 |
|
| 25 |
public function isKeyCommitting(): bool |
| 26 |
{ |
| 27 |
return match ($this) { |
| 28 |
self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY => true, |
| 29 |
default => false, |
| 30 |
}; |
| 31 |
} |
| 32 |
|
| 33 |
public function getDataKeyAlgorithm(): string |
| 34 |
{ |
| 35 |
return match ($this) { |
| 36 |
self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY, |
| 37 |
self::ALG_AES_256_GCM_IV12_TAG16_NO_KDF, |
| 38 |
self::ALG_AES_256_CBC_IV16_NO_KDF => "AES", |
| 39 |
}; |
| 40 |
} |
| 41 |
|
| 42 |
public function getDataKeyLengthBits(): string |
| 43 |
{ |
| 44 |
return match ($this) { |
| 45 |
self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY, |
| 46 |
self::ALG_AES_256_GCM_IV12_TAG16_NO_KDF, |
| 47 |
self::ALG_AES_256_CBC_IV16_NO_KDF => "256", |
| 48 |
}; |
| 49 |
} |
| 50 |
|
| 51 |
public function getCipherName(): string |
| 52 |
{ |
| 53 |
return match ($this) { |
| 54 |
self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY, |
| 55 |
self::ALG_AES_256_GCM_IV12_TAG16_NO_KDF => "gcm", |
| 56 |
self::ALG_AES_256_CBC_IV16_NO_KDF => "cbc", |
| 57 |
}; |
| 58 |
} |
| 59 |
|
| 60 |
public function getCipherBlockSizeBits(): int |
| 61 |
{ |
| 62 |
return match ($this) { |
| 63 |
self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY, |
| 64 |
self::ALG_AES_256_GCM_IV12_TAG16_NO_KDF, |
| 65 |
self::ALG_AES_256_CBC_IV16_NO_KDF => 128, |
| 66 |
}; |
| 67 |
} |
| 68 |
|
| 69 |
public function getCipherBlockSizeBytes(): int |
| 70 |
{ |
| 71 |
return $this->getCipherBlockSizeBits() / 8; |
| 72 |
} |
| 73 |
|
| 74 |
public function getIvLengthBits(): int |
| 75 |
{ |
| 76 |
return match ($this) { |
| 77 |
self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY, |
| 78 |
self::ALG_AES_256_GCM_IV12_TAG16_NO_KDF => 96, |
| 79 |
self::ALG_AES_256_CBC_IV16_NO_KDF => 128, |
| 80 |
}; |
| 81 |
} |
| 82 |
|
| 83 |
public function getIvLengthBytes(): int |
| 84 |
{ |
| 85 |
return $this->getIvLengthBits() / 8; |
| 86 |
} |
| 87 |
|
| 88 |
public function getCipherTagLengthBits(): int |
| 89 |
{ |
| 90 |
return match ($this) { |
| 91 |
self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY, |
| 92 |
self::ALG_AES_256_GCM_IV12_TAG16_NO_KDF => 128, |
| 93 |
self::ALG_AES_256_CBC_IV16_NO_KDF => 0, |
| 94 |
}; |
| 95 |
} |
| 96 |
|
| 97 |
public function getCipherTagLengthInBytes(): int |
| 98 |
{ |
| 99 |
return $this->getCipherTagLengthBits() / 8; |
| 100 |
} |
| 101 |
|
| 102 |
public function getCipherMaxContentLengthBits(): int |
| 103 |
{ |
| 104 |
return match ($this) { |
| 105 |
self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY, |
| 106 |
self::ALG_AES_256_GCM_IV12_TAG16_NO_KDF => AlgorithmConstants::GCM_MAX_CONTENT_LENGTH_BITS, |
| 107 |
self::ALG_AES_256_CBC_IV16_NO_KDF => AlgorithmConstants::CBC_MAX_CONTENT_LENGTH_BYTES, |
| 108 |
}; |
| 109 |
} |
| 110 |
|
| 111 |
public function getCipherMaxContentLengthBytes(): int |
| 112 |
{ |
| 113 |
return $this->getCipherMaxContentLengthBits() / 8; |
| 114 |
} |
| 115 |
|
| 116 |
public function getHashingAlgorithm(): string |
| 117 |
{ |
| 118 |
return match ($this) { |
| 119 |
self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY => "sha512", |
| 120 |
default => "", |
| 121 |
}; |
| 122 |
} |
| 123 |
|
| 124 |
public function getDerivationInputKeyLengthBits(): int |
| 125 |
{ |
| 126 |
return match ($this) { |
| 127 |
self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY => 256, |
| 128 |
default => 0, |
| 129 |
}; |
| 130 |
} |
| 131 |
|
| 132 |
public function getDerivationInputKeyLengthBytes(): int |
| 133 |
{ |
| 134 |
return $this->getDerivationInputKeyLengthBits() / 8; |
| 135 |
} |
| 136 |
|
| 137 |
public function getDerivationOutputKeyLengthBits(): int |
| 138 |
{ |
| 139 |
return match ($this) { |
| 140 |
self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY => 256, |
| 141 |
default => 0, |
| 142 |
}; |
| 143 |
} |
| 144 |
|
| 145 |
public function getDerivationOutputKeyLengthBytes(): int |
| 146 |
{ |
| 147 |
return $this->getDerivationOutputKeyLengthBits() / 8; |
| 148 |
} |
| 149 |
|
| 150 |
public function getCommitmentInputKeyLengthBits(): int |
| 151 |
{ |
| 152 |
return match ($this) { |
| 153 |
self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY => 256, |
| 154 |
default => 0, |
| 155 |
}; |
| 156 |
} |
| 157 |
|
| 158 |
public function getCommitmentInputKeyLengthBytes(): int |
| 159 |
{ |
| 160 |
return $this->getCommitmentInputKeyLengthBits() / 8; |
| 161 |
} |
| 162 |
|
| 163 |
public function getCommitmentOutputKeyLengthBits(): int |
| 164 |
{ |
| 165 |
return match ($this) { |
| 166 |
self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY => 224, |
| 167 |
default => 0, |
| 168 |
}; |
| 169 |
} |
| 170 |
|
| 171 |
public function getCommitmentOutputKeyLengthBytes(): int |
| 172 |
{ |
| 173 |
return $this->getCommitmentOutputKeyLengthBits() / 8; |
| 174 |
} |
| 175 |
|
| 176 |
public function getKeyCommitmentSaltLengthBits(): int |
| 177 |
{ |
| 178 |
return match ($this) { |
| 179 |
self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY => 224, |
| 180 |
default => 0, |
| 181 |
}; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public static function validateCommitmentPolicyOnEncrypt( |
| 187 |
array $cipherOptions, |
| 188 |
string $keyCommitmentPolicy |
| 189 |
): self { |
| 190 |
$cipherOptions['Cipher'] = strtolower($cipherOptions['Cipher']); |
| 191 |
|
| 192 |
|
| 193 |
if (!S3EncryptionClientV3::isSupportedCipher($cipherOptions['Cipher'])) { |
| 194 |
|
| 195 |
|
| 196 |
throw new \InvalidArgumentException('The cipher requested is not' |
| 197 |
. ' supported by the SDK.'); |
| 198 |
} |
| 199 |
|
| 200 |
if (empty($cipherOptions['KeySize'])) { |
| 201 |
$cipherOptions['KeySize'] = 256; |
| 202 |
} |
| 203 |
|
| 204 |
if (!is_int($cipherOptions['KeySize'])) { |
| 205 |
throw new \InvalidArgumentException('The cipher "KeySize" must be' |
| 206 |
. ' an integer.'); |
| 207 |
} |
| 208 |
|
| 209 |
if (!MaterialsProviderV3::isSupportedKeySize($cipherOptions['KeySize'])) { |
| 210 |
throw new \InvalidArgumentException('The cipher "KeySize" requested' |
| 211 |
. ' is not supported by AES (256).'); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
if ($keyCommitmentPolicy === 'FORBID_ENCRYPT_ALLOW_DECRYPT') { |
| 217 |
return self::ALG_AES_256_GCM_IV12_TAG16_NO_KDF; |
| 218 |
} else { |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
return self::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY; |
| 225 |
} |
| 226 |
} |
| 227 |
} |