SPProtoEncoder.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 <security/BRandom.h>
  28. #include <security/BHash.h>
  29. #include <flow/SPProtoEncoder.h>
  30. static int can_encode (SPProtoEncoder *o)
  31. {
  32. ASSERT(o->in_len >= 0)
  33. ASSERT(o->out_have)
  34. return (
  35. (!SPPROTO_HAVE_OTP(o->group->sp_params) || OTPGenerator_GetPosition(&o->group->otpgen) < o->group->sp_params.otp_num) &&
  36. (!SPPROTO_HAVE_ENCRYPTION(o->group->sp_params) || o->group->have_encryption_key)
  37. );
  38. }
  39. static int encode_packet (SPProtoEncoder *o)
  40. {
  41. ASSERT(o->in_len >= 0)
  42. ASSERT(o->in_len <= o->input_mtu)
  43. ASSERT(o->out_have)
  44. ASSERT(can_encode(o))
  45. // plaintext is either output packet or our buffer
  46. uint8_t *plaintext = (!SPPROTO_HAVE_ENCRYPTION(o->group->sp_params) ? o->out : o->buf);
  47. // plaintext begins with header
  48. uint8_t *header = plaintext;
  49. // plaintext is header + payload
  50. int plaintext_len = SPPROTO_HEADER_LEN(o->group->sp_params) + o->in_len;
  51. // write OTP
  52. if (SPPROTO_HAVE_OTP(o->group->sp_params)) {
  53. struct spproto_otpdata *header_otpd = (struct spproto_otpdata *)(header + SPPROTO_HEADER_OTPDATA_OFF(o->group->sp_params));
  54. header_otpd->seed_id = o->group->otpgen_seed_id;
  55. header_otpd->otp = OTPGenerator_GetOTP(&o->group->otpgen);
  56. }
  57. // write hash
  58. if (SPPROTO_HAVE_HASH(o->group->sp_params)) {
  59. uint8_t *header_hash = header + SPPROTO_HEADER_HASH_OFF(o->group->sp_params);
  60. // zero hash field
  61. memset(header_hash, 0, o->group->hash_size);
  62. // calculate hash
  63. uint8_t hash[o->group->hash_size];
  64. BHash_calculate(o->group->sp_params.hash_mode, plaintext, plaintext_len, hash);
  65. // set hash field
  66. memcpy(header_hash, hash, o->group->hash_size);
  67. }
  68. int out_len;
  69. if (SPPROTO_HAVE_ENCRYPTION(o->group->sp_params)) {
  70. // encrypting pad(header + payload)
  71. int cyphertext_len = BALIGN_UP_N((plaintext_len + 1), o->group->enc_block_size);
  72. // write padding
  73. plaintext[plaintext_len] = 1;
  74. for (int i = plaintext_len + 1; i < cyphertext_len; i++) {
  75. plaintext[i] = 0;
  76. }
  77. // generate IV
  78. BRandom_randomize(o->out, o->group->enc_block_size);
  79. // copy IV because BEncryption_Encrypt changes the IV
  80. uint8_t iv[o->group->enc_block_size];
  81. memcpy(iv, o->out, o->group->enc_block_size);
  82. // encrypt
  83. BEncryption_Encrypt(&o->group->encryptor, plaintext, o->out + o->group->enc_block_size, cyphertext_len, iv);
  84. out_len = o->group->enc_block_size + cyphertext_len;
  85. } else {
  86. out_len = plaintext_len;
  87. }
  88. o->in_len = -1;
  89. o->out_have = 0;
  90. return out_len;
  91. }
  92. static int output_handler_recv (SPProtoEncoder *o, uint8_t *data, int *data_len)
  93. {
  94. ASSERT(o->in_len == -1)
  95. ASSERT(!o->out_have)
  96. // remember output packet
  97. o->out_have = 1;
  98. o->out = data;
  99. // determine plaintext location
  100. uint8_t *plaintext = (!SPPROTO_HAVE_ENCRYPTION(o->group->sp_params) ? o->out : o->buf);
  101. // try to receive input packet
  102. int in_len;
  103. DEAD_ENTER(o->dead)
  104. int res = PacketRecvInterface_Receiver_Recv(o->input, plaintext + SPPROTO_HEADER_LEN(o->group->sp_params), &in_len);
  105. if (DEAD_LEAVE(o->dead)) {
  106. return -1;
  107. }
  108. ASSERT(res == 0 || res == 1)
  109. if (!res) {
  110. return 0;
  111. }
  112. ASSERT(in_len >= 0 && in_len <= o->input_mtu)
  113. // remember input packet
  114. o->in_len = in_len;
  115. // check if we can encode
  116. if (!can_encode(o)) {
  117. return 0;
  118. }
  119. // encode
  120. *data_len = encode_packet(o);
  121. return 1;
  122. }
  123. static void input_handler_done (SPProtoEncoder *o, int in_len)
  124. {
  125. ASSERT(o->in_len == -1)
  126. ASSERT(o->out_have)
  127. ASSERT(in_len >= 0 && in_len <= o->input_mtu)
  128. // remember input packet
  129. o->in_len = in_len;
  130. // check if we can encode
  131. if (!can_encode(o)) {
  132. return;
  133. }
  134. // encode
  135. int out_len = encode_packet(o);
  136. // inform output
  137. PacketRecvInterface_Done(&o->output, out_len);
  138. return;
  139. }
  140. static void job_handler (SPProtoEncoder *o)
  141. {
  142. if (o->in_len >= 0 && o->out_have && can_encode(o)) {
  143. // encode
  144. int out_len = encode_packet(o);
  145. // inform output
  146. PacketRecvInterface_Done(&o->output, out_len);
  147. return;
  148. }
  149. }
  150. static void schedule_jobs (SPProtoEncoderGroup *o)
  151. {
  152. LinkedList2Iterator it;
  153. LinkedList2Iterator_InitForward(&it, &o->encoders_list);
  154. LinkedList2Node *node;
  155. while (node = LinkedList2Iterator_Next(&it)) {
  156. SPProtoEncoder *enc = UPPER_OBJECT(node, SPProtoEncoder, group_list_node);
  157. BPending_Set(&enc->continue_job);
  158. }
  159. }
  160. int SPProtoEncoderGroup_Init (SPProtoEncoderGroup *o, struct spproto_security_params sp_params)
  161. {
  162. ASSERT(spproto_validate_security_params(sp_params))
  163. // init parameters
  164. o->sp_params = sp_params;
  165. // calculate hash size
  166. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  167. o->hash_size = BHash_size(o->sp_params.hash_mode);
  168. }
  169. // calculate encryption block and key sizes
  170. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  171. o->enc_block_size = BEncryption_cipher_block_size(o->sp_params.encryption_mode);
  172. o->enc_key_size = BEncryption_cipher_key_size(o->sp_params.encryption_mode);
  173. }
  174. // init otp generator
  175. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  176. if (!OTPGenerator_Init(&o->otpgen, o->sp_params.otp_num, o->sp_params.otp_mode)) {
  177. goto fail1;
  178. }
  179. }
  180. // have no encryption key
  181. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  182. o->have_encryption_key = 0;
  183. }
  184. // init encoders list
  185. LinkedList2_Init(&o->encoders_list);
  186. // init debug object
  187. DebugObject_Init(&o->d_obj);
  188. return 1;
  189. fail1:
  190. return 0;
  191. }
  192. void SPProtoEncoderGroup_Free (SPProtoEncoderGroup *o)
  193. {
  194. ASSERT(LinkedList2_IsEmpty(&o->encoders_list))
  195. DebugObject_Free(&o->d_obj);
  196. // free encryptor
  197. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params) && o->have_encryption_key) {
  198. BEncryption_Free(&o->encryptor);
  199. }
  200. // free otp generator
  201. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  202. OTPGenerator_Free(&o->otpgen);
  203. }
  204. }
  205. void SPProtoEncoderGroup_SetEncryptionKey (SPProtoEncoderGroup *o, uint8_t *encryption_key)
  206. {
  207. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  208. // free encryptor
  209. if (o->have_encryption_key) {
  210. BEncryption_Free(&o->encryptor);
  211. }
  212. // init encryptor
  213. BEncryption_Init(&o->encryptor, BENCRYPTION_MODE_ENCRYPT, o->sp_params.encryption_mode, encryption_key);
  214. // have encryption key
  215. o->have_encryption_key = 1;
  216. // set jobs
  217. schedule_jobs(o);
  218. }
  219. void SPProtoEncoderGroup_RemoveEncryptionKey (SPProtoEncoderGroup *o)
  220. {
  221. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  222. if (o->have_encryption_key) {
  223. // free encryptor
  224. BEncryption_Free(&o->encryptor);
  225. // have no encryption key
  226. o->have_encryption_key = 0;
  227. }
  228. }
  229. void SPProtoEncoderGroup_SetOTPSeed (SPProtoEncoderGroup *o, uint16_t seed_id, uint8_t *key, uint8_t *iv)
  230. {
  231. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  232. // give seed to OTP generator
  233. OTPGenerator_SetSeed(&o->otpgen, key, iv);
  234. // remember seed ID
  235. o->otpgen_seed_id = seed_id;
  236. // set jobs
  237. schedule_jobs(o);
  238. }
  239. void SPProtoEncoderGroup_RemoveOTPSeed (SPProtoEncoderGroup *o)
  240. {
  241. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  242. // reset OTP generator
  243. OTPGenerator_Reset(&o->otpgen);
  244. }
  245. int SPProtoEncoderGroup_GetOTPPosition (SPProtoEncoderGroup *o)
  246. {
  247. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  248. return OTPGenerator_GetPosition(&o->otpgen);
  249. }
  250. int SPProtoEncoder_Init (SPProtoEncoder *o, SPProtoEncoderGroup *group, PacketRecvInterface *input, BPendingGroup *pg)
  251. {
  252. // init parameters
  253. o->group = group;
  254. o->input = input;
  255. // init dead var
  256. DEAD_INIT(o->dead);
  257. // remember input MTU
  258. o->input_mtu = PacketRecvInterface_GetMTU(o->input);
  259. // calculate output MTU
  260. o->output_mtu = spproto_carrier_mtu_for_payload_mtu(o->group->sp_params, o->input_mtu);
  261. // init input
  262. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  263. // have no input in buffer
  264. o->in_len = -1;
  265. // init output
  266. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o);
  267. // have no output available
  268. o->out_have = 0;
  269. // allocate plaintext buffer
  270. if (SPPROTO_HAVE_ENCRYPTION(o->group->sp_params)) {
  271. int buf_size = BALIGN_UP_N((SPPROTO_HEADER_LEN(o->group->sp_params) + o->input_mtu + 1), o->group->enc_block_size);
  272. if (!(o->buf = malloc(buf_size))) {
  273. goto fail1;
  274. }
  275. }
  276. // insert to group list
  277. LinkedList2_Append(&o->group->encoders_list, &o->group_list_node);
  278. // init pending job
  279. BPending_Init(&o->continue_job, pg, (BPending_handler)job_handler, o);
  280. // init debug object
  281. DebugObject_Init(&o->d_obj);
  282. return 1;
  283. fail1:
  284. PacketRecvInterface_Free(&o->output);
  285. return 0;
  286. }
  287. void SPProtoEncoder_Free (SPProtoEncoder *o)
  288. {
  289. // free debug object
  290. DebugObject_Free(&o->d_obj);
  291. // free pending job
  292. BPending_Free(&o->continue_job);
  293. // remove from group list
  294. LinkedList2_Remove(&o->group->encoders_list, &o->group_list_node);
  295. // free plaintext buffer
  296. if (SPPROTO_HAVE_ENCRYPTION(o->group->sp_params)) {
  297. free(o->buf);
  298. }
  299. // free output
  300. PacketRecvInterface_Free(&o->output);
  301. // kill dead var
  302. DEAD_KILL(o->dead);
  303. }
  304. PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o)
  305. {
  306. return &o->output;
  307. }