SPProtoEncoder.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. static void encode_packet (SPProtoEncoder *o);
  33. static void encode_work_func (SPProtoEncoder *o);
  34. static void encode_work_handler (SPProtoEncoder *o);
  35. static void maybe_encode (SPProtoEncoder *o);
  36. static void output_handler_recv (SPProtoEncoder *o, uint8_t *data);
  37. static void input_handler_done (SPProtoEncoder *o, int data_len);
  38. static void handler_job_hander (SPProtoEncoder *o);
  39. static void otpgenerator_handler (SPProtoEncoder *o);
  40. static void maybe_stop_work (SPProtoEncoder *o);
  41. static int can_encode (SPProtoEncoder *o)
  42. {
  43. ASSERT(o->in_len >= 0)
  44. ASSERT(o->out_have)
  45. ASSERT(!o->tw_have)
  46. return (
  47. (!SPPROTO_HAVE_OTP(o->sp_params) || OTPGenerator_GetPosition(&o->otpgen) < o->sp_params.otp_num) &&
  48. (!SPPROTO_HAVE_ENCRYPTION(o->sp_params) || o->have_encryption_key)
  49. );
  50. }
  51. static void encode_packet (SPProtoEncoder *o)
  52. {
  53. ASSERT(o->in_len >= 0)
  54. ASSERT(o->out_have)
  55. ASSERT(!o->tw_have)
  56. ASSERT(can_encode(o))
  57. // generate OTP, remember seed ID
  58. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  59. o->tw_seed_id = o->otpgen_seed_id;
  60. o->tw_otp = OTPGenerator_GetOTP(&o->otpgen);
  61. }
  62. // start work
  63. BThreadWork_Init(&o->tw, o->twd, (BThreadWork_handler_done)encode_work_handler, o, (BThreadWork_work_func)encode_work_func, o);
  64. o->tw_have = 1;
  65. // schedule OTP warning handler
  66. if (SPPROTO_HAVE_OTP(o->sp_params) && OTPGenerator_GetPosition(&o->otpgen) == o->otp_warning_count) {
  67. BPending_Set(&o->handler_job);
  68. }
  69. }
  70. static void encode_work_func (SPProtoEncoder *o)
  71. {
  72. ASSERT(o->in_len >= 0)
  73. ASSERT(o->out_have)
  74. ASSERT(!SPPROTO_HAVE_ENCRYPTION(o->sp_params) || o->have_encryption_key)
  75. ASSERT(o->in_len <= o->input_mtu)
  76. // determine plaintext location
  77. uint8_t *plaintext = (SPPROTO_HAVE_ENCRYPTION(o->sp_params) ? o->buf : o->out);
  78. // plaintext begins with header
  79. uint8_t *header = plaintext;
  80. // plaintext is header + payload
  81. int plaintext_len = SPPROTO_HEADER_LEN(o->sp_params) + o->in_len;
  82. // write OTP
  83. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  84. struct spproto_otpdata *header_otpd = (struct spproto_otpdata *)(header + SPPROTO_HEADER_OTPDATA_OFF(o->sp_params));
  85. header_otpd->seed_id = htol16(o->tw_seed_id);
  86. header_otpd->otp = o->tw_otp;
  87. }
  88. // write hash
  89. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  90. uint8_t *header_hash = header + SPPROTO_HEADER_HASH_OFF(o->sp_params);
  91. // zero hash field
  92. memset(header_hash, 0, o->hash_size);
  93. // calculate hash
  94. uint8_t hash[o->hash_size];
  95. BHash_calculate(o->sp_params.hash_mode, plaintext, plaintext_len, hash);
  96. // set hash field
  97. memcpy(header_hash, hash, o->hash_size);
  98. }
  99. int out_len;
  100. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  101. // encrypting pad(header + payload)
  102. int cyphertext_len = balign_up((plaintext_len + 1), o->enc_block_size);
  103. // write padding
  104. plaintext[plaintext_len] = 1;
  105. for (int i = plaintext_len + 1; i < cyphertext_len; i++) {
  106. plaintext[i] = 0;
  107. }
  108. // generate IV
  109. BRandom_randomize(o->out, o->enc_block_size);
  110. // copy IV because BEncryption_Encrypt changes the IV
  111. uint8_t iv[o->enc_block_size];
  112. memcpy(iv, o->out, o->enc_block_size);
  113. // encrypt
  114. BEncryption_Encrypt(&o->encryptor, plaintext, o->out + o->enc_block_size, cyphertext_len, iv);
  115. out_len = o->enc_block_size + cyphertext_len;
  116. } else {
  117. out_len = plaintext_len;
  118. }
  119. // remember length
  120. o->tw_out_len = out_len;
  121. }
  122. static void encode_work_handler (SPProtoEncoder *o)
  123. {
  124. ASSERT(o->in_len >= 0)
  125. ASSERT(o->out_have)
  126. ASSERT(o->tw_have)
  127. // free work
  128. BThreadWork_Free(&o->tw);
  129. o->tw_have = 0;
  130. // finish packet
  131. o->in_len = -1;
  132. o->out_have = 0;
  133. PacketRecvInterface_Done(&o->output, o->tw_out_len);
  134. }
  135. static void maybe_encode (SPProtoEncoder *o)
  136. {
  137. if (o->in_len >= 0 && o->out_have && !o->tw_have && can_encode(o)) {
  138. encode_packet(o);
  139. }
  140. }
  141. static void output_handler_recv (SPProtoEncoder *o, uint8_t *data)
  142. {
  143. ASSERT(o->in_len == -1)
  144. ASSERT(!o->out_have)
  145. ASSERT(!o->tw_have)
  146. DebugObject_Access(&o->d_obj);
  147. // remember output packet
  148. o->out_have = 1;
  149. o->out = data;
  150. // determine plaintext location
  151. uint8_t *plaintext = (SPPROTO_HAVE_ENCRYPTION(o->sp_params) ? o->buf : o->out);
  152. // schedule receive
  153. PacketRecvInterface_Receiver_Recv(o->input, plaintext + SPPROTO_HEADER_LEN(o->sp_params));
  154. }
  155. static void input_handler_done (SPProtoEncoder *o, int data_len)
  156. {
  157. ASSERT(data_len >= 0)
  158. ASSERT(data_len <= o->input_mtu)
  159. ASSERT(o->in_len == -1)
  160. ASSERT(o->out_have)
  161. ASSERT(!o->tw_have)
  162. DebugObject_Access(&o->d_obj);
  163. // remember input packet
  164. o->in_len = data_len;
  165. // encode if possible
  166. if (can_encode(o)) {
  167. encode_packet(o);
  168. }
  169. }
  170. static void handler_job_hander (SPProtoEncoder *o)
  171. {
  172. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  173. DebugObject_Access(&o->d_obj);
  174. o->handler(o->user);
  175. return;
  176. }
  177. static void otpgenerator_handler (SPProtoEncoder *o)
  178. {
  179. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  180. DebugObject_Access(&o->d_obj);
  181. // remember seed ID
  182. o->otpgen_seed_id = o->otpgen_pending_seed_id;
  183. // possibly continue I/O
  184. maybe_encode(o);
  185. }
  186. static void maybe_stop_work (SPProtoEncoder *o)
  187. {
  188. // stop existing work
  189. if (o->tw_have) {
  190. BThreadWork_Free(&o->tw);
  191. o->tw_have = 0;
  192. }
  193. }
  194. int SPProtoEncoder_Init (SPProtoEncoder *o, PacketRecvInterface *input, struct spproto_security_params sp_params, int otp_warning_count, SPProtoEncoder_handler handler, void *user, BPendingGroup *pg, BThreadWorkDispatcher *twd)
  195. {
  196. spproto_assert_security_params(sp_params);
  197. ASSERT(spproto_carrier_mtu_for_payload_mtu(sp_params, PacketRecvInterface_GetMTU(input)) >= 0)
  198. if (SPPROTO_HAVE_OTP(sp_params)) {
  199. ASSERT(otp_warning_count > 0)
  200. ASSERT(otp_warning_count <= sp_params.otp_num)
  201. ASSERT(handler)
  202. }
  203. // init arguments
  204. o->input = input;
  205. o->sp_params = sp_params;
  206. o->otp_warning_count = otp_warning_count;
  207. o->handler = handler;
  208. o->user = user;
  209. o->twd = twd;
  210. // calculate hash size
  211. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  212. o->hash_size = BHash_size(o->sp_params.hash_mode);
  213. }
  214. // calculate encryption block and key sizes
  215. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  216. o->enc_block_size = BEncryption_cipher_block_size(o->sp_params.encryption_mode);
  217. o->enc_key_size = BEncryption_cipher_key_size(o->sp_params.encryption_mode);
  218. }
  219. // init otp generator
  220. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  221. if (!OTPGenerator_Init(&o->otpgen, o->sp_params.otp_num, o->sp_params.otp_mode, o->twd, (OTPGenerator_handler)otpgenerator_handler, o)) {
  222. goto fail0;
  223. }
  224. }
  225. // have no encryption key
  226. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  227. o->have_encryption_key = 0;
  228. }
  229. // remember input MTU
  230. o->input_mtu = PacketRecvInterface_GetMTU(o->input);
  231. // calculate output MTU
  232. o->output_mtu = spproto_carrier_mtu_for_payload_mtu(o->sp_params, o->input_mtu);
  233. // init input
  234. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  235. // have no input in buffer
  236. o->in_len = -1;
  237. // init output
  238. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  239. // have no output available
  240. o->out_have = 0;
  241. // allocate plaintext buffer
  242. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  243. int buf_size = balign_up((SPPROTO_HEADER_LEN(o->sp_params) + o->input_mtu + 1), o->enc_block_size);
  244. if (!(o->buf = malloc(buf_size))) {
  245. goto fail1;
  246. }
  247. }
  248. // init handler job
  249. BPending_Init(&o->handler_job, pg, (BPending_handler)handler_job_hander, o);
  250. // have no work
  251. o->tw_have = 0;
  252. DebugObject_Init(&o->d_obj);
  253. return 1;
  254. fail1:
  255. PacketRecvInterface_Free(&o->output);
  256. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  257. OTPGenerator_Free(&o->otpgen);
  258. }
  259. fail0:
  260. return 0;
  261. }
  262. void SPProtoEncoder_Free (SPProtoEncoder *o)
  263. {
  264. DebugObject_Free(&o->d_obj);
  265. // free work
  266. if (o->tw_have) {
  267. BThreadWork_Free(&o->tw);
  268. }
  269. // free handler job
  270. BPending_Free(&o->handler_job);
  271. // free plaintext buffer
  272. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  273. free(o->buf);
  274. }
  275. // free output
  276. PacketRecvInterface_Free(&o->output);
  277. // free encryptor
  278. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params) && o->have_encryption_key) {
  279. BEncryption_Free(&o->encryptor);
  280. }
  281. // free otp generator
  282. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  283. OTPGenerator_Free(&o->otpgen);
  284. }
  285. }
  286. PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o)
  287. {
  288. DebugObject_Access(&o->d_obj);
  289. return &o->output;
  290. }
  291. void SPProtoEncoder_SetEncryptionKey (SPProtoEncoder *o, uint8_t *encryption_key)
  292. {
  293. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  294. DebugObject_Access(&o->d_obj);
  295. // stop existing work
  296. maybe_stop_work(o);
  297. // free encryptor
  298. if (o->have_encryption_key) {
  299. BEncryption_Free(&o->encryptor);
  300. }
  301. // init encryptor
  302. BEncryption_Init(&o->encryptor, BENCRYPTION_MODE_ENCRYPT, o->sp_params.encryption_mode, encryption_key);
  303. // have encryption key
  304. o->have_encryption_key = 1;
  305. // possibly continue I/O
  306. maybe_encode(o);
  307. }
  308. void SPProtoEncoder_RemoveEncryptionKey (SPProtoEncoder *o)
  309. {
  310. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  311. DebugObject_Access(&o->d_obj);
  312. // stop existing work
  313. maybe_stop_work(o);
  314. if (o->have_encryption_key) {
  315. // free encryptor
  316. BEncryption_Free(&o->encryptor);
  317. // have no encryption key
  318. o->have_encryption_key = 0;
  319. }
  320. }
  321. void SPProtoEncoder_SetOTPSeed (SPProtoEncoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv)
  322. {
  323. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  324. DebugObject_Access(&o->d_obj);
  325. // give seed to OTP generator
  326. OTPGenerator_SetSeed(&o->otpgen, key, iv);
  327. // remember seed ID
  328. o->otpgen_pending_seed_id = seed_id;
  329. }
  330. void SPProtoEncoder_RemoveOTPSeed (SPProtoEncoder *o)
  331. {
  332. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  333. DebugObject_Access(&o->d_obj);
  334. // reset OTP generator
  335. OTPGenerator_Reset(&o->otpgen);
  336. }