SPProtoEncoder.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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->sp_params) || OTPGenerator_GetPosition(&o->otpgen) < o->sp_params.otp_num) &&
  36. (!SPPROTO_HAVE_ENCRYPTION(o->sp_params) || o->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->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->sp_params) + o->in_len;
  51. // write OTP
  52. if (SPPROTO_HAVE_OTP(o->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->otpgen_seed_id;
  55. header_otpd->otp = OTPGenerator_GetOTP(&o->otpgen);
  56. }
  57. // write hash
  58. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  59. uint8_t *header_hash = header + SPPROTO_HEADER_HASH_OFF(o->sp_params);
  60. // zero hash field
  61. memset(header_hash, 0, o->hash_size);
  62. // calculate hash
  63. uint8_t hash[o->hash_size];
  64. BHash_calculate(o->sp_params.hash_mode, plaintext, plaintext_len, hash);
  65. // set hash field
  66. memcpy(header_hash, hash, o->hash_size);
  67. }
  68. int out_len;
  69. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  70. // encrypting pad(header + payload)
  71. int cyphertext_len = BALIGN_UP_N((plaintext_len + 1), o->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->enc_block_size);
  79. // copy IV because BEncryption_Encrypt changes the IV
  80. uint8_t iv[o->enc_block_size];
  81. memcpy(iv, o->out, o->enc_block_size);
  82. // encrypt
  83. BEncryption_Encrypt(&o->encryptor, plaintext, o->out + o->enc_block_size, cyphertext_len, iv);
  84. out_len = o->enc_block_size + cyphertext_len;
  85. } else {
  86. out_len = plaintext_len;
  87. }
  88. return out_len;
  89. }
  90. static void do_encode (SPProtoEncoder *o)
  91. {
  92. ASSERT(o->in_len >= 0)
  93. ASSERT(o->in_len <= o->input_mtu)
  94. ASSERT(o->out_have)
  95. ASSERT(can_encode(o))
  96. // encode
  97. int out_len = encode_packet(o);
  98. // finish packet
  99. PacketRecvInterface_Done(&o->output, out_len);
  100. o->in_len = -1;
  101. o->out_have = 0;
  102. }
  103. static void maybe_encode (SPProtoEncoder *o)
  104. {
  105. if (o->in_len >= 0 && o->out_have && can_encode(o)) {
  106. do_encode(o);
  107. }
  108. }
  109. static void output_handler_recv (SPProtoEncoder *o, uint8_t *data)
  110. {
  111. ASSERT(o->in_len == -1)
  112. ASSERT(!o->out_have)
  113. DebugObject_Access(&o->d_obj);
  114. // remember output packet
  115. o->out_have = 1;
  116. o->out = data;
  117. // determine plaintext location
  118. uint8_t *plaintext = (!SPPROTO_HAVE_ENCRYPTION(o->sp_params) ? o->out : o->buf);
  119. // schedule receive
  120. PacketRecvInterface_Receiver_Recv(o->input, plaintext + SPPROTO_HEADER_LEN(o->sp_params));
  121. }
  122. static void input_handler_done (SPProtoEncoder *o, int in_len)
  123. {
  124. ASSERT(in_len >= 0)
  125. ASSERT(in_len <= o->input_mtu)
  126. ASSERT(o->in_len == -1)
  127. ASSERT(o->out_have)
  128. DebugObject_Access(&o->d_obj);
  129. // remember input packet
  130. o->in_len = in_len;
  131. // encode if possible
  132. if (can_encode(o)) {
  133. do_encode(o);
  134. }
  135. }
  136. int SPProtoEncoder_Init (SPProtoEncoder *o, struct spproto_security_params sp_params, PacketRecvInterface *input, BPendingGroup *pg)
  137. {
  138. ASSERT(spproto_validate_security_params(sp_params))
  139. // init parameters
  140. o->sp_params = sp_params;
  141. o->input = input;
  142. // calculate hash size
  143. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  144. o->hash_size = BHash_size(o->sp_params.hash_mode);
  145. }
  146. // calculate encryption block and key sizes
  147. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  148. o->enc_block_size = BEncryption_cipher_block_size(o->sp_params.encryption_mode);
  149. o->enc_key_size = BEncryption_cipher_key_size(o->sp_params.encryption_mode);
  150. }
  151. // init otp generator
  152. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  153. if (!OTPGenerator_Init(&o->otpgen, o->sp_params.otp_num, o->sp_params.otp_mode)) {
  154. goto fail0;
  155. }
  156. }
  157. // have no encryption key
  158. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  159. o->have_encryption_key = 0;
  160. }
  161. // remember input MTU
  162. o->input_mtu = PacketRecvInterface_GetMTU(o->input);
  163. // calculate output MTU
  164. o->output_mtu = spproto_carrier_mtu_for_payload_mtu(o->sp_params, o->input_mtu);
  165. // init input
  166. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  167. // have no input in buffer
  168. o->in_len = -1;
  169. // init output
  170. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  171. // have no output available
  172. o->out_have = 0;
  173. // allocate plaintext buffer
  174. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  175. int buf_size = BALIGN_UP_N((SPPROTO_HEADER_LEN(o->sp_params) + o->input_mtu + 1), o->enc_block_size);
  176. if (!(o->buf = malloc(buf_size))) {
  177. goto fail1;
  178. }
  179. }
  180. DebugObject_Init(&o->d_obj);
  181. return 1;
  182. fail1:
  183. PacketRecvInterface_Free(&o->output);
  184. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  185. OTPGenerator_Free(&o->otpgen);
  186. }
  187. fail0:
  188. return 0;
  189. }
  190. void SPProtoEncoder_Free (SPProtoEncoder *o)
  191. {
  192. DebugObject_Free(&o->d_obj);
  193. // free plaintext buffer
  194. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  195. free(o->buf);
  196. }
  197. // free output
  198. PacketRecvInterface_Free(&o->output);
  199. // free encryptor
  200. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params) && o->have_encryption_key) {
  201. BEncryption_Free(&o->encryptor);
  202. }
  203. // free otp generator
  204. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  205. OTPGenerator_Free(&o->otpgen);
  206. }
  207. }
  208. PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o)
  209. {
  210. DebugObject_Access(&o->d_obj);
  211. return &o->output;
  212. }
  213. void SPProtoEncoder_SetEncryptionKey (SPProtoEncoder *o, uint8_t *encryption_key)
  214. {
  215. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  216. DebugObject_Access(&o->d_obj);
  217. // free encryptor
  218. if (o->have_encryption_key) {
  219. BEncryption_Free(&o->encryptor);
  220. }
  221. // init encryptor
  222. BEncryption_Init(&o->encryptor, BENCRYPTION_MODE_ENCRYPT, o->sp_params.encryption_mode, encryption_key);
  223. // have encryption key
  224. o->have_encryption_key = 1;
  225. // possibly continue I/O
  226. maybe_encode(o);
  227. }
  228. void SPProtoEncoder_RemoveEncryptionKey (SPProtoEncoder *o)
  229. {
  230. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  231. DebugObject_Access(&o->d_obj);
  232. if (o->have_encryption_key) {
  233. // free encryptor
  234. BEncryption_Free(&o->encryptor);
  235. // have no encryption key
  236. o->have_encryption_key = 0;
  237. }
  238. }
  239. void SPProtoEncoder_SetOTPSeed (SPProtoEncoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv)
  240. {
  241. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  242. DebugObject_Access(&o->d_obj);
  243. // give seed to OTP generator
  244. OTPGenerator_SetSeed(&o->otpgen, key, iv);
  245. // remember seed ID
  246. o->otpgen_seed_id = seed_id;
  247. // possibly continue I/O
  248. maybe_encode(o);
  249. }
  250. void SPProtoEncoder_RemoveOTPSeed (SPProtoEncoder *o)
  251. {
  252. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  253. DebugObject_Access(&o->d_obj);
  254. // reset OTP generator
  255. OTPGenerator_Reset(&o->otpgen);
  256. }
  257. int SPProtoEncoder_GetOTPPosition (SPProtoEncoder *o)
  258. {
  259. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  260. DebugObject_Access(&o->d_obj);
  261. return OTPGenerator_GetPosition(&o->otpgen);
  262. }