spproto.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <limits.h>
  53. #include <misc/debug.h>
  54. #include <misc/balign.h>
  55. #include <security/BHash.h>
  56. #include <security/BEncryption.h>
  57. #include <security/OTPCalculator.h>
  58. #define SPPROTO_HASH_MODE_NONE 0
  59. #define SPPROTO_ENCRYPTION_MODE_NONE 0
  60. #define SPPROTO_OTP_MODE_NONE 0
  61. /**
  62. * Stores security parameters for SPProto.
  63. */
  64. struct spproto_security_params {
  65. /**
  66. * Hash mode.
  67. * Either SPPROTO_HASH_MODE_NONE for no hashes, or a valid bhash
  68. * hash mode.
  69. */
  70. int hash_mode;
  71. /**
  72. * Encryption mode.
  73. * Either SPPROTO_ENCRYPTION_MODE_NONE for no encryption, or a valid
  74. * {@link BEncryption} cipher.
  75. */
  76. int encryption_mode;
  77. /**
  78. * One-time password (OTP) mode.
  79. * Either SPPROTO_OTP_MODE_NONE for no OTPs, or a valid
  80. * {@link BEncryption} cipher.
  81. */
  82. int otp_mode;
  83. /**
  84. * If OTPs are used (otp_mode != SPPROTO_OTP_MODE_NONE), number of
  85. * OTPs generated from a single seed.
  86. */
  87. int otp_num;
  88. };
  89. #define SPPROTO_HAVE_HASH(_params) ((_params).hash_mode != SPPROTO_HASH_MODE_NONE)
  90. #define SPPROTO_HASH_SIZE(_params) ( \
  91. SPPROTO_HAVE_HASH(_params) ? \
  92. BHash_size((_params).hash_mode) : \
  93. 0 \
  94. )
  95. #define SPPROTO_HAVE_ENCRYPTION(_params) ((_params).encryption_mode != SPPROTO_ENCRYPTION_MODE_NONE)
  96. #define SPPROTO_HAVE_OTP(_params) ((_params).otp_mode != SPPROTO_OTP_MODE_NONE)
  97. struct spproto_otpdata {
  98. uint16_t seed_id;
  99. otp_t otp;
  100. } __attribute__((packed));
  101. #define SPPROTO_HEADER_OTPDATA_OFF(_params) 0
  102. #define SPPROTO_HEADER_OTPDATA_LEN(_params) (SPPROTO_HAVE_OTP(_params) ? sizeof(struct spproto_otpdata) : 0)
  103. #define SPPROTO_HEADER_HASH_OFF(_params) (SPPROTO_HEADER_OTPDATA_OFF(_params) + SPPROTO_HEADER_OTPDATA_LEN(_params))
  104. #define SPPROTO_HEADER_HASH_LEN(_params) SPPROTO_HASH_SIZE(_params)
  105. #define SPPROTO_HEADER_LEN(_params) (SPPROTO_HEADER_HASH_OFF(_params) + SPPROTO_HEADER_HASH_LEN(_params))
  106. /**
  107. * Asserts that the given SPProto security parameters are valid.
  108. *
  109. * @param params security parameters
  110. */
  111. static void spproto_assert_security_params (struct spproto_security_params params)
  112. {
  113. ASSERT(params.hash_mode == SPPROTO_HASH_MODE_NONE || BHash_type_valid(params.hash_mode))
  114. ASSERT(params.encryption_mode == SPPROTO_ENCRYPTION_MODE_NONE || BEncryption_cipher_valid(params.encryption_mode))
  115. ASSERT(params.otp_mode == SPPROTO_OTP_MODE_NONE || BEncryption_cipher_valid(params.otp_mode))
  116. ASSERT(params.otp_mode == SPPROTO_OTP_MODE_NONE || params.otp_num > 0)
  117. }
  118. /**
  119. * Calculates the maximum payload size for SPProto given the
  120. * security parameters and the maximum encoded packet size.
  121. *
  122. * @param params security parameters
  123. * @param carrier_mtu maximum encoded packet size. Must be >=0.
  124. * @return maximum payload size. Negative means is is impossible
  125. * to encode anything.
  126. */
  127. static int spproto_payload_mtu_for_carrier_mtu (struct spproto_security_params params, int carrier_mtu)
  128. {
  129. spproto_assert_security_params(params);
  130. ASSERT(carrier_mtu >= 0)
  131. if (params.encryption_mode == SPPROTO_ENCRYPTION_MODE_NONE) {
  132. return (carrier_mtu - SPPROTO_HEADER_LEN(params));
  133. } else {
  134. int block_size = BEncryption_cipher_block_size(params.encryption_mode);
  135. return (balign_down(carrier_mtu, block_size) - block_size - SPPROTO_HEADER_LEN(params) - 1);
  136. }
  137. }
  138. /**
  139. * Calculates the maximum encoded packet size for SPProto given the
  140. * security parameters and the maximum payload size.
  141. *
  142. * @param params security parameters
  143. * @param payload_mtu maximum payload size. Must be >=0.
  144. * @return maximum encoded packet size, -1 if payload_mtu is too large
  145. */
  146. static int spproto_carrier_mtu_for_payload_mtu (struct spproto_security_params params, int payload_mtu)
  147. {
  148. spproto_assert_security_params(params);
  149. ASSERT(payload_mtu >= 0)
  150. if (params.encryption_mode == SPPROTO_ENCRYPTION_MODE_NONE) {
  151. if (payload_mtu > INT_MAX - SPPROTO_HEADER_LEN(params)) {
  152. return -1;
  153. }
  154. return (SPPROTO_HEADER_LEN(params) + payload_mtu);
  155. } else {
  156. int block_size = BEncryption_cipher_block_size(params.encryption_mode);
  157. if (payload_mtu > INT_MAX - (block_size + SPPROTO_HEADER_LEN(params) + block_size)) {
  158. return -1;
  159. }
  160. return (block_size + balign_up((SPPROTO_HEADER_LEN(params) + payload_mtu + 1), block_size));
  161. }
  162. }
  163. #endif