"""
constants.py
"""

from binascii import unhexlify, hexlify
from base64 import b64encode as b64e
from base64 import b64decode as b64d

# NOTE: delay experiments
ARTIFICIAL_DELAY = 4.0
ARTIFICIAL_DELAY_INCREMENT = 0.5

# NOTE: estimated delays
NC_EKA_PERIOD  = 5.0   # sec
NC_EKA_TIMEOUT = 31.0  # sec

# NOTE: ECDH
POINT_LENS_BYTES = [31, 32, 33]

# NOTE: AES256 CBC
AES_ROUNDS = 14
AES_KEY_BYTES = 32
AES_KEY_BITS  = AES_KEY_BYTES * 8  ## 256
assert AES_KEY_BYTES % 8 == 0
assert AES_KEY_BITS % 8 == 0
AES_BLOCK_BYTES = 16  # Fixed
AES_BLOCK_BITS = AES_BLOCK_BYTES * 8  ## 128
assert AES_BLOCK_BYTES % 8 == 0
assert AES_BLOCK_BITS % 8 == 0
AES_IV_BYTES = AES_BLOCK_BYTES
AES_IV_BITS = AES_BLOCK_BITS

# NOTE: HMAC SHA256
HMAC_SHA256_KEY_BYTES = 32
HMAC_SHA256_KEY_BITS  = HMAC_SHA256_KEY_BYTES * 8
assert HMAC_SHA256_KEY_BYTES % 8 == 0
assert HMAC_SHA256_KEY_BITS % 8 == 0


# NOTE: ECDH with secp256r1 aka NIST P-256 (secp256r1)
EC_P = 0x00ffffffff00000001000000000000000000000000ffffffffffffffffffffffff;
EC_A = 0x00ffffffff00000001000000000000000000000000fffffffffffffffffffffffc;
EC_B = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b;
EC_XG_INT = 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296;
# EC_XG_BIN = unhexlify('6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296')
EC_XG_BIN = b'k\x17\xd1\xf2\xe1,BG\xf8\xbc\xe6\xe5c\xa4@\xf2w\x03}\x81-\xeb3\xa0\xf4\xa19E\xd8\x98\xc2\x96'
EC_YG_INT = 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5;
# EC_YG_BIN = unhexlify('4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5')
EC_YG_BIN = b'O\xe3B\xe2\xfe\x1a\x7f\x9b\x8e\xe7\xebJ|\x0f\x9e\x16+\xce3Wk1^\xce\xcb\xb6@h7\xbfQ\xf5'
EC_N =  0x00ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551;


EC_X2G_INT = 0x7CF27B188D034F7E8A52380304B51AC3C08969E277F21B35A60B48FC47669978;
EC_Y2G_INT = 0x07775510DB8ED040293D9AC69F7430DBBA7DADE63CE982299E04B79D227873D1;
EC_X2G_BIN = b'|\xf2{\x18\x8d\x03O~\x8aR8\x03\x04\xb5\x1a\xc3\xc0\x89i\xe2w\xf2\x1b5\xa6\x0bH\xfcGf\x99x'
EC_Y2G_BIN = b'\x07wU\x10\xdb\x8e\xd0@)=\x9a\xc6\x9ft0\xdb\xba}\xad\xe6<\xe9\x82)\x9e\x04\xb7\x9d"xs\xd1'

# NOTE: invalid points for NIST P-256 (secp256r1)
# https://web-in-security.blogspot.com/2015/09/practical-invalid-curve-attacks.html
EC_IX5 = 82794344854243450371984501721340198645022926339504713863786955730156937886079
EC_IY5 = 33552521881581467670836617859178523407344471948513881718969729275859461829010


X1_BT_ADDR = 'DC:53:60:04:93:35'
NEX_BTADDR = "48:59:29:01:AD:6F"
MOTO_BTADDR = "24:da:9b:66:9f:82"
PI2_BTADDR = "40:4E:36:A8:BF:5F"

SDP_UUID = {
    'PNP':             '1200',
    'L2CAP':           '0100',
}

P2P_STAR            = '!'   # 0x21
P2P_STAR_HEX         = b'!'.hex()
P2P_CLUSTER         = '"'   # 0x22
P2P_CLUSTER_HEX         = b'"'.hex()
P2P_POINT_TO_POINT  = 'TODO',

RFCOMM_PORT = [5, 8, 10]

# NOTE: modified version of md5(sid)
UUID_SID = "b8c1a306-9167-347e-b503-f0daba6c5723"

# NOTE: man ascii
NEWLINE = b'\x0a'
SUBSTITUTE = b'\x1a'
SPACE = b"\x20"
QUOTE = b"\x27"
ETB = b"\x17"
DOUBLE_QUOTE = b"\x22"
DOUBLE_QUOTE_ETB = b"\x22\x17"

