View source
<?php
$plugin = encrypt_openssl_encrypt_encryption_methods();
function encrypt_openssl_encrypt_encryption_methods() {
return array(
'title' => t('AES (OpenSSL) + HMAC-SHA256'),
'description' => t('Uses AES-128-CBC via OpenSSL along with HMAC-SHA256.'),
'encrypt callback' => '_encrypt_encryption_methods_openssl',
'dependency callback' => '_encrypt_openssl_extension_is_present',
);
}
function _encrypt_encryption_methods_openssl($op, $text, $key, $options = array(), $method_settings = array()) {
$disable_base64 = array_key_exists('base64', $options) && $options['base64'] == FALSE;
$method = 'AES-128-CBC';
$iv_size = openssl_cipher_iv_length($method);
$hash_function = 'sha256';
$allowed_key_sizes = array(
16,
24,
32,
);
$key_size = strlen($key);
$salt_size = 32;
$hmac_size = 32;
if (empty($key) || !in_array($key_size, $allowed_key_sizes)) {
$t_args = array(
'@action' => $op == 'decrypt' ? t('Decryption') : t('Encryption'),
);
drupal_set_message(t('@action failed because the key is not the right size.', $t_args), 'error');
watchdog('encrypt', '@action failed because the key is not the right size.', $t_args, WATCHDOG_CRITICAL);
throw new Exception(t('@action failed because the key is not the right size.', $t_args));
}
if ($op == 'decrypt') {
if (!$disable_base64) {
$text = base64_decode($text);
}
$hmac = substr($text, 0, $hmac_size);
$text = substr($text, $hmac_size);
$salt = substr($text, 0, $salt_size);
$esalt = substr($salt, 0, strlen($salt) / 2);
$asalt = substr($salt, strlen($salt) / 2);
$akey = _encrypt_encryption_methods_openssl_hkdf($hash_function, $key, $key_size, $asalt);
$calculated_hmac = _encrypt_encryption_methods_openssl_hkdf($hash_function, $text, $hmac_size, $akey);
if ($calculated_hmac != $hmac) {
drupal_set_message(t('Decryption failed because the HMAC could not be validated.'), 'error');
watchdog('encrypt', 'Decryption failed because the HMAC could not be validated.', array(), WATCHDOG_CRITICAL);
throw new Exception(t('Decryption failed because the HMAC could not be validated.'));
}
$text = substr($text, $salt_size);
$iv = substr($text, 0, $iv_size);
$text = substr($text, $iv_size);
$ekey = _encrypt_encryption_methods_openssl_hkdf($hash_function, $key, $key_size, $esalt);
$processed_text = openssl_decrypt($text, $method, $ekey, OPENSSL_RAW_DATA, $iv);
}
else {
$iv = openssl_random_pseudo_bytes($iv_size);
$salt = drupal_random_bytes($salt_size);
$esalt = substr($salt, 0, strlen($salt) / 2);
$asalt = substr($salt, strlen($salt) / 2);
$ekey = _encrypt_encryption_methods_openssl_hkdf($hash_function, $key, $key_size, $esalt);
$processed_text = openssl_encrypt($text, $method, $ekey, OPENSSL_RAW_DATA, $iv);
$processed_text = $salt . $iv . $processed_text;
$akey = _encrypt_encryption_methods_openssl_hkdf($hash_function, $key, $key_size, $asalt);
$hmac = _encrypt_encryption_methods_openssl_hkdf($hash_function, $processed_text, $hmac_size, $akey);
$processed_text = $hmac . $processed_text;
if (!$disable_base64) {
$processed_text = base64_encode($processed_text);
}
}
return $processed_text;
}
function _encrypt_encryption_methods_openssl_hkdf($hash_function, $ikm, $length, $salt) {
$key = hash_hmac($hash_function, $ikm, $salt, TRUE);
$key = substr($key, 0, $length);
return $key;
}
function _encrypt_openssl_extension_is_present() {
$errors = array();
if (!function_exists('openssl_encrypt')) {
$errors[] = t('OpenSSL library not installed.');
}
return $errors;
}