SPProtoEncoder.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 "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. if (o->handler) {
  175. o->handler(o->user);
  176. return;
  177. }
  178. }
  179. static void otpgenerator_handler (SPProtoEncoder *o)
  180. {
  181. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  182. DebugObject_Access(&o->d_obj);
  183. // remember seed ID
  184. o->otpgen_seed_id = o->otpgen_pending_seed_id;
  185. // possibly continue I/O
  186. maybe_encode(o);
  187. }
  188. static void maybe_stop_work (SPProtoEncoder *o)
  189. {
  190. // stop existing work
  191. if (o->tw_have) {
  192. BThreadWork_Free(&o->tw);
  193. o->tw_have = 0;
  194. }
  195. }
  196. int SPProtoEncoder_Init (SPProtoEncoder *o, PacketRecvInterface *input, struct spproto_security_params sp_params, int otp_warning_count, BPendingGroup *pg, BThreadWorkDispatcher *twd)
  197. {
  198. spproto_assert_security_params(sp_params);
  199. ASSERT(spproto_carrier_mtu_for_payload_mtu(sp_params, PacketRecvInterface_GetMTU(input)) >= 0)
  200. if (SPPROTO_HAVE_OTP(sp_params)) {
  201. ASSERT(otp_warning_count > 0)
  202. ASSERT(otp_warning_count <= sp_params.otp_num)
  203. }
  204. // init arguments
  205. o->input = input;
  206. o->sp_params = sp_params;
  207. o->otp_warning_count = otp_warning_count;
  208. o->twd = twd;
  209. // set no handlers
  210. o->handler = NULL;
  211. // calculate hash size
  212. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  213. o->hash_size = BHash_size(o->sp_params.hash_mode);
  214. }
  215. // calculate encryption block and key sizes
  216. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  217. o->enc_block_size = BEncryption_cipher_block_size(o->sp_params.encryption_mode);
  218. o->enc_key_size = BEncryption_cipher_key_size(o->sp_params.encryption_mode);
  219. }
  220. // init otp generator
  221. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  222. if (!OTPGenerator_Init(&o->otpgen, o->sp_params.otp_num, o->sp_params.otp_mode, o->twd, (OTPGenerator_handler)otpgenerator_handler, o)) {
  223. goto fail0;
  224. }
  225. }
  226. // have no encryption key
  227. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  228. o->have_encryption_key = 0;
  229. }
  230. // remember input MTU
  231. o->input_mtu = PacketRecvInterface_GetMTU(o->input);
  232. // calculate output MTU
  233. o->output_mtu = spproto_carrier_mtu_for_payload_mtu(o->sp_params, o->input_mtu);
  234. // init input
  235. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  236. // have no input in buffer
  237. o->in_len = -1;
  238. // init output
  239. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  240. // have no output available
  241. o->out_have = 0;
  242. // allocate plaintext buffer
  243. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  244. int buf_size = balign_up((SPPROTO_HEADER_LEN(o->sp_params) + o->input_mtu + 1), o->enc_block_size);
  245. if (!(o->buf = malloc(buf_size))) {
  246. goto fail1;
  247. }
  248. }
  249. // init handler job
  250. BPending_Init(&o->handler_job, pg, (BPending_handler)handler_job_hander, o);
  251. // have no work
  252. o->tw_have = 0;
  253. DebugObject_Init(&o->d_obj);
  254. return 1;
  255. fail1:
  256. PacketRecvInterface_Free(&o->output);
  257. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  258. OTPGenerator_Free(&o->otpgen);
  259. }
  260. fail0:
  261. return 0;
  262. }
  263. void SPProtoEncoder_Free (SPProtoEncoder *o)
  264. {
  265. DebugObject_Free(&o->d_obj);
  266. // free work
  267. if (o->tw_have) {
  268. BThreadWork_Free(&o->tw);
  269. }
  270. // free handler job
  271. BPending_Free(&o->handler_job);
  272. // free plaintext buffer
  273. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  274. free(o->buf);
  275. }
  276. // free output
  277. PacketRecvInterface_Free(&o->output);
  278. // free encryptor
  279. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params) && o->have_encryption_key) {
  280. BEncryption_Free(&o->encryptor);
  281. }
  282. // free otp generator
  283. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  284. OTPGenerator_Free(&o->otpgen);
  285. }
  286. }
  287. PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o)
  288. {
  289. DebugObject_Access(&o->d_obj);
  290. return &o->output;
  291. }
  292. void SPProtoEncoder_SetEncryptionKey (SPProtoEncoder *o, uint8_t *encryption_key)
  293. {
  294. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  295. DebugObject_Access(&o->d_obj);
  296. // stop existing work
  297. maybe_stop_work(o);
  298. // free encryptor
  299. if (o->have_encryption_key) {
  300. BEncryption_Free(&o->encryptor);
  301. }
  302. // init encryptor
  303. BEncryption_Init(&o->encryptor, BENCRYPTION_MODE_ENCRYPT, o->sp_params.encryption_mode, encryption_key);
  304. // have encryption key
  305. o->have_encryption_key = 1;
  306. // possibly continue I/O
  307. maybe_encode(o);
  308. }
  309. void SPProtoEncoder_RemoveEncryptionKey (SPProtoEncoder *o)
  310. {
  311. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  312. DebugObject_Access(&o->d_obj);
  313. // stop existing work
  314. maybe_stop_work(o);
  315. if (o->have_encryption_key) {
  316. // free encryptor
  317. BEncryption_Free(&o->encryptor);
  318. // have no encryption key
  319. o->have_encryption_key = 0;
  320. }
  321. }
  322. void SPProtoEncoder_SetOTPSeed (SPProtoEncoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv)
  323. {
  324. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  325. DebugObject_Access(&o->d_obj);
  326. // give seed to OTP generator
  327. OTPGenerator_SetSeed(&o->otpgen, key, iv);
  328. // remember seed ID
  329. o->otpgen_pending_seed_id = seed_id;
  330. }
  331. void SPProtoEncoder_RemoveOTPSeed (SPProtoEncoder *o)
  332. {
  333. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  334. DebugObject_Access(&o->d_obj);
  335. // reset OTP generator
  336. OTPGenerator_Reset(&o->otpgen);
  337. }
  338. void SPProtoEncoder_SetHandlers (SPProtoEncoder *o, SPProtoEncoder_handler handler, void *user)
  339. {
  340. DebugObject_Access(&o->d_obj);
  341. o->handler = handler;
  342. o->user = user;
  343. }