/
TpmEccParameters.php
96 lines (80 loc) · 2.64 KB
/
TpmEccParameters.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace MadWizard\WebAuthn\Attestation\Tpm;
use MadWizard\WebAuthn\Exception\UnsupportedException;
use MadWizard\WebAuthn\Format\ByteBuffer;
/**
* Represents TPMPS_ECC_PARMS structure.
*/
final class TpmEccParameters implements KeyParametersInterface
{
/**
* @var int
*/
private $symmetric;
/**
* @var int
*/
private $scheme;
/**
* @var int
*/
private $curveId;
/**
* @var int
*/
private $kdf;
public const TPM_ECC_NIST_P256 = 0x0003;
public const TPM_ECC_BN_P256 = 0x0010;
public const TPM_ECC_SM2_P256 = 0x0020;
public function __construct(int $symmetric, int $scheme, int $curveId, int $kdf)
{
$this->symmetric = $symmetric;
$this->scheme = $scheme;
$this->curveId = $curveId;
$this->kdf = $kdf;
}
public function getAlgorithm(): int
{
return TpmPublic::TPM_ALG_ECC;
}
public static function parse(ByteBuffer $buffer, int $offset, ?int &$endOffset): KeyParametersInterface
{
$symmetric = $buffer->getUint16Val($offset);
if ($symmetric !== TpmPublic::TPM_ALG_NULL) {
// Values other than TPM_ALG_NULL may be followed by additional fields (in TPMT_SYM_DEF_OBJECT)
// so bail out if symmetric algorithm is not null
throw new UnsupportedException('Only TPM_ALG_NULL supported for symmetric field in TPMPS_ECC_PARMS');
}
$scheme = $buffer->getUint16Val($offset + 2);
if ($scheme !== TpmPublic::TPM_ALG_NULL) {
// Values other than TPM_ALG_NULL may be followed by additional fields (in TPMT_ECC_SCHEME)
// so bail out if scheme algorithm is not null
throw new UnsupportedException('Only TPM_ALG_NULL supported for scheme field in TPMPS_ECC_PARMS');
}
$curveId = $buffer->getUint16Val($offset + 4);
$kdf = $buffer->getUint16Val($offset + 6);
if ($kdf !== TpmPublic::TPM_ALG_NULL) {
// Values other than TPM_ALG_NULL may be followed by additional fields (in TPMT_KDF_SCHEME+)
// so bail out if kdf algorithm is not null
throw new UnsupportedException('Only TPM_ALG_NULL supported for kdf field in TPMPS_ECC_PARMS');
}
$endOffset = $offset + 8;
return new self($symmetric, $scheme, $curveId, $kdf);
}
public function getSymmetric(): int
{
return $this->symmetric;
}
public function getScheme(): int
{
return $this->scheme;
}
public function getCurveId(): int
{
return $this->curveId;
}
public function getKdf(): int
{
return $this->kdf;
}
}