// Analyse bytes commands
 //-----------------------------------------------------------------------------
 #include "cmdanalyse.h"
+#include "nonce2key/nonce2key.h"
 
 static int CmdHelp(const char *Cmd);
 
        return ~calcSumNibbleSub(bytes, len, mask);
 }
 
+// measuring LFSR maximum length
+int CmdAnalyseLfsr(const char *Cmd){
+
+    uint16_t start_state = 0;  /* Any nonzero start state will work. */
+    uint16_t lfsr = start_state;
+    //uint32_t period = 0;
+
+       uint8_t iv = param_get8ex(Cmd, 0, 0, 16);
+       uint8_t find = param_get8ex(Cmd, 1, 0, 16);
+       
+       printf("LEGIC LFSR IV 0x%02X: \n", iv);
+       printf(" bit# | lfsr | ^0x40 |  0x%02X ^ lfsr \n",find);
+       
+       for (uint8_t i = 0x01; i < 0x30; i += 1) {
+               //period = 0;
+               legic_prng_init(iv);
+               legic_prng_forward(i);
+               lfsr = legic_prng_get_bits(12);
+
+               printf(" %02X | %03X | %03X | %03X \n",i, lfsr, 0x40 ^ lfsr, find ^ lfsr);
+       }
+       return 0;
+}
 int CmdAnalyseLCR(const char *Cmd) {
        uint8_t data[50];
        char cmdp = param_getchar(Cmd, 0);
        return 0;
 }
 
+int CmdAnalyseA(const char *Cmd){
+       
+// uid(2e086b1a) nt(230736f6) par(0000000000000000) ks(0b0008000804000e) nr(000000000)
+// uid(2e086b1a) nt(230736f6) par(0000000000000000) ks(0e0b0e0b090c0d02) nr(000000001)
+// uid(2e086b1a) nt(230736f6) par(0000000000000000) ks(0e05060e01080b08) nr(000000002)
+       uint32_t uid = 0x2e086b1a, nt = 0x230736f6, nr = 0x000000001;
+       uint64_t ks_list = 0x0e0b0e0b090c0d02, r_key = 0;
+
+       nonce2key_ex(0, 0 , uid, nt, nr, ks_list, &r_key);
+
+       nr = 0x000000002;
+       ks_list = 0x0e05060e01080b08;
+       nonce2key_ex(0, 0 , uid, nt, nr, ks_list, &r_key);
+
+       printf("Found valid key: %012"llx" \n", r_key); 
+       return 0;
+}
+
+
 static command_t CommandTable[] = {
        {"help",        CmdHelp,            1, "This help"},
        {"lcr",         CmdAnalyseLCR,          1, "Generate final byte for XOR LRC"},
        {"chksum",      CmdAnalyseCHKSUM,       1, "Checksum with adding, masking and one's complement"},
        {"dates",       CmdAnalyseDates,        1, "Look for datestamps in a given array of bytes"},
        {"tea",         CmdAnalyseTEASelfTest,  1, "Crypto TEA test"},
+       {"lfsr",        CmdAnalyseLfsr,         1,      "LFSR tests"},
+       {"a",           CmdAnalyseA,            1,      "num bits test"},
        {NULL, NULL, 0, NULL}
 };
 
 
 //-----------------------------------------------------------------------------
-// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
+// Copyright (C) 2016 iceman <iceman at ...>
 //
 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
 // at your option, any later version. See the LICENSE.txt file for the text of
 #include "crc.h"
 #include "../common/iso15693tools.h"
 #include "tea.h"
+#include "../include/legic_prng.h"
 
 int usage_analyse_lcr(void);
 int usage_analyse_checksum(void);
 int CmdAnalyseDates(const char *Cmd);
 int CmdAnalyseCRC(const char *Cmd);
 int CmdAnalyseTEASelfTest(const char *Cmd);
+int CmdAnalyseLfsr(const char *Cmd);
 #endif
 
                }
                count += (p_odd - p->states[ODD_STATE]) * (p_even - p->states[EVEN_STATE]);
                if (found_odd && found_even) {
-                       PrintAndLog("Key Found after testing %lld (2^%1.1f) out of %lld (2^%1.1f) keys. ", 
+                       PrintAndLog("\nKey Found after testing %lld (2^%1.1f) out of %lld (2^%1.1f) keys. ", 
                                count,
                                log(count)/log(2), 
                                maximum_states,
                if (maximum_states == 0) return false; // prevent keyspace reduction error (2^-inf)
 
                PrintAndLog("Brute force phase starting.");
-               time_t start, end;
-               time(&start);
+
+//             clock_t time1 = clock();                
+               time_t start1, end1;
+               time(&start1);
                keys_found = 0;
                foundkey = 0;
 
                        pthread_join(threads[i], 0);
                }
 
-               time(&end);
-               unsigned long  elapsed_time = difftime(end, start);
+               time(&end1);
+               unsigned long elapsed_time = difftime(end1, start1);
+               // time1 = clock() - time1;
+               // if ( time1 > 0 ) {
+                       // ((float)time1)/CLOCKS_PER_SEC
+               // }
 
                if (keys_found && TestIfKeyExists(foundkey)) {
-                       PrintAndLog("Success! Tested %"PRIu32" states, found %u keys after %u seconds", total_states_tested, keys_found, elapsed_time);
+                       printf("ICE: %u | %u | %u \n", start1, end1, elapsed_time);
+                       PrintAndLog("Success! Found %u keys after %u seconds", keys_found, elapsed_time);
                        PrintAndLog("\nFound key: %012"PRIx64"\n", foundkey);
                        ret = true;
                } else {
                candidates = NULL;
        }
        return 0;
-}
-
-
+}
\ No newline at end of file