SPProtoEncoder.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * @file SPProtoEncoder.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Object which encodes packets according to SPProto.
  25. */
  26. #ifndef BADVPN_FLOW_SPPROTOENCODER_H
  27. #define BADVPN_FLOW_SPPROTOENCODER_H
  28. #include <stdint.h>
  29. #include <misc/debug.h>
  30. #include <protocol/spproto.h>
  31. #include <system/DebugObject.h>
  32. #include <security/BEncryption.h>
  33. #include <security/OTPGenerator.h>
  34. #include <flow/PacketRecvInterface.h>
  35. #include <threadwork/BThreadWork.h>
  36. /**
  37. * Event context handler called when the remaining number of
  38. * OTPs equals the warning number after having encoded a packet.
  39. *
  40. * @param user as in {@link SPProtoEncoder_Init}
  41. */
  42. typedef void (*SPProtoEncoder_handler) (void *user);
  43. /**
  44. * Object which encodes packets according to SPProto.
  45. *
  46. * Input is with {@link PacketRecvInterface}.
  47. * Output is with {@link PacketRecvInterface}.
  48. */
  49. typedef struct {
  50. PacketRecvInterface *input;
  51. struct spproto_security_params sp_params;
  52. int otp_warning_count;
  53. SPProtoEncoder_handler handler;
  54. BThreadWorkDispatcher *twd;
  55. void *user;
  56. int hash_size;
  57. int enc_block_size;
  58. int enc_key_size;
  59. OTPGenerator otpgen;
  60. uint16_t otpgen_seed_id;
  61. uint16_t otpgen_pending_seed_id;
  62. int have_encryption_key;
  63. BEncryption encryptor;
  64. int input_mtu;
  65. int output_mtu;
  66. int in_len;
  67. PacketRecvInterface output;
  68. int out_have;
  69. uint8_t *out;
  70. uint8_t *buf;
  71. BPending handler_job;
  72. int tw_have;
  73. BThreadWork tw;
  74. uint16_t tw_seed_id;
  75. otp_t tw_otp;
  76. int tw_out_len;
  77. DebugObject d_obj;
  78. } SPProtoEncoder;
  79. /**
  80. * Initializes the object.
  81. * The object is initialized in blocked state.
  82. * {@link BSecurity_GlobalInitThreadSafe} must have been done if
  83. * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
  84. *
  85. * @param o the object
  86. * @param input input interface. Its MTU must not be too large, i.e. this must hold:
  87. * spproto_carrier_mtu_for_payload_mtu(sp_params, input MTU) >= 0
  88. * @param sp_params SPProto security parameters
  89. * @param otp_warning_count If using OTPs, after how many encoded packets to call the handler.
  90. * In this case, must be >0 and <=sp_params.otp_num.
  91. * @param pg pending group
  92. * @param twd thread work dispatcher
  93. * @return 1 on success, 0 on failure
  94. */
  95. int SPProtoEncoder_Init (SPProtoEncoder *o, PacketRecvInterface *input, struct spproto_security_params sp_params, int otp_warning_count, BPendingGroup *pg, BThreadWorkDispatcher *twd) WARN_UNUSED;
  96. /**
  97. * Frees the object.
  98. *
  99. * @param o the object
  100. */
  101. void SPProtoEncoder_Free (SPProtoEncoder *o);
  102. /**
  103. * Returns the output interface.
  104. * The MTU of the output interface will depend on the input MTU and security parameters,
  105. * that is spproto_carrier_mtu_for_payload_mtu(sp_params, input MTU).
  106. *
  107. * @param o the object
  108. * @return output interface
  109. */
  110. PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o);
  111. /**
  112. * Sets an encryption key to use.
  113. * Encryption must be enabled.
  114. *
  115. * @param o the object
  116. * @param encryption_key key to use
  117. */
  118. void SPProtoEncoder_SetEncryptionKey (SPProtoEncoder *o, uint8_t *encryption_key);
  119. /**
  120. * Removes an encryption key if one is configured.
  121. * Encryption must be enabled.
  122. *
  123. * @param o the object
  124. */
  125. void SPProtoEncoder_RemoveEncryptionKey (SPProtoEncoder *o);
  126. /**
  127. * Sets an OTP seed to use.
  128. * OTPs must be enabled.
  129. *
  130. * @param o the object
  131. * @param seed_id seed identifier
  132. * @param key OTP encryption key
  133. * @param iv OTP initialization vector
  134. */
  135. void SPProtoEncoder_SetOTPSeed (SPProtoEncoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  136. /**
  137. * Removes the OTP seed if one is configured.
  138. * OTPs must be enabled.
  139. *
  140. * @param o the object
  141. */
  142. void SPProtoEncoder_RemoveOTPSeed (SPProtoEncoder *o);
  143. /**
  144. * Sets handlers.
  145. *
  146. * @param o the object
  147. * @param handler OTP warning handler
  148. * @param user value to pass to handler
  149. */
  150. void SPProtoEncoder_SetHandlers (SPProtoEncoder *o, SPProtoEncoder_handler handler, void *user);
  151. #endif