/** * @throws \RuntimeException * @return mixed */ public function invoke() { if (func_num_args() <= 0) { throw new \RuntimeException('you must pass len as arg!'); } $byte_len = func_get_arg(0); return ByteUtil::randomBytes($byte_len); }
/** * https://tools.ietf.org/html/rfc7518#appendix-B.3 * * @throws InvalidKeyLengthAlgorithmException */ public function testAES_256_CBC_HMAC_SHA_512() { $a = new A256CBCHS512_Algorithm(); $key = ByteUtil::convertHalfWordArrayToBin(array(0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f)); $plain_text = ByteUtil::convertHalfWordArrayToBin(array(0x41, 0x20, 0x63, 0x69, 0x70, 0x68, 0x65, 0x72, 0x20, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x20, 0x6d, 0x75, 0x73, 0x74, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x62, 0x65, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x69, 0x74, 0x20, 0x6d, 0x75, 0x73, 0x74, 0x20, 0x62, 0x65, 0x20, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x74, 0x6f, 0x20, 0x66, 0x61, 0x6c, 0x6c, 0x20, 0x69, 0x6e, 0x74, 0x6f, 0x20, 0x74, 0x68, 0x65, 0x20, 0x68, 0x61, 0x6e, 0x64, 0x73, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x65, 0x6e, 0x65, 0x6d, 0x79, 0x20, 0x77, 0x69, 0x74, 0x68, 0x6f, 0x75, 0x74, 0x20, 0x69, 0x6e, 0x63, 0x6f, 0x6e, 0x76, 0x65, 0x6e, 0x69, 0x65, 0x6e, 0x63, 0x65)); $iv = ByteUtil::convertHalfWordArrayToBin(array(0x1a, 0xf3, 0x8c, 0x2d, 0xc2, 0xb9, 0x6f, 0xfd, 0xd8, 0x66, 0x94, 0x9, 0x23, 0x41, 0xbc, 0x4)); $aad = ByteUtil::convertHalfWordArrayToBin(array(0x54, 0x68, 0x65, 0x20, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x63, 0x69, 0x70, 0x6c, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x41, 0x75, 0x67, 0x75, 0x73, 0x74, 0x65, 0x20, 0x4b, 0x65, 0x72, 0x63, 0x6b, 0x68, 0x6f, 0x66, 0x66, 0x73)); list($cypher_text, $tag) = $a->encrypt($plain_text, $key, $iv, $aad); $cypher_text_should = ByteUtil::convertHalfWordArrayToBin(array(0x4a, 0xff, 0xaa, 0xad, 0xb7, 0x8c, 0x31, 0xc5, 0xda, 0x4b, 0x1b, 0x59, 0xd, 0x10, 0xff, 0xbd, 0x3d, 0xd8, 0xd5, 0xd3, 0x2, 0x42, 0x35, 0x26, 0x91, 0x2d, 0xa0, 0x37, 0xec, 0xbc, 0xc7, 0xbd, 0x82, 0x2c, 0x30, 0x1d, 0xd6, 0x7c, 0x37, 0x3b, 0xcc, 0xb5, 0x84, 0xad, 0x3e, 0x92, 0x79, 0xc2, 0xe6, 0xd1, 0x2a, 0x13, 0x74, 0xb7, 0x7f, 0x7, 0x75, 0x53, 0xdf, 0x82, 0x94, 0x10, 0x44, 0x6b, 0x36, 0xeb, 0xd9, 0x70, 0x66, 0x29, 0x6a, 0xe6, 0x42, 0x7e, 0xa7, 0x5c, 0x2e, 0x8, 0x46, 0xa1, 0x1a, 0x9, 0xcc, 0xf5, 0x37, 0xd, 0xc8, 0xb, 0xfe, 0xcb, 0xad, 0x28, 0xc7, 0x3f, 0x9, 0xb3, 0xa3, 0xb7, 0x5e, 0x66, 0x2a, 0x25, 0x94, 0x41, 0xa, 0xe4, 0x96, 0xb2, 0xe2, 0xe6, 0x60, 0x9e, 0x31, 0xe6, 0xe0, 0x2c, 0xc8, 0x37, 0xf0, 0x53, 0xd2, 0x1f, 0x37, 0xff, 0x4f, 0x51, 0x95, 0xb, 0xbe, 0x26, 0x38, 0xd0, 0x9d, 0xd7, 0xa4, 0x93, 0x9, 0x30, 0x80, 0x6d, 0x7, 0x3, 0xb1, 0xf6)); $tag_should = ByteUtil::convertHalfWordArrayToBin(array(0x4d, 0xd3, 0xb4, 0xc0, 0x88, 0xa7, 0xf4, 0x5c, 0x21, 0x68, 0x39, 0x64, 0x5b, 0x20, 0x12, 0xbf, 0x2e, 0x62, 0x69, 0xa8, 0xc5, 0x6a, 0x81, 0x6d, 0xbc, 0x1b, 0x26, 0x77, 0x61, 0x95, 0x5b, 0xc5)); $this->assertTrue($cypher_text == $cypher_text_should); $this->assertTrue($tag == $tag_should); }
/** * @return int */ public function getBitLength() { return ByteUtil::bitLength(strlen($this->secret)); }
/** * @param string $cypher_text * @param string $key * @param string $iv * @param string $aad * @return string */ protected function calculateAuthenticationTag($cypher_text, $key, $iv, $aad) { // first octets of key $t_len = $mac_key_len = strlen($key) / 2; // MAC_KEY = initial MAC_KEY_LEN octets of K $mac_key = substr($key, 0, $mac_key_len); /** * The octet string AL is equal to the number of bits in the * Additional Authenticated Data ($aad) expressed as a 64-bit unsigned * big-endian integer */ $al = ByteUtil::convert2UnsignedLongBE(strlen($aad) * 8); // M = MAC(MAC_KEY, A || IV || E || AL) $secured_input = implode('', array($aad, $iv, $cypher_text, $al)); $m = hash_hmac($this->getHashingAlgorithm(), $secured_input, $mac_key, true); // T = initial T_LEN octets of M. return substr($m, 0, $t_len); }