Option Explicit

#include Card.def
#Include EC-p.def
#include SHA.DEF
#include AES.DEF
#include OMAC.DEF

#IfDef MultiAppBasicCard
  #Include COMPONNT.DEF
  Dir "\"
    Application "DefaultApp" 
      Lock=Execute:Always
  End Dir
#EndIf

' Generator pro NIST P512
EEPROM G512N$ = Chr$(&H00,&HC6,&H85,&H8E,&H06,&HB7,&H04,&H04,&HE9,&HCD,&H9E,&H3E,&HCB,&H66,&H23,&H95,&HB4,&H42,&H9C,&H64,&H81,&H39,&H05,&H3F,&HB5,&H21,&HF8,&H28,&HAF,&H60,&H6B,&H4D,&H3D,&HBA,&HA1,&H4B,&H5E,&H77,&HEF,&HE7,&H59,&H28,&HFE,&H1D,&HC1,&H27,&HA2,&HFF,&HA8,&HDE,&H33,&H48,&HB3,&HC1,&H85,&H6A,&H42,&H9B,&HF9,&H7E,&H7E,&H31,&HC2,&HE5,&HBD,&H66,&H01,&H18,&H39,&H29,&H6A,&H78,&H9A,&H3B,&HC0,&H04,&H5C,&H8A,&H5F,&HB4,&H2C,&H7D,&H1B,&HD9,&H98,&HF5,&H44,&H49,&H57,&H9B,&H44,&H68,&H17,&HAF,&HBD,&H17,&H27,&H3E,&H66,&H2C,&H97,&HEE,&H72,&H99,&H5E,&HF4,&H26,&H40,&HC5,&H50,&HB9,&H01,&H3F,&HAD,&H07,&H61,&H35,&H3C,&H70,&H86,&HA2,&H72,&HC2,&H40,&H88,&HBE,&H94,&H76,&H9F,&HD1,&H66,&H50)
' Generator pro Brainpool 512r1
EEPROM G512B$ = Chr$(&H81,&HAE,&HE4,&HBD,&HD8,&H2E,&HD9,&H64,&H5A,&H21,&H32,&H2E,&H9C,&H4C,&H6A,&H93,&H85,&HED,&H9F,&H70,&HB5,&HD9,&H16,&HC1,&HB4,&H3B,&H62,&HEE,&HF4,&HD0,&H09,&H8E,&HFF,&H3B,&H1F,&H78,&HE2,&HD0,&HD4,&H8D,&H50,&HD1,&H68,&H7B,&H93,&HB9,&H7D,&H5F,&H7C,&H6D,&H50,&H47,&H40,&H6A,&H5E,&H68,&H8B,&H35,&H22,&H09,&HBC,&HB9,&HF8,&H22,&H7D,&HDE,&H38,&H5D,&H56,&H63,&H32,&HEC,&HC0,&HEA,&HBF,&HA9,&HCF,&H78,&H22,&HFD,&HF2,&H09,&HF7,&H00,&H24,&HA5,&H7B,&H1A,&HA0,&H00,&HC5,&H5B,&H88,&H1F,&H81,&H11,&HB2,&HDC,&HDE,&H49,&H4A,&H5F,&H48,&H5E,&H5B,&HCA,&H4B,&HD8,&H8A,&H27,&H63,&HAE,&HD1,&HCA,&H2B,&H2F,&HA8,&HF0,&H54,&H06,&H78,&HCD,&H1E,&H0F,&H3A,&HD8,&H08,&H92)
' Generator pro NIST P256
EEPROM G256N$ = Chr$(&H6b,&H17,&Hd1,&Hf2,&He1,&H2c,&H42,&H47,&Hf8,&Hbc,&He6,&He5,&H63,&Ha4,&H40,&Hf2,&H77,&H03,&H7d,&H81,&H2d,&Heb,&H33,&Ha0,&Hf4,&Ha1,&H39,&H45,&Hd8,&H98,&Hc2,&H96,&H4f,&He3,&H42,&He2,&Hfe,&H1a,&H7f,&H9b,&H8e,&He7,&Heb,&H4a,&H7c,&H0f,&H9e,&H16,&H2b,&Hce,&H33,&H57,&H6b,&H31,&H5e,&Hce,&Hcb,&Hb6,&H40,&H68,&H37,&Hbf,&H51,&Hf5)

