SPProtoEncoder.h 3.7 KB

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