mycrypt_pk.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. /* These use PKCS #1 v2.0 padding */
  91. int rsa_encrypt_key(const unsigned char *inkey, unsigned long inlen,
  92. unsigned char *outkey, unsigned long *outlen,
  93. const unsigned char *lparam, unsigned long lparamlen,
  94. prng_state *prng, int prng_idx, int hash_idx, rsa_key *key);
  95. int rsa_decrypt_key(const unsigned char *in, unsigned long inlen,
  96. unsigned char *outkey, unsigned long *keylen,
  97. const unsigned char *lparam, unsigned long lparamlen,
  98. prng_state *prng, int prng_idx,
  99. int hash_idx, int *res,
  100. rsa_key *key);
  101. int rsa_sign_hash(const unsigned char *msghash, unsigned long msghashlen,
  102. unsigned char *sig, unsigned long *siglen,
  103. prng_state *prng, int prng_idx,
  104. int hash_idx, unsigned long saltlen,
  105. rsa_key *key);
  106. int rsa_verify_hash(const unsigned char *sig, unsigned long siglen,
  107. const unsigned char *msghash, unsigned long msghashlen,
  108. prng_state *prng, int prng_idx,
  109. int hash_idx, unsigned long saltlen,
  110. int *stat, rsa_key *key);
  111. /* these use PKCS #1 v1.5 padding */
  112. int rsa_v15_encrypt_key(const unsigned char *inkey, unsigned long inlen,
  113. unsigned char *outkey, unsigned long *outlen,
  114. prng_state *prng, int prng_idx,
  115. rsa_key *key);
  116. int rsa_v15_decrypt_key(const unsigned char *in, unsigned long inlen,
  117. unsigned char *outkey, unsigned long keylen,
  118. prng_state *prng, int prng_idx,
  119. int *res, rsa_key *key);
  120. int rsa_v15_sign_hash(const unsigned char *msghash, unsigned long msghashlen,
  121. unsigned char *sig, unsigned long *siglen,
  122. prng_state *prng, int prng_idx,
  123. int hash_idx, rsa_key *key);
  124. int rsa_v15_verify_hash(const unsigned char *sig, unsigned long siglen,
  125. const unsigned char *msghash, unsigned long msghashlen,
  126. prng_state *prng, int prng_idx,
  127. int hash_idx, int *stat,
  128. rsa_key *key);
  129. /* PKCS #1 import/export */
  130. int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key);
  131. int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
  132. #endif
  133. /* ---- DH Routines ---- */
  134. #ifdef MDH
  135. typedef struct Dh_key {
  136. int idx, type;
  137. mp_int x, y;
  138. } dh_key;
  139. int dh_test(void);
  140. void dh_sizes(int *low, int *high);
  141. int dh_get_size(dh_key *key);
  142. int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key);
  143. void dh_free(dh_key *key);
  144. int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
  145. int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
  146. int dh_shared_secret(dh_key *private_key, dh_key *public_key,
  147. unsigned char *out, unsigned long *outlen);
  148. int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen,
  149. unsigned char *out, unsigned long *len,
  150. prng_state *prng, int wprng, int hash,
  151. dh_key *key);
  152. int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
  153. unsigned char *outkey, unsigned long *keylen,
  154. dh_key *key);
  155. int dh_sign_hash(const unsigned char *in, unsigned long inlen,
  156. unsigned char *out, unsigned long *outlen,
  157. prng_state *prng, int wprng, dh_key *key);
  158. int dh_verify_hash(const unsigned char *sig, unsigned long siglen,
  159. const unsigned char *hash, unsigned long hashlen,
  160. int *stat, dh_key *key);
  161. #endif
  162. /* ---- ECC Routines ---- */
  163. #ifdef MECC
  164. typedef struct {
  165. mp_int x, y;
  166. } ecc_point;
  167. typedef struct {
  168. int type, idx;
  169. ecc_point pubkey;
  170. mp_int k;
  171. } ecc_key;
  172. int ecc_test(void);
  173. void ecc_sizes(int *low, int *high);
  174. int ecc_get_size(ecc_key *key);
  175. int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
  176. void ecc_free(ecc_key *key);
  177. int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
  178. int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
  179. int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
  180. unsigned char *out, unsigned long *outlen);
  181. int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen,
  182. unsigned char *out, unsigned long *len,
  183. prng_state *prng, int wprng, int hash,
  184. ecc_key *key);
  185. int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
  186. unsigned char *outkey, unsigned long *keylen,
  187. ecc_key *key);
  188. int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
  189. unsigned char *out, unsigned long *outlen,
  190. prng_state *prng, int wprng, ecc_key *key);
  191. int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
  192. const unsigned char *hash, unsigned long hashlen,
  193. int *stat, ecc_key *key);
  194. #endif
  195. #ifdef MDSA
  196. typedef struct {
  197. int type, qord;
  198. mp_int g, q, p, x, y;
  199. } dsa_key;
  200. int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
  201. void dsa_free(dsa_key *key);
  202. int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
  203. unsigned char *out, unsigned long *outlen,
  204. prng_state *prng, int wprng, dsa_key *key);
  205. int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
  206. const unsigned char *hash, unsigned long inlen,
  207. int *stat, dsa_key *key);
  208. int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
  209. int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
  210. int dsa_verify_key(dsa_key *key, int *stat);
  211. #endif