EID_LEN =  4
AUTH_LEN = 5

NC_ERR_HEAD = b'\x08\x68\x12'

# NOTE: used both for KEP and AL
NC_HEAD2 = b'\x08\x01\x12'
CAES = b64e(NC_HEAD2)

NC_HEAD3 = b'\x08\x03*'

NC_HEAD4 = b'\x08\x04*'
NC_HEAD5 = b'\x08\x04\x1a'

NC_SEP = b'\x12'   # NOTE: aka form feed \f
NC_SEP2 = b'\x20'  # NOTE: aka space
NC_SEP3 = b'\x08'  # NOTE: aka space
NC_SEP4 = b'\x10'  # NOTE: aka dle

NC_KEP2_HEAD = b'\x08d'

NC_KEP3_HEAD = b'\x18d"'


NC_HEAD = b'x1c\x08\x01\x10\x02*'
NC_HEAD_PAY =  b'0'
NC_HEAD_EKA =  b'2\x04\x08\r\x10\x01\x12\x10'

NC_EKA_HEAD = unhexlify('0a08080112040805320010')
NC_EKA_LEN = 88
NC_EKA_TEMPLATE = unhexlify("000000540a300a1c080110022a103aebf352c5c1465377098ab7a2004bce3204080d100112102ddac9b3ec6158e8bed5621522997739122073b317233dc130288771e22d9f9313d8c9775ca9f465c25528ff3e9063942a59")
assert(len(NC_EKA_TEMPLATE) == NC_EKA_LEN)

NC_PAY_LEN = 120
NC_PT2_LEN = 37          # Bytes decoded with Pt2
NC_PT2_HEX_LEN = 37*2    # Hexdigits

# NOTE: always first packet from the adv to tell WLAN
NC_EWL_LEN = 104

# NOTE: always third packet from the adv to tell softAP
NC_EHA_LEN = 152

NC_EWD_LEN = 136

# NOTE: I'm using k1[0] from test_dissector.py
NC_KEP1_V2_TEMPLATE = unhexlify("0000001c08011218080112140a043044725612046e616d6520b1fbb8fa022802")
# NOTE: I'm using k1s[0] from test_dissector.py
NC_KEP1_STAR_HA_TEMPLATE = unhexlify("000000210801121d080112190a043475644112046e616d652086faa5702805280328022804")
NC_KEP1_STAR_WD_TEMPLATE = unhexlify("00000025080112210801121d0a046579565912046e616d6520fcbfebdef9ffffffff01280328022804")
# NOTE: I'm using k1c[0] from test_dissector.py
NC_KEP1_CLUS_TEMPLATE = unhexlify("000000200801121c080112180a043475644112046e616d6520ab92a9f806280528022804")

# NOTE: I'm using k1[1] from test_dissector.py
NC_KEP2_TEMPLATE = unhexlify("0000008808021283010801122090073f06afa1fee90af5336d04b00af6a366493151f60851245f8e7e59f10c3f1a44086412403c616654b5e51e5714489586655ea083881b8070673172036c3522c0e40e0838f3eed88a3911a243ac468ed1a6c5e314ea117066857b1f82738f284a07994e7022174145535f3235365f4342432d484d41435f534841323536")
NC_KEP2_LEN = 140
assert(len(NC_KEP2_TEMPLATE) == NC_KEP2_LEN)

# NOTE: I'm always using the 120 Bytes template, no need to adjust the len fields
NC_KEP3_TEMPLATE = unhexlify("0000007408031270080112208d83b8a9cd5cb2183cbc50f7fde33b435fe30fe7a94a905892d35562f1bff83318642248080112440a204840d302b4208deac7bf2e37f4ddce5974bbd2561ca2bc1c86e10cccf8eaa5d712207a2789ab50d9c1817c9503654c382926098f8c19e148bed81ff743249df687ed")
NC_KEP3_LEN = 120
assert(len(NC_KEP3_TEMPLATE) == NC_KEP3_LEN)

# NOTE: I'm using k1[3] from test_dissector.py
NC_KEP4_TEMPLATE = unhexlify("0000004e0804124a0a48080112440a207691b1f6a57371d7dad3bf154f34f91075ccabe7bbfcfcfd8c574757f5f59249122071e5a2993e2074113bc85aa04e84894ba50f29378971b8902c9e388ece763ba9")

# NOTE: I'm using r_ewl[0] from test_dissector.py
NC_EWL_TEMPLATE = unhexlify("000000640a400a1c080110022a105050fe126507dc579de7c105a44b8e423204080d1001122026a22e97d17157b42626cd459557049fa4ec5fbe82bd630a57a63ca11bc3debe12205d26af2adf1465ab60dad860c1e657552dc8354d764ffb4e76390e265d209dc9")
# NOTE: I'm using r_wl[0] from test_dissector.py
NC_WL_TEMPLATE = unhexlify("0a1a0801121608042a120801120e08051a0a0a04c0a8016410bfa1021001")
NC_WL_LEN = 30