'Heslo
EEPROM heslo$ = "tajne heslo"

Public Test$, Temp$
Public xxa$, xxb$, xa$, s$,yya$, yyb$, ya$, K$, hash$, Gstr$, Klic$, Kenc$, Kmac$, Kmac2$

'Nastaveni krivky P512
Public CurveIndex%
CurveIndex% = 1

'Generovani nahodneho "s", nasledne zasifrovani pomoci AES. 
'AES 128b pro krivku P256, pro delsi krivky AES 256b
Command &H88 &H00 Getz(Lc=0, Data$)
  s$ = Hex$(Rnd) + Hex$(Rnd) + Hex$(Rnd)
  s$ = Mid$(s$,1,16)
  Test$ = s$
  If CurveIndex% = 17 Then
    hash$ = Sha256Hash(Chr$(&H00) + heslo$)
    hash$ = Mid$(hash$,1,16)
    Data$ = AES(128,hash$, s$)
  Else
    hash$ = Sha256Hash(Chr$(&H00) + heslo$)
    Data$ = AES(256,hash$, s$)
  End If
End Command

'Ziskani krivky
Command &H88 &H01 GetG(Lc=0, Data%)
 Data% = CurveIndex%
End Command

'Terminal posle Xb
Command &H88 &H02 SetXb(Data$, Disable LE)
  xxb$ = Data$
End Command

'Vypocet Xa
'Generace nahodneho xa, nacteni generatoru dle typu krivky, vypocet Xa, nasledne vypocet G'
Command &H88 &H03 GetXa(Lc=0, Data$)
  xa$ = Hex$(Rnd) + Hex$(Rnd)
  If CurveIndex% = 19 Then
    xxa$ = G512N$
  ElseIf CurveIndex% = 17 Then
    xxa$ = G256N$
  ElseIf CurveIndex% = 13 Then
    xxa$ = G512B$
  End If
  Call ECpMultiplyPoint (xxa$, xa$)
  Data$ = xxa$
  Temp$ = xxb$
  Call ECpMultiplyPoint (Temp$, xa$)
  If CurveIndex% = 19 Then
    Gstr$ = G512N$
  ElseIf CurveIndex% = 17 Then
    Gstr$ = G256N$
  ElseIf CurveIndex% = 13 Then
    Gstr$ = G512B$
  End If
  Call ECpMultiplyPoint (Gstr$, s$)
  Call EcpAddPoints(Gstr$,Temp$)
End Command

'Terminal posle Yb
Command &H88 &H04 SetYb(Data$, Disable LE)
  yyb$ = Data$
End Command

'Vypocet Ya
'Generace nahodneho ya, vypocet Ya a nasledne vypocet K
Command &H88 &H05 GetYa(Lc=0, Data$)
  ya$ = Hex$(Rnd) + Hex$(Rnd)
  yya$ = Gstr$
  Call ECpMultiplyPoint (yya$, ya$)
  Data$ = yya$
  K$ = yyb$
  Call ECpMultiplyPoint (K$, ya$)
End Command

'Vypocet Kmac' a nasledne otisk Ta
Command &H88 &H06 MACTa(LC=0, Data$)
  Kmac2$ = Sha256Hash(Chr$(&H03) + K$)
  Klic$ = Kmac2$
  Temp$ = yyb$ + CurveIndex% as String
  Data$ = OMAC(256,Klic$,Temp$)
End command

'Vypocet Kecn a Kmac, nasledne vypocet otisku Tb, pokud se shoduje s prijatym Tb
'vraci se odpoved 9000, jinak 9001
Command &H88 &H07 MACTb(Data$, Disable LE)
  Kenc$ = Sha256Hash(Chr$(&H01) + K$)
  Kmac$ = Sha256Hash(Chr$(&H02) + K$)
  Klic$ = Kmac2$
  Kmac2$ = yya$ + CurveIndex% as String
  Kmac2$ = OMAC(256,Klic$,Kmac2$)
  If Data$ = Kmac2$ Then 
  SW1SW2=&H9000
  Else SW1SW2=&H9001
End command

'Nastaveni krivky terminalem
Command &H88 &H08 SetCurve(Data%, Disable LE)
  CurveIndex% = Data%
  Call ECpSetCurve (CurveIndex%)
End Command








