linux/arch/x86/crypto/morus1280_glue.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * The MORUS-1280 Authenticated-Encryption Algorithm
   4 *   Common x86 SIMD glue skeleton
   5 *
   6 * Copyright (c) 2016-2018 Ondrej Mosnacek <omosnacek@gmail.com>
   7 * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved.
   8 */
   9
  10#include <crypto/internal/aead.h>
  11#include <crypto/internal/skcipher.h>
  12#include <crypto/morus1280_glue.h>
  13#include <crypto/scatterwalk.h>
  14#include <linux/err.h>
  15#include <linux/init.h>
  16#include <linux/kernel.h>
  17#include <linux/module.h>
  18#include <linux/scatterlist.h>
  19#include <asm/fpu/api.h>
  20
  21struct morus1280_state {
  22        struct morus1280_block s[MORUS_STATE_BLOCKS];
  23};
  24
  25struct morus1280_ops {
  26        int (*skcipher_walk_init)(struct skcipher_walk *walk,
  27                                  struct aead_request *req, bool atomic);
  28
  29        void (*crypt_blocks)(void *state, const void *src, void *dst,
  30                             unsigned int length);
  31        void (*crypt_tail)(void *state, const void *src, void *dst,
  32                           unsigned int length);
  33};
  34
  35static void crypto_morus1280_glue_process_ad(
  36                struct morus1280_state *state,
  37                const struct morus1280_glue_ops *ops,
  38                struct scatterlist *sg_src, unsigned int assoclen)
  39{
  40        struct scatter_walk walk;
  41        struct morus1280_block buf;
  42        unsigned int pos = 0;
  43
  44        scatterwalk_start(&walk, sg_src);
  45        while (assoclen != 0) {
  46                unsigned int size = scatterwalk_clamp(&walk, assoclen);
  47                unsigned int left = size;
  48                void *mapped = scatterwalk_map(&walk);
  49                const u8 *src = (const u8 *)mapped;
  50
  51                if (pos + size >= MORUS1280_BLOCK_SIZE) {
  52                        if (pos > 0) {
  53                                unsigned int fill = MORUS1280_BLOCK_SIZE - pos;
  54                                memcpy(buf.bytes + pos, src, fill);
  55                                ops->ad(state, buf.bytes, MORUS1280_BLOCK_SIZE);
  56                                pos = 0;
  57                                left -= fill;
  58                                src += fill;
  59                        }
  60
  61                        ops->ad(state, src, left);
  62                        src += left & ~(MORUS1280_BLOCK_SIZE - 1);
  63                        left &= MORUS1280_BLOCK_SIZE - 1;
  64                }
  65
  66                memcpy(buf.bytes + pos, src, left);
  67
  68                pos += left;
  69                assoclen -= size;
  70                scatterwalk_unmap(mapped);
  71                scatterwalk_advance(&walk, size);
  72                scatterwalk_done(&walk, 0, assoclen);
  73        }
  74
  75        if (pos > 0) {
  76                memset(buf.bytes + pos, 0, MORUS1280_BLOCK_SIZE - pos);
  77                ops->ad(state, buf.bytes, MORUS1280_BLOCK_SIZE);
  78        }
  79}
  80
  81static void crypto_morus1280_glue_process_crypt(struct morus1280_state *state,
  82                                                struct morus1280_ops ops,
  83                                                struct skcipher_walk *walk)
  84{
  85        while (walk->nbytes >= MORUS1280_BLOCK_SIZE) {
  86                ops.crypt_blocks(state, walk->src.virt.addr,
  87                                 walk->dst.virt.addr,
  88                                 round_down(walk->nbytes,
  89                                            MORUS1280_BLOCK_SIZE));
  90                skcipher_walk_done(walk, walk->nbytes % MORUS1280_BLOCK_SIZE);
  91        }
  92
  93        if (walk->nbytes) {
  94                ops.crypt_tail(state, walk->src.virt.addr, walk->dst.virt.addr,
  95                               walk->nbytes);
  96                skcipher_walk_done(walk, 0);
  97        }
  98}
  99
 100int crypto_morus1280_glue_setkey(struct crypto_aead *aead, const u8 *key,
 101                                 unsigned int keylen)
 102{
 103        struct morus1280_ctx *ctx = crypto_aead_ctx(aead);
 104
 105        if (keylen == MORUS1280_BLOCK_SIZE) {
 106                memcpy(ctx->key.bytes, key, MORUS1280_BLOCK_SIZE);
 107        } else if (keylen == MORUS1280_BLOCK_SIZE / 2) {
 108                memcpy(ctx->key.bytes, key, keylen);
 109                memcpy(ctx->key.bytes + keylen, key, keylen);
 110        } else {
 111                crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
 112                return -EINVAL;
 113        }
 114
 115        return 0;
 116}
 117EXPORT_SYMBOL_GPL(crypto_morus1280_glue_setkey);
 118
 119int crypto_morus1280_glue_setauthsize(struct crypto_aead *tfm,
 120                                      unsigned int authsize)
 121{
 122        return (authsize <= MORUS_MAX_AUTH_SIZE) ? 0 : -EINVAL;
 123}
 124EXPORT_SYMBOL_GPL(crypto_morus1280_glue_setauthsize);
 125
 126static void crypto_morus1280_glue_crypt(struct aead_request *req,
 127                                        struct morus1280_ops ops,
 128                                        unsigned int cryptlen,
 129                                        struct morus1280_block *tag_xor)
 130{
 131        struct crypto_aead *tfm = crypto_aead_reqtfm(req);
 132        struct morus1280_ctx *ctx = crypto_aead_ctx(tfm);
 133        struct morus1280_state state;
 134        struct skcipher_walk walk;
 135
 136        ops.skcipher_walk_init(&walk, req, true);
 137
 138        kernel_fpu_begin();
 139
 140        ctx->ops->init(&state, &ctx->key, req->iv);
 141        crypto_morus1280_glue_process_ad(&state, ctx->ops, req->src, req->assoclen);
 142        crypto_morus1280_glue_process_crypt(&state, ops, &walk);
 143        ctx->ops->final(&state, tag_xor, req->assoclen, cryptlen);
 144
 145        kernel_fpu_end();
 146}
 147
 148int crypto_morus1280_glue_encrypt(struct aead_request *req)
 149{
 150        struct crypto_aead *tfm = crypto_aead_reqtfm(req);
 151        struct morus1280_ctx *ctx = crypto_aead_ctx(tfm);
 152        struct morus1280_ops OPS = {
 153                .skcipher_walk_init = skcipher_walk_aead_encrypt,
 154                .crypt_blocks = ctx->ops->enc,
 155                .crypt_tail = ctx->ops->enc_tail,
 156        };
 157
 158        struct morus1280_block tag = {};
 159        unsigned int authsize = crypto_aead_authsize(tfm);
 160        unsigned int cryptlen = req->cryptlen;
 161
 162        crypto_morus1280_glue_crypt(req, OPS, cryptlen, &tag);
 163
 164        scatterwalk_map_and_copy(tag.bytes, req->dst,
 165                                 req->assoclen + cryptlen, authsize, 1);
 166        return 0;
 167}
 168EXPORT_SYMBOL_GPL(crypto_morus1280_glue_encrypt);
 169
 170int crypto_morus1280_glue_decrypt(struct aead_request *req)
 171{
 172        static const u8 zeros[MORUS1280_BLOCK_SIZE] = {};
 173
 174        struct crypto_aead *tfm = crypto_aead_reqtfm(req);
 175        struct morus1280_ctx *ctx = crypto_aead_ctx(tfm);
 176        struct morus1280_ops OPS = {
 177                .skcipher_walk_init = skcipher_walk_aead_decrypt,
 178                .crypt_blocks = ctx->ops->dec,
 179                .crypt_tail = ctx->ops->dec_tail,
 180        };
 181
 182        struct morus1280_block tag;
 183        unsigned int authsize = crypto_aead_authsize(tfm);
 184        unsigned int cryptlen = req->cryptlen - authsize;
 185
 186        scatterwalk_map_and_copy(tag.bytes, req->src,
 187                                 req->assoclen + cryptlen, authsize, 0);
 188
 189        crypto_morus1280_glue_crypt(req, OPS, cryptlen, &tag);
 190
 191        return crypto_memneq(tag.bytes, zeros, authsize) ? -EBADMSG : 0;
 192}
 193EXPORT_SYMBOL_GPL(crypto_morus1280_glue_decrypt);
 194
 195void crypto_morus1280_glue_init_ops(struct crypto_aead *aead,
 196                                    const struct morus1280_glue_ops *ops)
 197{
 198        struct morus1280_ctx *ctx = crypto_aead_ctx(aead);
 199        ctx->ops = ops;
 200}
 201EXPORT_SYMBOL_GPL(crypto_morus1280_glue_init_ops);
 202
 203MODULE_LICENSE("GPL");
 204MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
 205MODULE_DESCRIPTION("MORUS-1280 AEAD mode -- glue for x86 optimizations");
 206