|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\CryptoTypes\Asymmetric\EC; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\ASN1\Type\Primitive\BitString; |
|
8
|
|
|
use Sop\ASN1\Type\Primitive\Integer; |
|
9
|
|
|
use Sop\ASN1\Type\Primitive\OctetString; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Implements data type conversions from SEC 1: Elliptic Curve Cryptography. |
|
13
|
|
|
* |
|
14
|
|
|
* @see http://www.secg.org/sec1-v2.pdf |
|
15
|
|
|
*/ |
|
16
|
|
|
class ECConversion |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Perform Bit-String-to-Octet-String Conversion. |
|
20
|
|
|
* |
|
21
|
|
|
* Defined in SEC 1 section 2.3.1. |
|
22
|
|
|
* |
|
23
|
|
|
* @param BitString $bs |
|
24
|
|
|
* |
|
25
|
|
|
* @throws \RuntimeException |
|
26
|
|
|
* |
|
27
|
|
|
* @return OctetString |
|
28
|
|
|
*/ |
|
29
|
5 |
|
public static function bitStringToOctetString(BitString $bs): OctetString |
|
30
|
|
|
{ |
|
31
|
5 |
|
$str = $bs->string(); |
|
32
|
5 |
|
if ($bs->unusedBits()) { |
|
33
|
|
|
// @todo pad string |
|
34
|
1 |
|
throw new \RuntimeException('Unaligned bitstrings to supported'); |
|
35
|
|
|
} |
|
36
|
4 |
|
return new OctetString($str); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Perform Octet-String-to-Bit-String Conversion. |
|
41
|
|
|
* |
|
42
|
|
|
* Defined in SEC 1 section 2.3.2. |
|
43
|
|
|
* |
|
44
|
|
|
* @param OctetString $os |
|
45
|
|
|
* |
|
46
|
|
|
* @return BitString |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public static function octetStringToBitString(OctetString $os): BitString |
|
49
|
|
|
{ |
|
50
|
4 |
|
return new BitString($os->string()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Perform Integer-to-Octet-String Conversion. |
|
55
|
|
|
* |
|
56
|
|
|
* Defined in SEC 1 section 2.3.7. |
|
57
|
|
|
* |
|
58
|
|
|
* @param Integer $num |
|
59
|
|
|
* @param null|int $mlen Optional desired output length |
|
60
|
|
|
* |
|
61
|
|
|
* @throws \UnexpectedValueException |
|
62
|
|
|
* |
|
63
|
|
|
* @return OctetString |
|
64
|
|
|
*/ |
|
65
|
13 |
|
public static function integerToOctetString(Integer $num, ?int $mlen = null): OctetString |
|
66
|
|
|
{ |
|
67
|
13 |
|
$gmp = gmp_init($num->number(), 10); |
|
68
|
13 |
|
$str = gmp_export($gmp, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
|
|
|
|
|
69
|
13 |
|
if (null !== $mlen) { |
|
70
|
10 |
|
$len = strlen($str); |
|
71
|
10 |
|
if ($len > $mlen) { |
|
72
|
1 |
|
throw new \RangeException('Number is too large.'); |
|
73
|
|
|
} |
|
74
|
|
|
// pad with zeroes |
|
75
|
9 |
|
if ($len < $mlen) { |
|
76
|
5 |
|
$str = str_repeat("\0", $mlen - $len) . $str; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
12 |
|
return new OctetString($str); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Perform Octet-String-to-Integer Conversion. |
|
84
|
|
|
* |
|
85
|
|
|
* Defined in SEC 1 section 2.3.8. |
|
86
|
|
|
* |
|
87
|
|
|
* @param OctetString $os |
|
88
|
|
|
* |
|
89
|
|
|
* @return Integer |
|
90
|
|
|
*/ |
|
91
|
9 |
|
public static function octetStringToInteger(OctetString $os): Integer |
|
92
|
|
|
{ |
|
93
|
9 |
|
$num = gmp_import($os->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
94
|
9 |
|
assert($num instanceof \GMP, new \RuntimeException('gmp_import() failed.')); |
|
95
|
9 |
|
return new Integer(gmp_strval($num, 10)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Convert a base-10 number to octets. |
|
100
|
|
|
* |
|
101
|
|
|
* This is a convenicence method for integer <-> octet string conversion |
|
102
|
|
|
* without the need for external ASN.1 dependencies. |
|
103
|
|
|
* |
|
104
|
|
|
* @param int|string $num Number in base-10 |
|
105
|
|
|
* @param null|int $mlen Optional desired output length |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public static function numberToOctets($num, ?int $mlen = null): string |
|
110
|
|
|
{ |
|
111
|
1 |
|
return self::integerToOctetString(new Integer($num), $mlen)->string(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Convert octets to a base-10 number. |
|
116
|
|
|
* |
|
117
|
|
|
* This is a convenicence method for integer <-> octet string conversion |
|
118
|
|
|
* without the need for external ASN.1 dependencies. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $str |
|
121
|
|
|
* |
|
122
|
|
|
* @return string Number in base-10 |
|
123
|
|
|
*/ |
|
124
|
2 |
|
public static function octetsToNumber(string $str): string |
|
125
|
|
|
{ |
|
126
|
2 |
|
return self::octetStringToInteger(new OctetString($str))->number(); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|