示例#1
0
// @return: need free.
char * crypt_decode(void *ctx, const char *input, int inlen, int *outlen)
{
  aes_context *aesctx;
  int proclen, rmdlen;
  char *decryrec;

  aesctx = (aes_context *)ctx;

  if (!ctx || !input || !inlen || !outlen)
    return NULL;

  decryrec = (char *)xcalloc(1, inlen + 1);
  proclen = 0;
  rmdlen = inlen;

  while (rmdlen >= 16)
  {
    aes_decrypt(aesctx, (unsigned char *)input + proclen, (unsigned char *)decryrec + proclen);
    proclen += 16;
    rmdlen -= 16;
  }

  rmdlen = 16 - decryrec[proclen-1];
  *outlen = proclen - 16  + rmdlen;
  decryrec[*outlen] = 0;
  return decryrec;
}
示例#2
0
err_status_t
aes_test_inverse(void) {
  v128_t x, y;
  aes_expanded_key_t expanded_key, decrypt_key;
  uint8_t plaintext[16] = {
    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff 
  };
  uint8_t key[16] = {
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  };
  v128_t k;
  v128_set_to_zero(&x);
  
  v128_copy_octet_string(&k, key);
  v128_copy_octet_string(&x, plaintext);
  aes_expand_encryption_key(k, expanded_key);
  aes_expand_decryption_key(k, decrypt_key);
  aes_encrypt(&x, expanded_key);
  aes_decrypt(&x, decrypt_key);
  
  /* compare to expected value then report */
  v128_copy_octet_string(&y, plaintext);

  if (v128_is_eq(&x, &y))
    return err_status_ok;
  return err_status_algo_fail;
  
}
int EncryptionModule::data_in(char *buf, size_t *datalen, size_t *buflen) {
	if (!handshake_done_) {
		return handshake_in(buf, datalen, buflen);
	}

	/* to decrease the chance we don't receive the full message,
	   don't process unless we're on an AES_BLOCK boundary.
	   FIXME: still possible we don't get all blocks; to fix this we
	   really need an SSL header w/ message size up front */
	if (*datalen % AES_BLOCK_SIZE != 0) {
		WARN("data_in can only process on AES block boundaries; waiting for more data\n");
		return ERR_NEED_MORE_DATA;
	}
	/* alloc a temp buffer to hold the ciphertext */
	char* ciphertext = (char *)malloc(*buflen);
    memcpy(ciphertext, buf, *datalen);
	/* decrpyt the ciphertext */
	int plaintext_len = aes_decrypt(de_,
							(unsigned char*)buf, *buflen,
							(unsigned char*)ciphertext, *datalen);
	if (plaintext_len <= 0) {
		ERROR("ERROR decrypting message");
		return ERR_GENERIC;
	}
	
	/* free the temp buffer */
	free(ciphertext);

	/* update sizes */
	*datalen = plaintext_len;

	return 0;
}
示例#4
0
int processDecryptBuf(UINT8 *recvbuf, UINT8 *decry_buf, int len)
{
	UINT8	tmp_block[16];

	int 	offset=0;
	int 	i, leftnum=0;
	int 	realLen=0;

	//make block 
	while(len-offset>=16){
		for (i = 0; i < 16; ++i){
			tmp_block[i]=recvbuf[offset+i];
		}
		aes_decrypt(tmp_block,key);
		for (i = offset; i < offset+16; ++i){
			decry_buf[i]=tmp_block[i-offset];
		}
		offset+=16;
	}

	//realLen = ParsingDecrypt(decry_buf, offset);
	realLen=len-decry_buf[len-1];

	return realLen;
}
示例#5
0
文件: test_aes.c 项目: behemot/pm
int main(int argc, char* argv[])
{
	char* p = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
	char* k = "\x10\xa5\x88\x69\xd7\x4b\xe5\xa3\x74\xcf\x86\x7c\xfb\x47\x38\x59";
	char* c = "\x6d\x25\x1e\x69\x44\xb0\x51\xe0\x4e\xaa\x6f\xb4\xdb\xf7\x84\x65";
	byte key[16], cipher[16], plain[16];
	memcpy(key, k, 16);
	memcpy(plain, p, 16);

	aes_ctx *ctx = aes_create_ctx(key);

	aes_encrypt(ctx, plain, cipher);
	print_hex(cipher, 16);
	for (int i = 0; i < 16; i++) {
		if ((byte)c[i] != cipher[i]) {
			fprintf(stderr, "ERROR on encrypt test vector\n");
			//exit(EXIT_FAILURE);
		}
	}
	printf("AES encrypt test vector ok\n");

	aes_decrypt(ctx, cipher, plain);
	print_hex(plain, 16);
	for (int i = 0; i < 16; i++) {
		if ((byte)p[i] != plain[i]) {
			fprintf(stderr, "ERROR on decrypt test vector\n");
			exit(EXIT_FAILURE);
		}
	}
	printf("AES decrypt test vector ok\n");

	return 0;
}
示例#6
0
void aes_decryptCBC(u8 *data,u8 *key)
{
  u8 buffer[16];
  u8 buffer_d[16];
  u8 i;
  for(i=0;i<16;i++)
  {
    buffer[i] = data[i];
    buffer_d[i] = data[i];
  }
  aes_decrypt(buffer,key);
  for(i=0;i<16;i++) data[i] = buffer[i] ^ 0x00; //^ 0;
  for(i=0;i<16;i++) buffer[i] = data[16+i];
  aes_decrypt(buffer,key);
  for(i=0;i<16;i++) data[i+16] = buffer[i]^buffer_d[i];
}
示例#7
0
void aes_decryptCBC(u8 data[32],u8 key1[16],u8 vector[16])
{
	u8 buffer[16];
	u8 buffer_d[16];
	u8 i;
	for(i=0;i<16;++i) 
	{
		buffer[i]= data[i]; 
		buffer_d[i] = data[i];
	}
	aes_decrypt(buffer,key1);
	for(i=0;i<16;++i) data[i] = buffer[i] ^ vector[i]; 
	for (i=0;i<16;i++) buffer[i] = data[16+i]; 
	aes_decrypt(buffer,key1); // AESap(AESap(ID^IV) ^ AESc(RANDOM))
	for (i=0;i<16;i++) data[i+16] = buffer[i]^buffer_d[i];
}
示例#8
0
void decrypt_buffer(u16 index, u8 *source, u8 *dest, u32 len) 
{
	static u8 iv[16];
	memset(iv, 0, 16);
	memcpy(iv, &index, 2);
	aes_decrypt(iv, source, dest, len);
}
示例#9
0
int AES128_Decrypt2048(unsigned char* message,const unsigned char* MAC,const unsigned char* cipher,const unsigned char*sharekey,const unsigned char*IV){   
	size8_t i,j;
	size32_t k;
    	//==================SHA256========================
    	//unsigned int BUFLEN = 16384;
    	unsigned char   buf[256]={0};
	for(k=0;k<256;k++)buf[k]=cipher[k];
    	sha256(buf,256);
	int flag=0;
	for(i=0;i<16;i++){
		if(MAC[i]!=buf[i]){
			flag=1;
			break;
		}
	}
	
	if(flag==1)return 0;



   	//==========DECRYPT===========
    	unsigned char d[16][16];
	unsigned char c[16][16];
	for(i=0;i<16;i++)
		for(j=0;j<16;j++)
		c[i][j]=cipher[i*16+j];

	/*for(i=0;i<16;i++){
		printf("rec_cipher %d:",i);
	 	for(k=0;k<16;k++)
			printf("%x ",c[i][k]);printf("\n");
    	}*/
	

 	aes_key key;
 	aes_set_decrypt_key(&key, sharekey, 128);
    
	const unsigned char *XOR_data;
    	for(i=0;i<16;i++){
        	XOR_data=(i==15)?IV:c[15-i-1];
        	aes_decrypt(&key, c[15-i] , d[15-i]);
        	for(j=0;j<16;j++){
            	d[15-i][j]=XOR_data[j]^d[15-i][j];
        	}
    	}
/*    
	for(i=0;i<16;i++){
    	    printf("dec_cipher %d:",i);
    	    for(k=0;k<16;k++)printf("%x ",d[i][k]);printf("\n");
    	}
*/
    	//==========DECRYPT===========
    	for(i=0;i<16;i++)
    	    for(j=0;j<16;j++)
		message[i*16+j]=d[i][j];


	return 1;
}
示例#10
0
void
AESAlgorithm::Decrypt(ThreadContext& context, uint8 *data, size_t length)
{
//dprintf("  aes-decrypt-pre:  %x (%d: %x)\n", *(int*)data, fDecryptScheduler, *(int*)context.BufferFor(fDecryptScheduler));
	aes_decrypt(data, data,
		(const aes_decrypt_ctx*)context.BufferFor(fDecryptScheduler));
//dprintf("  aes-decrypt-post: %x\n", *(int*)data);
}
示例#11
0
	// AES
	void CipherAES::Decrypt (byte *data) const
	{
#ifdef TC_AES_HW_CPU
		if (IsHwSupportAvailable())
			aes_hw_cpu_decrypt (ScheduledKey.Ptr() + sizeof (aes_encrypt_ctx), data);
		else
#endif
			aes_decrypt (data, data, (aes_decrypt_ctx *) (ScheduledKey.Ptr() + sizeof (aes_encrypt_ctx)));
	}
