HostInerKey = ( Generator ^ Host_Rnd ) mod Modulus
must be : 0x2B
i have tryed
B4X:
Dim Generator As Long = 29 ' (0x1D)
Dim Host_Rnd As Long = 131 ' (0X83)
Dim Modulus As Long = 53 ' ( 0x35)
Dim TmpPower As Long = Power(Generator ,Host_Rnd)
Dim HostInterKey As Int = TmpPower Mod Modulus
My first thought is that 29^131 is way larger than a 2^64 Long, and thus the Power function is probably superimposing a Mod 2^64 that messes things up.
(written using 2^64 instead of 2^63-1 to make it visually easier to see the problem)
But when I do the calculation "manually" using Big Number Calculator I get 29. Ditto for calculating the Power multiply-by-multiply and doing the Mod after each multiply. How reliable is the "must be : 43" ?
It is switching the parameters around in the calculations, so the host is using the Slave generated Inter Key and Host_Rnd to validate and the Device is using the HostInterKey and Slave_RND, both of which give 0x2B or 43
I think you'll still have to do the Power-Mod calculation step-by-step to avoid exceeding capacity of Long. On bright side, you only need an integer type big enough to hold the maximum product of two unsigned 8-bit integers and so an Int will do the job.
B4X:
Dim Generator As Int = 0x1D
Dim Host_Rnd As Int = 0x53
Dim Modulus As Int = 0x35
Dim HostInterKey As Int = 1
For I = 1 To Host_Rnd
HostInterKey = (HostInterKey * Generator) Mod Modulus
Next
Log(HostInterKey)
I think you'll still have to do the Power-Mod calculation step-by-step to avoid exceeding capacity of Long. On bright side, you only need an integer type big enough to hold the maximum product of two unsigned 8-bit integers and so an Int will do the job.
B4X:
Dim Generator As Int = 0x1D
Dim Host_Rnd As Int = 0x53
Dim Modulus As Int = 0x35
Dim HostInterKey As Int = 1
For I = 1 To Host_Rnd
HostInterKey = (HostInterKey * Generator) Mod Modulus
Next
Log(HostInterKey)