Blame view
crypto/ofb.c
2.49 KB
e497c5189
|
1 2 3 4 5 6 7 |
// SPDX-License-Identifier: GPL-2.0 /* * OFB: Output FeedBack mode * * Copyright (C) 2018 ARM Limited or its affiliates. * All rights reserved. |
e497c5189
|
8 9 10 |
*/ #include <crypto/algapi.h> |
0eb76ba29
|
11 |
#include <crypto/internal/cipher.h> |
e497c5189
|
12 13 14 15 16 |
#include <crypto/internal/skcipher.h> #include <linux/err.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h> |
e497c5189
|
17 |
|
b3e3e2db7
|
18 |
static int crypto_ofb_crypt(struct skcipher_request *req) |
e497c5189
|
19 |
{ |
e497c5189
|
20 |
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
21f3ca6cd
|
21 |
struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
b3e3e2db7
|
22 23 24 |
const unsigned int bsize = crypto_cipher_blocksize(cipher); struct skcipher_walk walk; int err; |
e497c5189
|
25 |
|
b3e3e2db7
|
26 |
err = skcipher_walk_virt(&walk, req, false); |
e497c5189
|
27 |
|
b3e3e2db7
|
28 29 30 31 32 |
while (walk.nbytes >= bsize) { const u8 *src = walk.src.virt.addr; u8 *dst = walk.dst.virt.addr; u8 * const iv = walk.iv; unsigned int nbytes = walk.nbytes; |
e497c5189
|
33 |
|
b3e3e2db7
|
34 35 36 37 38 39 |
do { crypto_cipher_encrypt_one(cipher, iv, iv); crypto_xor_cpy(dst, src, iv, bsize); dst += bsize; src += bsize; } while ((nbytes -= bsize) >= bsize); |
e497c5189
|
40 |
|
b3e3e2db7
|
41 42 |
err = skcipher_walk_done(&walk, nbytes); } |
e497c5189
|
43 |
|
b3e3e2db7
|
44 45 46 47 48 49 50 |
if (walk.nbytes) { crypto_cipher_encrypt_one(cipher, walk.iv, walk.iv); crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, walk.iv, walk.nbytes); err = skcipher_walk_done(&walk, 0); } return err; |
e497c5189
|
51 |
} |
e497c5189
|
52 53 54 |
static int crypto_ofb_create(struct crypto_template *tmpl, struct rtattr **tb) { struct skcipher_instance *inst; |
e497c5189
|
55 |
struct crypto_alg *alg; |
e497c5189
|
56 |
int err; |
b3c16bfc6
|
57 |
inst = skcipher_alloc_instance_simple(tmpl, tb); |
21f3ca6cd
|
58 59 |
if (IS_ERR(inst)) return PTR_ERR(inst); |
e497c5189
|
60 |
|
b3c16bfc6
|
61 |
alg = skcipher_ialg_simple(inst); |
b3e3e2db7
|
62 63 64 65 66 67 68 69 |
/* OFB mode is a stream cipher. */ inst->alg.base.cra_blocksize = 1; /* * To simplify the implementation, configure the skcipher walk to only * give a partial block at the very end, never earlier. */ inst->alg.chunksize = alg->cra_blocksize; |
b3e3e2db7
|
70 71 |
inst->alg.encrypt = crypto_ofb_crypt; inst->alg.decrypt = crypto_ofb_crypt; |
e497c5189
|
72 |
|
e497c5189
|
73 74 |
err = skcipher_register_instance(tmpl, inst); if (err) |
21f3ca6cd
|
75 |
inst->free(inst); |
e497c5189
|
76 |
|
e497c5189
|
77 |
return err; |
e497c5189
|
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
} static struct crypto_template crypto_ofb_tmpl = { .name = "ofb", .create = crypto_ofb_create, .module = THIS_MODULE, }; static int __init crypto_ofb_module_init(void) { return crypto_register_template(&crypto_ofb_tmpl); } static void __exit crypto_ofb_module_exit(void) { crypto_unregister_template(&crypto_ofb_tmpl); } |
c4741b230
|
95 |
subsys_initcall(crypto_ofb_module_init); |
e497c5189
|
96 97 98 |
module_exit(crypto_ofb_module_exit); MODULE_LICENSE("GPL"); |
21f3ca6cd
|
99 |
MODULE_DESCRIPTION("OFB block cipher mode of operation"); |
e497c5189
|
100 |
MODULE_ALIAS_CRYPTO("ofb"); |
0eb76ba29
|
101 |
MODULE_IMPORT_NS(CRYPTO_INTERNAL); |