Skip to content

Commit b071fcb

Browse files
author
Frank Denis
committed
sodium ext: add bindings for keygen() functions
This is the recommended way to generate keys correctly for all operations using a single key, since libsodium 1.0.12.
1 parent 8e8fbf5 commit b071fcb

File tree

6 files changed

+101
-12
lines changed

6 files changed

+101
-12
lines changed

ext/sodium/libsodium.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,20 @@ const zend_function_entry sodium_functions[] = {
252252
PHP_FE(sodium_memcmp, AI_TwoStrings)
253253
PHP_FE(sodium_memzero, AI_FirstArgByReferenceSecondLength)
254254

255+
#ifdef HAVE_AESGCM
256+
PHP_FE(sodium_crypto_aead_aes256gcm_keygen, AI_None)
257+
#endif
258+
PHP_FE(sodium_crypto_aead_chacha20poly1305_keygen, AI_None)
259+
PHP_FE(sodium_crypto_aead_chacha20poly1305_ietf_keygen, AI_None)
260+
#ifdef crypto_aead_xchacha20poly1305_IETF_NPUBBYTES
261+
PHP_FE(sodium_crypto_aead_xchacha20poly1305_ietf_keygen, AI_None)
262+
#endif
263+
PHP_FE(sodium_crypto_auth_keygen, AI_None)
264+
PHP_FE(sodium_crypto_generichash_keygen, AI_None)
265+
PHP_FE(sodium_crypto_secretbox_keygen, AI_None)
266+
PHP_FE(sodium_crypto_shorthash_keygen, AI_None)
267+
PHP_FE(sodium_crypto_stream_keygen, AI_None)
268+
255269
PHP_FALIAS(sodium_crypto_scalarmult_base, sodium_crypto_box_publickey_from_secretkey, AI_TwoStrings)
256270

257271
PHP_FE_END
@@ -2794,6 +2808,73 @@ PHP_FUNCTION(sodium_compare)
27942808
}
27952809
#endif
27962810

