xref: /openbsd-src/usr.bin/rsync/hash.c (revision dcad62ece826198d941fee395371ef36afd22483)
1*dcad62ecSclaudio /*	$OpenBSD: hash.c,v 1.5 2024/02/27 11:28:30 claudio Exp $ */
260a32ee9Sbenno /*
360a32ee9Sbenno  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
460a32ee9Sbenno  *
560a32ee9Sbenno  * Permission to use, copy, modify, and distribute this software for any
660a32ee9Sbenno  * purpose with or without fee is hereby granted, provided that the above
760a32ee9Sbenno  * copyright notice and this permission notice appear in all copies.
860a32ee9Sbenno  *
960a32ee9Sbenno  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1060a32ee9Sbenno  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1160a32ee9Sbenno  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1260a32ee9Sbenno  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1360a32ee9Sbenno  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1460a32ee9Sbenno  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1560a32ee9Sbenno  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1660a32ee9Sbenno  */
1760a32ee9Sbenno #include <sys/types.h>
1860a32ee9Sbenno 
1960a32ee9Sbenno #include <assert.h>
2060a32ee9Sbenno #include <endian.h>
2160a32ee9Sbenno #include <stdint.h>
2260a32ee9Sbenno #include <stdlib.h>
2360a32ee9Sbenno 
2434b470cbStb #include <openssl/md4.h>
2534b470cbStb 
2660a32ee9Sbenno #include "extern.h"
2760a32ee9Sbenno 
2860a32ee9Sbenno /*
2960a32ee9Sbenno  * A fast 32-bit hash.
3060a32ee9Sbenno  * Described in Tridgell's "Efficient Algorithms for Sorting and
3160a32ee9Sbenno  * Synchronization" thesis and the "Rolling checksum" document.
3260a32ee9Sbenno  */
3360a32ee9Sbenno uint32_t
hash_fast(const void * buf,size_t len)3460a32ee9Sbenno hash_fast(const void *buf, size_t len)
3560a32ee9Sbenno {
3660a32ee9Sbenno 	size_t			 i = 0;
3760a32ee9Sbenno 	uint32_t		 a = 0, /* part of a(k, l) */
3860a32ee9Sbenno 				 b = 0; /* b(k, l) */
3960a32ee9Sbenno 	const signed char	*dat = buf;
4060a32ee9Sbenno 
4160a32ee9Sbenno 	if (len > 4)
4260a32ee9Sbenno 		for ( ; i < len - 4; i += 4) {
4360a32ee9Sbenno 			b += 4 * (a + dat[i]) +
4460a32ee9Sbenno 			    3 * dat[i + 1] +
4560a32ee9Sbenno 			    2 * dat[i + 2] +
4660a32ee9Sbenno 			    dat[i + 3];
4760a32ee9Sbenno 			a += dat[i + 0] +
4860a32ee9Sbenno 			    dat[i + 1] +
4960a32ee9Sbenno 			    dat[i + 2] +
5060a32ee9Sbenno 			    dat[i + 3];
5160a32ee9Sbenno 		}
5260a32ee9Sbenno 
5360a32ee9Sbenno 	for ( ; i < len; i++) {
5460a32ee9Sbenno 		a += dat[i];
5560a32ee9Sbenno 		b += a;
5660a32ee9Sbenno 	}
5760a32ee9Sbenno 
5860a32ee9Sbenno 	/* s(k, l) = (eps % M) + 2^16 b(k, l) % M */
5960a32ee9Sbenno 
6060a32ee9Sbenno 	return (a & 0xffff) + (b << 16);
6160a32ee9Sbenno }
6260a32ee9Sbenno 
6360a32ee9Sbenno /*
6460a32ee9Sbenno  * Slow MD4-based hash with trailing seed.
6560a32ee9Sbenno  */
6660a32ee9Sbenno void
hash_slow(const void * buf,size_t len,unsigned char * md,const struct sess * sess)6760a32ee9Sbenno hash_slow(const void *buf, size_t len,
6860a32ee9Sbenno 	unsigned char *md, const struct sess *sess)
6960a32ee9Sbenno {
7060a32ee9Sbenno 	MD4_CTX		 ctx;
7160a32ee9Sbenno 	int32_t		 seed = htole32(sess->seed);
7260a32ee9Sbenno 
7360a32ee9Sbenno 	MD4_Init(&ctx);
7460a32ee9Sbenno 	MD4_Update(&ctx, buf, len);
7560a32ee9Sbenno 	MD4_Update(&ctx, (unsigned char *)&seed, sizeof(int32_t));
7660a32ee9Sbenno 	MD4_Final(md, &ctx);
7760a32ee9Sbenno }
7860a32ee9Sbenno 
7960a32ee9Sbenno /*
8060a32ee9Sbenno  * Hash an entire file.
8160a32ee9Sbenno  * This is similar to hash_slow() except the seed is hashed at the end
8260a32ee9Sbenno  * of the sequence, not the beginning.
8360a32ee9Sbenno  */
8460a32ee9Sbenno void
hash_file_start(MD4_CTX * ctx,const struct sess * sess)85*dcad62ecSclaudio hash_file_start(MD4_CTX *ctx, const struct sess *sess)
8660a32ee9Sbenno {
8760a32ee9Sbenno 	int32_t		 seed = htole32(sess->seed);
8860a32ee9Sbenno 
89*dcad62ecSclaudio 	MD4_Init(ctx);
90*dcad62ecSclaudio 	MD4_Update(ctx, (unsigned char *)&seed, sizeof(int32_t));
91*dcad62ecSclaudio }
92*dcad62ecSclaudio 
93*dcad62ecSclaudio void
hash_file_buf(MD4_CTX * ctx,const void * buf,size_t len)94*dcad62ecSclaudio hash_file_buf(MD4_CTX *ctx, const void *buf, size_t len)
95*dcad62ecSclaudio {
96*dcad62ecSclaudio 	MD4_Update(ctx, buf, len);
97*dcad62ecSclaudio }
98*dcad62ecSclaudio void
hash_file_final(MD4_CTX * ctx,unsigned char * md)99*dcad62ecSclaudio hash_file_final(MD4_CTX *ctx, unsigned char *md)
100*dcad62ecSclaudio {
101*dcad62ecSclaudio 	MD4_Final(md, ctx);
10260a32ee9Sbenno }
103