SPProtoEncoder.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/dead.h>
  30. #include <misc/debug.h>
  31. #include <protocol/spproto.h>
  32. #include <structure/LinkedList2.h>
  33. #include <system/DebugObject.h>
  34. #include <system/BPending.h>
  35. #include <security/BEncryption.h>
  36. #include <security/OTPGenerator.h>
  37. #include <flow/PacketRecvInterface.h>
  38. /**
  39. * Object shared between {@link SPProtoEncoder} objects using the same security parameters and resources.
  40. */
  41. typedef struct {
  42. DebugObject d_obj;
  43. struct spproto_security_params sp_params;
  44. int hash_size;
  45. int enc_block_size;
  46. int enc_key_size;
  47. OTPGenerator otpgen;
  48. uint16_t otpgen_seed_id;
  49. int have_encryption_key;
  50. BEncryption encryptor;
  51. LinkedList2 encoders_list;
  52. } SPProtoEncoderGroup;
  53. /**
  54. * Initializes the object.
  55. *
  56. * @param o the object
  57. * @param sp_params SPProto security parameters. Must be valid according to {@link spproto_validate_security_params}.
  58. * @return 1 on success, 0 on failure
  59. */
  60. int SPProtoEncoderGroup_Init (SPProtoEncoderGroup *o, struct spproto_security_params sp_params) WARN_UNUSED;
  61. /**
  62. * Frees the object.
  63. * There must be no encoders using this group.
  64. *
  65. * @param o the object
  66. */
  67. void SPProtoEncoderGroup_Free (SPProtoEncoderGroup *o);
  68. /**
  69. * Sets an encryption key to use.
  70. * Encryption must be enabled.
  71. *
  72. * @param o the object
  73. * @param encryption_key key to use
  74. */
  75. void SPProtoEncoderGroup_SetEncryptionKey (SPProtoEncoderGroup *o, uint8_t *encryption_key);
  76. /**
  77. * Removes an encryption key if one is configured.
  78. * Encryption must be enabled.
  79. *
  80. * @param o the object
  81. */
  82. void SPProtoEncoderGroup_RemoveEncryptionKey (SPProtoEncoderGroup *o);
  83. /**
  84. * Sets an OTP seed to use.
  85. * OTPs must be enabled.
  86. *
  87. * @param o the object
  88. * @param seed_id seed identifier
  89. * @param key OTP encryption key
  90. * @param iv OTP initialization vector
  91. */
  92. void SPProtoEncoderGroup_SetOTPSeed (SPProtoEncoderGroup *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  93. /**
  94. * Removes the OTP seed if one is configured.
  95. * OTPs must be enabled.
  96. *
  97. * @param o the object
  98. */
  99. void SPProtoEncoderGroup_RemoveOTPSeed (SPProtoEncoderGroup *o);
  100. /**
  101. * Returns the number of OTPs used so far, or total number if
  102. * no seed has been set yet.
  103. * OTPs must be enabled.
  104. *
  105. * @param o the object
  106. * @return OTP position
  107. */
  108. int SPProtoEncoderGroup_GetOTPPosition (SPProtoEncoderGroup *o);
  109. /**
  110. * Object which encodes packets according to SPProto.
  111. *
  112. * Input is with {@link PacketRecvInterface}.
  113. * Output is with {@link PacketRecvInterface}.
  114. */
  115. typedef struct {
  116. DebugObject d_obj;
  117. dead_t dead;
  118. SPProtoEncoderGroup *group;
  119. int input_mtu;
  120. int output_mtu;
  121. PacketRecvInterface *input;
  122. int in_len;
  123. PacketRecvInterface output;
  124. int out_have;
  125. uint8_t *out;
  126. uint8_t *buf;
  127. LinkedList2Node group_list_node;
  128. BPending continue_job;
  129. } SPProtoEncoder;
  130. /**
  131. * Initializes the object.
  132. * The object is initialized in blocked state.
  133. *
  134. * @param o the object
  135. * @param group {@link SPProtoEncoderGroup} object to use for security parameters and resources
  136. * @param input input interface
  137. * @param pg pending group
  138. * @return 1 on success, 0 on failure
  139. */
  140. int SPProtoEncoder_Init (SPProtoEncoder *o, SPProtoEncoderGroup *group, PacketRecvInterface *input, BPendingGroup *pg) WARN_UNUSED;
  141. /**
  142. * Frees the object.
  143. *
  144. * @param o the object
  145. */
  146. void SPProtoEncoder_Free (SPProtoEncoder *o);
  147. /**
  148. * Returns the output interface.
  149. * The MTU of the output interface will depend on the input MTU and security parameters,
  150. * that is spproto_carrier_mtu_for_payload_mtu(sp_params, input MTU).
  151. *
  152. * @param o the object
  153. * @return output interface
  154. */
  155. PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o);
  156. #endif