Skip to content

Commit

Permalink
gardening
Browse files Browse the repository at this point in the history
  • Loading branch information
mgregoro committed Apr 18, 2018
1 parent ccc0173 commit 6116470
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 45 deletions.
91 changes: 51 additions & 40 deletions Sodium.xs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ randombytes_buf(size)
randombytes_buf(buf, size);
RETVAL = newSVpvn((const char * const)buf, size);
sodium_free(buf);

OUTPUT:
RETVAL

Expand All @@ -427,6 +428,7 @@ real_crypto_scalarmult_base(n)
RETVAL = &PL_sv_undef;
}
sodium_free(q);

OUTPUT:
RETVAL

Expand All @@ -443,6 +445,7 @@ real_crypto_scalarmult(n, p)
RETVAL = &PL_sv_undef;
}
sodium_free(q);

OUTPUT:
RETVAL

Expand Down Expand Up @@ -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 *
Expand All @@ -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

Expand All @@ -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
5 changes: 2 additions & 3 deletions lib/Crypt/Sodium.pm
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ sub crypto_pwhash_scrypt_str_verify {
}

sub crypto_aead_xchacha20poly1305_ietf_nonce {

return randombytes_buf(crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
}

Expand Down Expand Up @@ -807,8 +806,8 @@ Michael Gregorowicz, E<lt>mike@mg2.orgE<gt>
=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
4 changes: 2 additions & 2 deletions t/Crypt-Sodium.t
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 6116470

Please sign in to comment.