forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSodiumHandler.php
More file actions
143 lines (114 loc) · 3.62 KB
/
SodiumHandler.php
File metadata and controls
143 lines (114 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Encryption\Handlers;
use CodeIgniter\Encryption\Exceptions\EncryptionException;
use SensitiveParameter;
/**
* SodiumHandler uses libsodium in encryption.
*
* @see https://github.com/jedisct1/libsodium/issues/392
* @see \CodeIgniter\Encryption\Handlers\SodiumHandlerTest
*/
class SodiumHandler extends BaseHandler
{
/**
* Starter key
*
* @var string|null Null is used for buffer cleanup.
*/
protected $key = '';
/**
* Block size for padding message.
*
* @var int
*/
protected $blockSize = 16;
/**
* {@inheritDoc}
*/
public function encrypt(#[SensitiveParameter] $data, #[SensitiveParameter] $params = null)
{
$this->parseParams($params);
if (empty($this->key)) {
throw EncryptionException::forNeedsStarterKey();
}
// create a nonce for this operation
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); // 24 bytes
// add padding before we encrypt the data
if ($this->blockSize <= 0) {
throw EncryptionException::forEncryptionFailed();
}
$data = sodium_pad($data, $this->blockSize);
// encrypt message and combine with nonce
$ciphertext = $nonce . sodium_crypto_secretbox($data, $nonce, $this->key);
// cleanup buffers
sodium_memzero($data);
sodium_memzero($this->key);
return $ciphertext;
}
/**
* {@inheritDoc}
*/
public function decrypt($data, #[SensitiveParameter] $params = null)
{
$this->parseParams($params);
if (empty($this->key)) {
throw EncryptionException::forNeedsStarterKey();
}
if (mb_strlen($data, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) {
// message was truncated
throw EncryptionException::forAuthenticationFailed();
}
// Extract info from encrypted data
$nonce = self::substr($data, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$ciphertext = self::substr($data, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
// decrypt data
$data = sodium_crypto_secretbox_open($ciphertext, $nonce, $this->key);
if ($data === false) {
// message was tampered in transit
throw EncryptionException::forAuthenticationFailed(); // @codeCoverageIgnore
}
// remove extra padding during encryption
if ($this->blockSize <= 0) {
throw EncryptionException::forAuthenticationFailed();
}
$data = sodium_unpad($data, $this->blockSize);
// cleanup buffers
sodium_memzero($ciphertext);
sodium_memzero($this->key);
return $data;
}
/**
* Parse the $params before doing assignment.
*
* @param array|string|null $params
*
* @return void
*
* @throws EncryptionException If key is empty
*/
protected function parseParams($params)
{
if ($params === null) {
return;
}
if (is_array($params)) {
if (isset($params['key'])) {
$this->key = $params['key'];
}
if (isset($params['blockSize'])) {
$this->blockSize = $params['blockSize'];
}
return;
}
$this->key = (string) $params;
}
}