datara
datara

Reputation: 21

Linux Kernel Crypto API aead callback function doesn't work

I have a kernel module that does encryption/decryption using Linux Kernel Crypto API. This is my encryption function.

int aes_enc(char *plaintext, int len, char *_key, char *_iv, int type)
{
    struct crypto_aead  *tfm = NULL;
    struct aead_request *req = NULL;
    struct scatterlist   sg;
    char  *buffer;
    int buffer_size;
    int ret = 0;

    tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
    if(!tfm){
        printk("crypto_alloc_aead failed\n");
        return -1;
    }

    ret = crypto_aead_setauthsize(tfm, 16);
    if(ret){
        printk("crypto_aead_setauthsize failed\n");
        return -1;
    }

    ret = crypto_aead_setkey(tfm, _key, 32);
    if(ret){
        printk("crypto_aead_setkey failed\n");
        return -1;
    }

    req = aead_request_alloc(tfm, GFP_KERNEL);
    req->assoclen = 0;

    buffer_size = len + AUTH_TAG_SIZE;
    buffer = plaintext;

    sg_init_one(&sg, buffer, buffer_size);
    aead_request_set_callback(req, 0, aes_enc_done, NULL);

    aead_request_set_crypt(req, &sg, &sg, len, _iv);

    ret = crypto_aead_encrypt(req);
    if(ret){
        printk("crypto_aead_encrypt failed\n");
        return -1;
    }

    return ret;
}

And callback function.

void aes_enc_done(void *data, int err){
        pr_alert("aes_enc_done\n");
}

Encryption and decryption functions work fine. I have 2 questions

  1. Why is the callback function never called, it's not printing anything(it's not being optimized out because I use -fno-inline-small-functions flag)
  2. The encryption/decryption is done asynchronously until I use crypto_wait_req() functions, right?

Upvotes: 2

Views: 83

Answers (0)

Related Questions