Chapter 6. Code Examples

Table of Contents

Code Example For Asynchronous Block Cipher Operation
Code Example For Synchronous Block Cipher Operation
Code Example For Use of Operational State Memory With SHASH
Code Example For Random Number Generator Usage

Code Example For Asynchronous Block Cipher Operation


struct tcrypt_result {
	struct completion completion;
	int err;
};

/* tie all data structures together */
struct ablkcipher_def {
	struct scatterlist sg;
	struct crypto_ablkcipher *tfm;
	struct ablkcipher_request *req;
	struct tcrypt_result result;
};

/* Callback function */
static void test_ablkcipher_cb(struct crypto_async_request *req, int error)
{
	struct tcrypt_result *result = req->data;

	if (error == -EINPROGRESS)
		return;
	result->err = error;
	complete(&result->completion);
	pr_info("Encryption finished successfully\n");
}

/* Perform cipher operation */
static unsigned int test_ablkcipher_encdec(struct ablkcipher_def *ablk,
					   int enc)
{
	int rc = 0;

	if (enc)
		rc = crypto_ablkcipher_encrypt(ablk->req);
	else
		rc = crypto_ablkcipher_decrypt(ablk->req);

	switch (rc) {
	case 0:
		break;
	case -EINPROGRESS:
	case -EBUSY:
		rc = wait_for_completion_interruptible(
			&ablk->result.completion);
		if (!rc && !ablk->result.err) {
			reinit_completion(&ablk->result.completion);
			break;
		}
	default:
		pr_info("ablkcipher encrypt returned with %d result %d\n",
		       rc, ablk->result.err);
		break;
	}
	init_completion(&ablk->result.completion);

	return rc;
}

/* Initialize and trigger cipher operation */
static int test_ablkcipher(void)
{
	struct ablkcipher_def ablk;
	struct crypto_ablkcipher *ablkcipher = NULL;
	struct ablkcipher_request *req = NULL;
	char *scratchpad = NULL;
	char *ivdata = NULL;
	unsigned char key[32];
	int ret = -EFAULT;

	ablkcipher = crypto_alloc_ablkcipher("cbc-aes-aesni", 0, 0);
	if (IS_ERR(ablkcipher)) {
		pr_info("could not allocate ablkcipher handle\n");
		return PTR_ERR(ablkcipher);
	}

	req = ablkcipher_request_alloc(ablkcipher, GFP_KERNEL);
	if (IS_ERR(req)) {
		pr_info("could not allocate request queue\n");
		ret = PTR_ERR(req);
		goto out;
	}

	ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
					test_ablkcipher_cb,
					&ablk.result);

	/* AES 256 with random key */
	get_random_bytes(&key, 32);
	if (crypto_ablkcipher_setkey(ablkcipher, key, 32)) {
		pr_info("key could not be set\n");
		ret = -EAGAIN;
		goto out;
	}

	/* IV will be random */
	ivdata = kmalloc(16, GFP_KERNEL);
	if (!ivdata) {
		pr_info("could not allocate ivdata\n");
		goto out;
	}
	get_random_bytes(ivdata, 16);

	/* Input data will be random */
	scratchpad = kmalloc(16, GFP_KERNEL);
	if (!scratchpad) {
		pr_info("could not allocate scratchpad\n");
		goto out;
	}
	get_random_bytes(scratchpad, 16);

	ablk.tfm = ablkcipher;
	ablk.req = req;

	/* We encrypt one block */
	sg_init_one(&ablk.sg, scratchpad, 16);
	ablkcipher_request_set_crypt(req, &ablk.sg, &ablk.sg, 16, ivdata);
	init_completion(&ablk.result.completion);

	/* encrypt data */
	ret = test_ablkcipher_encdec(&ablk, 1);
	if (ret)
		goto out;

	pr_info("Encryption triggered successfully\n");

out:
	if (ablkcipher)
		crypto_free_ablkcipher(ablkcipher);
	if (req)
		ablkcipher_request_free(req);
	if (ivdata)
		kfree(ivdata);
	if (scratchpad)
		kfree(scratchpad);
	return ret;
}