1
0
mirror of https://github.com/Yubico/yubikey-ksm.git synced 2024-12-11 09:24:14 +01:00

implementing openssl decrypt block found in https://github.com/Yubico/yubikey-ksm/issues/24

This commit is contained in:
theonlydoo 2019-08-13 14:21:09 +02:00
parent c1cb3e1dd8
commit a31bfb9211

View File

@ -33,7 +33,9 @@ function yubi_hex2bin($h)
if (!is_string($h)) return null;
$r = '';
for ($a = 0; $a < strlen($h); $a += 2) {
$r.=chr(hexdec($h{$a}.$h{($a+1)}));
$r .= chr(hexdec($h{
$a} . $h{
($a + 1)}));
}
return $r;
}
@ -45,12 +47,19 @@ function modhex2hex($m)
function aes128ecb_decrypt($key, $in)
{
if (extension_loaded('openssl') && function_exists('openssl_decrypt') && in_array('aes-128-ecb', openssl_get_cipher_methods())) {
$result = openssl_decrypt(yubi_hex2bin($in), 'aes-128-ecb', yubi_hex2bin($key), OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
if ($result !== false)
$result = bin2hex($result);
else
$result = '';
} else {
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'ecb', '');
$iv = yubi_hex2bin('00000000000000000000000000000000');
mcrypt_generic_init($td, yubi_hex2bin($key), $iv);
$result = bin2hex(mdecrypt_generic($td, yubi_hex2bin($in)));
mcrypt_generic_deinit($td);
}
return $result;
}
@ -72,9 +81,8 @@ function calculate_crc($token)
return $crc;
}
function crc_is_good($token) {
function crc_is_good($token)
{
$crc = calculate_crc($token);
return $crc == 0xf0b8;
}
?>