M5Unit-NFC 0.1.0 git rev:93745b5
Loading...
Searching...
No Matches
m5::nfc::a::mifare::classic::Crypto1 Class Reference

Crypto1 for MIFARE Classic. More...

#include <mifare_classic_crypto1.hpp>

Inheritance diagram for m5::nfc::a::mifare::classic::Crypto1:

Public Member Functions

 Crypto1 () noexcept
 Default ctor (zero-initialized state)
 
 Crypto1 (const uint64_t key48) noexcept
 Construct with 48-bit key.
 
void init (const uint64_t key48) noexcept
 Initialize with 48-bit key.
 
uint32_t inject (uint32_t uid, uint32_t Nt, const bool encrypted=false) noexcept
 Inject UID and card nonce into the cipher state.
 
bool step_with (const bool in, const bool enc=false) noexcept
 Step the cipher with one input bit.
 
uint8_t step8 (const uint8_t in, const bool enc=false) noexcept
 Step the cipher with 8 input bits.
 
uint32_t step32 (const uint32_t in, const bool enc=false) noexcept
 Step the cipher with 32 input bits.
 
uint8_t encrypt (uint8_t buf[8], const uint32_t Nr, const uint32_t Ar) noexcept
 Encrypt reader nonce and authenticator response.
 
uint32_t encrypt (uint8_t *out, const uint8_t *in, const uint8_t in_len)
 Encrypt a byte buffer.
 
bool filter () const noexcept
 Crypto1 nonlinear filter.
 

Static Public Member Functions

static uint8_t oddparity8 (uint8_t x) noexcept
 Calculate odd parity for one byte.
 
static bool fa (bool a, bool b, bool c, bool d) noexcept
 Crypto1 filter helper A.
 
static bool fb (bool a, bool b, bool c, bool d) noexcept
 Crypto1 filter helper B.
 
static bool fc (bool a, bool b, bool c, bool d, bool e) noexcept
 Crypto1 filter helper C.
 

Public Attributes

uint32_t _count {}
 

Detailed Description

Crypto1 for MIFARE Classic.

Constructor & Destructor Documentation

◆ Crypto1()

m5::nfc::a::mifare::classic::Crypto1::Crypto1 ( const uint64_t key48)
inlineexplicitnoexcept

Construct with 48-bit key.

Parameters
key4848-bit MIFARE Classic key

Member Function Documentation

◆ encrypt() [1/2]

uint32_t m5::nfc::a::mifare::classic::Crypto1::encrypt ( uint8_t * out,
const uint8_t * in,
const uint8_t in_len )
inline

Encrypt a byte buffer.

Parameters
[out]outOutput buffer
inInput buffer
in_lenInput length, up to 32 bytes
Returns
Packed parity bits for the encrypted bytes

◆ encrypt() [2/2]

uint8_t m5::nfc::a::mifare::classic::Crypto1::encrypt ( uint8_t buf[8],
const uint32_t Nr,
const uint32_t Ar )
inlinenoexcept

Encrypt reader nonce and authenticator response.

Parameters
[out]bufOutput buffer at least 8 bytes
NrReader nonce
ArReader authenticator
Returns
Packed parity bits for the encrypted bytes

◆ fa()

static bool m5::nfc::a::mifare::classic::Crypto1::fa ( bool a,
bool b,
bool c,
bool d )
inlinestaticnoexcept

Crypto1 filter helper A.

Parameters
aInput bit a
bInput bit b
cInput bit c
dInput bit d
Returns
Filter helper result

◆ fb()

static bool m5::nfc::a::mifare::classic::Crypto1::fb ( bool a,
bool b,
bool c,
bool d )
inlinestaticnoexcept

Crypto1 filter helper B.

Parameters
aInput bit a
bInput bit b
cInput bit c
dInput bit d
Returns
Filter helper result

◆ fc()

static bool m5::nfc::a::mifare::classic::Crypto1::fc ( bool a,
bool b,
bool c,
bool d,
bool e )
inlinestaticnoexcept

Crypto1 filter helper C.

Parameters
aInput bit a
bInput bit b
cInput bit c
dInput bit d
eInput bit e
Returns
Filter helper result

◆ filter()

bool m5::nfc::a::mifare::classic::Crypto1::filter ( ) const
inlinenoexcept

Crypto1 nonlinear filter.

Returns
Current keystream bit

◆ init()

void m5::nfc::a::mifare::classic::Crypto1::init ( const uint64_t key48)
inlinenoexcept

Initialize with 48-bit key.

Parameters
key4848-bit MIFARE Classic key

◆ inject()

uint32_t m5::nfc::a::mifare::classic::Crypto1::inject ( uint32_t uid,
uint32_t Nt,
const bool encrypted = false )
inlinenoexcept

Inject UID and card nonce into the cipher state.

Parameters
uid32-bit UID value
NtCard nonce
encryptedTrue if the injected bits are encrypted
Returns
Generated 32-bit keystream value

◆ oddparity8()

static uint8_t m5::nfc::a::mifare::classic::Crypto1::oddparity8 ( uint8_t x)
inlinestaticnoexcept

Calculate odd parity for one byte.

Parameters
xInput byte
Returns
Parity bit that makes the byte odd parity

◆ step32()

uint32_t m5::nfc::a::mifare::classic::Crypto1::step32 ( const uint32_t in,
const bool enc = false )
inlinenoexcept

Step the cipher with 32 input bits.

Parameters
inInput 32-bit value
encTrue if the input value is encrypted
Returns
Generated 32-bit keystream value

◆ step8()

uint8_t m5::nfc::a::mifare::classic::Crypto1::step8 ( const uint8_t in,
const bool enc = false )
inlinenoexcept

Step the cipher with 8 input bits.

Parameters
inInput byte
encTrue if the input byte is encrypted
Returns
Generated 8-bit keystream value

◆ step_with()

bool m5::nfc::a::mifare::classic::Crypto1::step_with ( const bool in,
const bool enc = false )
inlinenoexcept

Step the cipher with one input bit.

Parameters
inInput bit
encTrue if the input bit is encrypted
Returns
Generated keystream bit before the state update