blob: 60409c7b20545b65f9c1166248f3772ecb05db5d [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>
28#include "decryption_impl.h"
29#include "a5_1_2.h"
30
31const uint32_t BURST_SIZE=148;
32
33namespace gr {
34 namespace gsm {
35
36 decryption::sptr
37 decryption::make(const std::vector<uint8_t> & k_c)
38 {
39 return gnuradio::get_initial_sptr
40 (new decryption_impl(k_c));
41 }
42
43 /*
44 * The private constructor
45 */
46 decryption_impl::decryption_impl(const std::vector<uint8_t> & k_c)
47 : gr::block("decryption",
48 gr::io_signature::make(0, 0, 0),
49 gr::io_signature::make(0, 0, 0))
50 {
51 set_k_c(k_c);
52
53// std::cout << "Be careful with decryption block - it wasn't tested yet!" << std::endl;
54 message_port_register_in(pmt::mp("bursts"));
55 set_msg_handler(pmt::mp("bursts"), boost::bind(&decryption_impl::decrypt, this, _1));
56 message_port_register_out(pmt::mp("bursts"));
57 }
58
59 /*
60 * Virtual destructor
61 */
62 decryption_impl::~decryption_impl()
63 {
64 }
65
66 void decryption_impl::set_k_c(const std::vector<uint8_t> & k_c)
67 {
68 d_k_c = k_c;
69 }
70
71 void decryption_impl::decrypt(pmt::pmt_t msg)
72 {
73 if(d_k_c.size() != 8){
74 message_port_pub(pmt::mp("bursts"), msg);
75 } else
76 if(d_k_c[0] == 0 && d_k_c[1] == 0 && d_k_c[2] == 0 && d_k_c[3] == 0 &
77 d_k_c[4] == 0 && d_k_c[5] == 0 && d_k_c[6] == 0 && d_k_c[7] == 0)
78 {
79 message_port_pub(pmt::mp("bursts"), msg);
80 } else
81 {
82 uint8_t decrypted_data[BURST_SIZE];
83 uint8_t AtoBkeystream[15];
84 uint8_t BtoAkeystream[15];
85 uint8_t * keystream;
86
87 pmt::pmt_t header_plus_burst = pmt::cdr(msg);
88 gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(header_plus_burst);
89 uint8_t * burst_binary = (uint8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);
90
91 uint32_t frame_number = be32toh(header->frame_number) & 0x3fff;
92 bool uplink_burst = (be32toh(header->frame_number) & 0x4000) ? true : false;
93 uint32_t t1 = frame_number / (26*51);
94 uint32_t t2 = frame_number % 26;
95 uint32_t t3 = frame_number % 51;
96 uint32_t frame_number_mod = (t1 << 11) + (t3 << 5) + t2;
97 keysetup(&d_k_c[0], frame_number_mod);
ptrkrysika1871f52014-12-15 09:38:00 +010098 runA51(AtoBkeystream, BtoAkeystream);
ptrkrysik18b631e2014-12-15 09:09:18 +010099
100 if(uplink_burst){
101 //process uplink burst
102 keystream = BtoAkeystream;
103 } else {
104 //process downlink burst
105 keystream = AtoBkeystream;
106 }
107 /* guard bits */
108 for (int i = 0; i < 3; i++) {
109 decrypted_data[i] = burst_binary[i];
110 }
ptrkrysika1871f52014-12-15 09:38:00 +0100111 //encrypt first part of the burst
ptrkrysik18b631e2014-12-15 09:09:18 +0100112 for (int i = 0; i < 57; i++) {
113 decrypted_data[i+3] = keystream[i] ^ burst_binary[i+3];
114 }
115 /* stealing bits and midamble */
116 for (int i = 60; i < 88; i++) {
117 decrypted_data[i] = burst_binary[i];
118 }
ptrkrysika1871f52014-12-15 09:38:00 +0100119 //encrypt second part of the burst
ptrkrysik18b631e2014-12-15 09:09:18 +0100120 for (int i = 0; i < 57; i++) {
121 decrypted_data[i+88] = keystream[i+57] ^ burst_binary[i+88];
122 }
123 /* guard bits */
124 for (int i = 145; i < 148; i++) {
125 decrypted_data[i] = burst_binary[i];
126 }
127 uint8_t new_header_plus_burst[sizeof(gsmtap_hdr)+BURST_SIZE];
128 memcpy(new_header_plus_burst, header, sizeof(gsmtap_hdr));
129 memcpy(new_header_plus_burst+sizeof(gsmtap_hdr), decrypted_data, BURST_SIZE);
130
131 pmt::pmt_t msg_binary_blob = pmt::make_blob(new_header_plus_burst, sizeof(gsmtap_hdr)+BURST_SIZE);
132 pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);
133
134 message_port_pub(pmt::mp("bursts"), msg_out);
135 }
136 return;
137 }
138 } /* namespace gsm */
139} /* namespace gr */
140