Blame cbits/cryptonite_md5.c

Packit 141393
/*
Packit 141393
 * Copyright (C) 2006-2009 Vincent Hanquez <vincent@snarc.org>
Packit 141393
 *
Packit 141393
 * Redistribution and use in source and binary forms, with or without
Packit 141393
 * modification, are permitted provided that the following conditions
Packit 141393
 * are met:
Packit 141393
 * 1. Redistributions of source code must retain the above copyright
Packit 141393
 *    notice, this list of conditions and the following disclaimer.
Packit 141393
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 141393
 *    notice, this list of conditions and the following disclaimer in the
Packit 141393
 *    documentation and/or other materials provided with the distribution.
Packit 141393
 *
Packit 141393
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Packit 141393
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit 141393
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit 141393
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 141393
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit 141393
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 141393
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 141393
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 141393
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit 141393
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 141393
 */
Packit 141393
Packit 141393
#include <string.h>
Packit 141393
#include <stdio.h>
Packit 141393
#include "cryptonite_bitfn.h"
Packit 141393
#include "cryptonite_align.h"
Packit 141393
#include "cryptonite_md5.h"
Packit 141393
Packit 141393
void cryptonite_md5_init(struct md5_ctx *ctx)
Packit 141393
{
Packit 141393
	memset(ctx, 0, sizeof(*ctx));
Packit 141393
Packit 141393
	ctx->sz = 0ULL;
Packit 141393
	ctx->h[0] = 0x67452301;
Packit 141393
	ctx->h[1] = 0xefcdab89;
Packit 141393
	ctx->h[2] = 0x98badcfe;
Packit 141393
	ctx->h[3] = 0x10325476;
Packit 141393
}
Packit 141393
Packit 141393
#define f1(x, y, z)	(z ^ (x & (y ^ z)))
Packit 141393
#define f2(x, y, z)	f1(z, x, y)
Packit 141393
#define f3(x, y, z)	(x ^ y ^ z)
Packit 141393
#define f4(x, y, z)	(y ^ (x | ~z))
Packit 141393
#define R(f, a, b, c, d, i, k, s) a += f(b, c, d) + w[i] + k; a = rol32(a, s); a += b
Packit 141393
Packit 141393
static void md5_do_chunk(struct md5_ctx *ctx, uint32_t *buf)
Packit 141393
{
Packit 141393
	uint32_t a, b, c, d;
Packit 141393
#ifdef ARCH_IS_BIG_ENDIAN
Packit 141393
	uint32_t w[16];
Packit 141393
	cpu_to_le32_array(w, buf, 16);
Packit 141393
#else
Packit 141393
	uint32_t *w = buf;
Packit 141393
#endif
Packit 141393
	a = ctx->h[0]; b = ctx->h[1]; c = ctx->h[2]; d = ctx->h[3];
Packit 141393
Packit 141393
	R(f1, a, b, c, d, 0, 0xd76aa478, 7);
Packit 141393
	R(f1, d, a, b, c, 1, 0xe8c7b756, 12);
Packit 141393
	R(f1, c, d, a, b, 2, 0x242070db, 17);
Packit 141393
	R(f1, b, c, d, a, 3, 0xc1bdceee, 22);
Packit 141393
	R(f1, a, b, c, d, 4, 0xf57c0faf, 7);
Packit 141393
	R(f1, d, a, b, c, 5, 0x4787c62a, 12);
Packit 141393
	R(f1, c, d, a, b, 6, 0xa8304613, 17);
Packit 141393
	R(f1, b, c, d, a, 7, 0xfd469501, 22);
Packit 141393
	R(f1, a, b, c, d, 8, 0x698098d8, 7);
Packit 141393
	R(f1, d, a, b, c, 9, 0x8b44f7af, 12);
Packit 141393
	R(f1, c, d, a, b, 10, 0xffff5bb1, 17);
Packit 141393
	R(f1, b, c, d, a, 11, 0x895cd7be, 22);
Packit 141393
	R(f1, a, b, c, d, 12, 0x6b901122, 7);
Packit 141393
	R(f1, d, a, b, c, 13, 0xfd987193, 12);
Packit 141393
	R(f1, c, d, a, b, 14, 0xa679438e, 17);
Packit 141393
	R(f1, b, c, d, a, 15, 0x49b40821, 22);
Packit 141393
Packit 141393
	R(f2, a, b, c, d, 1, 0xf61e2562, 5);
Packit 141393
	R(f2, d, a, b, c, 6, 0xc040b340, 9);
Packit 141393
	R(f2, c, d, a, b, 11, 0x265e5a51, 14);
Packit 141393
	R(f2, b, c, d, a, 0, 0xe9b6c7aa, 20);
Packit 141393
	R(f2, a, b, c, d, 5, 0xd62f105d, 5);
Packit 141393
	R(f2, d, a, b, c, 10, 0x02441453, 9);
Packit 141393
	R(f2, c, d, a, b, 15, 0xd8a1e681, 14);
Packit 141393
	R(f2, b, c, d, a, 4, 0xe7d3fbc8, 20);
Packit 141393
	R(f2, a, b, c, d, 9, 0x21e1cde6, 5);
Packit 141393
	R(f2, d, a, b, c, 14, 0xc33707d6, 9);
Packit 141393
	R(f2, c, d, a, b, 3, 0xf4d50d87, 14);
Packit 141393
	R(f2, b, c, d, a, 8, 0x455a14ed, 20);
Packit 141393
	R(f2, a, b, c, d, 13, 0xa9e3e905, 5);
Packit 141393
	R(f2, d, a, b, c, 2, 0xfcefa3f8, 9);
Packit 141393
	R(f2, c, d, a, b, 7, 0x676f02d9, 14);
Packit 141393
	R(f2, b, c, d, a, 12, 0x8d2a4c8a, 20);
Packit 141393
Packit 141393
	R(f3, a, b, c, d, 5, 0xfffa3942, 4);
Packit 141393
	R(f3, d, a, b, c, 8, 0x8771f681, 11);
Packit 141393
	R(f3, c, d, a, b, 11, 0x6d9d6122, 16);
Packit 141393
	R(f3, b, c, d, a, 14, 0xfde5380c, 23);
Packit 141393
	R(f3, a, b, c, d, 1, 0xa4beea44, 4);
Packit 141393
	R(f3, d, a, b, c, 4, 0x4bdecfa9, 11);
Packit 141393
	R(f3, c, d, a, b, 7, 0xf6bb4b60, 16);
Packit 141393
	R(f3, b, c, d, a, 10, 0xbebfbc70, 23);
Packit 141393
	R(f3, a, b, c, d, 13, 0x289b7ec6, 4);
Packit 141393
	R(f3, d, a, b, c, 0, 0xeaa127fa, 11);
Packit 141393
	R(f3, c, d, a, b, 3, 0xd4ef3085, 16);
Packit 141393
	R(f3, b, c, d, a, 6, 0x04881d05, 23);
Packit 141393
	R(f3, a, b, c, d, 9, 0xd9d4d039, 4);
Packit 141393
	R(f3, d, a, b, c, 12, 0xe6db99e5, 11);
Packit 141393
	R(f3, c, d, a, b, 15, 0x1fa27cf8, 16);
Packit 141393
	R(f3, b, c, d, a, 2, 0xc4ac5665, 23);
Packit 141393
Packit 141393
	R(f4, a, b, c, d, 0, 0xf4292244, 6);
Packit 141393
	R(f4, d, a, b, c, 7, 0x432aff97, 10);
Packit 141393
	R(f4, c, d, a, b, 14, 0xab9423a7, 15);
Packit 141393
	R(f4, b, c, d, a, 5, 0xfc93a039, 21);
Packit 141393
	R(f4, a, b, c, d, 12, 0x655b59c3, 6);
Packit 141393
	R(f4, d, a, b, c, 3, 0x8f0ccc92, 10);
Packit 141393
	R(f4, c, d, a, b, 10, 0xffeff47d, 15);
Packit 141393
	R(f4, b, c, d, a, 1, 0x85845dd1, 21);
Packit 141393
	R(f4, a, b, c, d, 8, 0x6fa87e4f, 6);
Packit 141393
	R(f4, d, a, b, c, 15, 0xfe2ce6e0, 10);
Packit 141393
	R(f4, c, d, a, b, 6, 0xa3014314, 15);
Packit 141393
	R(f4, b, c, d, a, 13, 0x4e0811a1, 21);
Packit 141393
	R(f4, a, b, c, d, 4, 0xf7537e82, 6);
Packit 141393
	R(f4, d, a, b, c, 11, 0xbd3af235, 10);
Packit 141393
	R(f4, c, d, a, b, 2, 0x2ad7d2bb, 15);
Packit 141393
	R(f4, b, c, d, a, 9, 0xeb86d391, 21);
Packit 141393
Packit 141393
	ctx->h[0] += a; ctx->h[1] += b; ctx->h[2] += c; ctx->h[3] += d;
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_md5_update(struct md5_ctx *ctx, const uint8_t *data, uint32_t len)
Packit 141393
{
Packit 141393
	uint32_t index, to_fill;
Packit 141393
Packit 141393
	index = (uint32_t) (ctx->sz & 0x3f);
Packit 141393
	to_fill = 64 - index;
Packit 141393
Packit 141393
	ctx->sz += len;
Packit 141393
Packit 141393
	if (index && len >= to_fill) {
Packit 141393
		memcpy(ctx->buf + index, data, to_fill);
Packit 141393
		md5_do_chunk(ctx, (uint32_t *) ctx->buf);
Packit 141393
		len -= to_fill;
Packit 141393
		data += to_fill;
Packit 141393
		index = 0;
Packit 141393
	}
Packit 141393
Packit 141393
	if (need_alignment(data, 4)) {
Packit 141393
		uint32_t tramp[16];
Packit 141393
		ASSERT_ALIGNMENT(tramp, 4);
Packit 141393
		for (; len >= 64; len -= 64, data += 64) {
Packit 141393
			memcpy(tramp, data, 64);
Packit 141393
			md5_do_chunk(ctx, tramp);
Packit 141393
		}
Packit 141393
	} else {
Packit 141393
		/* process as much 64-block as possible */
Packit 141393
		for (; len >= 64; len -= 64, data += 64)
Packit 141393
			md5_do_chunk(ctx, (uint32_t *) data);
Packit 141393
	}
Packit 141393
Packit 141393
	/* append data into buf */
Packit 141393
	if (len)
Packit 141393
		memcpy(ctx->buf + index, data, len);
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_md5_finalize(struct md5_ctx *ctx, uint8_t *out)
Packit 141393
{
Packit 141393
	static uint8_t padding[64] = { 0x80, };
Packit 141393
	uint64_t bits;
Packit 141393
	uint32_t index, padlen;
Packit 141393
Packit 141393
	/* add padding and update data with it */
Packit 141393
	bits = cpu_to_le64(ctx->sz << 3);
Packit 141393
Packit 141393
	/* pad out to 56 */
Packit 141393
	index = (uint32_t) (ctx->sz & 0x3f);
Packit 141393
	padlen = (index < 56) ? (56 - index) : ((64 + 56) - index);
Packit 141393
	cryptonite_md5_update(ctx, padding, padlen);
Packit 141393
Packit 141393
	/* append length */
Packit 141393
	cryptonite_md5_update(ctx, (uint8_t *) &bits, sizeof(bits));
Packit 141393
Packit 141393
	/* output hash */
Packit 141393
	store_le32(out   , ctx->h[0]);
Packit 141393
	store_le32(out+ 4, ctx->h[1]);
Packit 141393
	store_le32(out+ 8, ctx->h[2]);
Packit 141393
	store_le32(out+12, ctx->h[3]);
Packit 141393
}