Ejemplo n.º 1
0
static int init_cfb_14_impl(unsigned char *key, void *ctx, size_t s, unsigned char *iv, size_t ivLength)
{
     Context_cfb* context;
     INFOTECS_ASSERT(sizeof(Context_cfb)<=kCfb14ContextLen);

     if(!ctx || !key || s > kBlockLen14 || (ivLength % kBlockLen14) || !ivLength || !s)
          return -1;

     context = (Context_cfb*)ctx;

     context->Alg = kAlg14;

     context->EncryptFunc = Encrypt_14;
     context->DecryptFunc = Decrypt_14;

     context->BlockLen = kBlockLen14;

     context->IV = (unsigned char*)malloc(ivLength);
     context->tmpblock = (unsigned char*)malloc(kBlockLen14);
     context->nextIV = (unsigned char*)malloc(ivLength);
     context->Keys = (unsigned char*)malloc(10 * kBlockLen14);
     if( !context->IV || !context->tmpblock || !context->nextIV || !context->Keys )
          return -1;

     memcpy(context->IV, iv, ivLength);

     context->M = ivLength;

     context->S = s;

     memset(context->Keys, 0, 10 * kBlockLen14);
     ExpandKey(key, context->Keys);

     return 0;
}
Ejemplo n.º 2
0
Archivo: aes.c Proyecto: TomCrypto/Ordo
int aes_init(struct AES_STATE *state,
             const void *key, size_t key_len,
             const struct AES_PARAMS *params)
{
    if (!limit_check(key_len, bits(128), bits(256), bits(64)))
        return ORDO_KEY_LEN;

    if (params)
    {
        if (params->rounds == 0) return ORDO_ARG;
        if (params->rounds > 20) return ORDO_ARG;
        state->rounds = params->rounds;
    }
    else
    {
        /* Set the default round numbers. */
        if (key_len == 16) state->rounds = 10;
        else if (key_len == 24) state->rounds = 12;
        else if (key_len == 32) state->rounds = 14;
    }

    ExpandKey((uint8_t *)key, state->key, key_len / 4, state->rounds);

    return ORDO_SUCCESS;
}
Ejemplo n.º 3
0
static int init_crt_14_impl(unsigned char* key, unsigned char *iv, size_t s, void *ctx)
{
     Context_crt* context;
     INFOTECS_ASSERT(sizeof(Context_crt)<=kCrt14ContextLen);

     if(!ctx || !key || s > kBlockLen14)
          return -1;

     context = (Context_crt*)ctx;

     context->Alg = kAlg14;

     context->EncryptFunc = Encrypt_14;

     context->BlockLen = kBlockLen14;

     context->S = s;

     context->tmpblock = (unsigned char*)malloc(kBlockLen14);
     context->Keys = (unsigned char*)malloc(10*kBlockLen14);
     context->Counter = (unsigned char*)malloc(kBlockLen14);
     if( !context->tmpblock || !context->Keys || !context->Counter )
          return -1;

     memset(context->Keys, 0, 10 * kBlockLen14);
     ExpandKey(key, context->Keys);

     memset(context->Counter, 0, kBlockLen14);
     memcpy(context->Counter, iv, kBlockLen14/2);

     return 0;
}
Ejemplo n.º 4
0
static int init_cbc_14_impl(unsigned char *key, void* ctx, const unsigned char *iv, size_t ivLength)
{
     Context_cbc* context;
     INFOTECS_ASSERT(sizeof(Context_cbc)<=kCbc14ContextLen);

     if(!ctx || !key || !iv || (ivLength % kBlockLen14))
          return -1;

     context = (Context_cbc*)ctx;

     context->Alg = kAlg14;

     context->EncryptFunc = Encrypt_14;
     context->DecryptFunc = Decrypt_14;

     context->BlockLen = kBlockLen14;

     context->M = ivLength;

     context->IV = (unsigned char*)malloc(ivLength);
     context->Keys = (unsigned char*)malloc(10 * kBlockLen14);
     context->tempIV = (unsigned char*)malloc(context->M);
     context->nextIV = (unsigned char*)malloc(context->M);
     context->tmpblock = (unsigned char *)malloc(kBlockLen14);
     if( !context->IV || !context->Keys || !context->tempIV || !context->nextIV || !context->tmpblock )
          return -1;

     memcpy(context->IV, iv, ivLength);
     memset(context->Keys, 0, 10 * kBlockLen14);

     ExpandKey(key, context->Keys);
     return 0;
}
Ejemplo n.º 5
0
static int init_imit_14_impl(unsigned char *key, size_t s, void *ctx)
{
     Context_imit* context;
     INFOTECS_ASSERT(sizeof(Context_imit)<=kImit14ContextLen);

     if(!ctx || !key || s > kBlockLen14 || !s)
          return -1;

     context = (Context_imit*)ctx;

     context->Alg = kAlg14;

     context->EncryptFunc = Encrypt_14;

     context->BlockLen = kBlockLen14;
     context->S = s;

     context->Keys = (unsigned char*)malloc(10 * kBlockLen14);
     context->R = (unsigned char*)malloc(kBlockLen14);
     context->B = (unsigned char*)malloc(kBlockLen14);
     context->K1 = (unsigned char*)malloc(kBlockLen14);
     context->K2 = (unsigned char*)malloc(kBlockLen14);
     context->C = (unsigned char*)malloc(kBlockLen14);
     context->LastBlock = (unsigned char*)malloc(kBlockLen14);
     context->tmpblock = (unsigned char*)malloc(kBlockLen14);
     context->resimit = (unsigned char*)malloc(kBlockLen14);
     if( !context->Keys || !context->R || !context->B 
          || !context->K1 || !context->K2 || !context->C
          || !context->LastBlock || !context->tmpblock || !context->resimit )
          return -1;

     memset(context->Keys, 0, 10 * kBlockLen14);

     ExpandKey(key, context->Keys);

     memset(context->R, 0, kBlockLen14);

     memset(context->B, 0, kBlockLen14);
     context->B[kBlockLen14-1] = 0x87;

     memset(context->K1, 0, kBlockLen14);

     memset(context->K2, 0, kBlockLen14);

     memset(context->C, 0, kBlockLen14);

     memset(context->LastBlock, 0, kBlockLen14);

     context->LastBlockSize = 0;

     context->isFistBlock = 1;

     ExpandImitKey(key, ctx);


     return 0;
}
Ejemplo n.º 6
0
/*------------------------------------------------------------------------
 * Decrypt a message using transport-layer encryption.
 *
 *  @param session	The MXit session object
 *	@param message	The encrypted message data (is base64-encoded).
 *  @return			The decrypted message.  Must be g_free'd when no longer needed.
 */
