Skip to content

Commit

Permalink
make it work
Browse files Browse the repository at this point in the history
  • Loading branch information
dangfan committed Dec 9, 2023
1 parent f4a3c06 commit 964d974
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 32 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ endif ()

add_subdirectory(canokey-crypto EXCLUDE_FROM_ALL)

if (DEFINED USBD_PRODUCT_STRING)
add_definitions(-DUSBD_PRODUCT_STRING="${USBD_PRODUCT_STRING}")
if (DEFINED PRODUCT_NAME)
add_definitions(-DUSBD_PRODUCT_STRING="${PRODUCT_NAME}")
endif ()

file(GLOB_RECURSE SRC src/*.c applets/*.c interfaces/*.c
Expand Down
1 change: 1 addition & 0 deletions applets/ctap/ctap.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <block-cipher.h>
#include <cbor.h>
#include <common.h>
#include <crypto-util.h>
#include <ctap.h>
#include <ctaphid.h>
#include <device.h>
Expand Down
1 change: 1 addition & 0 deletions applets/ctap/secret.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "secret.h"
#include <aes.h>
#include <block-cipher.h>
#include <crypto-util.h>
#include <ecc.h>
#include <fs.h>
#include <hmac.h>
Expand Down
1 change: 1 addition & 0 deletions applets/ctap/u2f.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
#include "u2f.h"
#include <apdu.h>
#include <crypto-util.h>
#include <device.h>
#include <ecc.h>
#include <fs.h>
Expand Down
2 changes: 1 addition & 1 deletion applets/oath/oath.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: Apache-2.0
#include <apdu.h>
#include <crypto-util.h>
#include <device.h>
#include <fs.h>
#include <hmac.h>
#include <inttypes.h>
#include <memzero.h>
#include <oath.h>
#include <rand.h>
Expand Down
3 changes: 2 additions & 1 deletion applets/piv/piv.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
#include <common.h>
#include <admin.h>
#include <common.h>
#include <crypto-util.h>
#include <des.h>
#include <device.h>
#include <ecc.h>
Expand Down
6 changes: 3 additions & 3 deletions include/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ void fm_csn_low(void);
* Disable FM chip by pull up CSN
*/
void fm_csn_high(void);
#if _NFC_CHIP == NFC_CHIP_FM11NC
#if NFC_CHIP == NFC_CHIP_FM11NC
void spi_transmit(uint8_t *buf, uint8_t len);
void spi_receive(uint8_t *buf, uint8_t len);
#elif _NFC_CHIP == NFC_CHIP_FM11NT
#elif NFC_CHIP == NFC_CHIP_FM11NT
void i2c_start(void);
void i2c_stop(void);
void scl_delay(void);
Expand Down Expand Up @@ -121,7 +121,7 @@ void fm_read_eeprom(uint16_t addr, uint8_t *buf, uint8_t len);
void fm_write_eeprom(uint16_t addr, const uint8_t *buf, uint8_t len);
void fm_read_fifo(uint8_t *buf, uint8_t len);
void fm_write_fifo(uint8_t *buf, uint8_t len);
#if _NFC_CHIP == NFC_CHIP_FM11NT
#if NFC_CHIP == NFC_CHIP_FM11NT
void fm11nt_read(uint16_t addr, uint8_t *buf, uint8_t len);
void fm11nt_write(uint16_t addr, const uint8_t *buf, uint8_t len);
uint8_t fm_crc8(const uint8_t *data, const uint8_t data_length);
Expand Down
4 changes: 2 additions & 2 deletions include/nfc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define NFC_CHIP_FM11NT 1
#define NFC_CHIP_NA -1

#if _NFC_CHIP == NFC_CHIP_FM11NC
#if NFC_CHIP == NFC_CHIP_FM11NC

#define REG_FIFO_FLUSH 0x1
#define REG_FIFO_WORDCNT 0x2
Expand All @@ -25,7 +25,7 @@

#define RF_STATE_MASK 0xE0

#elif _NFC_CHIP == NFC_CHIP_FM11NT
#elif NFC_CHIP == NFC_CHIP_FM11NT

#define FM_REG_USER_CFG0 0xFFE0
#define FM_REG_USER_CFG1 0xFFE1
Expand Down
41 changes: 20 additions & 21 deletions interfaces/NFC/fm.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,71 @@ static void device_delay_us(int us) {

uint8_t fm_read_reg(uint16_t reg) {
uint8_t val;
#if _NFC_CHIP == NFC_CHIP_FM11NC
#if NFC_CHIP == NFC_CHIP_FM11NC
fm_nss_low();
uint8_t addr = reg;
addr |= 0x20;
fm_transmit(&addr, 1);
fm_receive(&val, 1);
fm_nss_high();
#elif _NFC_CHIP == NFC_CHIP_FM11NT
#elif NFC_CHIP == NFC_CHIP_FM11NT
fm11nt_read(reg, &val, 1);
#endif
return val;
}

void fm_read_regs(uint16_t reg, uint8_t *buf, uint8_t len) {
#if _NFC_CHIP == NFC_CHIP_FM11NC
#if NFC_CHIP == NFC_CHIP_FM11NC
fm_nss_low();
uint8_t addr = reg;
addr |= 0x20;
fm_transmit(&addr, 1);
fm_receive(buf, len);
fm_nss_high();
#elif _NFC_CHIP == NFC_CHIP_FM11NT
#elif NFC_CHIP == NFC_CHIP_FM11NT
fm11nt_read(reg, buf, len);
#endif
}

void fm_write_reg(uint16_t reg, uint8_t val) {
#if _NFC_CHIP == NFC_CHIP_FM11NC
#if NFC_CHIP == NFC_CHIP_FM11NC
fm_nss_low();
uint8_t addr = reg;
fm_transmit(&addr, 1);
fm_transmit(&val, 1);
fm_nss_high();
#elif _NFC_CHIP == NFC_CHIP_FM11NT
#elif NFC_CHIP == NFC_CHIP_FM11NT
fm11nt_write(reg, &val, 1);
#endif
}

void fm_write_regs(uint16_t reg, const uint8_t *buf, uint8_t len) {
#if _NFC_CHIP == NFC_CHIP_FM11NC
#if NFC_CHIP == NFC_CHIP_FM11NC
fm_nss_low();
uint8_t addr = reg;
fm_transmit(&addr, 1);
fm_transmit(buf, len);
fm_nss_high();
#elif _NFC_CHIP == NFC_CHIP_FM11NT
#elif NFC_CHIP == NFC_CHIP_FM11NT
fm11nt_write(reg, buf, len);
#endif
}

void fm_read_eeprom(uint16_t addr, uint8_t *buf, uint8_t len) {
#if _NFC_CHIP == NFC_CHIP_FM11NC
#if NFC_CHIP == NFC_CHIP_FM11NC
fm_nss_low();
device_delay_us(100);
uint8_t data[2] = {0x60 | (addr >> 8), addr & 0xFF};
fm_transmit(data, 2);
fm_receive(buf, len);
fm_nss_high();
#elif _NFC_CHIP == NFC_CHIP_FM11NT
#elif NFC_CHIP == NFC_CHIP_FM11NT
fm11nt_read(addr, buf, len);
#endif
}

void fm_write_eeprom(uint16_t addr, const uint8_t *buf, uint8_t len) {
#if _NFC_CHIP == NFC_CHIP_FM11NC
#if NFC_CHIP == NFC_CHIP_FM11NC
fm_nss_low();
device_delay_us(100);
uint8_t data[2] = {0xCE, 0x55};
Expand All @@ -88,38 +88,38 @@ void fm_write_eeprom(uint16_t addr, const uint8_t *buf, uint8_t len) {
fm_transmit(data, 2);
fm_transmit(buf, len);
fm_nss_high();
#elif _NFC_CHIP == NFC_CHIP_FM11NT
#elif NFC_CHIP == NFC_CHIP_FM11NT
fm11nt_write(addr, buf, len);
device_delay(10);
#endif
}

void fm_read_fifo(uint8_t *buf, uint8_t len) {
#if _NFC_CHIP == NFC_CHIP_FM11NC
#if NFC_CHIP == NFC_CHIP_FM11NC
fm_nss_low();
uint8_t addr = 0xA0;
fm_transmit(&addr, 1);
fm_receive(buf, len);
fm_nss_high();
#elif _NFC_CHIP == NFC_CHIP_FM11NT
#elif NFC_CHIP == NFC_CHIP_FM11NT
fm11nt_read(FM_REG_FIFO_ACCESS, buf, len);
#endif
}

void fm_write_fifo(uint8_t *buf, uint8_t len) {
#if _NFC_CHIP == NFC_CHIP_FM11NC
#if NFC_CHIP == NFC_CHIP_FM11NC
fm_nss_low();
uint8_t addr = 0x80;
fm_transmit(&addr, 1);
fm_transmit(buf, len);
fm_nss_high();
#elif _NFC_CHIP == NFC_CHIP_FM11NT
#elif NFC_CHIP == NFC_CHIP_FM11NT
fm11nt_write(FM_REG_FIFO_ACCESS, buf, len);
#endif
}

void fm11_init(void) {
#if _NFC_CHIP == NFC_CHIP_FM11NC
#if NFC_CHIP == NFC_CHIP_FM11NC
uint8_t buf[7];
uint8_t data1[] = {0x44, 0x00, 0x04, 0x20};
uint8_t data2[] = {0x05, 0x72, 0x02, 0x00, 0xB3, 0x99, 0x00};
Expand All @@ -131,25 +131,24 @@ void fm11_init(void) {
fm_write_eeprom(0x3B0, data2, sizeof(data2));
fm_read_eeprom(0x3B0, buf, sizeof(data2));
} while (memcmp(data2, buf, sizeof(data2)) != 0);
#elif _NFC_CHIP == NFC_CHIP_FM11NT
#elif NFC_CHIP == NFC_CHIP_FM11NT
uint8_t crc_buffer[13];
const uint8_t user_cfg[] = {0x91, 0x82, 0x21, 0xCD};
const uint8_t atqa_sak[] = {0x44, 0x00, 0x04, 0x20};
fm_csn_low();
device_delay_us(200);
fm_write_regs(FM_EEPROM_USER_CFG0, user_cfg, sizeof(user_cfg));
fm_write_eeprom(FM_EEPROM_USER_CFG0, user_cfg, sizeof(user_cfg));
fm_write_eeprom(FM_EEPROM_ATQA, atqa_sak, sizeof(atqa_sak));
fm_read_eeprom(FM_EEPROM_SN, crc_buffer, 9);
DBG_MSG("SN: "); PRINT_HEX(crc_buffer, 9);
memcpy(crc_buffer + 9, atqa_sak, sizeof(atqa_sak));
const uint8_t crc8 = fm_crc8(crc_buffer, sizeof(crc_buffer));
fm_write_eeprom(FM_EEPROM_CRC8, &crc8, 1);
device_delay_us(1000);
fm_csn_high();
#endif
}

#if _NFC_CHIP == NFC_CHIP_FM11NT
#if NFC_CHIP == NFC_CHIP_FM11NT

#define I2C_ADDR 0x57

Expand Down
6 changes: 4 additions & 2 deletions interfaces/NFC/nfc.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ static void nfc_error_handler(int code) {
apdu_buffer_tx_size = 0;
last_sent = 0;
inf_sending = 0;

// TODO: reset
#if NFC_CHIP == NFC_CHIP_FM11NT
fm_write_reg(FM_REG_RF_TXEN, 0x77); // set NFC to IDLE
fm_write_reg(FM_REG_RESET_SILENCE, 0x55); // reset
#endif
}

static void do_nfc_send_frame(uint8_t prologue, uint8_t *data, uint8_t len) {
Expand Down

0 comments on commit 964d974

Please sign in to comment.