void FastRijndael::makeKey(unsigned char** key){ // if (key == NULL){ // return; // } _exp_key = new unsigned char* [_nek]; for (int i = 0; i < _nek; i++){ _exp_key[i] = new unsigned char [4]; } for (int i = 0; i < _nk; i++){ memcpy(_exp_key[i], key[i], 4); } for (int i = _nk; i < _nek; i++){ memcpy(_exp_key[i], _exp_key[i-1], 4); if (_nk == 8){ // 256 bit key - one subword only if ((i-4)%8 == 0){ // between each full step (fullstep -> rot,sub,rcon,xor) subWord(_exp_key[i]); } } if (i % _nk == 0){ rotWord(_exp_key[i]); subWord(_exp_key[i]); _exp_key[i][0] ^= _rcon[i/_nk]; //xor Rcon } for (int j = 0; j < 4; j++){ _exp_key[i][j] ^= _exp_key[i-_nk][j]; } // printf("%x%x%x%x\n", _exp_key[i][0], _exp_key[i][1], _exp_key[i][2], _exp_key[i][3]); } _initd = true; }
Ref<ByteArray> keyExpansion(ByteArray *key, int Nr) { if (Nr <= 0) Nr = numRounds(key->count() / 4); Ref<ByteArray> w = ByteArray::create(Ns * (Nr + 1)); const int Nk = key->count() / 4; int i = 0; for (; i < Nk; ++i) { w->wordAt(i) = word( key->at(4 * i), key->at(4 * i + 1), key->at(4 * i + 2), key->at(4 * i + 3) ); } for (; i < w->count() / 4; ++i) { uint32_t h = w->wordAt(i - 1); if (i % Nk == 0) h = subWord(rotWord(h)) ^ rCon(i / Nk); else if (Nk > 6 && i % Nk == 4) h = subWord(h); w->wordAt(i) = w->wordAt(i - Nk) ^ h; } return w; }
void FastRijndael::makeKey(unsigned char* key){ if (key == NULL){ return; } _exp_key = new unsigned char [_nek*4]; memcpy(&_exp_key[0], &key[0], _nk*4); for (int i = _nk; i < _nek; i++){ imult4 = i*4; //printf("%.2x%.2x%.2x%.2x\n", _exp_key[i*4], _exp_key[i*4+1], _exp_key[i*4+2], _exp_key[i*4+3]); memcpy(&_exp_key[imult4], &_exp_key[(i-1)*4], 4); // printf("%.2x%.2x%.2x%.2x\n", _exp_key[i*4], _exp_key[i*4+1], _exp_key[i*4+2], _exp_key[i*4+3]); if (_nk == 8){ // 256 bit key - one subword only if ((i-4)%8 == 0){ // between each full step (fullstep -> rot,sub,rcon,xor) subWord(&_exp_key[imult4]); // printf("%.2x%.2x%.2x%.2x\n", _exp_key[i*4], _exp_key[i*4+1], _exp_key[i*4+2], _exp_key[i*4+3]); } } if (i % _nk == 0){ rotWord(&_exp_key[imult4]); // printf("%.2x%.2x%.2x%.2x\n", _exp_key[i*4], _exp_key[i*4+1], _exp_key[i*4+2], _exp_key[i*4+3]); subWord(&_exp_key[imult4]); // printf("%.2x%.2x%.2x%.2x\n", _exp_key[i*4], _exp_key[i*4+1], _exp_key[i*4+2], _exp_key[i*4+3]); _exp_key[imult4] ^= _rcon[i/_nk]; //xor Rcon // printf("%.2x%.2x%.2x%.2x\n", _exp_key[i*4], _exp_key[i*4+1], _exp_key[i*4+2], _exp_key[i*4+3]); } for (int j = 0; j < 4; j++){ _exp_key[imult4+j] ^= _exp_key[(i-_nk)*4+j]; } // printf("%.2x%.2x%.2x%.2x\n", _exp_key[i*4], _exp_key[i*4+1], _exp_key[i*4+2], _exp_key[i*4+3]); } _initd = true; }
void test_subWord_given_0xcf4f3c09_expected_0x8a84eb01(void){ printf("No5.0 - subWord\n"); uint32_t expect0 = 0x8a << 24 | 0x84 << 16 | 0xeb << 8 | 0x01 << 0; uint32_t temp = 0xcf << 24 | 0x4f << 16 | 0x3c << 8 | 0x09 << 0; temp = subWord(temp); printf("%x %x",expect0,temp); TEST_ASSERT_EQUAL_UINT32(expect0,temp); }
void test_subWord_given_0x6c76052a_expected_0x50386be5(void){ printf("No6.0 - subWord\n"); uint32_t expect0 = 0x50 << 24 | 0x38 << 16 | 0x6b << 8 | 0xe5 << 0; uint32_t temp = 0x6c << 24 | 0x76 << 16 | 0x05 << 8 | 0x2a << 0; temp = subWord(temp); printf("%x %x",expect0,temp); TEST_ASSERT_EQUAL_UINT32(expect0,temp); }
void crypt_AES128( const plainData_t plainText[PLAIN_BSIZE], cryptData_t cryptedText[CRYPT_BSIZE], expKey_t expKey[KSCH_AES128_SIZE]) { uint8_t state[4*NB]; uint32_t* ptr, *statePtr; uint8_t i, round; /* copy plain input into state to initialize */ ptr = (uint32_t*) &plainText[0]; statePtr = (uint32_t*) &state[0]; for(i=0;i<NB;i++) *statePtr++ = *ptr++; /* first iteration, round 0 */ addRoundkey((uint32_t*) &state[0], &expKey[0]); /* Nround - 1 iterations */ for(round=1;round<NR_AES128;round++) { statePtr = (uint32_t*) &state[0]; for(i=0;i<NB;i++) *statePtr++ = subWord(*statePtr); shiftRows(state); mixColumns((uint32_t*) &state[0]); addRoundkey((uint32_t*) &state[0], &expKey[round*NB]); } /* last iteration, round 11 */ statePtr = (uint32_t*) &state[0]; for(i=0;i<NB;i++) *statePtr++ = subWord(*statePtr); shiftRows(state); addRoundkey((uint32_t*) &state[0], &expKey[(NR_AES128)*NB]); /* now copy back the crypted text into the buffer */ ptr = (uint32_t*) &cryptedText[0]; statePtr = (uint32_t*) &state[0]; for(i=0;i<NB;i++) *ptr++ = *statePtr++; }
static bool subwordsTransposed(char *s1, char *s2) { char sw1a[1024], sw1b[1024]; char sw2a[1024], sw2b[1024]; while(*s1 && *s2) { s1 = subWord(s1, sw1a); s2 = subWord(s2, sw2a); if (!streq(sw1a, sw2a) ) { if (*s1 == EOS || *s2 == EOS) fail; s1 = subWord(s1, sw1b); s2 = subWord(s2, sw2b); if (!streq(sw1a, sw2b) || !streq(sw1b, sw2a) ) fail; } } if (*s1 == EOS && *s2 == EOS) succeed; fail; }
void keyExpansion(uint16_t expanded_key[]){ char cipherKey[16][3] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; int i, j, limit = (nb*(nr+1)); uint16_t temp[4]; split(key, cipherKey); hexafy(cipherKey, expanded_key); for (i = nk; i < limit; i++) { for (j = 0; j < 4; j++) temp[j] = expanded_key[j+i*nk-4]; if (i % nk*nk == 0) { //temp = SubWord(RotWord(temp)) xor Rcon[i/Nk] rotWord(temp); subWord(temp); temp[0] ^= rcon[i/nk]; } for (j = 0; j < 4; j++) expanded_key[i*nk+j]= expanded_key[(i-nk)*nk+j] ^ temp[j]; } }
bool partialMatch(const QString &value) const { QString::const_iterator vbegin = value.constBegin(), vend = value.constEnd(); // If any of our tokens start with the value, this is a match foreach (const QString &word, filterKey) { QString::const_iterator wbegin = word.constBegin(), wend = word.constEnd(); QString::const_iterator vit = vbegin, wit = wbegin; while (wit != wend) { if (*wit != *vit) break; // Preceding base chars match - are there any continuing diacritics? QString::const_iterator vmatch = vit++, wmatch = wit++; while (vit != vend && (*vit).category() == QChar::Mark_NonSpacing) ++vit; while (wit != wend && (*wit).category() == QChar::Mark_NonSpacing) ++wit; if ((vit - vmatch) > 1) { // The match value contains diacritics - the word needs to match them QString subValue(value.mid(vmatch - vbegin, vit - vmatch)); QString subWord(word.mid(wmatch - wbegin, wit - wmatch)); if (subValue.compare(subWord) != 0) break; } else { // Ignore any diacritics in our word } if (vit == vend) { // We have matched to the end of the value return true; } } }
void Rijndael::makeKey(unsigned char** key){ // if (key == NULL){ // return; // } /* _exp_key = new unsigned char* [4]; for (int i = 0; i < 4; i++){ _exp_key[i] = new unsigned char [_nek]; } for (int i = 0; i < 4; i++){ for (int j = 0; j < 4; j++){ _exp_key[i][j] = key[i][j]; } } unsigned char temp; for (int j = _nk; j < _nek; j++){ //copy _exp_key[0][j] = _exp_key[0][j-1]; _exp_key[1][j] = _exp_key[1][j-1]; _exp_key[2][j] = _exp_key[2][j-1]; _exp_key[3][j] = _exp_key[3][j-1]; if (j % _nk == 0){ // rotWord(_exp_key[i]); temp = _exp_key[0][j]; _exp_key[0][j] = _exp_key[1][j]; _exp_key[1][j] = _exp_key[2][j]; _exp_key[2][j] = _exp_key[3][j]; _exp_key[3][j] = temp; // subWord(_exp_key[i]); for (int i = 0; i < 4; i++){ _exp_key[i][j] = _sbox[_exp_key[i][j]]; } _exp_key[0][j] ^= _rcon[j/_nk]; //xor Rcon } for (int i = 0; i < 4; i++){ _exp_key[i][j] ^= _exp_key[i][j-_nk]; } } _initd = true;*/ _exp_key = new unsigned char* [_nek]; for (int i = 0; i < _nek; i++){ _exp_key[i] = new unsigned char [4]; } for (int i = 0; i < _nk; i++){ memcpy(_exp_key[i], key[i], 4); } for (int i = _nk; i < _nek; i++){ memcpy(_exp_key[i], _exp_key[i-1], 4); if (_nk == 8){ // 256 bit key - one subword only if ((i-4)%8 == 0){ // between each full step (fullstep -> rot,sub,rcon,xor) subWord(_exp_key[i]); } } if (i % _nk == 0){ rotWord(_exp_key[i]); subWord(_exp_key[i]); _exp_key[i][0] ^= _rcon[i/_nk]; //xor Rcon } for (int j = 0; j < 4; j++){ _exp_key[i][j] ^= _exp_key[i-_nk][j]; } //printf("%x%x%x%x\n", _exp_key[i][0], _exp_key[i][1], _exp_key[i][2], _exp_key[i][3]); } _initd = true; }