/* * Cryptographic API. * * Support for Infineon DEU hardware crypto engine. * * Copyright (c) 2005 Johannes Doering , INFINEON * * modified from crypto/md5.c * * --------------------------------------------------------------------------- * Cryptographic API. * * MD5 Message Digest Algorithm (RFC1321). * * Derived from cryptoapi implementation, originally based on the * public domain implementation written by Colin Plumb in 1993. * * Copyright (c) Cryptoapi developers. * Copyright (c) 2002 James Morris * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * --------------------------------------------------------------------------- ** HISTORY ** $Date $Author $Comment * --------------------------------------------------------------------------- */ /** \addtogroup AMAZON_S_DEU \ingroup AMAZON_S_BSP \brief amazon_s deu driver module */ /*! \file amazon_s_deu_md5.c \ingroup AMAZON_S_DEU \brief md5 driver file */ /*! \addtogroup AMAZON_S_DEU_FUNCTIONS \ingroup AMAZON_S_DEU \brief amazon_s deu driver functions */ #include #include #include #include #include #include #include #define MD5_DIGEST_SIZE 16 #define MD5_HMAC_BLOCK_SIZE 64 #define MD5_BLOCK_WORDS 16 #define MD5_HASH_WORDS 4 #if 0 #define CRTCL_SECT_INIT #define CRTCL_SECT_START local_irq_save(flag) #define CRTCL_SECT_END local_irq_restore(flag) #else static spinlock_t lock; #define CRTCL_SECT_INIT spin_lock_init(&lock) #define CRTCL_SECT_START spin_lock_irqsave(&lock, flag) #define CRTCL_SECT_END spin_unlock_irqrestore(&lock, flag) #endif //#define CRYPTO_DEBUG #ifdef CRYPTO_DEBUG extern char debug_level; #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args); #else #define DPRINTF(level, format, args...) #endif #ifdef CONFIG_CRYPTO_DEV_AMAZON_S_MD5 #include #include #define HASH_START AMAZON_S_HASH_CON #endif #ifdef CONFIG_CRYPTO_DEV_AMAZON_S_DMA #include #include #include #endif struct md5_ctx { u32 hash[MD5_HASH_WORDS]; u32 block[MD5_BLOCK_WORDS]; u64 byte_count; }; extern int disable_deudma; /*! \fn static u32 endian_swap(u32 input) \ingroup AMAZON_S_DEU_FUNCTIONS \brief perform dword level endian swap \param input value of dword that requires to be swapped */ static u32 endian_swap(u32 input) { u8 *ptr = (u8 *)&input; return ((ptr[3] << 24) | (ptr[2] << 16) | (ptr[1] << 8) | ptr[0]); } /*! \fn static void md5_transform(u32 *hash, u32 const *in) \ingroup AMAZON_S_DEU_FUNCTIONS \brief main interface to md5 hardware \param hash current hash value \param in 64-byte block of input */ static void md5_transform(u32 *hash, u32 const *in) { int i; volatile struct deu_hash_t *hashs = (struct deu_hash_t *) HASH_START; unsigned long flag; CRTCL_SECT_START; for (i = 0; i < 16; i++) { //printk("in[%d] = %08x\n", i, endian_swap(in[i])); hashs->MR = endian_swap(in[i]); }; //wait for processing while (hashs->controlr.BSY) { // this will not take long } CRTCL_SECT_END; } /*! \fn static inline void md5_transform_helper(struct md5_ctx *ctx) \ingroup AMAZON_S_DEU_FUNCTIONS \brief interfacing function for md5_transform() \param ctx crypto context */ static inline void md5_transform_helper(struct md5_ctx *ctx) { //le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32)); md5_transform(ctx->hash, ctx->block); } /*! \fn static void md5_init(struct crypto_tfm *tfm) \ingroup AMAZON_S_DEU_FUNCTIONS \brief initialize md5 hardware \param tfm linux crypto algo transform */ static void md5_init(struct crypto_tfm *tfm) { struct md5_ctx *mctx = crypto_tfm_ctx(tfm); volatile struct deu_hash_t *hash = (struct deu_hash_t *) HASH_START; hash->controlr.SM = 1; hash->controlr.ALGO = 1; // 1 = md5 0 = sha1 hash->controlr.INIT = 1; // Initialize the hash operation by writing a '1' to the INIT bit. mctx->byte_count = 0; } /*! \fn static void md5_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len) \ingroup AMAZON_S_DEU_FUNCTIONS \brief on-the-fly md5 computation \param tfm linux crypto algo transform \param data input data \param len size of input data */ static void md5_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len) { struct md5_ctx *mctx = crypto_tfm_ctx(tfm); const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f); mctx->byte_count += len; if (avail > len) { memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), data, len); return; } memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), data, avail); md5_transform_helper(mctx); data += avail; len -= avail; while (len >= sizeof(mctx->block)) { memcpy(mctx->block, data, sizeof(mctx->block)); md5_transform_helper(mctx); data += sizeof(mctx->block); len -= sizeof(mctx->block); } memcpy(mctx->block, data, len); } /*! \fn static void md5_final(struct crypto_tfm *tfm, u8 *out) \ingroup AMAZON_S_DEU_FUNCTIONS \brief compute final md5 value \param tfm linux crypto algo transform \param out final md5 output value */ static void md5_final(struct crypto_tfm *tfm, u8 *out) { struct md5_ctx *mctx = crypto_tfm_ctx(tfm); const unsigned int offset = mctx->byte_count & 0x3f; char *p = (char *)mctx->block + offset; int padding = 56 - (offset + 1); volatile struct deu_hash_t *hashs = (struct deu_hash_t *) HASH_START; u32 flag; *p++ = 0x80; if (padding < 0) { memset(p, 0x00, padding + sizeof (u64)); md5_transform_helper(mctx); p = (char *)mctx->block; padding = 56; } memset(p, 0, padding); mctx->block[14] = endian_swap(mctx->byte_count << 3); mctx->block[15] = endian_swap(mctx->byte_count >> 29); #if 0 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) - sizeof(u64)) / sizeof(u32)); #endif md5_transform(mctx->hash, mctx->block); //cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32)); //memcpy(out, mctx->hash, sizeof(mctx->hash)); CRTCL_SECT_START; *((u32 *) out + 0) = endian_swap (hashs->D1R); *((u32 *) out + 1) = endian_swap (hashs->D2R); *((u32 *) out + 2) = endian_swap (hashs->D3R); *((u32 *) out + 3) = endian_swap (hashs->D4R); CRTCL_SECT_END; // Wipe context memset(mctx, 0, sizeof(*mctx)); } static struct crypto_alg ifxdeu_md5_alg = { .cra_name = "md5", .cra_driver_name = "ifxdeu-md5", .cra_flags = CRYPTO_ALG_TYPE_DIGEST, .cra_blocksize = MD5_HMAC_BLOCK_SIZE, .cra_ctxsize = sizeof(struct md5_ctx), .cra_module = THIS_MODULE, .cra_list = LIST_HEAD_INIT(ifxdeu_md5_alg.cra_list), .cra_u = { .digest = { .dia_digestsize = MD5_DIGEST_SIZE, .dia_init = md5_init, .dia_update = md5_update, .dia_final = md5_final } } }; /*! \fn int __init ifxdeu_init_md5 (void) \ingroup AMAZON_S_DEU_FUNCTIONS \brief initialize md5 driver */ int __init ifxdeu_init_md5 (void) { int ret; if ((ret = crypto_register_alg(&ifxdeu_md5_alg))) goto md5_err; CRTCL_SECT_INIT; printk (KERN_NOTICE "IFX DEU MD5 initialized%s.\n", disable_deudma ? "" : " (DMA)"); return ret; md5_err: printk(KERN_ERR "IFX DEU MD5 initialization failed!\n"); return ret; } /*! \fn void __exit ifxdeu_fini_md5 (void) \ingroup AMAZON_S_DEU_FUNCTIONS \brief unregister md5 driver */ void __exit ifxdeu_fini_md5 (void) { crypto_unregister_alg (&ifxdeu_md5_alg); }