You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

keypair.h 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef SRC_LIBCRYPTOBOX_KEYPAIR_H_
  17. #define SRC_LIBCRYPTOBOX_KEYPAIR_H_
  18. #include "config.h"
  19. #include "cryptobox.h"
  20. #include "ucl.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /**
  25. * Keypair type
  26. */
  27. enum rspamd_cryptobox_keypair_type {
  28. RSPAMD_KEYPAIR_KEX = 0,
  29. RSPAMD_KEYPAIR_SIGN
  30. };
  31. extern const guchar encrypted_magic[7];
  32. /**
  33. * Opaque structure for the full (public + private) keypair
  34. */
  35. struct rspamd_cryptobox_keypair;
  36. /**
  37. * Opaque structure for public only keypair
  38. */
  39. struct rspamd_cryptobox_pubkey;
  40. /**
  41. * Creates new full keypair
  42. * @param type type of the keypair
  43. * @param alg algorithm for the keypair
  44. * @return fresh keypair generated
  45. */
  46. struct rspamd_cryptobox_keypair *rspamd_keypair_new (
  47. enum rspamd_cryptobox_keypair_type type,
  48. enum rspamd_cryptobox_mode alg);
  49. /**
  50. * Increase refcount for the specific keypair
  51. * @param kp
  52. * @return
  53. */
  54. struct rspamd_cryptobox_keypair *rspamd_keypair_ref (
  55. struct rspamd_cryptobox_keypair *kp);
  56. /**
  57. * Decrease refcount for the specific keypair (or destroy when refcount == 0)
  58. * @param kp
  59. */
  60. void rspamd_keypair_unref (struct rspamd_cryptobox_keypair *kp);
  61. /**
  62. * Increase refcount for the specific pubkey
  63. * @param kp
  64. * @return
  65. */
  66. struct rspamd_cryptobox_pubkey *rspamd_pubkey_ref (
  67. struct rspamd_cryptobox_pubkey *kp);
  68. /**
  69. * Load pubkey from base32 string
  70. * @param b32 input string
  71. * @param type type of key (signing or kex)
  72. * @param alg algorithm of the key (nist or curve25519)
  73. * @return new pubkey or NULL in case of error
  74. */
  75. struct rspamd_cryptobox_pubkey *rspamd_pubkey_from_base32 (const gchar *b32,
  76. gsize len,
  77. enum rspamd_cryptobox_keypair_type type,
  78. enum rspamd_cryptobox_mode alg);
  79. /**
  80. * Load pubkey from hex string
  81. * @param hex input string
  82. * @param type type of key (signing or kex)
  83. * @param alg algorithm of the key (nist or curve25519)
  84. * @return new pubkey or NULL in case of error
  85. */
  86. struct rspamd_cryptobox_pubkey *rspamd_pubkey_from_hex (const gchar *hex,
  87. gsize len,
  88. enum rspamd_cryptobox_keypair_type type,
  89. enum rspamd_cryptobox_mode alg);
  90. /**
  91. * Load pubkey from raw chunk string
  92. * @param hex input data
  93. * @param type type of key (signing or kex)
  94. * @param alg algorithm of the key (nist or curve25519)
  95. * @return new pubkey or NULL in case of error
  96. */
  97. struct rspamd_cryptobox_pubkey *rspamd_pubkey_from_bin (const guchar *raw,
  98. gsize len,
  99. enum rspamd_cryptobox_keypair_type type,
  100. enum rspamd_cryptobox_mode alg);
  101. /**
  102. * Decrease refcount for the specific pubkey (or destroy when refcount == 0)
  103. * @param kp
  104. */
  105. void rspamd_pubkey_unref (struct rspamd_cryptobox_pubkey *kp);
  106. /**
  107. * Get type of keypair
  108. */
  109. enum rspamd_cryptobox_keypair_type rspamd_keypair_type (
  110. struct rspamd_cryptobox_keypair *kp);
  111. /**
  112. * Get type of pubkey
  113. */
  114. enum rspamd_cryptobox_keypair_type rspamd_pubkey_type (
  115. struct rspamd_cryptobox_pubkey *p);
  116. /**
  117. * Get algorithm of keypair
  118. */
  119. enum rspamd_cryptobox_mode rspamd_keypair_alg (struct rspamd_cryptobox_keypair *kp);
  120. /**
  121. * Get algorithm of pubkey
  122. */
  123. enum rspamd_cryptobox_mode rspamd_pubkey_alg (struct rspamd_cryptobox_pubkey *p);
  124. /**
  125. * Get cached NM for this specific pubkey
  126. * @param p
  127. * @return
  128. */
  129. const guchar *rspamd_pubkey_get_nm (struct rspamd_cryptobox_pubkey *p,
  130. struct rspamd_cryptobox_keypair *kp);
  131. /**
  132. * Calculate and store nm value for the specified local key (performs ECDH)
  133. * @param p
  134. * @return
  135. */
  136. const guchar *rspamd_pubkey_calculate_nm (struct rspamd_cryptobox_pubkey *p,
  137. struct rspamd_cryptobox_keypair *kp);
  138. /**
  139. * Get raw public key id for a specified keypair (rspamd_cryptobox_HASHBYTES)
  140. * @param kp
  141. * @return
  142. */
  143. const guchar *rspamd_keypair_get_id (struct rspamd_cryptobox_keypair *kp);
  144. /**
  145. * Get raw public key id for a specified key (rspamd_cryptobox_HASHBYTES)
  146. * @param kp
  147. * @return
  148. */
  149. const guchar *rspamd_pubkey_get_id (struct rspamd_cryptobox_pubkey *pk);
  150. /**
  151. * Get raw public key from pubkey opaque structure
  152. * @param pk
  153. * @param len
  154. * @return
  155. */
  156. const guchar *rspamd_pubkey_get_pk (struct rspamd_cryptobox_pubkey *pk,
  157. guint *len);
  158. /** Short ID characters count */
  159. #define RSPAMD_KEYPAIR_SHORT_ID_LEN 5
  160. /** Print pubkey */
  161. #define RSPAMD_KEYPAIR_PUBKEY 0x1
  162. /** Print secret key */
  163. #define RSPAMD_KEYPAIR_PRIVKEY 0x2
  164. /** Print key id */
  165. #define RSPAMD_KEYPAIR_ID 0x4
  166. /** Print short key id */
  167. #define RSPAMD_KEYPAIR_ID_SHORT 0x8
  168. /** Encode output with base 32 */
  169. #define RSPAMD_KEYPAIR_BASE32 0x10
  170. /** Human readable output */
  171. #define RSPAMD_KEYPAIR_HUMAN 0x20
  172. #define RSPAMD_KEYPAIR_HEX 0x40
  173. /**
  174. * Print keypair encoding it if needed
  175. * @param key key to print
  176. * @param how flags that specifies printing behaviour
  177. * @return newly allocated string with keypair
  178. */
  179. GString *rspamd_keypair_print (struct rspamd_cryptobox_keypair *kp,
  180. guint how);
  181. /**
  182. * Print pubkey encoding it if needed
  183. * @param key key to print
  184. * @param how flags that specifies printing behaviour
  185. * @return newly allocated string with keypair
  186. */
  187. GString *rspamd_pubkey_print (struct rspamd_cryptobox_pubkey *pk,
  188. guint how);
  189. /** Get keypair pubkey ID */
  190. #define RSPAMD_KEYPAIR_COMPONENT_ID 0
  191. /** Get keypair public key */
  192. #define RSPAMD_KEYPAIR_COMPONENT_PK 1
  193. /** Get keypair private key */
  194. #define RSPAMD_KEYPAIR_COMPONENT_SK 2
  195. /**
  196. * Get specific component of a keypair
  197. * @param kp keypair
  198. * @param ncomp component number
  199. * @param len length of input
  200. * @return raw content of the component
  201. */
  202. const guchar *rspamd_keypair_component (struct rspamd_cryptobox_keypair *kp,
  203. guint ncomp, guint *len);
  204. /**
  205. * Create a new keypair from ucl object
  206. * @param obj object to load
  207. * @return new structure or NULL if an object is invalid
  208. */
  209. struct rspamd_cryptobox_keypair *rspamd_keypair_from_ucl (const ucl_object_t *obj);
  210. /**
  211. * Converts keypair to ucl object
  212. * @param kp
  213. * @return
  214. */
  215. ucl_object_t *rspamd_keypair_to_ucl (struct rspamd_cryptobox_keypair *kp,
  216. gboolean is_hex);
  217. /**
  218. * Signs memory using the specified keypair
  219. * @param kp keypair
  220. * @param data data to sign
  221. * @param data to sign
  222. * @param sig output signature (allocated by function, must be freed by a callee)
  223. * @param outlen length of output data
  224. * @param err filled if function returns `FALSE`
  225. * @return TRUE if signature operation succeeded
  226. */
  227. gboolean rspamd_keypair_sign (struct rspamd_cryptobox_keypair *kp,
  228. const void *data, gsize len, guchar **sig, gsize *outlen,
  229. GError **err);
  230. /***
  231. * Verifies data using public key
  232. * @param pk public key
  233. * @param data data to sign
  234. * @param len data to sign
  235. * @param sig signature to verify
  236. * @param siglen length of signature
  237. * @param err filled if function returns `FALSE`
  238. * @return TRUE if signature is valid
  239. */
  240. gboolean rspamd_keypair_verify (struct rspamd_cryptobox_pubkey *pk,
  241. const void *data, gsize len, const guchar *sig, gsize siglen,
  242. GError **err);
  243. /**
  244. * Compares two public keys
  245. * @param k1 key to compare
  246. * @param k2 key to compare
  247. * @return TRUE if two keys are equal
  248. */
  249. gboolean rspamd_pubkey_equal (const struct rspamd_cryptobox_pubkey *k1,
  250. const struct rspamd_cryptobox_pubkey *k2);
  251. /**
  252. * Decrypts data using keypair and a pubkey stored in in, in must start from
  253. * `encrypted_magic` constant
  254. * @param kp keypair
  255. * @param in raw input
  256. * @param inlen input length
  257. * @param out output (allocated internally using g_malloc)
  258. * @param outlen output size
  259. * @return TRUE if decryption is completed, out must be freed in this case
  260. */
  261. gboolean rspamd_keypair_decrypt (struct rspamd_cryptobox_keypair *kp,
  262. const guchar *in, gsize inlen,
  263. guchar **out, gsize *outlen,
  264. GError **err);
  265. /**
  266. * Encrypts data usign specific keypair.
  267. * This method actually generates ephemeral local keypair, use public key from
  268. * the remote keypair and encrypts data
  269. * @param kp keypair
  270. * @param in raw input
  271. * @param inlen input length
  272. * @param out output (allocated internally using g_malloc)
  273. * @param outlen output size
  274. * @param err pointer to error
  275. * @return TRUE if encryption has been completed, out must be freed in this case
  276. */
  277. gboolean rspamd_keypair_encrypt (struct rspamd_cryptobox_keypair *kp,
  278. const guchar *in, gsize inlen,
  279. guchar **out, gsize *outlen,
  280. GError **err);
  281. /**
  282. * Encrypts data usign specific pubkey (must have KEX type).
  283. * This method actually generates ephemeral local keypair, use public key from
  284. * the remote keypair and encrypts data
  285. * @param kp keypair
  286. * @param in raw input
  287. * @param inlen input length
  288. * @param out output (allocated internally using g_malloc)
  289. * @param outlen output size
  290. * @param err pointer to error
  291. * @return TRUE if encryption has been completed, out must be freed in this case
  292. */
  293. gboolean rspamd_pubkey_encrypt (struct rspamd_cryptobox_pubkey *pk,
  294. const guchar *in, gsize inlen,
  295. guchar **out, gsize *outlen,
  296. GError **err);
  297. #ifdef __cplusplus
  298. }
  299. #endif
  300. #endif /* SRC_LIBCRYPTOBOX_KEYPAIR_H_ */