Completed
Push — master ( 3e460a...170823 )
by Michael
06:32 queued 05:08
created

Aes::decrypt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 6
cts 6
cp 1
rs 9.0856
cc 1
eloc 8
nc 1
nop 3
crap 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Aes::checksum() 0 12 1
1
<?php
2
3
/**
4
 * Aes.php
5
 *
6
 * PHP version 7
7
 *
8
 * @category Dcrypt
9
 * @package  Dcrypt
10
 * @author   Michael Meyer (mmeyer2k) <[email protected]>
11
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
12
 * @link     https://github.com/mmeyer2k/dcrypt
13
 */
14
15
namespace Dcrypt;
16
17
/**
18
 * Provides functionality common to the dcrypt AES block ciphers.
19
 *
20
 * @category Dcrypt
21
 * @package  Dcrypt
22
 * @author   Michael Meyer (mmeyer2k) <[email protected]>
23
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
24
 * @link     https://github.com/mmeyer2k/dcrypt
25
 * @link     https://apigen.ci/github/mmeyer2k/dcrypt/namespace-Dcrypt.html
26
 */
27
class Aes
28
{
29
    /**
30
     * This string is used when hashing to ensure cross compatibility between
31
     * dcrypt\mcrypt and dcrypt\aes. Since v7, this is only needed for backwards
32
     * compatibility with older versions
33
     */
34
    const RIJNDA = 'rijndael-128';
35
36
    /**
37
     * Hardcoded hashing algo string.
38
     */
39
    const ALGO = 'sha256';
40
41
    /**
42
     * Size of initialization vector in bytes
43
     *
44
     * @var int
45
     */
46
    const IVSIZE = 16;
47
48
    /**
49
     * Size of checksum in bytes
50
     *
51
     * @var int
52
     */
53
    const CKSIZE = 32;
54
55
    /**
56
     * Create a message authentication checksum.
57
     *
58
     * @param string $cyphertext Cyphertext that needs a checksum.
59
     * @param string $iv         Initialization vector.
60
     * @param string $key        HMAC key
61
     * @param string $mode       Cipher mode (cbc, ctr)
62
     *
63
     * @return string
64
     */
65 8
    protected static function checksum(string $cyphertext, string $iv, string $key, string $mode): string
66
    {
67
        // Prevent potentially multiple large string concats by hmac-ing the cyphertext
68 8
        // by itself first...
69
        $sum = Hash::hmac($cyphertext, $key, self::ALGO);
70
71 8
        // Add the other elements together before performing the final hash
72
        $sum = $sum . $iv . $mode . self::RIJNDA;
73
74 8
        // ... then hash other elements with previous hmac and return
75
        return Hash::hmac($sum, $key, self::ALGO);
76
    }
77 8
78
    /**
79
     * Transform password into key and perform iterative HMAC (if specified)
80 8
     *
81
     * @param string $password Encryption key
82
     * @param string $iv       Initialization vector
83 8
     * @param int    $cost     Number of HMAC iterations to perform on key
84
     * @param string $mode     Cipher mode (cbc, ctr)
85
     *
86 6
     * @return string
87
     */
88
    protected static function key(string $password, string $iv, int $cost, string $mode): string
89
    {
90
        // Perform key derivation
91
        return Hash::ihmac($iv . self::RIJNDA . $mode, $password, $cost, self::ALGO);
92
    }
93
94
    /**
95
     * Verify checksum during decryption step and throw error if mismatching.
96
     *
97
     * @param string $calculated
98 7
     * @param string $supplied
99
     */
100
    protected static function checksumVerify(string $calculated, string $supplied)
101 7
    {
102
        if (!Str::equal($calculated, $supplied)) {
103
            $e = 'Decryption can not proceed due to invalid cyphertext checksum.';
104 7
            throw new \InvalidArgumentException($e);
105
        }
106
    }
107 7
108
    /**
109
     * Return the encryption mode string. "cbc" or "ctr"
110 7
     *
111
     * @return string
112
     */
113
    protected static function mode(): string
114
    {
115 7
        return Str::substr(static::CIPHER, -3);
116
    }
117
}
118