spproto.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * @file spproto.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. * Protocol for securing datagram communication.
  25. *
  26. * Security features implemented:
  27. * - Encryption. Encrypts packets with a block cipher.
  28. * Protects against a third party from seeing the data
  29. * being transmitted.
  30. * - Hashes. Adds a hash of the packet into the packet.
  31. * Combined with encryption, protects against tampering
  32. * with packets and crafting new packets.
  33. * - One-time passwords. Adds a password to each packet
  34. * for the receiver to recognize. Protects agains replaying
  35. * packets and crafting new packets.
  36. *
  37. * A SPProto plaintext packet contains the following, in order:
  38. * - if OTPs are used, a struct {@link spproto_otpdata} which contains
  39. * the seed ID and the OTP,
  40. * - if hashes are used, the hash,
  41. * - payload data.
  42. *
  43. * If encryption is used:
  44. * - the plaintext is padded by appending a 0x01 byte and as many 0x00
  45. * bytes as needed to align to block size,
  46. * - the padded plaintext is encrypted, and
  47. * - the initialization vector (IV) is prepended.
  48. */
  49. #ifndef BADVPN_PROTOCOL_SPPROTO_H
  50. #define BADVPN_PROTOCOL_SPPROTO_H
  51. #include <stdint.h>
  52. #include <misc/debug.h>
  53. #include <misc/balign.h>
  54. #include <security/bhash.h>
  55. #include <security/BEncryption.h>
  56. #include <security/OTPCalculator.h>
  57. #define SPPROTO_HASH_MODE_NONE 0
  58. #define SPPROTO_ENCRYPTION_MODE_NONE 0
  59. #define SPPROTO_OTP_MODE_NONE 0
  60. /**
  61. * Stores security parameters for SPProto.
  62. */
  63. struct spproto_security_params {
  64. /**
  65. * Hash mode.
  66. * Either SPPROTO_HASH_MODE_NONE for no hashes, or a valid bhash
  67. * hash mode.
  68. */
  69. int hash_mode;
  70. /**
  71. * Encryption mode.
  72. * Either SPPROTO_ENCRYPTION_MODE_NONE for no encryption, or a valid
  73. * {@link BEncryption} cipher.
  74. */
  75. int encryption_mode;
  76. /**
  77. * One-time password (OTP) mode.
  78. * Either SPPROTO_OTP_MODE_NONE for no OTPs, or a valid
  79. * {@link BEncryption} cipher.
  80. */
  81. int otp_mode;
  82. /**
  83. * If OTPs are used (otp_mode != SPPROTO_OTP_MODE_NONE), number of
  84. * OTPs generated from a single seed.
  85. */
  86. int otp_num;
  87. };
  88. #define SPPROTO_HAVE_HASH(_params) ((_params).hash_mode != SPPROTO_HASH_MODE_NONE)
  89. #define SPPROTO_HASH_SIZE(_params) ( \
  90. SPPROTO_HAVE_HASH(_params) ? \
  91. BHash_size((_params).hash_mode) : \
  92. 0 \
  93. )
  94. #define SPPROTO_HAVE_ENCRYPTION(_params) ((_params).encryption_mode != SPPROTO_ENCRYPTION_MODE_NONE)
  95. #define SPPROTO_HAVE_OTP(_params) ((_params).otp_mode != SPPROTO_OTP_MODE_NONE)
  96. struct spproto_otpdata {
  97. uint16_t seed_id;
  98. otp_t otp;
  99. } __attribute__((packed));
  100. #define SPPROTO_HEADER_OTPDATA_OFF(_params) 0
  101. #define SPPROTO_HEADER_OTPDATA_LEN(_params) (SPPROTO_HAVE_OTP(_params) ? sizeof(struct spproto_otpdata) : 0)
  102. #define SPPROTO_HEADER_HASH_OFF(_params) (SPPROTO_HEADER_OTPDATA_OFF(_params) + SPPROTO_HEADER_OTPDATA_LEN(_params))
  103. #define SPPROTO_HEADER_HASH_LEN(_params) SPPROTO_HASH_SIZE(_params)
  104. #define SPPROTO_HEADER_LEN(_params) (SPPROTO_HEADER_HASH_OFF(_params) + SPPROTO_HEADER_HASH_LEN(_params))
  105. /**
  106. * Checks if the given SPProto security parameters are valid.
  107. *
  108. * @param params security parameters to check
  109. * @return 1 if valid, 0 if not
  110. */
  111. static int spproto_validate_security_params (struct spproto_security_params params)
  112. {
  113. return (
  114. (params.hash_mode == SPPROTO_HASH_MODE_NONE || BHash_type_valid(params.hash_mode)) &&
  115. (params.encryption_mode == SPPROTO_ENCRYPTION_MODE_NONE || BEncryption_cipher_valid(params.encryption_mode)) &&
  116. (params.otp_mode == SPPROTO_OTP_MODE_NONE || BEncryption_cipher_valid(params.otp_mode)) &&
  117. (params.otp_mode == SPPROTO_OTP_MODE_NONE || params.otp_num > 0)
  118. );
  119. }
  120. /**
  121. * Calculates the maximum payload size for SPProto given the
  122. * security parameters and the maximum encoded packet size.
  123. *
  124. * @param params security parameters Must be valid according to
  125. * {@link spproto_validate_security_params}.
  126. * @param carrier_mtu maximum encoded packet size. Must be >=0.
  127. * @return maximum payload size. Negative means is is impossible
  128. * to encode anything.
  129. */
  130. static int spproto_payload_mtu_for_carrier_mtu (struct spproto_security_params params, int carrier_mtu)
  131. {
  132. ASSERT(spproto_validate_security_params(params))
  133. ASSERT(carrier_mtu >= 0)
  134. if (params.encryption_mode == SPPROTO_ENCRYPTION_MODE_NONE) {
  135. return (carrier_mtu - SPPROTO_HEADER_LEN(params));
  136. } else {
  137. int block_size = BEncryption_cipher_block_size(params.encryption_mode);
  138. return (BALIGN_DOWN_N(carrier_mtu, block_size) - block_size - SPPROTO_HEADER_LEN(params) - 1);
  139. }
  140. }
  141. /**
  142. * Calculates the maximum encoded packet size for SPProto given the
  143. * security parameters and the maximum payload size.
  144. *
  145. * @param params security parameters Must be valid according to
  146. * {@link spproto_validate_security_params}.
  147. * @param payload_mtu maximum payload size. Must be >=0.
  148. * @return maximum encoded packet size
  149. */
  150. static int spproto_carrier_mtu_for_payload_mtu (struct spproto_security_params params, int payload_mtu)
  151. {
  152. ASSERT(spproto_validate_security_params(params))
  153. ASSERT(payload_mtu >= 0)
  154. if (params.encryption_mode == SPPROTO_ENCRYPTION_MODE_NONE) {
  155. return (SPPROTO_HEADER_LEN(params) + payload_mtu);
  156. } else {
  157. int block_size = BEncryption_cipher_block_size(params.encryption_mode);
  158. return (block_size + BALIGN_UP_N((SPPROTO_HEADER_LEN(params) + payload_mtu + 1), block_size));
  159. }
  160. }
  161. #endif