Search code examples
clinuxcryptographydrivercryptoapi

Linux Kernel Crypto API : skcipher algorithm name not found by "crypto_alloc_skcipher"


I'm trying to make a Linux kernel driver using crypto API.

So first I have my own skcipher algorithm that I developed successfully registered on the crypto API and I can see it in the list of cryptos that is well registered.

.base = {

/* Name used by the framework to find who is implementing what. */
    .cra_name = "cbc(aes)stackOverFlow",

/* Driver name. Can be used to request a specific implementation of an algorithm. */
    .cra_driver_name = "stackOverFlow-cbc-aes",

/* Priority is used when implementation auto-selection takes place:
* if there are several implementers, the one with the highest priority is chosen.
* By convention: HW engine > ASM/arch-optimized > plain C 
* */
    .cra_priority = 300,

/* Driver module */
    .cra_module = THIS_MODULE,

/* Size of the data blocks this algo operates on. */
    .cra_blocksize = AES_BLOCK_SIZE,

    .cra_flags = CRYPTO_ALG_INTERNAL | CRYPTO_ALG_TYPE_SKCIPHER,

/* Size of the context attached to an algorithm instance. 
* This value informs the kernel crypto API about the memory size
* needed to be allocated for the transformation context.
*/
    .cra_ctxsize = sizeof(struct crypto_aes_ctx),

/* Alignment mask for the input and output data buffer. */
    .cra_alignmask = 15,
},

/* constructor/destructor methods called every time an alg instance is created/destroyed. */
    .min_keysize = AES_MIN_KEY_SIZE,
    .max_keysize = AES_MAX_KEY_SIZE,
    .ivsize     = AES_BLOCK_SIZE,   

    .init = test_skcipher_cra_init,
    .exit = test_skcipher_cra_exit,

    .setkey = test_aes_setkey,
    .encrypt = test_cbc_aes_encrypt,
    .decrypt = test_cbc_aes_decrypt,


};

And this is my init function module :

static int __init test_skcipher_cra_init(struct crypto_skcipher *tfm){

int ret;

ret = crypto_register_skcipher(&test_cbc_aes_alg);
if (ret < 0){
    printk(KERN_ALERT "register failed %d", ret);
}
    
else{
    printk(KERN_INFO "SUCCESS crypto_register\n");

}

return ret;

}

So to ensure that my driver works fine, I'm using the implementation user code (that I got from link) to encrypt some data : https://www.kernel.org/doc/html/v4.17/crypto/api-samples.html

But when I compile everything and move to see the kernel log messages I receive an error message "could not allocate skcipher handle" that comes from a part of the implementation code :

skcipher = crypto_alloc_skcipher("stackOverFlow-cbc-aes", 0, 0);
    if (IS_ERR(skcipher)) {
        pr_info("could not allocate skcipher handle\n");
        return PTR_ERR(skcipher);
    }

But in the crypto API, I can see the driver :

name         : cbc(aes)stackOverFlow
driver       : stackOverFlow-cbc-aes
module       : kernel
priority     : 300
refcnt       : 1
selftest     : passed
internal     : yes
type         : skcipher
async        : no
blocksize    : 16
min keysize  : 16
max keysize  : 32
ivsize       : 16
chunksize    : 16
walksize     : 16

I really tried many times to modify the flag and other things in my algorithm but I don't understund it keeps showing me this message. So my question is why it gives me this error and my crypto driver is already registred on the crypto API ?

Notice that when I change the name to crypto_alloc_skcipher("cbc-aes-aesni", 0, 0) which is one of the already exist ones in the API, everything works fine.


Solution

  • I managed to resolve the problem, it was stupid mistake because of the Init algorithm function that I confused with Init function module.