示例#12
0
std::string CCCrypto::aes_decrypt(const std::string &sData, const std::string &sKey)
{
	std::string sResult;
	if (!aes_decrypt(sData, sKey, sResult))
	{
		throw std::runtime_error("aes decrypt");
	}
	return sResult;
}
示例#13
0
void * consumer_thread(void *arg) {
	unsigned int bytes_processed=0;
	_thr_data *thd=(_thr_data *)arg;
  EVP_CIPHER_CTX en, de;
  if (aes_init(key_data, key_data_len, (unsigned char *)&salt, &en, &de)) {
    fprintf(stderr,"Couldn't initialize AES cipher\n");
    exit(EXIT_FAILURE);
  }

//	pthread_mutex_lock(&thd->mu);
//		pthread_cond_signal(&thd->sig_producer);
//	pthread_cond_wait(&thd->sig_consumer, &thd->mu);
	int t=1;

//	pthread_mutex_lock(&thd->mu);
//	if (thd->bufsize >= 0) pthread_cond_wait(&thd->sig_consumer, &thd->mu);

	while (t) {
	while (__sync_add_and_fetch(&thd->_lock,0) != 1) { usleep(USLEEP_TIME); }
//	while (thd->bufsize >= 0) {
		//fprintf(stderr,"Line: %d -- Consumer reading buff %d\n", __LINE__, thd->bufsize);
//		pthread_cond_wait(&thd->sig_consumer, &thd->mu);
		//if (thd->bufsize >= 0) pthread_cond_wait(&thd->sig_consumer, &thd->mu);
		//if (thd->bufsize == 0) pthread_cond_wait(&thd->sig_consumer, &thd->mu);
//		if (__sync_add_and_fetch(&thd->_lock,0)==0) pthread_cond_wait(&thd->sig_consumer, &thd->mu);
//		if (thd->bufsize==0) {
//			fprintf(stderr,"Line: %d -- Consumer mutex unlock\n", __LINE__);
//			pthread_mutex_unlock(&thd->mu);
//			continue;
//		}
		if (thd->bufsize==-1) {
//			pthread_mutex_unlock(&thd->mu);
			t=0;
			__sync_sub_and_fetch(&thd->_lock,1);
			continue;
		}
		
//		pthread_cond_signal(&thd->sig_producer);
		//unsigned char *ciphertext;
		//fprintf(stderr,"%d\n", rb);
		if (decrypt) {
			thd->ciphertext=aes_decrypt(&de, thd->buff, &thd->bufsize);
		} else {
			thd->ciphertext=aes_encrypt(&en, thd->buff, &thd->bufsize, NULL);
		}
		__sync_sub_and_fetch(&thd->_lock,1);
		//write(1,ciphertext,rl);
//		int rc=write(1,ciphertext,thd->bufsize);
//		assert(rc==thd->bufsize);
//		free(thd->ciphertext);
		
	}	
  EVP_CIPHER_CTX_cleanup(&en);
  EVP_CIPHER_CTX_cleanup(&de);
	return NULL;
}
示例#14
0
static int32_t camd35_recv(uchar *buf, int32_t rs)
{
	int32_t rc = 0, s, n=0, buflen=0, len=0;
	for (s=0; !rc; s++) {
		switch(s) {
			case 0:
				if (rs < 36) {
					rc = -1;
					goto out;
				}
				break;
			case 1:
				switch (camd35_auth_client(buf)) {
					case  0:        break;	// ok
					case  1: rc=-2; break;	// unknown user
				}
				memmove(buf, buf+4, rs-=4);
				break;
			case 2:
				aes_decrypt(&cl_aes_keys, buf, rs);
				if (rs!=boundary(4, rs))
					cs_log_debug("WARNING: packet size has wrong decryption boundary");

				n=(buf[0]==3) ? 0x34 : 0;

				//Fix for ECM request size > 255 (use ecm length field)
				if(buf[0]==0)
					buflen = (((buf[21]&0x0f)<< 8) | buf[22])+3;
				else
					buflen = buf[1];

				n = boundary(4, n+20+buflen);

				cs_log_debug("received %d bytes from client", rs);

				if (n<rs)
					cs_log_debug("ignoring %d bytes of garbage", rs-n);
				else
					if (n>rs) rc=-3;
				break;
			case 3:
				if (crc32(0L, buf+20, buflen)!=b2i(4, buf+4)) rc=-4;
				if (!rc) rc=n;
				break;
		}
	}

out:
	switch(rc) {
		case -2:	cs_log("unknown user");			break;
		case -3:	cs_log("incomplete request!");			break;
		case -4:	cs_log("checksum error (wrong password?)");	break;
	}

	return(rc);
}
示例#15
0
void
aes_decrypt_with_raw_key(void *ciphertext, const void *key, int key_len) {
#ifndef OPENSSL
//FIXME: need to get this working through the crypto module interface
  aes_expanded_key_t expanded_key;

  aes_expand_decryption_key(key, key_len, &expanded_key);
  aes_decrypt(ciphertext, &expanded_key);
#endif
}
示例#16
0
/**
 * aes_unwrap - Unwrap key with AES Key Wrap Algorithm (RFC3394)
 * @kek: Key encryption key (KEK)
 * @kek_len: Length of KEK in octets
 * @n: Length of the plaintext key in 64-bit units; e.g., 2 = 128-bit = 16
 * bytes
 * @cipher: Wrapped key to be unwrapped, (n + 1) * 64 bits
 * @plain: Plaintext key, n * 64 bits
 * Returns: 0 on success, -1 on failure (e.g., integrity verification failed)
 */
