SPProtoDecoder.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /**
  2. * @file SPProtoDecoder.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 <misc/balign.h>
  31. #include <misc/byteorder.h>
  32. #include <security/BHash.h>
  33. #include "SPProtoDecoder.h"
  34. #include <generated/blog_channel_SPProtoDecoder.h>
  35. #define PeerLog(_o, ...) BLog_LogViaFunc((_o)->logfunc, (_o)->user, BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  36. static void decode_work_func (SPProtoDecoder *o)
  37. {
  38. ASSERT(o->in_len >= 0)
  39. ASSERT(o->in_len <= o->input_mtu)
  40. uint8_t *in = o->in;
  41. int in_len = o->in_len;
  42. o->tw_out_len = -1;
  43. uint8_t *plaintext;
  44. int plaintext_len;
  45. // decrypt if needed
  46. if (!SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  47. plaintext = in;
  48. plaintext_len = in_len;
  49. } else {
  50. // input must be a multiple of blocks size
  51. if (in_len % o->enc_block_size != 0) {
  52. PeerLog(o, BLOG_WARNING, "packet size not a multiple of block size");
  53. return;
  54. }
  55. // input must have an IV block
  56. if (in_len < o->enc_block_size) {
  57. PeerLog(o, BLOG_WARNING, "packet does not have an IV");
  58. return;
  59. }
  60. // check if we have encryption key
  61. if (!o->have_encryption_key) {
  62. PeerLog(o, BLOG_WARNING, "have no encryption key");
  63. return;
  64. }
  65. // copy IV as BEncryption_Decrypt changes the IV
  66. uint8_t iv[BENCRYPTION_MAX_BLOCK_SIZE];
  67. memcpy(iv, in, o->enc_block_size);
  68. // decrypt
  69. uint8_t *ciphertext = in + o->enc_block_size;
  70. int ciphertext_len = in_len - o->enc_block_size;
  71. plaintext = o->buf;
  72. BEncryption_Decrypt(&o->encryptor, ciphertext, plaintext, ciphertext_len, iv);
  73. // read padding
  74. if (ciphertext_len < o->enc_block_size) {
  75. PeerLog(o, BLOG_WARNING, "packet does not have a padding block");
  76. return;
  77. }
  78. int i;
  79. for (i = ciphertext_len - 1; i >= ciphertext_len - o->enc_block_size; i--) {
  80. if (plaintext[i] == 1) {
  81. break;
  82. }
  83. if (plaintext[i] != 0) {
  84. PeerLog(o, BLOG_WARNING, "packet padding wrong (nonzero byte)");
  85. return;
  86. }
  87. }
  88. if (i < ciphertext_len - o->enc_block_size) {
  89. PeerLog(o, BLOG_WARNING, "packet padding wrong (all zeroes)");
  90. return;
  91. }
  92. plaintext_len = i;
  93. }
  94. // check for header
  95. if (plaintext_len < SPPROTO_HEADER_LEN(o->sp_params)) {
  96. PeerLog(o, BLOG_WARNING, "packet has no header");
  97. return;
  98. }
  99. uint8_t *header = plaintext;
  100. // check data length
  101. if (plaintext_len - SPPROTO_HEADER_LEN(o->sp_params) > o->output_mtu) {
  102. PeerLog(o, BLOG_WARNING, "packet too long");
  103. return;
  104. }
  105. // check OTP
  106. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  107. // remember seed and OTP (can't check from here)
  108. struct spproto_otpdata header_otpd;
  109. memcpy(&header_otpd, header + SPPROTO_HEADER_OTPDATA_OFF(o->sp_params), sizeof(header_otpd));
  110. o->tw_out_seed_id = ltoh16(header_otpd.seed_id);
  111. o->tw_out_otp = header_otpd.otp;
  112. }
  113. // check hash
  114. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  115. uint8_t *header_hash = header + SPPROTO_HEADER_HASH_OFF(o->sp_params);
  116. // read hash
  117. uint8_t hash[BHASH_MAX_SIZE];
  118. memcpy(hash, header_hash, o->hash_size);
  119. // zero hash in packet
  120. memset(header_hash, 0, o->hash_size);
  121. // calculate hash
  122. uint8_t hash_calc[BHASH_MAX_SIZE];
  123. BHash_calculate(o->sp_params.hash_mode, plaintext, plaintext_len, hash_calc);
  124. // set hash field to its original value
  125. memcpy(header_hash, hash, o->hash_size);
  126. // compare hashes
  127. if (memcmp(hash, hash_calc, o->hash_size)) {
  128. PeerLog(o, BLOG_WARNING, "packet has wrong hash");
  129. return;
  130. }
  131. }
  132. // return packet
  133. o->tw_out = plaintext + SPPROTO_HEADER_LEN(o->sp_params);
  134. o->tw_out_len = plaintext_len - SPPROTO_HEADER_LEN(o->sp_params);
  135. }
  136. static void decode_work_handler (SPProtoDecoder *o)
  137. {
  138. ASSERT(o->in_len >= 0)
  139. ASSERT(o->tw_have)
  140. DebugObject_Access(&o->d_obj);
  141. // free work
  142. BThreadWork_Free(&o->tw);
  143. o->tw_have = 0;
  144. // check OTP
  145. if (SPPROTO_HAVE_OTP(o->sp_params) && o->tw_out_len >= 0) {
  146. if (!OTPChecker_CheckOTP(&o->otpchecker, o->tw_out_seed_id, o->tw_out_otp)) {
  147. PeerLog(o, BLOG_WARNING, "packet has wrong OTP");
  148. o->tw_out_len = -1;
  149. }
  150. }
  151. if (o->tw_out_len < 0) {
  152. // cannot decode, finish input packet
  153. PacketPassInterface_Done(&o->input);
  154. o->in_len = -1;
  155. } else {
  156. // submit decoded packet to output
  157. PacketPassInterface_Sender_Send(o->output, o->tw_out, o->tw_out_len);
  158. }
  159. }
  160. static void input_handler_send (SPProtoDecoder *o, uint8_t *data, int data_len)
  161. {
  162. ASSERT(data_len >= 0)
  163. ASSERT(data_len <= o->input_mtu)
  164. ASSERT(o->in_len == -1)
  165. ASSERT(!o->tw_have)
  166. DebugObject_Access(&o->d_obj);
  167. // remember input
  168. o->in = data;
  169. o->in_len = data_len;
  170. // start decoding
  171. BThreadWork_Init(&o->tw, o->twd, (BThreadWork_handler_done)decode_work_handler, o, (BThreadWork_work_func)decode_work_func, o);
  172. o->tw_have = 1;
  173. }
  174. static void output_handler_done (SPProtoDecoder *o)
  175. {
  176. ASSERT(o->in_len >= 0)
  177. ASSERT(!o->tw_have)
  178. DebugObject_Access(&o->d_obj);
  179. // finish input packet
  180. PacketPassInterface_Done(&o->input);
  181. o->in_len = -1;
  182. }
  183. static void maybe_stop_work_and_ignore (SPProtoDecoder *o)
  184. {
  185. ASSERT(!(o->tw_have) || o->in_len >= 0)
  186. if (o->tw_have) {
  187. // free work
  188. BThreadWork_Free(&o->tw);
  189. o->tw_have = 0;
  190. // ignore packet, receive next one
  191. PacketPassInterface_Done(&o->input);
  192. o->in_len = -1;
  193. }
  194. }
  195. int SPProtoDecoder_Init (SPProtoDecoder *o, PacketPassInterface *output, struct spproto_security_params sp_params, int num_otp_seeds, BPendingGroup *pg, BThreadWorkDispatcher *twd, void *user, BLog_logfunc logfunc)
  196. {
  197. spproto_assert_security_params(sp_params);
  198. ASSERT(spproto_carrier_mtu_for_payload_mtu(sp_params, PacketPassInterface_GetMTU(output)) >= 0)
  199. ASSERT(!SPPROTO_HAVE_OTP(sp_params) || num_otp_seeds >= 2)
  200. // init arguments
  201. o->output = output;
  202. o->sp_params = sp_params;
  203. o->twd = twd;
  204. o->user = user;
  205. o->logfunc = logfunc;
  206. // init output
  207. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  208. // remember output MTU
  209. o->output_mtu = PacketPassInterface_GetMTU(o->output);
  210. // calculate hash size
  211. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  212. o->hash_size = BHash_size(o->sp_params.hash_mode);
  213. }
  214. // calculate encryption block and key sizes
  215. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  216. o->enc_block_size = BEncryption_cipher_block_size(o->sp_params.encryption_mode);
  217. o->enc_key_size = BEncryption_cipher_key_size(o->sp_params.encryption_mode);
  218. }
  219. // calculate input MTU
  220. o->input_mtu = spproto_carrier_mtu_for_payload_mtu(o->sp_params, o->output_mtu);
  221. // allocate plaintext buffer
  222. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  223. int buf_size = balign_up((SPPROTO_HEADER_LEN(o->sp_params) + o->output_mtu + 1), o->enc_block_size);
  224. if (!(o->buf = (uint8_t *)malloc(buf_size))) {
  225. goto fail0;
  226. }
  227. }
  228. // init input
  229. PacketPassInterface_Init(&o->input, o->input_mtu, (PacketPassInterface_handler_send)input_handler_send, o, pg);
  230. // init OTP checker
  231. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  232. if (!OTPChecker_Init(&o->otpchecker, o->sp_params.otp_num, o->sp_params.otp_mode, num_otp_seeds, o->twd)) {
  233. goto fail1;
  234. }
  235. }
  236. // have no encryption key
  237. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  238. o->have_encryption_key = 0;
  239. }
  240. // have no input packet
  241. o->in_len = -1;
  242. // have no work
  243. o->tw_have = 0;
  244. DebugObject_Init(&o->d_obj);
  245. return 1;
  246. fail1:
  247. PacketPassInterface_Free(&o->input);
  248. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  249. free(o->buf);
  250. }
  251. fail0:
  252. return 0;
  253. }
  254. void SPProtoDecoder_Free (SPProtoDecoder *o)
  255. {
  256. DebugObject_Free(&o->d_obj);
  257. // free work
  258. if (o->tw_have) {
  259. BThreadWork_Free(&o->tw);
  260. }
  261. // free encryptor
  262. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params) && o->have_encryption_key) {
  263. BEncryption_Free(&o->encryptor);
  264. }
  265. // free OTP checker
  266. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  267. OTPChecker_Free(&o->otpchecker);
  268. }
  269. // free input
  270. PacketPassInterface_Free(&o->input);
  271. // free plaintext buffer
  272. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  273. free(o->buf);
  274. }
  275. }
  276. PacketPassInterface * SPProtoDecoder_GetInput (SPProtoDecoder *o)
  277. {
  278. DebugObject_Access(&o->d_obj);
  279. return &o->input;
  280. }
  281. void SPProtoDecoder_SetEncryptionKey (SPProtoDecoder *o, uint8_t *encryption_key)
  282. {
  283. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  284. DebugObject_Access(&o->d_obj);
  285. // stop existing work
  286. maybe_stop_work_and_ignore(o);
  287. // free encryptor
  288. if (o->have_encryption_key) {
  289. BEncryption_Free(&o->encryptor);
  290. }
  291. // init encryptor
  292. BEncryption_Init(&o->encryptor, BENCRYPTION_MODE_DECRYPT, o->sp_params.encryption_mode, encryption_key);
  293. // have encryption key
  294. o->have_encryption_key = 1;
  295. }
  296. void SPProtoDecoder_RemoveEncryptionKey (SPProtoDecoder *o)
  297. {
  298. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  299. DebugObject_Access(&o->d_obj);
  300. // stop existing work
  301. maybe_stop_work_and_ignore(o);
  302. if (o->have_encryption_key) {
  303. // free encryptor
  304. BEncryption_Free(&o->encryptor);
  305. // have no encryption key
  306. o->have_encryption_key = 0;
  307. }
  308. }
  309. void SPProtoDecoder_AddOTPSeed (SPProtoDecoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv)
  310. {
  311. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  312. DebugObject_Access(&o->d_obj);
  313. OTPChecker_AddSeed(&o->otpchecker, seed_id, key, iv);
  314. }
  315. void SPProtoDecoder_RemoveOTPSeeds (SPProtoDecoder *o)
  316. {
  317. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  318. DebugObject_Access(&o->d_obj);
  319. OTPChecker_RemoveSeeds(&o->otpchecker);
  320. }
  321. void SPProtoDecoder_SetHandlers (SPProtoDecoder *o, SPProtoDecoder_otp_handler otp_handler, void *user)
  322. {
  323. DebugObject_Access(&o->d_obj);
  324. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  325. OTPChecker_SetHandlers(&o->otpchecker, otp_handler, user);
  326. }
  327. }