void Crypt::EncryptAES128ECB(unsigned char * key, unsigned char const* plainTextIn, unsigned char* cipherTextOut) { unsigned long rk[RKLENGTH(KEYBITS)]; int nrounds; nrounds = rijndaelSetupEncrypt(rk, key, KEYBITS); rijndaelEncrypt(rk, nrounds, plainTextIn, cipherTextOut); }
void rijndael256Encrypt(const char *plaindata, unsigned char password[KEYLENGTH(KEYBITS)], unsigned char cipherdata[]) { unsigned long rk[RKLENGTH(KEYBITS)]; unsigned char key[KEYLENGTH(KEYBITS)]; int nrounds; for (int i = 0; i < sizeof(key); i++) key[i] = *password != 0 ? *password++ : 0; // Add PKCS7 padding. unsigned long data_len = strlen(plaindata); unsigned char new_plaindata[data_len]; memcpy(new_plaindata, plaindata, data_len); unsigned int pad = 16 - (data_len%16); int y = 0; for (; y < pad; new_plaindata[data_len + y++] = (char)pad); new_plaindata[data_len + y + 1] = 0; data_len = data_len + y; nrounds = rijndaelSetupEncrypt(rk, key, KEYBITS); unsigned char iv[16]; for (int i = 0; i < sizeof(iv); i++) { iv[i] = 0; } for (int i = 0; i < data_len/16; i++) { unsigned char plainblock[16]; unsigned char cipherblock[16]; for (int j = 0; j < sizeof(plainblock); j++) { plainblock[j] = new_plaindata[i*16+j] ^ iv[j]; } rijndaelEncrypt(rk, nrounds, plainblock, cipherblock); for (int k = 0; k < sizeof(cipherblock); k++) { cipherdata[i*16+k] = iv[k] = cipherblock[k]; } } }
void aes_pre_enc(uint32 bits, byte *key, byte *chain) { int ms; ms = ticks; nrounds = rijndaelSetupEncrypt(rk, key, bits); memset(chain, 0, 16); aes_enc_ticks += ticks-ms; }
int aes_setup_encrypt(unsigned char *key, unsigned char *round_keys, int keybits) { if(keybits != 128) panic(0, "AES doesn't support non-128 keylength"); #if CONFIG_ARCH == TYPE_ARCH_X86_64 if(has_intel_aes) { aes_x86_128_key_expand(key, round_keys); return 10; } #endif return rijndaelSetupEncrypt((uint32_t *)round_keys, key, keybits); }
rijndaelcbc *rijndaelcbc_init(unsigned char *key, int keybits, unsigned char *iv, int decrypt) { rijndaelcbc *ret = (rijndaelcbc *)malloc(sizeof(rijndaelcbc) + RKLENGTH(keybits) * sizeof(unsigned long)); if(!ret) return NULL; memcpy(ret->prevblock, iv, 16); if(decrypt) { ret->nrounds = rijndaelSetupDecrypt(ret->rk, key, keybits); } else { ret->nrounds = rijndaelSetupEncrypt(ret->rk, key, keybits); } return ret; }
char *snippet_encrypt(const char *plain_text, const char *password) { unsigned long rk[RKLENGTH(KEYBITS)]; unsigned char key[KEYLENGTH(KEYBITS)]; int i; int nrounds; for (i = 0; i < sizeof(key); i++) { key[i] = *password != 0 ? *password++ : 0; } nrounds = rijndaelSetupEncrypt(rk, key, KEYBITS); char **blocks = __snippet_split_to_blocks(plain_text); __snippet_encrypt_blocks(rk, nrounds, blocks); return NULL; }
void CAES::setKey(const char *pszKey, const unsigned int uiKeyType) { size_t iRKLens(RKLENGTH(uiKeyType)); size_t iKeyLens(KEYLENGTH(uiKeyType)); m_pEncodeRK = new(std::nothrow) unsigned long[iRKLens + 1]; H_ASSERT(NULL != m_pEncodeRK, "malloc memory error."); H_Zero(m_pEncodeRK, iRKLens + 1); m_pDecodeRK = new(std::nothrow) unsigned long[iRKLens + 1]; H_ASSERT(NULL != m_pDecodeRK, "malloc memory error."); H_Zero(m_pDecodeRK, iRKLens + 1); unsigned char * pKey = new(std::nothrow) unsigned char[iKeyLens + 1]; H_ASSERT(NULL != pKey, "malloc memory error."); H_Zero(pKey, iKeyLens + 1); memcpy(pKey, pszKey, strlen(pszKey) > iKeyLens ? iKeyLens : strlen(pszKey)); m_iEncodeRounds = rijndaelSetupEncrypt(m_pEncodeRK, pKey, uiKeyType); m_iDecodeRounds = rijndaelSetupDecrypt(m_pDecodeRK, pKey, uiKeyType); H_SafeDelArray(pKey); }
static void sm_s1(key_t k, key_t r1, key_t r2, key_t s1){ key_t r_prime; memcpy(&r_prime[0], r2, 8); memcpy(&r_prime[8], r1, 8); key_t r_flipped; swap128(r_prime, r_flipped); printf("r': "); hexdump(r_flipped, 16); key_t tk_flipped; swap128(sm_tk, tk_flipped); printf("tk' "); hexdump(tk_flipped, 16); // setup aes decryption unsigned long rk[RKLENGTH(KEYBITS)]; int nrounds = rijndaelSetupEncrypt(rk, &tk_flipped[0], KEYBITS); key_t s1_flipped; rijndaelEncrypt(rk, nrounds, r_flipped, s1_flipped); printf("s1' "); hexdump(s1_flipped, 16); swap128(s1_flipped, s1); printf("s1: "); hexdump(s1, 16); }
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 sm_c1(key_t k, key_t r, uint8_t preq[7], uint8_t pres[7], uint8_t iat, uint8_t rat, bd_addr_t ia, bd_addr_t ra, key_t c1){ // p1 = pres || preq || rat’ || iat’ // "The octet of iat’ becomes the least significant octet of p1 and the most signifi- // cant octet of pres becomes the most significant octet of p1. // For example, if the 8-bit iat’ is 0x01, the 8-bit rat’ is 0x00, the 56-bit preq // is 0x07071000000101 and the 56 bit pres is 0x05000800000302 then // p1 is 0x05000800000302070710000001010001." key_t p1_flipped; swap56(pres, &p1_flipped[0]); swap56(preq, &p1_flipped[7]); p1_flipped[14] = rat; p1_flipped[15] = iat; printf("p1' "); hexdump(p1_flipped, 16); // p2 = padding || ia || ra // "The least significant octet of ra becomes the least significant octet of p2 and // the most significant octet of padding becomes the most significant octet of p2. // For example, if 48-bit ia is 0xA1A2A3A4A5A6 and the 48-bit ra is // 0xB1B2B3B4B5B6 then p2 is 0x00000000A1A2A3A4A5A6B1B2B3B4B5B6. key_t p2_flipped; memset(p2_flipped, 0, 16); memcpy(&p2_flipped[4], ia, 6); memcpy(&p2_flipped[10], ra, 6); printf("p2' "); hexdump(p2_flipped, 16); // t1 = r xor p1 int i; key_t t1_flipped; for (i=0;i<16;i++){ t1_flipped[i] = r[15-i] ^ p1_flipped[i]; } printf("t1' "); hexdump(t1_flipped, 16); key_t tk_flipped; swap128(sm_tk, tk_flipped); printf("tk' "); hexdump(tk_flipped, 16); // setup aes decryption unsigned long rk[RKLENGTH(KEYBITS)]; int nrounds = rijndaelSetupEncrypt(rk, &tk_flipped[0], KEYBITS); // t2 = e(k, r_xor_p1) key_t t2_flipped; rijndaelEncrypt(rk, nrounds, t1_flipped, t2_flipped); printf("t2' "); hexdump(t2_flipped, 16); key_t t3_flipped; for (i=0;i<16;i++){ t3_flipped[i] = t2_flipped[i] ^ p2_flipped[i]; } printf("t3' "); hexdump(t3_flipped, 16); key_t c1_flipped; rijndaelEncrypt(rk, nrounds, t3_flipped, c1_flipped); printf("c1' "); hexdump(c1_flipped, 16); swap128(c1_flipped, c1); printf("c1: "); hexdump(c1, 16); }
crypto_cstring aesEncrypt(const char *data, const unsigned long dataLength, const char* password, unsigned long passwordLength) { assert(passwordLength == AES_KEYBYTE); int n = 0; if (dataLength / AES_BLOCKBYTE == dataLength * 1.0 / AES_BLOCKBYTE) n = dataLength / AES_BLOCKBYTE; else n = dataLength / AES_BLOCKBYTE + 1; ++n; crypto_cstring answer; answer.length = n * AES_BLOCKBYTE; answer.context = malloc(sizeof(char) * (answer.length) + 100); memcpy(answer.context, data, sizeof(char) * dataLength); unsigned long longi; for (longi = dataLength ; longi < (answer.length) ; ++longi) answer.context[longi] = '\0'; ((uint64_t*) answer.context)[LENGTHPOSITION(answer.length)] = (uint64_t) dataLength; int i; unsigned long rk[RKLENGTH(AES_KEYBIT)]; unsigned char key[KEYLENGTH(AES_KEYBIT)]; for (i = 0; i < passwordLength; i++) key[i] = password != 0 ? *password++ : 0; int nrounds = rijndaelSetupEncrypt(rk, key, AES_KEYBIT); int index = 0; int lastIndex; while (1) { lastIndex = index; unsigned char plaintext[32]; unsigned char ciphertext[32]; int j; for (j = 0; j < 16; j++) { if (index >= answer.length) break; plaintext[j] = answer.context[index++]; } if (j == 0) break; for (; j < sizeof(plaintext); j++) plaintext[j] = '\0'; rijndaelEncrypt(rk, nrounds, plaintext, ciphertext); memcpy(answer.context + lastIndex, ciphertext, AES_BLOCKBYTE); } return answer; }