# NOTE: Only with star I'm using _eha from test_dissector.py
NC_EHA_TEMPLATE = unhexlify("000000940a700a1c080110022a108f8aa8deca54eeb67fc67b5faf86702d3204080d100112504243cfb47cd8afd1b44783125d582f2a199c0c41d566a15790a4c0edfcbf46417b54d435650acbd2dc8b3c7a3c2d396f2ddf3383702c3fa8986968f57027c99f013c14e2f8c84433d57e53b090a047dd1220163b92bf47027294c8b4495dbf3583544d0f16e72db7e74b3c9c6573817be5e9")
# NOTE: I'm using ha from test_dissector.py
NC_HA_TEMPLATE = unhexlify("0a400801123c08042a3808011234080312300a1c72724f5575454e484a4a5f6f725151475f4376397270356554654848120c694e506d3343477655654e4c18bbac031003")
NC_HA_TEMPLATE = unhexlify("0a400801123c08042a3808011234080312300a1c53676144536f6c30586e61314e305f6e5f61424a632d71315243624e120c6d4c314231684c6a54726d3318b1dc031003")

NC_HA_LEN = 68  # hostapd

# NOTE: I'm using r_wd from test_dissector.py
NC_EWD_TEMPLATE = unhexlify("000000840a600a1c080110022a103f4c96b14cf8c5fa7ba1b70325b7bd833204080d100112404a380fbe38f5339dd6e6f0ed711a34d30288c51c3ec414f32c6da2513f431861af0fd45f2c794585f0bc4959cc00178dd8cf04adc9314f0733b5fa9c29f307cb12205f8a0414fc13aecbbe258037ae7357c0d4a9ad90f69d190cd8b1d23129d6e698")
NC_WD_TEMPLATE = unhexlify("0a350801123108042a2d08011229080312250a154449524543542d4b772d4d6f746f47335f653635371208533054627633333918dbe8021001")
NC_WD_LEN = 57  # direct

# NOTE: I'm using r_sh[0] from test_dissector.py
NC_SH_TEMPLATE =  unhexlify("0a0a0801120608042a0208021004")
NC_SH_LEN = 14
# NOTE: I'm using r_sh2[0] from test_dissector.py
NC_SH2_TEMPLATE = unhexlify("0a0a0801120608042a0208031004")
NC_SH2_LEN = 14

# NOTE: I'm using r_iw[0] from test_dissector.py
NC_IW_TEMPLATE = unhexlify("000000120801120e08042a0a08041a060a0444796831")
NC_IW_LEN = 22

# NOTE: I'm using pb[0] from test_dissector.py
NC_PAY_TEMPLATE = unhexlify("000000740a500a1c080110022a10354cba65f04068c8994b8bef204d37be3204080d100112302be4098b30f50d412a89b9399a816fca5f65759f9a21ed80930cd65a6ca7415fa354a9fc71ba2349e0df2fd50e3a74e912208438763733cbdef7730544cf8f162f7ba01b4a97f6e54d8c563cd2b8b5b518c8")
# NOTE: I'm using pb[1] from test_dissector.py
NC_PT_TEMPLATE = unhexlify("0a27080112230803221f0801120f0894aac89788fbd48a9001100118041a0a080010001a0473656e741002")
# NOTE: I'm using pb[2] from test_dissector.py
NC_PAY2_TEMPLATE = unhexlify("000000740a500a1c080110022a1000bce4bb44be88ec622445dd7ec27e503204080d10011230e13e2c8e9be9a79ee82c782e3c91eb92a0cef4805b056aac5f9e42d95437321f77ac93167b8213126915d419d2b8e7df122068ec513e3464ae1860de5b6d908162fbbfbe66d03907995c84cf12c62448c382")
# NOTE: I'm using pb[3] from test_dissector.py
NC_PT2_TEMPLATE = unhexlify("0a210801121d080322190801120f0894aac89788fbd48a9001100118041a04080110041003")

NC_KA_PRE_CON = unhexlify("000000080801120408053200")
NC_KA_LEN = 12
NC_KA_TEMPLATE = unhexlify("0a0808011204080532001001")
assert(len(NC_KA_TEMPLATE) == NC_KA_LEN)
NC_KA_TEMPLATE_STAR = unhexlify("0a0808011204080532001001")


NC_ACCEPT_CON = unhexlify("0000000a0801120608021a020800")
NC_REJECT_CON = unhexlify("0000000b0801120708021a0308c43e")

