crypto.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #ifndef CRYPTO_H_
  2. #define CRYPTO_H_
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. /* Cryptographic keys held by user. */
  6. #define CRYPTO_KEY_SIGN_PRIV 0
  7. #define CRYPTO_KEY_SIGN_PUB 1
  8. #define CRYPTO_KEY_ENCR_PRIV 2
  9. #define CRYPTO_KEY_ENCR_PUB 3
  10. #define CRYPTO_KEY_HMAC_FILE 4
  11. #define CRYPTO_KEY_HMAC_CHUNK 5
  12. #define CRYPTO_KEY_HMAC_NAME 6
  13. #define CRYPTO_KEY_HMAC_CPARAMS 7
  14. /* Cryptographic keys used in client-server protocol. */
  15. /* 8 is reserved for the private part of the server root RSA key. */
  16. #define CRYPTO_KEY_ROOT_PUB 9
  17. #define CRYPTO_KEY_AUTH_PUT 10
  18. #define CRYPTO_KEY_AUTH_GET 11
  19. #define CRYPTO_KEY_AUTH_DELETE 12
  20. /*
  21. * HMAC_FILE_WRITE is normally the same key as HMAC_FILE, but can be set to
  22. * a different value via a masked crypto_keys_import if we need to read from
  23. * one archive set and write to another (i.e., tarsnap-recrypt). Because it
  24. * is a duplicate key, HMAC_FILE_WRITE cannot be exported or generated.
  25. */
  26. /* Keys #13--18 reserved for server code. */
  27. #define CRYPTO_KEY_HMAC_FILE_WRITE 19
  28. /* Fake HMAC "key" to represent "just SHA256 the data". */
  29. #define CRYPTO_KEY_HMAC_SHA256 (-1)
  30. /* Bitmasks for use in representing multiple cryptographic keys. */
  31. #define CRYPTO_KEYMASK_SIGN_PRIV (1 << CRYPTO_KEY_SIGN_PRIV)
  32. #define CRYPTO_KEYMASK_SIGN_PUB (1 << CRYPTO_KEY_SIGN_PUB)
  33. #define CRYPTO_KEYMASK_SIGN \
  34. (CRYPTO_KEYMASK_SIGN_PRIV | CRYPTO_KEYMASK_SIGN_PUB)
  35. #define CRYPTO_KEYMASK_ENCR_PRIV (1 << CRYPTO_KEY_ENCR_PRIV)
  36. #define CRYPTO_KEYMASK_ENCR_PUB (1 << CRYPTO_KEY_ENCR_PUB)
  37. #define CRYPTO_KEYMASK_ENCR \
  38. (CRYPTO_KEYMASK_ENCR_PRIV | CRYPTO_KEYMASK_ENCR_PUB)
  39. #define CRYPTO_KEYMASK_HMAC_FILE (1 << CRYPTO_KEY_HMAC_FILE)
  40. #define CRYPTO_KEYMASK_HMAC_FILE_WRITE (1 << CRYPTO_KEY_HMAC_FILE_WRITE)
  41. #define CRYPTO_KEYMASK_HMAC_CHUNK (1 << CRYPTO_KEY_HMAC_CHUNK)
  42. #define CRYPTO_KEYMASK_HMAC_NAME (1 << CRYPTO_KEY_HMAC_NAME)
  43. #define CRYPTO_KEYMASK_HMAC_CPARAMS (1 << CRYPTO_KEY_HMAC_CPARAMS)
  44. #define CRYPTO_KEYMASK_READ \
  45. (CRYPTO_KEYMASK_ENCR_PRIV | CRYPTO_KEYMASK_SIGN_PUB | \
  46. CRYPTO_KEYMASK_HMAC_FILE | CRYPTO_KEYMASK_HMAC_CHUNK | \
  47. CRYPTO_KEYMASK_HMAC_NAME | CRYPTO_KEYMASK_AUTH_GET )
  48. #define CRYPTO_KEYMASK_WRITE \
  49. (CRYPTO_KEYMASK_SIGN_PRIV | CRYPTO_KEYMASK_ENCR_PUB | \
  50. CRYPTO_KEYMASK_HMAC_FILE | CRYPTO_KEYMASK_HMAC_CHUNK | \
  51. CRYPTO_KEYMASK_HMAC_NAME | CRYPTO_KEYMASK_HMAC_CPARAMS | \
  52. CRYPTO_KEYMASK_AUTH_PUT )
  53. #define CRYPTO_KEYMASK_ROOT_PUB (1 << CRYPTO_KEY_ROOT_PUB)
  54. #define CRYPTO_KEYMASK_AUTH_PUT (1 << CRYPTO_KEY_AUTH_PUT)
  55. #define CRYPTO_KEYMASK_AUTH_GET (1 << CRYPTO_KEY_AUTH_GET)
  56. #define CRYPTO_KEYMASK_AUTH_DELETE (1 << CRYPTO_KEY_AUTH_DELETE)
  57. /* Mask for all the cryptographic keys held by users. */
  58. #define CRYPTO_KEYMASK_USER \
  59. (CRYPTO_KEYMASK_SIGN_PRIV | CRYPTO_KEYMASK_SIGN_PUB | \
  60. CRYPTO_KEYMASK_ENCR_PRIV | CRYPTO_KEYMASK_ENCR_PUB | \
  61. CRYPTO_KEYMASK_HMAC_FILE | CRYPTO_KEYMASK_HMAC_CHUNK | \
  62. CRYPTO_KEYMASK_HMAC_NAME | CRYPTO_KEYMASK_HMAC_CPARAMS | \
  63. CRYPTO_KEYMASK_AUTH_PUT | CRYPTO_KEYMASK_AUTH_GET | \
  64. CRYPTO_KEYMASK_AUTH_DELETE)
  65. /* Sizes of file encryption headers and trailers. */
  66. #define CRYPTO_FILE_HLEN (256 + 8)
  67. #define CRYPTO_FILE_TLEN 32
  68. /*
  69. * Sizes of Diffie-Hellman private, public, and exchanged keys. These are
  70. * defined in libcperciva/crypto/crypto_dh.h, but are also included here as
  71. * the sizes are used in places which don't actually do Diffie-Hellman.
  72. */
  73. #define CRYPTO_DH_PRIVLEN 32
  74. #define CRYPTO_DH_PUBLEN 256
  75. #define CRYPTO_DH_KEYLEN 256
  76. /* Structure for holding client-server protocol cryptographic state. */
  77. typedef struct crypto_session_internal CRYPTO_SESSION;
  78. /**
  79. * crypto_keys_init(void):
  80. * Initialize cryptographic keys.
  81. */
  82. int crypto_keys_init(void);
  83. /**
  84. * crypto_keys_import(buf, buflen, keys):
  85. * Import keys from the provided buffer into the key cache. Ignore any keys
  86. * not specified in the mask ${keys}.
  87. */
  88. int crypto_keys_import(const uint8_t *, size_t, int);
  89. /**
  90. * crypto_keys_missing(keys):
  91. * Look for the specified keys. If they are all present, return NULL; if
  92. * not, return a pointer to the name of one of the keys.
  93. */
  94. const char * crypto_keys_missing(int);
  95. /**
  96. * crypto_keys_export(keys, buf, buflen):
  97. * Export the ${keys} specified to a buffer allocated using malloc.
  98. */
  99. int crypto_keys_export(int, uint8_t **, size_t *);
  100. /**
  101. * crypto_keys_generate(keys):
  102. * Create the ${keys} specified.
  103. */
  104. int crypto_keys_generate(int);
  105. /**
  106. * crypto_keys_raw_export_auth(buf):
  107. * Write into the specified buffer the 32-byte write authorization key,
  108. * the 32-byte read authorization key, and the 32-byte delete authorization
  109. * key, in that order.
  110. */
  111. int crypto_keys_raw_export_auth(uint8_t[96]);
  112. /**
  113. * crypto_hash_data_key(key, keylen, data, len, buf):
  114. * Hash the provided ${data} with the provided HMAC-SHA256 ${key}.
  115. */
  116. void crypto_hash_data_key(const uint8_t *, size_t,
  117. const uint8_t *, size_t, uint8_t[32]);
  118. /**
  119. * crypto_hash_data_key_2(key, keylen, data0, len0, data1, len1, buf):
  120. * Hash the concatenation of two buffers with the provided HMAC-SHA256 ${key}.
  121. */
  122. void crypto_hash_data_key_2(const uint8_t *, size_t,
  123. const uint8_t *, size_t, const uint8_t *, size_t, uint8_t[32]);
  124. /**
  125. * crypto_hash_data(key, data, len, buf):
  126. * Hash the provided ${data} with the HMAC-SHA256 ${key} specified; or if
  127. * ${key} == CRYPTO_KEY_HMAC_SHA256, just SHA256 the data.
  128. */
  129. int crypto_hash_data(int, const uint8_t *, size_t, uint8_t[32]);
  130. /**
  131. * crypto_hash_data_2(key, data0, len0, data1, len1, buf):
  132. * Hash the concatenation of two buffers, as in crypto_hash_data.
  133. */
  134. int crypto_hash_data_2(int, const uint8_t *, size_t,
  135. const uint8_t *, size_t, uint8_t[32]);
  136. /**
  137. * crypto_rsa_sign(key, data, len, sig, siglen):
  138. * Sign the provided ${data} with the specified ${key}, writing the signature
  139. * into ${sig}.
  140. */
  141. int crypto_rsa_sign(int, const uint8_t *, size_t, uint8_t *, size_t);
  142. /**
  143. * crypto_rsa_verify(key, data, len, sig, siglen):
  144. * Verify that the provided signature ${sig} matches the provided ${data}.
  145. * Return 0 if the signature is valid, 1 if the signature is invalid, or -1
  146. * on error.
  147. */
  148. int crypto_rsa_verify(int, const uint8_t *, size_t, const uint8_t *, size_t);
  149. /**
  150. * crypto_rsa_encrypt(key, data, len, out, outlen):
  151. * Encrypt the provided ${data} with the specified ${key}, writing the
  152. * ciphertext into ${out} (of length ${outlen}).
  153. */
  154. int crypto_rsa_encrypt(int, const uint8_t *, size_t, uint8_t *, size_t);
  155. /**
  156. * crypto_rsa_decrypt(key, data, len, out, outlen):
  157. * Decrypt the provided ${data} with the specified ${key}, writing the
  158. * ciphertext into ${out} (of length ${*outlen}). Set ${*outlen} to the
  159. * length of the plaintext, and return 0 on success, 1 if the ciphertext is
  160. * invalid, or 1 on error.
  161. */
  162. int crypto_rsa_decrypt(int, const uint8_t *, size_t, uint8_t *, size_t *);
  163. /**
  164. * crypto_file_enc(buf, len, filebuf):
  165. * Encrypt the buffer ${buf} of length ${len}, placing the result (including
  166. * encryption header and authentication trailer) into ${filebuf}.
  167. */
  168. int crypto_file_enc(const uint8_t *, size_t, uint8_t *);
  169. /**
  170. * crypto_file_dec(filebuf, len, buf):
  171. * Decrypt the buffer ${filebuf}, removing the encryption header and
  172. * authentication trailer, and place the result into ${buf} of length ${len}.
  173. */
  174. int crypto_file_dec(const uint8_t *, size_t, uint8_t *);
  175. /**
  176. * crypto_session_init(pub, priv, nonce, mkey, encr_write, auth_write,
  177. * encr_read, auth_read):
  178. * Compute K = ${pub}^(2^258 + ${priv}), mkey = MGF1(nonce || K, 48), and
  179. * return a CRYPTO_SESSION with encryption and authentication write and read
  180. * keys constructed from HMAC(mkey, (encr|auth)_(write|read)).
  181. */
  182. CRYPTO_SESSION * crypto_session_init(uint8_t[CRYPTO_DH_PUBLEN],
  183. uint8_t[CRYPTO_DH_PRIVLEN], uint8_t[32], uint8_t[48],
  184. const char *, const char *, const char *, const char *);
  185. /**
  186. * crypto_session_encrypt(CS, inbuf, outbuf, buflen):
  187. * Encrypt ${inbuf} with the session write key and write ciphertext to
  188. * ${outbuf}.
  189. */
  190. void crypto_session_encrypt(CRYPTO_SESSION *, const uint8_t *, uint8_t *,
  191. size_t);
  192. /**
  193. * crypto_session_decrypt(CS, inbuf, outbuf, buflen):
  194. * Decrypt ${inbuf} with the session read key and write plaintext to ${outbuf}.
  195. */
  196. void crypto_session_decrypt(CRYPTO_SESSION *, const uint8_t *, uint8_t *,
  197. size_t);
  198. /**
  199. * crypto_session_sign(CS, buf, buflen, sig):
  200. * Generate sig = write_auth(buf).
  201. */
  202. void crypto_session_sign(CRYPTO_SESSION *, const uint8_t *, size_t,
  203. uint8_t[32]);
  204. /**
  205. * crypto_session_verify(CS, buf, buflen, sig):
  206. * Verify that sig = read_auth(buf). Return non-zero if the signature
  207. * does not match.
  208. */
  209. int crypto_session_verify(CRYPTO_SESSION *, const uint8_t *, size_t,
  210. const uint8_t[32]);
  211. /**
  212. * crypto_session_free(CS):
  213. * Free a CRYPTO_SESSION structure.
  214. */
  215. void crypto_session_free(CRYPTO_SESSION *);
  216. /**
  217. * crypto_passwd_to_dh(passwd, salt, pub, priv):
  218. * Generate a Diffie-Hellman pair (${priv}, ${pub}), with ${pub} equal to
  219. * 2^(2^258 + ${priv}) modulo the group #14 modulus, and ${priv} equal to
  220. * HMAC(${salt}, ${passwd}), where ${passwd} is a NUL-terminated string.
  221. */
  222. int crypto_passwd_to_dh(const char *, const uint8_t[32],
  223. uint8_t[CRYPTO_DH_PUBLEN], uint8_t[CRYPTO_DH_PRIVLEN]);
  224. #endif /* !CRYPTO_H_ */