SPProtoEncoder.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * @file SPProtoEncoder.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Object which encodes packets according to SPProto.
  32. */
  33. #ifndef BADVPN_CLIENT_SPPROTOENCODER_H
  34. #define BADVPN_CLIENT_SPPROTOENCODER_H
  35. #include <stdint.h>
  36. #include <misc/debug.h>
  37. #include <protocol/spproto.h>
  38. #include <base/DebugObject.h>
  39. #include <security/BEncryption.h>
  40. #include <security/OTPGenerator.h>
  41. #include <flow/PacketRecvInterface.h>
  42. #include <threadwork/BThreadWork.h>
  43. /**
  44. * Event context handler called when the remaining number of
  45. * OTPs equals the warning number after having encoded a packet.
  46. *
  47. * @param user as in {@link SPProtoEncoder_Init}
  48. */
  49. typedef void (*SPProtoEncoder_handler) (void *user);
  50. /**
  51. * Object which encodes packets according to SPProto.
  52. *
  53. * Input is with {@link PacketRecvInterface}.
  54. * Output is with {@link PacketRecvInterface}.
  55. */
  56. typedef struct {
  57. PacketRecvInterface *input;
  58. struct spproto_security_params sp_params;
  59. int otp_warning_count;
  60. SPProtoEncoder_handler handler;
  61. BThreadWorkDispatcher *twd;
  62. void *user;
  63. int hash_size;
  64. int enc_block_size;
  65. int enc_key_size;
  66. OTPGenerator otpgen;
  67. uint16_t otpgen_seed_id;
  68. uint16_t otpgen_pending_seed_id;
  69. int have_encryption_key;
  70. BEncryption encryptor;
  71. int input_mtu;
  72. int output_mtu;
  73. int in_len;
  74. PacketRecvInterface output;
  75. int out_have;
  76. uint8_t *out;
  77. uint8_t *buf;
  78. BPending handler_job;
  79. int tw_have;
  80. BThreadWork tw;
  81. uint16_t tw_seed_id;
  82. otp_t tw_otp;
  83. int tw_out_len;
  84. DebugObject d_obj;
  85. } SPProtoEncoder;
  86. /**
  87. * Initializes the object.
  88. * The object is initialized in blocked state.
  89. * {@link BSecurity_GlobalInitThreadSafe} must have been done if
  90. * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
  91. *
  92. * @param o the object
  93. * @param input input interface. Its MTU must not be too large, i.e. this must hold:
  94. * spproto_carrier_mtu_for_payload_mtu(sp_params, input MTU) >= 0
  95. * @param sp_params SPProto security parameters
  96. * @param otp_warning_count If using OTPs, after how many encoded packets to call the handler.
  97. * In this case, must be >0 and <=sp_params.otp_num.
  98. * @param pg pending group
  99. * @param twd thread work dispatcher
  100. * @return 1 on success, 0 on failure
  101. */
  102. int SPProtoEncoder_Init (SPProtoEncoder *o, PacketRecvInterface *input, struct spproto_security_params sp_params, int otp_warning_count, BPendingGroup *pg, BThreadWorkDispatcher *twd) WARN_UNUSED;
  103. /**
  104. * Frees the object.
  105. *
  106. * @param o the object
  107. */
  108. void SPProtoEncoder_Free (SPProtoEncoder *o);
  109. /**
  110. * Returns the output interface.
  111. * The MTU of the output interface will depend on the input MTU and security parameters,
  112. * that is spproto_carrier_mtu_for_payload_mtu(sp_params, input MTU).
  113. *
  114. * @param o the object
  115. * @return output interface
  116. */
  117. PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o);
  118. /**
  119. * Sets an encryption key to use.
  120. * Encryption must be enabled.
  121. *
  122. * @param o the object
  123. * @param encryption_key key to use
  124. */
  125. void SPProtoEncoder_SetEncryptionKey (SPProtoEncoder *o, uint8_t *encryption_key);
  126. /**
  127. * Removes an encryption key if one is configured.
  128. * Encryption must be enabled.
  129. *
  130. * @param o the object
  131. */
  132. void SPProtoEncoder_RemoveEncryptionKey (SPProtoEncoder *o);
  133. /**
  134. * Sets an OTP seed to use.
  135. * OTPs must be enabled.
  136. *
  137. * @param o the object
  138. * @param seed_id seed identifier
  139. * @param key OTP encryption key
  140. * @param iv OTP initialization vector
  141. */
  142. void SPProtoEncoder_SetOTPSeed (SPProtoEncoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  143. /**
  144. * Removes the OTP seed if one is configured.
  145. * OTPs must be enabled.
  146. *
  147. * @param o the object
  148. */
  149. void SPProtoEncoder_RemoveOTPSeed (SPProtoEncoder *o);
  150. /**
  151. * Sets handlers.
  152. *
  153. * @param o the object
  154. * @param handler OTP warning handler
  155. * @param user value to pass to handler
  156. */
  157. void SPProtoEncoder_SetHandlers (SPProtoEncoder *o, SPProtoEncoder_handler handler, void *user);
  158. #endif