【问题标题】:How to implement a concat KDF for use with JOSE/JWE/JWT如何实现与 JOSE/JWE/JWT 一起使用的 concat KDF
【发布时间】:2014-12-23 23:20:05
【问题描述】:

我正在尝试编写代码来解密 PHP 中的 JWE 令牌,因为现有库不支持我需要的算法(A128CBC+HS256,这是一个已弃用的算法)。

我的问题是我无法理解如何生成使用“连接密钥”的内容加密密钥 推导函数”(请参阅​​此处的第 5.8.1 节:http://csrc.nist.gov/publications/nistpubs/800-56A/SP800-56A_Revision1_Mar08-2007.pdf)。函数的符号和解释超出了我的想象。

我的价值观基于JOSE JSON web algorithms draft 06

到目前为止,我的代码的相关部分如下所示:

// Derive CBC encryption & integrity keys
$shaSize = 256;
$encryptionKeySize = $shaSize / 2;
$integrityKeySize = $shaSize;

// Calculate the key derivation using Concat KDF for the content 
// encryption key
$encryptionSegments = [
    $masterKey,         // Z
    $encryptionKeySize, // keydatalen
    $this->packInt32sBe($encryptionKeySize) . utf8_encode('A128CBC+HS256'), // AlgorithmID
    $this->packInt32sBe(0), // PartyUInfo
    $this->packInt32sBe(0), // PartyUInfo
    'Encryption',           // SuppPubInfo
    $this->packInt32sBe(1), // SuppPrivInfo
];

// Calculate the SHA256 digest
$cek = hex2bin(hash('sha256', implode('', $encryptionSegments)));

可能相关,我获取大端整数的函数:

public function packInt32sBe($n)
{
    if (pack('L', 1) === pack('N', 1)) {
        return pack('l', $n);
    }

    return strrev(pack('l', $n));
}

这里没有显示的唯一变量是$masterKey,它是解密的内容主密钥。

【问题讨论】:

    标签: php encryption jwt jwe


    【解决方案1】:

    这是我自己根据相同规范的实现,但草案 #39:

    <?php
    
    class ConcatKDF
    {
        public static function generate($Z, $encryption_algorithm, $encryption_key_size, $apu = "", $apv = "")
        {
            $encryption_segments = array(
                self::toInt32Bits(1),                                                   // Round number 1
                $Z,                                                                     // Z (shared secret)
                self::toInt32Bits(strlen($encryption_algorithm)).$encryption_algorithm, // Size of algorithm and algorithm
                self::toInt32Bits(strlen($apu)).$apu,                                   // PartyUInfo
                self::toInt32Bits(strlen($apv)).$apv,                                   // PartyVInfo
                self::toInt32Bits($encryption_key_size),                                // SuppPubInfo (the encryption key size)
                "",                                                                     // SuppPrivInfo
            );
    
            return substr(hex2bin(hash('sha256', implode('', $encryption_segments))), 0, $encryption_key_size/8);
        }
    
        private static function toInt32Bits($value)
        {
            return hex2bin(str_pad(dechex($value), 8, "0", STR_PAD_LEFT));
        }
    }
    

    使用很简单:

    ConcatKDF::generate("The shared key here", 'A128CBC+HS256', 128);
    

    如果你有apu和apv参数:

    ConcatKDF::generate("Another shared key here", 'A128GCM', 128, "Alice", "Bob");
    

    【讨论】:

      【解决方案2】:

      我确实最终解决了这个问题。不确定它是否会帮助其他人,但以防万一:

      // Derive CBC encryption & integrity keys
      $shaSize = 256;
      $encryptionKeySize = $shaSize / 2;
      $integrityKeySize = $shaSize;
      
      // Calculate the key derivation using Concat KDF for the content 
      // encryption key
      $encryptionSegments = [
          $this->packInt32sBe(1), 
          $cmk,                              // Z
          $this->packInt32sBe($encryptionKeySize) . utf8_encode('A128CBC+HS256'), // AlgorithmID
          $this->packInt32sBe(0), // PartyUInfo
          $this->packInt32sBe(0), // PartyUInfo
          'Encryption',           // SuppPubInfo
      ];
      
      // Calculate the SHA256 digest, and then get the first 16 bytes of it
      $cek = substr(hex2bin(hash('sha256', implode('', $encryptionSegments))), 0, 16);
      

      这里唯一的未知变量是$cmk,这是我的内容主密钥,也就是“Z”值。在这种特定情况下,我通过从 XBOX One 令牌请求中解密得到主密钥。

      【讨论】:

      • 你可以优化你的方法packInt32sBe使用下面的单行return hex2bin(str_pad(dechex($value), 8, "0", STR_PAD_LEFT));
      猜你喜欢
      • 1970-01-01
      • 2016-01-05
      • 2022-10-31
      • 2018-07-17
      • 1970-01-01
      • 2018-03-27
      • 2015-11-03
      • 2017-02-09
      • 2020-12-24
      相关资源
      最近更新 更多