ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
Martin Jesper Low Madsen | af76964 | 2015-04-28 22:33:43 +0200 | [diff] [blame] | 2 | /* |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 3 | * Copyright 2014 <+YOU OR YOUR COMPANY+>. |
Martin Jesper Low Madsen | af76964 | 2015-04-28 22:33:43 +0200 | [diff] [blame] | 4 | * |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 5 | * This is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3, or (at your option) |
| 8 | * any later version. |
Martin Jesper Low Madsen | af76964 | 2015-04-28 22:33:43 +0200 | [diff] [blame] | 9 | * |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 10 | * This software is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Martin Jesper Low Madsen | af76964 | 2015-04-28 22:33:43 +0200 | [diff] [blame] | 14 | * |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this software; see the file COPYING. If not, write to |
| 17 | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 18 | * Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include "config.h" |
| 23 | #endif |
| 24 | |
| 25 | #include <gnuradio/io_signature.h> |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 26 | #include <grgsm/gsmtap.h> |
ptrkrysik | 5817a79 | 2015-04-25 18:51:08 +0200 | [diff] [blame] | 27 | #include <grgsm/endian.h> |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 28 | #include "decryption_impl.h" |
| 29 | #include "a5_1_2.h" |
| 30 | |
ptrkrysik | 5817a79 | 2015-04-25 18:51:08 +0200 | [diff] [blame] | 31 | |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 32 | const uint32_t BURST_SIZE=148; |
| 33 | |
| 34 | namespace gr { |
| 35 | namespace gsm { |
| 36 | |
| 37 | decryption::sptr |
| 38 | decryption::make(const std::vector<uint8_t> & k_c) |
| 39 | { |
| 40 | return gnuradio::get_initial_sptr |
| 41 | (new decryption_impl(k_c)); |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * The private constructor |
| 46 | */ |
| 47 | decryption_impl::decryption_impl(const std::vector<uint8_t> & k_c) |
| 48 | : gr::block("decryption", |
| 49 | gr::io_signature::make(0, 0, 0), |
| 50 | gr::io_signature::make(0, 0, 0)) |
| 51 | { |
| 52 | set_k_c(k_c); |
| 53 | |
| 54 | // std::cout << "Be careful with decryption block - it wasn't tested yet!" << std::endl; |
| 55 | message_port_register_in(pmt::mp("bursts")); |
| 56 | set_msg_handler(pmt::mp("bursts"), boost::bind(&decryption_impl::decrypt, this, _1)); |
| 57 | message_port_register_out(pmt::mp("bursts")); |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Virtual destructor |
| 62 | */ |
| 63 | decryption_impl::~decryption_impl() |
| 64 | { |
| 65 | } |
Martin Jesper Low Madsen | af76964 | 2015-04-28 22:33:43 +0200 | [diff] [blame] | 66 | |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 67 | void decryption_impl::set_k_c(const std::vector<uint8_t> & k_c) |
| 68 | { |
| 69 | d_k_c = k_c; |
| 70 | } |
Martin Jesper Low Madsen | af76964 | 2015-04-28 22:33:43 +0200 | [diff] [blame] | 71 | |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 72 | void decryption_impl::decrypt(pmt::pmt_t msg) |
| 73 | { |
| 74 | if(d_k_c.size() != 8){ |
| 75 | message_port_pub(pmt::mp("bursts"), msg); |
Martin Jesper Low Madsen | af76964 | 2015-04-28 22:33:43 +0200 | [diff] [blame] | 76 | } else |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 77 | if(d_k_c[0] == 0 && d_k_c[1] == 0 && d_k_c[2] == 0 && d_k_c[3] == 0 & |
| 78 | d_k_c[4] == 0 && d_k_c[5] == 0 && d_k_c[6] == 0 && d_k_c[7] == 0) |
| 79 | { |
| 80 | message_port_pub(pmt::mp("bursts"), msg); |
| 81 | } else |
| 82 | { |
| 83 | uint8_t decrypted_data[BURST_SIZE]; |
ptrkrysik | 4739c94 | 2015-02-07 19:51:03 +0100 | [diff] [blame] | 84 | uint8_t AtoBkeystream[114]; |
| 85 | uint8_t BtoAkeystream[114]; |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 86 | uint8_t * keystream; |
Martin Jesper Low Madsen | af76964 | 2015-04-28 22:33:43 +0200 | [diff] [blame] | 87 | |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 88 | pmt::pmt_t header_plus_burst = pmt::cdr(msg); |
| 89 | gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(header_plus_burst); |
| 90 | uint8_t * burst_binary = (uint8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr); |
Martin Jesper Low Madsen | af76964 | 2015-04-28 22:33:43 +0200 | [diff] [blame] | 91 | |
ptrkrysik | 4739c94 | 2015-02-07 19:51:03 +0100 | [diff] [blame] | 92 | uint32_t frame_number = be32toh(header->frame_number); |
| 93 | bool uplink_burst = (be16toh(header->arfcn) & 0x4000) ? true : false; |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 94 | uint32_t t1 = frame_number / (26*51); |
| 95 | uint32_t t2 = frame_number % 26; |
| 96 | uint32_t t3 = frame_number % 51; |
| 97 | uint32_t frame_number_mod = (t1 << 11) + (t3 << 5) + t2; |
| 98 | keysetup(&d_k_c[0], frame_number_mod); |
ptrkrysik | a1871f5 | 2014-12-15 09:38:00 +0100 | [diff] [blame] | 99 | runA51(AtoBkeystream, BtoAkeystream); |
Martin Jesper Low Madsen | af76964 | 2015-04-28 22:33:43 +0200 | [diff] [blame] | 100 | |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 101 | if(uplink_burst){ |
| 102 | //process uplink burst |
| 103 | keystream = BtoAkeystream; |
| 104 | } else { |
| 105 | //process downlink burst |
| 106 | keystream = AtoBkeystream; |
| 107 | } |
| 108 | /* guard bits */ |
| 109 | for (int i = 0; i < 3; i++) { |
| 110 | decrypted_data[i] = burst_binary[i]; |
| 111 | } |
ptrkrysik | a1871f5 | 2014-12-15 09:38:00 +0100 | [diff] [blame] | 112 | //encrypt first part of the burst |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 113 | for (int i = 0; i < 57; i++) { |
| 114 | decrypted_data[i+3] = keystream[i] ^ burst_binary[i+3]; |
| 115 | } |
| 116 | /* stealing bits and midamble */ |
| 117 | for (int i = 60; i < 88; i++) { |
| 118 | decrypted_data[i] = burst_binary[i]; |
| 119 | } |
ptrkrysik | a1871f5 | 2014-12-15 09:38:00 +0100 | [diff] [blame] | 120 | //encrypt second part of the burst |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 121 | for (int i = 0; i < 57; i++) { |
| 122 | decrypted_data[i+88] = keystream[i+57] ^ burst_binary[i+88]; |
| 123 | } |
| 124 | /* guard bits */ |
| 125 | for (int i = 145; i < 148; i++) { |
| 126 | decrypted_data[i] = burst_binary[i]; |
| 127 | } |
| 128 | uint8_t new_header_plus_burst[sizeof(gsmtap_hdr)+BURST_SIZE]; |
| 129 | memcpy(new_header_plus_burst, header, sizeof(gsmtap_hdr)); |
| 130 | memcpy(new_header_plus_burst+sizeof(gsmtap_hdr), decrypted_data, BURST_SIZE); |
Martin Jesper Low Madsen | af76964 | 2015-04-28 22:33:43 +0200 | [diff] [blame] | 131 | |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 132 | pmt::pmt_t msg_binary_blob = pmt::make_blob(new_header_plus_burst, sizeof(gsmtap_hdr)+BURST_SIZE); |
| 133 | pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob); |
Martin Jesper Low Madsen | af76964 | 2015-04-28 22:33:43 +0200 | [diff] [blame] | 134 | |
ptrkrysik | 18b631e | 2014-12-15 09:09:18 +0100 | [diff] [blame] | 135 | message_port_pub(pmt::mp("bursts"), msg_out); |
| 136 | } |
| 137 | return; |
| 138 | } |
| 139 | } /* namespace gsm */ |
| 140 | } /* namespace gr */ |