示例#1
0
void aes_decrypt(struct aes_keys *aes, uchar *buf, int32_t n)
{
	int32_t i;
	for(i = 0; i < n; i += 16)
	{
		AES_decrypt(buf + i, buf + i, &aes->aeskey_decrypt);
	}
}
示例#2
0
void
AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
		unsigned long size, const AES_KEY *key,
		unsigned char *iv, int forward_encrypt)
{
    unsigned char tmp[AES_BLOCK_SIZE];
    int i;

    if (forward_encrypt) {
	while (size >= AES_BLOCK_SIZE) {
	    for (i = 0; i < AES_BLOCK_SIZE; i++)
		tmp[i] = in[i] ^ iv[i];
	    AES_encrypt(tmp, out, key);
	    memcpy(iv, out, AES_BLOCK_SIZE);
	    size -= AES_BLOCK_SIZE;
	    in += AES_BLOCK_SIZE;
	    out += AES_BLOCK_SIZE;
	}
	if (size) {
	    for (i = 0; i < size; i++)
		tmp[i] = in[i] ^ iv[i];
	    for (i = size; i < AES_BLOCK_SIZE; i++)
		tmp[i] = iv[i];
	    AES_encrypt(tmp, out, key);
	    memcpy(iv, out, AES_BLOCK_SIZE);
	}
    } else {
	while (size >= AES_BLOCK_SIZE) {
	    memcpy(tmp, in, AES_BLOCK_SIZE);
	    AES_decrypt(tmp, out, key);
	    for (i = 0; i < AES_BLOCK_SIZE; i++)
		out[i] ^= iv[i];
	    memcpy(iv, tmp, AES_BLOCK_SIZE);
	    size -= AES_BLOCK_SIZE;
	    in += AES_BLOCK_SIZE;
	    out += AES_BLOCK_SIZE;
	}
	if (size) {
	    memcpy(tmp, in, AES_BLOCK_SIZE);
	    AES_decrypt(tmp, out, key);
	    for (i = 0; i < size; i++)
		out[i] ^= iv[i];
	    memcpy(iv, tmp, AES_BLOCK_SIZE);
	}
    }
}
示例#3
0
int crypt_dec_block(cipher_context *cctx, uint8 *in, uint8 *out, int len)
{
	if(len%cctx->block_size)
		return 0;
		
	switch(cctx->alg){
	case ALG_AES_ECB:
	case ALG_AES_CTR:
	{
		int j;

		for(j=0; j<len; j+=cctx->block_size){
			AES_decrypt(in+j, out+j, cctx->enc_ctx);
		}
		return 1;
	}
	case ALG_AES_CBC:
	{
		int i, j;
		uint8 temp[MAX_CIPHER_BLOCK_SIZE];

		for(j=0; j<len; j+=cctx->block_size){
			memcpy(temp, in+j, cctx->block_size);
			AES_decrypt(temp, out+j, cctx->enc_ctx);
			for(i=0; i<cctx->block_size; i++){
				out[j+i]^=cctx->iv[i];
			}
			
			memcpy(cctx->iv, temp, cctx->block_size);
		}
		return 1;
	}
	case ALG_SM4_ECB:
	case ALG_SM4_CTR:
		sm4_crypt_ecb(cctx->enc_ctx, SM4_DECRYPT, len, in, out);
		return 1;
		break;
	case ALG_SM4_CBC:
		sm4_crypt_cbc(cctx->enc_ctx, SM4_DECRYPT, len, cctx->iv, in, out);
		return 1;
		break;
	}
	
	return 0;
}
示例#4
0
 ustring aes_decode(ustring const& passphrase, ustring const& indata, int length) {
     AES_KEY key;
     AES_set_decrypt_key(passphrase.c_str(), 8 * length, &key);
     unsigned char* outdata = new unsigned char[length];
     AES_decrypt(indata.c_str(), outdata, &key);
     ustring res(outdata, length);
     delete[] outdata;
     return res;
 }
