| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/* |
| 6 |
* The MIT License (MIT) |
| 7 |
* |
| 8 |
* Copyright (c) 2014-2021 Spomky-Labs |
| 9 |
* |
| 10 |
* This software may be modified and distributed under the terms |
| 11 |
* of the MIT license. See the LICENSE file for details. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace Cose; |
| 15 |
|
| 16 |
use Assert\Assertion; |
| 17 |
use Assert\AssertionFailedException; |
| 18 |
|
| 19 |
/** |
| 20 |
* @see https://www.iana.org/assignments/cose/cose.xhtml#algorithms |
| 21 |
*/ |
| 22 |
abstract class Algorithms |
| 23 |
{ |
| 24 |
public const COSE_ALGORITHM_AES_CCM_64_128_256 = 33; |
| 25 |
public const COSE_ALGORITHM_AES_CCM_64_128_128 = 32; |
| 26 |
public const COSE_ALGORITHM_AES_CCM_16_128_256 = 31; |
| 27 |
public const COSE_ALGORITHM_AES_CCM_16_128_128 = 30; |
| 28 |
public const COSE_ALGORITHM_AES_MAC_256_128 = 26; |
| 29 |
public const COSE_ALGORITHM_AES_MAC_128_128 = 25; |
| 30 |
public const COSE_ALGORITHM_CHACHA20_POLY1305 = 24; |
| 31 |
public const COSE_ALGORITHM_AES_MAC_256_64 = 15; |
| 32 |
public const COSE_ALGORITHM_AES_MAC_128_64 = 14; |
| 33 |
public const COSE_ALGORITHM_AES_CCM_64_64_256 = 13; |
| 34 |
public const COSE_ALGORITHM_AES_CCM_64_64_128 = 12; |
| 35 |
public const COSE_ALGORITHM_AES_CCM_16_64_256 = 11; |
| 36 |
public const COSE_ALGORITHM_AES_CCM_16_64_128 = 10; |
| 37 |
public const COSE_ALGORITHM_HS512 = 7; |
| 38 |
public const COSE_ALGORITHM_HS384 = 6; |
| 39 |
public const COSE_ALGORITHM_HS256 = 5; |
| 40 |
public const COSE_ALGORITHM_HS256_64 = 4; |
| 41 |
public const COSE_ALGORITHM_A256GCM = 3; |
| 42 |
public const COSE_ALGORITHM_A192GCM = 2; |
| 43 |
public const COSE_ALGORITHM_A128GCM = 1; |
| 44 |
public const COSE_ALGORITHM_A128KW = -3; |
| 45 |
public const COSE_ALGORITHM_A192KW = -4; |
| 46 |
public const COSE_ALGORITHM_A256KW = -5; |
| 47 |
public const COSE_ALGORITHM_DIRECT = -6; |
| 48 |
public const COSE_ALGORITHM_ES256 = -7; |
| 49 |
public const COSE_ALGORITHM_EdDSA = -8; |
| 50 |
public const COSE_ALGORITHM_ED256 = -260; |
| 51 |
public const COSE_ALGORITHM_ED512 = -261; |
| 52 |
public const COSE_ALGORITHM_DIRECT_HKDF_SHA_256 = -10; |
| 53 |
public const COSE_ALGORITHM_DIRECT_HKDF_SHA_512 = -11; |
| 54 |
public const COSE_ALGORITHM_DIRECT_HKDF_AES_128 = -12; |
| 55 |
public const COSE_ALGORITHM_DIRECT_HKDF_AES_256 = -13; |
| 56 |
public const COSE_ALGORITHM_ECDH_ES_HKDF_256 = -25; |
| 57 |
public const COSE_ALGORITHM_ECDH_ES_HKDF_512 = -26; |
| 58 |
public const COSE_ALGORITHM_ECDH_SS_HKDF_256 = -27; |
| 59 |
public const COSE_ALGORITHM_ECDH_SS_HKDF_512 = -28; |
| 60 |
public const COSE_ALGORITHM_ECDH_ES_A128KW = -29; |
| 61 |
public const COSE_ALGORITHM_ECDH_ES_A192KW = -30; |
| 62 |
public const COSE_ALGORITHM_ECDH_ES_A256KW = -31; |
| 63 |
public const COSE_ALGORITHM_ECDH_SS_A128KW = -32; |
| 64 |
public const COSE_ALGORITHM_ECDH_SS_A192KW = -33; |
| 65 |
public const COSE_ALGORITHM_ECDH_SS_A256KW = -34; |
| 66 |
public const COSE_ALGORITHM_ES384 = -35; |
| 67 |
public const COSE_ALGORITHM_ES512 = -36; |
| 68 |
public const COSE_ALGORITHM_PS256 = -37; |
| 69 |
public const COSE_ALGORITHM_PS384 = -38; |
| 70 |
public const COSE_ALGORITHM_PS512 = -39; |
| 71 |
public const COSE_ALGORITHM_RSAES_OAEP = -40; |
| 72 |
public const COSE_ALGORITHM_RSAES_OAEP_256 = -41; |
| 73 |
public const COSE_ALGORITHM_RSAES_OAEP_512 = -42; |
| 74 |
public const COSE_ALGORITHM_ES256K = -46; |
| 75 |
public const COSE_ALGORITHM_RS256 = -257; |
| 76 |
public const COSE_ALGORITHM_RS384 = -258; |
| 77 |
public const COSE_ALGORITHM_RS512 = -259; |
| 78 |
public const COSE_ALGORITHM_RS1 = -65535; |
| 79 |
|
| 80 |
public const COSE_ALGORITHM_MAP = [ |
| 81 |
self::COSE_ALGORITHM_ES256 => OPENSSL_ALGO_SHA256, |
| 82 |
self::COSE_ALGORITHM_ES384 => OPENSSL_ALGO_SHA384, |
| 83 |
self::COSE_ALGORITHM_ES512 => OPENSSL_ALGO_SHA512, |
| 84 |
self::COSE_ALGORITHM_RS256 => OPENSSL_ALGO_SHA256, |
| 85 |
self::COSE_ALGORITHM_RS384 => OPENSSL_ALGO_SHA384, |
| 86 |
self::COSE_ALGORITHM_RS512 => OPENSSL_ALGO_SHA512, |
| 87 |
self::COSE_ALGORITHM_RS1 => OPENSSL_ALGO_SHA1, |
| 88 |
]; |
| 89 |
|
| 90 |
public const COSE_HASH_MAP = [ |
| 91 |
self::COSE_ALGORITHM_ES256K => 'sha256', |
| 92 |
self::COSE_ALGORITHM_ES256 => 'sha256', |
| 93 |
self::COSE_ALGORITHM_ES384 => 'sha384', |
| 94 |
self::COSE_ALGORITHM_ES512 => 'sha512', |
| 95 |
self::COSE_ALGORITHM_RS256 => 'sha256', |
| 96 |
self::COSE_ALGORITHM_RS384 => 'sha384', |
| 97 |
self::COSE_ALGORITHM_RS512 => 'sha512', |
| 98 |
self::COSE_ALGORITHM_PS256 => 'sha256', |
| 99 |
self::COSE_ALGORITHM_PS384 => 'sha384', |
| 100 |
self::COSE_ALGORITHM_PS512 => 'sha512', |
| 101 |
self::COSE_ALGORITHM_RS1 => 'sha1', |
| 102 |
]; |
| 103 |
|
| 104 |
/** |
| 105 |
* @throws AssertionFailedException |
| 106 |
*/ |
| 107 |
public static function getOpensslAlgorithmFor(int $algorithmIdentifier): int |
| 108 |
{ |
| 109 |
Assertion::keyExists(self::COSE_ALGORITHM_MAP, $algorithmIdentifier, 'The specified algorithm identifier is not supported'); |
| 110 |
|
| 111 |
return self::COSE_ALGORITHM_MAP[$algorithmIdentifier]; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @throws AssertionFailedException |
| 116 |
*/ |
| 117 |
public static function getHashAlgorithmFor(int $algorithmIdentifier): string |
| 118 |
{ |
| 119 |
Assertion::keyExists(self::COSE_HASH_MAP, $algorithmIdentifier, 'The specified algorithm identifier is not supported'); |
| 120 |
|
| 121 |
return self::COSE_HASH_MAP[$algorithmIdentifier]; |
| 122 |
} |
| 123 |
} |
| 124 |
|