SPProtoEncoder.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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;
  91. header_otpd.seed_id = htol16(o->tw_seed_id);
  92. header_otpd.otp = o->tw_otp;
  93. memcpy(header + SPPROTO_HEADER_OTPDATA_OFF(o->sp_params), &header_otpd, sizeof(header_otpd));
  94. }
  95. // write hash
  96. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  97. uint8_t *header_hash = header + SPPROTO_HEADER_HASH_OFF(o->sp_params);
  98. // zero hash field
  99. memset(header_hash, 0, o->hash_size);
  100. // calculate hash
  101. uint8_t hash[BHASH_MAX_SIZE];
  102. BHash_calculate(o->sp_params.hash_mode, plaintext, plaintext_len, hash);
  103. // set hash field
  104. memcpy(header_hash, hash, o->hash_size);
  105. }
  106. int out_len;
  107. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  108. // encrypting pad(header + payload)
  109. int cyphertext_len = balign_up((plaintext_len + 1), o->enc_block_size);
  110. // write padding
  111. plaintext[plaintext_len] = 1;
  112. for (int i = plaintext_len + 1; i < cyphertext_len; i++) {
  113. plaintext[i] = 0;
  114. }
  115. // generate IV
  116. BRandom_randomize(o->out, o->enc_block_size);
  117. // copy IV because BEncryption_Encrypt changes the IV
  118. uint8_t iv[BENCRYPTION_MAX_BLOCK_SIZE];
  119. memcpy(iv, o->out, o->enc_block_size);
  120. // encrypt
  121. BEncryption_Encrypt(&o->encryptor, plaintext, o->out + o->enc_block_size, cyphertext_len, iv);
  122. out_len = o->enc_block_size + cyphertext_len;
  123. } else {
  124. out_len = plaintext_len;
  125. }
  126. // remember length
  127. o->tw_out_len = out_len;
  128. }
  129. static void encode_work_handler (SPProtoEncoder *o)
  130. {
  131. ASSERT(o->in_len >= 0)
  132. ASSERT(o->out_have)
  133. ASSERT(o->tw_have)
  134. // free work
  135. BThreadWork_Free(&o->tw);
  136. o->tw_have = 0;
  137. // finish packet
  138. o->in_len = -1;
  139. o->out_have = 0;
  140. PacketRecvInterface_Done(&o->output, o->tw_out_len);
  141. }
  142. static void maybe_encode (SPProtoEncoder *o)
  143. {
  144. if (o->in_len >= 0 && o->out_have && !o->tw_have && can_encode(o)) {
  145. encode_packet(o);
  146. }
  147. }
  148. static void output_handler_recv (SPProtoEncoder *o, uint8_t *data)
  149. {
  150. ASSERT(o->in_len == -1)
  151. ASSERT(!o->out_have)
  152. ASSERT(!o->tw_have)
  153. DebugObject_Access(&o->d_obj);
  154. // remember output packet
  155. o->out_have = 1;
  156. o->out = data;
  157. // determine plaintext location
  158. uint8_t *plaintext = (SPPROTO_HAVE_ENCRYPTION(o->sp_params) ? o->buf : o->out);
  159. // schedule receive
  160. PacketRecvInterface_Receiver_Recv(o->input, plaintext + SPPROTO_HEADER_LEN(o->sp_params));
  161. }
  162. static void input_handler_done (SPProtoEncoder *o, int data_len)
  163. {
  164. ASSERT(data_len >= 0)
  165. ASSERT(data_len <= o->input_mtu)
  166. ASSERT(o->in_len == -1)
  167. ASSERT(o->out_have)
  168. ASSERT(!o->tw_have)
  169. DebugObject_Access(&o->d_obj);
  170. // remember input packet
  171. o->in_len = data_len;
  172. // encode if possible
  173. if (can_encode(o)) {
  174. encode_packet(o);
  175. }
  176. }
  177. static void handler_job_hander (SPProtoEncoder *o)
  178. {
  179. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  180. DebugObject_Access(&o->d_obj);
  181. if (o->handler) {
  182. o->handler(o->user);
  183. return;
  184. }
  185. }
  186. static void otpgenerator_handler (SPProtoEncoder *o)
  187. {
  188. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  189. DebugObject_Access(&o->d_obj);
  190. // remember seed ID
  191. o->otpgen_seed_id = o->otpgen_pending_seed_id;
  192. // possibly continue I/O
  193. maybe_encode(o);
  194. }
  195. static void maybe_stop_work (SPProtoEncoder *o)
  196. {
  197. // stop existing work
  198. if (o->tw_have) {
  199. BThreadWork_Free(&o->tw);
  200. o->tw_have = 0;
  201. }
  202. }
  203. int SPProtoEncoder_Init (SPProtoEncoder *o, PacketRecvInterface *input, struct spproto_security_params sp_params, int otp_warning_count, BPendingGroup *pg, BThreadWorkDispatcher *twd)
  204. {
  205. spproto_assert_security_params(sp_params);
  206. ASSERT(spproto_carrier_mtu_for_payload_mtu(sp_params, PacketRecvInterface_GetMTU(input)) >= 0)
  207. if (SPPROTO_HAVE_OTP(sp_params)) {
  208. ASSERT(otp_warning_count > 0)
  209. ASSERT(otp_warning_count <= sp_params.otp_num)
  210. }
  211. // init arguments
  212. o->input = input;
  213. o->sp_params = sp_params;
  214. o->otp_warning_count = otp_warning_count;
  215. o->twd = twd;
  216. // set no handlers
  217. o->handler = NULL;
  218. // calculate hash size
  219. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  220. o->hash_size = BHash_size(o->sp_params.hash_mode);
  221. }
  222. // calculate encryption block and key sizes
  223. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  224. o->enc_block_size = BEncryption_cipher_block_size(o->sp_params.encryption_mode);
  225. o->enc_key_size = BEncryption_cipher_key_size(o->sp_params.encryption_mode);
  226. }
  227. // init otp generator
  228. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  229. if (!OTPGenerator_Init(&o->otpgen, o->sp_params.otp_num, o->sp_params.otp_mode, o->twd, (OTPGenerator_handler)otpgenerator_handler, o)) {
  230. goto fail0;
  231. }
  232. }
  233. // have no encryption key
  234. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  235. o->have_encryption_key = 0;
  236. }
  237. // remember input MTU
  238. o->input_mtu = PacketRecvInterface_GetMTU(o->input);
  239. // calculate output MTU
  240. o->output_mtu = spproto_carrier_mtu_for_payload_mtu(o->sp_params, o->input_mtu);
  241. // init input
  242. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  243. // have no input in buffer
  244. o->in_len = -1;
  245. // init output
  246. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  247. // have no output available
  248. o->out_have = 0;
  249. // allocate plaintext buffer
  250. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  251. int buf_size = balign_up((SPPROTO_HEADER_LEN(o->sp_params) + o->input_mtu + 1), o->enc_block_size);
  252. if (!(o->buf = (uint8_t *)malloc(buf_size))) {
  253. goto fail1;
  254. }
  255. }
  256. // init handler job
  257. BPending_Init(&o->handler_job, pg, (BPending_handler)handler_job_hander, o);
  258. // have no work
  259. o->tw_have = 0;
  260. DebugObject_Init(&o->d_obj);
  261. return 1;
  262. fail1:
  263. PacketRecvInterface_Free(&o->output);
  264. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  265. OTPGenerator_Free(&o->otpgen);
  266. }
  267. fail0:
  268. return 0;
  269. }
  270. void SPProtoEncoder_Free (SPProtoEncoder *o)
  271. {
  272. DebugObject_Free(&o->d_obj);
  273. // free work
  274. if (o->tw_have) {
  275. BThreadWork_Free(&o->tw);
  276. }
  277. // free handler job
  278. BPending_Free(&o->handler_job);
  279. // free plaintext buffer
  280. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  281. free(o->buf);
  282. }
  283. // free output
  284. PacketRecvInterface_Free(&o->output);
  285. // free encryptor
  286. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params) && o->have_encryption_key) {
  287. BEncryption_Free(&o->encryptor);
  288. }
  289. // free otp generator
  290. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  291. OTPGenerator_Free(&o->otpgen);
  292. }
  293. }
  294. PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o)
  295. {
  296. DebugObject_Access(&o->d_obj);
  297. return &o->output;
  298. }
  299. void SPProtoEncoder_SetEncryptionKey (SPProtoEncoder *o, uint8_t *encryption_key)
  300. {
  301. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  302. DebugObject_Access(&o->d_obj);
  303. // stop existing work
  304. maybe_stop_work(o);
  305. // free encryptor
  306. if (o->have_encryption_key) {
  307. BEncryption_Free(&o->encryptor);
  308. }
  309. // init encryptor
  310. BEncryption_Init(&o->encryptor, BENCRYPTION_MODE_ENCRYPT, o->sp_params.encryption_mode, encryption_key);
  311. // have encryption key
  312. o->have_encryption_key = 1;
  313. // possibly continue I/O
  314. maybe_encode(o);
  315. }
  316. void SPProtoEncoder_RemoveEncryptionKey (SPProtoEncoder *o)
  317. {
  318. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  319. DebugObject_Access(&o->d_obj);
  320. // stop existing work
  321. maybe_stop_work(o);
  322. if (o->have_encryption_key) {
  323. // free encryptor
  324. BEncryption_Free(&o->encryptor);
  325. // have no encryption key
  326. o->have_encryption_key = 0;
  327. }
  328. }
  329. void SPProtoEncoder_SetOTPSeed (SPProtoEncoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv)
  330. {
  331. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  332. DebugObject_Access(&o->d_obj);
  333. // give seed to OTP generator
  334. OTPGenerator_SetSeed(&o->otpgen, key, iv);
  335. // remember seed ID
  336. o->otpgen_pending_seed_id = seed_id;
  337. }
  338. void SPProtoEncoder_RemoveOTPSeed (SPProtoEncoder *o)
  339. {
  340. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  341. DebugObject_Access(&o->d_obj);
  342. // reset OTP generator
  343. OTPGenerator_Reset(&o->otpgen);
  344. }
  345. void SPProtoEncoder_SetHandlers (SPProtoEncoder *o, SPProtoEncoder_handler handler, void *user)
  346. {
  347. DebugObject_Access(&o->d_obj);
  348. o->handler = handler;
  349. o->user = user;
  350. }