char* mxit_decrypt_message( struct MXitSession* session, char* message )
{
	guchar*			raw_message;
	gsize			raw_len;
	char			exkey[512];
	GString*		decoded		= NULL;
	unsigned int	i;

	/* remove optional header: <mxitencrypted ver="5.2"/> */
	if ( strncmp( message, ENCRYPT_HEADER, strlen( ENCRYPT_HEADER ) ) == 0 )
		message += strlen( ENCRYPT_HEADER );

	/* base64 decode the message */
	raw_message = purple_base64_decode( message, &raw_len );

	/* AES-encrypted data is always blocks of 16 bytes */
	if ( ( raw_len == 0 ) || ( raw_len % 16 != 0 ) )
		return NULL;

	/* build the AES key */
	ExpandKey( (unsigned char*) transport_layer_key( session ), (unsigned char*) exkey );

	/* AES decrypt each block */
	decoded = g_string_sized_new( raw_len );
	for ( i = 0; i < raw_len; i += 16 ) {
		char	block[16];

		Decrypt( (unsigned char*) raw_message + i, (unsigned char*) exkey, (unsigned char*) block );
		g_string_append_len( decoded, block, 16 );
	}
	g_free( raw_message );

	/* check that the decrypted message starts with header: <mxit/> */
	if ( strncmp( decoded->str, SECRET_HEADER, strlen( SECRET_HEADER ) != 0 ) ) {
		g_string_free( decoded, TRUE );
		return NULL;			/* message could not be decrypted */
	}

	/* remove ISO10126 padding */
	padding_remove( decoded );

	/* remove encryption header */
	g_string_erase( decoded, 0, strlen( SECRET_HEADER ) );

	return g_string_free( decoded, FALSE );
}
Ejemplo n.º 7
0
void bernstein(char *seed){
	int loops, b, j, k;

	mrandom(strlen(seed), seed);

	for(loops=4; loops<=65536; loops*=16){
		for(b=0; b<16; b++){
			printf("%.2d, %.5d loops:", b, loops);
			for(k=0; k<10; k++){
				for(j=0; j<16; j++){
					key[j] = xrandom() >> 16;
				}
				ExpandKey(key, expkey);
				printf(" %.2x", bestx(b, loops) ^ key[b]);
				fflush(stdout);
			}
			printf("\n");
		}
	}
}
Ejemplo n.º 8
0
QVector<bit16> Full_CBC_SAES_Encrypt(QVector<bit16> PTVector, bit16 Key16)
{
    int L = PTVector.size();
    QVector<bit16> CTVector;
    QVector<bit16> KeyExpansion = ExpandKey(Key16);

    bit16 IV(std::string("0001011111010011"));   //backwards because of Endian conversion

    bit16 ForNextRound = IV;

    for(int i=0; i<L; i++)
    {
        bit16 Plaintext = PTVector[i];
        bit16 InputToSAES = ForNextRound ^ Plaintext;
        ForNextRound = SAES_Encrypt(KeyExpansion, InputToSAES);
        CTVector.push_back(ForNextRound);
    }

    return CTVector;

}
Ejemplo n.º 9
0
QVector<bit16> Full_CBC_SAES_Decrypt(QVector<bit16> CTVectorInv, bit16 Key16Inv)
{
    int LInv = CTVectorInv.size();
    QVector<bit16> PTVectorInv;

    QVector<bit16> KeyExpansionInv = ExpandKey(Key16Inv);
    bit16 IV(std::string("0001011111010011"));
    bit16 ForNextRoundInv = IV;

    for(int i=0; i<LInv; i++)
    {
        bit16 CiphertextInv = CTVectorInv[i];
        bit16 TempInv = SAES_Decrypt(KeyExpansionInv, CiphertextInv);
        bit16 PTPieceInv = TempInv ^ ForNextRoundInv;
        PTVectorInv.push_back(PTPieceInv);
        ForNextRoundInv = CiphertextInv;
    }

    return PTVectorInv;

}
Ejemplo n.º 10
0
/*------------------------------------------------------------------------
 * Encrypt the user's cleartext password using the AES 128-bit (ECB)
 *  encryption algorithm.
 *
 *  @param session	The MXit session object
 *  @return			The encrypted & encoded password.  Must be g_free'd when no longer needed.
 */
