如何实现与JOSE/JWE/JWT一起使用的concat KDF


How to implement a concat KDF for use with JOSE/JWE/JWT

我正试图用PHP编写代码来解密JWE令牌,因为现有库不支持我需要的算法(A128CBC+HS256,这是一种不推荐使用的算法)。

我的问题是我无法理解如何生成使用"连接密钥"的内容加密密钥推导函数"(见第5.8.1节:http://csrc.nist.gov/publications/nistpubs/800-56A/SP800-56A_Revision1_Mar08-2007.pdf)。这个函数的符号和解释让我难以理解。

我的值是基于JOSE JSON web算法草案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,它是解密的内容主密钥。

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

// 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令牌请求中解密主密钥。

这是我自己根据相同规范实现的,但草案#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");