# 
# File:   keytool.py

# Based on the work of:
# Copyright (c) 2017, paul@discotd5.com

# Modified by:
# Copyright (c) 2017, xabiergarmendia@gmail.com

# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:

# 1. Redistributions of source code must retain the above copyright notice, this
   # list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
   # this list of conditions and the following disclaimer in the documentation
   # and/or other materials provided with the distribution.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# The views and conclusions contained in the software and documentation are those
# of the authors and should not be interpreted as representing official policies,
# either expressed or implied, of the FreeBSD Project.
#

#!/usr/bin/python

import sys

def printhelp():
    print "\nLand Rover Td5 Storm Engine ECU auth keygen"
    print "Usage:"
    print "\tGet key with seed (0-65535), example:"
    print "\t\tkeytool.py 1082"
    print "\t\tWill return key"
    exit()

if len(sys.argv)==2:
    if 0<=int(sys.argv[1])<=0xFFFF:

        seedin=int(sys.argv[1])
        seed=seedin
        
        count = ((seed >> 0xC & 0x8) + (seed >> 0x5 & 0x4) + (seed >> 0x3 & 0x2) + (seed & 0x1)) + 1

        for idx in range(0, count):
                tap = ((seed >> 1) ^ (seed >> 2 ) ^ (seed >> 8 ) ^ (seed >> 9)) & 1
                tmp = (seed >> 1) | ( tap << 0xF)
                if (seed >> 0x3 & 1) and (seed >> 0xD & 1):
                        seed = tmp & ~1
                else:
                        seed = tmp | 1

        #Calculate high and low bytes, for auth response      
        if (seed<256):
            high=0x00
            low=seed
        else:
            high=seed/256
            low=seed%256
            
        print "Seed: ",seedin," (",hex(seedin),") - Key: ",seed," (",hex(seed),") "
    else:
        printhelp()
else:
    printhelp()
@@ -1694,18 +1694,28 @@
 on%0A%0A
+from sys 
 import 
-sys
+argv
 %0A%0Ade
@@ -1847,16 +1847,20 @@
 -65535),
+ for
  example
@@ -1860,16 +1860,18 @@
 example:
+%5Cn
 %22%0A    pr
@@ -1913,11 +1913,62 @@
  %22%5Ct
-%5CtW
+or%22%0A    print %22%5Ct%5Ctkeytool.py 0x043A%5Cn%22%0A    print %22%5Ctw
 ill 
@@ -1979,16 +1979,21 @@
 rn key%22%0A
+    %0A
     exit
@@ -2003,20 +2003,16 @@
 %0Aif len(
-sys.
 argv)==2
@@ -2021,40 +2021,87 @@
     
-if 0%3C=int(sys.argv%5B1%5D)%3C=0xFFFF:%0A
+# %0A    if  argv%5B1%5D.startswith(%220x%22):%0A        seedin = int(argv%5B1%5D,16)%0A    else:
 %0A   
@@ -2115,25 +2115,23 @@
 edin
-=
+ = 
 int(
-sys.
 argv%5B1%5D)
 %0A   
@@ -2126,16 +2126,62 @@
 argv%5B1%5D)
+      %0A            %0A    if 0%3C=seedin%3C=0xFFFF:%0A
 %0A       
@@ -2711,105 +2711,24 @@
     
-if (seed%3C256):%0A            high=0x00%0A            low=seed%0A        else:%0A            high=seed/256
+high = seed %3E%3E 8
 %0A   
@@ -2736,24 +2736,24 @@
     
-    low=
+low = 
 seed
-%25256
+ & 255
 %0A   

Add handling for hex seeds. Simplify handling of high and low bytes.