static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, unsigned int key_len) { struct AES_CTX *ctx = crypto_tfm_ctx(tfm); switch (key_len) { case AES_KEYSIZE_128: key_len = 128; break; case AES_KEYSIZE_192: key_len = 192; break; case AES_KEYSIZE_256: key_len = 256; break; default: tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; return -EINVAL; } if (private_AES_set_encrypt_key(in_key, key_len, &ctx->enc_key) == -1) { tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; return -EINVAL; } /* private_AES_set_decrypt_key expects an encryption key as input */ ctx->dec_key = ctx->enc_key; if (private_AES_set_decrypt_key(in_key, key_len, &ctx->dec_key) == -1) { tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; return -EINVAL; } return 0; }
int AES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key) { #ifdef OPENSSL_FIPS fips_cipher_abort(AES); #endif return private_AES_set_decrypt_key(userKey, bits, key); }
// ---------------------------------------------------------------------------- // AES-CBC-128 based on OpenSSL library. // ---------------------------------------------------------------------------- BYTE *aes( const BYTE *in, BYTE *out, int cb, const BYTE *key, const BYTE *iv, bool bEncrypt ) { AES_KEY aks; bEncrypt ? private_AES_set_encrypt_key( key, AesCbc128_BlkLen * 8, &aks ) : private_AES_set_decrypt_key( key, AesCbc128_BlkLen * 8, &aks ) ; AES_cbc_encrypt( in, out, cb, &aks, (BYTE *)iv, bEncrypt?1:0 ); return out; }
/** * AES解密 * 参数说明: * cipher:[in] 密文数据 * cipherLen: [in] 密文长度 * Key: [in] 加密密钥 * KeyLen: [in] 密钥长度 * plain: [out] 解密后的明文 * plainLen: [in] plain数组长度 * 返回值: * 非0: 解密后的明文长度 * 0: 错误 */ int AESDecrypt(word8 *cipher, int cipherLen, word8 *Key, int keyBits, word8 *plain, int plainLen) { int block; int blockNum; AES_KEY AesKey; if(plainLen < cipherLen) { return 0; } blockNum = cipherLen / 16; /*initKey(Key, KeyLen, tkey, keySched);*/ private_AES_set_decrypt_key(Key, keyBits, &AesKey); for(block = 0; block < blockNum; block++) { USIMM_AES_decrypt(&cipher[block * BLOCK_LEN], &plain[block * BLOCK_LEN], &AesKey); } /* //ECBģʽ���ֿ���ܣ�ÿ���С16�ֽ� for(block = 0; block < blockNum; block++) { // convert plain for(i=0;i<4;i++) { for(j=0;j<4;j++) { tplain[j][i] = cipher[block * BLOCK_LEN + 4*i+j]; } } // AES Encryption rijndaelDecrypt(tplain, KeyLen,BLOCK_LEN * 8, keySched); // convert result for(i=0;i<4;i++) { for(j=0;j<4;j++) { plain[block * BLOCK_LEN + 4*i+j] = tplain[j][i]; } } } */ return Unpadding_PKCS5(plain, cipherLen); }
static int aesbs_cbc_set_key(struct crypto_tfm *tfm, const u8 *in_key, unsigned int key_len) { struct aesbs_cbc_ctx *ctx = crypto_tfm_ctx(tfm); int bits = key_len * 8; if (private_AES_set_encrypt_key(in_key, bits, &ctx->enc)) { tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; return -EINVAL; } ctx->dec.rk = ctx->enc; private_AES_set_decrypt_key(in_key, bits, &ctx->dec.rk); ctx->dec.converted = 0; return 0; }
int AES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key) { return private_AES_set_decrypt_key(userKey, bits, key); }