cryptnox-pos 1.0.0
Standalone USDC payment terminal firmware (ESP32 + Cryptnox smart card)
Loading...
Searching...
No Matches
card_status.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: LGPL-3.0-or-later
3 * Copyright (c) 2026 Cryptnox SA
4 */
5
31
32#ifndef CARD_STATUS_H
33#define CARD_STATUS_H
34
35#include <stddef.h>
36#include <stdint.h>
37
39#define CARD_SELECT_APDU { 0x00U, 0xA4U, 0x04U, 0x00U, 0x07U, \
40 0xA0U, 0x00U, 0x00U, 0x10U, 0x00U, 0x01U, 0x12U }
41
43#define CARD_FLAGS_OFF 5U
44
46#define CARD_SELECT_MIN 8U
47
48#define CARD_FLAG_PIN_SET 0x40U
49#define CARD_FLAG_SEEDED 0x20U
50
59
70static inline card_state_t card_state(const uint8_t *r, size_t n)
71{
72 if ((r == NULL) || (n < CARD_SELECT_MIN)) { return CARD_UNKNOWN; }
73 /* Not 0x9000 means the applet did not answer this SELECT, so there is no
74 * layout below to trust. */
75 if ((r[n - 2U] != 0x90U) || (r[n - 1U] != 0x00U)) { return CARD_UNKNOWN; }
76 /* A card type whose flags byte we have not been told about could have
77 * anything in bit 6, and reading it wrong here would refuse a card that
78 * works. 'B' is Basic, 'N' is NFT; both carry these two flags. */
79 if ((r[0] != (uint8_t)'B') && (r[0] != (uint8_t)'N')) { return CARD_UNKNOWN; }
80
81 const uint8_t flags = r[CARD_FLAGS_OFF];
82 if ((flags & CARD_FLAG_PIN_SET) == 0U) { return CARD_NO_PIN; }
83 if ((flags & CARD_FLAG_SEEDED) == 0U) { return CARD_NO_KEY; }
84 return CARD_READY;
85}
86
94static inline const char *card_state_text(card_state_t s)
95{
96 switch (s) {
97 case CARD_NO_PIN: return "Card not set up - initialise it first";
98 case CARD_NO_KEY: return "Card has no key - load a seed first";
99 default: return NULL;
100 }
101}
102
103#endif /* CARD_STATUS_H */
#define CARD_FLAG_PIN_SET
Definition card_status.h:48
#define CARD_SELECT_MIN
Shortest SELECT response this can read: flags plus the status word.
Definition card_status.h:46
card_state_t
What a tapped card is missing.
Definition card_status.h:52
@ CARD_READY
Definition card_status.h:53
@ CARD_UNKNOWN
Definition card_status.h:54
@ CARD_NO_KEY
Definition card_status.h:57
@ CARD_NO_PIN
Definition card_status.h:56
static const char * card_state_text(card_state_t s)
The line to put in front of the operator, or NULL if there is none.
Definition card_status.h:94
#define CARD_FLAGS_OFF
Offset of the flags byte in a SELECT response.
Definition card_status.h:43
#define CARD_FLAG_SEEDED
Definition card_status.h:49
static card_state_t card_state(const uint8_t *r, size_t n)
Judge a card from its SELECT response.
Definition card_status.h:70