curve_mechanism_base.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #ifndef __ZMQ_CURVE_MECHANISM_BASE_HPP_INCLUDED__
  25. #define __ZMQ_CURVE_MECHANISM_BASE_HPP_INCLUDED__
  26. #ifdef ZMQ_HAVE_CURVE
  27. #if defined(ZMQ_USE_TWEETNACL)
  28. #include "tweetnacl.h"
  29. #elif defined(ZMQ_USE_LIBSODIUM)
  30. #include "sodium.h"
  31. #endif
  32. #if crypto_box_NONCEBYTES != 24 || crypto_box_PUBLICKEYBYTES != 32 \
  33. || crypto_box_SECRETKEYBYTES != 32 || crypto_box_ZEROBYTES != 32 \
  34. || crypto_box_BOXZEROBYTES != 16 || crypto_secretbox_NONCEBYTES != 24 \
  35. || crypto_secretbox_ZEROBYTES != 32 || crypto_secretbox_BOXZEROBYTES != 16
  36. #error "CURVE library not built properly"
  37. #endif
  38. #include "mechanism_base.hpp"
  39. #include "options.hpp"
  40. #include <memory>
  41. namespace zmq
  42. {
  43. class curve_encoding_t
  44. {
  45. public:
  46. curve_encoding_t (const char *encode_nonce_prefix_,
  47. const char *decode_nonce_prefix_,
  48. const bool downgrade_sub_);
  49. int encode (msg_t *msg_);
  50. int decode (msg_t *msg_, int *error_event_code_);
  51. uint8_t *get_writable_precom_buffer () { return _cn_precom; }
  52. const uint8_t *get_precom_buffer () const { return _cn_precom; }
  53. typedef uint64_t nonce_t;
  54. nonce_t get_and_inc_nonce () { return _cn_nonce++; }
  55. void set_peer_nonce (nonce_t peer_nonce_) { _cn_peer_nonce = peer_nonce_; };
  56. private:
  57. int check_validity (msg_t *msg_, int *error_event_code_);
  58. const char *_encode_nonce_prefix;
  59. const char *_decode_nonce_prefix;
  60. nonce_t _cn_nonce;
  61. nonce_t _cn_peer_nonce;
  62. // Intermediary buffer used to speed up boxing and unboxing.
  63. uint8_t _cn_precom[crypto_box_BEFORENMBYTES];
  64. const bool _downgrade_sub;
  65. ZMQ_NON_COPYABLE_NOR_MOVABLE (curve_encoding_t)
  66. };
  67. class curve_mechanism_base_t : public virtual mechanism_base_t,
  68. public curve_encoding_t
  69. {
  70. public:
  71. curve_mechanism_base_t (session_base_t *session_,
  72. const options_t &options_,
  73. const char *encode_nonce_prefix_,
  74. const char *decode_nonce_prefix_,
  75. const bool downgrade_sub_);
  76. // mechanism implementation
  77. int encode (msg_t *msg_) ZMQ_OVERRIDE;
  78. int decode (msg_t *msg_) ZMQ_OVERRIDE;
  79. };
  80. }
  81. #endif
  82. #endif