Completed
Push — v2.0.x ( 45cd5b...255948 )
by Florent
12:36
created

AESGCMKW::getKeyManagementMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Algorithm\KeyEncryption;
13
14
use Base64Url\Base64Url;
15
use Crypto\Cipher;
16
use Jose\Object\JWKInterface;
17
18
/**
19
 * Class AESGCMKW.
20
 */
21
abstract class AESGCMKW implements KeyEncryptionInterface
22
{
23
    /**
24
     * @param \Jose\Object\JWKInterface $key
25
     * @param string                    $cek
26
     * @param array                     $header
27
     *
28
     * @return mixed
29
     */
30
    public function encryptKey(JWKInterface $key, $cek, array &$header)
31
    {
32
        $this->checkKey($key);
33
34
        $cipher = Cipher::aes(Cipher::MODE_GCM, $this->getKeySize());
35
        $cipher->setAAD(null);
36
        $iv = openssl_random_pseudo_bytes(96 / 8);
37
        $encryted_cek = $cipher->encrypt($cek, Base64Url::decode($key->get('k')), $iv);
38
39
        $header['iv'] = Base64Url::encode($iv);
40
        $header['tag'] = Base64Url::encode($cipher->getTag());
41
42
        return $encryted_cek;
43
    }
44
45
    /**
46
     * @param \Jose\Object\JWKInterface $key
47
     * @param string                    $encryted_cek
48
     * @param array                     $header
49
     *
50
     * @return mixed
51
     */
52
    public function decryptKey(JWKInterface $key, $encryted_cek, array $header)
53
    {
54
        $this->checkKey($key);
55
        $this->checkAdditionalParameters($header);
56
57
        $cipher = Cipher::aes(Cipher::MODE_GCM, $this->getKeySize());
58
        $cipher->setTag(Base64Url::decode($header['tag']));
59
        $cipher->setAAD(null);
60
61
        $cek = $cipher->decrypt($encryted_cek, Base64Url::decode($key->get('k')), Base64Url::decode($header['iv']));
62
63
        return $cek;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getKeyManagementMode()
70
    {
71
        return self::MODE_WRAP;
72
    }
73
74
    /**
75
     * @param JWKInterface $key
76
     */
77
    protected function checkKey(JWKInterface $key)
78
    {
79
        if ('oct' !== $key->get('kty') || !$key->has('k')) {
80
            throw new \InvalidArgumentException('The key is not valid');
81
        }
82
    }
83
84
    /**
85
     * @param array $header
86
     */
87
    protected function checkAdditionalParameters(array $header)
88
    {
89
        if (!array_key_exists('iv', $header) || !array_key_exists('tag', $header)) {
90
            throw new \InvalidArgumentException("Missing parameters 'iv' or 'tag'.");
91
        }
92
    }
93
94
    /**
95
     * @return int
96
     */
97
    abstract protected function getKeySize();
98
}
99