示例#5
0
static void test_xts_aes_decrypt(const void *ctx,
                                 size_t length,
                                 uint8_t *dst,
                                 const uint8_t *src)
{
    const struct TestAES *aesctx = ctx;

    AES_decrypt(src, dst, &aesctx->dec);
}
int cbcdec(unsigned char* CText, int length){
  unsigned char MBlock[16];
  unsigned char CBlock_cur[16];
  unsigned char CBlock_prev[16];
  unsigned char Key[16];
  int i, j, tmp;
  FILE *fpOut;
  AES_KEY AESkey;

  // This is just for illustration; the actual key used was not the all-0 key!
  Key[0] = Key[1] = Key[2] = Key[3] = 0x00;
  Key[4] = Key[5] = Key[6] = Key[7] = 0x00;
  Key[8] = Key[9] = Key[10] = Key[11] = 0x00;
  Key[12] = Key[13] = Key[14] = Key[15] = 0x00;

  AES_set_decrypt_key((const unsigned char *) Key, 128, &AESkey);

  if (length < 2) return 0;

  for (i=0; i<16; i++)
    CBlock_prev[i] = CText[i];

  j = 1;

  while (j < length) {
    for (i=0; i<16; i++)
      CBlock_cur[i] = CText[16*j+i];

    AES_decrypt((const unsigned char *) CBlock_cur, MBlock, (const AES_KEY *) &AESkey);

    for (i=0; i<16; i++) {
      MBlock[i] ^= CBlock_prev[i];
      // fprintf(fpOut, "%X", MBlock[i]/16), fprintf(fpOut, "%X", MBlock[i]%16);
      // Uncomment this to output the message + the padding for debugging purposes.
      // If we were implementing this for real, we would only output the message
      CBlock_prev[i] = CBlock_cur[i];
    }
    j++;
  }

  j = MBlock[15];
  if ((j==0) || (j>16)) {
   // printf("Error: final byte out of range\n");
    return 0;
  }
  for (i=14; i>=16-j; i--) {
    if (MBlock[i] != j) {
      // printf("Error: incorrect padding\n");
      return 0;
    }
  }

  // printf("Everything fine\n");
  return 1;

}
示例#7
0
void AES_ecb_encrypt(const unsigned char *in, unsigned char *out,
		     const AES_KEY *key, const int enc) 
{
    assert(in && out && key);
	assert((AES_ENCRYPT == enc)||(AES_DECRYPT == enc));

	if (AES_ENCRYPT == enc)
		AES_encrypt(in, out, key);
	else
		AES_decrypt(in, out, key);
}
示例#8
0
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size)
{
    mz_crypt_aes *aes = (mz_crypt_aes *)handle;
    if (aes == NULL || buf == NULL)
        return MZ_PARAM_ERROR;
    if (size != MZ_AES_BLOCK_SIZE)
        return MZ_PARAM_ERROR;

    AES_decrypt(buf, buf, &aes->key);
    return size;
}
示例#9
0
/**
 * mega_aes_key_decrypt_raw:
 * @aes_key: a #MegaAesKey
 * @cipher: (element-type guint8) (array length=len): Ciphertext
 * @plain: (element-type guint8) (array length=len) (out caller-allocates): Plaintext output data
 * @len: 16 byte aligned length of ciphertext and plaintext data.
 *
 * Decrypt ciphertext blocks using AES key
 */
