18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Cryptographic API. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * MD5 Message Digest Algorithm (RFC1321). 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Derived from cryptoapi implementation, originally based on the 78c2ecf20Sopenharmony_ci * public domain implementation written by Colin Plumb in 1993. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Copyright (c) Cryptoapi developers. 108c2ecf20Sopenharmony_ci * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it 138c2ecf20Sopenharmony_ci * under the terms of the GNU General Public License as published by the Free 148c2ecf20Sopenharmony_ci * Software Foundation; either version 2 of the License, or (at your option) 158c2ecf20Sopenharmony_ci * any later version. 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci */ 188c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h> 198c2ecf20Sopenharmony_ci#include <crypto/md5.h> 208c2ecf20Sopenharmony_ci#include <linux/init.h> 218c2ecf20Sopenharmony_ci#include <linux/module.h> 228c2ecf20Sopenharmony_ci#include <linux/string.h> 238c2ecf20Sopenharmony_ci#include <linux/types.h> 248c2ecf20Sopenharmony_ci#include <asm/byteorder.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ciconst u8 md5_zero_message_hash[MD5_DIGEST_SIZE] = { 278c2ecf20Sopenharmony_ci 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 288c2ecf20Sopenharmony_ci 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e, 298c2ecf20Sopenharmony_ci}; 308c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(md5_zero_message_hash); 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define F1(x, y, z) (z ^ (x & (y ^ z))) 338c2ecf20Sopenharmony_ci#define F2(x, y, z) F1(z, x, y) 348c2ecf20Sopenharmony_ci#define F3(x, y, z) (x ^ y ^ z) 358c2ecf20Sopenharmony_ci#define F4(x, y, z) (y ^ (x | ~z)) 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci#define MD5STEP(f, w, x, y, z, in, s) \ 388c2ecf20Sopenharmony_ci (w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x) 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic void md5_transform(__u32 *hash, __u32 const *in) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci u32 a, b, c, d; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci a = hash[0]; 458c2ecf20Sopenharmony_ci b = hash[1]; 468c2ecf20Sopenharmony_ci c = hash[2]; 478c2ecf20Sopenharmony_ci d = hash[3]; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); 508c2ecf20Sopenharmony_ci MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); 518c2ecf20Sopenharmony_ci MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); 528c2ecf20Sopenharmony_ci MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); 538c2ecf20Sopenharmony_ci MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); 548c2ecf20Sopenharmony_ci MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); 558c2ecf20Sopenharmony_ci MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); 568c2ecf20Sopenharmony_ci MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); 578c2ecf20Sopenharmony_ci MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); 588c2ecf20Sopenharmony_ci MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); 598c2ecf20Sopenharmony_ci MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); 608c2ecf20Sopenharmony_ci MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); 618c2ecf20Sopenharmony_ci MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); 628c2ecf20Sopenharmony_ci MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); 638c2ecf20Sopenharmony_ci MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); 648c2ecf20Sopenharmony_ci MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); 678c2ecf20Sopenharmony_ci MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); 688c2ecf20Sopenharmony_ci MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); 698c2ecf20Sopenharmony_ci MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); 708c2ecf20Sopenharmony_ci MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); 718c2ecf20Sopenharmony_ci MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); 728c2ecf20Sopenharmony_ci MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); 738c2ecf20Sopenharmony_ci MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); 748c2ecf20Sopenharmony_ci MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); 758c2ecf20Sopenharmony_ci MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); 768c2ecf20Sopenharmony_ci MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); 778c2ecf20Sopenharmony_ci MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); 788c2ecf20Sopenharmony_ci MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); 798c2ecf20Sopenharmony_ci MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); 808c2ecf20Sopenharmony_ci MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); 818c2ecf20Sopenharmony_ci MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); 848c2ecf20Sopenharmony_ci MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); 858c2ecf20Sopenharmony_ci MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); 868c2ecf20Sopenharmony_ci MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); 878c2ecf20Sopenharmony_ci MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); 888c2ecf20Sopenharmony_ci MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); 898c2ecf20Sopenharmony_ci MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); 908c2ecf20Sopenharmony_ci MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); 918c2ecf20Sopenharmony_ci MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); 928c2ecf20Sopenharmony_ci MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); 938c2ecf20Sopenharmony_ci MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); 948c2ecf20Sopenharmony_ci MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); 958c2ecf20Sopenharmony_ci MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); 968c2ecf20Sopenharmony_ci MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); 978c2ecf20Sopenharmony_ci MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); 988c2ecf20Sopenharmony_ci MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); 1018c2ecf20Sopenharmony_ci MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); 1028c2ecf20Sopenharmony_ci MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); 1038c2ecf20Sopenharmony_ci MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); 1048c2ecf20Sopenharmony_ci MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); 1058c2ecf20Sopenharmony_ci MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); 1068c2ecf20Sopenharmony_ci MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); 1078c2ecf20Sopenharmony_ci MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); 1088c2ecf20Sopenharmony_ci MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); 1098c2ecf20Sopenharmony_ci MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); 1108c2ecf20Sopenharmony_ci MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); 1118c2ecf20Sopenharmony_ci MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); 1128c2ecf20Sopenharmony_ci MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); 1138c2ecf20Sopenharmony_ci MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); 1148c2ecf20Sopenharmony_ci MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); 1158c2ecf20Sopenharmony_ci MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci hash[0] += a; 1188c2ecf20Sopenharmony_ci hash[1] += b; 1198c2ecf20Sopenharmony_ci hash[2] += c; 1208c2ecf20Sopenharmony_ci hash[3] += d; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic inline void md5_transform_helper(struct md5_state *ctx) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32)); 1268c2ecf20Sopenharmony_ci md5_transform(ctx->hash, ctx->block); 1278c2ecf20Sopenharmony_ci} 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cistatic int md5_init(struct shash_desc *desc) 1308c2ecf20Sopenharmony_ci{ 1318c2ecf20Sopenharmony_ci struct md5_state *mctx = shash_desc_ctx(desc); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci mctx->hash[0] = MD5_H0; 1348c2ecf20Sopenharmony_ci mctx->hash[1] = MD5_H1; 1358c2ecf20Sopenharmony_ci mctx->hash[2] = MD5_H2; 1368c2ecf20Sopenharmony_ci mctx->hash[3] = MD5_H3; 1378c2ecf20Sopenharmony_ci mctx->byte_count = 0; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci return 0; 1408c2ecf20Sopenharmony_ci} 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_cistatic int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len) 1438c2ecf20Sopenharmony_ci{ 1448c2ecf20Sopenharmony_ci struct md5_state *mctx = shash_desc_ctx(desc); 1458c2ecf20Sopenharmony_ci const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f); 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci mctx->byte_count += len; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci if (avail > len) { 1508c2ecf20Sopenharmony_ci memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 1518c2ecf20Sopenharmony_ci data, len); 1528c2ecf20Sopenharmony_ci return 0; 1538c2ecf20Sopenharmony_ci } 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 1568c2ecf20Sopenharmony_ci data, avail); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci md5_transform_helper(mctx); 1598c2ecf20Sopenharmony_ci data += avail; 1608c2ecf20Sopenharmony_ci len -= avail; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci while (len >= sizeof(mctx->block)) { 1638c2ecf20Sopenharmony_ci memcpy(mctx->block, data, sizeof(mctx->block)); 1648c2ecf20Sopenharmony_ci md5_transform_helper(mctx); 1658c2ecf20Sopenharmony_ci data += sizeof(mctx->block); 1668c2ecf20Sopenharmony_ci len -= sizeof(mctx->block); 1678c2ecf20Sopenharmony_ci } 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci memcpy(mctx->block, data, len); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci return 0; 1728c2ecf20Sopenharmony_ci} 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_cistatic int md5_final(struct shash_desc *desc, u8 *out) 1758c2ecf20Sopenharmony_ci{ 1768c2ecf20Sopenharmony_ci struct md5_state *mctx = shash_desc_ctx(desc); 1778c2ecf20Sopenharmony_ci const unsigned int offset = mctx->byte_count & 0x3f; 1788c2ecf20Sopenharmony_ci char *p = (char *)mctx->block + offset; 1798c2ecf20Sopenharmony_ci int padding = 56 - (offset + 1); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci *p++ = 0x80; 1828c2ecf20Sopenharmony_ci if (padding < 0) { 1838c2ecf20Sopenharmony_ci memset(p, 0x00, padding + sizeof (u64)); 1848c2ecf20Sopenharmony_ci md5_transform_helper(mctx); 1858c2ecf20Sopenharmony_ci p = (char *)mctx->block; 1868c2ecf20Sopenharmony_ci padding = 56; 1878c2ecf20Sopenharmony_ci } 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci memset(p, 0, padding); 1908c2ecf20Sopenharmony_ci mctx->block[14] = mctx->byte_count << 3; 1918c2ecf20Sopenharmony_ci mctx->block[15] = mctx->byte_count >> 29; 1928c2ecf20Sopenharmony_ci le32_to_cpu_array(mctx->block, (sizeof(mctx->block) - 1938c2ecf20Sopenharmony_ci sizeof(u64)) / sizeof(u32)); 1948c2ecf20Sopenharmony_ci md5_transform(mctx->hash, mctx->block); 1958c2ecf20Sopenharmony_ci cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32)); 1968c2ecf20Sopenharmony_ci memcpy(out, mctx->hash, sizeof(mctx->hash)); 1978c2ecf20Sopenharmony_ci memset(mctx, 0, sizeof(*mctx)); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci return 0; 2008c2ecf20Sopenharmony_ci} 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_cistatic int md5_export(struct shash_desc *desc, void *out) 2038c2ecf20Sopenharmony_ci{ 2048c2ecf20Sopenharmony_ci struct md5_state *ctx = shash_desc_ctx(desc); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci memcpy(out, ctx, sizeof(*ctx)); 2078c2ecf20Sopenharmony_ci return 0; 2088c2ecf20Sopenharmony_ci} 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_cistatic int md5_import(struct shash_desc *desc, const void *in) 2118c2ecf20Sopenharmony_ci{ 2128c2ecf20Sopenharmony_ci struct md5_state *ctx = shash_desc_ctx(desc); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci memcpy(ctx, in, sizeof(*ctx)); 2158c2ecf20Sopenharmony_ci return 0; 2168c2ecf20Sopenharmony_ci} 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_cistatic struct shash_alg alg = { 2198c2ecf20Sopenharmony_ci .digestsize = MD5_DIGEST_SIZE, 2208c2ecf20Sopenharmony_ci .init = md5_init, 2218c2ecf20Sopenharmony_ci .update = md5_update, 2228c2ecf20Sopenharmony_ci .final = md5_final, 2238c2ecf20Sopenharmony_ci .export = md5_export, 2248c2ecf20Sopenharmony_ci .import = md5_import, 2258c2ecf20Sopenharmony_ci .descsize = sizeof(struct md5_state), 2268c2ecf20Sopenharmony_ci .statesize = sizeof(struct md5_state), 2278c2ecf20Sopenharmony_ci .base = { 2288c2ecf20Sopenharmony_ci .cra_name = "md5", 2298c2ecf20Sopenharmony_ci .cra_driver_name = "md5-generic", 2308c2ecf20Sopenharmony_ci .cra_blocksize = MD5_HMAC_BLOCK_SIZE, 2318c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 2328c2ecf20Sopenharmony_ci } 2338c2ecf20Sopenharmony_ci}; 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_cistatic int __init md5_mod_init(void) 2368c2ecf20Sopenharmony_ci{ 2378c2ecf20Sopenharmony_ci return crypto_register_shash(&alg); 2388c2ecf20Sopenharmony_ci} 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_cistatic void __exit md5_mod_fini(void) 2418c2ecf20Sopenharmony_ci{ 2428c2ecf20Sopenharmony_ci crypto_unregister_shash(&alg); 2438c2ecf20Sopenharmony_ci} 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_cisubsys_initcall(md5_mod_init); 2468c2ecf20Sopenharmony_cimodule_exit(md5_mod_fini); 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 2498c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MD5 Message Digest Algorithm"); 2508c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("md5"); 251