view release on metacpan or search on metacpan
lib/Crypt/KeyDerivation.pm view on Meta::CPAN
#or
my $derived_key = argon2_pbkdf($type, $password, $salt, $t_cost, $m_factor, $parallelism, $len);
#or
my $derived_key = argon2_pbkdf($type, $password, $salt, $t_cost, $m_factor, $parallelism);
#or
my $derived_key = argon2_pbkdf($type, $password, $salt);
# $type ... [string] one of 'argon2d', 'argon2i', 'argon2id'
# $password ... [binary string] input keying material (password)
# $salt ... [binary string] salt/nonce (recommended: at least 16 bytes)
# $t_cost ... [integer] optional, time cost (number of iterations), DEFAULT: 3
# $m_factor ... [integer] optional, memory cost in kibibytes (1 KiB = 1024 B), DEFAULT: 65536 (= 64 MiB)
# $parallelism ... [integer] optional, degree of parallelism, DEFAULT: 1
# $len ... [integer] optional, derived key len in bytes, DEFAULT: 32
# $secret ... [binary string] optional, secret value, DEFAULT: ''
# $ad ... [binary string] optional, associated data, DEFAULT: ''
Increasing C<$t_cost>, C<$m_factor>, or C<$parallelism> increases work and
memory requirements. Invalid combinations croak. Optional C<$secret> and
C<$ad> may be C<undef>; otherwise they must be string or stringifiable scalars.
lib/Math/BigInt/LTM.pm view on Meta::CPAN
return $x;
}
### same as _nok() in Math::BigInt::Lib
sub _nok {
# Return binomial coefficient (n over k).
my ($class, $n, $k) = @_;
# If k > n/2, or, equivalently, 2*k > n, compute nok(n, k) as
# nok(n, n-k), to minimize the number if iterations in the loop.
{
my $twok = $class -> _mul($class -> _two(), $class -> _copy($k));
if ($class -> _acmp($twok, $n) > 0) {
$k = $class -> _sub($class -> _copy($n), $k);
}
}
# Example:
#
src/ltc/headers/tomcrypt_custom.h view on Meta::CPAN
/* Default number of nodes when decoding an OID. */
#define LTC_DER_OID_DEFAULT_NODES 12
#endif
#endif
#if defined(LTC_MECC) || defined(LTC_MRSA) || defined(LTC_MDSA) || defined(LTC_SSH)
/* Include the MPI functionality? (required by the PK algorithms) */
#define LTC_MPI
#ifndef LTC_PK_MAX_RETRIES
/* iterations limit for retry-loops */
#define LTC_PK_MAX_RETRIES 20
#endif
#endif
#ifdef LTC_MRSA
#define LTC_PKCS_1
#endif
#if defined(LTC_MRSA) || defined(LTC_MECC)
#define LTC_PKCS_8
src/ltc/headers/tomcrypt_private.h view on Meta::CPAN
unsigned long blocklen;
} pbes_properties;
typedef struct
{
pbes_properties type;
struct password pw;
ltc_asn1_list *enc_data;
ltc_asn1_list *salt;
ltc_asn1_list *iv;
unsigned long iterations;
/* only used for RC2 */
unsigned long key_bits;
} pbes_arg;
typedef struct {
const pbes_properties *data;
const char *oid;
} oid_to_pbes;
#endif
src/ltc/headers/tomcrypt_private.h view on Meta::CPAN
#ifdef LTC_PKCS_12
int pkcs12_utf8_to_utf16(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int pkcs12_kdf( int hash_id,
const unsigned char *pw, unsigned long pwlen,
const unsigned char *salt, unsigned long saltlen,
unsigned int iterations, unsigned char purpose,
unsigned char *out, unsigned long outlen);
#endif /* LTC_PKCS_12 */
/* tomcrypt_prng.h */
#define LTC_PRNG_EXPORT(which) \
int which ## _export(unsigned char *out, unsigned long *outlen, prng_state *prng) \
{ \
unsigned long len = which ## _desc.export_size; \
src/ltc/misc/argon2/argon2.c view on Meta::CPAN
Hash a password with Argon2 (RFC 9106)
@param pwd Password (or message)
@param pwdlen Length of password
@param salt Salt
@param saltlen Length of salt
@param secret Optional secret value (may be NULL)
@param secretlen Length of secret
@param ad Optional associated data (may be NULL)
@param adlen Length of associated data
@param t_cost Number of passes (iterations), minimum 1
@param m_cost Memory size in KiB, minimum 8*parallelism
@param parallelism Degree of parallelism (number of lanes), minimum 1
@param type ARGON2_D, ARGON2_I, or ARGON2_ID
@param out [out] Output tag
@param outlen Desired output length (4..2^32-1)
@return CRYPT_OK on success
*/
int argon2_hash(const unsigned char *pwd, unsigned long pwdlen,
const unsigned char *salt, unsigned long saltlen,
const unsigned char *secret, unsigned long secretlen,
src/ltc/misc/bcrypt/bcrypt.c view on Meta::CPAN
const unsigned char pt[] = "OxychromaticBlowfishSwatDynamite";
return s_bcrypt_hash(pt, pass, passlen, salt, saltlen, out, outlen);
}
/**
Compatible to bcrypt_pbkdf() as provided in OpenBSD
@param password The input password (or key)
@param password_len The length of the password (octets)
@param salt The salt (or nonce)
@param salt_len The length of the salt (octets)
@param rounds # of iterations desired [read specs for more]
@param hash_idx The index of the hash desired
@param out [out] The destination for this algorithm
@param outlen [in/out] The desired size of the algorithm output
@return CRYPT_OK if successful
*/
int bcrypt_pbkdf_openbsd(const void *secret, unsigned long secret_len,
const unsigned char *salt, unsigned long salt_len,
unsigned int rounds, int hash_idx,
unsigned char *out, unsigned long *outlen)
{
src/ltc/misc/pbes/pbes.c view on Meta::CPAN
if (arg->iv != NULL) {
iv = arg->iv->data;
} else {
iv = k + klen;
klen += arg->type.blocklen;
}
if (klen > sizeof(k)) return CRYPT_INVALID_ARG;
if ((err = arg->type.kdf(&arg->pw, arg->salt->data, arg->salt->size, arg->iterations, hid, k, &klen)) != CRYPT_OK) goto LBL_ERROR;
if ((err = cbc_start(cid, iv, k, keylen, 0, &cbc)) != CRYPT_OK) goto LBL_ERROR;
if ((err = cbc_decrypt(arg->enc_data->data, dec_data, arg->enc_data->size, &cbc)) != CRYPT_OK) goto LBL_ERROR;
if ((err = cbc_done(&cbc)) != CRYPT_OK) goto LBL_ERROR;
dlen = arg->enc_data->size;
if ((err = padding_depad(dec_data, &dlen, LTC_PAD_PKCS7)) != CRYPT_OK) goto LBL_ERROR;
diff = (long)arg->enc_data->size - (long)dlen;
if ((diff <= 0) || (diff > cipher_descriptor[cid].block_length)) {
err = CRYPT_PK_INVALID_PADDING;
goto LBL_ERROR;
}
src/ltc/misc/pbes/pbes1.c view on Meta::CPAN
!LTC_ASN1_IS_TYPE(s->next->child, LTC_ASN1_OCTET_STRING) ||
!LTC_ASN1_IS_TYPE(s->next->child->next, LTC_ASN1_INTEGER)) {
return CRYPT_INVALID_PACKET;
}
/* PBES1: encrypted pkcs8 - pbeWithMD5AndDES-CBC:
* 0:d=0 hl=4 l= 329 cons: SEQUENCE
* 4:d=1 hl=2 l= 27 cons: SEQUENCE
* 6:d=2 hl=2 l= 9 prim: OBJECT :pbeWithMD5AndDES-CBC (== 1.2.840.113549.1.5.3) (== *s)
* 17:d=2 hl=2 l= 14 cons: SEQUENCE (== *lalgparam)
* 19:d=3 hl=2 l= 8 prim: OCTET STRING [HEX DUMP]:8EDF749A06CCDE51 (== salt)
* 29:d=3 hl=2 l= 2 prim: INTEGER :0800 (== iterations)
* 33:d=1 hl=4 l= 296 prim: OCTET STRING :bytes (== encrypted data)
*/
res->salt = s->next->child;
res->iterations = ltc_mp_get_int(s->next->child->next->data);
return CRYPT_OK;
}
#endif
src/ltc/misc/pbes/pbes2.c view on Meta::CPAN
}
/* PBES2: encrypted pkcs8 - PBES2+PBKDF2+des-ede3-cbc:
* 0:d=0 hl=4 l= 380 cons: SEQUENCE
* 4:d=1 hl=2 l= 78 cons: SEQUENCE
* 6:d=2 hl=2 l= 9 prim: OBJECT :PBES2 (== 1.2.840.113549.1.5.13) (== *s)
* 17:d=2 hl=2 l= 65 cons: SEQUENCE
* 19:d=3 hl=2 l= 41 cons: SEQUENCE
* 21:d=4 hl=2 l= 9 prim: OBJECT :PBKDF2 (== *lkdf)
* 32:d=4 hl=2 l= 28 cons: SEQUENCE
* 34:d=5 hl=2 l= 8 prim: OCTET STRING [HEX DUMP]:28BA4ABF6AA76A3D (== res->salt)
* 44:d=5 hl=2 l= 2 prim: INTEGER :0800 (== res->iterations, *liter)
* 48:d=5 hl=2 l= 12 cons: SEQUENCE (== *loptseq - this sequence is optional, may be missing)
* 50:d=6 hl=2 l= 8 prim: OBJECT :hmacWithSHA256 (== *lhmac)
* 60:d=6 hl=2 l= 0 prim: NULL
* 62:d=3 hl=2 l= 20 cons: SEQUENCE
* 64:d=4 hl=2 l= 8 prim: OBJECT :des-ede3-cbc (== *lenc)
* 74:d=4 hl=2 l= 8 prim: OCTET STRING [HEX DUMP]:B1404C4688DC9A5A
* 84:d=1 hl=4 l= 296 prim: OCTET STRING :bytes (== encrypted data)
*/
lkdf = s->next->child->child;
lenc = s->next->child->next->child;
src/ltc/misc/pbes/pbes2.c view on Meta::CPAN
if (!LTC_ASN1_IS_TYPE(lkdf->next, LTC_ASN1_SEQUENCE) ||
!LTC_ASN1_IS_TYPE(lkdf->next->child, LTC_ASN1_OCTET_STRING) ||
!LTC_ASN1_IS_TYPE(lkdf->next->child->next, LTC_ASN1_INTEGER)) {
return CRYPT_INVALID_PACKET;
}
liter = lkdf->next->child->next;
loptseq = liter->next;
res->salt = lkdf->next->child;
res->iterations = ltc_mp_get_int(liter->data);
/* There's an optional INTEGER keyLength after the iterations, skip that if it's there.
* c.f. RFC 2898 A.2 PBKDF2 */
if(LTC_ASN1_IS_TYPE(loptseq, LTC_ASN1_INTEGER)) {
loptseq = loptseq->next;
}
/* this sequence is optional */
lhmac = NULL;
if (LTC_ASN1_IS_TYPE(loptseq, LTC_ASN1_SEQUENCE) &&
LTC_ASN1_IS_TYPE(loptseq->child, LTC_ASN1_OBJECT_IDENTIFIER)) {
lhmac = loptseq->child;
src/ltc/misc/pkcs12/pkcs12_kdf.c view on Meta::CPAN
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
#ifdef LTC_PKCS_12
int pkcs12_kdf( int hash_id,
const unsigned char *pw, unsigned long pwlen,
const unsigned char *salt, unsigned long saltlen,
unsigned int iterations, unsigned char purpose,
unsigned char *out, unsigned long outlen)
{
unsigned long u = hash_descriptor[hash_id].hashsize;
unsigned long v = hash_descriptor[hash_id].blocksize;
unsigned long c = (outlen + u - 1) / u;
unsigned long Slen = ((saltlen + v - 1) / v) * v;
unsigned long Plen = ((pwlen + v - 1) / v) * v;
unsigned long k = (Plen + Slen) / v;
unsigned long Alen, keylen = 0;
unsigned int tmp, i, j, n;
src/ltc/misc/pkcs12/pkcs12_kdf.c view on Meta::CPAN
zeromem(key, u * c);
for (i = 0; i < v; i++) D[i] = purpose; /* D - diversifier */
for (i = 0; i < Slen; i++) I[i] = salt[i % saltlen];
for (i = 0; i < Plen; i++) I[Slen + i] = pw[i % pwlen]; /* I = Salt || Pass */
for (i = 0; i < c; i++) {
Alen = sizeof(A);
err = hash_memory_multi(hash_id, A, &Alen, D, v, I, Slen + Plen, LTC_NULL); /* A = HASH(D || I) */
if (err != CRYPT_OK) goto DONE;
for (j = 1; j < iterations; j++) {
err = hash_memory(hash_id, A, Alen, A, &Alen); /* A = HASH(A) */
if (err != CRYPT_OK) goto DONE;
}
/* fill buffer B with A */
for (j = 0; j < v; j++) B[j] = A[j % Alen];
/* B += 1 */
for (j = v; j > 0; j--) {
if (++B[j - 1] != 0) break;
}
/* I_n += B */
src/ltc/misc/pkcs5/pkcs_5_2.c view on Meta::CPAN
PKCS #5, Algorithm #2, Tom St Denis
*/
#ifdef LTC_PKCS_5
/**
Execute PKCS #5 v2
@param password The input password (or key)
@param password_len The length of the password (octets)
@param salt The salt (or nonce)
@param salt_len The length of the salt (octets)
@param iteration_count # of iterations desired for PKCS #5 v2 [read specs for more]
@param hash_idx The index of the hash desired
@param out [out] The destination for this algorithm
@param outlen [in/out] The max size and resulting size of the algorithm output
@return CRYPT_OK if successful
*/
int pkcs_5_alg2(const unsigned char *password, unsigned long password_len,
const unsigned char *salt, unsigned long salt_len,
int iteration_count, int hash_idx,
unsigned char *out, unsigned long *outlen)
{
src/ltc/pk/dh/dh_generate_key.c view on Meta::CPAN
if (groupsize <= 1024) {
return 77; /* 8192-bit => key size 616-bit */
}
return 0;
}
int dh_generate_key(prng_state *prng, int wprng, dh_key *key)
{
unsigned char *buf;
unsigned long keysize;
int err, max_iterations = LTC_PK_MAX_RETRIES;
LTC_ARGCHK(key != NULL);
LTC_ARGCHK(ltc_mp.name != NULL);
/* good prng? */
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
return err;
}
keysize = s_dh_groupsize_to_keysize(ltc_mp_unsigned_bin_size(key->prime));
src/ltc/pk/dh/dh_generate_key.c view on Meta::CPAN
}
/* load the x value - private key */
if ((err = ltc_mp_read_unsigned_bin(key->x, buf, keysize)) != CRYPT_OK) {
goto freebuf;
}
/* compute the y value - public key */
if ((err = ltc_mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) {
goto freebuf;
}
err = dh_check_pubkey(key);
} while (err != CRYPT_OK && max_iterations-- > 0);
freebuf:
zeromem(buf, keysize);
XFREE(buf);
freemp:
if (err != CRYPT_OK) dh_free(key);
return err;
}
#endif /* LTC_MDH */
src/ltc/pk/ecc/ecc_sign_hash_internal.c view on Meta::CPAN
#include "tomcrypt_private.h"
#ifdef LTC_MECC
int ecc_sign_hash_internal(const unsigned char *in, unsigned long inlen,
void *r, void *s, ltc_ecc_sig_opts *opts, const ecc_key *key)
{
ecc_key pubkey;
void *e, *p, *b;
int v = 0;
int err, max_iterations = LTC_PK_MAX_RETRIES;
unsigned long pbits, pbytes, i, shift_right;
unsigned char ch, buf[MAXBLOCKSIZE];
LTC_ARGCHK(r != NULL);
LTC_ARGCHK(s != NULL);
LTC_ARGCHK(in != NULL);
LTC_ARGCHK(opts != NULL);
LTC_ARGCHK(key != NULL);
/* is this a private key? */
src/ltc/pk/ecc/ecc_sign_hash_internal.c view on Meta::CPAN
if ((err = ltc_mp_mulmod(key->k, r, p, s)) != CRYPT_OK) { goto error; } /* s = xr */
if ((err = ltc_mp_mulmod(pubkey.k, s, p, s)) != CRYPT_OK) { goto error; } /* s = xr/kb */
if ((err = ltc_mp_mulmod(pubkey.k, e, p, e)) != CRYPT_OK) { goto error; } /* e = e/kb */
if ((err = ltc_mp_add(e, s, s)) != CRYPT_OK) { goto error; } /* s = e/kb + xr/kb */
if ((err = ltc_mp_mulmod(s, b, p, s)) != CRYPT_OK) { goto error; } /* s = b(e/kb + xr/kb) = (e + xr)/k */
ecc_free(&pubkey);
if (ltc_mp_iszero(s) == LTC_MP_NO) {
break;
}
}
} while (--max_iterations > 0);
if (max_iterations == 0) {
goto errnokey;
}
if (opts->enable_recovery_id) opts->recovery_id = v;
goto errnokey;
error:
ecc_free(&pubkey);
errnokey:
ltc_mp_deinit_multi(e, b, LTC_NULL);
src/ltm/mp_error_to_string.c view on Meta::CPAN
switch (code) {
case MP_OKAY:
return "Successful";
case MP_ERR:
return "Unknown error";
case MP_MEM:
return "Out of heap";
case MP_VAL:
return "Value out of range";
case MP_ITER:
return "Max. iterations reached";
case MP_BUF:
return "Buffer overflow";
case MP_OVF:
return "Integer overflow";
default:
return "Invalid error code";
}
}
#endif
src/ltm/tommath.h view on Meta::CPAN
MP_LT = -1, /* less than */
MP_EQ = 0, /* equal */
MP_GT = 1 /* greater than */
} mp_ord;
typedef enum {
MP_OKAY = 0, /* no error */
MP_ERR = -1, /* unknown error */
MP_MEM = -2, /* out of mem */
MP_VAL = -3, /* invalid input */
MP_ITER = -4, /* maximum iterations reached */
MP_BUF = -5, /* buffer overflow, supplied buffer too small */
MP_OVF = -6 /* mp_int overflow, too many digits */
} mp_err;
typedef enum {
MP_LSB_FIRST = -1,
MP_MSB_FIRST = 1
} mp_order;
typedef enum {