blob: de02353c1fcf60cea4c16dee036b5e96fd53623d [file] [log] [blame]
ptrkrysik18b631e2014-12-15 09:09:18 +01001/* -*- c++ -*- */
Martin Jesper Low Madsenaf769642015-04-28 22:33:43 +02002/*
ptrkrysik18b631e2014-12-15 09:09:18 +01003 * Copyright 2014 <+YOU OR YOUR COMPANY+>.
Martin Jesper Low Madsenaf769642015-04-28 22:33:43 +02004 *
ptrkrysik18b631e2014-12-15 09:09:18 +01005 * 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 Madsenaf769642015-04-28 22:33:43 +02009 *
ptrkrysik18b631e2014-12-15 09:09:18 +010010 * 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 Madsenaf769642015-04-28 22:33:43 +020014 *
ptrkrysik18b631e2014-12-15 09:09:18 +010015 * 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>
ptrkrysik18b631e2014-12-15 09:09:18 +010026#include <grgsm/gsmtap.h>
ptrkrysik5817a792015-04-25 18:51:08 +020027#include <grgsm/endian.h>
ptrkrysik18b631e2014-12-15 09:09:18 +010028#include "decryption_impl.h"
29#include "a5_1_2.h"
30
ptrkrysik5817a792015-04-25 18:51:08 +020031
ptrkrysik18b631e2014-12-15 09:09:18 +010032const uint32_t BURST_SIZE=148;
33
34namespace 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 Madsenaf769642015-04-28 22:33:43 +020066
ptrkrysik18b631e2014-12-15 09:09:18 +010067 void decryption_impl::set_k_c(const std::vector<uint8_t> & k_c)
68 {
69 d_k_c = k_c;
70 }
Martin Jesper Low Madsenaf769642015-04-28 22:33:43 +020071
ptrkrysik18b631e2014-12-15 09:09:18 +010072 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 Madsenaf769642015-04-28 22:33:43 +020076 } else
ptrkrysik18b631e2014-12-15 09:09:18 +010077 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];
ptrkrysik4739c942015-02-07 19:51:03 +010084 uint8_t AtoBkeystream[114];
85 uint8_t BtoAkeystream[114];
ptrkrysik18b631e2014-12-15 09:09:18 +010086 uint8_t * keystream;
Martin Jesper Low Madsenaf769642015-04-28 22:33:43 +020087
ptrkrysik18b631e2014-12-15 09:09:18 +010088 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 Madsenaf769642015-04-28 22:33:43 +020091
ptrkrysik4739c942015-02-07 19:51:03 +010092 uint32_t frame_number = be32toh(header->frame_number);
93 bool uplink_burst = (be16toh(header->arfcn) & 0x4000) ? true : false;
ptrkrysik18b631e2014-12-15 09:09:18 +010094 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);
ptrkrysika1871f52014-12-15 09:38:00 +010099 runA51(AtoBkeystream, BtoAkeystream);
Martin Jesper Low Madsenaf769642015-04-28 22:33:43 +0200100
ptrkrysik18b631e2014-12-15 09:09:18 +0100101 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 }
ptrkrysika1871f52014-12-15 09:38:00 +0100112 //encrypt first part of the burst
ptrkrysik18b631e2014-12-15 09:09:18 +0100113 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 }
ptrkrysika1871f52014-12-15 09:38:00 +0100120 //encrypt second part of the burst
ptrkrysik18b631e2014-12-15 09:09:18 +0100121 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 Madsenaf769642015-04-28 22:33:43 +0200131
ptrkrysik18b631e2014-12-15 09:09:18 +0100132 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 Madsenaf769642015-04-28 22:33:43 +0200134
ptrkrysik18b631e2014-12-15 09:09:18 +0100135 message_port_pub(pmt::mp("bursts"), msg_out);
136 }
137 return;
138 }
139 } /* namespace gsm */
140} /* namespace gr */