^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Cryptographic API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * MD5 Message Digest Algorithm (RFC1321).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Adapted for OCTEON by Aaro Koskinen <aaro.koskinen@iki.fi>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Based on crypto/md5.c, which is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Derived from cryptoapi implementation, originally based on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * public domain implementation written by Colin Plumb in 1993.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Copyright (c) Cryptoapi developers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * This program is free software; you can redistribute it and/or modify it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * under the terms of the GNU General Public License as published by the Free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Software Foundation; either version 2 of the License, or (at your option)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <crypto/md5.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <asm/octeon/octeon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include "octeon-crypto.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * We pass everything as 64-bit. OCTEON can handle misaligned data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static void octeon_md5_store_hash(struct md5_state *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) u64 *hash = (u64 *)ctx->hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) write_octeon_64bit_hash_dword(hash[0], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) write_octeon_64bit_hash_dword(hash[1], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static void octeon_md5_read_hash(struct md5_state *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) u64 *hash = (u64 *)ctx->hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) hash[0] = read_octeon_64bit_hash_dword(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) hash[1] = read_octeon_64bit_hash_dword(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static void octeon_md5_transform(const void *_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) const u64 *block = _block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) write_octeon_64bit_block_dword(block[0], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) write_octeon_64bit_block_dword(block[1], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) write_octeon_64bit_block_dword(block[2], 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) write_octeon_64bit_block_dword(block[3], 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) write_octeon_64bit_block_dword(block[4], 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) write_octeon_64bit_block_dword(block[5], 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) write_octeon_64bit_block_dword(block[6], 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) octeon_md5_start(block[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int octeon_md5_init(struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct md5_state *mctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) mctx->hash[0] = cpu_to_le32(MD5_H0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) mctx->hash[1] = cpu_to_le32(MD5_H1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) mctx->hash[2] = cpu_to_le32(MD5_H2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) mctx->hash[3] = cpu_to_le32(MD5_H3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) mctx->byte_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int octeon_md5_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct md5_state *mctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct octeon_cop2_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) mctx->byte_count += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (avail > len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) avail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) flags = octeon_crypto_enable(&state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) octeon_md5_store_hash(mctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) octeon_md5_transform(mctx->block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) data += avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) len -= avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) while (len >= sizeof(mctx->block)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) octeon_md5_transform(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) data += sizeof(mctx->block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) len -= sizeof(mctx->block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) octeon_md5_read_hash(mctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) octeon_crypto_disable(&state, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) memcpy(mctx->block, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static int octeon_md5_final(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct md5_state *mctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) const unsigned int offset = mctx->byte_count & 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) char *p = (char *)mctx->block + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int padding = 56 - (offset + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct octeon_cop2_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) *p++ = 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) flags = octeon_crypto_enable(&state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) octeon_md5_store_hash(mctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (padding < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) memset(p, 0x00, padding + sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) octeon_md5_transform(mctx->block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) p = (char *)mctx->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) padding = 56;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) memset(p, 0, padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) mctx->block[14] = cpu_to_le32(mctx->byte_count << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) mctx->block[15] = cpu_to_le32(mctx->byte_count >> 29);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) octeon_md5_transform(mctx->block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) octeon_md5_read_hash(mctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) octeon_crypto_disable(&state, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) memcpy(out, mctx->hash, sizeof(mctx->hash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) memset(mctx, 0, sizeof(*mctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int octeon_md5_export(struct shash_desc *desc, void *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct md5_state *ctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) memcpy(out, ctx, sizeof(*ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static int octeon_md5_import(struct shash_desc *desc, const void *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct md5_state *ctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) memcpy(ctx, in, sizeof(*ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static struct shash_alg alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) .digestsize = MD5_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .init = octeon_md5_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .update = octeon_md5_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .final = octeon_md5_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .export = octeon_md5_export,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .import = octeon_md5_import,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .descsize = sizeof(struct md5_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .statesize = sizeof(struct md5_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .cra_name = "md5",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .cra_driver_name= "octeon-md5",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) .cra_priority = OCTEON_CR_OPCODE_PRIORITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int __init md5_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!octeon_has_crypto())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return -ENOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return crypto_register_shash(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static void __exit md5_mod_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) crypto_unregister_shash(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) module_init(md5_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) module_exit(md5_mod_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) MODULE_DESCRIPTION("MD5 Message Digest Algorithm (OCTEON)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");