本文整理汇总了C++中crypto_blkcipher_encrypt函数的典型用法代码示例。如果您正苦于以下问题:C++ crypto_blkcipher_encrypt函数的具体用法?C++ crypto_blkcipher_encrypt怎么用?C++ crypto_blkcipher_encrypt使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了crypto_blkcipher_encrypt函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: test_cipher_cycles
static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
struct scatterlist *sg, int blen)
{
unsigned long cycles = 0;
int ret = 0;
int i;
local_bh_disable();
local_irq_disable();
/* Warm-up run. */
for (i = 0; i < 4; i++) {
if (enc)
ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
else
ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
if (ret)
goto out;
}
/* The real thing. */
for (i = 0; i < 8; i++) {
cycles_t start, end;
start = get_cycles();
if (enc)
ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
else
ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
end = get_cycles();
if (ret)
goto out;
cycles += end - start;
}
out:
local_irq_enable();
local_bh_enable();
if (ret == 0)
printk("1 operation in %lu cycles (%d bytes)\n",
(cycles + 4) / 8, blen);
return ret;
}
示例2: AES_cbc
static void AES_cbc(const __u8 *iv, int ivLength,
const __u8 *key, int keyLength,
const __u8 *input, int inputLength,
__u8 *output, int encrypt)
{
struct scatterlist src[1];
struct scatterlist dst[1];
struct blkcipher_desc desc;
struct crypto_blkcipher *cipher = crypto_alloc_blkcipher("cbc(aes)", 0, 0);
crypto_blkcipher_setkey(cipher, key, keyLength);
sg_init_table(dst, 1);
sg_init_table(src, 1);
sg_set_buf(&dst[0], output, inputLength);
sg_set_buf(&src[0], input, inputLength);
desc.tfm = cipher;
desc.flags = 0;
crypto_blkcipher_set_iv(cipher, iv, ivLength);
if (encrypt)
crypto_blkcipher_encrypt(&desc, dst, src, inputLength);
else
crypto_blkcipher_decrypt(&desc, dst, src, inputLength);
crypto_free_blkcipher(cipher);
}
示例3: smp_e
static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
{
struct blkcipher_desc desc;
struct scatterlist sg;
int err;
if (tfm == NULL) {
BT_ERR("tfm %p", tfm);
return -EINVAL;
}
desc.tfm = tfm;
desc.flags = 0;
err = crypto_blkcipher_setkey(tfm, k, 16);
if (err) {
BT_ERR("cipher setkey failed: %d", err);
return err;
}
sg_init_one(&sg, r, 16);
err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
if (err)
BT_ERR("Encrypt data error %d", err);
return err;
}
示例4: p8_aes_ctr_crypt
static int p8_aes_ctr_crypt(struct blkcipher_desc *desc,
struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes)
{
int ret;
struct blkcipher_walk walk;
struct p8_aes_ctr_ctx *ctx = crypto_tfm_ctx(
crypto_blkcipher_tfm(desc->tfm));
struct blkcipher_desc fallback_desc = {
.tfm = ctx->fallback,
.info = desc->info,
.flags = desc->flags
};
if (in_interrupt()) {
ret = crypto_blkcipher_encrypt(&fallback_desc, dst, src, nbytes);
} else {
blkcipher_walk_init(&walk, dst, src, nbytes);
ret = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
pagefault_disable();
enable_kernel_altivec();
aes_p8_ctr32_encrypt_blocks(walk.src.virt.addr, walk.dst.virt.addr,
(nbytes & AES_BLOCK_MASK)/AES_BLOCK_SIZE, &ctx->enc_key, walk.iv);
pagefault_enable();
crypto_inc(walk.iv, AES_BLOCK_SIZE);
nbytes &= AES_BLOCK_SIZE - 1;
ret = blkcipher_walk_done(desc, &walk, nbytes);
}
if (walk.nbytes) {
p8_aes_ctr_final(ctx, &walk);
ret = blkcipher_walk_done(desc, &walk, 0);
}
}
return ret;
}
struct crypto_alg p8_aes_ctr_alg = {
.cra_name = "ctr(aes)",
.cra_driver_name = "p8_aes_ctr",
.cra_module = THIS_MODULE,
.cra_priority = 1000,
.cra_type = &crypto_blkcipher_type,
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | CRYPTO_ALG_NEED_FALLBACK,
.cra_alignmask = 0,
.cra_blocksize = 1,
.cra_ctxsize = sizeof(struct p8_aes_ctr_ctx),
.cra_init = p8_aes_ctr_init,
.cra_exit = p8_aes_ctr_exit,
.cra_blkcipher = {
.ivsize = 0,
.min_keysize = AES_MIN_KEY_SIZE,
.max_keysize = AES_MAX_KEY_SIZE,
.setkey = p8_aes_ctr_setkey,
.encrypt = p8_aes_ctr_crypt,
.decrypt = p8_aes_ctr_crypt,
},
};
示例5: aes_encrypt
int aes_encrypt(char *buf, unsigned int keylen, void *read_buf, size_t src_len)
{
struct scatterlist sg;
struct blkcipher_desc desc;
int ret;
struct crypto_blkcipher *tfm = crypto_alloc_blkcipher("cbc(aes)", 0, 0);
if (IS_ERR(tfm))
{return PTR_ERR(tfm);}
desc.tfm = tfm;
desc.flags=0;
ret = crypto_blkcipher_setkey((void *)tfm, buf, keylen);
if(ret)
{
printk(KERN_ALERT "\n setkey failed\n");
goto free_tfm;
}
printk(KERN_ALERT "\n setkey passed\n");
sg_set_buf(&sg, read_buf, src_len);
ret = crypto_blkcipher_encrypt(&desc, &sg, &sg, src_len);
if (ret)
{
goto free_tfm;
}
free_tfm:
crypto_free_blkcipher(tfm);
return ret;
}
示例6: smp_e
static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
{
struct blkcipher_desc desc;
struct scatterlist sg;
int err, iv_len;
unsigned char iv[128];
if (tfm == NULL) {
BT_ERR("tfm %p", tfm);
return -EINVAL;
}
desc.tfm = tfm;
desc.flags = 0;
err = crypto_blkcipher_setkey(tfm, k, 16);
if (err) {
BT_ERR("cipher setkey failed: %d", err);
return err;
}
sg_init_one(&sg, r, 16);
iv_len = crypto_blkcipher_ivsize(tfm);
if (iv_len) {
memset(&iv, 0xff, iv_len);
crypto_blkcipher_set_iv(tfm, iv, iv_len);
}
err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
if (err)
BT_ERR("Encrypt data error %d", err);
return err;
}
示例7: my_encrypt
int my_encrypt(char *input, int inputlen, char *output, int outputlen,
char *key, int keylen)
{
struct crypto_blkcipher *tfm = NULL;
struct blkcipher_desc desc;
struct scatterlist src[1], dst[1];
unsigned int retval = 0;
tfm = crypto_alloc_blkcipher("ctr(aes)", 0, 0);
if (IS_ERR(tfm)) {
printk(KERN_INFO "crypto_alloc_blkcipher failed\n");
return -EINVAL;
}
desc.tfm = tfm;
desc.flags = 0;
retval = crypto_blkcipher_setkey(tfm, key, keylen);
if (retval) {
printk(KERN_INFO "crypto_blkcipher_setkey failed\n");
crypto_free_blkcipher(tfm);
return -EINVAL;
}
sg_init_table(src, 1);
sg_set_buf(&src[0], input, inputlen);
sg_init_table(dst, 1);
sg_set_buf(dst, output, outputlen);
retval = crypto_blkcipher_encrypt(&desc, dst, src, inputlen);
crypto_free_blkcipher(tfm);
return retval;
}
示例8: prism2_wep_encrypt
/* Perform WEP encryption on given skb that has at least 4 bytes of headroom
* for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted,
* so the payload length increases with 8 bytes.
*
* WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
*/
static int prism2_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
{
struct prism2_wep_data *wep = priv;
u32 klen, len;
u8 key[WEP_KEY_LEN + 3];
u8 *pos;
struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
MAX_DEV_ADDR_SIZE);
struct blkcipher_desc desc = {.tfm = wep->tx_tfm};
u32 crc;
u8 *icv;
struct scatterlist sg;
if (skb_headroom(skb) < 4 || skb_tailroom(skb) < 4 ||
skb->len < hdr_len) {
printk(KERN_ERR "Error!!! headroom=%d tailroom=%d skblen=%d"
" hdr_len=%d\n", skb_headroom(skb), skb_tailroom(skb),
skb->len, hdr_len);
return -1;
}
len = skb->len - hdr_len;
pos = skb_push(skb, 4);
memmove(pos, pos + 4, hdr_len);
pos += hdr_len;
klen = 3 + wep->key_len;
wep->iv++;
/* Fluhrer, Mantin, and Shamir have reported weaknesses in the key
* scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N)
* can be used to speedup attacks, so avoid using them. */
if ((wep->iv & 0xff00) == 0xff00) {
u8 B = (wep->iv >> 16) & 0xff;
if (B >= 3 && B < klen)
wep->iv += 0x0100;
}
/* Prepend 24-bit IV to RC4 key and TX frame */
*pos++ = key[0] = (wep->iv >> 16) & 0xff;
*pos++ = key[1] = (wep->iv >> 8) & 0xff;
*pos++ = key[2] = wep->iv & 0xff;
*pos++ = wep->key_idx << 6;
/* Copy rest of the WEP key (the secret part) */
memcpy(key + 3, wep->key, wep->key_len);
if (!tcb_desc->bHwSec) {
/* Append little-endian CRC32 and encrypt it to produce ICV */
crc = ~crc32_le(~0, pos, len);
icv = skb_put(skb, 4);
icv[0] = crc;
icv[1] = crc >> 8;
icv[2] = crc >> 16;
icv[3] = crc >> 24;
sg_init_one(&sg, pos, len+4);
crypto_blkcipher_setkey(wep->tx_tfm, key, klen);
return crypto_blkcipher_encrypt(&desc, &sg, &sg, len + 4);
}
示例9: encrypt_Cipher
static int encrypt_Cipher(char *key, char *src, char *dest, unsigned int len, int *written) {
struct crypto_blkcipher *blkcipher = NULL;
char *cipher = "cbc(aes)";
struct scatterlist sg_in[2];
struct scatterlist sg_out[1];
struct blkcipher_desc desc;
unsigned int encrypted_datalen;
unsigned int padlen;
char pad[16];
char *iv=NULL;
int ret = -EFAULT;
encrypted_datalen = nearestRoundup(len);
padlen = encrypted_datalen - len;
blkcipher = crypto_alloc_blkcipher(cipher, 0, 0);
if (IS_ERR(blkcipher)) {
printk("could not allocate blkcipher handle for %s\n", cipher);
return -PTR_ERR(blkcipher);
}
if (crypto_blkcipher_setkey(blkcipher, key, strlen(key))) {
printk("key could not be set\n");
ret = -EAGAIN;
goto out;
}
desc.flags = 0;
desc.tfm = blkcipher;
iv = (char *)kmalloc(crypto_blkcipher_ivsize(blkcipher) , GFP_KERNEL);
if(iv==NULL) {
printk("Initialisation vector not initialised\n");
ret = -ENOMEM;
goto out;
}
memset(iv, 0, crypto_blkcipher_ivsize(blkcipher));
memset(pad, 0, sizeof pad);
sg_init_table(sg_in, 2);
sg_set_buf(&sg_in[0], src, len);
sg_set_buf(&sg_in[1], pad, padlen);
sg_init_table(sg_out, 1);
sg_set_buf(sg_out, dest, encrypted_datalen);
crypto_blkcipher_set_iv(blkcipher, iv, crypto_blkcipher_ivsize(blkcipher));
ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in, encrypted_datalen);
(*written) = encrypted_datalen;
printk("Cipher Encryption operation completed\n");
kfree(iv);
crypto_free_blkcipher(blkcipher);
return ret;
out:
if (blkcipher)
crypto_free_blkcipher(blkcipher);
if (iv)
kfree(iv);
return ret;
}
示例10: encrypt_decrypt_file
static int encrypt_decrypt_file(char *buf, unsigned char *key, int len, int flag)
{
struct crypto_blkcipher *blkcipher = NULL;
char *cipher = "ctr(aes)";
struct scatterlist sg;
struct blkcipher_desc desc;
int rc;
blkcipher = crypto_alloc_blkcipher(cipher, 0, 0);
if (IS_ERR(blkcipher)) {
printk("could not allocate blkcipher handle for %s\n", cipher);
rc= -PTR_ERR(blkcipher);
goto out;
}
if (crypto_blkcipher_setkey(blkcipher, key, 16)) {
printk("key could not be set\n");
rc = -EAGAIN;
goto out;
}
desc.flags = 0;
desc.tfm = blkcipher;
sg_init_one(&sg, buf, len);
/* encrypt data */
if(flag == 1)
{
rc = crypto_blkcipher_encrypt(&desc, &sg, &sg, len);
if(rc){
printk("Encryption failed \n");
rc = -EFAULT;
goto out;
}
}
/* decrypt data */
else if(flag == 0)
{
rc = crypto_blkcipher_decrypt(&desc, &sg, &sg, len);
if(rc){
printk("Decryption failed \n");
rc = -EFAULT;
goto out;
}
}
return 0;
out:
if (blkcipher)
crypto_free_blkcipher(blkcipher);
return rc;
}
示例11: prism2_wep_encrypt
static int prism2_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
{
struct prism2_wep_data *wep = priv;
struct blkcipher_desc desc = { .tfm = wep->tx_tfm };
u32 klen, len;
u8 key[WEP_KEY_LEN + 3];
u8 *pos;
u32 crc;
u8 *icv;
struct scatterlist sg;
if (skb_headroom(skb) < 4 || skb_tailroom(skb) < 4 ||
skb->len < hdr_len)
return -1;
len = skb->len - hdr_len;
pos = skb_push(skb, 4);
memmove(pos, pos + 4, hdr_len);
pos += hdr_len;
klen = 3 + wep->key_len;
wep->iv++;
/*
*/
if ((wep->iv & 0xff00) == 0xff00) {
u8 B = (wep->iv >> 16) & 0xff;
if (B >= 3 && B < klen)
wep->iv += 0x0100;
}
/* */
*pos++ = key[0] = (wep->iv >> 16) & 0xff;
*pos++ = key[1] = (wep->iv >> 8) & 0xff;
*pos++ = key[2] = wep->iv & 0xff;
*pos++ = wep->key_idx << 6;
/* */
memcpy(key + 3, wep->key, wep->key_len);
/* */
crc = ~crc32_le(~0, pos, len);
icv = skb_put(skb, 4);
icv[0] = crc;
icv[1] = crc >> 8;
icv[2] = crc >> 16;
icv[3] = crc >> 24;
crypto_blkcipher_setkey(wep->tx_tfm, key, klen);
sg_init_one(&sg, pos, len + 4);
return crypto_blkcipher_encrypt(&desc, &sg, &sg, len + 4);
}
示例12: crypto_rfc4543_copy_src_to_dst
static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
{
struct crypto_aead *aead = crypto_aead_reqtfm(req);
struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
unsigned int authsize = crypto_aead_authsize(aead);
unsigned int nbytes = req->cryptlen - (enc ? 0 : authsize);
struct blkcipher_desc desc = {
.tfm = ctx->null,
};
return crypto_blkcipher_encrypt(&desc, req->dst, req->src, nbytes);
}
static int crypto_rfc4543_encrypt(struct aead_request *req)
{
struct crypto_aead *aead = crypto_aead_reqtfm(req);
struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req);
struct aead_request *subreq;
int err;
if (req->src != req->dst) {
err = crypto_rfc4543_copy_src_to_dst(req, true);
if (err)
return err;
}
subreq = crypto_rfc4543_crypt(req, true);
err = crypto_aead_encrypt(subreq);
if (err)
return err;
scatterwalk_map_and_copy(rctx->auth_tag, req->dst, req->cryptlen,
crypto_aead_authsize(aead), 1);
return 0;
}
static int crypto_rfc4543_decrypt(struct aead_request *req)
{
int err;
if (req->src != req->dst) {
err = crypto_rfc4543_copy_src_to_dst(req, false);
if (err)
return err;
}
req = crypto_rfc4543_crypt(req, false);
return crypto_aead_decrypt(req);
}
示例13: test2
static inline void test2(void)
{
struct crypto_blkcipher *tfm;
int rc;
unsigned char *crap;
struct blkcipher_desc desc;
struct scatterlist in, out;
printk(KERN_INFO "test in\n");
crap = kmalloc(4096, GFP_KERNEL);
tfm = crypto_alloc_blkcipher("rsa", 0, CRYPTO_ALG_ASYNC);
if (IS_ERR(tfm)) {
printk(KERN_INFO "crypto_alloc_blkcipher()\n");
return;
}
rc = crypto_blkcipher_setkey(tfm, key, sizeof(key) - 1);
printk(KERN_INFO "crypto_blkcipher_setkey = %d\n", rc);
strcpy(crap, "AABC");
desc.tfm = tfm;
desc.flags = 0;
sg_init_table(&in, 1);
sg_set_buf(&in, crap, 4);
sg_init_table(&out, 1);
sg_set_buf(&out, crap, 4096);
rc = crypto_blkcipher_encrypt(&desc, &out, &in, 4);
printk(KERN_INFO "crypto_blkcipher_encrypt RC %d %x %x %x\n",
rc, crap[0], crap[1], crap[2]);
sg_init_one(&in, crap, rc);
sg_init_one(&out, crap, 4096);
rc = crypto_blkcipher_decrypt(&desc, &out, &in, rc);
printk(KERN_INFO "crypto_blkcipher_decrypt RC %d %x %x %x\n",
rc, crap[0], crap[1], crap[2]);
crypto_free_blkcipher(tfm);
kfree(crap);
printk(KERN_INFO "test out\n");
}
示例14: derived_key_encrypt
/* Before returning data to userspace, encrypt decrypted data. */
static int derived_key_encrypt(struct encrypted_key_payload *epayload,
const u8 *derived_key,
unsigned int derived_keylen)
{
struct scatterlist sg_in[2];
struct scatterlist sg_out[1];
struct blkcipher_desc desc;
unsigned int encrypted_datalen;
unsigned int padlen;
char pad[16];
int ret;
encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
padlen = encrypted_datalen - epayload->decrypted_datalen;
ret = init_blkcipher_desc(&desc, derived_key, derived_keylen,
epayload->iv, ivsize);
if (ret < 0)
goto out;
dump_decrypted_data(epayload);
memset(pad, 0, sizeof pad);
sg_init_table(sg_in, 2);
sg_set_buf(&sg_in[0], epayload->decrypted_data,
epayload->decrypted_datalen);
sg_set_buf(&sg_in[1], pad, padlen);
sg_init_table(sg_out, 1);
sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in, encrypted_datalen);
crypto_free_blkcipher(desc.tfm);
if (ret < 0)
pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
else
dump_encrypted_data(epayload, encrypted_datalen);
out:
return ret;
}
示例15: test_cipher_jiffies
static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
struct scatterlist *sg, int blen, int sec)
{
unsigned long start, end;
int bcount;
int ret;
for (start = jiffies, end = start + sec * HZ, bcount = 0;
time_before(jiffies, end); bcount++) {
if (enc)
ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
else
ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
if (ret)
return ret;
}
printk("%d operations in %d seconds (%ld bytes)\n",
bcount, sec, (long)bcount * blen);
return 0;
}