NC_HMAC_AES_I1 = unhexlify("cede08e7109e75b132839139a9e9c3d6478a1d347fa803742ba8aa558707becd")
NC_HMAC_AES_O1 = unhexlify("1aff4359a25958237e2dcd242bcb0a0110550616c58f432c8d2c9cb404a7d6c8")
NC_HMAC_AES_O2 = unhexlify("2bfcbd6fabf5a8725e33791a4420a6e5dcb7396780c8d104c457dde51dfa1dd5")

NC_HMAC_K1 = unhexlify("bf9d2a53c63616d75db0a7165b91c1ef73e537f2427405fa23610a4be657642e")
NC_HMAC_I1 = unhexlify("cede08e7109e75b132839139a9e9c3d6478a1d347fa803742ba8aa558707becd")
NC_HMAC_O1 = unhexlify("1aff4359a25958237e2dcd242bcb0a0110550616c58f432c8d2c9cb404a7d6c8")
# NOTE: HMAC_I3 = HEAD | iv | FOOT | ct
NC_HMAC_O2 = unhexlify("16458174defe48964ccdded8849392c0ee39143a8a8257a8246b0f3761cbfd30")

# NOTE: used for  enc, dec and mac
NC_KEY = unhexlify("bf9d2a53c63616d75db0a7165b91c1ef73e537f2427405fa23610a4be657642e")

# NOTE: KDF
NC_KDF_KEY = unhexlify("82aa55a0d397f88346ca1cee8d3909b95f13fa7deb1d4ab38376b8256da85510")


NC_STR_SecureMessageì = unhexlify("5365637572654d657373616765")
NC_STR_UKEY2v1auth    = unhexlify("554B4559322076312061757468")
NC_STR_UKEY2v1next    = unhexlify("554b455932207631206e657874")
# NOTE: see trace-nc.md
NC_KDF_INP_HEAD = '080212830108011220'
NC_KDF_SEP = '1a4408641240'
NC_KDF_AES_STR = '22174145535f3235365f4342432d484d41435f534841323536'  # "AES_256_CBC-HMAC_SHA256
NC_KDF_INP_FOOT = '0803127008011220768447b83e627078a8193ff80ae8d59e8f4b71fc6d978a67cac88844f70d31dc18642248080112440a206b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c29612204fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'

NC_STR_CLIENT = unhexlify("636c69656e74")
NC_STR_SERVER = unhexlify("736572766572")

NC_MAC_PRE_IV = "0a1c080110022a10"

NC_PT_TYPES = [
    # NOTE: encrypted
    'eka',
    'ewd',
    'eha',
    'esh', 'esh2',
    'ewl',
    'pay', 'pay2',
    # NOTE: not encrypted
    'kep1', 'kep2', 'kep3', 'kep4',
    'iw',
]

NC_MAC_POST_IV = {
    'eka':     "3204080d10011210",  #  88 Bytes
    'esh':     "3204080d10011210",  #  88 Bytes
    'esh2':    "3204080d10011210",  #  88 Bytes
    'ewl':     "3204080d10011220",  # 104 Bytes
    'pay':     "3204080d10011230",  # 120 Bytes
    'pay2':    "3204080d10011230",  # 120 Bytes
    'ewd':     "3204080d10011240",  # 136 Bytes
    'eha':     "3204080d10011250",  # 152 Bytes
}

# NOTE: CIPHER
NC_STR_SIG1 = unhexlify("5349473a31")
NC_STR_ENC2 = unhexlify("454e433a32")

BTNAME_SEP = "AAAAAAAAA"

WIFI_MODES = [ 'hostapd', 'direct' ]

"""
constants.py
"""

from binascii import unhexlify, hexlify
from base64 import b64encode as b64e
from base64 import b64decode as b64d

# NOTE: delay experiments
ARTIFICIAL_DELAY = 4.0
ARTIFICIAL_DELAY_INCREMENT = 0.5

# NOTE: estimated delays
NC_EKA_PERIOD  = 5.0   # sec
NC_EKA_TIMEOUT = 31.0  # sec

# NOTE: ECDH
POINT_LENS_BYTES = [31, 32, 33]

# NOTE: AES256 CBC
AES_ROUNDS = 14
AES_KEY_BYTES = 32
AES_KEY_BITS  = AES_KEY_BYTES * 8  ## 256
assert AES_KEY_BYTES % 8 == 0
assert AES_KEY_BITS % 8 == 0
AES_BLOCK_BYTES = 16  # Fixed
AES_BLOCK_BITS = AES_BLOCK_BYTES * 8  ## 128
assert AES_BLOCK_BYTES % 8 == 0
assert AES_BLOCK_BITS % 8 == 0
AES_IV_BYTES = AES_BLOCK_BYTES
AES_IV_BITS = AES_BLOCK_BITS