char* mxit_encrypt_password( struct MXitSession* session )
{
	char			key[16 + 1];
	char			exkey[512];
	GString*		pass			= NULL;
	GString*		encrypted		= NULL;
	char*			base64;
	unsigned int	i;

	purple_debug_info( MXIT_PLUGIN_ID, "mxit_encrypt_password\n" );

	/* build the AES encryption key */
	g_strlcpy( key, INITIAL_KEY, sizeof( key ) );
	memcpy( key, session->clientkey, strlen( session->clientkey ) );
	ExpandKey( (unsigned char*) key, (unsigned char*) exkey );

	/* build the secret data to be encrypted: SECRET_HEADER + password */
	pass = g_string_new( SECRET_HEADER );
	g_string_append( pass, purple_connection_get_password( session->con ) );
	padding_add( pass );		/* add ISO10126 padding */

	/* now encrypt the secret. we encrypt each block separately (ECB mode) */
	encrypted = g_string_sized_new( pass->len );
	for ( i = 0; i < pass->len; i += 16 ) {
		char	block[16];

		Encrypt( (unsigned char*) pass->str + i, (unsigned char*) exkey, (unsigned char*) block );
		g_string_append_len( encrypted, block, 16 );
	}

	/* now base64 encode the encrypted password */
	base64 = purple_base64_encode( (unsigned char*) encrypted->str, encrypted->len );
	g_string_free( encrypted, TRUE );

	g_string_free( pass, TRUE );

	return base64;
}
Ejemplo n.º 11
0
static int init_ecb_14_impl(unsigned char *key, void* ctx)
{
     Context_ecb* context = 0;
     INFOTECS_ASSERT(sizeof(Context_ecb)<=kEcb14ContextLen);

     if(!ctx || !key)
          return -1;

     context = (Context_ecb*)ctx;

     context->Alg = kAlg14;
     context->EncryptFunc = Encrypt_14;
     context->DecryptFunc = Decrypt_14;
     context->BlockLen = kBlockLen14;

     context->Keys = (unsigned char*)malloc(10 * kBlockLen14);
     if( !context->Keys )
          return -1;
     memset(context->Keys, 0, 10 * kBlockLen14);

     ExpandKey(key, context->Keys);
     return 0;
}
Ejemplo n.º 12
0
/*------------------------------------------------------------------------
 * Encrypt a message using transport-layer encryption.
 *
 *  @param session	The MXit session object
 *	@param message	The message data.
 *  @return			The encrypted message.  Must be g_free'd when no longer needed.
 */
char* mxit_encrypt_message( struct MXitSession* session, char* message )
{
	GString*		raw_message	= NULL;
	char			exkey[512];
	GString*		encoded		= NULL;
	gchar*			base64;
	unsigned int	i;

	purple_debug_info( MXIT_PLUGIN_ID, "encrypt message: '%s'\n", message );

	/* append encryption header to message data */
	raw_message = g_string_new( SECRET_HEADER );
	g_string_append( raw_message, message );
	padding_add( raw_message );		/* add ISO10126 padding */

	/* build the AES key */
	ExpandKey( (unsigned char*) transport_layer_key( session ), (unsigned char*) exkey );

	/* AES encrypt each block */
	encoded = g_string_sized_new( raw_message->len );
	for ( i = 0; i < raw_message->len; i += 16 ) {
		char	block[16];

		Encrypt( (unsigned char*) raw_message->str + i, (unsigned char*) exkey, (unsigned char*) block );
		g_string_append_len( encoded, block, 16 );
	}
	g_string_free( raw_message, TRUE );

	/* base64 encode the encrypted message */
	base64 = purple_base64_encode( (unsigned char *) encoded->str, encoded->len );
	g_string_free( encoded, TRUE );

	purple_debug_info( MXIT_PLUGIN_ID, "encrypted message: '%s'\n", base64 );

	return base64;
}
Ejemplo n.º 13
0
int aes_handshake(int socket, unsigned char *key)
{
	unsigned char expkey[4 * 4 * (10 + 1)];

	unsigned char token[TOKEN_SIZE];
	unsigned char enc[TOKEN_SIZE];
	unsigned char response[TOKEN_SIZE];
	int i;

	ExpandKey(key,expkey);

	for(i=0;i<TOKEN_SIZE;i++)
	{
		token[i] = rand()%255;
	}

	Encrypt(token, expkey, enc);

	//send token
	write(socket, enc, TOKEN_SIZE);

	//read response
	if(read_nbytes(socket, enc, TOKEN_SIZE) == 0)
		return 0;

	Decrypt(enc, expkey, response);

	//check response
	for(i=0;i<TOKEN_SIZE;i++)
	{
		if((response[i] ^ token_xor_key[i]) != token[i])
			return 0; 
	}

	return 1;
}
Ejemplo n.º 14
0
int crypto_stream_speck128128ctr_avx2(
  unsigned char *out, 
  unsigned long long outlen, 
  const unsigned char *n, 
  const unsigned char *k
)
{
  int i;
  u64 nonce[2],K[4],key[34],A,B,C,D,x,y;
  unsigned char block[16];
  u256 rk[34];

  if (!outlen) return 0;

  nonce[0]=((u64 *)n)[0];
  nonce[1]=((u64 *)n)[1];

  for(i=0;i<numkeywords;i++) K[i]=((u64 *)k)[i];

  if (outlen<=16){
    B=K[1]; A=K[0];
    x=nonce[1]; y=nonce[0]++;
    for(i=0;i<numrounds;i++){
      Rx1b(x,y,A); Rx1b(B,A,i); 
    }
    ((u64 *)block)[1]=x; ((u64 *)block)[0]=y;
    for(i=0;i<outlen;i++) out[i]=block[i];

    return 0;
  }
  
  ExpandKey(K,rk,key);

  while(outlen>=320){
    Encrypt(out,nonce,rk,key,320);
    out+=320; outlen-=320;
  }

  if (outlen>=256){
    Encrypt(out,nonce,rk,key,256);
    out+=256; outlen-=256;
  }

  if (outlen>=192){
    Encrypt(out,nonce,rk,key,192);
    out+=192; outlen-=192;
  }

  if (outlen>=128){
    Encrypt(out,nonce,rk,key,128);
    out+=128; outlen-=128;
  }

  if (outlen>=64){
    Encrypt(out,nonce,rk,key,64);
    out+=64; outlen-=64;
  }

  if (outlen>=32){
    Encrypt(out,nonce,rk,key,32);
    out+=32; outlen-=32;
  }

  if (outlen>=16){
    Encrypt(out,nonce,rk,key,16);
    out+=16; outlen-=16;
  }

  if (outlen>0){ 
    Encrypt(block,nonce,rk,key,16);
    for (i=0;i<outlen;i++) out[i] = block[i];
  }

  return 0;
}
Ejemplo n.º 15
0
int crypto_stream_speck128128ctr_sse4_xor(
  unsigned char *out, 
  const unsigned char *in, 
  unsigned long long inlen, 
  const unsigned char *n, 
  const unsigned char *k
)
{
  int i;
  u64 nonce[2],K[4],key[34],A,B,C,D,x,y;
  unsigned char block[16];
  u128 rk[34];

  if (!inlen) return 0;

  nonce[0]=((u64 *)n)[0];
  nonce[1]=((u64 *)n)[1];

  for(i=0;i<numkeywords;i++) K[i]=((u64 *)k)[i];

  if (inlen<=16){
    B=K[1]; A=K[0];
    x=nonce[1]; y=nonce[0]; nonce[0]++;
    for(i=0;i<numrounds;i++){
      Rx1b(x,y,A); Rx1b(B,A,i); 
    }
    ((u64 *)block)[1]=x; ((u64 *)block)[0]=y;
    for(i=0;i<inlen;i++) out[i]=block[i]^in[i];

    return 0;
  }
  
  ExpandKey(K,rk,key);

  while(inlen>=128){
    Encrypt_Xor(out,in,nonce,rk,key,128);
    in+=128; inlen-=128; out+=128;
  }

  if (inlen>=96){
    Encrypt_Xor(out,in,nonce,rk,key,96);
    in+=96; inlen-=96; out+=96;
  }

  if (inlen>=64){
    Encrypt_Xor(out,in,nonce,rk,key,64);
    in+=64; inlen-=64; out+=64;
  }

  if (inlen>=32){
    Encrypt_Xor(out,in,nonce,rk,key,32);
    in+=32; inlen-=32; out+=32;
  }

  if (inlen>=16){
    Encrypt_Xor(block,in,nonce,rk,key,16);
    ((u64 *)out)[0]=((u64 *)block)[0]^((u64 *)in)[0];
    ((u64 *)out)[1]=((u64 *)block)[1]^((u64 *)in)[1];
    in+=16; inlen-=16; out+=16;
  }

  if (inlen>0){ 
    Encrypt_Xor(block,in,nonce,rk,key,16);
    for (i=0;i<inlen;i++) out[i]=block[i]^in[i];
  }

  return 0;
}
Ejemplo n.º 16
0
 virtual void do_prepare(const TaskData &) override
 {
     ExpandKey(m_key.data(), m_ctx);
 }
