Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cryptobox.h 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (c) 2015, Vsevolod Stakhov
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  13. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  16. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #ifndef CRYPTOBOX_H_
  24. #define CRYPTOBOX_H_
  25. #include "config.h"
  26. #define rspamd_cryptobox_NONCEBYTES 24
  27. #define rspamd_cryptobox_PKBYTES 32
  28. #define rspamd_cryptobox_SKBYTES 32
  29. #define rspamd_cryptobox_MACBYTES 16
  30. #define rspamd_cryptobox_NMBYTES 32
  31. typedef guchar rspamd_pk_t[rspamd_cryptobox_PKBYTES];
  32. typedef guchar rspamd_sk_t[rspamd_cryptobox_SKBYTES];
  33. typedef guchar rspamd_sig_t[rspamd_cryptobox_MACBYTES];
  34. typedef guchar rspamd_nm_t[rspamd_cryptobox_NMBYTES];
  35. struct rspamd_encrypt_segment {
  36. guchar *buf;
  37. gsize len;
  38. };
  39. /**
  40. * Init cryptobox library
  41. */
  42. void rspamd_cryptobox_init (void);
  43. /**
  44. * Generate new keypair
  45. * @param pk public key buffer
  46. * @param sk secret key buffer
  47. */
  48. void rspamd_cryptobox_keypair (rspamd_pk_t pk, rspamd_sk_t sk);
  49. /**
  50. * Encrypt segments of data inplace adding signature to sig afterwards
  51. * @param segments segments of data
  52. * @param cnt count of segments
  53. * @param pk remote pubkey
  54. * @param sk local secret key
  55. * @param sig output signature
  56. */
  57. void rspamd_cryptobox_encrypt_inplace (guchar *data, gsize len,
  58. gsize cnt, const rspamd_pk_t pk, const rspamd_sk_t sk, rspamd_sig_t sig);
  59. /**
  60. * Decrypt and verify data chunk inplace
  61. * @param data data to decrypt
  62. * @param len lenght of data
  63. * @param pk remote pubkey
  64. * @param sk local privkey
  65. * @param sig signature input
  66. * @return TRUE if input has been verified successfully
  67. */
  68. gboolean rspamd_cryptobox_decrypt_inplace (guchar *data, gsize len,
  69. const rspamd_pk_t pk, const rspamd_sk_t sk, const rspamd_sig_t sig);
  70. /**
  71. * Encrypt segments of data inplace adding signature to sig afterwards
  72. * @param segments segments of data
  73. * @param cnt count of segments
  74. * @param pk remote pubkey
  75. * @param sk local secret key
  76. * @param sig output signature
  77. */
  78. void rspamd_cryptobox_encrypt_nm_inplace (struct rspamd_encrypt_segment *segments,
  79. gsize cnt, const rspamd_nm_t nm, rspamd_sig_t sig);
  80. /**
  81. * Decrypt and verify data chunk inplace
  82. * @param data data to decrypt
  83. * @param len lenght of data
  84. * @param pk remote pubkey
  85. * @param sk local privkey
  86. * @param sig signature input
  87. * @return TRUE if input has been verified successfully
  88. */
  89. gboolean rspamd_cryptobox_decrypt_nm_inplace (guchar *data, gsize len,
  90. const rspamd_nm_t nm, const rspamd_sig_t sig);
  91. /**
  92. * Generate shared secret from local sk and remote pk
  93. * @param nm shared secret
  94. * @param pk remote pubkey
  95. * @param sk local privkey
  96. */
  97. void rspamd_cryptobox_nm (rspamd_nm_t nm, rspamd_pk_t pk, rspamd_sk_t sk);
  98. /**
  99. * Securely clear the buffer specified
  100. * @param buf buffer to zero
  101. * @param buflen length of buffer
  102. */
  103. void rspamd_explicit_memzero (void * const buf, gsize buflen);
  104. #endif /* CRYPTOBOX_H_ */