Esempio n. 1
0
File: gf_add.c Progetto: AleksMx/qfs
int main(int argc, char **argv)
{
  int hex, w;
  uint32_t a, b, c, top;
  uint64_t a64, b64, c64;
  uint64_t a128[2], b128[2], c128[2];
  char *format;

  if (argc != 4) usage(NULL);
  if (sscanf(argv[3], "%d", &w) == 0) usage("Bad w\n");

  if (w <= 0 || (w > 32 && w != 64 && w != 128)) usage("Bad w");

  hex = (strchr(argv[3], 'h') != NULL);

  if (!hex && w == 128) usage(NULL);
 
  if (w <= 32) {
    format = (hex) ? "%x" : "%u";
    if (sscanf(argv[1], format, &a) == 0) usage("Bad a\n");
    if (sscanf(argv[2], format, &b) == 0) usage("Bad b\n");

    if (w < 32) {
      top = (w == 31) ? 0x80000000 : (1 << w);
      if (w != 32 && a >= top) usage("a is too large\n");
      if (w != 32 && b >= top) usage("b is too large\n");
    }
  
    c = a ^ b;
    printf(format, c);
    printf("\n");

  } else if (w == 64) {
    format = (hex) ? "%llx" : "%llu";
    if (sscanf(argv[1], format, &a64) == 0) usage("Bad a\n");
    if (sscanf(argv[2], format, &b64) == 0) usage("Bad b\n");
    c64 = a64 ^ b64;

    printf(format, c64);
    printf("\n");

  } else if (w == 128) {

    if (read_128(argv[1], a128) == 0) usage("Bad a\n");
    if (read_128(argv[2], b128) == 0) usage("Bad b\n");
    c128[0] = a128[0] ^ b128[0];
    c128[1] = a128[1] ^ b128[1];

    print_128(c128);
  }
  exit(0);
}
Esempio n. 2
0
int main(void) {
    io_init();
    uint8_t input[16];
    aes128_ctx_t ks, iks;


    printf("Build Date: %s\n", build_date);
    printf("Git:        %s\n\n", build_git_sha);
    printf("  Photon DA AES\n");
    printf("-----------------\n");
    printf("ROUNDS  = %lu\n", (unsigned long) ROUNDS);
    printf("DELAY   = %lu\n", (unsigned long) DELAY);
    printf("STARTUP = %lu\n", (unsigned long) STARTUP);
    printf("-----------------\n");
    printf("SBOX:  0x%.2X\n", (unsigned int) aes_sbox);
    printf("Input: 0x%.2X\n", (unsigned int) input);
    printf("Key:   0x%.2X\n", (unsigned int) key);
    printf("-----------------\n");
#if ROUNDS == 0
    uint16_t writes = eeprom_read_word(&write_cycles);
    uint8_t byte = eeprom_read_byte(&input_byte);
    printf("write_cycles = %u\n", writes);
    printf("input_byte = 0x%.2X\n", byte);
#endif
#ifdef PHOTON
    printf("Computing S-Box only!\n");
#endif
    printf("\n");

#if STARTUP > 0
    _delay_ms(STARTUP * 1000);
#endif

#ifdef VERBOSE
    printf("Printing AES Key:\n");
    print_128(key);
    printf("\n");
#endif

    // aes128_init(const void* key, aes128_ctx_t* ctx);
    aes128_init(key, &ks);

#ifdef VERBOSE
    printf("Printing AES SBOX:\n");
    uint16_t s = 0;
    for(s=0; s < 256; s++) {
        printf("%.2X", aes_sbox[s]);
        if((s + 1) % 16 == 0)
            printf("\n");
    }
    printf("\n");
#endif

#if ROUNDS > 0
    unsigned long round = 1;
    uint8_t i=0;
    unsigned long total_rounds = 256 * ROUNDS;
    while(round <= total_rounds) {
        reset_input(input, i);
#else
    reset_input(input, byte);
    memcpy(&init,&input,sizeof(uint8_t)*16);
    printf("Input:\n");
    print_128(input);
    printf("\n");
    while(1) {
        memcpy(&input,&init,sizeof(uint8_t)*16);
#endif
        memcpy(&iks,&ks,sizeof(aes128_ctx_t));

#ifdef VERBOSE
#if ROUNDS > 0
        printf("[%6ld]: Input:\n", round);
#else
        printf("Input:\n");
#endif
        print_128(input);
        printf("\n");
#endif

        // void aes128_enc(void* buffer, aes128_ctx_t* ctx);
        aes128_enc(input, &ks);

#ifdef VERBOSE
#if ROUNDS  > 0
        printf("[%6ld]: Result:\n", round);
#else
        printf("Result:\n");
#endif

        //Result gets written back to the input
        print_128(input);
        printf("\n");
#endif

#if ROUNDS > 0
        round++;
        if ( (round - 1) % ROUNDS == 0)
            i++;
#endif
#if DELAY > 0
        _delay_ms(DELAY);
#endif
    }
#if ROUNDS  > 0
    printf("Exiting... after round %li\n", round - 1);
#endif

    return(0);
}