SPProtoEncoder.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. DebugObject d_obj;
  59. } SPProtoEncoder;
  60. /**
  61. * Initializes the object.
  62. * The object is initialized in blocked state.
  63. *
  64. * @param o the object
  65. * @param input input interface. Its MTU must not be too large, i.e. this must hold:
  66. * spproto_carrier_mtu_for_payload_mtu(sp_params, input MTU) >= 0
  67. * @param sp_params SPProto security parameters
  68. * @param pg pending group
  69. * @return 1 on success, 0 on failure
  70. */
  71. int SPProtoEncoder_Init (SPProtoEncoder *o, PacketRecvInterface *input, struct spproto_security_params sp_params, BPendingGroup *pg) WARN_UNUSED;
  72. /**
  73. * Frees the object.
  74. *
  75. * @param o the object
  76. */
  77. void SPProtoEncoder_Free (SPProtoEncoder *o);
  78. /**
  79. * Returns the output interface.
  80. * The MTU of the output interface will depend on the input MTU and security parameters,
  81. * that is spproto_carrier_mtu_for_payload_mtu(sp_params, input MTU).
  82. *
  83. * @param o the object
  84. * @return output interface
  85. */
  86. PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o);
  87. /**
  88. * Sets an encryption key to use.
  89. * Encryption must be enabled.
  90. *
  91. * @param o the object
  92. * @param encryption_key key to use
  93. */
  94. void SPProtoEncoder_SetEncryptionKey (SPProtoEncoder *o, uint8_t *encryption_key);
  95. /**
  96. * Removes an encryption key if one is configured.
  97. * Encryption must be enabled.
  98. *
  99. * @param o the object
  100. */
  101. void SPProtoEncoder_RemoveEncryptionKey (SPProtoEncoder *o);
  102. /**
  103. * Sets an OTP seed to use.
  104. * OTPs must be enabled.
  105. *
  106. * @param o the object
  107. * @param seed_id seed identifier
  108. * @param key OTP encryption key
  109. * @param iv OTP initialization vector
  110. */
  111. void SPProtoEncoder_SetOTPSeed (SPProtoEncoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  112. /**
  113. * Removes the OTP seed if one is configured.
  114. * OTPs must be enabled.
  115. *
  116. * @param o the object
  117. */
  118. void SPProtoEncoder_RemoveOTPSeed (SPProtoEncoder *o);
  119. /**
  120. * Returns the number of OTPs used so far, or total number if
  121. * no seed has been set yet.
  122. * OTPs must be enabled.
  123. *
  124. * @param o the object
  125. * @return OTP position
  126. */
  127. int SPProtoEncoder_GetOTPPosition (SPProtoEncoder *o);
  128. #endif