SPProtoEncoder.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /**
  2. * @file SPProtoEncoder.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <misc/balign.h>
  32. #include <misc/offset.h>
  33. #include <misc/byteorder.h>
  34. #include <security/BRandom.h>
  35. #include <security/BHash.h>
  36. #include "SPProtoEncoder.h"
  37. static int can_encode (SPProtoEncoder *o);
  38. static void encode_packet (SPProtoEncoder *o);
  39. static void encode_work_func (SPProtoEncoder *o);
  40. static void encode_work_handler (SPProtoEncoder *o);
  41. static void maybe_encode (SPProtoEncoder *o);
  42. static void output_handler_recv (SPProtoEncoder *o, uint8_t *data);
  43. static void input_handler_done (SPProtoEncoder *o, int data_len);
  44. static void handler_job_hander (SPProtoEncoder *o);
  45. static void otpgenerator_handler (SPProtoEncoder *o);
  46. static void maybe_stop_work (SPProtoEncoder *o);
  47. static int can_encode (SPProtoEncoder *o)
  48. {
  49. ASSERT(o->in_len >= 0)
  50. ASSERT(o->out_have)
  51. ASSERT(!o->tw_have)
  52. return (
  53. (!SPPROTO_HAVE_OTP(o->sp_params) || OTPGenerator_GetPosition(&o->otpgen) < o->sp_params.otp_num) &&
  54. (!SPPROTO_HAVE_ENCRYPTION(o->sp_params) || o->have_encryption_key)
  55. );
  56. }
  57. static void encode_packet (SPProtoEncoder *o)
  58. {
  59. ASSERT(o->in_len >= 0)
  60. ASSERT(o->out_have)
  61. ASSERT(!o->tw_have)
  62. ASSERT(can_encode(o))
  63. // generate OTP, remember seed ID
  64. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  65. o->tw_seed_id = o->otpgen_seed_id;
  66. o->tw_otp = OTPGenerator_GetOTP(&o->otpgen);
  67. }
  68. // start work
  69. BThreadWork_Init(&o->tw, o->twd, (BThreadWork_handler_done)encode_work_handler, o, (BThreadWork_work_func)encode_work_func, o);
  70. o->tw_have = 1;
  71. // schedule OTP warning handler
  72. if (SPPROTO_HAVE_OTP(o->sp_params) && OTPGenerator_GetPosition(&o->otpgen) == o->otp_warning_count) {
  73. BPending_Set(&o->handler_job);
  74. }
  75. }
  76. static void encode_work_func (SPProtoEncoder *o)
  77. {
  78. ASSERT(o->in_len >= 0)
  79. ASSERT(o->out_have)
  80. ASSERT(!SPPROTO_HAVE_ENCRYPTION(o->sp_params) || o->have_encryption_key)
  81. ASSERT(o->in_len <= o->input_mtu)
  82. // determine plaintext location
  83. uint8_t *plaintext = (SPPROTO_HAVE_ENCRYPTION(o->sp_params) ? o->buf : o->out);
  84. // plaintext begins with header
  85. uint8_t *header = plaintext;
  86. // plaintext is header + payload
  87. int plaintext_len = SPPROTO_HEADER_LEN(o->sp_params) + o->in_len;
  88. // write OTP
  89. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  90. struct spproto_otpdata *header_otpd = (struct spproto_otpdata *)(header + SPPROTO_HEADER_OTPDATA_OFF(o->sp_params));
  91. header_otpd->seed_id = htol16(o->tw_seed_id);
  92. header_otpd->otp = o->tw_otp;
  93. }
  94. // write hash
  95. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  96. uint8_t *header_hash = header + SPPROTO_HEADER_HASH_OFF(o->sp_params);
  97. // zero hash field
  98. memset(header_hash, 0, o->hash_size);
  99. // calculate hash
  100. uint8_t hash[BHASH_MAX_SIZE];
  101. BHash_calculate(o->sp_params.hash_mode, plaintext, plaintext_len, hash);
  102. // set hash field
  103. memcpy(header_hash, hash, o->hash_size);
  104. }
  105. int out_len;
  106. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  107. // encrypting pad(header + payload)
  108. int cyphertext_len = balign_up((plaintext_len + 1), o->enc_block_size);
  109. // write padding
  110. plaintext[plaintext_len] = 1;
  111. for (int i = plaintext_len + 1; i < cyphertext_len; i++) {
  112. plaintext[i] = 0;
  113. }
  114. // generate IV
  115. BRandom_randomize(o->out, o->enc_block_size);
  116. // copy IV because BEncryption_Encrypt changes the IV
  117. uint8_t iv[BENCRYPTION_MAX_BLOCK_SIZE];
  118. memcpy(iv, o->out, o->enc_block_size);
  119. // encrypt
  120. BEncryption_Encrypt(&o->encryptor, plaintext, o->out + o->enc_block_size, cyphertext_len, iv);
  121. out_len = o->enc_block_size + cyphertext_len;
  122. } else {
  123. out_len = plaintext_len;
  124. }
  125. // remember length
  126. o->tw_out_len = out_len;
  127. }
  128. static void encode_work_handler (SPProtoEncoder *o)
  129. {
  130. ASSERT(o->in_len >= 0)
  131. ASSERT(o->out_have)
  132. ASSERT(o->tw_have)
  133. // free work
  134. BThreadWork_Free(&o->tw);
  135. o->tw_have = 0;
  136. // finish packet
  137. o->in_len = -1;
  138. o->out_have = 0;
  139. PacketRecvInterface_Done(&o->output, o->tw_out_len);
  140. }
  141. static void maybe_encode (SPProtoEncoder *o)
  142. {
  143. if (o->in_len >= 0 && o->out_have && !o->tw_have && can_encode(o)) {
  144. encode_packet(o);
  145. }
  146. }
  147. static void output_handler_recv (SPProtoEncoder *o, uint8_t *data)
  148. {
  149. ASSERT(o->in_len == -1)
  150. ASSERT(!o->out_have)
  151. ASSERT(!o->tw_have)
  152. DebugObject_Access(&o->d_obj);
  153. // remember output packet
  154. o->out_have = 1;
  155. o->out = data;
  156. // determine plaintext location
  157. uint8_t *plaintext = (SPPROTO_HAVE_ENCRYPTION(o->sp_params) ? o->buf : o->out);
  158. // schedule receive
  159. PacketRecvInterface_Receiver_Recv(o->input, plaintext + SPPROTO_HEADER_LEN(o->sp_params));
  160. }
  161. static void input_handler_done (SPProtoEncoder *o, int data_len)
  162. {
  163. ASSERT(data_len >= 0)
  164. ASSERT(data_len <= o->input_mtu)
  165. ASSERT(o->in_len == -1)
  166. ASSERT(o->out_have)
  167. ASSERT(!o->tw_have)
  168. DebugObject_Access(&o->d_obj);
  169. // remember input packet
  170. o->in_len = data_len;
  171. // encode if possible
  172. if (can_encode(o)) {
  173. encode_packet(o);
  174. }
  175. }
  176. static void handler_job_hander (SPProtoEncoder *o)
  177. {
  178. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  179. DebugObject_Access(&o->d_obj);
  180. if (o->handler) {
  181. o->handler(o->user);
  182. return;
  183. }
  184. }
  185. static void otpgenerator_handler (SPProtoEncoder *o)
  186. {
  187. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  188. DebugObject_Access(&o->d_obj);
  189. // remember seed ID
  190. o->otpgen_seed_id = o->otpgen_pending_seed_id;
  191. // possibly continue I/O
  192. maybe_encode(o);
  193. }
  194. static void maybe_stop_work (SPProtoEncoder *o)
  195. {
  196. // stop existing work
  197. if (o->tw_have) {
  198. BThreadWork_Free(&o->tw);
  199. o->tw_have = 0;
  200. }
  201. }
  202. int SPProtoEncoder_Init (SPProtoEncoder *o, PacketRecvInterface *input, struct spproto_security_params sp_params, int otp_warning_count, BPendingGroup *pg, BThreadWorkDispatcher *twd)
  203. {
  204. spproto_assert_security_params(sp_params);
  205. ASSERT(spproto_carrier_mtu_for_payload_mtu(sp_params, PacketRecvInterface_GetMTU(input)) >= 0)
  206. if (SPPROTO_HAVE_OTP(sp_params)) {
  207. ASSERT(otp_warning_count > 0)
  208. ASSERT(otp_warning_count <= sp_params.otp_num)
  209. }
  210. // init arguments
  211. o->input = input;
  212. o->sp_params = sp_params;
  213. o->otp_warning_count = otp_warning_count;
  214. o->twd = twd;
  215. // set no handlers
  216. o->handler = NULL;
  217. // calculate hash size
  218. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  219. o->hash_size = BHash_size(o->sp_params.hash_mode);
  220. }
  221. // calculate encryption block and key sizes
  222. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  223. o->enc_block_size = BEncryption_cipher_block_size(o->sp_params.encryption_mode);
  224. o->enc_key_size = BEncryption_cipher_key_size(o->sp_params.encryption_mode);
  225. }
  226. // init otp generator
  227. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  228. if (!OTPGenerator_Init(&o->otpgen, o->sp_params.otp_num, o->sp_params.otp_mode, o->twd, (OTPGenerator_handler)otpgenerator_handler, o)) {
  229. goto fail0;
  230. }
  231. }
  232. // have no encryption key
  233. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  234. o->have_encryption_key = 0;
  235. }
  236. // remember input MTU
  237. o->input_mtu = PacketRecvInterface_GetMTU(o->input);
  238. // calculate output MTU
  239. o->output_mtu = spproto_carrier_mtu_for_payload_mtu(o->sp_params, o->input_mtu);
  240. // init input
  241. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  242. // have no input in buffer
  243. o->in_len = -1;
  244. // init output
  245. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  246. // have no output available
  247. o->out_have = 0;
  248. // allocate plaintext buffer
  249. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  250. int buf_size = balign_up((SPPROTO_HEADER_LEN(o->sp_params) + o->input_mtu + 1), o->enc_block_size);
  251. if (!(o->buf = malloc(buf_size))) {
  252. goto fail1;
  253. }
  254. }
  255. // init handler job
  256. BPending_Init(&o->handler_job, pg, (BPending_handler)handler_job_hander, o);
  257. // have no work
  258. o->tw_have = 0;
  259. DebugObject_Init(&o->d_obj);
  260. return 1;
  261. fail1:
  262. PacketRecvInterface_Free(&o->output);
  263. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  264. OTPGenerator_Free(&o->otpgen);
  265. }
  266. fail0:
  267. return 0;
  268. }
  269. void SPProtoEncoder_Free (SPProtoEncoder *o)
  270. {
  271. DebugObject_Free(&o->d_obj);
  272. // free work
  273. if (o->tw_have) {
  274. BThreadWork_Free(&o->tw);
  275. }
  276. // free handler job
  277. BPending_Free(&o->handler_job);
  278. // free plaintext buffer
  279. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  280. free(o->buf);
  281. }
  282. // free output
  283. PacketRecvInterface_Free(&o->output);
  284. // free encryptor
  285. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params) && o->have_encryption_key) {
  286. BEncryption_Free(&o->encryptor);
  287. }
  288. // free otp generator
  289. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  290. OTPGenerator_Free(&o->otpgen);
  291. }
  292. }
  293. PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o)
  294. {
  295. DebugObject_Access(&o->d_obj);
  296. return &o->output;
  297. }
  298. void SPProtoEncoder_SetEncryptionKey (SPProtoEncoder *o, uint8_t *encryption_key)
  299. {
  300. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  301. DebugObject_Access(&o->d_obj);
  302. // stop existing work
  303. maybe_stop_work(o);
  304. // free encryptor
  305. if (o->have_encryption_key) {
  306. BEncryption_Free(&o->encryptor);
  307. }
  308. // init encryptor
  309. BEncryption_Init(&o->encryptor, BENCRYPTION_MODE_ENCRYPT, o->sp_params.encryption_mode, encryption_key);
  310. // have encryption key
  311. o->have_encryption_key = 1;
  312. // possibly continue I/O
  313. maybe_encode(o);
  314. }
  315. void SPProtoEncoder_RemoveEncryptionKey (SPProtoEncoder *o)
  316. {
  317. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  318. DebugObject_Access(&o->d_obj);
  319. // stop existing work
  320. maybe_stop_work(o);
  321. if (o->have_encryption_key) {
  322. // free encryptor
  323. BEncryption_Free(&o->encryptor);
  324. // have no encryption key
  325. o->have_encryption_key = 0;
  326. }
  327. }
  328. void SPProtoEncoder_SetOTPSeed (SPProtoEncoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv)
  329. {
  330. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  331. DebugObject_Access(&o->d_obj);
  332. // give seed to OTP generator
  333. OTPGenerator_SetSeed(&o->otpgen, key, iv);
  334. // remember seed ID
  335. o->otpgen_pending_seed_id = seed_id;
  336. }
  337. void SPProtoEncoder_RemoveOTPSeed (SPProtoEncoder *o)
  338. {
  339. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  340. DebugObject_Access(&o->d_obj);
  341. // reset OTP generator
  342. OTPGenerator_Reset(&o->otpgen);
  343. }
  344. void SPProtoEncoder_SetHandlers (SPProtoEncoder *o, SPProtoEncoder_handler handler, void *user)
  345. {
  346. DebugObject_Access(&o->d_obj);
  347. o->handler = handler;
  348. o->user = user;
  349. }