mycrypt_pk.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* ---- NUMBER THEORY ---- */
  2. #ifdef MPI
  3. #include "ltc_tommath.h"
  4. /* in/out macros */
  5. #define OUTPUT_BIGNUM(num, out, y, z) \
  6. { \
  7. if ((y + 4) > *outlen) { return CRYPT_BUFFER_OVERFLOW; } \
  8. z = (unsigned long)mp_unsigned_bin_size(num); \
  9. STORE32L(z, out+y); \
  10. y += 4; \
  11. if ((y + z) > *outlen) { return CRYPT_BUFFER_OVERFLOW; } \
  12. if ((err = mp_to_unsigned_bin(num, out+y)) != MP_OKAY) { return mpi_to_ltc_error(err); } \
  13. y += z; \
  14. }
  15. #define INPUT_BIGNUM(num, in, x, y, inlen) \
  16. { \
  17. /* load value */ \
  18. if ((y + 4) > inlen) { \
  19. err = CRYPT_INVALID_PACKET; \
  20. goto error; \
  21. } \
  22. LOAD32L(x, in+y); \
  23. y += 4; \
  24. \
  25. /* sanity check... */ \
  26. if ((x+y) > inlen) { \
  27. err = CRYPT_INVALID_PACKET; \
  28. goto error; \
  29. } \
  30. \
  31. /* load it */ \
  32. if ((err = mp_read_unsigned_bin(num, (unsigned char *)in+y, (int)x)) != MP_OKAY) {\
  33. err = mpi_to_ltc_error(err); \
  34. goto error; \
  35. } \
  36. y += x; \
  37. if ((err = mp_shrink(num)) != MP_OKAY) { \
  38. err = mpi_to_ltc_error(err); \
  39. goto error; \
  40. } \
  41. }
  42. int is_prime(mp_int *, int *);
  43. int rand_prime(mp_int *N, long len, prng_state *prng, int wprng);
  44. #else
  45. #ifdef MRSA
  46. #error RSA requires the big int library
  47. #endif
  48. #ifdef MECC
  49. #error ECC requires the big int library
  50. #endif
  51. #ifdef MDH
  52. #error DH requires the big int library
  53. #endif
  54. #ifdef MDSA
  55. #error DSA requires the big int library
  56. #endif
  57. #endif /* MPI */
  58. /* ---- PUBLIC KEY CRYPTO ---- */
  59. #define PK_PRIVATE 0 /* PK private keys */
  60. #define PK_PUBLIC 1 /* PK public keys */
  61. #define PK_PRIVATE_OPTIMIZED 2 /* PK private key [rsa optimized] */
  62. /* ---- PACKET ---- */
  63. #ifdef PACKET
  64. void packet_store_header(unsigned char *dst, int section, int subsection);
  65. int packet_valid_header(unsigned char *src, int section, int subsection);
  66. #endif
  67. /* ---- RSA ---- */
  68. #ifdef MRSA
  69. /* Min and Max RSA key sizes (in bits) */
  70. #define MIN_RSA_SIZE 1024
  71. #define MAX_RSA_SIZE 4096
  72. /* Stack required for temps (plus padding) */
  73. // #define RSA_STACK (8 + (MAX_RSA_SIZE/8))
  74. typedef struct Rsa_key {
  75. int type;
  76. mp_int e, d, N, qP, pQ, dP, dQ, p, q;
  77. } rsa_key;
  78. int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
  79. int rsa_exptmod(const unsigned char *in, unsigned long inlen,
  80. unsigned char *out, unsigned long *outlen, int which,
  81. prng_state *prng, int prng_idx,
  82. rsa_key *key);
  83. #ifdef RSA_TIMING
  84. int tim_exptmod(prng_state *prng, int prng_idx,
  85. mp_int *c, mp_int *e, mp_int *d, mp_int *n, mp_int *m);
  86. #else
  87. #define tim_exptmod(prng, prng_idx, c, e, d, n, m) mpi_to_ltc_error(mp_exptmod(c, d, n, m))
  88. #endif
  89. void rsa_free(rsa_key *key);
  90. int rsa_encrypt_key(const unsigned char *inkey, unsigned long inlen,
  91. unsigned char *outkey, unsigned long *outlen,
  92. const unsigned char *lparam, unsigned long lparamlen,
  93. prng_state *prng, int prng_idx, int hash_idx, rsa_key *key);
  94. int rsa_decrypt_key(const unsigned char *in, unsigned long inlen,
  95. unsigned char *outkey, unsigned long *keylen,
  96. const unsigned char *lparam, unsigned long lparamlen,
  97. prng_state *prng, int prng_idx,
  98. int hash_idx, int *res,
  99. rsa_key *key);
  100. int rsa_sign_hash(const unsigned char *msghash, unsigned long msghashlen,
  101. unsigned char *sig, unsigned long *siglen,
  102. prng_state *prng, int prng_idx,
  103. int hash_idx, unsigned long saltlen,
  104. rsa_key *key);
  105. int rsa_verify_hash(const unsigned char *sig, unsigned long siglen,
  106. const unsigned char *msghash, unsigned long msghashlen,
  107. prng_state *prng, int prng_idx,
  108. int hash_idx, unsigned long saltlen,
  109. int *stat, rsa_key *key);
  110. int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key);
  111. int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
  112. #endif
  113. /* ---- DH Routines ---- */
  114. #ifdef MDH
  115. typedef struct Dh_key {
  116. int idx, type;
  117. mp_int x, y;
  118. } dh_key;
  119. int dh_test(void);
  120. void dh_sizes(int *low, int *high);
  121. int dh_get_size(dh_key *key);
  122. int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key);
  123. void dh_free(dh_key *key);
  124. int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
  125. int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
  126. int dh_shared_secret(dh_key *private_key, dh_key *public_key,
  127. unsigned char *out, unsigned long *outlen);
  128. int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen,
  129. unsigned char *out, unsigned long *len,
  130. prng_state *prng, int wprng, int hash,
  131. dh_key *key);
  132. int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
  133. unsigned char *outkey, unsigned long *keylen,
  134. dh_key *key);
  135. int dh_sign_hash(const unsigned char *in, unsigned long inlen,
  136. unsigned char *out, unsigned long *outlen,
  137. prng_state *prng, int wprng, dh_key *key);
  138. int dh_verify_hash(const unsigned char *sig, unsigned long siglen,
  139. const unsigned char *hash, unsigned long hashlen,
  140. int *stat, dh_key *key);
  141. #endif
  142. /* ---- ECC Routines ---- */
  143. #ifdef MECC
  144. typedef struct {
  145. mp_int x, y;
  146. } ecc_point;
  147. typedef struct {
  148. int type, idx;
  149. ecc_point pubkey;
  150. mp_int k;
  151. } ecc_key;
  152. int ecc_test(void);
  153. void ecc_sizes(int *low, int *high);
  154. int ecc_get_size(ecc_key *key);
  155. int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
  156. void ecc_free(ecc_key *key);
  157. int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
  158. int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
  159. int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
  160. unsigned char *out, unsigned long *outlen);
  161. int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen,
  162. unsigned char *out, unsigned long *len,
  163. prng_state *prng, int wprng, int hash,
  164. ecc_key *key);
  165. int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
  166. unsigned char *outkey, unsigned long *keylen,
  167. ecc_key *key);
  168. int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
  169. unsigned char *out, unsigned long *outlen,
  170. prng_state *prng, int wprng, ecc_key *key);
  171. int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
  172. const unsigned char *hash, unsigned long hashlen,
  173. int *stat, ecc_key *key);
  174. #endif
  175. #ifdef MDSA
  176. typedef struct {
  177. int type, qord;
  178. mp_int g, q, p, x, y;
  179. } dsa_key;
  180. int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
  181. void dsa_free(dsa_key *key);
  182. int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
  183. unsigned char *out, unsigned long *outlen,
  184. prng_state *prng, int wprng, dsa_key *key);
  185. int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
  186. const unsigned char *hash, unsigned long inlen,
  187. int *stat, dsa_key *key);
  188. int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
  189. int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
  190. int dsa_verify_key(dsa_key *key, int *stat);
  191. #endif