Skip to content
This repository was archived by the owner on Oct 5, 2022. It is now read-only.

Commit 99afd3f

Browse files
committed
Merge pull request #127 from technion/cryptsecure
Cryptsecure
2 parents 5b204f0 + 85d08aa commit 99afd3f

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

lib/phpSec/Crypt/Crypto.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,24 @@ public function encrypt($data, $key) {
7777
return false;
7878
}
7979
} else {
80-
/* No spsecific size is needed. */
80+
/* No specific size is needed. */
8181
if($keySize == 0 || $keySize > mcrypt_enc_get_key_size($td)) {
8282
throw new \phpSec\Exception\InvalidKeySpecException('Key is out of range. Should be between 1 and ' . mcrypt_enc_get_key_size($td).' bytes.');
8383
return false;
8484
}
8585
}
8686

87+
/* Using PBKDF with constant salts dedicated to each purpose
88+
* can securely derivce two keys from one */
89+
$key1 = $this->pbkdf2($key, "encrypt", 1, $keySize);
90+
$key2 = $this->pbkdf2($key, "HMAC", 1, $keySize);
91+
8792
/* Create IV. */
8893
$rnd = $this->psl['crypt/rand'];
8994
$iv = $rnd->bytes(mcrypt_enc_get_iv_size($td));
9095

9196
/* Init mcrypt. */
92-
mcrypt_generic_init($td, $key, $iv);
97+
mcrypt_generic_init($td, $key1, $iv);
9398

9499
/* Prepeare the array with data. */
95100
$serializedData = serialize($data);
@@ -111,7 +116,7 @@ public function encrypt($data, $key) {
111116
$encrypted['iv'] = base64_encode($iv); /* Initialization vector, just a bunch of randomness. */
112117
$encrypted['cdata'] = base64_encode(mcrypt_generic($td, $serializedData)); /* The encrypted data. */
113118
$encrypted['mac'] = base64_encode( /* The message authentication code. Used to make sure the */
114-
$this->pbkdf2($encrypted['cdata'], $key, 1000, 32) /* message is valid when decrypted. */
119+
$this->pbkdf2($encrypted['cdata'], $key2, 1, 32) /* message is valid when decrypted. */
115120
);
116121
return json_encode($encrypted);
117122
}
@@ -148,14 +153,20 @@ public function decrypt($data, $key) {
148153
$td = mcrypt_module_open($data['algo'], '', $data['mode'], '');
149154
$block = mcrypt_enc_get_block_size($td);
150155

156+
/* Using PBKDF with constant salts dedicated to each purpose
157+
* can securely derivce two keys from one */
158+
$keySize = strlen($key);
159+
$key1 = $this->pbkdf2($key, "encrypt", 1, $keySize);
160+
$key2 = $this->pbkdf2($key, "HMAC", 1, $keySize);
161+
151162
/* Check MAC. */
152-
if(base64_decode($data['mac']) != $this->pbkdf2($data['cdata'], $key, 1000, 32)) {
163+
if(base64_decode($data['mac']) != $this->pbkdf2($data['cdata'], $key2, 1, 32)) {
153164
throw new \phpSec\Exception\GeneralSecurityException('Message authentication code invalid');
154165
return false;
155166
}
156167

157168
/* Init mcrypt. */
158-
mcrypt_generic_init($td, $key, base64_decode($data['iv']));
169+
mcrypt_generic_init($td, $key1, base64_decode($data['iv']));
159170

160171
$decrypted = rtrim(mdecrypt_generic($td, base64_decode($this->stripPadding($block, $data['cdata']))));
161172

tests/phpSec/Crypt/CryptoTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ public function testCrypto() {
77

88
$str = 'foobaz';
99
$key = '123abc12123abc12';
10+
$badkey = '123abcR77123abc12';
11+
1012

1113
$encrypted = $crypto->encrypt($str, $key);
1214

1315
$decrypted = $crypto->decrypt($encrypted, $key);
1416

1517
$this->assertEquals($decrypted, $str);
18+
try {
19+
$ret = $crypto->decrypt($encrypted, $badkey);
20+
}catch(Exception $e){
21+
$this->assertEquals("Message authentication code invalid",$e->getMessage());
22+
}
1623

1724
}
18-
}
25+
}

0 commit comments

Comments
 (0)