0030-rsync-Add-implementation-of-MD4.patch (11896B)
1 From 01657f2c1445cf4874337a0966a76f24ba9c9536 Mon Sep 17 00:00:00 2001 2 From: Michael Forney <mforney@mforney.org> 3 Date: Wed, 15 Apr 2020 22:10:06 -0700 4 Subject: [PATCH] rsync: Add implementation of MD4 5 6 --- 7 usr.bin/rsync/Makefile | 2 +- 8 usr.bin/rsync/blocks.c | 2 +- 9 usr.bin/rsync/downloader.c | 2 +- 10 usr.bin/rsync/hash.c | 2 +- 11 usr.bin/rsync/md4.c | 266 +++++++++++++++++++++++++++++++++++++ 12 usr.bin/rsync/md4.h | 47 +++++++ 13 usr.bin/rsync/sender.c | 2 +- 14 7 files changed, 318 insertions(+), 5 deletions(-) 15 create mode 100644 usr.bin/rsync/md4.c 16 create mode 100644 usr.bin/rsync/md4.h 17 18 diff --git a/usr.bin/rsync/Makefile b/usr.bin/rsync/Makefile 19 index 3c60f18e07f..172045ce7ac 100644 20 --- a/usr.bin/rsync/Makefile 21 +++ b/usr.bin/rsync/Makefile 22 @@ -2,7 +2,7 @@ 23 24 PROG= openrsync 25 SRCS= blocks.c client.c copy.c downloader.c fargs.c flist.c hash.c ids.c \ 26 - io.c log.c main.c misc.c mkpath.c mktemp.c receiver.c rmatch.c \ 27 + io.c log.c main.c md4.c misc.c mkpath.c mktemp.c receiver.c rmatch.c \ 28 rules.c sender.c server.c session.c socket.c symlinks.c uploader.c 29 LDADD+= -lcrypto -lm -lutil 30 DPADD+= ${LIBCRYPTO} ${LIBM} ${LIBUTIL} 31 diff --git a/usr.bin/rsync/blocks.c b/usr.bin/rsync/blocks.c 32 index 906733c968e..0a8c3f485d1 100644 33 --- a/usr.bin/rsync/blocks.c 34 +++ b/usr.bin/rsync/blocks.c 35 @@ -26,7 +26,7 @@ 36 #include <string.h> 37 #include <unistd.h> 38 39 -#include <openssl/md4.h> 40 +#include "md4.h" 41 42 #include "extern.h" 43 44 diff --git a/usr.bin/rsync/downloader.c b/usr.bin/rsync/downloader.c 45 index 07ec334f6b4..6543851fd2c 100644 46 --- a/usr.bin/rsync/downloader.c 47 +++ b/usr.bin/rsync/downloader.c 48 @@ -28,7 +28,7 @@ 49 #include <time.h> 50 #include <unistd.h> 51 52 -#include <openssl/md4.h> 53 +#include "md4.h" 54 55 #include "extern.h" 56 57 diff --git a/usr.bin/rsync/hash.c b/usr.bin/rsync/hash.c 58 index b87c56f527c..44ae0d26282 100644 59 --- a/usr.bin/rsync/hash.c 60 +++ b/usr.bin/rsync/hash.c 61 @@ -21,7 +21,7 @@ 62 #include <stdint.h> 63 #include <stdlib.h> 64 65 -#include <openssl/md4.h> 66 +#include "md4.h" 67 68 #include "extern.h" 69 70 diff --git a/usr.bin/rsync/md4.c b/usr.bin/rsync/md4.c 71 new file mode 100644 72 index 00000000000..528f985563f 73 --- /dev/null 74 +++ b/usr.bin/rsync/md4.c 75 @@ -0,0 +1,266 @@ 76 +/* 77 + * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. 78 + * MD4 Message-Digest Algorithm (RFC 1320). 79 + * 80 + * Homepage: 81 + * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md4 82 + * 83 + * Author: 84 + * Alexander Peslyak, better known as Solar Designer <solar at openwall.com> 85 + * 86 + * This software was written by Alexander Peslyak in 2001. No copyright is 87 + * claimed, and the software is hereby placed in the public domain. 88 + * In case this attempt to disclaim copyright and place the software in the 89 + * public domain is deemed null and void, then the software is 90 + * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the 91 + * general public under the following terms: 92 + * 93 + * Redistribution and use in source and binary forms, with or without 94 + * modification, are permitted. 95 + * 96 + * There's ABSOLUTELY NO WARRANTY, express or implied. 97 + * 98 + * (This is a heavily cut-down "BSD license".) 99 + * 100 + * This differs from Colin Plumb's older public domain implementation in that 101 + * no exactly 32-bit integer data type is required (any 32-bit or wider 102 + * unsigned integer data type will do), there's no compile-time endianness 103 + * configuration, and the function prototypes match OpenSSL's. No code from 104 + * Colin Plumb's implementation has been reused; this comment merely compares 105 + * the properties of the two independent implementations. 106 + * 107 + * The primary goals of this implementation are portability and ease of use. 108 + * It is meant to be fast, but not as fast as possible. Some known 109 + * optimizations are not included to reduce source code size and avoid 110 + * compile-time configuration. 111 + */ 112 + 113 +#include <string.h> 114 + 115 +#include "md4.h" 116 + 117 +/* 118 + * The basic MD4 functions. 119 + * 120 + * F and G are optimized compared to their RFC 1320 definitions, with the 121 + * optimization for F borrowed from Colin Plumb's MD5 implementation. 122 + */ 123 +#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) 124 +#define G(x, y, z) (((x) & ((y) | (z))) | ((y) & (z))) 125 +#define H(x, y, z) ((x) ^ (y) ^ (z)) 126 + 127 +/* 128 + * The MD4 transformation for all three rounds. 129 + */ 130 +#define STEP(f, a, b, c, d, x, s) \ 131 + (a) += f((b), (c), (d)) + (x); \ 132 + (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); 133 + 134 +/* 135 + * SET reads 4 input bytes in little-endian byte order and stores them in a 136 + * properly aligned word in host byte order. 137 + * 138 + * The check for little-endian architectures that tolerate unaligned memory 139 + * accesses is just an optimization. Nothing will break if it fails to detect 140 + * a suitable architecture. 141 + * 142 + * Unfortunately, this optimization may be a C strict aliasing rules violation 143 + * if the caller's data buffer has effective type that cannot be aliased by 144 + * MD4_u32plus. In practice, this problem may occur if these MD4 routines are 145 + * inlined into a calling function, or with future and dangerously advanced 146 + * link-time optimizations. For the time being, keeping these MD4 routines in 147 + * their own translation unit avoids the problem. 148 + */ 149 +#if defined(__i386__) || defined(__x86_64__) || defined(__vax__) 150 +#define SET(n) \ 151 + (*(MD4_u32plus *)&ptr[(n) * 4]) 152 +#define GET(n) \ 153 + SET(n) 154 +#else 155 +#define SET(n) \ 156 + (ctx->block[(n)] = \ 157 + (MD4_u32plus)ptr[(n) * 4] | \ 158 + ((MD4_u32plus)ptr[(n) * 4 + 1] << 8) | \ 159 + ((MD4_u32plus)ptr[(n) * 4 + 2] << 16) | \ 160 + ((MD4_u32plus)ptr[(n) * 4 + 3] << 24)) 161 +#define GET(n) \ 162 + (ctx->block[(n)]) 163 +#endif 164 + 165 +/* 166 + * This processes one or more 64-byte data blocks, but does NOT update the bit 167 + * counters. There are no alignment requirements. 168 + */ 169 +static const void *body(MD4_CTX *ctx, const void *data, unsigned long size) 170 +{ 171 + const unsigned char *ptr; 172 + MD4_u32plus a, b, c, d; 173 + MD4_u32plus saved_a, saved_b, saved_c, saved_d; 174 + const MD4_u32plus ac1 = 0x5a827999, ac2 = 0x6ed9eba1; 175 + 176 + ptr = (const unsigned char *)data; 177 + 178 + a = ctx->a; 179 + b = ctx->b; 180 + c = ctx->c; 181 + d = ctx->d; 182 + 183 + do { 184 + saved_a = a; 185 + saved_b = b; 186 + saved_c = c; 187 + saved_d = d; 188 + 189 +/* Round 1 */ 190 + STEP(F, a, b, c, d, SET(0), 3) 191 + STEP(F, d, a, b, c, SET(1), 7) 192 + STEP(F, c, d, a, b, SET(2), 11) 193 + STEP(F, b, c, d, a, SET(3), 19) 194 + STEP(F, a, b, c, d, SET(4), 3) 195 + STEP(F, d, a, b, c, SET(5), 7) 196 + STEP(F, c, d, a, b, SET(6), 11) 197 + STEP(F, b, c, d, a, SET(7), 19) 198 + STEP(F, a, b, c, d, SET(8), 3) 199 + STEP(F, d, a, b, c, SET(9), 7) 200 + STEP(F, c, d, a, b, SET(10), 11) 201 + STEP(F, b, c, d, a, SET(11), 19) 202 + STEP(F, a, b, c, d, SET(12), 3) 203 + STEP(F, d, a, b, c, SET(13), 7) 204 + STEP(F, c, d, a, b, SET(14), 11) 205 + STEP(F, b, c, d, a, SET(15), 19) 206 + 207 +/* Round 2 */ 208 + STEP(G, a, b, c, d, GET(0) + ac1, 3) 209 + STEP(G, d, a, b, c, GET(4) + ac1, 5) 210 + STEP(G, c, d, a, b, GET(8) + ac1, 9) 211 + STEP(G, b, c, d, a, GET(12) + ac1, 13) 212 + STEP(G, a, b, c, d, GET(1) + ac1, 3) 213 + STEP(G, d, a, b, c, GET(5) + ac1, 5) 214 + STEP(G, c, d, a, b, GET(9) + ac1, 9) 215 + STEP(G, b, c, d, a, GET(13) + ac1, 13) 216 + STEP(G, a, b, c, d, GET(2) + ac1, 3) 217 + STEP(G, d, a, b, c, GET(6) + ac1, 5) 218 + STEP(G, c, d, a, b, GET(10) + ac1, 9) 219 + STEP(G, b, c, d, a, GET(14) + ac1, 13) 220 + STEP(G, a, b, c, d, GET(3) + ac1, 3) 221 + STEP(G, d, a, b, c, GET(7) + ac1, 5) 222 + STEP(G, c, d, a, b, GET(11) + ac1, 9) 223 + STEP(G, b, c, d, a, GET(15) + ac1, 13) 224 + 225 +/* Round 3 */ 226 + STEP(H, a, b, c, d, GET(0) + ac2, 3) 227 + STEP(H, d, a, b, c, GET(8) + ac2, 9) 228 + STEP(H, c, d, a, b, GET(4) + ac2, 11) 229 + STEP(H, b, c, d, a, GET(12) + ac2, 15) 230 + STEP(H, a, b, c, d, GET(2) + ac2, 3) 231 + STEP(H, d, a, b, c, GET(10) + ac2, 9) 232 + STEP(H, c, d, a, b, GET(6) + ac2, 11) 233 + STEP(H, b, c, d, a, GET(14) + ac2, 15) 234 + STEP(H, a, b, c, d, GET(1) + ac2, 3) 235 + STEP(H, d, a, b, c, GET(9) + ac2, 9) 236 + STEP(H, c, d, a, b, GET(5) + ac2, 11) 237 + STEP(H, b, c, d, a, GET(13) + ac2, 15) 238 + STEP(H, a, b, c, d, GET(3) + ac2, 3) 239 + STEP(H, d, a, b, c, GET(11) + ac2, 9) 240 + STEP(H, c, d, a, b, GET(7) + ac2, 11) 241 + STEP(H, b, c, d, a, GET(15) + ac2, 15) 242 + 243 + a += saved_a; 244 + b += saved_b; 245 + c += saved_c; 246 + d += saved_d; 247 + 248 + ptr += 64; 249 + } while (size -= 64); 250 + 251 + ctx->a = a; 252 + ctx->b = b; 253 + ctx->c = c; 254 + ctx->d = d; 255 + 256 + return ptr; 257 +} 258 + 259 +void MD4_Init(MD4_CTX *ctx) 260 +{ 261 + ctx->a = 0x67452301; 262 + ctx->b = 0xefcdab89; 263 + ctx->c = 0x98badcfe; 264 + ctx->d = 0x10325476; 265 + 266 + ctx->lo = 0; 267 + ctx->hi = 0; 268 +} 269 + 270 +void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size) 271 +{ 272 + MD4_u32plus saved_lo; 273 + unsigned long used, available; 274 + 275 + saved_lo = ctx->lo; 276 + if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) 277 + ctx->hi++; 278 + ctx->hi += size >> 29; 279 + 280 + used = saved_lo & 0x3f; 281 + 282 + if (used) { 283 + available = 64 - used; 284 + 285 + if (size < available) { 286 + memcpy(&ctx->buffer[used], data, size); 287 + return; 288 + } 289 + 290 + memcpy(&ctx->buffer[used], data, available); 291 + data = (const unsigned char *)data + available; 292 + size -= available; 293 + body(ctx, ctx->buffer, 64); 294 + } 295 + 296 + if (size >= 64) { 297 + data = body(ctx, data, size & ~(unsigned long)0x3f); 298 + size &= 0x3f; 299 + } 300 + 301 + memcpy(ctx->buffer, data, size); 302 +} 303 + 304 +#define OUT(dst, src) \ 305 + (dst)[0] = (unsigned char)(src); \ 306 + (dst)[1] = (unsigned char)((src) >> 8); \ 307 + (dst)[2] = (unsigned char)((src) >> 16); \ 308 + (dst)[3] = (unsigned char)((src) >> 24); 309 + 310 +void MD4_Final(unsigned char *result, MD4_CTX *ctx) 311 +{ 312 + unsigned long used, available; 313 + 314 + used = ctx->lo & 0x3f; 315 + 316 + ctx->buffer[used++] = 0x80; 317 + 318 + available = 64 - used; 319 + 320 + if (available < 8) { 321 + memset(&ctx->buffer[used], 0, available); 322 + body(ctx, ctx->buffer, 64); 323 + used = 0; 324 + available = 64; 325 + } 326 + 327 + memset(&ctx->buffer[used], 0, available - 8); 328 + 329 + ctx->lo <<= 3; 330 + OUT(&ctx->buffer[56], ctx->lo) 331 + OUT(&ctx->buffer[60], ctx->hi) 332 + 333 + body(ctx, ctx->buffer, 64); 334 + 335 + OUT(&result[0], ctx->a) 336 + OUT(&result[4], ctx->b) 337 + OUT(&result[8], ctx->c) 338 + OUT(&result[12], ctx->d) 339 + 340 + memset(ctx, 0, sizeof(*ctx)); 341 +} 342 diff --git a/usr.bin/rsync/md4.h b/usr.bin/rsync/md4.h 343 new file mode 100644 344 index 00000000000..ebf5bb555a0 345 --- /dev/null 346 +++ b/usr.bin/rsync/md4.h 347 @@ -0,0 +1,47 @@ 348 +/* 349 + * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. 350 + * MD4 Message-Digest Algorithm (RFC 1320). 351 + * 352 + * Homepage: 353 + * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md4 354 + * 355 + * Author: 356 + * Alexander Peslyak, better known as Solar Designer <solar at openwall.com> 357 + * 358 + * This software was written by Alexander Peslyak in 2001. No copyright is 359 + * claimed, and the software is hereby placed in the public domain. 360 + * In case this attempt to disclaim copyright and place the software in the 361 + * public domain is deemed null and void, then the software is 362 + * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the 363 + * general public under the following terms: 364 + * 365 + * Redistribution and use in source and binary forms, with or without 366 + * modification, are permitted. 367 + * 368 + * There's ABSOLUTELY NO WARRANTY, express or implied. 369 + * 370 + * See md4.c for more information. 371 + */ 372 + 373 +#ifndef _MD4_H 374 +#define _MD4_H 375 + 376 +#include <stdint.h> 377 + 378 +#define MD4_DIGEST_LENGTH 16 379 + 380 +/* Any 32-bit or wider unsigned integer data type will do */ 381 +typedef uint_fast32_t MD4_u32plus; 382 + 383 +typedef struct { 384 + MD4_u32plus lo, hi; 385 + MD4_u32plus a, b, c, d; 386 + unsigned char buffer[64]; 387 + MD4_u32plus block[16]; 388 +} MD4_CTX; 389 + 390 +extern void MD4_Init(MD4_CTX *ctx); 391 +extern void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size); 392 +extern void MD4_Final(unsigned char *result, MD4_CTX *ctx); 393 + 394 +#endif 395 diff --git a/usr.bin/rsync/sender.c b/usr.bin/rsync/sender.c 396 index 9dd008def01..2aeb99b64a0 100644 397 --- a/usr.bin/rsync/sender.c 398 +++ b/usr.bin/rsync/sender.c 399 @@ -26,7 +26,7 @@ 400 #include <string.h> 401 #include <unistd.h> 402 403 -#include <openssl/md4.h> 404 +#include "md4.h" 405 406 #include "extern.h" 407 408 -- 409 2.35.1 410