CurveOidMapper::getCurveFromOid()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Mdanter\Ecc\Serializer\Util;
5
6
use FG\ASN1\Universal\ObjectIdentifier;
7
use Mdanter\Ecc\Curves\NamedCurveFp;
8
use Mdanter\Ecc\Curves\CurveFactory;
9
use Mdanter\Ecc\Curves\NistCurve;
10
use Mdanter\Ecc\Curves\SecgCurve;
11
use Mdanter\Ecc\Exception\UnsupportedCurveException;
12
use Mdanter\Ecc\Primitives\CurveFpInterface;
13
use Mdanter\Ecc\Primitives\GeneratorPoint;
14
15
class CurveOidMapper
16
{
17
18
    const NIST_P192_OID = '1.2.840.10045.3.1.1';
19
20
    const NIST_P224_OID = '1.3.132.0.33';
21
22
    const NIST_P256_OID = '1.2.840.10045.3.1.7';
23
24
    const NIST_P384_OID = '1.3.132.0.34';
25
26
    const NIST_P521_OID = '1.3.132.0.35';
27
28
    const SECP_112R1_OID = '1.3.132.0.6';
29
30
    const SECP_192K1_OID = '1.3.132.0.31';
31
32
    const SECP_256K1_OID = '1.3.132.0.10';
33
34
    const SECP_256R1_OID = '1.2.840.10045.3.1.7';
35
36
    const SECP_384R1_OID = '1.3.132.0.34';
37
38
    /**
39
     * @var array
40
     */
41
    private static $oidMap = array(
42
        NistCurve::NAME_P192 => self::NIST_P192_OID,
43
        NistCurve::NAME_P224 => self::NIST_P224_OID,
44
        NistCurve::NAME_P256 => self::NIST_P256_OID,
45
        NistCurve::NAME_P384 => self::NIST_P384_OID,
46
        NistCurve::NAME_P521 => self::NIST_P521_OID,
47
        SecgCurve::NAME_SECP_112R1 => self::SECP_112R1_OID,
48
        SecgCurve::NAME_SECP_192K1 => self::SECP_192K1_OID,
49
        SecgCurve::NAME_SECP_256K1 => self::SECP_256K1_OID,
50
        SecgCurve::NAME_SECP_256R1 => self::SECP_256R1_OID,
51
        SecgCurve::NAME_SECP_384R1 => self::SECP_384R1_OID,
52
    );
53
54
    /**
55
     * @var array
56
     */
57
    private static $sizeMap = array(
58
        NistCurve::NAME_P192 => 24,
59
        NistCurve::NAME_P224 => 28,
60
        NistCurve::NAME_P256 => 32,
61
        NistCurve::NAME_P384 => 48,
62
        NistCurve::NAME_P521 => 66,
63
        SecgCurve::NAME_SECP_112R1 => 14,
64
        SecgCurve::NAME_SECP_192K1 => 24,
65
        SecgCurve::NAME_SECP_256K1 => 32,
66
        SecgCurve::NAME_SECP_256R1 => 32,
67
        SecgCurve::NAME_SECP_384R1 => 48,
68
    );
69
70
    /**
71
     * @return array
72
     */
73
    public static function getNames(): array
74
    {
75
        return array_keys(self::$oidMap);
76
    }
77
78
    /**
79
     * @param CurveFpInterface $curve
80
     * @return int
81
     */
82
    public static function getByteSize(CurveFpInterface $curve): int
83
    {
84
        if ($curve instanceof NamedCurveFp && array_key_exists($curve->getName(), self::$sizeMap)) {
85
            return self::$sizeMap[$curve->getName()];
86
        }
87
88
        throw new UnsupportedCurveException('Unsupported curve type');
89
    }
90
91
    /**
92
     * @param NamedCurveFp $curve
93
     * @return ObjectIdentifier
94
     */
95
    public static function getCurveOid(NamedCurveFp $curve): ObjectIdentifier
96
    {
97
        if (array_key_exists($curve->getName(), self::$oidMap)) {
98
            $oidString = self::$oidMap[$curve->getName()];
99
100
            return new ObjectIdentifier($oidString);
101
        }
102
103
        throw new UnsupportedCurveException('Unsupported curve type');
104
    }
105
106
    /**
107
     * @param ObjectIdentifier $oid
108
     * @return NamedCurveFp
109
     */
110
    public static function getCurveFromOid(ObjectIdentifier $oid): NamedCurveFp
111
    {
112
        $oidString = $oid->getContent();
113
        $invertedMap = array_flip(self::$oidMap);
114
115
        if (array_key_exists($oidString, $invertedMap)) {
116
            return CurveFactory::getCurveByName($invertedMap[$oidString]);
117
        }
118
119
        $error = new UnsupportedCurveException('Invalid data: unsupported curve.');
120
        $error->setOid($oidString);
121
        throw $error;
122
    }
123
124
    /**
125
     * @param ObjectIdentifier $oid
126
     * @return GeneratorPoint
127
     */
128
    public static function getGeneratorFromOid(ObjectIdentifier $oid): GeneratorPoint
129
    {
130
        $oidString = $oid->getContent();
131
        $invertedMap = array_flip(self::$oidMap);
132
133
        if (array_key_exists($oidString, $invertedMap)) {
134
            return CurveFactory::getGeneratorByName($invertedMap[$oidString]);
135
        }
136
137
        $error = new UnsupportedCurveException('Invalid data: unsupported generator.');
138
        $error->setOid($oidString);
139
        throw $error;
140
    }
141
}
142