2018-03-22 15:51:09 +01:00
|
|
|
MODULE = CryptX PACKAGE = Crypt::Stream::Sober128
|
|
|
|
|
2018-03-22 15:54:03 +01:00
|
|
|
PROTOTYPES: DISABLE
|
|
|
|
|
2018-03-22 15:51:09 +01:00
|
|
|
Crypt::Stream::Sober128
|
2018-03-22 15:54:03 +01:00
|
|
|
new(Class, SV * key, SV * nonce)
|
2018-03-22 15:51:09 +01:00
|
|
|
CODE:
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
STRLEN iv_len=0, k_len=0;
|
|
|
|
unsigned char *iv=NULL, *k=NULL;
|
|
|
|
|
|
|
|
if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
|
|
|
|
if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
|
|
|
|
k = (unsigned char *) SvPVbyte(key, k_len);
|
|
|
|
iv = (unsigned char *) SvPVbyte(nonce, iv_len);
|
|
|
|
|
2018-03-22 15:54:03 +01:00
|
|
|
Newz(0, RETVAL, 1, sober128_state);
|
2018-03-22 15:51:09 +01:00
|
|
|
if (!RETVAL) croak("FATAL: Newz failed");
|
|
|
|
|
2018-03-22 15:54:03 +01:00
|
|
|
rv = sober128_stream_setup(RETVAL, k, (unsigned long)k_len);
|
|
|
|
if (rv != CRYPT_OK) {
|
|
|
|
Safefree(RETVAL);
|
|
|
|
croak("FATAL: sober128_stream_setup failed: %s", error_to_string(rv));
|
|
|
|
}
|
2018-03-22 15:51:09 +01:00
|
|
|
|
2018-03-22 15:54:03 +01:00
|
|
|
rv = sober128_stream_setiv(RETVAL, iv, (unsigned long)iv_len);
|
|
|
|
if (rv != CRYPT_OK) {
|
|
|
|
Safefree(RETVAL);
|
|
|
|
croak("FATAL: sober128_stream_setiv failed: %s", error_to_string(rv));
|
|
|
|
}
|
2018-03-22 15:51:09 +01:00
|
|
|
}
|
|
|
|
OUTPUT:
|
|
|
|
RETVAL
|
|
|
|
|
|
|
|
void
|
|
|
|
DESTROY(Crypt::Stream::Sober128 self)
|
|
|
|
CODE:
|
2018-03-22 15:54:03 +01:00
|
|
|
sober128_stream_done(self);
|
2018-03-22 15:51:09 +01:00
|
|
|
Safefree(self);
|
|
|
|
|
|
|
|
Crypt::Stream::Sober128
|
|
|
|
clone(Crypt::Stream::Sober128 self)
|
|
|
|
CODE:
|
2018-03-22 15:54:03 +01:00
|
|
|
Newz(0, RETVAL, 1, sober128_state);
|
2018-03-22 15:51:09 +01:00
|
|
|
if (!RETVAL) croak("FATAL: Newz failed");
|
2018-03-22 15:54:03 +01:00
|
|
|
Copy(self, RETVAL, 1, sober128_state);
|
2018-03-22 15:51:09 +01:00
|
|
|
OUTPUT:
|
|
|
|
RETVAL
|
|
|
|
|
|
|
|
SV *
|
|
|
|
keystream(Crypt::Stream::Sober128 self, STRLEN out_len)
|
|
|
|
CODE:
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
unsigned char *out_data;
|
|
|
|
|
2018-03-22 15:54:03 +01:00
|
|
|
if (out_len == 0) {
|
|
|
|
RETVAL = newSVpvn("", 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
RETVAL = NEWSV(0, out_len); /* avoid zero! */
|
|
|
|
SvPOK_only(RETVAL);
|
|
|
|
SvCUR_set(RETVAL, out_len);
|
|
|
|
out_data = (unsigned char *)SvPVX(RETVAL);
|
|
|
|
rv = sober128_stream_keystream(self, out_data, (unsigned long)out_len);
|
|
|
|
if (rv != CRYPT_OK) {
|
|
|
|
SvREFCNT_dec(RETVAL);
|
|
|
|
croak("FATAL: sober128_stream_keystream failed: %s", error_to_string(rv));
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 15:51:09 +01:00
|
|
|
}
|
|
|
|
OUTPUT:
|
|
|
|
RETVAL
|
|
|
|
|
|
|
|
SV *
|
|
|
|
crypt(Crypt::Stream::Sober128 self, SV * data)
|
|
|
|
CODE:
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
STRLEN in_data_len;
|
|
|
|
unsigned char *in_data, *out_data;
|
|
|
|
|
|
|
|
in_data = (unsigned char *)SvPVbyte(data, in_data_len);
|
2018-03-22 15:54:03 +01:00
|
|
|
if (in_data_len == 0) {
|
2018-03-22 15:51:09 +01:00
|
|
|
RETVAL = newSVpvn("", 0);
|
|
|
|
}
|
|
|
|
else {
|
2018-03-22 15:54:03 +01:00
|
|
|
RETVAL = NEWSV(0, in_data_len); /* avoid zero! */
|
2018-03-22 15:51:09 +01:00
|
|
|
SvPOK_only(RETVAL);
|
|
|
|
SvCUR_set(RETVAL, in_data_len);
|
2018-03-22 15:54:03 +01:00
|
|
|
out_data = (unsigned char *)SvPVX(RETVAL);
|
|
|
|
rv = sober128_stream_crypt(self, in_data, (unsigned long)in_data_len, out_data);
|
|
|
|
if (rv != CRYPT_OK) {
|
|
|
|
SvREFCNT_dec(RETVAL);
|
|
|
|
croak("FATAL: sober128_stream_crypt failed: %s", error_to_string(rv));
|
|
|
|
}
|
2018-03-22 15:51:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
OUTPUT:
|
|
|
|
RETVAL
|