#include "rijndael.h" #include #include typedef uint8_t sm_key_t[16]; typedef uint8_t sm_key256_t[32]; static const char * key_string = "2b7e1516 28aed2a6 abf71588 09cf4f3c"; static const char * k0_string = "7df76b0c 1ab899b3 3e42f047 b91b546f"; static const char * k1_string = "fbeed618 35713366 7c85e08f 7236a8de"; static const char * k2_string = "f7ddac30 6ae266cc f90bc11e e46d513b"; static const char * m0_string = ""; static const char * cmac_m0_string = "bb1d6929 e9593728 7fa37d12 9b756746"; static const char * m16_string = "6bc1bee2 2e409f96 e93d7e11 7393172a"; static const char * cmac_m16_string = "070a16b4 6b4d4144 f79bdd9d d04a287c"; static const char * m40_string = "6bc1bee2 2e409f96 e93d7e11 7393172a ae2d8a57 1e03ac9c 9eb76fac 45af8e51 30c81c46 a35ce411"; static const char * cmac_m40_string = "dfa66747 de9ae630 30ca3261 1497c827"; static const char * m64_string = "6bc1bee2 2e409f96 e93d7e11 7393172a ae2d8a57 1e03ac9c 9eb76fac 45af8e51 30c81c46 a35ce411 e5fbc119 1a0a52ef f69f2445 df4f9b17 ad2b417b e66c3710"; static const char * cmac_m64_string = "51f0bebf 7e3b9d92 fc497417 79363cfe"; // f4 static const char * f4_u_string = "20b003d2 f297be2c 5e2c83a7 e9f9a5b9 eff49111 acf4fddb cc030148 0e359de6"; static const char * f4_v_string = "55188b3d 32f6bb9a 900afcfb eed4e72a 59cb9ac2 f19d7cfb 6b4fdd49 f47fc5fd"; static const char * f4_x_string = "d5cb8454 d177733e ffffb2ec 712baeab"; static const char * f4_z_string = "00"; static const char * f4_cmac_string = "f2c916f1 07a9bd1c f1eda1be a974872d"; static int nibble_for_char(char c){ if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'A' && c <= 'F') return c - 'F' + 10; return -1; } static int parse_hex(uint8_t * buffer, const char * hex_string){ int len = 0; while (*hex_string){ if (*hex_string == ' '){ hex_string++; continue; } int high_nibble = nibble_for_char(*hex_string++); int low_nibble = nibble_for_char(*hex_string++); *buffer++ = (high_nibble << 4) | low_nibble; len++; } return len; } static void sm_shift_left_by_one_bit_inplace(int len, uint8_t * data){ int i; int carry = 0; for (i=len-1; i >= 0 ; i--){ int new_carry = data[i] >> 7; data[i] = data[i] << 1 | carry; carry = new_carry; } } void aes128_calc_cyphertext(uint8_t key[16], uint8_t plaintext[16], uint8_t cyphertext[16]){ uint32_t rk[RKLENGTH(KEYBITS)]; int nrounds = rijndaelSetupEncrypt(rk, &key[0], KEYBITS); rijndaelEncrypt(rk, nrounds, plaintext, cyphertext); } static void hexdump2(void *data, int size){ if (size <= 0) return; int i; for (i=0; i