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 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 unsigned char 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 char *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 char *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 unsigned char *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 unsigned char *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 unsigned char *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 unsigned char *rspamd_keypair_get_id(struct rspamd_cryptobox_keypair *kp);
  144. /**
  145. * Returns keypair extensions if any
  146. * @param kp
  147. * @return
  148. */
  149. const ucl_object_t *rspamd_keypair_get_extensions(struct rspamd_cryptobox_keypair *kp);
  150. /**
  151. * Get raw public key id for a specified key (rspamd_cryptobox_HASHBYTES)
  152. * @param kp
  153. * @return
  154. */
  155. const unsigned char *rspamd_pubkey_get_id(struct rspamd_cryptobox_pubkey *pk);
  156. /**
  157. * Get raw public key from pubkey opaque structure
  158. * @param pk
  159. * @param len
  160. * @return
  161. */
  162. const unsigned char *rspamd_pubkey_get_pk(struct rspamd_cryptobox_pubkey *pk,
  163. unsigned int *len);
  164. /** Short ID characters count */
  165. #define RSPAMD_KEYPAIR_SHORT_ID_LEN 5
  166. /** Print pubkey */
  167. #define RSPAMD_KEYPAIR_PUBKEY 0x1
  168. /** Print secret key */
  169. #define RSPAMD_KEYPAIR_PRIVKEY 0x2
  170. /** Print key id */
  171. #define RSPAMD_KEYPAIR_ID 0x4
  172. /** Print short key id */
  173. #define RSPAMD_KEYPAIR_ID_SHORT 0x8
  174. /** Encode output with base 32 */
  175. #define RSPAMD_KEYPAIR_BASE32 0x10
  176. /** Human readable output */
  177. #define RSPAMD_KEYPAIR_HUMAN 0x20
  178. #define RSPAMD_KEYPAIR_HEX 0x40
  179. /**
  180. * Print keypair encoding it if needed
  181. * @param key key to print
  182. * @param how flags that specifies printing behaviour
  183. * @return newly allocated string with keypair
  184. */
  185. GString *rspamd_keypair_print(struct rspamd_cryptobox_keypair *kp,
  186. unsigned int how);
  187. /**
  188. * Print pubkey encoding it if needed
  189. * @param key key to print
  190. * @param how flags that specifies printing behaviour
  191. * @return newly allocated string with keypair
  192. */
  193. GString *rspamd_pubkey_print(struct rspamd_cryptobox_pubkey *pk,
  194. unsigned int how);
  195. /** Get keypair pubkey ID */
  196. #define RSPAMD_KEYPAIR_COMPONENT_ID 0
  197. /** Get keypair public key */
  198. #define RSPAMD_KEYPAIR_COMPONENT_PK 1
  199. /** Get keypair private key */
  200. #define RSPAMD_KEYPAIR_COMPONENT_SK 2
  201. /**
  202. * Get specific component of a keypair
  203. * @param kp keypair
  204. * @param ncomp component number
  205. * @param len length of input
  206. * @return raw content of the component
  207. */
  208. const unsigned char *rspamd_keypair_component(struct rspamd_cryptobox_keypair *kp,
  209. unsigned int ncomp, unsigned int *len);
  210. /**
  211. * Create a new keypair from ucl object
  212. * @param obj object to load
  213. * @return new structure or NULL if an object is invalid
  214. */
  215. struct rspamd_cryptobox_keypair *rspamd_keypair_from_ucl(const ucl_object_t *obj);
  216. enum rspamd_keypair_dump_flags {
  217. RSPAMD_KEYPAIR_DUMP_DEFAULT = 0,
  218. RSPAMD_KEYPAIR_DUMP_HEX = 1u << 0u,
  219. RSPAMD_KEYPAIR_DUMP_NO_SECRET = 1u << 1u,
  220. RSPAMD_KEYPAIR_DUMP_FLATTENED = 1u << 2u,
  221. };
  222. /**
  223. * Converts keypair to ucl object
  224. * @param kp
  225. * @return
  226. */
  227. ucl_object_t *rspamd_keypair_to_ucl(struct rspamd_cryptobox_keypair *kp,
  228. enum rspamd_keypair_dump_flags flags);
  229. /**
  230. * Decrypts data using keypair and a pubkey stored in in, in must start from
  231. * `encrypted_magic` constant
  232. * @param kp keypair
  233. * @param in raw input
  234. * @param inlen input length
  235. * @param out output (allocated internally using g_malloc)
  236. * @param outlen output size
  237. * @return TRUE if decryption is completed, out must be freed in this case
  238. */
  239. gboolean rspamd_keypair_decrypt(struct rspamd_cryptobox_keypair *kp,
  240. const unsigned char *in, gsize inlen,
  241. unsigned char **out, gsize *outlen,
  242. GError **err);
  243. /**
  244. * Encrypts data usign specific keypair.
  245. * This method actually generates ephemeral local keypair, use public key from
  246. * the remote keypair and encrypts data
  247. * @param kp keypair
  248. * @param in raw input
  249. * @param inlen input length
  250. * @param out output (allocated internally using g_malloc)
  251. * @param outlen output size
  252. * @param err pointer to error
  253. * @return TRUE if encryption has been completed, out must be freed in this case
  254. */
  255. gboolean rspamd_keypair_encrypt(struct rspamd_cryptobox_keypair *kp,
  256. const unsigned char *in, gsize inlen,
  257. unsigned char **out, gsize *outlen,
  258. GError **err);
  259. /**
  260. * Encrypts data usign specific pubkey (must have KEX type).
  261. * This method actually generates ephemeral local keypair, use public key from
  262. * the remote keypair and encrypts data
  263. * @param kp keypair
  264. * @param in raw input
  265. * @param inlen input length
  266. * @param out output (allocated internally using g_malloc)
  267. * @param outlen output size
  268. * @param err pointer to error
  269. * @return TRUE if encryption has been completed, out must be freed in this case
  270. */
  271. gboolean rspamd_pubkey_encrypt(struct rspamd_cryptobox_pubkey *pk,
  272. const unsigned char *in, gsize inlen,
  273. unsigned char **out, gsize *outlen,
  274. GError **err);
  275. #ifdef __cplusplus
  276. }
  277. #endif
  278. #endif /* SRC_LIBCRYPTOBOX_KEYPAIR_H_ */