Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可以用的,应用层测试代码 #3

Open
liweifoic opened this issue Nov 6, 2021 · 4 comments
Open

可以用的,应用层测试代码 #3

liweifoic opened this issue Nov 6, 2021 · 4 comments

Comments

@liweifoic
Copy link

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include "atsha204a_ioctl.h"

const char *filename = "/dev/atsha204a";

const uint8_t secret_key[32] = { 0x33, 0x77, 0x16, 0x22, 0x82, 0xde, 0xad, 0x8c,
0xe9, 0x14, 0x21, 0x87, 0xf5, 0x34, 0x6e, 0x55,
0xee, 0x42, 0x55, 0xd5, 0xff, 0x33, 0x3a, 0x40,
0x9a, 0xdf, 0xdb, 0x83, 0x55, 0x1b, 0xe2, 0x66
};

void print_hex(const char *str, const uint8_t *hex, const int len)
{
int i;

printf("%s : ", str);

for (i = 0; i < len; i++)
{
    if (i > 0)
    {
        printf(" ");
    }
    printf("0x%02X", hex[i]);
}

printf("\n");
printf("\r\n");

}

uint8_t serial_number[9] = { 0 };
int main()
{
int file;
int rc = 0;

static atsha204a_cmd_sn_t sn;
static atsha204a_cmd_mac_t mac;
static atsha204a_cmd_otp_t otp;
static uint16_t random_number = 0;
static atsha204a_cmd_nonce_t nonce;
static atsha204a_cmd_verify_key_t  verify_key;

if ((file = open(filename, O_RDWR)) < 0)
{
    /* ERROR HANDLING: you can check errno to see what went wrong */
    perror("Failed to open the i2c bus");
    exit(1);
}

if (ioctl(file, ATSHA204A_CMD_READ_SN, &sn) < 0)
{
    printf("ATSHA204A_CMD_READ_SN failed \n");
    rc = 1;
    goto close_exit;
}
print_hex("ATSHA204A_CMD_READ_SN Received data", (uint8_t *)sn.sn,
          sizeof(sn));

memset(&verify_key,0,sizeof(verify_key));
verify_key.slot=KEY_ID_0;
memcpy(verify_key.key, secret_key, sizeof(verify_key.key));

if (ioctl(file, ATSHA204A_CMD_VERIFY_KEY, &verify_key) < 0) 
{
    printf("ATSHA204A_CMD_MAC failed \n");
    rc = 1;
    goto close_exit;
}

printf("ATSHA204A_CMD_VERIFY_KEY OK \n");

close_exit:

close(file);

return rc;

}

@liweifoic liweifoic changed the title 应用层测试代码 可以用的,应用层测试代码 Nov 6, 2021
@CZ052
Copy link

CZ052 commented Sep 23, 2022

verify_key.slot=KEY_ID_0; KEY_ID_0? is not defind

@LongZoz
Copy link

LongZoz commented Sep 25, 2023

#include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <linux/i2c-dev.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdint.h> #include "atsha204a_ioctl.h"

const char *filename = "/dev/atsha204a";

const uint8_t secret_key[32] = { 0x33, 0x77, 0x16, 0x22, 0x82, 0xde, 0xad, 0x8c, 0xe9, 0x14, 0x21, 0x87, 0xf5, 0x34, 0x6e, 0x55, 0xee, 0x42, 0x55, 0xd5, 0xff, 0x33, 0x3a, 0x40, 0x9a, 0xdf, 0xdb, 0x83, 0x55, 0x1b, 0xe2, 0x66 };

void print_hex(const char *str, const uint8_t *hex, const int len) { int i;

printf("%s : ", str);

for (i = 0; i < len; i++)
{
    if (i > 0)
    {
        printf(" ");
    }
    printf("0x%02X", hex[i]);
}

printf("\n");
printf("\r\n");

}

uint8_t serial_number[9] = { 0 }; int main() { int file; int rc = 0;

static atsha204a_cmd_sn_t sn;
static atsha204a_cmd_mac_t mac;
static atsha204a_cmd_otp_t otp;
static uint16_t random_number = 0;
static atsha204a_cmd_nonce_t nonce;
static atsha204a_cmd_verify_key_t  verify_key;

if ((file = open(filename, O_RDWR)) < 0)
{
    /* ERROR HANDLING: you can check errno to see what went wrong */
    perror("Failed to open the i2c bus");
    exit(1);
}

if (ioctl(file, ATSHA204A_CMD_READ_SN, &sn) < 0)
{
    printf("ATSHA204A_CMD_READ_SN failed \n");
    rc = 1;
    goto close_exit;
}
print_hex("ATSHA204A_CMD_READ_SN Received data", (uint8_t *)sn.sn,
          sizeof(sn));

memset(&verify_key,0,sizeof(verify_key));
verify_key.slot=KEY_ID_0;
memcpy(verify_key.key, secret_key, sizeof(verify_key.key));

if (ioctl(file, ATSHA204A_CMD_VERIFY_KEY, &verify_key) < 0) 
{
    printf("ATSHA204A_CMD_MAC failed \n");
    rc = 1;
    goto close_exit;
}

printf("ATSHA204A_CMD_VERIFY_KEY OK \n");

close_exit:

close(file);

return rc;

}

这个芯片在i2C总线上的地址 是多少呀

@liweifoic
Copy link
Author

liweifoic commented Oct 24, 2023 via email

@LongZoz
Copy link

LongZoz commented Dec 27, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants