| 1 | /* |
| 2 | * arch/ubicom32/crypto/md5_ubicom32.c |
| 3 | * Ubicom32 implementation of the MD5 Secure Hash Algorithm |
| 4 | * |
| 5 | * (C) Copyright 2009, Ubicom, Inc. |
| 6 | * |
| 7 | * This file is part of the Ubicom32 Linux Kernel Port. |
| 8 | * |
| 9 | * The Ubicom32 Linux Kernel Port is free software: you can redistribute |
| 10 | * it and/or modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation, either version 2 of the |
| 12 | * License, or (at your option) any later version. |
| 13 | * |
| 14 | * The Ubicom32 Linux Kernel Port is distributed in the hope that it |
| 15 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 16 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 17 | * the GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with the Ubicom32 Linux Kernel Port. If not, |
| 21 | * see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | * Ubicom32 implementation derived from (with many thanks): |
| 24 | * arch/m68knommu |
| 25 | * arch/blackfin |
| 26 | * arch/parisc |
| 27 | */ |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/crypto.h> |
| 31 | |
| 32 | #include "crypto_ubicom32.h" |
| 33 | |
| 34 | #define MD5_DIGEST_SIZE 16 |
| 35 | #define MD5_BLOCK_SIZE 64 |
| 36 | #define MD5_HASH_WORDS 4 |
| 37 | |
| 38 | extern void _md5_ip5k_init_digest(u32_t *digest); |
| 39 | extern void _md5_ip5k_transform(u32_t *data_input); |
| 40 | extern void _md5_ip5k_get_digest(u32_t *digest); |
| 41 | |
| 42 | struct ubicom32_md5_ctx { |
| 43 | u64 count; /* message length */ |
| 44 | u32 state[MD5_HASH_WORDS]; |
| 45 | u8 buf[2 * MD5_BLOCK_SIZE]; |
| 46 | }; |
| 47 | |
| 48 | static void md5_init(struct crypto_tfm *tfm) |
| 49 | { |
| 50 | struct ubicom32_md5_ctx *mctx = crypto_tfm_ctx(tfm); |
| 51 | mctx->state[0] = 0x01234567; |
| 52 | mctx->state[1] = 0x89abcdef; |
| 53 | mctx->state[2] = 0xfedcba98; |
| 54 | mctx->state[3] = 0x76543210; |
| 55 | |
| 56 | mctx->count = 0; |
| 57 | } |
| 58 | |
| 59 | static inline void _md5_process(u32 *digest, const u8 *data) |
| 60 | { |
| 61 | _md5_ip5k_transform((u32 *)data); |
| 62 | } |
| 63 | |
| 64 | static void md5_update(struct crypto_tfm *tfm, const u8 *data, |
| 65 | unsigned int len) |
| 66 | { |
| 67 | struct ubicom32_md5_ctx *mctx = crypto_tfm_ctx(tfm); |
| 68 | int index, clen; |
| 69 | |
| 70 | /* how much is already in the buffer? */ |
| 71 | index = mctx->count & 0x3f; |
| 72 | |
| 73 | mctx->count += len; |
| 74 | |
| 75 | if (index + len < MD5_BLOCK_SIZE) { |
| 76 | goto store_only; |
| 77 | } |
| 78 | |
| 79 | hw_crypto_lock(); |
| 80 | hw_crypto_check(); |
| 81 | |
| 82 | /* init digest set ctrl register too */ |
| 83 | _md5_ip5k_init_digest(mctx->state); |
| 84 | |
| 85 | if (unlikely(index == 0 && SEC_ALIGNED(data))) { |
| 86 | fast_process: |
| 87 | while (len >= MD5_BLOCK_SIZE) { |
| 88 | _md5_process(mctx->state, data); |
| 89 | data += MD5_BLOCK_SIZE; |
| 90 | len -= MD5_BLOCK_SIZE; |
| 91 | } |
| 92 | goto store; |
| 93 | } |
| 94 | |
| 95 | /* process one stored block */ |
| 96 | if (index) { |
| 97 | clen = MD5_BLOCK_SIZE - index; |
| 98 | memcpy(mctx->buf + index, data, clen); |
| 99 | _md5_process(mctx->state, mctx->buf); |
| 100 | data += clen; |
| 101 | len -= clen; |
| 102 | index = 0; |
| 103 | } |
| 104 | |
| 105 | if (likely(SEC_ALIGNED(data))) { |
| 106 | goto fast_process; |
| 107 | } |
| 108 | |
| 109 | /* process as many blocks as possible */ |
| 110 | while (len >= MD5_BLOCK_SIZE) { |
| 111 | memcpy(mctx->buf, data, MD5_BLOCK_SIZE); |
| 112 | _md5_process(mctx->state, mctx->buf); |
| 113 | data += MD5_BLOCK_SIZE; |
| 114 | len -= MD5_BLOCK_SIZE; |
| 115 | } |
| 116 | |
| 117 | store: |
| 118 | _md5_ip5k_get_digest(mctx->state); |
| 119 | hw_crypto_unlock(); |
| 120 | |
| 121 | store_only: |
| 122 | /* anything left? */ |
| 123 | if (len) |
| 124 | memcpy(mctx->buf + index , data, len); |
| 125 | } |
| 126 | |
| 127 | /* Add padding and return the message digest. */ |
| 128 | static void md5_final(struct crypto_tfm *tfm, u8 *out) |
| 129 | { |
| 130 | struct ubicom32_md5_ctx *mctx = crypto_tfm_ctx(tfm); |
| 131 | u32 bits[2]; |
| 132 | unsigned int index, end; |
| 133 | |
| 134 | /* must perform manual padding */ |
| 135 | index = mctx->count & 0x3f; |
| 136 | end = (index < 56) ? MD5_BLOCK_SIZE : (2 * MD5_BLOCK_SIZE); |
| 137 | |
| 138 | /* start pad with 1 */ |
| 139 | mctx->buf[index] = 0x80; |
| 140 | |
| 141 | /* pad with zeros */ |
| 142 | index++; |
| 143 | memset(mctx->buf + index, 0x00, end - index - 8); |
| 144 | |
| 145 | /* append message length */ |
| 146 | bits[0] = mctx->count << 3; |
| 147 | bits[1] = mctx->count >> 29; |
| 148 | __cpu_to_le32s(bits); |
| 149 | __cpu_to_le32s(bits + 1); |
| 150 | |
| 151 | memcpy(mctx->buf + end - 8, &bits, sizeof(bits)); |
| 152 | |
| 153 | /* force to use the mctx->buf and ignore the partial buf */ |
| 154 | mctx->count = mctx->count & ~0x3f; |
| 155 | md5_update(tfm, mctx->buf, end); |
| 156 | |
| 157 | /* copy digest to out */ |
| 158 | memcpy(out, mctx->state, MD5_DIGEST_SIZE); |
| 159 | |
| 160 | /* wipe context */ |
| 161 | memset(mctx, 0, sizeof *mctx); |
| 162 | } |
| 163 | |
| 164 | static struct crypto_alg alg = { |
| 165 | .cra_name = "md5", |
| 166 | .cra_driver_name= "md5-ubicom32", |
| 167 | .cra_priority = CRYPTO_UBICOM32_PRIORITY, |
| 168 | .cra_flags = CRYPTO_ALG_TYPE_DIGEST, |
| 169 | .cra_blocksize = MD5_BLOCK_SIZE, |
| 170 | .cra_ctxsize = sizeof(struct ubicom32_md5_ctx), |
| 171 | .cra_module = THIS_MODULE, |
| 172 | .cra_list = LIST_HEAD_INIT(alg.cra_list), |
| 173 | .cra_u = { |
| 174 | .digest = { |
| 175 | .dia_digestsize = MD5_DIGEST_SIZE, |
| 176 | .dia_init = md5_init, |
| 177 | .dia_update = md5_update, |
| 178 | .dia_final = md5_final, |
| 179 | } |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | static int __init init(void) |
| 184 | { |
| 185 | hw_crypto_init(); |
| 186 | return crypto_register_alg(&alg); |
| 187 | } |
| 188 | |
| 189 | static void __exit fini(void) |
| 190 | { |
| 191 | crypto_unregister_alg(&alg); |
| 192 | } |
| 193 | |
| 194 | module_init(init); |
| 195 | module_exit(fini); |
| 196 | |
| 197 | MODULE_ALIAS("md5"); |
| 198 | |
| 199 | MODULE_LICENSE("GPL"); |
| 200 | MODULE_DESCRIPTION("MD5 Secure Hash Algorithm"); |
| 201 | |