SPProtoDecoder.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 = (struct spproto_otpdata *)(header + SPPROTO_HEADER_OTPDATA_OFF(o->sp_params));
  109. o->tw_out_seed_id = ltoh16(header_otpd->seed_id);
  110. o->tw_out_otp = header_otpd->otp;
  111. }
  112. // check hash
  113. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  114. uint8_t *header_hash = header + SPPROTO_HEADER_HASH_OFF(o->sp_params);
  115. // read hash
  116. uint8_t hash[BHASH_MAX_SIZE];
  117. memcpy(hash, header_hash, o->hash_size);
  118. // zero hash in packet
  119. memset(header_hash, 0, o->hash_size);
  120. // calculate hash
  121. uint8_t hash_calc[BHASH_MAX_SIZE];
  122. BHash_calculate(o->sp_params.hash_mode, plaintext, plaintext_len, hash_calc);
  123. // set hash field to its original value
  124. memcpy(header_hash, hash, o->hash_size);
  125. // compare hashes
  126. if (memcmp(hash, hash_calc, o->hash_size)) {
  127. PeerLog(o, BLOG_WARNING, "packet has wrong hash");
  128. return;
  129. }
  130. }
  131. // return packet
  132. o->tw_out = plaintext + SPPROTO_HEADER_LEN(o->sp_params);
  133. o->tw_out_len = plaintext_len - SPPROTO_HEADER_LEN(o->sp_params);
  134. }
  135. static void decode_work_handler (SPProtoDecoder *o)
  136. {
  137. ASSERT(o->in_len >= 0)
  138. ASSERT(o->tw_have)
  139. DebugObject_Access(&o->d_obj);
  140. // free work
  141. BThreadWork_Free(&o->tw);
  142. o->tw_have = 0;
  143. // check OTP
  144. if (SPPROTO_HAVE_OTP(o->sp_params) && o->tw_out_len >= 0) {
  145. if (!OTPChecker_CheckOTP(&o->otpchecker, o->tw_out_seed_id, o->tw_out_otp)) {
  146. PeerLog(o, BLOG_WARNING, "packet has wrong OTP");
  147. o->tw_out_len = -1;
  148. }
  149. }
  150. if (o->tw_out_len < 0) {
  151. // cannot decode, finish input packet
  152. PacketPassInterface_Done(&o->input);
  153. o->in_len = -1;
  154. } else {
  155. // submit decoded packet to output
  156. PacketPassInterface_Sender_Send(o->output, o->tw_out, o->tw_out_len);
  157. }
  158. }
  159. static void input_handler_send (SPProtoDecoder *o, uint8_t *data, int data_len)
  160. {
  161. ASSERT(data_len >= 0)
  162. ASSERT(data_len <= o->input_mtu)
  163. ASSERT(o->in_len == -1)
  164. ASSERT(!o->tw_have)
  165. DebugObject_Access(&o->d_obj);
  166. // remember input
  167. o->in = data;
  168. o->in_len = data_len;
  169. // start decoding
  170. BThreadWork_Init(&o->tw, o->twd, (BThreadWork_handler_done)decode_work_handler, o, (BThreadWork_work_func)decode_work_func, o);
  171. o->tw_have = 1;
  172. }
  173. static void output_handler_done (SPProtoDecoder *o)
  174. {
  175. ASSERT(o->in_len >= 0)
  176. ASSERT(!o->tw_have)
  177. DebugObject_Access(&o->d_obj);
  178. // finish input packet
  179. PacketPassInterface_Done(&o->input);
  180. o->in_len = -1;
  181. }
  182. static void maybe_stop_work_and_ignore (SPProtoDecoder *o)
  183. {
  184. ASSERT(!(o->tw_have) || o->in_len >= 0)
  185. if (o->tw_have) {
  186. // free work
  187. BThreadWork_Free(&o->tw);
  188. o->tw_have = 0;
  189. // ignore packet, receive next one
  190. PacketPassInterface_Done(&o->input);
  191. o->in_len = -1;
  192. }
  193. }
  194. 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)
  195. {
  196. spproto_assert_security_params(sp_params);
  197. ASSERT(spproto_carrier_mtu_for_payload_mtu(sp_params, PacketPassInterface_GetMTU(output)) >= 0)
  198. ASSERT(!SPPROTO_HAVE_OTP(sp_params) || num_otp_seeds >= 2)
  199. // init arguments
  200. o->output = output;
  201. o->sp_params = sp_params;
  202. o->twd = twd;
  203. o->user = user;
  204. o->logfunc = logfunc;
  205. // init output
  206. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  207. // remember output MTU
  208. o->output_mtu = PacketPassInterface_GetMTU(o->output);
  209. // calculate hash size
  210. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  211. o->hash_size = BHash_size(o->sp_params.hash_mode);
  212. }
  213. // calculate encryption block and key sizes
  214. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  215. o->enc_block_size = BEncryption_cipher_block_size(o->sp_params.encryption_mode);
  216. o->enc_key_size = BEncryption_cipher_key_size(o->sp_params.encryption_mode);
  217. }
  218. // calculate input MTU
  219. o->input_mtu = spproto_carrier_mtu_for_payload_mtu(o->sp_params, o->output_mtu);
  220. // allocate plaintext buffer
  221. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  222. int buf_size = balign_up((SPPROTO_HEADER_LEN(o->sp_params) + o->output_mtu + 1), o->enc_block_size);
  223. if (!(o->buf = (uint8_t *)malloc(buf_size))) {
  224. goto fail0;
  225. }
  226. }
  227. // init input
  228. PacketPassInterface_Init(&o->input, o->input_mtu, (PacketPassInterface_handler_send)input_handler_send, o, pg);
  229. // init OTP checker
  230. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  231. if (!OTPChecker_Init(&o->otpchecker, o->sp_params.otp_num, o->sp_params.otp_mode, num_otp_seeds, o->twd)) {
  232. goto fail1;
  233. }
  234. }
  235. // have no encryption key
  236. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  237. o->have_encryption_key = 0;
  238. }
  239. // have no input packet
  240. o->in_len = -1;
  241. // have no work
  242. o->tw_have = 0;
  243. DebugObject_Init(&o->d_obj);
  244. return 1;
  245. fail1:
  246. PacketPassInterface_Free(&o->input);
  247. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  248. free(o->buf);
  249. }
  250. fail0:
  251. return 0;
  252. }
  253. void SPProtoDecoder_Free (SPProtoDecoder *o)
  254. {
  255. DebugObject_Free(&o->d_obj);
  256. // free work
  257. if (o->tw_have) {
  258. BThreadWork_Free(&o->tw);
  259. }
  260. // free encryptor
  261. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params) && o->have_encryption_key) {
  262. BEncryption_Free(&o->encryptor);
  263. }
  264. // free OTP checker
  265. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  266. OTPChecker_Free(&o->otpchecker);
  267. }
  268. // free input
  269. PacketPassInterface_Free(&o->input);
  270. // free plaintext buffer
  271. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  272. free(o->buf);
  273. }
  274. }
  275. PacketPassInterface * SPProtoDecoder_GetInput (SPProtoDecoder *o)
  276. {
  277. DebugObject_Access(&o->d_obj);
  278. return &o->input;
  279. }
  280. void SPProtoDecoder_SetEncryptionKey (SPProtoDecoder *o, uint8_t *encryption_key)
  281. {
  282. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  283. DebugObject_Access(&o->d_obj);
  284. // stop existing work
  285. maybe_stop_work_and_ignore(o);
  286. // free encryptor
  287. if (o->have_encryption_key) {
  288. BEncryption_Free(&o->encryptor);
  289. }
  290. // init encryptor
  291. BEncryption_Init(&o->encryptor, BENCRYPTION_MODE_DECRYPT, o->sp_params.encryption_mode, encryption_key);
  292. // have encryption key
  293. o->have_encryption_key = 1;
  294. }
  295. void SPProtoDecoder_RemoveEncryptionKey (SPProtoDecoder *o)
  296. {
  297. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  298. DebugObject_Access(&o->d_obj);
  299. // stop existing work
  300. maybe_stop_work_and_ignore(o);
  301. if (o->have_encryption_key) {
  302. // free encryptor
  303. BEncryption_Free(&o->encryptor);
  304. // have no encryption key
  305. o->have_encryption_key = 0;
  306. }
  307. }
  308. void SPProtoDecoder_AddOTPSeed (SPProtoDecoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv)
  309. {
  310. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  311. DebugObject_Access(&o->d_obj);
  312. OTPChecker_AddSeed(&o->otpchecker, seed_id, key, iv);
  313. }
  314. void SPProtoDecoder_RemoveOTPSeeds (SPProtoDecoder *o)
  315. {
  316. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  317. DebugObject_Access(&o->d_obj);
  318. OTPChecker_RemoveSeeds(&o->otpchecker);
  319. }
  320. void SPProtoDecoder_SetHandlers (SPProtoDecoder *o, SPProtoDecoder_otp_handler otp_handler, void *user)
  321. {
  322. DebugObject_Access(&o->d_obj);
  323. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  324. OTPChecker_SetHandlers(&o->otpchecker, otp_handler, user);
  325. }
  326. }