Crypt-Sodium-XS

 view release on metacpan or  search on metacpan

XS.xs  view on Meta::CPAN


  ret = newSV(0);
  sv_usepvn_flags(ret, (char *)nonce_buf, out_len, SV_HAS_TRAILING_NUL);

  return ret;
}

/* NB: croaks on failure */
static SV * sv_keygen(pTHX_ STRLEN size, SV * flags) {
  protmem *key_pm;
  unsigned int mv_flags = g_protmem_default_flags_key;

  SvGETMAGIC(flags);
  if (SvOK(flags))
    mv_flags = SvUV_nomg(flags);

  key_pm = protmem_init(aTHX_ size, mv_flags);
  if (key_pm == NULL)
    croak("sv_keygen: Failed to allocate protmem");

  randombytes_buf(key_pm->pm_ptr, key_pm->size);

  if (protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
    protmem_free(aTHX_ key_pm);
    croak("sv_keygen: Failed to release key protmem RW");
  }

  return protmem_to_sv(aTHX_ key_pm, MEMVAULT_CLASS);
}

=for TODO

there is a metric boatload (or two) of error handling in this code that needs
cleaning up. the whole thing could do with a re-factor already. very much
duplicated long-form code that could be abstracted (CAREFULLY). might not be
worth the added complexity. this'll be a beast to maintain as-is, though.

also necessary all over the place is handling older versions of libsodium.
there are version definitions usable from pre-processor that should be used to
protect stuff only in the newer versions, and throw otherwise.

also also, should be keeping track of whether the used libsodium is a "minimal"
build. availablility of a number of algorithms and such are dependent on being
a not-minimal build and should be guarded.

=cut

MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS

BOOT:
if (sodium_init() != 0)
  croak("Failed to initialze library");
has_aes256gcm = crypto_aead_aes256gcm_is_available();

PROTOTYPES: ENABLE

void _define_constants()
  PREINIT:
  HV *stash = gv_stashpv("Crypt::Sodium::XS", 0);

  PPCODE:
  newCONSTSUB(stash, "SODIUM_VERSION_STRING", newSVpvs(SODIUM_VERSION_STRING));
  newCONSTSUB(stash, "SODIUM_LIBRARY_VERSION_MAJOR",
              newSVuv(SODIUM_LIBRARY_VERSION_MAJOR));
  newCONSTSUB(stash, "SODIUM_LIBRARY_VERSION_MINOR",
              newSVuv(SODIUM_LIBRARY_VERSION_MINOR));
  XSRETURN_YES;

const char *
sodium_version_string()

INCLUDE: inc/base64.xs

INCLUDE: inc/util.xs

INCLUDE: inc/protmem.xs

INCLUDE: inc/memvault.xs

INCLUDE: inc/core.xs

INCLUDE: inc/curve25519.xs

INCLUDE: inc/kx.xs

INCLUDE: inc/kdf.xs

INCLUDE: inc/hkdf.xs

INCLUDE: inc/secretbox.xs

INCLUDE: inc/box.xs

INCLUDE: inc/sign.xs

INCLUDE: inc/secretstream.xs

INCLUDE: inc/aead.xs

INCLUDE: inc/stream.xs

INCLUDE: inc/shorthash.xs

INCLUDE: inc/generichash.xs

INCLUDE: inc/pwhash.xs

INCLUDE: inc/hash.xs

INCLUDE: inc/auth.xs

INCLUDE: inc/onetimeauth.xs

INCLUDE: inc/scalarmult.xs

INCLUDE: inc/ipcrypt.xs



( run in 0.561 second using v1.01-cache-2.11-cpan-71847e10f99 )