Documentation
¶
Overview ¶
Package cryptoutils provides utility functions for binary and cryptographic operations. Translated from the provided Python implementation using only Go standard libraries.
Package sessionkey provides EMV common session key derivation per A1.3.1.
Index ¶
- Constants
- func CMAC(msg, ks []byte, s int) ([]byte, error)
- func CalculateMAC(msg, ks []byte, s, algo int) ([]byte, error)
- func CheckKeyParity(key []byte) bool
- func Chunk(b []byte, sz int) [][]byte
- func DeriveICCKey(imk []byte, pan, panSeq, option string) ([]byte, error)
- func DeriveSessionKey(km, r []byte) ([]byte, error)
- func ExtendDoubleToTripleKey(doubleKey []byte) ([]byte, error)
- func ExtendToDouble(singleKey []byte) []byte
- func FixKeyParity(key []byte) []byte
- func GenerateARPC10(issMKAC, arqc, arpcRc []byte, pan, psn string) ([]byte, error)
- func GenerateARPC18(issMKAC []byte, pan, psn string, atc []byte, arqc, csu, propAuthData []byte) ([]byte, error)
- func GenerateARPC22(issMKAC []byte, pan, psn string, atc []byte, arqc, csu, propAuthData []byte) ([]byte, error)
- func GenerateARQC10(issMKAC, data []byte, pan, psn string) ([]byte, error)
- func GenerateARQC18(issMKAC []byte, data []byte, atc []byte, pan, psn string) ([]byte, error)
- func GenerateARQC22(issMKAC []byte, data []byte, atc []byte, pan, psn string) ([]byte, error)
- func GenerateRandomKey(length int) ([]byte, error)
- func GetDigitsFromString(ct string, length int) string
- func GetVisaCVV(panHex, expDate, servCode string, cvkRaw []byte) ([]byte, error)
- func GetVisaPVV(accountNumber, keyIndex, pin string, pvkHex []byte) ([]byte, error)
- func Hexify(n int) (string, error)
- func KeyCV(keyHex []byte, kcvLen int) ([]byte, error)
- func NewECBDecrypter(b cipher.Block) cipher.BlockMode
- func NewECBEncrypter(b cipher.Block) cipher.BlockMode
- func ParityOf(x int) int
- func PrepareTripleDESKey(key []byte) []byte
- func Raw2B(raw []byte) []byte
- func Raw2Str(raw []byte) string
- func TruncateToSingle(doubleKey []byte) []byte
- func XOR(block1, block2 []byte) ([]byte, error)
- func XORBytes(a, b []byte) ([]byte, error)
Constants ¶
const ( ARQC_ARPC_LENGTH = 8 TRIPLE_DES_MODE = 3 ICC_KEY_DERIVATION_OPTION_A = "A" ICC_KEY_DERIVATION_OPTION_B = "B" ATC_PADDING_LENGTH = 6 ARPC_LENGTH_CVN18 = 4 ISSUER_MASTER_KEY_LENGTH = 16 )
const ( ISO9797_METHOD2_PADDING_BYTE = 0x80 KEY_LENGTH_SINGLE = 8 KEY_LENGTH_DOUBLE = 16 KEY_LENGTH_TRIPLE = 24 RANDOM_SEED_LENGTH = 32 PVV_PAN_LENGTH = 11 PVV_LENGTH = 4 EXP_DATE_LENGTH = 4 CVK_LENGTH = 16 SERVICE_CODE_LENGTH = 3 CVV_DATA_LENGTH = 32 CVV_LENGTH = 3 PAN_MIN_LENGTH = 13 PAN_MAX_LENGTH = 19 CVV_PADDING_BYTE = "0" HEX_TO_DECIMAL_OFFSET = 10 XOR_BIT_FLIP = 1 DOUBLE_LENGTH_FACTOR = 2 )
Variables ¶
This section is empty.
Functions ¶
func CMAC ¶
CMAC computes an s-byte AES-CMAC (4 ≤ s ≤ 8) over msg using key ks. Implements ISO/IEC 9797-1 Algorithm 5 (CMAC).
func CalculateMAC ¶
CalculateMAC computes an s-byte MAC (4 ≤ s ≤ 8) over msg using ISO/IEC 9797-1 CBC-DES Method 1 or 3 (algo == 1 or 3). ks must be 8 bytes (single-DES) or 16 bytes (two-key DES: k1||k2). s is the truncation length in bytes msg is already padded data.
func CheckKeyParity ¶
CheckKeyParity returns true if every byte in key has ODD parity.
func DeriveICCKey ¶
DeriveICCKey derives the k-bit ICC Master Key (UDK) per EMV A1.4 (Option A, B or C).
func DeriveSessionKey ¶
DeriveSessionKey derives an EMV session key KS from master key km and diversification data r, following Annex A1.3.1 (common session key option).
- km: ICC Master Key (single, double or triple length)
- r: diversification data (n bytes, where n = block size: 8 for DES, 16 for AES)
For single-length km (len(km)==len(r)), returns E_km(r). For double-length (len(r)<len(km)<=2*len(r)), forms two variants f1,f2 and returns leftmost len(km) bytes of E_km(f1)||E_km(f2).
func ExtendDoubleToTripleKey ¶
ExtendDoubleToTripleKey extends a 16-byte double-length key to a 24-byte triple-length key (K1K2K1). This is a common way to form a TDEA keying option 1 key (K1, K2, K3) where K3=K1 from a double-length key (K1, K2).
func ExtendToDouble ¶
ExtendToDouble extends a single length key to double length by concatenating it with itself.
func FixKeyParity ¶
FixKeyParity sets each byte to have ODD parity (as required by DES).
func GenerateARPC10 ¶
GenerateARPC10 computes the 8-byte ARPC per Visa CVN10 (Method 1).
func GenerateARPC18 ¶
func GenerateARPC18( issMKAC []byte, pan, psn string, atc []byte, arqc, csu, propAuthData []byte, ) ([]byte, error)
GenerateARPC18 implements Visa CVN-18 ARPC (method 2). issMKAC: 16-byte Issuer Master Key for AC (DES key) pan, psn: ASCII PAN and PSN used for ICC MK derivation atc: 2-byte application transaction counter arqc: 8-byte ARQC csu: 4-byte card status update propAuthData: optional 0–8 bytes Proprietary Authentication Data.
func GenerateARPC22 ¶
func GenerateARPC22( issMKAC []byte, pan, psn string, atc []byte, arqc, csu, propAuthData []byte, ) ([]byte, error)
GenerateARPC22 implements Visa CVN-22 ARPC (method 2). issMKAC: 16-byte Issuer Master Key for AC (DES key) pan, psn: ASCII PAN and PSN used for ICC MK derivation atc: 2-byte application transaction counter arqc: 8-byte ARQC csu: 4-byte card status update propAuthData: optional 0–8 bytes Proprietary Authentication Data.
func GenerateARQC10 ¶
GenerateARQC10 computes the 8-byte ARQC per Visa CVN10 algorithm. issMKAC: Issuer Master Key for AC (16-byte DES key). data: concatenated tag data in the proper order. pan, psn: ASCII PAN and PSN used for ICC MK derivation. Uses ISO7816-4 padding and DES3-CBC with zero IV.
func GenerateARQC18 ¶
GenerateARQC18 implements Visa CVN-18 ARQC calculation. issMKAC: 16-byte Issuer Master Key for AC (DES key) pan, psn: ASCII PAN and PSN used for ICC MK derivation atc: 2-byte application transaction counter data: concatenated tag data in EMV order (9F02..9F10).
func GenerateARQC22 ¶
GenerateARQC22 implements Visa CVN-22 ARQC calculation. issMKAC: 16-byte Issuer Master Key for AC (DES key) pan, psn: ASCII PAN and PSN used for ICC MK derivation atc: 2-byte application transaction counter data: concatenated tag data in EMV order (9F02..9F10).
func GenerateRandomKey ¶
GenerateRandomKey generates a cryptographically secure random key of specified length. Length must be 8 (single), 16 (double), or 24 (triple) bytes.
func GetDigitsFromString ¶
GetDigitsFromString extracts up to 'length' decimal digits from a hex string, applying a second pass on non-decimal hex chars if needed.
func GetVisaCVV ¶
GetVisaCVV calculates the CVV for a given set of card data and a CVK. panHex: Primary Account Number as a hex string. expDate: Expiration date in YYMM format. servCode: Service code, 3 digits. cvkRaw: The raw Card Verification Key bytes (must be 16 bytes for double-length key).
func GetVisaPVV ¶
GetVisaPVV generates a 4-digit PIN Verification Value (PVV) using 3DES ECB.
func NewECBDecrypter ¶
NewECBDecrypter returns a cipher.BlockMode for ECB decryption.
func NewECBEncrypter ¶
NewECBEncrypter returns a cipher.BlockMode for ECB encryption.
func PrepareTripleDESKey ¶
PrepareTripleDESKey extends double length key to triple length if needed.
func TruncateToSingle ¶
TruncateToSingle takes the first half of a double length key.
Types ¶
This section is empty.