mycrypt_pk.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. /* ---- PACKET ---- */
  62. #ifdef PACKET
  63. void packet_store_header(unsigned char *dst, int section, int subsection);
  64. int packet_valid_header(unsigned char *src, int section, int subsection);
  65. #endif
  66. /* ---- RSA ---- */
  67. #ifdef MRSA
  68. /* Min and Max RSA key sizes (in bits) */
  69. #define MIN_RSA_SIZE 1024
  70. #define MAX_RSA_SIZE 4096
  71. /* Stack required for temps (plus padding) */
  72. // #define RSA_STACK (8 + (MAX_RSA_SIZE/8))
  73. typedef struct Rsa_key {
  74. int type;
  75. mp_int e, d, N, p, q, qP, dP, dQ;
  76. } rsa_key;
  77. int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
  78. int rsa_exptmod(const unsigned char *in, unsigned long inlen,
  79. unsigned char *out, unsigned long *outlen, int which,
  80. prng_state *prng, int prng_idx,
  81. rsa_key *key);
  82. #ifdef RSA_TIMING
  83. int tim_exptmod(prng_state *prng, int prng_idx,
  84. mp_int *c, mp_int *e, mp_int *d, mp_int *n, mp_int *m);
  85. #else
  86. #define tim_exptmod(prng, prng_idx, c, e, d, n, m) mpi_to_ltc_error(mp_exptmod(c, d, n, m))
  87. #endif
  88. void rsa_free(rsa_key *key);
  89. /* These use PKCS #1 v2.0 padding */
  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. /* these use PKCS #1 v1.5 padding */
  111. int rsa_v15_encrypt_key(const unsigned char *inkey, unsigned long inlen,
  112. unsigned char *outkey, unsigned long *outlen,
  113. prng_state *prng, int prng_idx,
  114. rsa_key *key);
  115. int rsa_v15_decrypt_key(const unsigned char *in, unsigned long inlen,
  116. unsigned char *outkey, unsigned long keylen,
  117. prng_state *prng, int prng_idx,
  118. int *res, rsa_key *key);
  119. int rsa_v15_sign_hash(const unsigned char *msghash, unsigned long msghashlen,
  120. unsigned char *sig, unsigned long *siglen,
  121. prng_state *prng, int prng_idx,
  122. int hash_idx, rsa_key *key);
  123. int rsa_v15_verify_hash(const unsigned char *sig, unsigned long siglen,
  124. const unsigned char *msghash, unsigned long msghashlen,
  125. prng_state *prng, int prng_idx,
  126. int hash_idx, int *stat,
  127. rsa_key *key);
  128. /* PKCS #1 import/export */
  129. int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key);
  130. int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
  131. #endif
  132. /* ---- DH Routines ---- */
  133. #ifdef MDH
  134. typedef struct Dh_key {
  135. int idx, type;
  136. mp_int x, y;
  137. } dh_key;
  138. int dh_test(void);
  139. void dh_sizes(int *low, int *high);
  140. int dh_get_size(dh_key *key);
  141. int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key);
  142. void dh_free(dh_key *key);
  143. int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
  144. int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
  145. int dh_shared_secret(dh_key *private_key, dh_key *public_key,
  146. unsigned char *out, unsigned long *outlen);
  147. int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen,
  148. unsigned char *out, unsigned long *len,
  149. prng_state *prng, int wprng, int hash,
  150. dh_key *key);
  151. int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
  152. unsigned char *outkey, unsigned long *keylen,
  153. dh_key *key);
  154. int dh_sign_hash(const unsigned char *in, unsigned long inlen,
  155. unsigned char *out, unsigned long *outlen,
  156. prng_state *prng, int wprng, dh_key *key);
  157. int dh_verify_hash(const unsigned char *sig, unsigned long siglen,
  158. const unsigned char *hash, unsigned long hashlen,
  159. int *stat, dh_key *key);
  160. #endif
  161. /* ---- ECC Routines ---- */
  162. #ifdef MECC
  163. typedef struct {
  164. mp_int x, y;
  165. } ecc_point;
  166. typedef struct {
  167. int type, idx;
  168. ecc_point pubkey;
  169. mp_int k;
  170. } ecc_key;
  171. int ecc_test(void);
  172. void ecc_sizes(int *low, int *high);
  173. int ecc_get_size(ecc_key *key);
  174. int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
  175. void ecc_free(ecc_key *key);
  176. int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
  177. int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
  178. int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
  179. unsigned char *out, unsigned long *outlen);
  180. int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen,
  181. unsigned char *out, unsigned long *len,
  182. prng_state *prng, int wprng, int hash,
  183. ecc_key *key);
  184. int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
  185. unsigned char *outkey, unsigned long *keylen,
  186. ecc_key *key);
  187. int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
  188. unsigned char *out, unsigned long *outlen,
  189. prng_state *prng, int wprng, ecc_key *key);
  190. int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
  191. const unsigned char *hash, unsigned long hashlen,
  192. int *stat, ecc_key *key);
  193. #endif
  194. #ifdef MDSA
  195. typedef struct {
  196. int type, qord;
  197. mp_int g, q, p, x, y;
  198. } dsa_key;
  199. int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
  200. void dsa_free(dsa_key *key);
  201. int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
  202. unsigned char *out, unsigned long *outlen,
  203. prng_state *prng, int wprng, dsa_key *key);
  204. int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
  205. const unsigned char *hash, unsigned long inlen,
  206. int *stat, dsa_key *key);
  207. int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
  208. int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
  209. int dsa_verify_key(dsa_key *key, int *stat);
  210. #endif
  211. /* DER handling */
  212. int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen);
  213. int der_decode_integer(const unsigned char *in, unsigned long *inlen, mp_int *num);
  214. int der_length_integer(mp_int *num, unsigned long *len);
  215. int der_put_multi_integer(unsigned char *dst, unsigned long *outlen, mp_int *num, ...);
  216. int der_get_multi_integer(const unsigned char *src, unsigned long *inlen, mp_int *num, ...);