2811+
#ifdef HAVE_AESGCM
2812+
PHP_FUNCTION(sodium_crypto_aead_aes256gcm_keygen)
2813+
{
2814+
unsigned char key[crypto_aead_aes256gcm_KEYBYTES];
2815+
randombytes_buf(key, sizeof key);
2816+
RETURN_STRINGL((const char *) key, sizeof key);
2817+
}
2818+
#endif
2819+
2820+
PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_keygen)
2821+
{
2822+
unsigned char key[crypto_aead_chacha20poly1305_KEYBYTES];
2823+
randombytes_buf(key, sizeof key);
2824+
RETURN_STRINGL((const char *) key, sizeof key);
2825+
}
2826+
2827+
PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_ietf_keygen)
2828+
{
2829+
unsigned char key[crypto_aead_chacha20poly1305_ietf_KEYBYTES];
2830+
randombytes_buf(key, sizeof key);
2831+
RETURN_STRINGL((const char *) key, sizeof key);
2832+
}
2833+
2834+
#ifdef crypto_aead_xchacha20poly1305_IETF_NPUBBYTES
2835+
PHP_FUNCTION(sodium_crypto_aead_xchacha20poly1305_ietf_keygen)
2836+
{
2837+
unsigned char key[crypto_aead_xchacha20poly1305_ietf_KEYBYTES];
2838+
randombytes_buf(key, sizeof key);
2839+
RETURN_STRINGL((const char *) key, sizeof key);
2840+
}
2841+
#endif
2842+
2843+
PHP_FUNCTION(sodium_crypto_auth_keygen)
2844+
{
2845+
unsigned char key[crypto_auth_KEYBYTES];
2846+
randombytes_buf(key, sizeof key);
2847+
RETURN_STRINGL((const char *) key, sizeof key);
2848+
}
2849+
2850+
PHP_FUNCTION(sodium_crypto_generichash_keygen)
2851+
{
2852+
unsigned char key[crypto_generichash_KEYBYTES];
2853+
randombytes_buf(key, sizeof key);
2854+
RETURN_STRINGL((const char *) key, sizeof key);
2855+
}
2856+
2857+
PHP_FUNCTION(sodium_crypto_secretbox_keygen)
2858+
{
2859+
unsigned char key[crypto_secretbox_KEYBYTES];
2860+
randombytes_buf(key, sizeof key);
2861+
RETURN_STRINGL((const char *) key, sizeof key);
2862+
}
2863+
2864+
PHP_FUNCTION(sodium_crypto_shorthash_keygen)
2865+
{
2866+
unsigned char key[crypto_shorthash_KEYBYTES];
2867+
randombytes_buf(key, sizeof key);
2868+
RETURN_STRINGL((const char *) key, sizeof key);
2869+
}
2870+
2871+
PHP_FUNCTION(sodium_crypto_stream_keygen)
2872+
{
2873+
unsigned char key[crypto_stream_KEYBYTES];
2874+
randombytes_buf(key, sizeof key);
2875+
RETURN_STRINGL((const char *) key, sizeof key);
2876+
}
2877+
27972878
/*
27982879
* Local variables:
27992880
* tab-width: 4

ext/sodium/php_libsodium.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ PHP_FUNCTION(sodium_compare);
4040
PHP_FUNCTION(sodium_crypto_aead_aes256gcm_decrypt);
4141
PHP_FUNCTION(sodium_crypto_aead_aes256gcm_encrypt);
4242
PHP_FUNCTION(sodium_crypto_aead_aes256gcm_is_available);
43+
PHP_FUNCTION(sodium_crypto_aead_aes256gcm_keygen);
4344
PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_decrypt);
4445
PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_encrypt);
46+
PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_keygen);
4547
PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_ietf_decrypt);
4648
PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_ietf_encrypt);
49+
PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_ietf_keygen);
4750
PHP_FUNCTION(sodium_crypto_aead_xchacha20poly1305_ietf_decrypt);
4851
PHP_FUNCTION(sodium_crypto_aead_xchacha20poly1305_ietf_encrypt);
52+
PHP_FUNCTION(sodium_crypto_aead_xchacha20poly1305_ietf_keygen);
4953
PHP_FUNCTION(sodium_crypto_auth);
54+
PHP_FUNCTION(sodium_crypto_auth_keygen);
5055
PHP_FUNCTION(sodium_crypto_auth_verify);
5156
PHP_FUNCTION(sodium_crypto_box);
5257
PHP_FUNCTION(sodium_crypto_box_keypair);
@@ -61,6 +66,7 @@ PHP_FUNCTION(sodium_crypto_box_seed_keypair);
6166
PHP_FUNCTION(sodium_crypto_generichash);
6267
PHP_FUNCTION(sodium_crypto_generichash_final);
6368
PHP_FUNCTION(sodium_crypto_generichash_init);
69+
PHP_FUNCTION(sodium_crypto_generichash_keygen);
6470
PHP_FUNCTION(sodium_crypto_generichash_update);
6571
PHP_FUNCTION(sodium_crypto_kx_client_session_keys);
6672
PHP_FUNCTION(sodium_crypto_kx_keypair);
@@ -77,8 +83,10 @@ PHP_FUNCTION(sodium_crypto_pwhash_str_verify);
7783
PHP_FUNCTION(sodium_crypto_scalarmult);
7884
PHP_FUNCTION(sodium_crypto_scalarmult_base);
7985
PHP_FUNCTION(sodium_crypto_secretbox);
86+
PHP_FUNCTION(sodium_crypto_secretbox_keygen);
8087
PHP_FUNCTION(sodium_crypto_secretbox_open);
8188
PHP_FUNCTION(sodium_crypto_shorthash);
89+
PHP_FUNCTION(sodium_crypto_shorthash_keygen);
8290
PHP_FUNCTION(sodium_crypto_sign);
8391
PHP_FUNCTION(sodium_crypto_sign_detached);
8492
PHP_FUNCTION(sodium_crypto_sign_ed25519_pk_to_curve25519);
@@ -92,6 +100,7 @@ PHP_FUNCTION(sodium_crypto_sign_secretkey);
92100
PHP_FUNCTION(sodium_crypto_sign_seed_keypair);
93101
PHP_FUNCTION(sodium_crypto_sign_verify_detached);
94102
PHP_FUNCTION(sodium_crypto_stream);
103+
PHP_FUNCTION(sodium_crypto_stream_keygen);
95104
PHP_FUNCTION(sodium_crypto_stream_xor);
96105
PHP_FUNCTION(sodium_hex2bin);
97106
PHP_FUNCTION(sodium_increment);

ext/sodium/tests/crypto_aead.phpt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ echo "aead_chacha20poly1305:\n";
1111

1212
$msg = random_bytes(random_int(1, 1000));
1313
$nonce = random_bytes(SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES);
14-
$key = random_bytes(SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES);
14+
$key = sodium_crypto_aead_chacha20poly1305_keygen();
1515
$ad = random_bytes(random_int(1, 1000));
1616

1717
$ciphertext = sodium_crypto_aead_chacha20poly1305_encrypt($msg, $ad, $nonce, $key);
@@ -34,7 +34,7 @@ if (SODIUM_LIBRARY_MAJOR_VERSION > 7 ||
3434
SODIUM_LIBRARY_MINOR_VERSION >= 6)) {
3535
$msg = random_bytes(random_int(1, 1000));
3636
$nonce = random_bytes(SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES);
37-
$key = random_bytes(SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES);
37+
$key = sodium_crypto_aead_chacha20poly1305_ietf_keygen();
3838
$ad = random_bytes(random_int(1, 1000));
3939

4040
$ciphertext = sodium_crypto_aead_chacha20poly1305_ietf_encrypt($msg, $ad, $nonce, $key);
@@ -63,7 +63,7 @@ if (SODIUM_LIBRARY_MAJOR_VERSION > 9 ||
6363
SODIUM_LIBRARY_MINOR_VERSION >= 4)) {
6464
$msg = random_bytes(random_int(1, 1000));
6565
$nonce = random_bytes(SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES);
66-
$key = random_bytes(SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES);
66+
$key = sodium_crypto_aead_xchacha20poly1305_ietf_keygen();
6767
$ad = random_bytes(random_int(1, 1000));
6868

6969
$ciphertext = sodium_crypto_aead_xchacha20poly1305_ietf_encrypt($msg, $ad, $nonce, $key);
@@ -87,12 +87,11 @@ if (SODIUM_LIBRARY_MAJOR_VERSION > 9 ||
8787

8888
echo "aead_aes256gcm:\n";
8989

90-
$msg = random_bytes(random_int(1, 1000));
91-
$nonce = random_bytes(SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES);
92-
$key = random_bytes(SODIUM_CRYPTO_AEAD_AES256GCM_KEYBYTES);
93-
$ad = random_bytes(random_int(1, 1000));
94-
9590
if (sodium_crypto_aead_aes256gcm_is_available()) {
91+
$msg = random_bytes(random_int(1, 1000));
92+
$nonce = random_bytes(SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES);
93+
$ad = random_bytes(random_int(1, 1000));
94+
$key = sodium_crypto_aead_aes256gcm_keygen();
9695
$ciphertext = sodium_crypto_aead_aes256gcm_encrypt($msg, $ad, $nonce, $key);
9796
$msg2 = sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $ad, $nonce, $key);
9897
var_dump($ciphertext !== $msg);

ext/sodium/tests/crypto_auth.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Check for libsodium auth
55
--FILE--
66
<?php
77
$msg = random_bytes(1000);
8-
$key = random_bytes(SODIUM_CRYPTO_AUTH_KEYBYTES);
8+
$key = sodium_crypto_auth_keygen();
99
$mac = sodium_crypto_auth($msg, $key);
1010

1111
// This should validate

ext/sodium/tests/crypto_secretbox.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Check for libsodium secretbox
55
--FILE--
66
<?php
77
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
8-
$key = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
8+
$key = sodium_crypto_secretbox_keygen();
99

1010
$a = sodium_crypto_secretbox('test', $nonce, $key);
1111
$x = sodium_crypto_secretbox_open($a, $nonce, $key);

ext/sodium/tests/crypto_stream.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Check for libsodium stream
55
--FILE--
66
<?php
77
$nonce = random_bytes(SODIUM_CRYPTO_STREAM_NONCEBYTES);
8-
$key = random_bytes(SODIUM_CRYPTO_STREAM_KEYBYTES);
8+
$key = sodium_crypto_stream_keygen();
99

1010
$len = 100;
1111
$stream = sodium_crypto_stream($len, $nonce, $key);
@@ -16,7 +16,7 @@ $stream2 = sodium_crypto_stream($len, $nonce, $key);
1616
$nonce = random_bytes(SODIUM_CRYPTO_STREAM_NONCEBYTES);
1717
$stream3 = sodium_crypto_stream($len, $nonce, $key);
1818

19-
$key = random_bytes(SODIUM_CRYPTO_STREAM_KEYBYTES);
19+
$key = sodium_crypto_stream_keygen();
2020
$stream4 = sodium_crypto_stream($len, $nonce, $key);
2121

2222
var_dump($stream === $stream2);

0 commit comments

Comments
 (0)