CryptX

 view release on metacpan or  search on metacpan

src/ltc/headers/tomcrypt_pk.h  view on Meta::CPAN

int rsa_verify_hash_ex(const unsigned char *sig,            unsigned long  siglen,
                       const unsigned char *hash,           unsigned long  hashlen,
                             int            padding,
                             int            hash_idx,       unsigned long  saltlen,
                             int           *stat,     const rsa_key       *key);
#endif /* LTC_NO_DEPRECATED_APIS */

#define rsa_sign_saltlen_get_max(hash_idx, key) \
  rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, hash_idx, key)

int rsa_sign_saltlen_get_max_ex(int padding, int hash_idx, const rsa_key *key);

/* PKCS #1 import/export */
int rsa_export(unsigned char *out, unsigned long *outlen, int type, const rsa_key *key);
int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);

int rsa_import_x509(const unsigned char *in, unsigned long inlen, rsa_key *key);
int rsa_import_pkcs8(const unsigned char *in, unsigned long inlen,
                     const password_ctx  *pw_ctx, rsa_key *key);

int rsa_set_key(const unsigned char *N,  unsigned long Nlen,
                const unsigned char *e,  unsigned long elen,
                const unsigned char *d,  unsigned long dlen,
                rsa_key *key);
int rsa_set_factors(const unsigned char *p,  unsigned long plen,
                    const unsigned char *q,  unsigned long qlen,
                    rsa_key *key);
int rsa_set_crt_params(const unsigned char *dP, unsigned long dPlen,
                       const unsigned char *dQ, unsigned long dQlen,
                       const unsigned char *qP, unsigned long qPlen,
                       rsa_key *key);
#endif

/* ---- DH Routines ---- */
#ifdef LTC_MDH

typedef struct {
    int type;
    void *x;
    void *y;
    void *base;
    void *prime;
} dh_key;

int dh_get_groupsize(const dh_key *key);

int dh_export(unsigned char *out, unsigned long *outlen, int type, const dh_key *key);
int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
int dh_import_pkcs8(const unsigned char *in, unsigned long inlen,
                    const password_ctx  *pw_ctx, dh_key *key);

int dh_set_pg(const unsigned char *p, unsigned long plen,
              const unsigned char *g, unsigned long glen,
              dh_key *key);
int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh_key *key);
int dh_set_pg_groupsize(int groupsize, dh_key *key);

int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key);
int dh_generate_key(prng_state *prng, int wprng, dh_key *key);

int dh_shared_secret(const dh_key  *private_key, const dh_key  *public_key,
                     unsigned char *out,         unsigned long *outlen);

void dh_free(dh_key *key);

int dh_export_key(void *out, unsigned long *outlen, int type, const dh_key *key);
#endif /* LTC_MDH */


/* ---- ECC Routines ---- */
#ifdef LTC_MECC

/* size of our temp buffers for exported keys */
#define ECC_BUF_SIZE 256

/* max private key size */
#define ECC_MAXSIZE  66

/** Structure defines a GF(p) curve */
typedef struct {
   /** The prime that defines the field the curve is in (encoded in hex) */
   const char *prime;

   /** The fields A param (hex) */
   const char *A;

   /** The fields B param (hex) */
   const char *B;

   /** The order of the curve (hex) */
   const char *order;

   /** The x co-ordinate of the base point on the curve (hex) */
   const char *Gx;

   /** The y co-ordinate of the base point on the curve (hex) */
   const char *Gy;

   /** The co-factor */
   unsigned long cofactor;

   /** The OID */
   const char *OID;
} ltc_ecc_curve;

/** A point on a ECC curve, stored in Jacbobian format such that (x,y,z) => (x/z^2, y/z^3, 1) when interpretted as affine */
typedef struct {
    /** The x co-ordinate */
    void *x;

    /** The y co-ordinate */
    void *y;

    /** The z co-ordinate */
    void *z;
} ecc_point;

/** ECC key's domain parameters */
typedef struct {
   /** The size of the curve in octets */
   int size;

src/ltc/headers/tomcrypt_pk.h  view on Meta::CPAN

   /* raw R, S, V (+27) values */
   LTC_ECCSIG_ETH27      = 0x2,
   /* SSH + ECDSA signature format defined by RFC5656 */
   LTC_ECCSIG_RFC5656    = 0x3,
} ecc_signature_type;

