码迷,mamicode.com
首页 > Windows程序 > 详细

kernel crypto hmac sha256 API call code

时间:2015-05-05 16:29:36      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

static int hmac_sha256(char *plaintext, unsigned int plain_text_size, char *key, unsigned int key_size, uint8_t *result)
{
    struct scatterlist sg;
    struct crypto_hash *tfm;
    struct hash_desc desc;
    int ret;

    if (!result) {
        printk(KERN_ERR "param err\n"); 
        return -EINVAL; 
    } 

    tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
    if (IS_ERR(tfm)) { 
        printk(KERN_ERR "crypto_alloc_ahash failed: err %ld", PTR_ERR(tfm));
        return -EINVAL; 
     } 

     desc.tfm = tfm;
     desc.flags = 0; 
     sg_set_buf(&sg, plaintext, plain_text_size); 
     ret = crypto_hash_setkey(tfm, key, key_size); 
     if (ret) {
         printk(KERN_ERR "crypto_ahash_setkey failed: err %d", ret); 
         goto out; 
     } 

     ret = crypto_hash_digest(&desc, &sg, plain_text_size, result);
     if(ret) { 
         printk(KERN_ERR "digest() failed ret = %d\n", ret);
         goto out; 
     } 
     printk(KERN_DEBUG, "crypto hash digest size %d\n", crypto_hash_digestsize(tfm));

out: 
    crypto_free_hash(tfm); 
    return -EINVAL;
}

kernel crypto hmac sha256 API call code

标签:

原文地址:http://blog.csdn.net/xuyong7/article/details/45503843

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!