# NOTE: HMAC SHA256
HMAC_SHA256_KEY_BYTES = 32
HMAC_SHA256_KEY_BITS  = HMAC_SHA256_KEY_BYTES * 8
assert HMAC_SHA256_KEY_BYTES % 8 == 0
assert HMAC_SHA256_KEY_BITS % 8 == 0


# NOTE: ECDH with secp256r1 aka NIST P-256 (secp256r1)
EC_P = 0x00ffffffff00000001000000000000000000000000ffffffffffffffffffffffff;
EC_A = 0x00ffffffff00000001000000000000000000000000fffffffffffffffffffffffc;
EC_B = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b;
EC_XG_INT = 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296;
# EC_XG_BIN = unhexlify('6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296')
EC_XG_BIN = b'k\x17\xd1\xf2\xe1,BG\xf8\xbc\xe6\xe5c\xa4@\xf2w\x03}\x81-\xeb3\xa0\xf4\xa19E\xd8\x98\xc2\x96'
EC_YG_INT = 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5;
# EC_YG_BIN = unhexlify('4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5')
EC_YG_BIN = b'O\xe3B\xe2\xfe\x1a\x7f\x9b\x8e\xe7\xebJ|\x0f\x9e\x16+\xce3Wk1^\xce\xcb\xb6@h7\xbfQ\xf5'
EC_N =  0x00ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551;


EC_X2G_INT = 0x7CF27B188D034F7E8A52380304B51AC3C08969E277F21B35A60B48FC47669978;
EC_Y2G_INT = 0x07775510DB8ED040293D9AC69F7430DBBA7DADE63CE982299E04B79D227873D1;
EC_X2G_BIN = b'|\xf2{\x18\x8d\x03O~\x8aR8\x03\x04\xb5\x1a\xc3\xc0\x89i\xe2w\xf2\x1b5\xa6\x0bH\xfcGf\x99x'
EC_Y2G_BIN = b'\x07wU\x10\xdb\x8e\xd0@)=\x9a\xc6\x9ft0\xdb\xba}\xad\xe6<\xe9\x82)\x9e\x04\xb7\x9d"xs\xd1'

# NOTE: invalid points for NIST P-256 (secp256r1)
# https://web-in-security.blogspot.com/2015/09/practical-invalid-curve-attacks.html
EC_IX5 = 82794344854243450371984501721340198645022926339504713863786955730156937886079
EC_IY5 = 33552521881581467670836617859178523407344471948513881718969729275859461829010


X1_BT_ADDR = 'DC:53:60:04:93:35'
NEX_BTADDR = "48:59:29:01:AD:6F"
MOTO_BTADDR = "24:da:9b:66:9f:82"
PI2_BTADDR = "40:4E:36:A8:BF:5F"

SDP_UUID = {
    'PNP':             '1200',
    'L2CAP':           '0100',
}

P2P_STAR            = '!'   # 0x21
P2P_STAR_HEX         = b'!'.hex()
P2P_CLUSTER         = '"'   # 0x22
P2P_CLUSTER_HEX         = b'"'.hex()
P2P_POINT_TO_POINT  = 'TODO',

RFCOMM_PORT = [5, 8, 10]

# NOTE: modified version of md5(sid)
UUID_SID = "b8c1a306-9167-347e-b503-f0daba6c5723"

# NOTE: man ascii
NEWLINE = b'\x0a'
SUBSTITUTE = b'\x1a'
SPACE = b"\x20"
QUOTE = b"\x27"
ETB = b"\x17"
DOUBLE_QUOTE = b"\x22"
DOUBLE_QUOTE_ETB = b"\x22\x17"

EID_LEN =  4
AUTH_LEN = 5

NC_ERR_HEAD = b'\x08\x68\x12'

# NOTE: used both for KEP and AL
NC_HEAD2 = b'\x08\x01\x12'
CAES = b64e(NC_HEAD2)

NC_HEAD3 = b'\x08\x03*'

NC_HEAD4 = b'\x08\x04*'
NC_HEAD5 = b'\x08\x04\x1a'

NC_SEP = b'\x12'   # NOTE: aka form feed \f
NC_SEP2 = b'\x20'  # NOTE: aka space
NC_SEP3 = b'\x08'  # NOTE: aka space
NC_SEP4 = b'\x10'  # NOTE: aka dle

NC_KEP2_HEAD = b'\x08d'

NC_KEP3_HEAD = b'\x18d"'


NC_HEAD = b'x1c\x08\x01\x10\x02*'
NC_HEAD_PAY =  b'0'
NC_HEAD_EKA =  b'2\x04\x08\r\x10\x01\x12\x10'

