SPProtoEncoder.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 void 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((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. // schedule OTP warning handler
  94. if (SPPROTO_HAVE_OTP(o->sp_params) && OTPGenerator_GetPosition(&o->otpgen) == o->otp_warning_count) {
  95. BPending_Set(&o->handler_job);
  96. }
  97. }
  98. static void maybe_encode (SPProtoEncoder *o)
  99. {
  100. if (o->in_len >= 0 && o->out_have && can_encode(o)) {
  101. encode_packet(o);
  102. }
  103. }
  104. static void output_handler_recv (SPProtoEncoder *o, uint8_t *data)
  105. {
  106. ASSERT(o->in_len == -1)
  107. ASSERT(!o->out_have)
  108. DebugObject_Access(&o->d_obj);
  109. // remember output packet
  110. o->out_have = 1;
  111. o->out = data;
  112. // determine plaintext location
  113. uint8_t *plaintext = (SPPROTO_HAVE_ENCRYPTION(o->sp_params) ? o->buf : o->out);
  114. // schedule receive
  115. PacketRecvInterface_Receiver_Recv(o->input, plaintext + SPPROTO_HEADER_LEN(o->sp_params));
  116. }
  117. static void input_handler_done (SPProtoEncoder *o, int data_len)
  118. {
  119. ASSERT(data_len >= 0)
  120. ASSERT(data_len <= o->input_mtu)
  121. ASSERT(o->in_len == -1)
  122. ASSERT(o->out_have)
  123. DebugObject_Access(&o->d_obj);
  124. // remember input packet
  125. o->in_len = data_len;
  126. // encode if possible
  127. if (can_encode(o)) {
  128. encode_packet(o);
  129. }
  130. }
  131. static void handler_job_hander (SPProtoEncoder *o)
  132. {
  133. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  134. DebugObject_Access(&o->d_obj);
  135. o->handler(o->user);
  136. return;
  137. }
  138. int SPProtoEncoder_Init (SPProtoEncoder *o, PacketRecvInterface *input, struct spproto_security_params sp_params, int otp_warning_count, SPProtoEncoder_handler handler, void *user, BPendingGroup *pg)
  139. {
  140. spproto_assert_security_params(sp_params);
  141. ASSERT(spproto_carrier_mtu_for_payload_mtu(sp_params, PacketRecvInterface_GetMTU(input)) >= 0)
  142. if (SPPROTO_HAVE_OTP(sp_params)) {
  143. ASSERT(otp_warning_count > 0)
  144. ASSERT(otp_warning_count <= sp_params.otp_num)
  145. ASSERT(handler)
  146. }
  147. // init arguments
  148. o->input = input;
  149. o->sp_params = sp_params;
  150. o->otp_warning_count = otp_warning_count;
  151. o->handler = handler;
  152. o->user = user;
  153. // calculate hash size
  154. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  155. o->hash_size = BHash_size(o->sp_params.hash_mode);
  156. }
  157. // calculate encryption block and key sizes
  158. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  159. o->enc_block_size = BEncryption_cipher_block_size(o->sp_params.encryption_mode);
  160. o->enc_key_size = BEncryption_cipher_key_size(o->sp_params.encryption_mode);
  161. }
  162. // init otp generator
  163. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  164. if (!OTPGenerator_Init(&o->otpgen, o->sp_params.otp_num, o->sp_params.otp_mode)) {
  165. goto fail0;
  166. }
  167. }
  168. // have no encryption key
  169. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  170. o->have_encryption_key = 0;
  171. }
  172. // remember input MTU
  173. o->input_mtu = PacketRecvInterface_GetMTU(o->input);
  174. // calculate output MTU
  175. o->output_mtu = spproto_carrier_mtu_for_payload_mtu(o->sp_params, o->input_mtu);
  176. // init input
  177. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  178. // have no input in buffer
  179. o->in_len = -1;
  180. // init output
  181. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  182. // have no output available
  183. o->out_have = 0;
  184. // allocate plaintext buffer
  185. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  186. int buf_size = balign_up((SPPROTO_HEADER_LEN(o->sp_params) + o->input_mtu + 1), o->enc_block_size);
  187. if (!(o->buf = malloc(buf_size))) {
  188. goto fail1;
  189. }
  190. }
  191. // init handler job
  192. BPending_Init(&o->handler_job, pg, (BPending_handler)handler_job_hander, o);
  193. DebugObject_Init(&o->d_obj);
  194. return 1;
  195. fail1:
  196. PacketRecvInterface_Free(&o->output);
  197. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  198. OTPGenerator_Free(&o->otpgen);
  199. }
  200. fail0:
  201. return 0;
  202. }
  203. void SPProtoEncoder_Free (SPProtoEncoder *o)
  204. {
  205. DebugObject_Free(&o->d_obj);
  206. // free handler job
  207. BPending_Free(&o->handler_job);
  208. // free plaintext buffer
  209. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  210. free(o->buf);
  211. }
  212. // free output
  213. PacketRecvInterface_Free(&o->output);
  214. // free encryptor
  215. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params) && o->have_encryption_key) {
  216. BEncryption_Free(&o->encryptor);
  217. }
  218. // free otp generator
  219. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  220. OTPGenerator_Free(&o->otpgen);
  221. }
  222. }
  223. PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o)
  224. {
  225. DebugObject_Access(&o->d_obj);
  226. return &o->output;
  227. }
  228. void SPProtoEncoder_SetEncryptionKey (SPProtoEncoder *o, uint8_t *encryption_key)
  229. {
  230. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  231. DebugObject_Access(&o->d_obj);
  232. // free encryptor
  233. if (o->have_encryption_key) {
  234. BEncryption_Free(&o->encryptor);
  235. }
  236. // init encryptor
  237. BEncryption_Init(&o->encryptor, BENCRYPTION_MODE_ENCRYPT, o->sp_params.encryption_mode, encryption_key);
  238. // have encryption key
  239. o->have_encryption_key = 1;
  240. // possibly continue I/O
  241. maybe_encode(o);
  242. }
  243. void SPProtoEncoder_RemoveEncryptionKey (SPProtoEncoder *o)
  244. {
  245. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  246. DebugObject_Access(&o->d_obj);
  247. if (o->have_encryption_key) {
  248. // free encryptor
  249. BEncryption_Free(&o->encryptor);
  250. // have no encryption key
  251. o->have_encryption_key = 0;
  252. }
  253. }
  254. void SPProtoEncoder_SetOTPSeed (SPProtoEncoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv)
  255. {
  256. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  257. DebugObject_Access(&o->d_obj);
  258. // give seed to OTP generator
  259. OTPGenerator_SetSeed(&o->otpgen, key, iv);
  260. // remember seed ID
  261. o->otpgen_seed_id = seed_id;
  262. // possibly continue I/O
  263. maybe_encode(o);
  264. }
  265. void SPProtoEncoder_RemoveOTPSeed (SPProtoEncoder *o)
  266. {
  267. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  268. DebugObject_Access(&o->d_obj);
  269. // reset OTP generator
  270. OTPGenerator_Reset(&o->otpgen);
  271. }