typedef struct ltc_ecc_sig_opts {
   /** Signature type */
   ecc_signature_type type;
   /** The PRNG to use.
    *  This must be set in case deterministic signature generation
    *  according to RFC6979 is not enabled.
    */
   prng_state *prng;
   int wprng;

   /** Enable generation of a recovery ID.
    *  This must be set in case one requires the recovery ID of a
    *  signature operation.
    */
   int *recid;

   /** The hash algorithm to use when creating a signature.
    *  Setting this will enable RFC6979 compatible signature generation.
    */
   const char *rfc6979_hash_alg;
} ltc_ecc_sig_opts;

/** the ECC params provided */
extern const ltc_ecc_curve ltc_ecc_curves[];

void ecc_sizes(int *low, int *high);
int  ecc_get_size(const ecc_key *key);

int  ecc_find_curve(const char* name_or_oid, const ltc_ecc_curve** cu);
int  ecc_set_curve(const ltc_ecc_curve *cu, ecc_key *key);
int  ecc_generate_key(prng_state *prng, int wprng, ecc_key *key);
int  ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key *key);
int  ecc_get_key(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
int  ecc_get_oid_str(char *out, unsigned long *outlen, const ecc_key *key);

int  ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
int  ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_curve *cu);
void ecc_free(ecc_key *key);

#if defined(LTC_DER)
int  ecc_export(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
int  ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
int  ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_curve *cu);

int  ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
int  ecc_import_openssl(const unsigned char *in, unsigned long inlen, ecc_key *key);
int  ecc_import_pkcs8(const unsigned char *in, unsigned long inlen, const password_ctx *pw_ctx, ecc_key *key);
int  ecc_import_x509(const unsigned char *in, unsigned long inlen, ecc_key *key);
#endif

int  ecc_ansi_x963_export(const ecc_key *key, unsigned char *out, unsigned long *outlen);
int  ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
int  ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_curve *cu);

int  ecc_shared_secret(const ecc_key *private_key, const ecc_key *public_key,
                       unsigned char *out, unsigned long *outlen);

int ecc_sign_hash_v2(const unsigned char    *in,
                           unsigned long     inlen,
                           unsigned char    *out,
                           unsigned long    *outlen,
                           ltc_ecc_sig_opts *opts,
                     const       ecc_key    *key);

int ecc_verify_hash_v2(const unsigned char *sig,
                             unsigned long  siglen,
                       const unsigned char *hash,
                             unsigned long  hashlen,
                          ltc_ecc_sig_opts *opts,
                                       int *stat,
                       const       ecc_key *key);

#if defined(LTC_DER)
int  ecc_encrypt_key(const unsigned char *in,   unsigned long inlen,
                           unsigned char *out,  unsigned long *outlen,
                           prng_state *prng, int wprng, int hash,
                           const ecc_key *key);

int  ecc_decrypt_key(const unsigned char *in,  unsigned long  inlen,
                           unsigned char *out, unsigned long *outlen,
                           const ecc_key *key);
#endif /* LTC_DER */

#define ltc_ecc_sign_hash(i, il, o, ol, p, wp, k)         \
      ecc_sign_hash_v2(i, il, o, ol,                      \
                       &(ltc_ecc_sig_opts){               \
                           .type = LTC_ECCSIG_ANSIX962,   \
                           .prng = p,                     \
                           .wprng = wp,                   \
                        }, k)
#define ltc_ecc_sign_hash_rfc7518(i, il, o, ol, p, wp, k)    \
      ecc_sign_hash_v2(i, il, o, ol,                         \
                       &(ltc_ecc_sig_opts){                  \
                           .type = LTC_ECCSIG_RFC7518,       \
                           .prng = p,                        \
                           .wprng = wp,                      \
                        }, k)

#define ltc_ecc_verify_hash(s, sl, h, hl, st, k)          \
      ecc_verify_hash_v2(s, sl, h, hl,                    \
                         &(ltc_ecc_sig_opts){             \
                             .type = LTC_ECCSIG_ANSIX962, \
                          }, st, k)
#define ltc_ecc_verify_hash_rfc7518(s, sl, h, hl, st, k)     \
      ecc_verify_hash_v2(s, sl, h, hl,                       \
                         &(ltc_ecc_sig_opts){                \
                             .type = LTC_ECCSIG_RFC7518,     \
                          }, st, k)

#ifdef LTC_NO_DEPRECATED_APIS
#define ecc_sign_hash ltc_ecc_sign_hash
#define ecc_verify_hash ltc_ecc_verify_hash
#define ecc_sign_hash_rfc7518 ltc_ecc_sign_hash_rfc7518
#define ecc_verify_hash_rfc7518 ltc_ecc_verify_hash_rfc7518
#else /* LTC_NO_DEPRECATED_APIS */

