SPProtoEncoder.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * Object which encodes packets according to SPProto.
  37. *
  38. * Input is with {@link PacketRecvInterface}.
  39. * Output is with {@link PacketRecvInterface}.
  40. */
  41. typedef struct {
  42. struct spproto_security_params sp_params;
  43. int hash_size;
  44. int enc_block_size;
  45. int enc_key_size;
  46. OTPGenerator otpgen;
  47. uint16_t otpgen_seed_id;
  48. int have_encryption_key;
  49. BEncryption encryptor;
  50. int input_mtu;
  51. int output_mtu;
  52. PacketRecvInterface *input;
  53. int in_len;
  54. PacketRecvInterface output;
  55. int out_have;
  56. uint8_t *out;
  57. uint8_t *buf;
  58. LinkedList2Node group_list_node;
  59. BPending continue_job;
  60. DebugObject d_obj;
  61. } SPProtoEncoder;
  62. /**
  63. * Initializes the object.
  64. * The object is initialized in blocked state.
  65. *
  66. * @param o the object
  67. * @param sp_params SPProto security parameters. Must be valid according to {@link spproto_validate_security_params}.
  68. * @param input input interface
  69. * @param pg pending group
  70. * @return 1 on success, 0 on failure
  71. */
  72. int SPProtoEncoder_Init (SPProtoEncoder *o, struct spproto_security_params sp_params, PacketRecvInterface *input, BPendingGroup *pg) WARN_UNUSED;
  73. /**
  74. * Frees the object.
  75. *
  76. * @param o the object
  77. */
  78. void SPProtoEncoder_Free (SPProtoEncoder *o);
  79. /**
  80. * Returns the output interface.
  81. * The MTU of the output interface will depend on the input MTU and security parameters,
  82. * that is spproto_carrier_mtu_for_payload_mtu(sp_params, input MTU).
  83. *
  84. * @param o the object
  85. * @return output interface
  86. */
  87. PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o);
  88. /**
  89. * Sets an encryption key to use.
  90. * Encryption must be enabled.
  91. *
  92. * @param o the object
  93. * @param encryption_key key to use
  94. */
  95. void SPProtoEncoder_SetEncryptionKey (SPProtoEncoder *o, uint8_t *encryption_key);
  96. /**
  97. * Removes an encryption key if one is configured.
  98. * Encryption must be enabled.
  99. *
  100. * @param o the object
  101. */
  102. void SPProtoEncoder_RemoveEncryptionKey (SPProtoEncoder *o);
  103. /**
  104. * Sets an OTP seed to use.
  105. * OTPs must be enabled.
  106. *
  107. * @param o the object
  108. * @param seed_id seed identifier
  109. * @param key OTP encryption key
  110. * @param iv OTP initialization vector
  111. */
  112. void SPProtoEncoder_SetOTPSeed (SPProtoEncoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  113. /**
  114. * Removes the OTP seed if one is configured.
  115. * OTPs must be enabled.
  116. *
  117. * @param o the object
  118. */
  119. void SPProtoEncoder_RemoveOTPSeed (SPProtoEncoder *o);
  120. /**
  121. * Returns the number of OTPs used so far, or total number if
  122. * no seed has been set yet.
  123. * OTPs must be enabled.
  124. *
  125. * @param o the object
  126. * @return OTP position
  127. */
  128. int SPProtoEncoder_GetOTPPosition (SPProtoEncoder *o);
  129. #endif