int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
	       u8 *plain)
{
	u8 a[8], *r, b[AES_BLOCK_SIZE];
	int i, j;
	void *ctx;
	unsigned int t;

	/* 1) Initialize variables. */
	memcpy(a, cipher, 8);
	r = plain;
	memcpy(r, cipher + 8, 8 * n);

	ctx = aes_decrypt_init(kek, kek_len);
	if (ctx == NULL)
		return -1;

	/* 2) Compute intermediate values.
	 * For j = 5 to 0
	 *     For i = n to 1
	 *         B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i
	 *         A = MSB(64, B)
	 *         R[i] = LSB(64, B)
	 */
	for (j = 5; j >= 0; j--) {
		r = plain + (n - 1) * 8;
		for (i = n; i >= 1; i--) {
			memcpy(b, a, 8);
			t = n * j + i;
			b[7] ^= t;
			b[6] ^= t >> 8;
			b[5] ^= t >> 16;
			b[4] ^= t >> 24;

			memcpy(b + 8, r, 8);
			aes_decrypt(ctx, b, b);
			memcpy(a, b, 8);
			memcpy(r, b + 8, 8);
			r -= 8;
		}
	}
	aes_decrypt_deinit(ctx);

	/* 3) Output results.
	 *
	 * These are already in @plain due to the location of temporary
	 * variables. Just verify that the IV matches with the expected value.
	 */
	for (i = 0; i < 8; i++) {
		if (a[i] != 0xa6)
			return -1;
	}

	return 0;
}
示例#17
0
// pick up a job from the head of job queue
int crypto_consume_job(void *data)
{
	struct crypto_job *nj;
	char *key = "justice";

	printk(KERN_ALERT "[CRYPTO] kernel thread %d is created\n", current->pid);

	do {
		// wait until job is available
		wait_event_interruptible(cq_wq, cq.num>0||cq.stop==1);
		mutex_lock(&cq_mutex);
		nj = NULL;
		if(cq.num > 0)
		{
			// get the job from queue
			nj = cq.head;
			cq.head = cq.head->next;
			nj->next = NULL;
			cq.num--;

			if(cq.num == 0)
				cq.tail = NULL;
		}
		mutex_unlock(&cq_mutex);

		// process the job
		if(nj != NULL)
		{
			printk(KERN_ALERT "[CRYPTO] Thread %d process job %d\n", current->pid, nj->job_id);
			
			if(nj->operation == KVM_ENCRYPTION)
			{
				aes_encrypt((u8 *)nj->input, (u8 *)nj->output, key, nj->len);
			}
			else if(nj->operation == KVM_DECRYPTION)
			{

				aes_decrypt((u8 *)nj->input, (u8 *)nj->output, key, nj->len);
			}
			else
			{
				printk(KERN_ALERT "[CRYPTO] Error: unkown operation\n");
			}

			// wake up the pending finish queue
			nj->status = 1;
			wake_up_interruptible(&finish_wq);

			schedule();
		}

	} while(!kthread_should_stop() && cq.stop!=1);

	return 0;
}
示例#18
0
文件: main.c 项目: adamselevan/dicio
void EncryptionTask()
{

uint8_t i, data[32], len;

  while(1) {
	
   nrk_led_toggle(ORANGE_LED);
   
   /* 16-byte AES key */
   uint8_t key[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};  
   /* 32-byte string is used as example plaintext data */
   sprintf(data, "This string is used as an input");
   len = 32;

   /* Set AES encryption key
   ** (must be 16 bytes) */
   aes_setkey(key);
   
   printf("key hex (16B):   ");
   for(i=0; i<16; i++)
      printf("%x ", key[i]);
   printf("\r\n");   
   printf("plaintext (%dB): \"%s\"\r\n", len, data);
   printf("plaintext hex:   ");
   for(i=0; i<len; i++)
      printf("%x ", data[i]);
   printf("\r\n");
  
   /* Encrypt data (len must be
   ** a multiple of 16 bytes) */
   aes_encrypt(data, len);
   
   printf("\t\t\t\t...encrypt...\r\n");
   printf("cyphertext hex:  ");
   for(i=0; i<len; i++)
      printf("%x ", data[i]);
   printf("\r\n");
   
   /* Decrypt data (len must be
   ** a multiple of 16 bytes) */
   aes_decrypt(data, len);
   
   printf("\t\t\t\t...decrypt...\r\n");
   printf("plaintext hex:   ");
   for(i=0; i<len; i++)
      printf("%x ", data[i]);
   printf("\r\n\r\n\r\n");


   nrk_wait_until_next_period();

	}
}
示例#19
0
void aes_speed_test()
{
   unsigned int key_sz=128;//256;
   unsigned char pt[16]; memcpy(pt,"0123456789ABCDEF",16);
   unsigned char ct[16];
   unsigned char *key=(unsigned char *)"0123456789ABCDEF01234566789ABCDEF";
   unsigned char key_expansion[(14+1)*4*4];

    aes_key_expansion(
      key_sz,
      key_expansion,
      key,
      1
    );
    unsigned const nTimes=100;
    unsigned const nBlocks=100;
    uint64_t cycles_start, cycles_end;
    std::vector<uint64_t> runtimes(nTimes);
    for (unsigned j = 0; j < nTimes; ++j)
   {
    cycles_start = get_cycles();
    for (unsigned i = 0; i < nBlocks; ++i)
    {
      aes_encrypt(
        key_sz,
        key_expansion,
        pt,
        ct
      );
    }
    cycles_end = get_cycles();
    runtimes[j] = cycles_end - cycles_start;
    }
    std::sort(runtimes.begin(),runtimes.end());
    printf("AES-128 Encryption Cycles per byte: %.2f\n",double(runtimes[nTimes/2])/double(nBlocks*16));

    for (unsigned j = 0; j < nTimes; ++j)
    {
      cycles_start = get_cycles();
      for (unsigned i = 0; i < nBlocks; ++i)
      {
        aes_decrypt(
          key_sz,
          key_expansion,
          pt,
          ct
        );
      }
      cycles_end = get_cycles();
      runtimes[j] = cycles_end - cycles_start;
    }
    std::sort(runtimes.begin(), runtimes.end());
    printf("AES-128 Decryption Cycles per byte: %.2f\n",double(runtimes[nTimes/2])/double(nBlocks*16));
}
示例#20
0
u32 DecryptTitlekeys(void)
{
    EncKeysInfo *info = (EncKeysInfo*)0x20316000;

    if (!DebugFileOpen("/encTitleKeys.bin"))
        return 1;
    
    if (!DebugFileRead(info, 16, 0)) {
        FileClose();
        return 1;
    }

    if (!info->n_entries || info->n_entries > MAX_ENTRIES) {
        Debug("Too many/few entries specified: %i", info->n_entries);
        FileClose();
        return 1;
    }

    Debug("Number of entries: %i", info->n_entries);
    if (!DebugFileRead(info->entries, info->n_entries * sizeof(TitleKeyEntry), 16)) {
        FileClose();
        return 1;
    }
    
    FileClose();

    Debug("Decrypting Title Keys...");

    u8 ctr[16] __attribute__((aligned(32)));
    u8 keyY[16] __attribute__((aligned(32)));
    u32 i;
    for (i = 0; i < info->n_entries; i++) {
        memset(ctr, 0, 16);
        memcpy(ctr, info->entries[i].titleId, 8);
        set_ctr(AES_BIG_INPUT|AES_NORMAL_INPUT, ctr);
        memcpy(keyY, (void *)common_keyy[info->entries[i].commonKeyIndex], 16);
        setup_aeskey(0x3D, AES_BIG_INPUT|AES_NORMAL_INPUT, keyY);
        use_aeskey(0x3D);
        aes_decrypt(info->entries[i].encryptedTitleKey, info->entries[i].encryptedTitleKey, ctr, 1, AES_CBC_DECRYPT_MODE);
    }

    if (!DebugFileCreate("/decTitleKeys.bin", true))
        return 1;
    if (!DebugFileWrite(info, info->n_entries * sizeof(TitleKeyEntry) + 16, 0)) {
        FileClose();
        return 1;
    }
    FileClose();

    Debug("Done!");

    return 0;
}
示例#21
0
void DecipherBlock(int cipher, void *data, void *ks)
{
#ifdef GST_WINDOWS_BOOT_AES
	if (IsAesHwCpuSupported())
		aes_hw_cpu_decrypt ((byte *) ks + sizeof (aes_encrypt_ctx) + 14 * 16, data);
	else
		aes_decrypt (data, data, (aes_decrypt_ctx *) ((byte *) ks + sizeof(aes_encrypt_ctx))); 
#elif defined (GST_WINDOWS_BOOT_SERPENT)
	serpent_decrypt (data, data, ks);
#elif defined (GST_WINDOWS_BOOT_TWOFISH)
	twofish_decrypt (ks, data, data);
#endif
}
示例#22
0
static int32_t camd33_recv(struct s_client * client, uchar *buf, int32_t l)
{
  int32_t n;
  if (!client->pfd) return(-1);
  if ((n=recv(client->pfd, buf, l, 0))>0)
  {
    client->last=time((time_t *) 0);
    if (client->crypted)
      aes_decrypt(buf, n);
  }
  cs_ddump_mask(D_CLIENT, buf, n, "received %d bytes from client", n);
  return(n);
}
示例#23
0
文件: main.c 项目: Fat-Zer/semestr-6
int main(int argc, char *argv[])
{
//	unsigned char key[KEY_128] = {
//		0xff, 0xfe, 0xfd, 0xfc,
//		0xfb, 0xfa, 0xf9, 0xf8,
//		0xf7, 0xf6, 0xf5, 0xf4,
//		0xf3, 0xf2, 0xf1, 0xf0
//	};
	unsigned char key[16] = {
		0x00, 0x01, 0x02, 0x03,
		0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0a, 0x0b,
		0x0c, 0x0d, 0x0e, 0x0f
	};
	
//	unsigned char ptext[16] = {
//		0x00, 0x01, 0x02, 0x03,
//		0x04, 0x05, 0x06, 0x07,
//		0x08, 0x09, 0x0a, 0x0b,
//		0x0c, 0x0d, 0x0e, 0x0f
//	};
	unsigned char ptext[16] = {
		0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00
	};
	unsigned char ctext[16];
	unsigned char decptext[16];
	aes_ctx_t *ctx;
	int i;

	init_aes();
	ctx = aes_alloc_ctx(key, sizeof(key));
	if(!ctx) {
		perror("aes_alloc_ctx");
		return EXIT_FAILURE;
	}
	print_block_paralel("keys ", 1, 0x40, 4,
		ctx->keysched);
	print_block_paralel("keys ", 3, 16, 4,
		ptext, ctext, decptext);
	aes_encrypt(ctx, ptext, ctext);
	aes_decrypt(ctx, ctext, decptext);
	i = 16;
	print_block_paralel("plaintext ciphertext dectext ", 3, 16, 4,
		ptext, ctext, decptext);
	
	aes_free_ctx(ctx);
	return EXIT_SUCCESS;
}
示例#24
0
int main() {
    EVP_CIPHER_CTX context;
    unsigned char key[KEY_LENGTH/8]; // AES key
    unsigned char iv[KEY_LENGTH/8];  // IV
    unsigned char *enc_msg;          // Encrypted message
    char *dec_msg;                   // Decrypted message

    // Init the decyption context
    EVP_CIPHER_CTX_init(&context);

    // Read the encrypted message, key, and IV
    printf("Reading encrypted message and attempting decryption...\n");

    enc_msg = malloc(BUFFER);

    // Read the encrypted message
    FILE *fd = fopen(MSG_FILENAME, "r");

    // Determine size of the encrypted message file
    fseek(fd, 0L, SEEK_END);
    size_t enc_msg_len = ftell(fd);
    fseek(fd, 0, SEEK_SET);

    size_t enc_len = fread(enc_msg, 1, enc_msg_len, fd);
    fclose(fd);

    // Read the key
    fd = fopen(KEY_FILENAME, "r");
    fread(key, 1, KEY_LENGTH/8, fd);
    fclose(fd);

    // Read the IV
    fd = fopen(IV_FILENAME, "r");
    fread(iv, 1, KEY_LENGTH/8, fd);
    fclose(fd);


    // Decrypt it
    aes_decrypt(&context, key, iv, enc_msg, enc_len, &dec_msg);
    printf("Decrypted message: %s\n", dec_msg);


    // Clean up...
    EVP_CIPHER_CTX_cleanup(&context);

    free(enc_msg);
    free(dec_msg);
    enc_msg = NULL;
    dec_msg = NULL;
    return 0;
}
示例#25
0
void QAesWrap::ecbdecrypt(const BYTE *in, size_t size, QByteArray & out) const
{
    BYTE buf_in[AES_BLOCK_SIZE] = {0}, buf_out[AES_BLOCK_SIZE] = {0};
    int blocks, idx;
    char * data = out.data();
    blocks = size / AES_BLOCK_SIZE;
    memset(buf_out,0,AES_BLOCK_SIZE);
    for (idx = 0; idx < blocks; idx++) {
        memcpy(buf_in, &in[idx * AES_BLOCK_SIZE], AES_BLOCK_SIZE);
        aes_decrypt(buf_in, buf_out, mpass, mbit);
        memcpy(&data[idx * AES_BLOCK_SIZE], buf_out, AES_BLOCK_SIZE);
        memset(buf_out,0,AES_BLOCK_SIZE);
    }
}
示例#26
0
static void _decrypt_title_key(u8 *tik, u8 *title_key)
{
	u8 common_key[16] = {
		0xeb, 0xe4, 0x2a, 0x22, 0x5e, 0x85, 0x93, 0xe4, 0x48, 0xd9, 0xc5, 0x45,
		0x73, 0x81, 0xaa, 0xf7
	};
	u8 iv[16];

	wbfs_memset(iv, 0, sizeof iv);
	wbfs_memcpy(iv, tik + 0x01dc, 8);
	aes_set_key(common_key);
	//_aes_cbc_dec(common_key, iv, tik + 0x01bf, 16, title_key);
	aes_decrypt(iv, tik + 0x01bf, title_key, 16);
}
示例#27
0
static int crypto_aes_decrypt(lua_State *L) //String=encrypt(String,String,String,bool)
{
    size_t avail_in;
    const unsigned char *in = (const unsigned char *) luaL_checklstring(L, 1, &avail_in);
    const unsigned char *key = (const unsigned char *) luaL_checkstring(L, 2);
    const unsigned char *iv = (const unsigned char *) luaL_optstring(L, 3,NULL);
    bool mode=lua_toboolean(L,4);
    avail_in&=(~15);
    unsigned char *out=(unsigned char *) malloc(avail_in);
    aes_decrypt(in,out,avail_in,key,iv,mode?1:0);
    lua_pushlstring(L, (const char *)out,avail_in);
    free(out);
    return 1;
}
示例#28
0
void DecipherBlock(int cipher, void *data, void *ks)
{
	switch (cipher)
	{
	case SERPENT:	serpent_decrypt (data, data, ks); break;
	case TWOFISH:	twofish_decrypt (ks, data, data); break;
#ifndef TC_WINDOWS_BOOT

	case AES:
#if defined (_WIN64) || !defined (TC_WINDOWS_DRIVER)
		if (IsAesHwCpuSupported())
			aes_hw_cpu_decrypt ((byte *) ks + sizeof (aes_encrypt_ctx), data);
		else
#endif
			aes_decrypt (data, data, (void *) ((char *) ks + sizeof(aes_encrypt_ctx)));
		break;

#else
	case AES:		aes_decrypt (data, data, ks); break;
#endif
	default:		TC_THROW_FATAL_EXCEPTION;	// Unknown/wrong ID
	}
}
示例#29
0
static int update(RCrypto *cry, const ut8 *buf, int len) {
	if (!iv_set) {
		eprintf ("IV not set. Use -I [iv]\n");
		return false;
	}
	const int diff = (BLOCK_SIZE - (len % BLOCK_SIZE)) % BLOCK_SIZE;
	const int size = len + diff;
	const int blocks = size / BLOCK_SIZE;

	ut8 *const obuf = calloc (1, size);
	if (!obuf) return false;

	ut8 *const ibuf = calloc (1, size);
	if (!ibuf) {
		free (obuf);
		return false;
	}

	memset (ibuf, 0, size);
	memcpy (ibuf, buf, len);

	if (diff) {
		ibuf[len] = 0b1000;
	}

	int i, j;
	if (flag == 0) {
		for (i = 0; i < blocks; i++) {
			for (j = 0; j < BLOCK_SIZE; j++) {
				ibuf[i * BLOCK_SIZE + j] ^= iv[j];
			}
			aes_encrypt (&st, ibuf + BLOCK_SIZE * i, obuf + BLOCK_SIZE * i);
			memcpy (iv, obuf + BLOCK_SIZE * i, BLOCK_SIZE);
		}
	} else if (flag == 1) {
		for (i = 0; i < blocks; i++) {
			aes_decrypt (&st, ibuf + BLOCK_SIZE * i, obuf + BLOCK_SIZE * i);
			for (j = 0; j < BLOCK_SIZE; j++) {
				obuf[i * BLOCK_SIZE + j] ^= iv[j];
			}
			memcpy(iv, buf + BLOCK_SIZE * i, BLOCK_SIZE);
		}
	}

	r_crypto_append (cry, obuf, size);
	free (obuf);
	free (ibuf);
	return 0;
}
示例#30
0
int
crypto_buf(crypto_ctx_t *cctx, uchar_t *from, uchar_t *to, int64_t bytes, uint64_t id)
{
	if (cctx->crypto_alg == CRYPTO_ALG_AES) {
		if (cctx->enc_dec == ENCRYPT_FLAG) {
			return (aes_encrypt(cctx->crypto_ctx, from, to, bytes, id));
		} else {
			return (aes_decrypt(cctx->crypto_ctx, from, to, bytes, id));
		}
	} else {
		fprintf(stderr, "Unrecognized algorithm code: %d\n", cctx->crypto_alg);
		return (-1);
	}
	return (0);
}