cryptoutils

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 13 Imported by: 0

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

View Source
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
)
View Source
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

func CMAC(msg, ks []byte, s int) ([]byte, error)

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

func CalculateMAC(msg, ks []byte, s, algo int) ([]byte, error)

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

func CheckKeyParity(key []byte) bool

CheckKeyParity returns true if every byte in key has ODD parity.

func Chunk

func Chunk(b []byte, sz int) [][]byte

Chunk splits b into blocks of size sz. The last block may be shorter if needed.

func DeriveICCKey

func DeriveICCKey(imk []byte, pan, panSeq, option string) ([]byte, error)

DeriveICCKey derives the k-bit ICC Master Key (UDK) per EMV A1.4 (Option A, B or C).

func DeriveSessionKey

func DeriveSessionKey(km, r []byte) ([]byte, error)

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

func ExtendDoubleToTripleKey(doubleKey []byte) ([]byte, error)

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

func ExtendToDouble(singleKey []byte) []byte

ExtendToDouble extends a single length key to double length by concatenating it with itself.

func FixKeyParity

func FixKeyParity(key []byte) []byte

FixKeyParity sets each byte to have ODD parity (as required by DES).

func GenerateARPC10

func GenerateARPC10(issMKAC, arqc, arpcRc []byte, pan, psn string) ([]byte, error)

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

func GenerateARQC10(issMKAC, data []byte, pan, psn string) ([]byte, error)

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

func GenerateARQC18(
	issMKAC []byte,
	data []byte,
	atc []byte,
	pan, psn string,
) ([]byte, error)

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

func GenerateARQC22(
	issMKAC []byte,
	data []byte,
	atc []byte,
	pan, psn string,
) ([]byte, error)

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

func GenerateRandomKey(length int) ([]byte, error)

GenerateRandomKey generates a cryptographically secure random key of specified length. Length must be 8 (single), 16 (double), or 24 (triple) bytes.

func GetDigitsFromString

func GetDigitsFromString(ct string, length int) string

GetDigitsFromString extracts up to 'length' decimal digits from a hex string, applying a second pass on non-decimal hex chars if needed.

func GetVisaCVV

func GetVisaCVV(panHex, expDate, servCode string, cvkRaw []byte) ([]byte, error)

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

func GetVisaPVV(accountNumber, keyIndex, pin string, pvkHex []byte) ([]byte, error)

GetVisaPVV generates a 4-digit PIN Verification Value (PVV) using 3DES ECB.

func Hexify

func Hexify(n int) (string, error)

Hexify converts a non-negative integer to an even-length uppercase hex string.

func KeyCV

func KeyCV(keyHex []byte, kcvLen int) ([]byte, error)

func NewECBDecrypter

func NewECBDecrypter(b cipher.Block) cipher.BlockMode

NewECBDecrypter returns a cipher.BlockMode for ECB decryption.

func NewECBEncrypter

func NewECBEncrypter(b cipher.Block) cipher.BlockMode

NewECBEncrypter returns a cipher.BlockMode for ECB encryption.

func ParityOf

func ParityOf(x int) int

ParityOf returns 0 for even number of set bits, -1 for odd.

func PrepareTripleDESKey

func PrepareTripleDESKey(key []byte) []byte

PrepareTripleDESKey extends double length key to triple length if needed.

func Raw2B

func Raw2B(raw []byte) []byte

Raw2B returns the uppercase hex representation of raw data as bytes.

func Raw2Str

func Raw2Str(raw []byte) string

Raw2Str converts raw binary data to an uppercase hex string.

func TruncateToSingle

func TruncateToSingle(doubleKey []byte) []byte

TruncateToSingle takes the first half of a double length key.

func XOR

func XOR(block1, block2 []byte) ([]byte, error)

XOR takes two equal-length hex-encoded byte slices, XORs their raw bytes, and returns the result as uppercase hex bytes.

func XORBytes

func XORBytes(a, b []byte) ([]byte, error)

XORBytes returns a^b for equal-length slices. Returns error if lengths differ.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL