SPProtoEncoder.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. void *user;
  55. int hash_size;
  56. int enc_block_size;
  57. int enc_key_size;
  58. OTPGenerator otpgen;
  59. uint16_t otpgen_seed_id;
  60. uint16_t otpgen_pending_seed_id;
  61. int have_encryption_key;
  62. BEncryption encryptor;
  63. int input_mtu;
  64. int output_mtu;
  65. int in_len;
  66. PacketRecvInterface output;
  67. int out_have;
  68. uint8_t *out;
  69. uint8_t *buf;
  70. BPending handler_job;
  71. DebugObject d_obj;
  72. } SPProtoEncoder;
  73. /**
  74. * Initializes the object.
  75. * The object is initialized in blocked state.
  76. *
  77. * @param o the object
  78. * @param input input interface. Its MTU must not be too large, i.e. this must hold:
  79. * spproto_carrier_mtu_for_payload_mtu(sp_params, input MTU) >= 0
  80. * @param sp_params SPProto security parameters
  81. * @param otp_warning_count If using OTPs, after how many encoded packets to call the handler.
  82. * In this case, must be >0 and <=sp_params.otp_num.
  83. * @param handler OTP warning handler
  84. * @param user value to pass to handler
  85. * @param pg pending group
  86. * @param twd thread work dispatcher
  87. * @return 1 on success, 0 on failure
  88. */
  89. int SPProtoEncoder_Init (SPProtoEncoder *o, PacketRecvInterface *input, struct spproto_security_params sp_params, int otp_warning_count, SPProtoEncoder_handler handler, void *user, BPendingGroup *pg, BThreadWorkDispatcher *twd) WARN_UNUSED;
  90. /**
  91. * Frees the object.
  92. *
  93. * @param o the object
  94. */
  95. void SPProtoEncoder_Free (SPProtoEncoder *o);
  96. /**
  97. * Returns the output interface.
  98. * The MTU of the output interface will depend on the input MTU and security parameters,
  99. * that is spproto_carrier_mtu_for_payload_mtu(sp_params, input MTU).
  100. *
  101. * @param o the object
  102. * @return output interface
  103. */
  104. PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o);
  105. /**
  106. * Sets an encryption key to use.
  107. * Encryption must be enabled.
  108. *
  109. * @param o the object
  110. * @param encryption_key key to use
  111. */
  112. void SPProtoEncoder_SetEncryptionKey (SPProtoEncoder *o, uint8_t *encryption_key);
  113. /**
  114. * Removes an encryption key if one is configured.
  115. * Encryption must be enabled.
  116. *
  117. * @param o the object
  118. */
  119. void SPProtoEncoder_RemoveEncryptionKey (SPProtoEncoder *o);
  120. /**
  121. * Sets an OTP seed to use.
  122. * OTPs must be enabled.
  123. *
  124. * @param o the object
  125. * @param seed_id seed identifier
  126. * @param key OTP encryption key
  127. * @param iv OTP initialization vector
  128. */
  129. void SPProtoEncoder_SetOTPSeed (SPProtoEncoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  130. /**
  131. * Removes the OTP seed if one is configured.
  132. * OTPs must be enabled.
  133. *
  134. * @param o the object
  135. */
  136. void SPProtoEncoder_RemoveOTPSeed (SPProtoEncoder *o);
  137. #endif