2 * Application layer <-> CCID layer data structure
8 uint8_t *cmd_apdu_head; /* CLS INS P1 P2 [ internal Lc ] */
9 uint8_t *cmd_apdu_data;
10 uint16_t cmd_apdu_data_len; /* Nc, calculated by Lc field */
11 uint16_t expected_res_size; /* Ne, calculated by Le field */
15 uint16_t res_apdu_data_len;
16 uint8_t *res_apdu_data;
19 extern struct apdu apdu;
21 #define CARD_CHANGE_INSERT 0
22 #define CARD_CHANGE_REMOVE 1
23 #define CARD_CHANGE_TOGGLE 2
24 void ccid_card_change_signal (int how);
25 void ccid_usb_reset (int);
28 #define EV_RX_DATA_READY 1 /* USB Rx data available */
29 #define EV_EXEC_FINISHED 2 /* OpenPGP Execution finished */
30 #define EV_TX_FINISHED 4 /* CCID Tx finished */
31 #define EV_CARD_CHANGE 8
32 #define EV_USB_SET_INTERFACE 16
33 #define EV_USB_DEVICE_RESET 32
35 /* OpenPGPcard thread */
36 #define EV_PINPAD_INPUT_DONE 1
38 #define EV_CMD_AVAILABLE 4
39 #define EV_VERIFY_CMD_AVAILABLE 8
40 #define EV_MODIFY_CMD_AVAILABLE 16
42 /* Maximum cmd apdu data is key import 24+4+256+256 (proc_key_import) */
43 #define MAX_CMD_APDU_DATA_SIZE (24+4+256+256) /* without header */
44 /* Maximum res apdu data is public key 5+9+512 (gpg_do_public_key) */
45 #define MAX_RES_APDU_DATA_SIZE (5+9+512) /* without trailer */
47 #define CCID_MSG_HEADER_SIZE 10
49 #define res_APDU apdu.res_apdu_data
50 #define res_APDU_size apdu.res_apdu_data_len
52 /* USB buffer size of LL (Low-level): size of single Bulk transaction */
53 #define USB_LL_BUF_SIZE 64
56 CCID_STATE_NOCARD, /* No card available */
57 CCID_STATE_START, /* Initial */
58 CCID_STATE_WAIT, /* Waiting APDU */
59 /* Busy1, Busy2, Busy3, Busy5 */
60 CCID_STATE_EXECUTE, /* Busy4 */
61 CCID_STATE_RECEIVE, /* APDU Received Partially */
62 CCID_STATE_SEND, /* APDU Sent Partially */
64 CCID_STATE_EXITED, /* ICC Thread Terminated */
65 CCID_STATE_EXEC_REQUESTED, /* Exec requested */
69 extern enum ccid_state *const ccid_state_p;
71 extern volatile uint8_t auth_status;
72 #define AC_NONE_AUTHORIZED 0x00
73 #define AC_PSO_CDS_AUTHORIZED 0x01 /* PW1 with 0x81 verified */
74 #define AC_OTHER_AUTHORIZED 0x02 /* PW1 with 0x82 verified */
75 #define AC_ADMIN_AUTHORIZED 0x04 /* PW3 verified */
77 #define AC_ALWAYS 0xFF
82 int gpg_pw_get_retry_counter (int who);
83 int gpg_pw_locked (uint8_t which);
84 void gpg_pw_reset_err_counter (uint8_t which);
85 void gpg_pw_increment_err_counter (uint8_t which);
87 int ac_check_status (uint8_t ac_flag);
88 int verify_pso_cds (const uint8_t *pw, int pw_len);
89 int verify_other (const uint8_t *pw, int pw_len);
90 int verify_user_0 (uint8_t access, const uint8_t *pw, int buf_len,
91 int pw_len_known, const uint8_t *ks_pw1, int saveks);
92 int verify_admin (const uint8_t *pw, int pw_len);
93 int verify_admin_0 (const uint8_t *pw, int buf_len, int pw_len_known,
94 const uint8_t *ks_pw3, int saveks);
96 void ac_reset_pso_cds (void);
97 void ac_reset_other (void);
98 void ac_reset_admin (void);
102 void set_res_sw (uint8_t sw1, uint8_t sw2);
103 extern const uint8_t historical_bytes[];
104 extern uint16_t data_objects_number_of_bytes;
106 #define CHALLENGE_LEN 32
108 void gpg_data_scan (const uint8_t *p);
109 void gpg_data_copy (const uint8_t *p);
110 void gpg_do_terminate (void);
111 void gpg_do_get_data (uint16_t tag, int with_tag);
112 void gpg_do_put_data (uint16_t tag, const uint8_t *data, int len);
113 void gpg_do_public_key (uint8_t kk_byte);
114 void gpg_do_keygen (uint8_t kk_byte);
116 const uint8_t *gpg_get_firmware_update_key (uint8_t keyno);
118 /* Constants: algo+size */
120 #define ALGO_NISTP256R1 1
121 #define ALGO_SECP256K1 2
122 #define ALGO_ED25519 3
123 #define ALGO_CURVE25519 4
124 #define ALGO_RSA2K 255
127 GPG_KEY_FOR_SIGNING = 0,
128 GPG_KEY_FOR_DECRYPTION,
129 GPG_KEY_FOR_AUTHENTICATION,
133 GPG_KEY_STORAGE = 0, /* PUBKEY + PRVKEY rounded to 2^N */
138 int gpg_get_algo_attr (enum kind_of_key kk);
139 int gpg_get_algo_attr_key_size (enum kind_of_key kk, enum size_of_key s);
141 const uint8_t *flash_init (void);
142 void flash_terminate (void);
143 void flash_activate (void);
144 void flash_init_keys (void);
145 void flash_do_release (const uint8_t *);
146 const uint8_t *flash_do_write (uint8_t nr, const uint8_t *data, int len);
147 uint8_t *flash_key_alloc (enum kind_of_key);
148 void flash_key_release (uint8_t *, int);
149 void flash_key_release_page (enum kind_of_key);
150 int flash_key_write (uint8_t *key_addr,
151 const uint8_t *key_data, int key_data_len,
152 const uint8_t *pubkey, int pubkey_len);
153 void flash_set_data_pool_last (const uint8_t *p);
154 void flash_clear_halfword (uint32_t addr);
155 void flash_increment_counter (uint8_t counter_tag_nr);
156 void flash_reset_counter (uint8_t counter_tag_nr);
158 #define FILEID_SERIAL_NO 0
159 #define FILEID_UPDATE_KEY_0 1
160 #define FILEID_UPDATE_KEY_1 2
161 #define FILEID_UPDATE_KEY_2 3
162 #define FILEID_UPDATE_KEY_3 4
163 #define FILEID_CH_CERTIFICATE 5
164 int flash_erase_binary (uint8_t file_id);
165 int flash_write_binary (uint8_t file_id, const uint8_t *data,
166 uint16_t len, uint16_t offset);
168 #define FLASH_CH_CERTIFICATE_SIZE 2048
170 /* Linker set these two symbols */
171 extern uint8_t ch_certificate_start;
173 #define FIRMWARE_UPDATE_KEY_CONTENT_LEN 256 /* RSA-2048 (p and q) */
175 #define INITIAL_VECTOR_SIZE 16
176 #define DATA_ENCRYPTION_KEY_SIZE 16
178 #define MAX_PRVKEY_LEN 512 /* Maximum is the case for RSA 4096-bit. */
181 const uint8_t *pubkey; /* Pointer to public key */
182 uint8_t data[MAX_PRVKEY_LEN]; /* decrypted private key data content */
189 uint8_t iv[INITIAL_VECTOR_SIZE];
193 uint8_t checksum_encrypted[DATA_ENCRYPTION_KEY_SIZE];
195 * DEK (Data Encryption Key) encrypted
197 uint8_t dek_encrypted_1[DATA_ENCRYPTION_KEY_SIZE]; /* For user */
198 uint8_t dek_encrypted_2[DATA_ENCRYPTION_KEY_SIZE]; /* For resetcode */
199 uint8_t dek_encrypted_3[DATA_ENCRYPTION_KEY_SIZE]; /* For admin */
203 #define BY_RESETCODE 2
207 * Maximum length of pass phrase is 127.
208 * We use the top bit (0x80) to encode if keystring is available within DO.
210 #define PW_LEN_MAX 127
211 #define PW_LEN_MASK 0x7f
212 #define PW_LEN_KEYSTRING_BIT 0x80
216 void s2k (const unsigned char *salt, size_t slen,
217 const unsigned char *input, size_t ilen, unsigned char output[32]);
219 #define KEYSTRING_PASSLEN_SIZE 1
220 #define KEYSTRING_SALT_SIZE SALT_SIZE
221 #define KEYSTRING_MD_SIZE 32
222 #define KEYSTRING_SIZE (KEYSTRING_PASSLEN_SIZE + KEYSTRING_SALT_SIZE \
224 #define KS_META_SIZE (KEYSTRING_PASSLEN_SIZE + KEYSTRING_SALT_SIZE)
225 #define KS_GET_SALT(ks) (ks + KEYSTRING_PASSLEN_SIZE)
226 #define KS_GET_KEYSTRING(ks) (ks + KS_META_SIZE)
228 void gpg_do_clear_prvkey (enum kind_of_key kk);
229 int gpg_do_load_prvkey (enum kind_of_key kk, int who, const uint8_t *keystring);
230 int gpg_do_chks_prvkey (enum kind_of_key kk,
231 int who_old, const uint8_t *old_ks,
232 int who_new, const uint8_t *new_ks);
234 int gpg_change_keystring (int who_old, const uint8_t *old_ks,
235 int who_new, const uint8_t *new_ks);
237 extern struct key_data kd[3];
240 void stdout_init (void);
243 * Debug functions in debug.c
245 void put_byte (uint8_t b);
246 void put_byte_with_no_nl (uint8_t b);
247 void put_short (uint16_t x);
248 void put_word (uint32_t x);
249 void put_int (uint32_t x);
250 void put_string (const char *s);
251 void put_binary (const char *s, int len);
253 #define DEBUG_INFO(msg) put_string (msg)
254 #define DEBUG_WORD(w) put_word (w)
255 #define DEBUG_SHORT(h) put_short (h)
256 #define DEBUG_BYTE(b) put_byte (b)
257 #define DEBUG_BINARY(s,len) put_binary ((const char *)s,len)
259 #define DEBUG_INFO(msg)
260 #define DEBUG_WORD(w)
261 #define DEBUG_SHORT(h)
262 #define DEBUG_BYTE(b)
263 #define DEBUG_BINARY(s,len)
266 int rsa_sign (const uint8_t *, uint8_t *, int, struct key_data *, int);
267 uint8_t *modulus_calc (const uint8_t *, int);
268 int rsa_decrypt (const uint8_t *, uint8_t *, int, struct key_data *,
270 int rsa_verify (const uint8_t *, int, const uint8_t *, const uint8_t *);
271 uint8_t *rsa_genkey (int);
273 int ecdsa_sign_p256r1 (const uint8_t *hash, uint8_t *output,
274 const uint8_t *key_data);
275 uint8_t *ecc_compute_public_p256r1 (const uint8_t *key_data);
276 int ecc_check_secret_p256r1 (const uint8_t *d0, uint8_t *d1);
277 int ecdh_decrypt_p256r1 (const uint8_t *input, uint8_t *output,
278 const uint8_t *key_data);
280 int ecdsa_sign_p256k1 (const uint8_t *hash, uint8_t *output,
281 const uint8_t *key_data);
282 uint8_t *ecc_compute_public_p256k1 (const uint8_t *key_data);
283 int ecc_check_secret_p256k1 (const uint8_t *d0, uint8_t *d1);
284 int ecdh_decrypt_p256k1 (const uint8_t *input, uint8_t *output,
285 const uint8_t *key_data);
287 int eddsa_sign_25519 (const uint8_t *input, size_t ilen, uint32_t *output,
288 const uint8_t *sk_a, const uint8_t *seed,
290 uint8_t *eddsa_compute_public_25519 (const uint8_t *a);
291 uint8_t *ecdh_compute_public_25519 (const uint8_t *a);
292 int ecdh_decrypt_curve25519 (const uint8_t *input, uint8_t *output,
293 const uint8_t *key_data);
295 const uint8_t *gpg_do_read_simple (uint8_t);
296 void gpg_do_write_simple (uint8_t, const uint8_t *, int);
297 void gpg_increment_digital_signature_counter (void);
300 void fatal (uint8_t code) __attribute__ ((noreturn));
301 #define FATAL_FLASH 1
302 #define FATAL_RANDOM 2
304 extern uint8_t keystring_md_pw3[KEYSTRING_MD_SIZE];
305 extern uint8_t admin_authorized;
307 /*** Flash memory tag values ***/
310 * Representation of data object:
312 * <-1 halfword-> <--len/2 halfwords->
313 * <-tag-><-len-> <---data content--->
315 #define NR_DO_SEX 0x00
316 #define NR_DO_FP_SIG 0x01
317 #define NR_DO_FP_DEC 0x02
318 #define NR_DO_FP_AUT 0x03
319 #define NR_DO_CAFP_1 0x04
320 #define NR_DO_CAFP_2 0x05
321 #define NR_DO_CAFP_3 0x06
322 #define NR_DO_KGTIME_SIG 0x07
323 #define NR_DO_KGTIME_DEC 0x08
324 #define NR_DO_KGTIME_AUT 0x09
325 #define NR_DO_LOGIN_DATA 0x0a
326 #define NR_DO_URL 0x0b
327 #define NR_DO_NAME 0x0c
328 #define NR_DO_LANGUAGE 0x0d
329 #define NR_DO_PRVKEY_SIG 0x0e
330 #define NR_DO_PRVKEY_DEC 0x0f
331 #define NR_DO_PRVKEY_AUT 0x10
332 #define NR_DO_KEYSTRING_PW1 0x11
333 #define NR_DO_KEYSTRING_RC 0x12
334 #define NR_DO_KEYSTRING_PW3 0x13
335 #define NR_DO__LAST__ 20 /* == 0x14 */
336 /* 14-bit counter for DS: Recorded in flash memory by 1-halfword (2-byte). */
338 * Representation of 14-bit counter:
344 #define NR_COUNTER_DS 0x80 /* ..0xbf */
345 /* 10-bit counter for DS: Recorded in flash memory by 1-halfword (2-byte). */
347 * Representation of 10-bit counter:
353 #define NR_COUNTER_DS_LSB 0xc0 /* ..0xc3 */
355 * Boolean object, small enum, or 8-bit integer:
356 * Recorded in flash memory by 1-halfword (2-byte)
359 * Representation of Boolean object:
360 * 0: No record in flash memory
363 #define NR_BOOL_PW1_LIFETIME 0xf0
365 * Representation of algorithm attribute object:
366 * RSA-2048: No record in flash memory
370 * ECC Ed25519: 0xf?03
371 * ECC Curve25519: 0xf?04
372 * where <?> == 1 (signature), 2 (decryption) or 3 (authentication)
374 #define NR_KEY_ALGO_ATTR_SIG 0xf1
375 #define NR_KEY_ALGO_ATTR_DEC 0xf2
376 #define NR_KEY_ALGO_ATTR_AUT 0xf3
378 * NR_UINT_SOMETHING could be here... Use 0xf[456789abcd]
380 /* 123-counters: Recorded in flash memory by 2-halfword (4-byte). */
382 * Representation of 123-counters:
383 * 0: No record in flash memory
387 * where <counter_id> is placed at second byte <??>
389 #define NR_COUNTER_123 0xfe
390 #define NR_EMPTY 0xff
392 #define SIZE_PW_STATUS_BYTES 7
395 #define NUM_ALL_PRV_KEYS 3 /* SIG, DEC and AUT */
397 #if !defined(OPENPGP_CARD_INITIAL_PW1)
398 #define OPENPGP_CARD_INITIAL_PW1 "123456"
401 #if !defined(OPENPGP_CARD_INITIAL_PW3)
402 #define OPENPGP_CARD_INITIAL_PW3 "12345678"
405 extern const uint8_t openpgpcard_aid[14];
407 void flash_bool_clear (const uint8_t **addr_p);
408 const uint8_t *flash_bool_write (uint8_t nr);
409 void flash_enum_clear (const uint8_t **addr_p);
410 const uint8_t *flash_enum_write (uint8_t nr, uint8_t v);
411 int flash_cnt123_get_value (const uint8_t *p);
412 void flash_cnt123_increment (uint8_t which, const uint8_t **addr_p);
413 void flash_cnt123_clear (const uint8_t **addr_p);
414 void flash_put_data (uint16_t hw);
415 void flash_warning (const char *msg);
417 void flash_put_data_internal (const uint8_t *p, uint16_t hw);
418 void flash_bool_write_internal (const uint8_t *p, int nr);
419 void flash_enum_write_internal (const uint8_t *p, int nr, uint8_t v);
420 void flash_cnt123_write_internal (const uint8_t *p, int which, int v);
421 void flash_do_write_internal (const uint8_t *p, int nr,
422 const uint8_t *data, int len);
424 extern const uint8_t gnuk_string_serial[];
426 #define LED_ONESHOT 1
427 #define LED_TWOSHOTS 2
428 #define LED_SHOW_STATUS 4
431 #define LED_GNUK_EXEC 32
432 #define LED_START_COMMAND 64
433 #define LED_FINISH_COMMAND 128
434 void led_blink (int spec);
436 #if defined(PINPAD_SUPPORT)
437 # if defined(PINPAD_CIR_SUPPORT)
438 void cir_init (void);
439 # elif defined(PINPAD_DIAL_SUPPORT)
440 void dial_sw_disable (void);
441 void dial_sw_enable (void);
442 # elif defined(PINPAD_DND_SUPPORT)
443 void msc_init (void);
444 void msc_media_insert_change (int available);
445 int msc_scsi_write (uint32_t lba, const uint8_t *buf, size_t size);
446 int msc_scsi_read (uint32_t lba, const uint8_t **sector_p);
447 void msc_scsi_stop (uint8_t code);
449 #define PIN_INPUT_CURRENT 1
450 #define PIN_INPUT_NEW 2
451 #define PIN_INPUT_CONFIRM 3
452 #define MAX_PIN_CHARS 32
453 extern uint8_t pin_input_buffer[MAX_PIN_CHARS];
454 extern uint8_t pin_input_len;
456 int pinpad_getline (int msg_code, uint32_t timeout_usec);
460 extern uint8_t _regnual_start, __heap_end__[];