NC_EKA_HEAD = unhexlify('0a08080112040805320010')
NC_EKA_LEN = 88
NC_EKA_TEMPLATE = unhexlify("000000540a300a1c080110022a103aebf352c5c1465377098ab7a2004bce3204080d100112102ddac9b3ec6158e8bed5621522997739122073b317233dc130288771e22d9f9313d8c9775ca9f465c25528ff3e9063942a59")
assert(len(NC_EKA_TEMPLATE) == NC_EKA_LEN)

NC_PAY_LEN = 120
NC_PT2_LEN = 37          # Bytes decoded with Pt2
NC_PT2_HEX_LEN = 37*2    # Hexdigits

# NOTE: always first packet from the adv to tell WLAN
NC_EWL_LEN = 104

# NOTE: always third packet from the adv to tell softAP
NC_EHA_LEN = 152

NC_EWD_LEN = 136

# NOTE: I'm using k1[0] from test_dissector.py
NC_KEP1_V2_TEMPLATE = unhexlify("0000001c08011218080112140a043044725612046e616d6520b1fbb8fa022802")
# NOTE: I'm using k1s[0] from test_dissector.py
NC_KEP1_STAR_HA_TEMPLATE = unhexlify("000000210801121d080112190a043475644112046e616d652086faa5702805280328022804")
NC_KEP1_STAR_WD_TEMPLATE = unhexlify("00000025080112210801121d0a046579565912046e616d6520fcbfebdef9ffffffff01280328022804")
# NOTE: I'm using k1c[0] from test_dissector.py
NC_KEP1_CLUS_TEMPLATE = unhexlify("000000200801121c080112180a043475644112046e616d6520ab92a9f806280528022804")

# NOTE: I'm using k1[1] from test_dissector.py
NC_KEP2_TEMPLATE = unhexlify("0000008808021283010801122090073f06afa1fee90af5336d04b00af6a366493151f60851245f8e7e59f10c3f1a44086412403c616654b5e51e5714489586655ea083881b8070673172036c3522c0e40e0838f3eed88a3911a243ac468ed1a6c5e314ea117066857b1f82738f284a07994e7022174145535f3235365f4342432d484d41435f534841323536")
NC_KEP2_LEN = 140
assert(len(NC_KEP2_TEMPLATE) == NC_KEP2_LEN)

# NOTE: I'm always using the 120 Bytes template, no need to adjust the len fields
NC_KEP3_TEMPLATE = unhexlify("0000007408031270080112208d83b8a9cd5cb2183cbc50f7fde33b435fe30fe7a94a905892d35562f1bff83318642248080112440a204840d302b4208deac7bf2e37f4ddce5974bbd2561ca2bc1c86e10cccf8eaa5d712207a2789ab50d9c1817c9503654c382926098f8c19e148bed81ff743249df687ed")
NC_KEP3_LEN = 120
assert(len(NC_KEP3_TEMPLATE) == NC_KEP3_LEN)

# NOTE: I'm using k1[3] from test_dissector.py
NC_KEP4_TEMPLATE = unhexlify("0000004e0804124a0a48080112440a207691b1f6a57371d7dad3bf154f34f91075ccabe7bbfcfcfd8c574757f5f59249122071e5a2993e2074113bc85aa04e84894ba50f29378971b8902c9e388ece763ba9")

# NOTE: I'm using r_ewl[0] from test_dissector.py
NC_EWL_TEMPLATE = unhexlify("000000640a400a1c080110022a105050fe126507dc579de7c105a44b8e423204080d1001122026a22e97d17157b42626cd459557049fa4ec5fbe82bd630a57a63ca11bc3debe12205d26af2adf1465ab60dad860c1e657552dc8354d764ffb4e76390e265d209dc9")
# NOTE: I'm using r_wl[0] from test_dissector.py
NC_WL_TEMPLATE = unhexlify("0a1a0801121608042a120801120e08051a0a0a04c0a8016410bfa1021001")
NC_WL_LEN = 30

# NOTE: Only with star I'm using _eha from test_dissector.py
NC_EHA_TEMPLATE = unhexlify("000000940a700a1c080110022a108f8aa8deca54eeb67fc67b5faf86702d3204080d100112504243cfb47cd8afd1b44783125d582f2a199c0c41d566a15790a4c0edfcbf46417b54d435650acbd2dc8b3c7a3c2d396f2ddf3383702c3fa8986968f57027c99f013c14e2f8c84433d57e53b090a047dd1220163b92bf47027294c8b4495dbf3583544d0f16e72db7e74b3c9c6573817be5e9")
# NOTE: I'm using ha from test_dissector.py
NC_HA_TEMPLATE = unhexlify("0a400801123c08042a3808011234080312300a1c72724f5575454e484a4a5f6f725151475f4376397270356554654848120c694e506d3343477655654e4c18bbac031003")
NC_HA_TEMPLATE = unhexlify("0a400801123c08042a3808011234080312300a1c53676144536f6c30586e61314e305f6e5f61424a632d71315243624e120c6d4c314231684c6a54726d3318b1dc031003")

