⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tomcrypt_pk.h

📁 AES加密算法对socket通信过程进行加密传输
💻 H
📖 第 1 页 / 共 2 页
字号:
/* ---- NUMBER THEORY ---- */enum {   PK_PUBLIC=0,   PK_PRIVATE=1};int rand_prime(void *N, long len, prng_state *prng, int wprng);/* ---- RSA ---- */#ifdef LTC_MRSA/* Min and Max RSA key sizes (in bits) */#define MIN_RSA_SIZE 1024#define MAX_RSA_SIZE 4096/** RSA LTC_PKCS style key */typedef struct Rsa_key {    /** Type of key, PK_PRIVATE or PK_PUBLIC */    int type;    /** The public exponent */    void *e;     /** The private exponent */    void *d;     /** The modulus */    void *N;     /** The p factor of N */    void *p;     /** The q factor of N */    void *q;     /** The 1/q mod p CRT param */    void *qP;     /** The d mod (p - 1) CRT param */    void *dP;     /** The d mod (q - 1) CRT param */    void *dQ;} rsa_key;int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);int rsa_exptmod(const unsigned char *in,   unsigned long inlen,                      unsigned char *out,  unsigned long *outlen, int which,                      rsa_key *key);void rsa_free(rsa_key *key);/* These use LTC_PKCS #1 v2.0 padding */#define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, _key) \  rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, LTC_LTC_PKCS_1_OAEP, _key)#define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \  rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_LTC_PKCS_1_OAEP, _stat, _key)#define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _prng_idx, _hash_idx, _saltlen, _key) \  rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_LTC_PKCS_1_PSS, _prng, _prng_idx, _hash_idx, _saltlen, _key)#define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \  rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key)/* These can be switched between LTC_PKCS #1 v2.x and LTC_PKCS #1 v1.5 paddings */int rsa_encrypt_key_ex(const unsigned char *in,     unsigned long inlen,                             unsigned char *out,    unsigned long *outlen,                       const unsigned char *lparam, unsigned long lparamlen,                       prng_state *prng, int prng_idx, int hash_idx, int padding, rsa_key *key);int rsa_decrypt_key_ex(const unsigned char *in,       unsigned long  inlen,                             unsigned char *out,      unsigned long *outlen,                       const unsigned char *lparam,   unsigned long  lparamlen,                             int            hash_idx, int            padding,                             int           *stat,     rsa_key       *key);int rsa_sign_hash_ex(const unsigned char *in,       unsigned long  inlen,                           unsigned char *out,      unsigned long *outlen,                           int            padding,                           prng_state    *prng,     int            prng_idx,                           int            hash_idx, unsigned long  saltlen,                           rsa_key *key);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,     rsa_key      *key);/* LTC_PKCS #1 import/export */int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key);int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);                        #endif/* ---- Katja ---- */#ifdef MKAT/* Min and Max KAT key sizes (in bits) */#define MIN_KAT_SIZE 1024#define MAX_KAT_SIZE 4096/** Katja LTC_PKCS style key */typedef struct KAT_key {    /** Type of key, PK_PRIVATE or PK_PUBLIC */    int type;    /** The private exponent */    void *d;     /** The modulus */    void *N;     /** The p factor of N */    void *p;     /** The q factor of N */    void *q;     /** The 1/q mod p CRT param */    void *qP;     /** The d mod (p - 1) CRT param */    void *dP;     /** The d mod (q - 1) CRT param */    void *dQ;    /** The pq param */    void *pq;} katja_key;int katja_make_key(prng_state *prng, int wprng, int size, katja_key *key);int katja_exptmod(const unsigned char *in,   unsigned long inlen,                        unsigned char *out,  unsigned long *outlen, int which,                        katja_key *key);void katja_free(katja_key *key);/* These use LTC_PKCS #1 v2.0 padding */int katja_encrypt_key(const unsigned char *in,     unsigned long inlen,                            unsigned char *out,    unsigned long *outlen,                      const unsigned char *lparam, unsigned long lparamlen,                      prng_state *prng, int prng_idx, int hash_idx, katja_key *key);                                        int katja_decrypt_key(const unsigned char *in,       unsigned long inlen,                            unsigned char *out,      unsigned long *outlen,                       const unsigned char *lparam,   unsigned long lparamlen,                            int            hash_idx, int *stat,                            katja_key       *key);/* LTC_PKCS #1 import/export */int katja_export(unsigned char *out, unsigned long *outlen, int type, katja_key *key);int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key);                        #endif/* ---- 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 NIST GF(p) curve */typedef struct {   /** The size of the curve in octets */   int size;   /** name of curve */   char *name;    /** The prime that defines the field the curve is in (encoded in hex) */   char *prime;   /** The fields B param (hex) */   char *B;   /** The order of the curve (hex) */   char *order;     /** The x co-ordinate of the base point on the curve (hex) */   char *Gx;    /** The y co-ordinate of the base point on the curve (hex) */   char *Gy;} ltc_ecc_set_type;/** 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;/** An ECC key */typedef struct {    /** Type of key, PK_PRIVATE or PK_PUBLIC */    int type;    /** Index into the ltc_ecc_sets[] for the parameters of this curve; if -1, then this key is using user supplied curve in dp */    int idx;	/** pointer to domain parameters; either points to NIST curves (identified by idx >= 0) or user supplied curve */	const ltc_ecc_set_type *dp;    /** The public key */    ecc_point pubkey;    /** The private key */    void *k;} ecc_key;/** the ECC params provided */extern const ltc_ecc_set_type ltc_ecc_sets[];int  ecc_test(void);void ecc_sizes(int *low, int *high);int  ecc_get_size(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_set_type *dp);void ecc_free(ecc_key *key);int  ecc_export(unsigned char *out, unsigned long *outlen, int type, 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_set_type *dp);int ecc_ansi_x963_export(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, ltc_ecc_set_type *dp);int  ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,                        unsigned char *out, unsigned long *outlen);int  ecc_encrypt_key(const unsigned char *in,   unsigned long inlen,                           unsigned char *out,  unsigned long *outlen,                            prng_state *prng, int wprng, int hash,                            ecc_key *key);int  ecc_decrypt_key(const unsigned char *in,  unsigned long  inlen,                           unsigned char *out, unsigned long *outlen,                            ecc_key *key);int  ecc_sign_hash(const unsigned char *in,  unsigned long inlen,                          unsigned char *out, unsigned long *outlen,                          prng_state *prng, int wprng, ecc_key *key);int  ecc_verify_hash(const unsigned char *sig,  unsigned long siglen,                     const unsigned char *hash, unsigned long hashlen,                      int *stat, ecc_key *key);/* low level functions */ecc_point *ltc_ecc_new_point(void);void       ltc_ecc_del_point(ecc_point *p);int        ltc_ecc_is_valid_idx(int n);/* point ops (mp == montgomery digit) */#if !defined(LTC_MECC_ACCEL) || defined(LTM_LTC_DESC) || defined(GMP_LTC_DESC)/* R = 2P */int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp);/* R = P + Q */int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp);#endif#if defined(LTC_MECC_FP)/* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);/* functions for saving/loading/freeing/adding to fixed point cache */int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);void ltc_ecc_fp_free(void);int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock);/* lock/unlock all points currently in fixed point cache */void ltc_ecc_fp_tablelock(int lock);#endif/* R = kG */int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -