Passed
Branch master (c86cc6)
by Tim
01:57
created

HMACSignatureTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 128
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testVerificationFailure() 0 13 1
A testSign() 0 38 1
A setUpBeforeClass() 0 4 1
A testVerify() 0 40 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\Alg\Signature;
6
7
use PHPUnit\Framework\TestCase;
8
use SimpleSAML\XMLSecurity\Alg\Signature\HMAC;
9
use SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmFactory;
10
use SimpleSAML\XMLSecurity\Constants as C;
11
use SimpleSAML\XMLSecurity\Key\PrivateKey;
12
use SimpleSAML\XMLSecurity\Key\PublicKey;
13
use SimpleSAML\XMLSecurity\Key\SymmetricKey;
14
use SimpleSAML\XMLSecurity\Key\X509Certificate;
15
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
16
use TypeError;
17
18
use function bin2hex;
19
use function hex2bin;
20
21
/**
22
 * Tests for SimpleSAML\XMLSecurity\Alg\Signature\HMAC.
23
 *
24
 * @package SimpleSAML\Signature
25
 */
26
final class HMACSignatureTest extends TestCase
27
{
28
    /** @var string */
29
    protected const PLAINTEXT = 'plaintext';
30
31
    /** @var string */
32
    protected const SECRET = 'de54fbd0f10c34df6e800b11043024fa';
33
34
    /** @var \SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmFactory */
35
    protected static SignatureAlgorithmFactory $factory;
36
37
    /** @var \SimpleSAML\XMLSecurity\Key\SymmetricKey */
38
    protected static SymmetricKey $key;
39
40
41
    public static function setUpBeforeClass(): void
42
    {
43
        self::$factory = new SignatureAlgorithmFactory([]);
44
        self::$key = new SymmetricKey(self::SECRET);
45
    }
46
47
48
    /**
49
     * Test that signing works.
50
     */
51
    public function testSign(): void
52
    {
53
        // test HMAC-SHA1
54
        $hmac = self::$factory->getAlgorithm(C::SIG_HMAC_SHA1, self::$key);
55
        $this->assertEquals('655c3b4277b39f31dedf5adc7f4cc9f07da7102c', bin2hex($hmac->sign(self::PLAINTEXT)));
0 ignored issues
show
Bug introduced by
It seems like $hmac->sign(self::PLAINTEXT) can also be of type false; however, parameter $string of bin2hex() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
        $this->assertEquals('655c3b4277b39f31dedf5adc7f4cc9f07da7102c', bin2hex(/** @scrutinizer ignore-type */ $hmac->sign(self::PLAINTEXT)));
Loading history...
56
57
        // test HMAC-SHA224
58
        $hmac = self::$factory->getAlgorithm(C::SIG_HMAC_SHA224, self::$key);
59
        $this->assertEquals(
60
            '645405ccc725e10022e5a89e98cc33db07c0cd89ba78c21caf931f40',
61
            bin2hex($hmac->sign(self::PLAINTEXT)),
62
        );
63
64
        // test HMAC-SHA256
65
        $hmac = self::$factory->getAlgorithm(C::SIG_HMAC_SHA256, self::$key);
66
        $this->assertEquals(
67
            '721d8385785a3d4c8d16c7b4a96b343728a11e221656e6dd9502d540d4e87ef2',
68
            bin2hex($hmac->sign(self::PLAINTEXT)),
69
        );
70
71
        // test HMAC-SHA384
72
        $hmac = self::$factory->getAlgorithm(C::SIG_HMAC_SHA384, self::$key);
73
        $this->assertEquals(
74
            'b3ad2e39a057fd7a952cffd503d30eca295c6698dc23ddf0bebf98631a0162da0db0105db156a220dec78cebaf2c202c',
75
            bin2hex($hmac->sign(self::PLAINTEXT)),
76
        );
77
78
        // test HMAC-SHA512
79
        $hmac = self::$factory->getAlgorithm(C::SIG_HMAC_SHA512, self::$key);
80
        $this->assertEquals(
81
            '9cc73c95f564a142b28340cf6e1d6b509a9e97dab6577e5d0199760a858105185252e203b6b096ad24708a2b7e34a0f506776d8' .
82
            '8e2f47fff055fc51342b69cdc',
83
            bin2hex($hmac->sign(self::PLAINTEXT)),
84
        );
85
86
        // test HMAC-RIPEMD160
87
        $hmac = self::$factory->getAlgorithm(C::SIG_HMAC_RIPEMD160, self::$key);
88
        $this->assertEquals('a9fd77b68644464d08be0ba2cd998eab3e2a7b1d', bin2hex($hmac->sign(self::PLAINTEXT)));
89
    }
90
91
92
    /**
93
     * Test that signature verification works.
94
     */
95
    public function testVerify(): void
96
    {
97
        // test HMAC-SHA1
98
        $hmac = self::$factory->getAlgorithm(C::SIG_HMAC_SHA1, self::$key);
99
        $this->assertTrue($hmac->verify(self::PLAINTEXT, hex2bin('655c3b4277b39f31dedf5adc7f4cc9f07da7102c')));
100
101
        // test HMAC-SHA224
102
        $hmac = self::$factory->getAlgorithm(C::SIG_HMAC_SHA224, self::$key);
103
        $this->assertTrue($hmac->verify(
104
            self::PLAINTEXT,
105
            hex2bin('645405ccc725e10022e5a89e98cc33db07c0cd89ba78c21caf931f40'),
106
        ));
107
108
        // test HMAC-SHA256
109
        $hmac = self::$factory->getAlgorithm(C::SIG_HMAC_SHA256, self::$key);
110
        $this->assertTrue($hmac->verify(
111
            self::PLAINTEXT,
112
            hex2bin('721d8385785a3d4c8d16c7b4a96b343728a11e221656e6dd9502d540d4e87ef2'),
113
        ));
114
115
        // test HMAC-SHA384
116
        $hmac = self::$factory->getAlgorithm(C::SIG_HMAC_SHA384, self::$key);
117
        $this->assertTrue($hmac->verify(
118
            self::PLAINTEXT,
119
            hex2bin('b3ad2e39a057fd7a952cffd503d30eca295c6698dc23ddf0bebf98631a0162da0db0105db156a220dec78cebaf2c202c'),
120
        ));
121
122
        // test HMAC-SHA512
123
        $hmac = self::$factory->getAlgorithm(C::SIG_HMAC_SHA512, self::$key);
124
        $this->assertTrue($hmac->verify(
125
            self::PLAINTEXT,
126
            hex2bin(
127
                '9cc73c95f564a142b28340cf6e1d6b509a9e97dab6577e5d0199760a858105185252e203b6b096ad24708a2b7e34a0f5067' .
128
                '76d88e2f47fff055fc51342b69cdc',
129
            ),
130
        ));
131
132
        // test HMAC-RIPEMD160
133
        $hmac = self::$factory->getAlgorithm(C::SIG_HMAC_RIPEMD160, self::$key);
134
        $this->assertTrue($hmac->verify(self::PLAINTEXT, hex2bin('a9fd77b68644464d08be0ba2cd998eab3e2a7b1d')));
135
    }
136
137
138
    /**
139
     * Test that signature verification fails properly.
140
     */
141
    public function testVerificationFailure(): void
142
    {
143
        // test wrong plaintext
144
        $hmac = self::$factory->getAlgorithm(C::SIG_HMAC_SHA1, self::$key);
145
        $this->assertFalse($hmac->verify(self::PLAINTEXT . '.', hex2bin('655c3b4277b39f31dedf5adc7f4cc9f07da7102c')));
146
147
        // test wrong signature
148
        $this->assertFalse($hmac->verify(self::PLAINTEXT, hex2bin('655c3b4277b39f31dedf5adc7f4cc9f07da7102d')));
149
150
        // test wrong key
151
        $wrongKey = new SymmetricKey('de54fbd0f10c34df6e800b11043024fb');
152
        $hmac = self::$factory->getAlgorithm(C::SIG_HMAC_SHA1, $wrongKey);
153
        $this->assertFalse($hmac->verify(self::PLAINTEXT, hex2bin('655c3b4277b39f31dedf5adc7f4cc9f07da7102c')));
154
    }
155
}
156