NC_HA_LEN = 68  # hostapd

# NOTE: I'm using r_wd from test_dissector.py
NC_EWD_TEMPLATE = unhexlify("000000840a600a1c080110022a103f4c96b14cf8c5fa7ba1b70325b7bd833204080d100112404a380fbe38f5339dd6e6f0ed711a34d30288c51c3ec414f32c6da2513f431861af0fd45f2c794585f0bc4959cc00178dd8cf04adc9314f0733b5fa9c29f307cb12205f8a0414fc13aecbbe258037ae7357c0d4a9ad90f69d190cd8b1d23129d6e698")
NC_WD_TEMPLATE = unhexlify("0a350801123108042a2d08011229080312250a154449524543542d4b772d4d6f746f47335f653635371208533054627633333918dbe8021001")
NC_WD_LEN = 57  # direct

# NOTE: I'm using r_sh[0] from test_dissector.py
NC_SH_TEMPLATE =  unhexlify("0a0a0801120608042a0208021004")
NC_SH_LEN = 14
# NOTE: I'm using r_sh2[0] from test_dissector.py
NC_SH2_TEMPLATE = unhexlify("0a0a0801120608042a0208031004")
NC_SH2_LEN = 14

# NOTE: I'm using r_iw[0] from test_dissector.py
NC_IW_TEMPLATE = unhexlify("000000120801120e08042a0a08041a060a0444796831")
NC_IW_LEN = 22

# NOTE: I'm using pb[0] from test_dissector.py
NC_PAY_TEMPLATE = unhexlify("000000740a500a1c080110022a10354cba65f04068c8994b8bef204d37be3204080d100112302be4098b30f50d412a89b9399a816fca5f65759f9a21ed80930cd65a6ca7415fa354a9fc71ba2349e0df2fd50e3a74e912208438763733cbdef7730544cf8f162f7ba01b4a97f6e54d8c563cd2b8b5b518c8")
# NOTE: I'm using pb[1] from test_dissector.py
NC_PT_TEMPLATE = unhexlify("0a27080112230803221f0801120f0894aac89788fbd48a9001100118041a0a080010001a0473656e741002")
# NOTE: I'm using pb[2] from test_dissector.py
NC_PAY2_TEMPLATE = unhexlify("000000740a500a1c080110022a1000bce4bb44be88ec622445dd7ec27e503204080d10011230e13e2c8e9be9a79ee82c782e3c91eb92a0cef4805b056aac5f9e42d95437321f77ac93167b8213126915d419d2b8e7df122068ec513e3464ae1860de5b6d908162fbbfbe66d03907995c84cf12c62448c382")
# NOTE: I'm using pb[3] from test_dissector.py
NC_PT2_TEMPLATE = unhexlify("0a210801121d080322190801120f0894aac89788fbd48a9001100118041a04080110041003")

NC_KA_PRE_CON = unhexlify("000000080801120408053200")
NC_KA_LEN = 12
NC_KA_TEMPLATE = unhexlify("0a0808011204080532001001")
assert(len(NC_KA_TEMPLATE) == NC_KA_LEN)
NC_KA_TEMPLATE_STAR = unhexlify("0a0808011204080532001001")


NC_ACCEPT_CON = unhexlify("0000000a0801120608021a020800")
NC_REJECT_CON = unhexlify("0000000b0801120708021a0308c43e")

NC_HMAC_AES_I1 = unhexlify("cede08e7109e75b132839139a9e9c3d6478a1d347fa803742ba8aa558707becd")
NC_HMAC_AES_O1 = unhexlify("1aff4359a25958237e2dcd242bcb0a0110550616c58f432c8d2c9cb404a7d6c8")
NC_HMAC_AES_O2 = unhexlify("2bfcbd6fabf5a8725e33791a4420a6e5dcb7396780c8d104c457dde51dfa1dd5")

NC_HMAC_K1 = unhexlify("bf9d2a53c63616d75db0a7165b91c1ef73e537f2427405fa23610a4be657642e")
NC_HMAC_I1 = unhexlify("cede08e7109e75b132839139a9e9c3d6478a1d347fa803742ba8aa558707becd")
NC_HMAC_O1 = unhexlify("1aff4359a25958237e2dcd242bcb0a0110550616c58f432c8d2c9cb404a7d6c8")
# NOTE: HMAC_I3 = HEAD | iv | FOOT | ct
NC_HMAC_O2 = unhexlify("16458174defe48964ccdded8849392c0ee39143a8a8257a8246b0f3761cbfd30")

# NOTE: used for  enc, dec and mac
NC_KEY = unhexlify("bf9d2a53c63616d75db0a7165b91c1ef73e537f2427405fa23610a4be657642e")