void encrypt() {
    bzero(StateArray, 4*4*sizeof(unsigned char));
    bzero(ExpandedKey, 11*4*sizeof(unsigned char));

#if (AES_PRINT & AES_PRINT_MAIN)
    xil_printf("-- Test Encyption Key \r\n\n");
    AES_printf(Key);
    xil_printf("-------------------------\r\n\n");

    xil_printf("-- Test Plaintext \r\n\n");
    AES_printf(PlainText);
    xil_printf("-------------------------\r\n\n");
#endif

#if (AES_PRINT & AES_PRINT_MAIN)
    xil_printf("-- Starting Key Expension   \r\n\n");
#endif


    ExpandKey(Key, ExpandedKey);  //


#if (AES_PRINT & AES_PRINT_MAIN)
    xil_printf("-- Starting Encryption  \r\n\n");
#endif

    long int x;
    for(x=0; x<100; x++) {
        memcpy(StateArray, PlainText, 4*4*sizeof(unsigned char));

#if (AES_PRINT & AES_PRINT_DETAILS)
        xil_printf("-- Test State - Start of Round 0 \r\n\n");
        AES_printf(StateArray);
        xil_printf("-------------------------\r\n\n");
#endif

        AddRoundKey(ExpandedKey[0], StateArray);  //


#if (AES_PRINT & AES_PRINT_DETAILS)
        xil_printf("-- Test State - Start of Round 0 \r\n\n");
        AES_printf(StateArray);
        xil_printf("-------------------------\r\n\n");
#endif

        int i;

        //Rounds
        for(i=1; i<=10; i++) {

            SubBytes(StateArray);
#if (AES_PRINT & AES_PRINT_DETAILS)
            xil_printf("-- Test State - Round %d after SubBytes \r\n\n", i);
            AES_printf(StateArray);
            xil_printf("-------------------\r\n\n");
#endif

            ShiftRows(StateArray);
#if (AES_PRINT & AES_PRINT_DETAILS)
            xil_printf("-- Test State - Round %d after shiftRows \r\n\n", i);
            AES_printf(StateArray);
            xil_printf("-----------------------------\r\n\n");
#endif

            if(i != 10) {

                MixColumns(StateArray);
#if (AES_PRINT & AES_PRINT_DETAILS)
                xil_printf("-- Test State - Round %d after MixColumns \r\n\n", i);
                AES_printf(StateArray);
                xil_printf("-----------------------------\r\n\n");
#endif
            }

            AddRoundKey(ExpandedKey[i], StateArray);
#if (AES_PRINT & AES_PRINT_DETAILS)
            xil_printf("-- Test State - Round %d after RoundKeyValue \r\n\n", i);
            AES_printf(StateArray);
            xil_printf("-----------------------------\r\n\n");
#endif
        }
    }

#if (AES_PRINT & AES_PRINT_DETAILS)
    xil_printf("-- AES key expansion and encryption test completed.   \r\n\n");
    xil_printf("-- Test State - End  \r\n\n");
    AES_printf(StateArray);
    xil_printf("-----------------------------\r\n\n");
#endif

    xil_printf("****************Encryption*********************\r\n");

}