11953c22fSAaro Koskinen /* 21953c22fSAaro Koskinen * Cryptographic API. 31953c22fSAaro Koskinen * 41953c22fSAaro Koskinen * MD5 Message Digest Algorithm (RFC1321). 51953c22fSAaro Koskinen * 61953c22fSAaro Koskinen * Adapted for OCTEON by Aaro Koskinen <aaro.koskinen@iki.fi>. 71953c22fSAaro Koskinen * 81953c22fSAaro Koskinen * Based on crypto/md5.c, which is: 91953c22fSAaro Koskinen * 101953c22fSAaro Koskinen * Derived from cryptoapi implementation, originally based on the 111953c22fSAaro Koskinen * public domain implementation written by Colin Plumb in 1993. 121953c22fSAaro Koskinen * 131953c22fSAaro Koskinen * Copyright (c) Cryptoapi developers. 141953c22fSAaro Koskinen * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 151953c22fSAaro Koskinen * 161953c22fSAaro Koskinen * This program is free software; you can redistribute it and/or modify it 171953c22fSAaro Koskinen * under the terms of the GNU General Public License as published by the Free 181953c22fSAaro Koskinen * Software Foundation; either version 2 of the License, or (at your option) 191953c22fSAaro Koskinen * any later version. 201953c22fSAaro Koskinen */ 211953c22fSAaro Koskinen 221953c22fSAaro Koskinen #include <crypto/md5.h> 231953c22fSAaro Koskinen #include <linux/init.h> 241953c22fSAaro Koskinen #include <linux/types.h> 251953c22fSAaro Koskinen #include <linux/module.h> 261953c22fSAaro Koskinen #include <linux/string.h> 271953c22fSAaro Koskinen #include <asm/byteorder.h> 281953c22fSAaro Koskinen #include <asm/octeon/octeon.h> 291953c22fSAaro Koskinen #include <crypto/internal/hash.h> 301953c22fSAaro Koskinen 311953c22fSAaro Koskinen #include "octeon-crypto.h" 321953c22fSAaro Koskinen 331953c22fSAaro Koskinen /* 341953c22fSAaro Koskinen * We pass everything as 64-bit. OCTEON can handle misaligned data. 351953c22fSAaro Koskinen */ 361953c22fSAaro Koskinen 371953c22fSAaro Koskinen static void octeon_md5_store_hash(struct md5_state *ctx) 381953c22fSAaro Koskinen { 391953c22fSAaro Koskinen u64 *hash = (u64 *)ctx->hash; 401953c22fSAaro Koskinen 411953c22fSAaro Koskinen write_octeon_64bit_hash_dword(hash[0], 0); 421953c22fSAaro Koskinen write_octeon_64bit_hash_dword(hash[1], 1); 431953c22fSAaro Koskinen } 441953c22fSAaro Koskinen 451953c22fSAaro Koskinen static void octeon_md5_read_hash(struct md5_state *ctx) 461953c22fSAaro Koskinen { 471953c22fSAaro Koskinen u64 *hash = (u64 *)ctx->hash; 481953c22fSAaro Koskinen 491953c22fSAaro Koskinen hash[0] = read_octeon_64bit_hash_dword(0); 501953c22fSAaro Koskinen hash[1] = read_octeon_64bit_hash_dword(1); 511953c22fSAaro Koskinen } 521953c22fSAaro Koskinen 531953c22fSAaro Koskinen static void octeon_md5_transform(const void *_block) 541953c22fSAaro Koskinen { 551953c22fSAaro Koskinen const u64 *block = _block; 561953c22fSAaro Koskinen 571953c22fSAaro Koskinen write_octeon_64bit_block_dword(block[0], 0); 581953c22fSAaro Koskinen write_octeon_64bit_block_dword(block[1], 1); 591953c22fSAaro Koskinen write_octeon_64bit_block_dword(block[2], 2); 601953c22fSAaro Koskinen write_octeon_64bit_block_dword(block[3], 3); 611953c22fSAaro Koskinen write_octeon_64bit_block_dword(block[4], 4); 621953c22fSAaro Koskinen write_octeon_64bit_block_dword(block[5], 5); 631953c22fSAaro Koskinen write_octeon_64bit_block_dword(block[6], 6); 641953c22fSAaro Koskinen octeon_md5_start(block[7]); 651953c22fSAaro Koskinen } 661953c22fSAaro Koskinen 671953c22fSAaro Koskinen static int octeon_md5_init(struct shash_desc *desc) 681953c22fSAaro Koskinen { 691953c22fSAaro Koskinen struct md5_state *mctx = shash_desc_ctx(desc); 701953c22fSAaro Koskinen 71*7c2f5537SHerbert Xu mctx->hash[0] = MD5_H0; 72*7c2f5537SHerbert Xu mctx->hash[1] = MD5_H1; 73*7c2f5537SHerbert Xu mctx->hash[2] = MD5_H2; 74*7c2f5537SHerbert Xu mctx->hash[3] = MD5_H3; 75*7c2f5537SHerbert Xu cpu_to_le32_array(mctx->hash, 4); 761953c22fSAaro Koskinen mctx->byte_count = 0; 771953c22fSAaro Koskinen 781953c22fSAaro Koskinen return 0; 791953c22fSAaro Koskinen } 801953c22fSAaro Koskinen 811953c22fSAaro Koskinen static int octeon_md5_update(struct shash_desc *desc, const u8 *data, 821953c22fSAaro Koskinen unsigned int len) 831953c22fSAaro Koskinen { 841953c22fSAaro Koskinen struct md5_state *mctx = shash_desc_ctx(desc); 851953c22fSAaro Koskinen const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f); 861953c22fSAaro Koskinen struct octeon_cop2_state state; 871953c22fSAaro Koskinen unsigned long flags; 881953c22fSAaro Koskinen 891953c22fSAaro Koskinen mctx->byte_count += len; 901953c22fSAaro Koskinen 911953c22fSAaro Koskinen if (avail > len) { 921953c22fSAaro Koskinen memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 931953c22fSAaro Koskinen data, len); 941953c22fSAaro Koskinen return 0; 951953c22fSAaro Koskinen } 961953c22fSAaro Koskinen 971953c22fSAaro Koskinen memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), data, 981953c22fSAaro Koskinen avail); 991953c22fSAaro Koskinen 1001953c22fSAaro Koskinen flags = octeon_crypto_enable(&state); 1011953c22fSAaro Koskinen octeon_md5_store_hash(mctx); 1021953c22fSAaro Koskinen 1031953c22fSAaro Koskinen octeon_md5_transform(mctx->block); 1041953c22fSAaro Koskinen data += avail; 1051953c22fSAaro Koskinen len -= avail; 1061953c22fSAaro Koskinen 1071953c22fSAaro Koskinen while (len >= sizeof(mctx->block)) { 1081953c22fSAaro Koskinen octeon_md5_transform(data); 1091953c22fSAaro Koskinen data += sizeof(mctx->block); 1101953c22fSAaro Koskinen len -= sizeof(mctx->block); 1111953c22fSAaro Koskinen } 1121953c22fSAaro Koskinen 1131953c22fSAaro Koskinen octeon_md5_read_hash(mctx); 1141953c22fSAaro Koskinen octeon_crypto_disable(&state, flags); 1151953c22fSAaro Koskinen 1161953c22fSAaro Koskinen memcpy(mctx->block, data, len); 1171953c22fSAaro Koskinen 1181953c22fSAaro Koskinen return 0; 1191953c22fSAaro Koskinen } 1201953c22fSAaro Koskinen 1211953c22fSAaro Koskinen static int octeon_md5_final(struct shash_desc *desc, u8 *out) 1221953c22fSAaro Koskinen { 1231953c22fSAaro Koskinen struct md5_state *mctx = shash_desc_ctx(desc); 1241953c22fSAaro Koskinen const unsigned int offset = mctx->byte_count & 0x3f; 1251953c22fSAaro Koskinen char *p = (char *)mctx->block + offset; 1261953c22fSAaro Koskinen int padding = 56 - (offset + 1); 1271953c22fSAaro Koskinen struct octeon_cop2_state state; 1281953c22fSAaro Koskinen unsigned long flags; 1291953c22fSAaro Koskinen 1301953c22fSAaro Koskinen *p++ = 0x80; 1311953c22fSAaro Koskinen 1321953c22fSAaro Koskinen flags = octeon_crypto_enable(&state); 1331953c22fSAaro Koskinen octeon_md5_store_hash(mctx); 1341953c22fSAaro Koskinen 1351953c22fSAaro Koskinen if (padding < 0) { 1361953c22fSAaro Koskinen memset(p, 0x00, padding + sizeof(u64)); 1371953c22fSAaro Koskinen octeon_md5_transform(mctx->block); 1381953c22fSAaro Koskinen p = (char *)mctx->block; 1391953c22fSAaro Koskinen padding = 56; 1401953c22fSAaro Koskinen } 1411953c22fSAaro Koskinen 1421953c22fSAaro Koskinen memset(p, 0, padding); 143*7c2f5537SHerbert Xu mctx->block[14] = mctx->byte_count << 3; 144*7c2f5537SHerbert Xu mctx->block[15] = mctx->byte_count >> 29; 145*7c2f5537SHerbert Xu cpu_to_le32_array(mctx->block + 14, 2); 1461953c22fSAaro Koskinen octeon_md5_transform(mctx->block); 1471953c22fSAaro Koskinen 1481953c22fSAaro Koskinen octeon_md5_read_hash(mctx); 1491953c22fSAaro Koskinen octeon_crypto_disable(&state, flags); 1501953c22fSAaro Koskinen 1511953c22fSAaro Koskinen memcpy(out, mctx->hash, sizeof(mctx->hash)); 1521953c22fSAaro Koskinen memset(mctx, 0, sizeof(*mctx)); 1531953c22fSAaro Koskinen 1541953c22fSAaro Koskinen return 0; 1551953c22fSAaro Koskinen } 1561953c22fSAaro Koskinen 1571953c22fSAaro Koskinen static int octeon_md5_export(struct shash_desc *desc, void *out) 1581953c22fSAaro Koskinen { 1591953c22fSAaro Koskinen struct md5_state *ctx = shash_desc_ctx(desc); 1601953c22fSAaro Koskinen 1611953c22fSAaro Koskinen memcpy(out, ctx, sizeof(*ctx)); 1621953c22fSAaro Koskinen return 0; 1631953c22fSAaro Koskinen } 1641953c22fSAaro Koskinen 1651953c22fSAaro Koskinen static int octeon_md5_import(struct shash_desc *desc, const void *in) 1661953c22fSAaro Koskinen { 1671953c22fSAaro Koskinen struct md5_state *ctx = shash_desc_ctx(desc); 1681953c22fSAaro Koskinen 1691953c22fSAaro Koskinen memcpy(ctx, in, sizeof(*ctx)); 1701953c22fSAaro Koskinen return 0; 1711953c22fSAaro Koskinen } 1721953c22fSAaro Koskinen 1731953c22fSAaro Koskinen static struct shash_alg alg = { 1741953c22fSAaro Koskinen .digestsize = MD5_DIGEST_SIZE, 1751953c22fSAaro Koskinen .init = octeon_md5_init, 1761953c22fSAaro Koskinen .update = octeon_md5_update, 1771953c22fSAaro Koskinen .final = octeon_md5_final, 1781953c22fSAaro Koskinen .export = octeon_md5_export, 1791953c22fSAaro Koskinen .import = octeon_md5_import, 1801953c22fSAaro Koskinen .descsize = sizeof(struct md5_state), 1811953c22fSAaro Koskinen .statesize = sizeof(struct md5_state), 1821953c22fSAaro Koskinen .base = { 1831953c22fSAaro Koskinen .cra_name = "md5", 1841953c22fSAaro Koskinen .cra_driver_name= "octeon-md5", 1851953c22fSAaro Koskinen .cra_priority = OCTEON_CR_OPCODE_PRIORITY, 1861953c22fSAaro Koskinen .cra_blocksize = MD5_HMAC_BLOCK_SIZE, 1871953c22fSAaro Koskinen .cra_module = THIS_MODULE, 1881953c22fSAaro Koskinen } 1891953c22fSAaro Koskinen }; 1901953c22fSAaro Koskinen 1911953c22fSAaro Koskinen static int __init md5_mod_init(void) 1921953c22fSAaro Koskinen { 1931953c22fSAaro Koskinen if (!octeon_has_crypto()) 1941953c22fSAaro Koskinen return -ENOTSUPP; 1951953c22fSAaro Koskinen return crypto_register_shash(&alg); 1961953c22fSAaro Koskinen } 1971953c22fSAaro Koskinen 1981953c22fSAaro Koskinen static void __exit md5_mod_fini(void) 1991953c22fSAaro Koskinen { 2001953c22fSAaro Koskinen crypto_unregister_shash(&alg); 2011953c22fSAaro Koskinen } 2021953c22fSAaro Koskinen 2031953c22fSAaro Koskinen module_init(md5_mod_init); 2041953c22fSAaro Koskinen module_exit(md5_mod_fini); 2051953c22fSAaro Koskinen 2061953c22fSAaro Koskinen MODULE_LICENSE("GPL"); 2071953c22fSAaro Koskinen MODULE_DESCRIPTION("MD5 Message Digest Algorithm (OCTEON)"); 2081953c22fSAaro Koskinen MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>"); 209