diff mycrypt_pk.h @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents 6362d3854bb4
children c2b93763dac9
line wrap: on
line diff
--- a/mycrypt_pk.h	Tue Jun 15 14:07:21 2004 +0000
+++ b/mycrypt_pk.h	Sun Dec 19 11:34:45 2004 +0000
@@ -44,8 +44,8 @@
      }                                                           \
 }
 
-extern int is_prime(mp_int *, int *);
-extern int rand_prime(mp_int *N, long len, prng_state *prng, int wprng);
+ int is_prime(mp_int *, int *);
+ int rand_prime(mp_int *N, long len, prng_state *prng, int wprng);
 
 #else
    #ifdef MRSA
@@ -67,13 +67,12 @@
 
 #define PK_PRIVATE            0        /* PK private keys */
 #define PK_PUBLIC             1        /* PK public keys */
-#define PK_PRIVATE_OPTIMIZED  2        /* PK private key [rsa optimized] */
 
 /* ---- PACKET ---- */
 #ifdef PACKET
 
-extern void packet_store_header(unsigned char *dst, int section, int subsection);
-extern int packet_valid_header(unsigned char *src, int section, int subsection);
+ void packet_store_header(unsigned char *dst, int section, int subsection);
+ int packet_valid_header(unsigned char *src, int section, int subsection);
 
 #endif
 
@@ -90,19 +89,19 @@
 
 typedef struct Rsa_key {
     int type;
-    mp_int e, d, N, qP, pQ, dP, dQ, p, q;
+    mp_int e, d, N, p, q, qP, dP, dQ;
 } rsa_key;
 
-extern int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
+ int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
 