src/ltc/headers/tomcrypt_pk.h  view on Meta::CPAN

LTC_DEPRECATED(ecc_sign_hash_v2)
int ecc_sign_hash_rfc7518(const unsigned char *in,
                                unsigned long  inlen,
                                unsigned char *out,
                                unsigned long *outlen,
                                   prng_state *prng,
                                          int  wprng,
                          const       ecc_key *key);

LTC_DEPRECATED(ecc_verify_hash_v2)
int ecc_verify_hash_rfc7518(const unsigned char *sig,
                                  unsigned long  siglen,
                            const unsigned char *hash,
                                  unsigned long  hashlen,
                                            int *stat,
                            const       ecc_key *key);
#endif /* LTC_NO_DEPRECATED_APIS */

int  ecc_recover_key(const unsigned char *sig,
                           unsigned long  siglen,
                     const unsigned char *hash,
                           unsigned long  hashlen,
                        ltc_ecc_sig_opts *opts,
                                 ecc_key *key);

#endif

#ifdef LTC_CURVE25519

typedef struct {
   /** The key type, PK_PRIVATE or PK_PUBLIC */
   enum public_key_type type;

   /** The PK-algorithm, LTC_PKA_ED25519 or LTC_PKA_X25519 */
   enum ltc_pka_id pka;

   /** The private key */
   unsigned char priv[32];

   /** The public key */
   unsigned char pub[32];
} curve25519_key;


/** Ed25519 Signature API */
int ed25519_make_key(prng_state *prng, int wprng, curve25519_key *key);

int ed25519_export(       unsigned char *out, unsigned long *outlen,
                                    int  which,
                   const curve25519_key *key);

int ed25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *key);
int ed25519_import_raw(const unsigned char *in, unsigned long inlen, int which, curve25519_key *key);
int ed25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key);
int ed25519_import_pkcs8(const unsigned char  *in, unsigned long inlen,
                         const password_ctx   *pw_ctx,
                               curve25519_key *key);

int ed25519_sign(const  unsigned char *msg, unsigned long msglen,
                        unsigned char *sig, unsigned long *siglen,
                 const curve25519_key *private_key);
int ed25519ctx_sign(const  unsigned char *msg, unsigned long  msglen,
                           unsigned char *sig, unsigned long *siglen,
                    const  unsigned char *ctx, unsigned long  ctxlen,
                    const curve25519_key *private_key);
int ed25519ph_sign(const  unsigned char *msg, unsigned long  msglen,
                          unsigned char *sig, unsigned long *siglen,
                   const  unsigned char *ctx, unsigned long  ctxlen,
                   const curve25519_key *private_key);
int ed25519_verify(const  unsigned char *msg, unsigned long msglen,
                   const  unsigned char *sig, unsigned long siglen,
                                    int *stat,
                   const curve25519_key *public_key);
int ed25519ctx_verify(const  unsigned char *msg, unsigned long msglen,
                      const  unsigned char *sig, unsigned long siglen,
                      const  unsigned char *ctx, unsigned long ctxlen,
                                       int *stat,
                      const curve25519_key *public_key);
int ed25519ph_verify(const  unsigned char *msg, unsigned long msglen,
                     const  unsigned char *sig, unsigned long siglen,
                     const  unsigned char *ctx, unsigned long ctxlen,
                                      int *stat,
                     const curve25519_key *public_key);

/** X25519 Key-Exchange API */
int x25519_make_key(prng_state *prng, int wprng, curve25519_key *key);

int x25519_export(       unsigned char *out, unsigned long *outlen,
                                   int  which,
                  const curve25519_key *key);

int x25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *key);
int x25519_import_raw(const unsigned char *in, unsigned long inlen, int which, curve25519_key *key);
int x25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key);
int x25519_import_pkcs8(const unsigned char  *in, unsigned long inlen,
                        const password_ctx   *pw_ctx,
                              curve25519_key *key);

int x25519_shared_secret(const curve25519_key *private_key,
                         const curve25519_key *public_key,
                                unsigned char *out, unsigned long *outlen);

#endif /* LTC_CURVE25519 */

#ifdef LTC_MDSA

/* Max diff between group and modulus size in bytes (max case: L=8192bits, N=256bits) */
#define LTC_MDSA_DELTA 992

