PBES1AlgorithmIdentifier::_paramsASN1()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\PKCS5\ASN1\AlgorithmIdentifier;
6
7
use Sop\ASN1\Element;
8
use Sop\ASN1\Type\Constructed\Sequence;
9
use Sop\ASN1\Type\Primitive\Integer;
10
use Sop\ASN1\Type\Primitive\OctetString;
11
use Sop\ASN1\Type\UnspecifiedType;
12
use Sop\CryptoTypes\AlgorithmIdentifier\Cipher\BlockCipherAlgorithmIdentifier;
13
use Sop\CryptoTypes\AlgorithmIdentifier\SpecificAlgorithmIdentifier;
14
use Sop\PKCS5\HashFunc\HashFunc;
15
16
/*
17
From RFC 2898 - A.3 PBES1:
18
19
   For each OID, the parameters field associated with the OID in an
20
   AlgorithmIdentifier shall have type PBEParameter:
21
22
   PBEParameter ::= SEQUENCE {
23
       salt OCTET STRING (SIZE(8)),
24
       iterationCount INTEGER }
25
26
From RFC 7292 - Appendix C.  Keys and IVs for Password Privacy Mode:
27
28
   This standard does not prescribe a length for the salt either.
29
   Ideally, the salt is as long as the output of the hash function being
30
   used and consists of completely random bits.
31
32
   pkcs-12PbeParams ::= SEQUENCE {
33
       salt        OCTET STRING,
34
       iterations  INTEGER
35
   }
36
*/
37
38
/**
39
 * Base class for PBES1 encryption scheme.
40
 *
41
 * @see https://tools.ietf.org/html/rfc2898#section-6.1
42
 * @see https://tools.ietf.org/html/rfc2898#appendix-A.3
43
 * @see https://tools.ietf.org/html/rfc7292#appendix-C
44
 */
45
abstract class PBES1AlgorithmIdentifier extends PBEAlgorithmIdentifier
46
{
47
    /**
48
     * Get the hash function used by the scheme.
49
     *
50
     * @return HashFunc
51
     */
52
    abstract public function hashFunc(): HashFunc;
53
54
    /**
55
     * Get the block cipher algorithm identifier used by the scheme.
56
     *
57
     * @return BlockCipherAlgorithmIdentifier
58
     */
59
    abstract public function blockCipher(): BlockCipherAlgorithmIdentifier;
60
61
    /**
62
     * {@inheritdoc}
63
     *
64
     * @return self
65
     */
66 9
    public static function fromASN1Params(
67
        ?UnspecifiedType $params = null): SpecificAlgorithmIdentifier
68
    {
69 9
        if (!isset($params)) {
70 1
            throw new \UnexpectedValueException('No parameters.');
71
        }
72 8
        $seq = $params->asSequence();
73 8
        $salt = $seq->at(0)->asOctetString()->string();
74 8
        $iteration_count = $seq->at(1)->asInteger()->intNumber();
75 8
        return new static($salt, $iteration_count);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     *
81
     * @return Sequence
82
     */
83 9
    protected function _paramsASN1(): ?Element
84
    {
85 9
        return new Sequence(new OctetString($this->_salt),
86 9
            new Integer($this->_iterationCount));
87
    }
88
}
89