Path: blob/master/arch/mips/cavium-octeon/crypto/octeon-md5.c
26535 views
/*1* Cryptographic API.2*3* MD5 Message Digest Algorithm (RFC1321).4*5* Adapted for OCTEON by Aaro Koskinen <[email protected]>.6*7* Based on crypto/md5.c, which is:8*9* Derived from cryptoapi implementation, originally based on the10* public domain implementation written by Colin Plumb in 1993.11*12* Copyright (c) Cryptoapi developers.13* Copyright (c) 2002 James Morris <[email protected]>14*15* This program is free software; you can redistribute it and/or modify it16* under the terms of the GNU General Public License as published by the Free17* Software Foundation; either version 2 of the License, or (at your option)18* any later version.19*/2021#include <asm/octeon/crypto.h>22#include <asm/octeon/octeon.h>23#include <crypto/internal/hash.h>24#include <crypto/md5.h>25#include <linux/kernel.h>26#include <linux/module.h>27#include <linux/string.h>28#include <linux/unaligned.h>2930struct octeon_md5_state {31__le32 hash[MD5_HASH_WORDS];32u64 byte_count;33};3435/*36* We pass everything as 64-bit. OCTEON can handle misaligned data.37*/3839static void octeon_md5_store_hash(struct octeon_md5_state *ctx)40{41u64 *hash = (u64 *)ctx->hash;4243write_octeon_64bit_hash_dword(hash[0], 0);44write_octeon_64bit_hash_dword(hash[1], 1);45}4647static void octeon_md5_read_hash(struct octeon_md5_state *ctx)48{49u64 *hash = (u64 *)ctx->hash;5051hash[0] = read_octeon_64bit_hash_dword(0);52hash[1] = read_octeon_64bit_hash_dword(1);53}5455static void octeon_md5_transform(const void *_block)56{57const u64 *block = _block;5859write_octeon_64bit_block_dword(block[0], 0);60write_octeon_64bit_block_dword(block[1], 1);61write_octeon_64bit_block_dword(block[2], 2);62write_octeon_64bit_block_dword(block[3], 3);63write_octeon_64bit_block_dword(block[4], 4);64write_octeon_64bit_block_dword(block[5], 5);65write_octeon_64bit_block_dword(block[6], 6);66octeon_md5_start(block[7]);67}6869static int octeon_md5_init(struct shash_desc *desc)70{71struct octeon_md5_state *mctx = shash_desc_ctx(desc);7273mctx->hash[0] = cpu_to_le32(MD5_H0);74mctx->hash[1] = cpu_to_le32(MD5_H1);75mctx->hash[2] = cpu_to_le32(MD5_H2);76mctx->hash[3] = cpu_to_le32(MD5_H3);77mctx->byte_count = 0;7879return 0;80}8182static int octeon_md5_update(struct shash_desc *desc, const u8 *data,83unsigned int len)84{85struct octeon_md5_state *mctx = shash_desc_ctx(desc);86struct octeon_cop2_state state;87unsigned long flags;8889mctx->byte_count += len;90flags = octeon_crypto_enable(&state);91octeon_md5_store_hash(mctx);9293do {94octeon_md5_transform(data);95data += MD5_HMAC_BLOCK_SIZE;96len -= MD5_HMAC_BLOCK_SIZE;97} while (len >= MD5_HMAC_BLOCK_SIZE);9899octeon_md5_read_hash(mctx);100octeon_crypto_disable(&state, flags);101mctx->byte_count -= len;102return len;103}104105static int octeon_md5_finup(struct shash_desc *desc, const u8 *src,106unsigned int offset, u8 *out)107{108struct octeon_md5_state *mctx = shash_desc_ctx(desc);109int padding = 56 - (offset + 1);110struct octeon_cop2_state state;111u32 block[MD5_BLOCK_WORDS];112unsigned long flags;113char *p;114115p = memcpy(block, src, offset);116p += offset;117*p++ = 0x80;118119flags = octeon_crypto_enable(&state);120octeon_md5_store_hash(mctx);121122if (padding < 0) {123memset(p, 0x00, padding + sizeof(u64));124octeon_md5_transform(block);125p = (char *)block;126padding = 56;127}128129memset(p, 0, padding);130mctx->byte_count += offset;131block[14] = mctx->byte_count << 3;132block[15] = mctx->byte_count >> 29;133cpu_to_le32_array(block + 14, 2);134octeon_md5_transform(block);135136octeon_md5_read_hash(mctx);137octeon_crypto_disable(&state, flags);138139memzero_explicit(block, sizeof(block));140memcpy(out, mctx->hash, sizeof(mctx->hash));141142return 0;143}144145static int octeon_md5_export(struct shash_desc *desc, void *out)146{147struct octeon_md5_state *ctx = shash_desc_ctx(desc);148union {149u8 *u8;150u32 *u32;151u64 *u64;152} p = { .u8 = out };153int i;154155for (i = 0; i < MD5_HASH_WORDS; i++)156put_unaligned(le32_to_cpu(ctx->hash[i]), p.u32++);157put_unaligned(ctx->byte_count, p.u64);158return 0;159}160161static int octeon_md5_import(struct shash_desc *desc, const void *in)162{163struct octeon_md5_state *ctx = shash_desc_ctx(desc);164union {165const u8 *u8;166const u32 *u32;167const u64 *u64;168} p = { .u8 = in };169int i;170171for (i = 0; i < MD5_HASH_WORDS; i++)172ctx->hash[i] = cpu_to_le32(get_unaligned(p.u32++));173ctx->byte_count = get_unaligned(p.u64);174return 0;175}176177static struct shash_alg alg = {178.digestsize = MD5_DIGEST_SIZE,179.init = octeon_md5_init,180.update = octeon_md5_update,181.finup = octeon_md5_finup,182.export = octeon_md5_export,183.import = octeon_md5_import,184.statesize = MD5_STATE_SIZE,185.descsize = sizeof(struct octeon_md5_state),186.base = {187.cra_name = "md5",188.cra_driver_name= "octeon-md5",189.cra_priority = OCTEON_CR_OPCODE_PRIORITY,190.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,191.cra_blocksize = MD5_HMAC_BLOCK_SIZE,192.cra_module = THIS_MODULE,193}194};195196static int __init md5_mod_init(void)197{198if (!octeon_has_crypto())199return -ENOTSUPP;200return crypto_register_shash(&alg);201}202203static void __exit md5_mod_fini(void)204{205crypto_unregister_shash(&alg);206}207208module_init(md5_mod_init);209module_exit(md5_mod_fini);210211MODULE_LICENSE("GPL");212MODULE_DESCRIPTION("MD5 Message Digest Algorithm (OCTEON)");213MODULE_AUTHOR("Aaro Koskinen <[email protected]>");214215216