diff --git a/Sodium.xs b/Sodium.xs index c59e440..0aa04a6 100644 --- a/Sodium.xs +++ b/Sodium.xs @@ -412,6 +412,7 @@ randombytes_buf(size) randombytes_buf(buf, size); RETVAL = newSVpvn((const char * const)buf, size); sodium_free(buf); + OUTPUT: RETVAL @@ -427,6 +428,7 @@ real_crypto_scalarmult_base(n) RETVAL = &PL_sv_undef; } sodium_free(q); + OUTPUT: RETVAL @@ -443,6 +445,7 @@ real_crypto_scalarmult(n, p) RETVAL = &PL_sv_undef; } sodium_free(q); + OUTPUT: RETVAL @@ -934,10 +937,12 @@ real_crypto_pwhash_scrypt_str_verify(hp, p) SV * real_crypto_aead_xchacha20poly1305_ietf_keygen() CODE: - unsigned char key[crypto_aead_xchacha20poly1305_ietf_KEYBYTES]; - crypto_aead_xchacha20poly1305_ietf_keygen(key); - RETVAL = newSVpvn((unsigned char *)key, sizeof(key)); - OUTPUT: + unsigned char k* = sodium_malloc(crypto_aead_xchacha20poly1305_ietf_KEYBYTES); + crypto_aead_xchacha20poly1305_ietf_keygen(k); + RETVAL = newSVpvn((unsigned char *)k, crypto_aead_xchacha20poly1305_ietf_KEYBYTES); + sodium_free(k); + + OUTPUT: RETVAL SV * @@ -950,26 +955,29 @@ real_crypto_aead_xchacha20poly1305_ietf_encrypt(m, mlen, ad, adlen, nsec, k) unsigned char *k CODE: - unsigned char ciphertext[mlen + crypto_aead_xchacha20poly1305_ietf_ABYTES]; - unsigned long long ciphertext_len; - - int status = crypto_aead_xchacha20poly1305_ietf_encrypt( - ciphertext, - &ciphertext_len, - (const unsigned char*)m, - (unsigned long long) mlen, - (const unsigned char*)ad, - (unsigned long long) adlen, - NULL, - (unsigned char *)nsec, - (unsigned char *)k - ); - - if (status == 0) { - RETVAL = newSVpvn((unsigned char *)ciphertext, ciphertext_len); - } else { + unsigned char *c = sodium_malloc(mlen + crypto_aead_xchacha20poly1305_ietf_ABYTES); + unsigned long long clen; + + int status = crypto_aead_xchacha20poly1305_ietf_encrypt( + c, + &clen, + (const unsigned char*)m, + (unsigned long long) mlen, + (const unsigned char*)ad, + (unsigned long long) adlen, + NULL, + (unsigned char *)nsec, + (unsigned char *)k + ); + + if (status == 0) { + RETVAL = newSVpvn((unsigned char *)c, clen); + } else { RETVAL = &PL_sv_undef; - } + } + + sodium_free(c); + OUTPUT: RETVAL @@ -983,24 +991,27 @@ real_crypto_aead_xchacha20poly1305_ietf_decrypt(c, clen, ad, adlen, npub, k) unsigned char *npub CODE: - unsigned char m[clen - crypto_aead_xchacha20poly1305_ietf_ABYTES]; - unsigned long long mlen; - int status = crypto_aead_xchacha20poly1305_ietf_decrypt( - m, - &mlen, - NULL, - (const unsigned char*)c, - (unsigned long long)clen, - (const unsigned char*)ad, - (unsigned long long) adlen, - (const unsigned char*)npub, - (const unsigned char*)k - ); - - if (status == 0) { + unsigned char *m = sodium_malloc(clen - crypto_aead_xchacha20poly1305_ietf_ABYTES); + unsigned long long mlen; + int status = crypto_aead_xchacha20poly1305_ietf_decrypt( + m, + &mlen, + NULL, + (const unsigned char*)c, + (unsigned long long)clen, + (const unsigned char*)ad, + (unsigned long long) adlen, + (const unsigned char*)npub, + (const unsigned char*)k + ); + + if (status == 0) { RETVAL = newSVpvn((unsigned char *)m, mlen); - } else { + } else { RETVAL = &PL_sv_undef; - } + } + + sodium_free(m); + OUTPUT: RETVAL diff --git a/lib/Crypt/Sodium.pm b/lib/Crypt/Sodium.pm index c2dd23b..4707e8f 100644 --- a/lib/Crypt/Sodium.pm +++ b/lib/Crypt/Sodium.pm @@ -530,7 +530,6 @@ sub crypto_pwhash_scrypt_str_verify { } sub crypto_aead_xchacha20poly1305_ietf_nonce { - return randombytes_buf(crypto_aead_xchacha20poly1305_ietf_NPUBBYTES); } @@ -807,8 +806,8 @@ Michael Gregorowicz, Emike@mg2.orgE =head1 COPYRIGHT AND LICENSE -Copyright (C) 2015-2017 Michael Gregorowicz +Copyright (C) 2015-2018 Michael Gregorowicz -This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.18 or, at your option, any later version of Perl 5 you may have available. +This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.26 or, at your option, any later version of Perl 5 you may have available. =cut diff --git a/t/Crypt-Sodium.t b/t/Crypt-Sodium.t index c9675df..f9a2db3 100644 --- a/t/Crypt-Sodium.t +++ b/t/Crypt-Sodium.t @@ -169,9 +169,9 @@ ok(crypto_pwhash_str_verify($ahashed, 'Ultra Secret Fantastico'), 'password veri ok(!crypto_pwhash_str_verify($ahashed, 'Ultra Secretish Fantastico'), 'password verification failed on bad password, moderate difficulty'); # xchacha/poly1035 -ok(my $xchacha_key = crypto_aead_xchacha20poly1305_ietf_keygen()); +ok(my $xchacha_key = crypto_aead_xchacha20poly1305_ietf_keygen(), "generating a xchacha/poly1035 key"); ok(length($xchacha_key) == crypto_aead_xchacha20poly1305_ietf_KEYBYTES, "returned a string crypto_aead_xchacha20poly1305_ietf_KEYBYTES in length"); -ok(my $xchacha_nonce = crypto_aead_xchacha20poly1305_ietf_nonce()); +ok(my $xchacha_nonce = crypto_aead_xchacha20poly1305_ietf_nonce(), "generating a xchacha/poly1035 nonce"); ok(length($xchacha_nonce) == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES, "returned a string crypto_aead_xchacha20poly1305_ietf_NPUBBYTES in length"); ok(my $ciphered = crypto_aead_xchacha20poly1305_ietf_encrypt("1234", "additional data", $xchacha_nonce, $xchacha_key), "xchacha/poly1035 encryption succeeded"); ok(crypto_aead_xchacha20poly1305_ietf_decrypt($ciphered, "additional data", $xchacha_nonce, $xchacha_key) eq "1234", "xchacha/poly1035 decryption succeeded");