crypto.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef CRYPTO_H_DEFINED
  2. #define CRYPTO_H_DEFINED
  3. #include <stdint.h>
  4. #define DIGEST_SHA1 0
  5. #define PRNG_SOBER 0
  6. enum {
  7. CRYPT_OK=0, /* Result OK */
  8. CRYPT_ERROR, /* Generic Error */
  9. CRYPT_NOP, /* Not a failure but no operation was performed */
  10. CRYPT_INVALID_KEYSIZE, /* Invalid key size given */
  11. CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */
  12. CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */
  13. CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */
  14. CRYPT_INVALID_PACKET, /* Invalid input packet given */
  15. CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */
  16. CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */
  17. CRYPT_INVALID_CIPHER, /* Invalid cipher specified */
  18. CRYPT_INVALID_HASH, /* Invalid hash specified */
  19. CRYPT_INVALID_PRNG, /* Invalid PRNG specified */
  20. CRYPT_MEM, /* Out of memory */
  21. CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */
  22. CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */
  23. CRYPT_INVALID_ARG, /* Generic invalid argument */
  24. CRYPT_FILE_NOTFOUND, /* File Not Found */
  25. CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */
  26. CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */
  27. CRYPT_PK_DUP, /* Duplicate key already in key ring */
  28. CRYPT_PK_NOT_FOUND, /* Key not found in keyring */
  29. CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */
  30. CRYPT_INVALID_PRIME_SIZE/* Invalid size of prime requested */
  31. };
  32. struct sha1_state {
  33. unsigned long long length;
  34. unsigned long state[5], curlen;
  35. unsigned char buf[64];
  36. };
  37. typedef union Hash_state {
  38. struct sha1_state sha1;
  39. } hash_state;
  40. struct _hash_descriptor {
  41. const char *name;
  42. unsigned char ID;
  43. unsigned long hashsize; /* digest output size in bytes */
  44. unsigned long blocksize; /* the block size the hash uses */
  45. unsigned char DER[64]; /* DER encoded identifier */
  46. unsigned long DERlen; /* length of DER encoding */
  47. void (*init)(hash_state *);
  48. int (*process)(hash_state *, const unsigned char *, unsigned long);
  49. int (*done)(hash_state *, unsigned char *);
  50. int (*test)(void);
  51. };
  52. extern const struct _hash_descriptor *hash_descriptor[];
  53. void sha1_init(hash_state * md);
  54. int sha1_process(hash_state * md, const unsigned char *buf, unsigned long len);
  55. int sha1_done(hash_state * md, unsigned char *hash);
  56. int sha1_test(void);
  57. int hash_memory(int hash, const unsigned char *data, unsigned long len, unsigned char *dst, unsigned long *outlen);
  58. #define MAXBLOCKSIZE 128
  59. typedef struct Hmac_state {
  60. hash_state md;
  61. int hash;
  62. hash_state hashstate;
  63. unsigned char key[MAXBLOCKSIZE];
  64. } hmac_state;
  65. int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen);
  66. int hmac_process(hmac_state *hmac, const unsigned char *buf, unsigned long len);
  67. int hmac_done(hmac_state *hmac, unsigned char *hashOut, unsigned long *outlen);
  68. int hmac_test(void);
  69. int hmac_memory(int hash, const unsigned char *key, unsigned long keylen,
  70. const unsigned char *data, unsigned long len,
  71. unsigned char *dst, unsigned long *dstlen);
  72. struct sober128_prng {
  73. uint32_t R[17], /* Working storage for the shift register */
  74. initR[17], /* saved register contents */
  75. konst, /* key dependent constant */
  76. sbuf; /* partial word encryption buffer */
  77. int nbuf, /* number of part-word stream bits buffered */
  78. flag, /* first add_entropy call or not? */
  79. set; /* did we call add_entropy to set key? */
  80. };
  81. typedef union Prng_state {
  82. struct sober128_prng sober128;
  83. } prng_state;
  84. struct _prng_descriptor {
  85. const char *name;
  86. int export_size; /* size in bytes of exported state */
  87. int (*start)(prng_state *);
  88. int (*add_entropy)(const unsigned char *, unsigned long, prng_state *);
  89. int (*ready)(prng_state *);
  90. unsigned long (*read)(unsigned char *, unsigned long, prng_state *);
  91. };
  92. extern const struct _prng_descriptor *prng_descriptor[];
  93. int sober128_start(prng_state *prng);
  94. int sober128_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng);
  95. int sober128_ready(prng_state *prng);
  96. unsigned long sober128_read(unsigned char *buf, unsigned long len, prng_state *prng);
  97. int sober128_done(prng_state *prng);
  98. int sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
  99. int sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
  100. int sober128_test(void);
  101. unsigned long rng_get_bytes(unsigned char *buf,
  102. unsigned long len,
  103. void (*callback)(void));
  104. int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void));
  105. #endif /* CRYPTO_H_DEFINED */