SPProtoEncoder.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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/balign.h>
  25. #include <misc/offset.h>
  26. #include <misc/byteorder.h>
  27. #include <security/BRandom.h>
  28. #include <security/BHash.h>
  29. #include "SPProtoEncoder.h"
  30. static int can_encode (SPProtoEncoder *o);
  31. static void encode_packet (SPProtoEncoder *o);
  32. static void encode_work_func (SPProtoEncoder *o);
  33. static void encode_work_handler (SPProtoEncoder *o);
  34. static void maybe_encode (SPProtoEncoder *o);
  35. static void output_handler_recv (SPProtoEncoder *o, uint8_t *data);
  36. static void input_handler_done (SPProtoEncoder *o, int data_len);
  37. static void handler_job_hander (SPProtoEncoder *o);
  38. static void otpgenerator_handler (SPProtoEncoder *o);
  39. static void maybe_stop_work (SPProtoEncoder *o);
  40. static int can_encode (SPProtoEncoder *o)
  41. {
  42. ASSERT(o->in_len >= 0)
  43. ASSERT(o->out_have)
  44. ASSERT(!o->tw_have)
  45. return (
  46. (!SPPROTO_HAVE_OTP(o->sp_params) || OTPGenerator_GetPosition(&o->otpgen) < o->sp_params.otp_num) &&
  47. (!SPPROTO_HAVE_ENCRYPTION(o->sp_params) || o->have_encryption_key)
  48. );
  49. }
  50. static void encode_packet (SPProtoEncoder *o)
  51. {
  52. ASSERT(o->in_len >= 0)
  53. ASSERT(o->out_have)
  54. ASSERT(!o->tw_have)
  55. ASSERT(can_encode(o))
  56. // generate OTP, remember seed ID
  57. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  58. o->tw_seed_id = o->otpgen_seed_id;
  59. o->tw_otp = OTPGenerator_GetOTP(&o->otpgen);
  60. }
  61. // start work
  62. BThreadWork_Init(&o->tw, o->twd, (BThreadWork_handler_done)encode_work_handler, o, (BThreadWork_work_func)encode_work_func, o);
  63. o->tw_have = 1;
  64. // schedule OTP warning handler
  65. if (SPPROTO_HAVE_OTP(o->sp_params) && OTPGenerator_GetPosition(&o->otpgen) == o->otp_warning_count) {
  66. BPending_Set(&o->handler_job);
  67. }
  68. }
  69. static void encode_work_func (SPProtoEncoder *o)
  70. {
  71. ASSERT(o->in_len >= 0)
  72. ASSERT(o->out_have)
  73. ASSERT(!SPPROTO_HAVE_ENCRYPTION(o->sp_params) || o->have_encryption_key)
  74. ASSERT(o->in_len <= o->input_mtu)
  75. // determine plaintext location
  76. uint8_t *plaintext = (SPPROTO_HAVE_ENCRYPTION(o->sp_params) ? o->buf : o->out);
  77. // plaintext begins with header
  78. uint8_t *header = plaintext;
  79. // plaintext is header + payload
  80. int plaintext_len = SPPROTO_HEADER_LEN(o->sp_params) + o->in_len;
  81. // write OTP
  82. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  83. struct spproto_otpdata *header_otpd = (struct spproto_otpdata *)(header + SPPROTO_HEADER_OTPDATA_OFF(o->sp_params));
  84. header_otpd->seed_id = htol16(o->tw_seed_id);
  85. header_otpd->otp = o->tw_otp;
  86. }
  87. // write hash
  88. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  89. uint8_t *header_hash = header + SPPROTO_HEADER_HASH_OFF(o->sp_params);
  90. // zero hash field
  91. memset(header_hash, 0, o->hash_size);
  92. // calculate hash
  93. uint8_t hash[BHASH_MAX_SIZE];
  94. BHash_calculate(o->sp_params.hash_mode, plaintext, plaintext_len, hash);
  95. // set hash field
  96. memcpy(header_hash, hash, o->hash_size);
  97. }
  98. int out_len;
  99. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  100. // encrypting pad(header + payload)
  101. int cyphertext_len = balign_up((plaintext_len + 1), o->enc_block_size);
  102. // write padding
  103. plaintext[plaintext_len] = 1;
  104. for (int i = plaintext_len + 1; i < cyphertext_len; i++) {
  105. plaintext[i] = 0;
  106. }
  107. // generate IV
  108. BRandom_randomize(o->out, o->enc_block_size);
  109. // copy IV because BEncryption_Encrypt changes the IV
  110. uint8_t iv[BENCRYPTION_MAX_BLOCK_SIZE];
  111. memcpy(iv, o->out, o->enc_block_size);
  112. // encrypt
  113. BEncryption_Encrypt(&o->encryptor, plaintext, o->out + o->enc_block_size, cyphertext_len, iv);
  114. out_len = o->enc_block_size + cyphertext_len;
  115. } else {
  116. out_len = plaintext_len;
  117. }
  118. // remember length
  119. o->tw_out_len = out_len;
  120. }
  121. static void encode_work_handler (SPProtoEncoder *o)
  122. {
  123. ASSERT(o->in_len >= 0)
  124. ASSERT(o->out_have)
  125. ASSERT(o->tw_have)
  126. // free work
  127. BThreadWork_Free(&o->tw);
  128. o->tw_have = 0;
  129. // finish packet
  130. o->in_len = -1;
  131. o->out_have = 0;
  132. PacketRecvInterface_Done(&o->output, o->tw_out_len);
  133. }
  134. static void maybe_encode (SPProtoEncoder *o)
  135. {
  136. if (o->in_len >= 0 && o->out_have && !o->tw_have && can_encode(o)) {
  137. encode_packet(o);
  138. }
  139. }
  140. static void output_handler_recv (SPProtoEncoder *o, uint8_t *data)
  141. {
  142. ASSERT(o->in_len == -1)
  143. ASSERT(!o->out_have)
  144. ASSERT(!o->tw_have)
  145. DebugObject_Access(&o->d_obj);
  146. // remember output packet
  147. o->out_have = 1;
  148. o->out = data;
  149. // determine plaintext location
  150. uint8_t *plaintext = (SPPROTO_HAVE_ENCRYPTION(o->sp_params) ? o->buf : o->out);
  151. // schedule receive
  152. PacketRecvInterface_Receiver_Recv(o->input, plaintext + SPPROTO_HEADER_LEN(o->sp_params));
  153. }
  154. static void input_handler_done (SPProtoEncoder *o, int data_len)
  155. {
  156. ASSERT(data_len >= 0)
  157. ASSERT(data_len <= o->input_mtu)
  158. ASSERT(o->in_len == -1)
  159. ASSERT(o->out_have)
  160. ASSERT(!o->tw_have)
  161. DebugObject_Access(&o->d_obj);
  162. // remember input packet
  163. o->in_len = data_len;
  164. // encode if possible
  165. if (can_encode(o)) {
  166. encode_packet(o);
  167. }
  168. }
  169. static void handler_job_hander (SPProtoEncoder *o)
  170. {
  171. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  172. DebugObject_Access(&o->d_obj);
  173. if (o->handler) {
  174. o->handler(o->user);
  175. return;
  176. }
  177. }
  178. static void otpgenerator_handler (SPProtoEncoder *o)
  179. {
  180. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  181. DebugObject_Access(&o->d_obj);
  182. // remember seed ID
  183. o->otpgen_seed_id = o->otpgen_pending_seed_id;
  184. // possibly continue I/O
  185. maybe_encode(o);
  186. }
  187. static void maybe_stop_work (SPProtoEncoder *o)
  188. {
  189. // stop existing work
  190. if (o->tw_have) {
  191. BThreadWork_Free(&o->tw);
  192. o->tw_have = 0;
  193. }
  194. }
  195. int SPProtoEncoder_Init (SPProtoEncoder *o, PacketRecvInterface *input, struct spproto_security_params sp_params, int otp_warning_count, BPendingGroup *pg, BThreadWorkDispatcher *twd)
  196. {
  197. spproto_assert_security_params(sp_params);
  198. ASSERT(spproto_carrier_mtu_for_payload_mtu(sp_params, PacketRecvInterface_GetMTU(input)) >= 0)
  199. if (SPPROTO_HAVE_OTP(sp_params)) {
  200. ASSERT(otp_warning_count > 0)
  201. ASSERT(otp_warning_count <= sp_params.otp_num)
  202. }
  203. // init arguments
  204. o->input = input;
  205. o->sp_params = sp_params;
  206. o->otp_warning_count = otp_warning_count;
  207. o->twd = twd;
  208. // set no handlers
  209. o->handler = NULL;
  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. }
  337. void SPProtoEncoder_SetHandlers (SPProtoEncoder *o, SPProtoEncoder_handler handler, void *user)
  338. {
  339. DebugObject_Access(&o->d_obj);
  340. o->handler = handler;
  341. o->user = user;
  342. }