SPProtoEncoder.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * @file SPProtoEncoder.c
  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. #include <string.h>
  23. #include <stdlib.h>
  24. #include <misc/debug.h>
  25. #include <misc/balign.h>
  26. #include <misc/offset.h>
  27. #include <misc/byteorder.h>
  28. #include <security/BRandom.h>
  29. #include <security/BHash.h>
  30. #include <flow/SPProtoEncoder.h>
  31. static int can_encode (SPProtoEncoder *o)
  32. {
  33. ASSERT(o->in_len >= 0)
  34. ASSERT(o->out_have)
  35. return (
  36. (!SPPROTO_HAVE_OTP(o->sp_params) || OTPGenerator_GetPosition(&o->otpgen) < o->sp_params.otp_num) &&
  37. (!SPPROTO_HAVE_ENCRYPTION(o->sp_params) || o->have_encryption_key)
  38. );
  39. }
  40. static int encode_packet (SPProtoEncoder *o)
  41. {
  42. ASSERT(o->in_len >= 0)
  43. ASSERT(o->out_have)
  44. ASSERT(can_encode(o))
  45. ASSERT(o->in_len <= o->input_mtu)
  46. // determine plaintext location
  47. uint8_t *plaintext = (SPPROTO_HAVE_ENCRYPTION(o->sp_params) ? o->buf : o->out);
  48. // plaintext begins with header
  49. uint8_t *header = plaintext;
  50. // plaintext is header + payload
  51. int plaintext_len = SPPROTO_HEADER_LEN(o->sp_params) + o->in_len;
  52. // write OTP
  53. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  54. struct spproto_otpdata *header_otpd = (struct spproto_otpdata *)(header + SPPROTO_HEADER_OTPDATA_OFF(o->sp_params));
  55. header_otpd->seed_id = htol16(o->otpgen_seed_id);
  56. header_otpd->otp = OTPGenerator_GetOTP(&o->otpgen);
  57. }
  58. // write hash
  59. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  60. uint8_t *header_hash = header + SPPROTO_HEADER_HASH_OFF(o->sp_params);
  61. // zero hash field
  62. memset(header_hash, 0, o->hash_size);
  63. // calculate hash
  64. uint8_t hash[o->hash_size];
  65. BHash_calculate(o->sp_params.hash_mode, plaintext, plaintext_len, hash);
  66. // set hash field
  67. memcpy(header_hash, hash, o->hash_size);
  68. }
  69. int out_len;
  70. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  71. // encrypting pad(header + payload)
  72. int cyphertext_len = BALIGN_UP_N((plaintext_len + 1), o->enc_block_size);
  73. // write padding
  74. plaintext[plaintext_len] = 1;
  75. for (int i = plaintext_len + 1; i < cyphertext_len; i++) {
  76. plaintext[i] = 0;
  77. }
  78. // generate IV
  79. BRandom_randomize(o->out, o->enc_block_size);
  80. // copy IV because BEncryption_Encrypt changes the IV
  81. uint8_t iv[o->enc_block_size];
  82. memcpy(iv, o->out, o->enc_block_size);
  83. // encrypt
  84. BEncryption_Encrypt(&o->encryptor, plaintext, o->out + o->enc_block_size, cyphertext_len, iv);
  85. out_len = o->enc_block_size + cyphertext_len;
  86. } else {
  87. out_len = plaintext_len;
  88. }
  89. // finish packet
  90. o->in_len = -1;
  91. o->out_have = 0;
  92. PacketRecvInterface_Done(&o->output, out_len);
  93. }
  94. static void maybe_encode (SPProtoEncoder *o)
  95. {
  96. if (o->in_len >= 0 && o->out_have && can_encode(o)) {
  97. encode_packet(o);
  98. }
  99. }
  100. static void output_handler_recv (SPProtoEncoder *o, uint8_t *data)
  101. {
  102. ASSERT(o->in_len == -1)
  103. ASSERT(!o->out_have)
  104. DebugObject_Access(&o->d_obj);
  105. // remember output packet
  106. o->out_have = 1;
  107. o->out = data;
  108. // determine plaintext location
  109. uint8_t *plaintext = (SPPROTO_HAVE_ENCRYPTION(o->sp_params) ? o->buf : o->out);
  110. // schedule receive
  111. PacketRecvInterface_Receiver_Recv(o->input, plaintext + SPPROTO_HEADER_LEN(o->sp_params));
  112. }
  113. static void input_handler_done (SPProtoEncoder *o, int data_len)
  114. {
  115. ASSERT(data_len >= 0)
  116. ASSERT(data_len <= o->input_mtu)
  117. ASSERT(o->in_len == -1)
  118. ASSERT(o->out_have)
  119. DebugObject_Access(&o->d_obj);
  120. // remember input packet
  121. o->in_len = data_len;
  122. // encode if possible
  123. if (can_encode(o)) {
  124. encode_packet(o);
  125. }
  126. }
  127. int SPProtoEncoder_Init (SPProtoEncoder *o, PacketRecvInterface *input, struct spproto_security_params sp_params, BPendingGroup *pg)
  128. {
  129. spproto_assert_security_params(sp_params);
  130. ASSERT(spproto_carrier_mtu_for_payload_mtu(sp_params, PacketRecvInterface_GetMTU(input)) >= 0)
  131. // init parameters
  132. o->sp_params = sp_params;
  133. o->input = input;
  134. // calculate hash size
  135. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  136. o->hash_size = BHash_size(o->sp_params.hash_mode);
  137. }
  138. // calculate encryption block and key sizes
  139. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  140. o->enc_block_size = BEncryption_cipher_block_size(o->sp_params.encryption_mode);
  141. o->enc_key_size = BEncryption_cipher_key_size(o->sp_params.encryption_mode);
  142. }
  143. // init otp generator
  144. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  145. if (!OTPGenerator_Init(&o->otpgen, o->sp_params.otp_num, o->sp_params.otp_mode)) {
  146. goto fail0;
  147. }
  148. }
  149. // have no encryption key
  150. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  151. o->have_encryption_key = 0;
  152. }
  153. // remember input MTU
  154. o->input_mtu = PacketRecvInterface_GetMTU(o->input);
  155. // calculate output MTU
  156. o->output_mtu = spproto_carrier_mtu_for_payload_mtu(o->sp_params, o->input_mtu);
  157. // init input
  158. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  159. // have no input in buffer
  160. o->in_len = -1;
  161. // init output
  162. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  163. // have no output available
  164. o->out_have = 0;
  165. // allocate plaintext buffer
  166. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  167. int buf_size = BALIGN_UP_N((SPPROTO_HEADER_LEN(o->sp_params) + o->input_mtu + 1), o->enc_block_size);
  168. if (!(o->buf = malloc(buf_size))) {
  169. goto fail1;
  170. }
  171. }
  172. DebugObject_Init(&o->d_obj);
  173. return 1;
  174. fail1:
  175. PacketRecvInterface_Free(&o->output);
  176. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  177. OTPGenerator_Free(&o->otpgen);
  178. }
  179. fail0:
  180. return 0;
  181. }
  182. void SPProtoEncoder_Free (SPProtoEncoder *o)
  183. {
  184. DebugObject_Free(&o->d_obj);
  185. // free plaintext buffer
  186. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  187. free(o->buf);
  188. }
  189. // free output
  190. PacketRecvInterface_Free(&o->output);
  191. // free encryptor
  192. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params) && o->have_encryption_key) {
  193. BEncryption_Free(&o->encryptor);
  194. }
  195. // free otp generator
  196. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  197. OTPGenerator_Free(&o->otpgen);
  198. }
  199. }
  200. PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o)
  201. {
  202. DebugObject_Access(&o->d_obj);
  203. return &o->output;
  204. }
  205. void SPProtoEncoder_SetEncryptionKey (SPProtoEncoder *o, uint8_t *encryption_key)
  206. {
  207. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  208. DebugObject_Access(&o->d_obj);
  209. // free encryptor
  210. if (o->have_encryption_key) {
  211. BEncryption_Free(&o->encryptor);
  212. }
  213. // init encryptor
  214. BEncryption_Init(&o->encryptor, BENCRYPTION_MODE_ENCRYPT, o->sp_params.encryption_mode, encryption_key);
  215. // have encryption key
  216. o->have_encryption_key = 1;
  217. // possibly continue I/O
  218. maybe_encode(o);
  219. }
  220. void SPProtoEncoder_RemoveEncryptionKey (SPProtoEncoder *o)
  221. {
  222. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  223. DebugObject_Access(&o->d_obj);
  224. if (o->have_encryption_key) {
  225. // free encryptor
  226. BEncryption_Free(&o->encryptor);
  227. // have no encryption key
  228. o->have_encryption_key = 0;
  229. }
  230. }
  231. void SPProtoEncoder_SetOTPSeed (SPProtoEncoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv)
  232. {
  233. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  234. DebugObject_Access(&o->d_obj);
  235. // give seed to OTP generator
  236. OTPGenerator_SetSeed(&o->otpgen, key, iv);
  237. // remember seed ID
  238. o->otpgen_seed_id = seed_id;
  239. // possibly continue I/O
  240. maybe_encode(o);
  241. }
  242. void SPProtoEncoder_RemoveOTPSeed (SPProtoEncoder *o)
  243. {
  244. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  245. DebugObject_Access(&o->d_obj);
  246. // reset OTP generator
  247. OTPGenerator_Reset(&o->otpgen);
  248. }
  249. int SPProtoEncoder_GetOTPPosition (SPProtoEncoder *o)
  250. {
  251. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  252. DebugObject_Access(&o->d_obj);
  253. return OTPGenerator_GetPosition(&o->otpgen);
  254. }