/* Max DSA group size in bytes */
#define LTC_MDSA_MAX_GROUP 64

/* Max DSA modulus size in bytes (the actual DSA size, max 8192 bits) */
#define LTC_MDSA_MAX_MODULUS 1024

/** DSA key structure */
typedef struct {
   /** The key type, PK_PRIVATE or PK_PUBLIC */
   int type;

   /** The order of the sub-group used in octets */
   int qord;

   /** The generator  */
   void *g;

   /** The prime used to generate the sub-group */
   void *q;

   /** The large prime that generats the field the contains the sub-group */
   void *p;

   /** The private key */
   void *x;

   /** The public key */
   void *y;
} dsa_key;

int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);

int dsa_set_pqg(const unsigned char *p,  unsigned long plen,
                const unsigned char *q,  unsigned long qlen,
                const unsigned char *g,  unsigned long glen,
                dsa_key *key);
int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen, dsa_key *key);
int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);

int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key);
int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key);

void dsa_free(dsa_key *key);

int dsa_sign_hash_raw(const unsigned char *in,  unsigned long inlen,
                                   void *r,   void *s,
                               prng_state *prng, int wprng, const dsa_key *key);

int dsa_sign_hash(const unsigned char *in,  unsigned long inlen,
                        unsigned char *out, unsigned long *outlen,
                        prng_state *prng, int wprng, const dsa_key *key);

int dsa_verify_hash_raw(         void *r,          void *s,
                    const unsigned char *hash, unsigned long hashlen,
                                    int *stat, const dsa_key *key);

int dsa_verify_hash(const unsigned char *sig,        unsigned long  siglen,
                    const unsigned char *hash,       unsigned long  hashlen,
                          int           *stat, const dsa_key       *key);

int dsa_encrypt_key(const unsigned char *in,   unsigned long inlen,
                          unsigned char *out,  unsigned long *outlen,
                          prng_state    *prng, int wprng, int hash,
                    const dsa_key       *key);

int dsa_decrypt_key(const unsigned char *in,  unsigned long  inlen,
                          unsigned char *out, unsigned long *outlen,
                    const dsa_key       *key);

int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
int dsa_import_pkcs8(const unsigned char *in, unsigned long inlen,
                     const password_ctx  *pw_ctx,
                     dsa_key *key);
int dsa_export(unsigned char *out, unsigned long *outlen, int type, const dsa_key *key);
int dsa_verify_key(const dsa_key *key, int *stat);
int dsa_shared_secret(void          *private_key, void *base,
                      const dsa_key *public_key,
                      unsigned char *out,         unsigned long *outlen);
#endif /* LTC_MDSA */

/*
 * LibTomCrypt tagged-union for holding a Public Key
 */

typedef struct {
   union {
#ifdef LTC_CURVE25519
      curve25519_key x25519;
      curve25519_key ed25519;
#endif
#ifdef LTC_MDH
      dh_key dh;
#endif
#ifdef LTC_MDSA
      dsa_key dsa;
#endif
#ifdef LTC_MECC
      ecc_key ecc;
#endif
#ifdef LTC_MRSA
      rsa_key rsa;
#endif
      char dummy;
   } u;
   enum ltc_pka_id id;
} ltc_pka_key;

void pka_key_free(ltc_pka_key *key);
void pka_key_destroy(ltc_pka_key **key);

#ifdef LTC_DER
/* DER handling */

typedef enum ltc_asn1_type_ {
 /*  0 */
 LTC_ASN1_EOL,
 LTC_ASN1_BOOLEAN,
 LTC_ASN1_INTEGER,
 LTC_ASN1_SHORT_INTEGER,
 LTC_ASN1_BIT_STRING,
 /*  5 */
 LTC_ASN1_OCTET_STRING,
 LTC_ASN1_NULL,
 LTC_ASN1_OBJECT_IDENTIFIER,
 LTC_ASN1_IA5_STRING,
 LTC_ASN1_PRINTABLE_STRING,
 /* 10 */
 LTC_ASN1_UTF8_STRING,
 LTC_ASN1_UTCTIME,
 LTC_ASN1_CHOICE,
 LTC_ASN1_SEQUENCE,
 LTC_ASN1_SET,
 /* 15 */
 LTC_ASN1_SETOF,
 LTC_ASN1_RAW_BIT_STRING,
 LTC_ASN1_TELETEX_STRING,



( run in 0.826 second using v1.01-cache-2.11-cpan-39bf76dae61 )