-extern int rsa_exptmod(const unsigned char *in,   unsigned long inlen,
+ int rsa_exptmod(const unsigned char *in,   unsigned long inlen,
                       unsigned char *out,  unsigned long *outlen, int which,
                       prng_state    *prng, int           prng_idx,
                       rsa_key *key);
 
 #ifdef RSA_TIMING
 
-extern int tim_exptmod(prng_state *prng, int prng_idx, 
+ int tim_exptmod(prng_state *prng, int prng_idx, 
                        mp_int *c, mp_int *e, mp_int *d, mp_int *n, mp_int *m);
 
 #else
@@ -111,8 +110,9 @@
 
 #endif
 
-extern void rsa_free(rsa_key *key);
+ void rsa_free(rsa_key *key);
 
+/* These use PKCS #1 v2.0 padding */
 int rsa_encrypt_key(const unsigned char *inkey,  unsigned long inlen,
                           unsigned char *outkey, unsigned long *outlen,
                     const unsigned char *lparam, unsigned long lparamlen,
@@ -137,6 +137,30 @@
                           int            hash_idx, unsigned long saltlen,
                           int           *stat,     rsa_key      *key);
 
+/* these use PKCS #1 v1.5 padding */
+int rsa_v15_encrypt_key(const unsigned char *inkey,    unsigned long  inlen,
+                              unsigned char *outkey,   unsigned long *outlen,
+                              prng_state    *prng,     int            prng_idx, 
+                              rsa_key       *key);
+			      
+int rsa_v15_decrypt_key(const unsigned char *in,     unsigned long  inlen,
+                              unsigned char *outkey, unsigned long keylen, 
+                              prng_state    *prng,   int            prng_idx,
+                              int           *res,    rsa_key       *key);
+
+int rsa_v15_sign_hash(const unsigned char *msghash,  unsigned long  msghashlen, 
+                            unsigned char *sig,      unsigned long *siglen, 
+                            prng_state    *prng,     int            prng_idx,
+                            int            hash_idx, rsa_key       *key);
+
+int rsa_v15_verify_hash(const unsigned char *sig,      unsigned long siglen,
+                        const unsigned char *msghash,  unsigned long msghashlen,
+                              prng_state    *prng,     int           prng_idx,
+                              int            hash_idx, int          *stat,     
+                              rsa_key       *key);
+
+
+/* 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);
                         
@@ -150,33 +174,33 @@
     mp_int x, y;
 } dh_key;
 
-extern int dh_test(void);
-extern void dh_sizes(int *low, int *high);
-extern int dh_get_size(dh_key *key);
+ int dh_test(void);
+ void dh_sizes(int *low, int *high);
+ int dh_get_size(dh_key *key);
 
-extern int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key);
-extern void dh_free(dh_key *key);
+ int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key);
+ void dh_free(dh_key *key);
 
-extern int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
-extern int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
+ int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
+ int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
 
-extern int dh_shared_secret(dh_key *private_key, dh_key *public_key,
+ int dh_shared_secret(dh_key *private_key, dh_key *public_key,
                             unsigned char *out, unsigned long *outlen);
 
-extern int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen,
+ int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen,
                                 unsigned char *out,  unsigned long *len, 
                                 prng_state *prng, int wprng, int hash, 
                                 dh_key *key);
 
-extern int dh_decrypt_key(const unsigned char *in,  unsigned long inlen, 
+ int dh_decrypt_key(const unsigned char *in,  unsigned long inlen, 
                                 unsigned char *outkey, unsigned long *keylen, 
                                 dh_key *key);
 
-extern int dh_sign_hash(const unsigned char *in,  unsigned long inlen,
+ int dh_sign_hash(const unsigned char *in,  unsigned long inlen,
                               unsigned char *out, unsigned long *outlen,
                               prng_state *prng, int wprng, dh_key *key);
 
-extern int dh_verify_hash(const unsigned char *sig, unsigned long siglen,
+ int dh_verify_hash(const unsigned char *sig, unsigned long siglen,
                           const unsigned char *hash, unsigned long hashlen, 
                                 int *stat, dh_key *key);
 
@@ -195,33 +219,33 @@
     mp_int k;
 } ecc_key;
 
-extern int ecc_test(void);
-extern void ecc_sizes(int *low, int *high);
-extern int ecc_get_size(ecc_key *key);
+ int ecc_test(void);
+ void ecc_sizes(int *low, int *high);
+ int ecc_get_size(ecc_key *key);
 
-extern int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
-extern void ecc_free(ecc_key *key);
+ int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
+ void ecc_free(ecc_key *key);
 
-extern int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
-extern int ecc_import(const unsigned char *in, unsigned long inlen, 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);
 
-extern int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, 
+ int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, 
                              unsigned char *out, unsigned long *outlen);
 
-extern int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen,
+ int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen,
                                  unsigned char *out,  unsigned long *len, 
                                  prng_state *prng, int wprng, int hash, 
                                  ecc_key *key);
 
-extern int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
+ int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
                                  unsigned char *outkey, unsigned long *keylen, 
                                  ecc_key *key);
 
-extern int ecc_sign_hash(const unsigned char *in,  unsigned long inlen,
+ 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);
 
-extern int ecc_verify_hash(const unsigned char *sig,  unsigned long siglen,
+ int ecc_verify_hash(const unsigned char *sig,  unsigned long siglen,
                            const unsigned char *hash, unsigned long hashlen, 
                                  int *stat, ecc_key *key);
 #endif
@@ -233,21 +257,29 @@
    mp_int g, q, p, x, y;
 } dsa_key;
 
-extern int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
-extern void dsa_free(dsa_key *key);
+ int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
+ void dsa_free(dsa_key *key);
 
-extern int dsa_sign_hash(const unsigned char *in,  unsigned long inlen,
+ int dsa_sign_hash(const unsigned char *in,  unsigned long inlen,
                         unsigned char *out, unsigned long *outlen,
                         prng_state *prng, int wprng, dsa_key *key);
 
-extern int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
+ int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
                            const unsigned char *hash, unsigned long inlen, 
                            int *stat, dsa_key *key);
 
-extern int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
+ int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
 
-extern int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
+ int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
 
-extern int dsa_verify_key(dsa_key *key, int *stat);
+ int dsa_verify_key(dsa_key *key, int *stat);
 
 #endif
+
+/* DER handling */
+int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen);
+int der_decode_integer(const unsigned char *in, unsigned long *inlen, mp_int *num);
+int der_length_integer(mp_int *num, unsigned long *len);
+int der_put_multi_integer(unsigned char *dst, unsigned long *outlen, mp_int *num, ...);
+int der_get_multi_integer(const unsigned char *src, unsigned long *inlen,  mp_int *num, ...);
+