예제 #1
0
void f2_5(u8* keyArr, u8* response_arr) {
    u8* out2;
    out2 = malloc(16);

    for (i = 0; i < 16; i++) {
        out2[i] = temp[i] ^ opc[i];
    }
    
    convertToBin(out2, binArr);
    rotWord(binArr, 128, 0x3A);
    convertToHex(binArr, out2);

    for (i = 0; i < 16; i++) {
        out2[i] ^= c2[i];
    }
    encrypt(out2, keyArr, out2);

    for (i = 0; i < 16; i++) {
        out2[i] ^= opc[i];
    }

    printf("\r\nAK: ");
    for (i = 0; i < 6; i++) {
        ak[i] = out2[i];
        printf("%02x", ak[i]);
    }
    printf("\r\nRES: ");
    for (i = 8; i < 16; i++) {
        sprintf(&response_arr[(i-8)*2], "%02x", out2[i]);
        printf("%02x", out2[i]);
    }
}
예제 #2
0
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;
}
예제 #3
0
파일: Aes.cpp 프로젝트: frankencode/fluxkit
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;
}
예제 #4
0
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_rotWord_given_0x7359f67f_and_expected_0x59f67f73(void){
  printf("No4.0 - rotWord\n");
  uint32_t expect0 = 0x59 << 24 | 0xf6 << 16 | 0x7f << 8 | 0x73 << 0;
  uint32_t temp = 0x73 << 24 | 0x59 << 16 | 0xf6 << 8 | 0x7f << 0;
  temp = rotWord(temp);
  printf("%x %x",expect0,temp);
  TEST_ASSERT_EQUAL_UINT32(expect0,temp); 
}
void test_rotWord_given_0x2a6c7605_and_expected_0x6c76052a(void){
  printf("No3.0 - rotWord\n");
  uint32_t expect0 = 0x6c << 24 | 0x76 << 16 | 0x05 << 8 | 0x2a << 0;
  uint32_t temp = 0x2a << 24 | 0x6c << 16 | 0x76 << 8 | 0x05 << 0;
  temp = rotWord(temp);
  printf("%x %x",expect0,temp);
  TEST_ASSERT_EQUAL_UINT32(expect0,temp); 
}
/*
       -----                -----
  --->| 31 |               | 34 |
  |   -----                -----
  |   | 32 |     rotWord   | 31 |
  |   -----     ------>    -----
  |   | 33 |               | 32 |
  |   -----                -----
  ----| 34 |               | 33 |
      -----                -----
  
*/
void test_rotWord_given_0x31323334_and_expected_0x34313233(void){
  printf("No2.0 - rotWord\n");
  uint32_t expect0 = '2' << 24 | '3' << 16 | '4' << 8 | '1' << 0;
  uint32_t temp = '1' << 24 | '2' << 16 | '3' << 8 | '4' << 0;
  temp = rotWord(temp);
  printf("%x %x",expect0,temp);
  TEST_ASSERT_EQUAL_UINT32(expect0,temp); 
}
예제 #8
0
void f1(u8* keyArr, u8* mrand) {
    printf("\nSQN: ");
    for (i = 0; i < 6; i++) {
        printf("%02x", sqn[i]);
    }
    
    printf("\nAMF: ");
    for (i = 0; i < 2; i++) {
        printf("%02x", amf[i]);
    }

    // create IN1
    for (i = 0; i < 6; i++) {
        in1[i] = sqn[i];
        in1[i + 8] = sqn[i];
    }
    for (i = 0; i < 2; i++) {
        in1[i + 6] = amf[i];
        in1[i + 14] = amf[i];
    }

    printf("\nRAND: ");
    for (i = 0; i < 16; i++) {
        printf("%02x", mrand[i]);
    }

    for (i = 0; i < 16; i++) {
        toEncrypt[i] = mrand[i] ^ opc[i];
    }
    encrypt(toEncrypt, keyArr, temp);
    
    for (i = 0; i < 16; i++) {
        out1[i] = in1[i]^opc[i];
    }

    convertToBin(out1, binArr);
    rotWord(binArr, 128, 0x1F);
    convertToHex(binArr, out1);

    for (i = 0; i < 16; i++) {
        out1[i] ^= temp[i]^c1[i];
    }
    encrypt(out1, keyArr, out1);

    for (i = 0; i < 16; i++) {
        out1[i] ^= opc[i];
    }

    printf("\nMAC-A: ");
    for (i = 0; i < 8; i++) {
        printf("%02x", out1[i]);
    }

    printf("\nMAC-S: ");
    for (i = 8; i < 16; i++) {
        printf("%02x", out1[i]);
    }
}
예제 #9
0
void f5star(u8* keyArr, u8* sqn_ak) {
    u8* out5;
    out5 = malloc(16);

    for (i = 0; i < 16; i++) {
        out5[i] = temp[i] ^ opc[i];
    }
    
    convertToBin(out5, binArr);
    rotWord(binArr, 128, 0x08);
    convertToHex(binArr, out5);

    for (i = 0; i < 16; i++) {
        out5[i] ^= c5[i];
    }
    encrypt(out5, keyArr, out5);

    for (i = 0; i < 16; i++) {
        out5[i] ^= opc[i];
    }

    printf("\r\nAK (f5*): ");
    for (i = 0; i < 6; i++) {
        ak[i] = out5[i];
        printf("%02x", ak[i]);
    }
    
    for (i = 0; i < 6; i++) {
        sqn[i] = ak[i] ^ sqn_ak[i];
    }
    
    u8 ind = (sqn[5] & 0b00011111);
    u8 seq = (sqn[5] & 0b11100000);
    ind = (ind + 1) % 32;
    sqn[5] = 0;
    sqn[5] |= ind;
    
    seq += 0b00100000;
    if (seq == 0) {
        sqn[4] += 1;
    }
    sqn[5] |= seq;
}
예제 #10
0
파일: aes.c 프로젝트: Gunga/adesso
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];

    }
}
예제 #11
0
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;
}