1
0
mirror of https://github.com/Yubico/yubikey-ksm.git synced 2024-12-03 21:24:19 +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

@ -31,35 +31,44 @@
function yubi_hex2bin($h) function yubi_hex2bin($h)
{ {
if (!is_string($h)) return null; if (!is_string($h)) return null;
$r=''; $r = '';
for ($a=0; $a<strlen($h); $a+=2) { 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; return $r;
} }
function modhex2hex($m) function modhex2hex($m)
{ {
return strtr ($m, "cbdefghijklnrtuv", "0123456789abcdef"); return strtr($m, "cbdefghijklnrtuv", "0123456789abcdef");
} }
function aes128ecb_decrypt($key,$in) function aes128ecb_decrypt($key, $in)
{ {
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'ecb', ''); if (extension_loaded('openssl') && function_exists('openssl_decrypt') && in_array('aes-128-ecb', openssl_get_cipher_methods())) {
$iv = yubi_hex2bin('00000000000000000000000000000000'); $result = openssl_decrypt(yubi_hex2bin($in), 'aes-128-ecb', yubi_hex2bin($key), OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
mcrypt_generic_init($td, yubi_hex2bin($key), $iv); if ($result !== false)
$result = bin2hex(mdecrypt_generic($td, yubi_hex2bin($in))); $result = bin2hex($result);
mcrypt_generic_deinit($td); 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; return $result;
} }
function calculate_crc($token) function calculate_crc($token)
{ {
$crc = 0xffff; $crc = 0xffff;
for ($i = 0; $i < 16; $i++ ) { for ($i = 0; $i < 16; $i++) {
$b = hexdec($token[$i*2].$token[($i*2)+1]); $b = hexdec($token[$i * 2] . $token[($i * 2) + 1]);
$crc = $crc ^ ($b & 0xff); $crc = $crc ^ ($b & 0xff);
for ($j = 0; $j < 8; $j++) { for ($j = 0; $j < 8; $j++) {
$n = $crc & 1; $n = $crc & 1;
@ -72,9 +81,8 @@ function calculate_crc($token)
return $crc; return $crc;
} }
function crc_is_good($token) { function crc_is_good($token)
{
$crc = calculate_crc($token); $crc = calculate_crc($token);
return $crc == 0xf0b8; return $crc == 0xf0b8;
} }
?>