29#define ALIRO_URSK_LEN 32u
30#define ALIRO_KEY_BLOCK_LEN 160u
31#define ALIRO_URSK_OFFSET 128u
32#define ALIRO_SESSION_KEY_LEN 32u
33#define ALIRO_SHARED_SECRET_LEN 32u
34#define ALIRO_TXID_LEN 16u
35#define ALIRO_EC_PUBX_LEN 32u
36#define ALIRO_GCM_NONCE_LEN 12u
37#define ALIRO_GCM_TAG_LEN 16u
41#define ALIRO_IFACE_NFC 0x5Eu
42#define ALIRO_IFACE_BLE 0xC3u
45int aliro_crypto_init(
void);
48void aliro_crypto_ursk_from_block(
const uint8_t block[ALIRO_KEY_BLOCK_LEN],
49 uint8_t ursk[ALIRO_URSK_LEN]);
58void aliro_crypto_derive_z(
const uint8_t shared_secret[ALIRO_SHARED_SECRET_LEN],
59 const uint8_t txid[ALIRO_TXID_LEN], uint8_t z[32]);
65int aliro_crypto_derive_block(
const uint8_t z[32],
const uint8_t *salt,
size_t salt_len,
66 const uint8_t device_pub_x[ALIRO_EC_PUBX_LEN],
67 uint8_t block[ALIRO_KEY_BLOCK_LEN]);
74int aliro_crypto_derive_key32(
const uint8_t z[32],
const uint8_t *salt,
size_t salt_len,
75 const uint8_t device_pub_x[ALIRO_EC_PUBX_LEN], uint8_t out[32]);
83void aliro_crypto_split(
const uint8_t block[ALIRO_KEY_BLOCK_LEN],
int with_c,
84 uint8_t enc_key[ALIRO_SESSION_KEY_LEN],
85 uint8_t dec_key[ALIRO_SESSION_KEY_LEN], uint8_t ursk[ALIRO_URSK_LEN]);
113#define ALIRO_CRYPTOGRAM_SK_OFFSET 0u
114#define ALIRO_CRYPTOGRAM_LEN 64u
122int aliro_crypto_verify_cryptogram(
const uint8_t cryptogram_sk[ALIRO_SESSION_KEY_LEN],
123 const uint8_t *cryptogram,
size_t cryptogram_len,
124 uint8_t *plain_payload);
134struct aliro_secchan {
135 uint8_t enc_key[ALIRO_SESSION_KEY_LEN];
136 uint8_t dec_key[ALIRO_SESSION_KEY_LEN];
141void aliro_secchan_init(
struct aliro_secchan *sc,
const uint8_t enc_key[ALIRO_SESSION_KEY_LEN],
142 const uint8_t dec_key[ALIRO_SESSION_KEY_LEN]);
143void aliro_crypto_gcm_nonce(uint64_t direction, uint32_t counter,
144 uint8_t nonce[ALIRO_GCM_NONCE_LEN]);
148int aliro_secchan_seal(
struct aliro_secchan *sc,
const uint8_t *aad,
size_t aad_len,
149 const uint8_t *pt,
size_t pt_len, uint8_t *ct,
150 uint8_t tag[ALIRO_GCM_TAG_LEN]);
151int aliro_secchan_open(
struct aliro_secchan *sc,
const uint8_t *aad,
size_t aad_len,
152 const uint8_t *ct,
size_t ct_len,
const uint8_t tag[ALIRO_GCM_TAG_LEN],
166#define ALIRO_BLESK_OFFSET 96u
171int aliro_crypto_derive_ble_keys(
const uint8_t block[ALIRO_KEY_BLOCK_LEN],
const uint8_t *salt,
172 size_t salt_len, uint8_t ble_reader[ALIRO_SESSION_KEY_LEN],
173 uint8_t ble_device[ALIRO_SESSION_KEY_LEN]);
178int aliro_msg_seal(
struct aliro_secchan *sc,
const uint8_t *plain,
size_t plain_len, uint8_t *wire,
179 size_t wire_cap,
size_t *wire_len);
183int aliro_msg_open(
struct aliro_secchan *sc,
const uint8_t *wire,
size_t wire_len, uint8_t *plain,
184 size_t plain_cap,
size_t *plain_len);
198enum aliro_salt_type {
199 ALIRO_SALT_CRYPTOGRAM = 0,
200 ALIRO_SALT_SESSION = 1,
201 ALIRO_SALT_KPERSISTENT = 2
204#define ALIRO_SALT_MAX 256u
206int aliro_salt_build(
enum aliro_salt_type type,
const uint8_t txid[ALIRO_TXID_LEN],
207 const uint8_t span_s1[ALIRO_EC_PUBX_LEN],
208 const uint8_t reader_value[ALIRO_EC_PUBX_LEN],
const uint8_t reader_id[32],
209 uint8_t interface_byte, uint16_t proto_version, uint8_t exp_phase_type,
210 uint8_t user_auth_policy,
211 const uint8_t s3opt[ALIRO_EC_PUBX_LEN] ,
212 const uint8_t *a5_tlv,
size_t a5_tlv_len, uint8_t *out,
size_t *out_len);