SPProtoEncoder.h 4.2 KB

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