blob: 02837ea7c8bcd0c69d040b3b2a141419f83e154e [file] [log] [blame]
ptrkrysik18b631e2014-12-15 09:09:18 +01001/* -*- c++ -*- */
2/*
3 * Copyright 2014 <+YOU OR YOUR COMPANY+>.
4 *
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.
9 *
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.
14 *
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>
26#include <grgsm/endian.h>
27#include <grgsm/gsmtap.h>
ptrkrysik5817a792015-04-25 18:51:08 +020028#include <grgsm/endian.h>
ptrkrysik18b631e2014-12-15 09:09:18 +010029#include "decryption_impl.h"
30#include "a5_1_2.h"
31
ptrkrysik5817a792015-04-25 18:51:08 +020032
ptrkrysik18b631e2014-12-15 09:09:18 +010033const uint32_t BURST_SIZE=148;
34
35namespace gr {
36 namespace gsm {
37
38 decryption::sptr
39 decryption::make(const std::vector<uint8_t> & k_c)
40 {
41 return gnuradio::get_initial_sptr
42 (new decryption_impl(k_c));
43 }
44
45 /*
46 * The private constructor
47 */
48 decryption_impl::decryption_impl(const std::vector<uint8_t> & k_c)
49 : gr::block("decryption",
50 gr::io_signature::make(0, 0, 0),
51 gr::io_signature::make(0, 0, 0))
52 {
53 set_k_c(k_c);
54
55// std::cout << "Be careful with decryption block - it wasn't tested yet!" << std::endl;
56 message_port_register_in(pmt::mp("bursts"));
57 set_msg_handler(pmt::mp("bursts"), boost::bind(&decryption_impl::decrypt, this, _1));
58 message_port_register_out(pmt::mp("bursts"));
59 }
60
61 /*
62 * Virtual destructor
63 */
64 decryption_impl::~decryption_impl()
65 {
66 }
67
68 void decryption_impl::set_k_c(const std::vector<uint8_t> & k_c)
69 {
70 d_k_c = k_c;
71 }
72
73 void decryption_impl::decrypt(pmt::pmt_t msg)
74 {
75 if(d_k_c.size() != 8){
76 message_port_pub(pmt::mp("bursts"), msg);
77 } else
78 if(d_k_c[0] == 0 && d_k_c[1] == 0 && d_k_c[2] == 0 && d_k_c[3] == 0 &
79 d_k_c[4] == 0 && d_k_c[5] == 0 && d_k_c[6] == 0 && d_k_c[7] == 0)
80 {
81 message_port_pub(pmt::mp("bursts"), msg);
82 } else
83 {
84 uint8_t decrypted_data[BURST_SIZE];
ptrkrysik4739c942015-02-07 19:51:03 +010085 uint8_t AtoBkeystream[114];
86 uint8_t BtoAkeystream[114];
ptrkrysik18b631e2014-12-15 09:09:18 +010087 uint8_t * keystream;
88
89 pmt::pmt_t header_plus_burst = pmt::cdr(msg);
90 gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(header_plus_burst);
91 uint8_t * burst_binary = (uint8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);
92
ptrkrysik4739c942015-02-07 19:51:03 +010093 uint32_t frame_number = be32toh(header->frame_number);
94 bool uplink_burst = (be16toh(header->arfcn) & 0x4000) ? true : false;
ptrkrysik18b631e2014-12-15 09:09:18 +010095 uint32_t t1 = frame_number / (26*51);
96 uint32_t t2 = frame_number % 26;
97 uint32_t t3 = frame_number % 51;
98 uint32_t frame_number_mod = (t1 << 11) + (t3 << 5) + t2;
99 keysetup(&d_k_c[0], frame_number_mod);
ptrkrysika1871f52014-12-15 09:38:00 +0100100 runA51(AtoBkeystream, BtoAkeystream);
ptrkrysik18b631e2014-12-15 09:09:18 +0100101
102 if(uplink_burst){
103 //process uplink burst
104 keystream = BtoAkeystream;
105 } else {
106 //process downlink burst
107 keystream = AtoBkeystream;
108 }
109 /* guard bits */
110 for (int i = 0; i < 3; i++) {
111 decrypted_data[i] = burst_binary[i];
112 }
ptrkrysika1871f52014-12-15 09:38:00 +0100113 //encrypt first part of the burst
ptrkrysik18b631e2014-12-15 09:09:18 +0100114 for (int i = 0; i < 57; i++) {
115 decrypted_data[i+3] = keystream[i] ^ burst_binary[i+3];
116 }
117 /* stealing bits and midamble */
118 for (int i = 60; i < 88; i++) {
119 decrypted_data[i] = burst_binary[i];
120 }
ptrkrysika1871f52014-12-15 09:38:00 +0100121 //encrypt second part of the burst
ptrkrysik18b631e2014-12-15 09:09:18 +0100122 for (int i = 0; i < 57; i++) {
123 decrypted_data[i+88] = keystream[i+57] ^ burst_binary[i+88];
124 }
125 /* guard bits */
126 for (int i = 145; i < 148; i++) {
127 decrypted_data[i] = burst_binary[i];
128 }
129 uint8_t new_header_plus_burst[sizeof(gsmtap_hdr)+BURST_SIZE];
130 memcpy(new_header_plus_burst, header, sizeof(gsmtap_hdr));
131 memcpy(new_header_plus_burst+sizeof(gsmtap_hdr), decrypted_data, BURST_SIZE);
132
133 pmt::pmt_t msg_binary_blob = pmt::make_blob(new_header_plus_burst, sizeof(gsmtap_hdr)+BURST_SIZE);
134 pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);
135
136 message_port_pub(pmt::mp("bursts"), msg_out);
137 }
138 return;
139 }
140 } /* namespace gsm */
141} /* namespace gr */
142