spproto.h 7.0 KB

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