Linux Audio

Check our new training course

Loading...
v4.17
 
  1/*
  2 * echainiv: Encrypted Chain IV Generator
  3 *
  4 * This generator generates an IV based on a sequence number by multiplying
  5 * it with a salt and then encrypting it with the same key as used to encrypt
  6 * the plain text.  This algorithm requires that the block size be equal
  7 * to the IV size.  It is mainly useful for CBC.
  8 *
  9 * This generator can only be used by algorithms where authentication
 10 * is performed after encryption (i.e., authenc).
 11 *
 12 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
 13 *
 14 * This program is free software; you can redistribute it and/or modify it
 15 * under the terms of the GNU General Public License as published by the Free
 16 * Software Foundation; either version 2 of the License, or (at your option)
 17 * any later version.
 18 *
 19 */
 20
 21#include <crypto/internal/geniv.h>
 22#include <crypto/scatterwalk.h>
 23#include <crypto/skcipher.h>
 24#include <linux/err.h>
 25#include <linux/init.h>
 26#include <linux/kernel.h>
 27#include <linux/module.h>
 28#include <linux/slab.h>
 29#include <linux/string.h>
 30
 31static int echainiv_encrypt(struct aead_request *req)
 32{
 33	struct crypto_aead *geniv = crypto_aead_reqtfm(req);
 34	struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
 35	struct aead_request *subreq = aead_request_ctx(req);
 36	__be64 nseqno;
 37	u64 seqno;
 38	u8 *info;
 39	unsigned int ivsize = crypto_aead_ivsize(geniv);
 40	int err;
 41
 42	if (req->cryptlen < ivsize)
 43		return -EINVAL;
 44
 45	aead_request_set_tfm(subreq, ctx->child);
 46
 47	info = req->iv;
 48
 49	if (req->src != req->dst) {
 50		SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
 51
 52		skcipher_request_set_tfm(nreq, ctx->sknull);
 53		skcipher_request_set_callback(nreq, req->base.flags,
 54					      NULL, NULL);
 55		skcipher_request_set_crypt(nreq, req->src, req->dst,
 56					   req->assoclen + req->cryptlen,
 57					   NULL);
 58
 59		err = crypto_skcipher_encrypt(nreq);
 60		if (err)
 61			return err;
 62	}
 63
 64	aead_request_set_callback(subreq, req->base.flags,
 65				  req->base.complete, req->base.data);
 66	aead_request_set_crypt(subreq, req->dst, req->dst,
 67			       req->cryptlen, info);
 68	aead_request_set_ad(subreq, req->assoclen);
 69
 70	memcpy(&nseqno, info + ivsize - 8, 8);
 71	seqno = be64_to_cpu(nseqno);
 72	memset(info, 0, ivsize);
 73
 74	scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
 75
 76	do {
 77		u64 a;
 78
 79		memcpy(&a, ctx->salt + ivsize - 8, 8);
 80
 81		a |= 1;
 82		a *= seqno;
 83
 84		memcpy(info + ivsize - 8, &a, 8);
 85	} while ((ivsize -= 8));
 86
 87	return crypto_aead_encrypt(subreq);
 88}
 89
 90static int echainiv_decrypt(struct aead_request *req)
 91{
 92	struct crypto_aead *geniv = crypto_aead_reqtfm(req);
 93	struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
 94	struct aead_request *subreq = aead_request_ctx(req);
 95	crypto_completion_t compl;
 96	void *data;
 97	unsigned int ivsize = crypto_aead_ivsize(geniv);
 98
 99	if (req->cryptlen < ivsize)
100		return -EINVAL;
101
102	aead_request_set_tfm(subreq, ctx->child);
103
104	compl = req->base.complete;
105	data = req->base.data;
106
107	aead_request_set_callback(subreq, req->base.flags, compl, data);
108	aead_request_set_crypt(subreq, req->src, req->dst,
109			       req->cryptlen - ivsize, req->iv);
110	aead_request_set_ad(subreq, req->assoclen + ivsize);
111
112	scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
113
114	return crypto_aead_decrypt(subreq);
115}
116
117static int echainiv_aead_create(struct crypto_template *tmpl,
118				struct rtattr **tb)
119{
120	struct aead_instance *inst;
121	int err;
122
123	inst = aead_geniv_alloc(tmpl, tb, 0, 0);
124
125	if (IS_ERR(inst))
126		return PTR_ERR(inst);
127
128	err = -EINVAL;
129	if (inst->alg.ivsize & (sizeof(u64) - 1) || !inst->alg.ivsize)
130		goto free_inst;
131
132	inst->alg.encrypt = echainiv_encrypt;
133	inst->alg.decrypt = echainiv_decrypt;
134
135	inst->alg.init = aead_init_geniv;
136	inst->alg.exit = aead_exit_geniv;
137
138	inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
139	inst->alg.base.cra_ctxsize += inst->alg.ivsize;
140
141	inst->free = aead_geniv_free;
142
143	err = aead_register_instance(tmpl, inst);
144	if (err)
145		goto free_inst;
146
147out:
148	return err;
149
150free_inst:
151	aead_geniv_free(inst);
152	goto out;
153}
154
155static void echainiv_free(struct crypto_instance *inst)
156{
157	aead_geniv_free(aead_instance(inst));
158}
159
160static struct crypto_template echainiv_tmpl = {
161	.name = "echainiv",
162	.create = echainiv_aead_create,
163	.free = echainiv_free,
164	.module = THIS_MODULE,
165};
166
167static int __init echainiv_module_init(void)
168{
169	return crypto_register_template(&echainiv_tmpl);
170}
171
172static void __exit echainiv_module_exit(void)
173{
174	crypto_unregister_template(&echainiv_tmpl);
175}
176
177module_init(echainiv_module_init);
178module_exit(echainiv_module_exit);
179
180MODULE_LICENSE("GPL");
181MODULE_DESCRIPTION("Encrypted Chain IV Generator");
182MODULE_ALIAS_CRYPTO("echainiv");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * echainiv: Encrypted Chain IV Generator
  4 *
  5 * This generator generates an IV based on a sequence number by multiplying
  6 * it with a salt and then encrypting it with the same key as used to encrypt
  7 * the plain text.  This algorithm requires that the block size be equal
  8 * to the IV size.  It is mainly useful for CBC.
  9 *
 10 * This generator can only be used by algorithms where authentication
 11 * is performed after encryption (i.e., authenc).
 12 *
 13 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
 
 
 
 
 
 
 14 */
 15
 16#include <crypto/internal/geniv.h>
 17#include <crypto/scatterwalk.h>
 18#include <crypto/skcipher.h>
 19#include <linux/err.h>
 20#include <linux/init.h>
 21#include <linux/kernel.h>
 22#include <linux/module.h>
 23#include <linux/slab.h>
 24#include <linux/string.h>
 25
 26static int echainiv_encrypt(struct aead_request *req)
 27{
 28	struct crypto_aead *geniv = crypto_aead_reqtfm(req);
 29	struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
 30	struct aead_request *subreq = aead_request_ctx(req);
 31	__be64 nseqno;
 32	u64 seqno;
 33	u8 *info;
 34	unsigned int ivsize = crypto_aead_ivsize(geniv);
 35	int err;
 36
 37	if (req->cryptlen < ivsize)
 38		return -EINVAL;
 39
 40	aead_request_set_tfm(subreq, ctx->child);
 41
 42	info = req->iv;
 43
 44	if (req->src != req->dst) {
 45		SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
 46
 47		skcipher_request_set_sync_tfm(nreq, ctx->sknull);
 48		skcipher_request_set_callback(nreq, req->base.flags,
 49					      NULL, NULL);
 50		skcipher_request_set_crypt(nreq, req->src, req->dst,
 51					   req->assoclen + req->cryptlen,
 52					   NULL);
 53
 54		err = crypto_skcipher_encrypt(nreq);
 55		if (err)
 56			return err;
 57	}
 58
 59	aead_request_set_callback(subreq, req->base.flags,
 60				  req->base.complete, req->base.data);
 61	aead_request_set_crypt(subreq, req->dst, req->dst,
 62			       req->cryptlen, info);
 63	aead_request_set_ad(subreq, req->assoclen);
 64
 65	memcpy(&nseqno, info + ivsize - 8, 8);
 66	seqno = be64_to_cpu(nseqno);
 67	memset(info, 0, ivsize);
 68
 69	scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
 70
 71	do {
 72		u64 a;
 73
 74		memcpy(&a, ctx->salt + ivsize - 8, 8);
 75
 76		a |= 1;
 77		a *= seqno;
 78
 79		memcpy(info + ivsize - 8, &a, 8);
 80	} while ((ivsize -= 8));
 81
 82	return crypto_aead_encrypt(subreq);
 83}
 84
 85static int echainiv_decrypt(struct aead_request *req)
 86{
 87	struct crypto_aead *geniv = crypto_aead_reqtfm(req);
 88	struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
 89	struct aead_request *subreq = aead_request_ctx(req);
 90	crypto_completion_t compl;
 91	void *data;
 92	unsigned int ivsize = crypto_aead_ivsize(geniv);
 93
 94	if (req->cryptlen < ivsize)
 95		return -EINVAL;
 96
 97	aead_request_set_tfm(subreq, ctx->child);
 98
 99	compl = req->base.complete;
100	data = req->base.data;
101
102	aead_request_set_callback(subreq, req->base.flags, compl, data);
103	aead_request_set_crypt(subreq, req->src, req->dst,
104			       req->cryptlen - ivsize, req->iv);
105	aead_request_set_ad(subreq, req->assoclen + ivsize);
106
107	scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
108
109	return crypto_aead_decrypt(subreq);
110}
111
112static int echainiv_aead_create(struct crypto_template *tmpl,
113				struct rtattr **tb)
114{
115	struct aead_instance *inst;
116	int err;
117
118	inst = aead_geniv_alloc(tmpl, tb);
119
120	if (IS_ERR(inst))
121		return PTR_ERR(inst);
122
123	err = -EINVAL;
124	if (inst->alg.ivsize & (sizeof(u64) - 1) || !inst->alg.ivsize)
125		goto free_inst;
126
127	inst->alg.encrypt = echainiv_encrypt;
128	inst->alg.decrypt = echainiv_decrypt;
129
130	inst->alg.init = aead_init_geniv;
131	inst->alg.exit = aead_exit_geniv;
132
133	inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
134	inst->alg.base.cra_ctxsize += inst->alg.ivsize;
135
 
 
136	err = aead_register_instance(tmpl, inst);
137	if (err) {
 
 
 
 
 
138free_inst:
139		inst->free(inst);
140	}
141	return err;
 
 
 
 
142}
143
144static struct crypto_template echainiv_tmpl = {
145	.name = "echainiv",
146	.create = echainiv_aead_create,
 
147	.module = THIS_MODULE,
148};
149
150static int __init echainiv_module_init(void)
151{
152	return crypto_register_template(&echainiv_tmpl);
153}
154
155static void __exit echainiv_module_exit(void)
156{
157	crypto_unregister_template(&echainiv_tmpl);
158}
159
160subsys_initcall(echainiv_module_init);
161module_exit(echainiv_module_exit);
162
163MODULE_LICENSE("GPL");
164MODULE_DESCRIPTION("Encrypted Chain IV Generator");
165MODULE_ALIAS_CRYPTO("echainiv");