spproto.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @file spproto.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Protocol for securing datagram communication.
  32. *
  33. * Security features implemented:
  34. * - Encryption. Encrypts packets with a block cipher.
  35. * Protects against a third party from seeing the data
  36. * being transmitted.
  37. * - Hashes. Adds a hash of the packet into the packet.
  38. * Combined with encryption, protects against tampering
  39. * with packets and crafting new packets.
  40. * - One-time passwords. Adds a password to each packet
  41. * for the receiver to recognize. Protects agains replaying
  42. * packets and crafting new packets.
  43. *
  44. * A SPProto plaintext packet contains the following, in order:
  45. * - if OTPs are used, a struct {@link spproto_otpdata} which contains
  46. * the seed ID and the OTP,
  47. * - if hashes are used, the hash,
  48. * - payload data.
  49. *
  50. * If encryption is used:
  51. * - the plaintext is padded by appending a 0x01 byte and as many 0x00
  52. * bytes as needed to align to block size,
  53. * - the padded plaintext is encrypted, and
  54. * - the initialization vector (IV) is prepended.
  55. */
  56. #ifndef BADVPN_PROTOCOL_SPPROTO_H
  57. #define BADVPN_PROTOCOL_SPPROTO_H
  58. #include <stdint.h>
  59. #include <limits.h>
  60. #include <misc/debug.h>
  61. #include <misc/balign.h>
  62. #include <misc/packed.h>
  63. #include <security/BHash.h>
  64. #include <security/BEncryption.h>
  65. #include <security/OTPCalculator.h>
  66. #define SPPROTO_HASH_MODE_NONE 0
  67. #define SPPROTO_ENCRYPTION_MODE_NONE 0
  68. #define SPPROTO_OTP_MODE_NONE 0
  69. /**
  70. * Stores security parameters for SPProto.
  71. */
  72. struct spproto_security_params {
  73. /**
  74. * Hash mode.
  75. * Either SPPROTO_HASH_MODE_NONE for no hashes, or a valid bhash
  76. * hash mode.
  77. */
  78. int hash_mode;
  79. /**
  80. * Encryption mode.
  81. * Either SPPROTO_ENCRYPTION_MODE_NONE for no encryption, or a valid
  82. * {@link BEncryption} cipher.
  83. */
  84. int encryption_mode;
  85. /**
  86. * One-time password (OTP) mode.
  87. * Either SPPROTO_OTP_MODE_NONE for no OTPs, or a valid
  88. * {@link BEncryption} cipher.
  89. */
  90. int otp_mode;
  91. /**
  92. * If OTPs are used (otp_mode != SPPROTO_OTP_MODE_NONE), number of
  93. * OTPs generated from a single seed.
  94. */
  95. int otp_num;
  96. };
  97. #define SPPROTO_HAVE_HASH(_params) ((_params).hash_mode != SPPROTO_HASH_MODE_NONE)
  98. #define SPPROTO_HASH_SIZE(_params) ( \
  99. SPPROTO_HAVE_HASH(_params) ? \
  100. BHash_size((_params).hash_mode) : \
  101. 0 \
  102. )
  103. #define SPPROTO_HAVE_ENCRYPTION(_params) ((_params).encryption_mode != SPPROTO_ENCRYPTION_MODE_NONE)
  104. #define SPPROTO_HAVE_OTP(_params) ((_params).otp_mode != SPPROTO_OTP_MODE_NONE)
  105. B_START_PACKED
  106. struct spproto_otpdata {
  107. uint16_t seed_id;
  108. otp_t otp;
  109. } B_PACKED;
  110. B_END_PACKED
  111. #define SPPROTO_HEADER_OTPDATA_OFF(_params) 0
  112. #define SPPROTO_HEADER_OTPDATA_LEN(_params) (SPPROTO_HAVE_OTP(_params) ? sizeof(struct spproto_otpdata) : 0)
  113. #define SPPROTO_HEADER_HASH_OFF(_params) (SPPROTO_HEADER_OTPDATA_OFF(_params) + SPPROTO_HEADER_OTPDATA_LEN(_params))
  114. #define SPPROTO_HEADER_HASH_LEN(_params) SPPROTO_HASH_SIZE(_params)
  115. #define SPPROTO_HEADER_LEN(_params) (SPPROTO_HEADER_HASH_OFF(_params) + SPPROTO_HEADER_HASH_LEN(_params))
  116. /**
  117. * Asserts that the given SPProto security parameters are valid.
  118. *
  119. * @param params security parameters
  120. */
  121. static void spproto_assert_security_params (struct spproto_security_params params)
  122. {
  123. ASSERT(params.hash_mode == SPPROTO_HASH_MODE_NONE || BHash_type_valid(params.hash_mode))
  124. ASSERT(params.encryption_mode == SPPROTO_ENCRYPTION_MODE_NONE || BEncryption_cipher_valid(params.encryption_mode))
  125. ASSERT(params.otp_mode == SPPROTO_OTP_MODE_NONE || BEncryption_cipher_valid(params.otp_mode))
  126. ASSERT(params.otp_mode == SPPROTO_OTP_MODE_NONE || params.otp_num > 0)
  127. }
  128. /**
  129. * Calculates the maximum payload size for SPProto given the
  130. * security parameters and the maximum encoded packet size.
  131. *
  132. * @param params security parameters
  133. * @param carrier_mtu maximum encoded packet size. Must be >=0.
  134. * @return maximum payload size. Negative means is is impossible
  135. * to encode anything.
  136. */
  137. static int spproto_payload_mtu_for_carrier_mtu (struct spproto_security_params params, int carrier_mtu)
  138. {
  139. spproto_assert_security_params(params);
  140. ASSERT(carrier_mtu >= 0)
  141. if (params.encryption_mode == SPPROTO_ENCRYPTION_MODE_NONE) {
  142. return (carrier_mtu - SPPROTO_HEADER_LEN(params));
  143. } else {
  144. int block_size = BEncryption_cipher_block_size(params.encryption_mode);
  145. return (balign_down(carrier_mtu, block_size) - block_size - SPPROTO_HEADER_LEN(params) - 1);
  146. }
  147. }
  148. /**
  149. * Calculates the maximum encoded packet size for SPProto given the
  150. * security parameters and the maximum payload size.
  151. *
  152. * @param params security parameters
  153. * @param payload_mtu maximum payload size. Must be >=0.
  154. * @return maximum encoded packet size, -1 if payload_mtu is too large
  155. */
  156. static int spproto_carrier_mtu_for_payload_mtu (struct spproto_security_params params, int payload_mtu)
  157. {
  158. spproto_assert_security_params(params);
  159. ASSERT(payload_mtu >= 0)
  160. if (params.encryption_mode == SPPROTO_ENCRYPTION_MODE_NONE) {
  161. if (payload_mtu > INT_MAX - SPPROTO_HEADER_LEN(params)) {
  162. return -1;
  163. }
  164. return (SPPROTO_HEADER_LEN(params) + payload_mtu);
  165. } else {
  166. int block_size = BEncryption_cipher_block_size(params.encryption_mode);
  167. if (payload_mtu > INT_MAX - (block_size + SPPROTO_HEADER_LEN(params) + block_size)) {
  168. return -1;
  169. }
  170. return (block_size + balign_up((SPPROTO_HEADER_LEN(params) + payload_mtu + 1), block_size));
  171. }
  172. }
  173. #endif