# NOTE: KDF
NC_KDF_KEY = unhexlify("82aa55a0d397f88346ca1cee8d3909b95f13fa7deb1d4ab38376b8256da85510")


NC_STR_SecureMessageì = unhexlify("5365637572654d657373616765")
NC_STR_UKEY2v1auth    = unhexlify("554B4559322076312061757468")
NC_STR_UKEY2v1next    = unhexlify("554b455932207631206e657874")
# NOTE: see trace-nc.md
NC_KDF_INP_HEAD = '080212830108011220'
NC_KDF_SEP = '1a4408641240'
NC_KDF_AES_STR = '22174145535f3235365f4342432d484d41435f534841323536'  # "AES_256_CBC-HMAC_SHA256
NC_KDF_INP_FOOT = '0803127008011220768447b83e627078a8193ff80ae8d59e8f4b71fc6d978a67cac88844f70d31dc18642248080112440a206b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c29612204fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'

NC_STR_CLIENT = unhexlify("636c69656e74")
NC_STR_SERVER = unhexlify("736572766572")

NC_MAC_PRE_IV = "0a1c080110022a10"

NC_PT_TYPES = [
    # NOTE: encrypted
    'eka',
    'ewd',
    'eha',
    'esh', 'esh2',
    'ewl',
    'pay', 'pay2',
    # NOTE: not encrypted
    'kep1', 'kep2', 'kep3', 'kep4',
    'iw',
]

NC_MAC_POST_IV = {
    'eka':     "3204080d10011210",  #  88 Bytes
    'esh':     "3204080d10011210",  #  88 Bytes
    'esh2':    "3204080d10011210",  #  88 Bytes
    'ewl':     "3204080d10011220",  # 104 Bytes
    'pay':     "3204080d10011230",  # 120 Bytes
    'pay2':    "3204080d10011230",  # 120 Bytes
    'ewd':     "3204080d10011240",  # 136 Bytes
    'eha':     "3204080d10011250",  # 152 Bytes
}

# NOTE: CIPHER
NC_STR_SIG1 = unhexlify("5349473a31")
NC_STR_ENC2 = unhexlify("454e433a32")

BTNAME_SEP = "AAAAAAAAA"

WIFI_MODES = [ 'hostapd', 'direct' ]

constants.py # NOTE: delay experiments # NOTE: estimated delays # sec # sec # NOTE: ECDH # NOTE: AES256 CBC ## 256 # Fixed ## 128 # NOTE: HMAC SHA256 # NOTE: ECDH with secp256r1 aka NIST P-256 (secp256r1) # EC_XG_BIN = unhexlify('6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296') # EC_YG_BIN = unhexlify('4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5') # NOTE: invalid points for NIST P-256 (secp256r1) # https://web-in-security.blogspot.com/2015/09/practical-invalid-curve-attacks.html # 0x21 # 0x22 # NOTE: modified version of md5(sid) # NOTE: man ascii # NOTE: used both for KEP and AL # NOTE: aka form feed \f # NOTE: aka space # NOTE: aka space # NOTE: aka dle # Bytes decoded with Pt2 # Hexdigits # NOTE: always first packet from the adv to tell WLAN # NOTE: always third packet from the adv to tell softAP # NOTE: I'm using k1[0] from test_dissector.py # NOTE: I'm using k1s[0] from test_dissector.py # NOTE: I'm using k1c[0] from test_dissector.py # NOTE: I'm using k1[1] from test_dissector.py # NOTE: I'm always using the 120 Bytes template, no need to adjust the len fields # NOTE: I'm using k1[3] from test_dissector.py # NOTE: I'm using r_ewl[0] from test_dissector.py # NOTE: I'm using r_wl[0] from test_dissector.py # NOTE: Only with star I'm using _eha from test_dissector.py # NOTE: I'm using ha from test_dissector.py # hostapd # NOTE: I'm using r_wd from test_dissector.py # direct # NOTE: I'm using r_sh[0] from test_dissector.py # NOTE: I'm using r_sh2[0] from test_dissector.py # NOTE: I'm using r_iw[0] from test_dissector.py # NOTE: I'm using pb[0] from test_dissector.py # NOTE: I'm using pb[1] from test_dissector.py # NOTE: I'm using pb[2] from test_dissector.py # NOTE: I'm using pb[3] from test_dissector.py # NOTE: HMAC_I3 = HEAD | iv | FOOT | ct # NOTE: used for  enc, dec and mac # NOTE: KDF # NOTE: see trace-nc.md # "AES_256_CBC-HMAC_SHA256 # NOTE: encrypted # NOTE: not encrypted #  88 Bytes #  88 Bytes #  88 Bytes # 104 Bytes # 120 Bytes # 120 Bytes # 136 Bytes # 152 Bytes # NOTE: CIPHER