void mega_aes_key_decrypt_raw(MegaAesKey* aes_key, const guchar* cipher, guchar* plain, gsize len)
{
    gsize off;

    g_return_if_fail(MEGA_IS_AES_KEY(aes_key));
    g_return_if_fail(cipher != NULL);
    g_return_if_fail(plain != NULL);
    g_return_if_fail(len % 16 == 0);

    for (off = 0; off < len; off += 16)
        AES_decrypt(cipher + off, plain + off, &aes_key->priv->dec_key);
}
示例#10
0
static int __license_decrypt_time(int fd, time_t *_limit)
{
        int ret, cnt;
        time_t limit;
        uint8_t epad[AES_BLOCK_SIZE];
        uint8_t etime[AES_BLOCK_SIZE], etime_hex[LICENSE_AES_HEXLEN];
        AES_KEY key;

        ret = lseek(fd, 0, SEEK_SET);
        if (ret < 0) {
                ret = errno;
                GOTO(err_ret, ret);
        }

        cnt = read(fd, etime_hex, sizeof(etime_hex));
        if (cnt != sizeof(etime_hex)) {
                ret = EIO;
                GOTO(err_ret, ret);
        }

        if (!memcmp(etime_hex, LICENSE_VERSION_2, sizeof(etime_hex))) {
                cnt = pread(fd, etime_hex, sizeof(etime_hex), sizeof(etime_hex) * 2);
                if (cnt != sizeof(etime_hex)) {
                        ret = EIO;
                        GOTO(err_ret, ret);
                }
        }

        if (!__arrcmp(permanent_free, 10, etime_hex, sizeof(etime_hex))) {
               *_limit = -1;
               return 0;
        }

        ret = __hex2str(etime_hex, sizeof(etime_hex), etime, sizeof(etime));
        if (ret)
                GOTO(err_ret, ret);

        ret = AES_set_decrypt_key((uint8_t *)LICENSE_AES_KEY, 128, &key);
        if (ret < 0) {
                ret = EFAULT;
                GOTO(err_ret, ret);
        }

        AES_decrypt((uint8_t *)etime, (uint8_t *)epad, &key);

        memcpy(&limit, epad, sizeof(time_t));

        *_limit = LICENSE_DECRYPT(limit);

        return 0;
err_ret:
        return ret;
}
示例#11
0
// cipterText: 암호문, plainText: 평문, key: 암호화에 사용할 키, keyLength: 키크기 128, 198, 256
unsigned char* aes_decrypt_text(unsigned char* cipherText, unsigned char* key, int keyLength)
{
	AES_KEY decKey;
	unsigned char* plainText = (unsigned char*)malloc(sizeof(cipherText));

	if (AES_set_decrypt_key(key, keyLength, &decKey) < 0)
		return NULL;

	AES_decrypt(cipherText, plainText, &decKey);

	return plainText;
}
示例#12
0
int edata_sign_free(u8 *edata_buf, u8 *pgd_key)
{
	MAC_KEY mkey;
	AES_ctx aes;
	u8 sha1_hash[20], license_key[16];
	int flag, i;

	printf("re-sign EDATA ...\n");

	flag = *(u8*)(edata_buf+15);

	// get license_key
	if(flag&1){
		sceDrmBBMacInit(&mkey, 3);
		sceDrmBBMacUpdate(&mkey, edata_buf, 0x80);
		bbmac_getkey(&mkey, edata_buf+0x80, license_key);
		if(verbose) hex_dump("license key", license_key, 16);
	}

	// change to use free license
	*(u32*)(edata_buf+8) = 0x01000000;

	// build ecdsa
	ecdsa_set_curve(&ecdsa_app);
	ecdsa_set_priv(priv_key_edata);
	SHA1(edata_buf, 0x58, sha1_hash);
	ecdsa_sign(sha1_hash, edata_buf+0x58, edata_buf+0x6c, NULL);

	// build BBMAC
	if(flag&1){
		sceDrmBBMacInit(&mkey, 3);
		sceDrmBBMacUpdate(&mkey, edata_buf, 0x80);
		sceDrmBBMacFinal(&mkey, edata_buf+0x80, license_key);
		bbmac_build_final2(3, edata_buf+0x80);
	}

	// build PGD key
	sceNpDrmGetFixedKey(pgd_key, (char*)(edata_buf+16), 0x01000000);
	if(verbose) hex_dump("get_fixed_key", pgd_key, 16);

	if(flag&1){
		for(i=0; i<16; i++){
			pgd_key[i] ^= license_key[i];
		}
	}

	AES_set_key(&aes, edat_aeskey, 128);
	AES_decrypt(&aes, pgd_key, pgd_key);
	if(verbose) hex_dump("new PGD key", pgd_key, 16);

	return 0;
}
示例#13
0
void cbc_decrypt(char *encrypted, char *decrypted, size_t num_bytes, char *iv, char *key) {
    AES_KEY aes_key;
    AES_set_decrypt_key((unsigned char*)key, 128, &aes_key);
    char prev[16];
    char buf[16];
    memcpy(prev, iv, 16);
    for (size_t offset = 0; offset < num_bytes; offset += 16) {
    	memcpy(buf, encrypted + offset, 16);
        AES_decrypt((unsigned char*)encrypted + offset, (unsigned char*)decrypted + offset, &aes_key);
    	xor(decrypted + offset, prev, 16, decrypted + offset);
        memcpy(prev, buf, 16);
    }
}
示例#14
0
static int aes_decrypt(EdpPacket* pkg, int remain_pos){
    size_t in_len = 0;
    unsigned char* in = NULL;
    unsigned char* out = NULL;
    size_t offset = 0;
    size_t padding_len = 0;
    uint32 len_aft_dec = 0;
    uint8 tmp_buf[5] = {0};
    EdpPacket tmp_pkg;
    int diff = 0;
    int i = 0;

    in_len = pkg->_write_pos - pkg->_read_pos;
    in = pkg->_data + pkg->_read_pos;
    out = in;

    for (offset=0; (offset+AES_BLOCK_SIZE)<=in_len; offset+=AES_BLOCK_SIZE){
	AES_decrypt(in + offset, out + offset, &g_aes_decrypt_key);
    }

    padding_len = *(in + offset -1) - '0';
    if (padding_len > AES_BLOCK_SIZE){
	return -1;
    }

    /* 解密后的remainlen会变小,其占用空间可能变小
     * 利用一个临时的EdpPacket来测试加密后remainlen的长度是否发生改变
     * 若改变,则解密后的数据应该依次往前移,以消除多余空间
     */
    len_aft_dec = offset - padding_len;
    tmp_pkg._data = tmp_buf;
    tmp_pkg._write_pos = 1;
    tmp_pkg._read_pos = 0;
    WriteRemainlen(&tmp_pkg, len_aft_dec);

    diff = remain_pos - tmp_pkg._write_pos;
    if (diff > 0){
	i = 0;
	for (i=0; i<len_aft_dec; i++){
	    *(in + i - diff)  = *(in + i);
	}
    }
    
    pkg->_write_pos = 1;
    pkg->_read_pos = 0;
    WriteRemainlen(pkg, len_aft_dec);
    pkg->_read_pos = 1;
    pkg->_write_pos += len_aft_dec;

    return len_aft_dec;
}
示例#15
0
QByteArray
NvPairingManager::decrypt(QByteArray ciphertext, AES_KEY* key)
{
    QByteArray plaintext(ciphertext.size(), 0);

    for (int i = 0; i < plaintext.size(); i += 16)
    {
        AES_decrypt(reinterpret_cast<unsigned char*>(&ciphertext.data()[i]),
                    reinterpret_cast<unsigned char*>(&plaintext.data()[i]),
                    key);
    }

    return plaintext;
}
示例#16
0
void encrypt_data(FILE* input_file, FILE* output_file)
{
	unsigned char inbuf[80];
	unsigned char outbuf[80];
	unsigned char decbuf[80];
	int inlen, outlen;

	AES_KEY enc_key;
	AES_KEY dec_key;
	AES_set_encrypt_key(key, 128, &enc_key);

	//while(1) {
		int i;
		inlen = 80;
		outlen = 80;
//		inlen = fread(inbuf, 1, 80, input_file);
		for (i=0; i<inlen; i++) {
			inbuf[i]=0;
		}
		printf("input data is \n");
		for (i = 0; i < inlen; i++) {
			printf("%c ", inbuf[i]);
		}
		printf("\n");

		AES_encrypt(inbuf, outbuf, &enc_key);  

		printf("encrypted data is \n");
		for (i = 0; i < inlen; i++) {
			printf("%X ", outbuf[i]);
		}
		printf("\n");
		
		AES_set_decrypt_key(key, 128, &dec_key);
		AES_decrypt(outbuf, decbuf, &dec_key);  

		printf("decrypted data is \n");
		for (i = 0; i < inlen; i++) {
			printf("%c ", decbuf[i]);
		}
		printf("\n");

		outlen = fwrite(outbuf, 1, inlen, output_file);

//		if (outlen < AES_BLOCK_SIZE)
//		{
//			break;
//		}
	//}
}
示例#17
0
static void qcrypto_cipher_aes_ecb_decrypt(AES_KEY *key,
                                           const void *in,
                                           void *out,
                                           size_t len)
{
    const uint8_t *inptr = in;
    uint8_t *outptr = out;
    while (len) {
        if (len > AES_BLOCK_SIZE) {
            AES_decrypt(inptr, outptr, key);
            inptr += AES_BLOCK_SIZE;
            outptr += AES_BLOCK_SIZE;
            len -= AES_BLOCK_SIZE;
        } else {
            uint8_t tmp1[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
            memcpy(tmp1, inptr, len);
            /* Fill with 0 to avoid valgrind uninitialized reads */
            memset(tmp1 + len, 0, sizeof(tmp1) - len);
            AES_decrypt(tmp1, tmp2, key);
            memcpy(outptr, tmp2, len);
            len = 0;
        }
    }
}
示例#18
0
void decryptImage(unsigned char* srcaddr, unsigned int len, unsigned char* dstaddr) {
	unsigned int remaining = len;
	unsigned int decrypted = 0;
	while (remaining >= AES_BLOCK_SIZE) {
		AES_decrypt(srcaddr, dstaddr, &_gdKeyImage);
		srcaddr += AES_BLOCK_SIZE;
		dstaddr += AES_BLOCK_SIZE;
		remaining -= AES_BLOCK_SIZE;
		decrypted++;
	}
	if (remaining != 0) {
		decrypted = decrypted * AES_BLOCK_SIZE;
		memcpy(dstaddr, srcaddr, remaining);
	}
}
示例#19
0
void decrypt_block(struct poet_ctx *ctx, const uint8_t ciphertext[16],
		   uint8_t plaintext[16])
{
  block tmp;
  BOTTOM_HASH;
  xor_block(ctx->y, ciphertext,ctx->y);

  AES_decrypt(ctx->y, tmp, &(ctx->aes_dec));

  TOP_HASH;
  xor_block(plaintext, tmp,ctx->x);

  memcpy(ctx->x, tmp, BLOCKLEN);
  ctx->mlen+=BLOCKLEN_BITS;
}
示例#20
0
/* Encrypt or decrypt one block with AES and the given key */
static void aesBlock(unsigned char out[16], const unsigned char key[],
                     int keyBits, const unsigned char in[16], int dir)
{
  extern void exit(int status);
  int retCode;
  AES_KEY aesKey;

  if (dir==AES_ENCRYPT)
    retCode = AES_set_encrypt_key(key, keyBits, &aesKey);
  else
    retCode = AES_set_decrypt_key(key, keyBits, &aesKey);
  if (retCode!=0) exit(retCode);       /* Not very gracefull, but still... */

  if (dir==AES_ENCRYPT) AES_encrypt(in, out, &aesKey);
  else                  AES_decrypt(in, out, &aesKey);
}
示例#21
0
int
g_bde_keyloc_decrypt(u_char *sha2, void *input, uint64_t *output)
{
	keyInstance ki;
	cipherInstance ci;
	u_char buf[16];

	AES_init(&ci);
	AES_makekey(&ki, DIR_DECRYPT, G_BDE_KKEYBITS, sha2 + 0);
	AES_decrypt(&ci, &ki, input, buf, sizeof buf);
	*output = le64dec(buf);
	bzero(buf, sizeof buf);
	bzero(&ci, sizeof ci);
	bzero(&ki, sizeof ki);
	return(0);
}
示例#22
0
std::string AESCrypter::decrypt(const std::string &input_cipher)
{
    if (!input_cipher.size()
            || input_cipher.size() % AES_CRYPTER_BLOCK_SIZE_BYTES != 0)
    {
        throw AESBlockSizeException(AES_CRYPTER_BLOCK_SIZE_BYTES,
                "input cipher size must be a multiple of AES block size");
    }
    std::string result(input_cipher.size(), 0);
    for (size_t offs = 0; offs < input_cipher.size();
            offs += AES_CRYPTER_BLOCK_SIZE_BYTES)
    {
        AES_decrypt(TO_CONST_UCHAR(input_cipher) + offs,
                TO_UCHAR(result) + offs, TO_AES_KEY(decrypt_key_));
    }
    return result;
}
示例#23
0
文件: aes.c 项目: LexManos/WardenDLL
uint32_t __stdcall aes_crypt(AES_KEY *aes, uint32_t type, uint32_t direction, uint8_t *ivec, uint32_t length, uint8_t *data, uint8_t *buffer){
	if (direction != AES_ENCRYPT && direction != AES_DECRYPT)
		return AES_UNKNOWN_DIRECTION;

	switch(type){
		case AES_NORMAL:
			if (direction == AES_ENCRYPT)
				AES_encrypt(data, buffer, aes);
			else
				AES_decrypt(data, buffer, aes);
			break;
		case AES_ECB: AES_ecb_encrypt(data, buffer, aes, direction); break;
		case AES_CBC: AES_cbc_encrypt(data, buffer, length, aes, ivec, direction); break;
		default: return AES_UNKNOWN_TYPE;
	}
	return AES_SUCCESS;
}
示例#24
0
void svCrypto::AESCrypt(svAESCrypt mode, AES_KEY &key, uint8_t *src,
	uint32_t length, uint8_t *dst)
{
	uint8_t *ptr_src = src, *ptr_dst = dst;

	switch (mode) {
	case svAES_ENCRYPT:
		for (uint32_t i = 0; i < length; i += AES_BLOCK_SIZE)
			AES_encrypt(ptr_src + i, ptr_dst + i, &key);
		break;

	case svAES_DECRYPT:
		for (uint32_t i = 0; i < length; i += AES_BLOCK_SIZE)
			AES_decrypt(ptr_src + i, ptr_dst + i, &key);
		break;
	}
}
示例#25
0
main(){
  AES_KEY AESkey;
  unsigned char MBlock[16];
  unsigned char MBlock2[16];
  unsigned char CBlock[16];
  unsigned char Key[16];
  int i;

  /* 
   * Key contains the actual 128-bit AES key. AESkey is a data structure 
   * holding a transformed version of the key, for efficiency. 
   */

  Key[0]=1;

  for (i=1; i<=15; i++) {
    Key[i] = 0;
    } 

  AES_set_encrypt_key((const unsigned char *) Key, 128, &AESkey);

  MBlock[0] = 1;

  for (i=1; i<16; i++)
    MBlock[i] = 0;

  AES_encrypt((const unsigned char *) MBlock, CBlock, (const AES_KEY *) &AESkey);
  
  for (i=0; i<16; i++) 
    printf("%X", CBlock[i]/16), printf("%X", CBlock[i]%16);
  printf("\n");

  /* 
   * We need to set AESkey appropriately before inverting AES. 
   * Note that the underlying key Key is the same; just the data structure
   * AESkey is changing (for reasons of efficiency).
   */
  AES_set_decrypt_key((const unsigned char *) Key, 128, &AESkey);
  
  AES_decrypt((const unsigned char *) CBlock, MBlock2, (const AES_KEY *) &AESkey); 

  for (i=0; i<16; i++) 
    printf("%X", MBlock2[i]/16), printf("%X", MBlock2[i]%16);
  printf("\n");
}
示例#26
0
static ERL_NIF_TERM aes_ecb_decrypt(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary in, out, key;
    // key must be binary
    if(!enif_inspect_binary(env, argv[0], &key)) {
        return enif_make_badarg(env);
    }
    // key size should be 16 byte
    if(key.size != 16) {
      return enif_make_badarg(env);
    }
    // cipher must be binary
    if(!enif_inspect_binary(env, argv[1], &in)) {
      return enif_make_badarg(env);
    }
    // cipher shoule be 16 byte block
    if(in.size % 16) {
      return enif_make_badarg(env);
    }

    unsigned char* decoded = (unsigned char*)malloc(sizeof(unsigned char)*in.size);

    struct aes_key_st* decrypt_key = (struct aes_key_st*)malloc(sizeof(AES_KEY));
    memset(decrypt_key, 0, sizeof(AES_KEY));
    AES_set_decrypt_key((unsigned char*)(key.data), 128, decrypt_key);

    int i = 0;
    for(i = 0; i < in.size; i += 16) {
      AES_decrypt((unsigned char*)&in.data[i], (unsigned char*)&decoded[i], decrypt_key);
    }
    //Remove padding
    unsigned char padding = (unsigned char) decoded[in.size-1];

    if(!enif_alloc_binary(in.size - padding, &out)) {
        free(decoded);
        free(decrypt_key);
        return enif_make_badarg(env);
    }
    
    strncpy((unsigned char*)out.data, decoded, in.size - padding);

    free(decoded);
    free(decrypt_key);
    return enif_make_binary(env, &out);
}
示例#27
0
int32_t aes_decrypt_from_list(AES_ENTRY *list, uint16_t caid, uint32_t provid, int32_t keyid, uchar *buf, int32_t n)
{
	AES_ENTRY *current = aes_list_find(list, caid, provid, keyid);
	if(!current)
		{ return 0; }
	AES_KEY dummy;
	int32_t i;
	// hack for card that do the AES decrypt themsleves
	memset(&dummy, 0, sizeof(AES_KEY));
	if(!memcmp(&current->key, &dummy, sizeof(AES_KEY)))
	{
		return 1;
	}
	// decode the key
	for(i = 0; i < n; i += 16)
		{ AES_decrypt(buf + i, buf + i, &(current->key)); }
	return 1; // all ok, key decoded.
}
示例#28
0
static bool aes_block_decrypt(void * buffer, size_t len)
{
    AES_KEY key;
    char out[16];
    size_t bc = len / 16;
    char *cbuf = (char *)buffer;

    if(NULL == buffer || len % 16 != 0)
        return false;

    AES_set_decrypt_key((const unsigned char*)aes_ukey, 128, &key);

    for(size_t i = 0 ; i < bc ; i ++) {
        AES_decrypt((const unsigned char*)(cbuf + i * 16), (unsigned char*)out, &key);
        memcpy(cbuf + i * 16, out, 16);
    }
    return true;
}
示例#29
0
static int aesbs_cbc_decrypt(struct blkcipher_desc *desc,
			     struct scatterlist *dst,
			     struct scatterlist *src, unsigned int nbytes)
{
	struct aesbs_cbc_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
	struct blkcipher_walk walk;
	int err;

	blkcipher_walk_init(&walk, dst, src, nbytes);
	err = blkcipher_walk_virt_block(desc, &walk, 8 * AES_BLOCK_SIZE);

	while ((walk.nbytes / AES_BLOCK_SIZE) >= 8) {
		kernel_neon_begin();
		bsaes_cbc_encrypt(walk.src.virt.addr, walk.dst.virt.addr,
				  walk.nbytes, &ctx->dec, walk.iv);
		kernel_neon_end();
		err = blkcipher_walk_done(desc, &walk, 0);
	}
	while (walk.nbytes) {
		u32 blocks = walk.nbytes / AES_BLOCK_SIZE;
		u8 *dst = walk.dst.virt.addr;
		u8 *src = walk.src.virt.addr;
		u8 bk[2][AES_BLOCK_SIZE];
		u8 *iv = walk.iv;

		do {
			if (walk.dst.virt.addr == walk.src.virt.addr)
				memcpy(bk[blocks & 1], src, AES_BLOCK_SIZE);

			AES_decrypt(src, dst, &ctx->dec.rk);
			crypto_xor(dst, iv, AES_BLOCK_SIZE);

			if (walk.dst.virt.addr == walk.src.virt.addr)
				iv = bk[blocks & 1];
			else
				iv = src;

			dst += AES_BLOCK_SIZE;
			src += AES_BLOCK_SIZE;
		} while (--blocks);
		err = blkcipher_walk_done(desc, &walk, 0);
	}
	return err;
}
static int
aes_1(v_U32_t cryptHandle, tANI_U8 *keyBytes, tANI_U32 keyLen,
      tANI_U8 at[ANI_SSM_AES_KEY_WRAP_BLOCK_SIZE],
      tANI_U8 ri[ANI_SSM_AES_KEY_WRAP_BLOCK_SIZE],
      tANI_U8 b[AES_BLOCK_SIZE]) {

    int retVal;

//    AES_KEY aesKey;

    tANI_U8 in[AES_BLOCK_SIZE];
    tANI_U8 *out;

    VOS_ASSERT (AES_BLOCK_SIZE == ANI_SSM_AES_KEY_WRAP_BLOCK_SIZE*2);

    // Concatenate A and R[i]
    vos_mem_copy(in, at, ANI_SSM_AES_KEY_WRAP_BLOCK_SIZE);
    vos_mem_copy(in + ANI_SSM_AES_KEY_WRAP_BLOCK_SIZE, 
           ri, ANI_SSM_AES_KEY_WRAP_BLOCK_SIZE);
    out = b;

#if 0
    retVal = AES_set_decrypt_key(keyBytes, keyLen*8, &aesKey);
    if (retVal != 0) {
        ANI_SSM_LOG_E("AES_set_encrypt_key returned %d", retVal);
        assert(0 && "AES_set_encrypt_key failed!");
        return ANI_E_FAILED;
    }

    AES_decrypt(in, out, &aesKey);
#else
    retVal = vos_decrypt_AES(cryptHandle, /* Handle */
                             in, /* input */
                             out, /* output */
                             keyBytes); /* key */
    if (retVal != 0) {
        VOS_TRACE( VOS_MODULE_ID_BAP, VOS_TRACE_LEVEL_ERROR,
                   "vos_decrypt_AES returned %d", retVal);
    }
#endif
    return ANI_OK;
}