Added support for A5/2, A5/3, A5/4. Issue #85
diff --git a/grc/decryption/gsm_decryption.xml b/grc/decryption/gsm_decryption.xml
index ae91408..4f3de22 100644
--- a/grc/decryption/gsm_decryption.xml
+++ b/grc/decryption/gsm_decryption.xml
@@ -3,15 +3,25 @@
   <name>Decryption</name>
   <key>gsm_decryption</key>
   <import>import grgsm</import>
-  <make>grgsm.decryption($k_c)</make>
+  <make>grgsm.decryption($k_c, $a5_version)</make>
 
   <param>
     <name>Kc session key</name>
     <key>k_c</key>
-    <value>[0,0,0,0,0,0,0,0]</value>    
+    <value>[0,0,0,0,0,0,0,0]</value>
     <type>int_vector</type>
   </param>
 
+  <param>
+    <name>A5 version</name>
+    <key>a5_version</key>
+    <value>1</value>
+    <type>int</type>
+  </param>
+
+  <check>$a5_version &gt; 0</check>
+  <check>$a5_version &lt; 5</check>
+
   <sink>
     <name>bursts</name>
     <type>message</type>
@@ -20,5 +30,5 @@
     <name>bursts</name>
     <type>message</type>
   </source>
-  
+
 </block>
diff --git a/include/grgsm/decryption/decryption.h b/include/grgsm/decryption/decryption.h
index be0f7cd..51328b1 100644
--- a/include/grgsm/decryption/decryption.h
+++ b/include/grgsm/decryption/decryption.h
@@ -1,14 +1,14 @@
 /* -*- c++ -*- */
-/* 
+/*
  *  @file
  *  @author Piotr Krysik <ptrkrysik@gmail.com>
  *  @section LICENSE
- *  
+ *
  *  Gr-gsm is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 3, or (at your option)
  *  any later version.
- *  
+ *
  *  Gr-gsm is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
@@ -18,7 +18,7 @@
  *  along with gr-gsm; see the file COPYING.  If not, write to
  *  the Free Software Foundation, Inc., 51 Franklin Street,
  *  Boston, MA 02110-1301, USA.
- * 
+ *
  */
 
 
@@ -50,8 +50,9 @@
        * class. gsm::decryption::make is the public interface for
        * creating new instances.
        */
-      static sptr make(const std::vector<uint8_t> & k_c);
+      static sptr make(const std::vector<uint8_t> & k_c, unsigned int a5_version);
       virtual void set_k_c(const std::vector<uint8_t> & k_c) = 0;
+      virtual void set_a5_version(unsigned int a5_version) = 0;
     };
 
   } // namespace gsm
diff --git a/lib/decryption/decryption_impl.cc b/lib/decryption/decryption_impl.cc
index 836d9d2..9e6112b 100644
--- a/lib/decryption/decryption_impl.cc
+++ b/lib/decryption/decryption_impl.cc
@@ -25,6 +25,7 @@
 #include <gnuradio/io_signature.h>
 #include <grgsm/gsmtap.h>
 #include <grgsm/endian.h>
+#include <numeric>
 #include "decryption_impl.h"
 
 extern "C" {
@@ -37,21 +38,24 @@
   namespace gsm {
 
     decryption::sptr
-    decryption::make(const std::vector<uint8_t> & k_c)
+    decryption::make(const std::vector<uint8_t> & k_c, unsigned int a5_version)
     {
       return gnuradio::get_initial_sptr
-        (new decryption_impl(k_c));
+        (new decryption_impl(k_c, a5_version));
     }
 
     /*
      * The private constructor
      */
-    decryption_impl::decryption_impl(const std::vector<uint8_t> & k_c)
+    decryption_impl::decryption_impl(const std::vector<uint8_t> & k_c, unsigned int a5_version)
       : gr::block("decryption",
               gr::io_signature::make(0, 0, 0),
-              gr::io_signature::make(0, 0, 0))
+              gr::io_signature::make(0, 0, 0)),
+        d_k_c_valid(false)
     {
         set_k_c(k_c);
+        set_a5_version(a5_version);
+        validate_k_c();
 
 //        std::cout << "Be careful with decryption block - it wasn't tested yet!" << std::endl;
         message_port_register_in(pmt::mp("bursts"));
@@ -68,29 +72,50 @@
 
     void decryption_impl::set_k_c(const std::vector<uint8_t> & k_c)
     {
-        if (k_c.size() == 8)
+        d_k_c = k_c;
+    }
+
+    void decryption_impl::set_a5_version(unsigned int a5_version)
+    {
+        d_a5_version = 1;
+        if (a5_version >= 1 && a5_version <= 4)
         {
-            for (int i=0; i<8; i++)
-            {
-                d_k_c[i] = k_c[i];
-            }
+            d_a5_version = a5_version;
+        }
+    }
+
+    void decryption_impl::validate_k_c()
+    {
+        if (d_k_c.size() == 0)
+        {
+            d_k_c_valid = false;
+            return;
+        }
+        else if ((d_a5_version < 4 && d_k_c.size() != 8) || (d_a5_version == 4 && d_k_c.size() != 16))
+        {
+            d_k_c_valid = false;
+            return;
         }
         else
         {
-            for (int i=0; i<8; i++)
+            for (int i=0; i<d_k_c.size(); i++)
             {
-                d_k_c[i] = 0;
+                if (d_k_c[i] != 0)
+                {
+                    d_k_c_valid = true;
+                    return;
+                }
             }
         }
     }
 
     void decryption_impl::decrypt(pmt::pmt_t msg)
     {
-        if(d_k_c[0] == 0 && d_k_c[1] == 0 && d_k_c[2] == 0 && d_k_c[3] == 0 &
-           d_k_c[4] == 0 && d_k_c[5] == 0 && d_k_c[6] == 0 && d_k_c[7] == 0)
+        if (!d_k_c_valid)
         {
             message_port_pub(pmt::mp("bursts"), msg);
-        } else
+        }
+        else
         {
             uint8_t decrypted_data[BURST_SIZE];
             uint8_t keystream[114];
@@ -104,10 +129,10 @@
 
             if(uplink_burst){
                 //process uplink burst
-                osmo_a5(1, d_k_c, frame_number, NULL, keystream);
+                osmo_a5(d_a5_version, &d_k_c[0], frame_number, NULL, keystream);
             } else {
                 //process downlink burst
-                osmo_a5(1, d_k_c, frame_number, keystream, NULL);
+                osmo_a5(d_a5_version, &d_k_c[0], frame_number, keystream, NULL);
             }
             /* guard bits */
             for (int i = 0; i < 3; i++) {
diff --git a/lib/decryption/decryption_impl.h b/lib/decryption/decryption_impl.h
index fff028b..68fbb9c 100644
--- a/lib/decryption/decryption_impl.h
+++ b/lib/decryption/decryption_impl.h
@@ -30,13 +30,16 @@
     class decryption_impl : public decryption
     {
      private:
-//      std::vector<uint8_t> d_k_c;
-      uint8_t d_k_c[8];
+      std::vector<uint8_t> d_k_c;
+      bool d_k_c_valid;
+      uint8_t d_a5_version;
       void decrypt(pmt::pmt_t msg);
+      void validate_k_c();
      public:
-      decryption_impl(const std::vector<uint8_t> & k_c);
+      decryption_impl(const std::vector<uint8_t> & k_c, unsigned int a5_version);
       ~decryption_impl();
       virtual void set_k_c(const std::vector<uint8_t> & k_c);
+      virtual void set_a5_version(unsigned int a5_version);
     };
   } // namespace gsm
 } // namespace gr