add linux support to mtree fetch sha256 and indexinfo
[ravenports.git] / bucket_B3 / sha256
1 # Buildsheet autogenerated by ravenadm tool -- Do not edit.
2
3 NAMEBASE=               sha256
4 VERSION=                1.0
5 KEYWORDS=               security
6 VARIANTS=               standard
7 SDESC[standard]=        MD5 and SHA-256 digest tools from DragonFly
8 HOMEPAGE=               none
9 CONTACT=                John_Marino[draco@marino.st]
10
11 DOWNLOAD_GROUPS=        none
12 SPKGS[standard]=        single
13
14 OPTIONS_AVAILABLE=      none
15 OPTIONS_STANDARD=       none
16
17 BUILD_DEPENDS=          libressl:single:static
18
19 do-extract:
20         ${MKDIR} ${WRKSRC}
21         cp ${FILESDIR}/* ${WRKSRC}/
22
23 [FILE:322:descriptions/desc.single]
24 The md5 and sha256 programs are command-line utilities originating with
25 FreeBSD.  Their purpose is to generate message digests.
26
27 The core hash routines were implemented by Colin Percival based on the
28 published FIPS 180-2 standard.  Matt Dillon modified them to use
29 OpenSSL/LibreSSL routines to avoid conflicts with libmd.
30
31
32 [FILE:18:manifests/plist.single]
33 bin/
34  md5
35  sha256
36
37
38 [FILE:887:files/Makefile]
39 # $FreeBSD: src/lib/libmd/Makefile,v 1.42 2005/03/09 19:23:04 cperciva Exp $
40
41 SRCS=           md5c.c md5hl.c sha256hl.c
42 OBJS=           ${SRCS:.c=.o}
43 PREFIX?=        /usr/local
44 LIBS=           ${PREFIX}/lib/libcrypto.a
45 CFLAGS=         -I${PREFIX}/include -I. 
46
47 .if "${OPSYS}" == "Linux"
48 LIBS+=          ${PREFIX}/lib/libbsd.a
49 .endif
50
51 all:    program
52
53 ${OBJS}: ${.TARGET:.o=.c}
54         ${CC} -c ${.ALLSRC} ${CFLAGS}
55
56 md5hl.c: mdXhl.c
57         (echo '#define LENGTH 16'; \
58                 sed -e 's/mdX/md5/g' -e 's/MDX/MD5/g' ${.ALLSRC}) > ${.TARGET}
59
60 sha256hl.c: mdXhl.c
61         (echo '#define LENGTH 32'; \
62                 sed -e 's/mdX/sha256/g' -e 's/MDX/SHA256_/g'    \
63                         -e  's/SHA256__/SHA256_/g' \
64                 ${.ALLSRC}) > ${.TARGET}
65
66 program: ${OBJS} main.c
67         ${CC} -o md5 main.c ${OBJS} ${CFLAGS} ${LIBS}
68         ln md5 sha256
69
70 install:
71         ${BSD_INSTALL_PROGRAM} md5 ${DESTDIR}${PREFIX}/bin/
72         ${BSD_INSTALL_PROGRAM} sha256 ${DESTDIR}${PREFIX}/bin/
73
74 clean:
75         rm -f md5hl.c sha256hl.c md5 sha256 ${OBJS}
76
77
78 [FILE:11594:files/main.c]
79 /*
80  * Derived from:
81  *
82  * MDDRIVER.C - test driver for MD2, MD4 and MD5
83  *
84  * $FreeBSD: src/sbin/md5/md5.c,v 1.35 2006/01/17 15:35:57 phk Exp $
85  */
86
87 /*
88  *  Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
89  *  rights reserved.
90  *
91  *  RSA Data Security, Inc. makes no representations concerning either
92  *  the merchantability of this software or the suitability of this
93  *  software for any particular purpose. It is provided "as is"
94  *  without express or implied warranty of any kind.
95  *
96  *  These notices must be retained in any copies of any part of this
97  *  documentation and/or software.
98  */
99
100 #include <fcntl.h>
101 #include <sys/types.h>
102 #include <sys/stat.h>
103 #include <sys/time.h>
104 #include <sys/resource.h>
105 #include <err.h>
106 #include <sys/mman.h>
107 #include <stdio.h>
108 #include <stdlib.h>
109 #include <string.h>
110 #include <time.h>
111 #include <unistd.h>
112 #include <sysexits.h>
113
114 #ifdef __linux__
115 #include <bsd/stdlib.h>
116 #endif /* __linux__ */
117
118 #include "md5.h"
119 #include "sha256.h"
120
121 /*
122  * Length of test block, number of test blocks.
123  */
124 #define TEST_BLOCK_LEN 10000
125 #define TEST_BLOCK_COUNT 100000
126 #define MDTESTCOUNT 8
127
128 int qflag;
129 int rflag;
130 int sflag;
131
132 typedef void (DIGEST_Init)(void *);
133 typedef void (DIGEST_Update)(void *, const unsigned char *, size_t);
134 typedef char *(DIGEST_End)(void *, char *);
135
136 extern const char *MD5TestOutput[MDTESTCOUNT];
137 extern const char *SHA256_TestOutput[MDTESTCOUNT];
138
139 typedef struct Algorithm_t {
140         const char *progname;
141         const char *name;
142         const char *(*TestOutput)[MDTESTCOUNT];
143         DIGEST_Init *Init;
144         DIGEST_Update *Update;
145         DIGEST_End *End;
146         char *(*Data)(const void *, unsigned int, char *);
147         char *(*File)(const char *, char *);
148 } Algorithm_t;
149
150 static void MDString(Algorithm_t *, const char *);
151 static void MDTimeTrial(Algorithm_t *);
152 static void MDTestSuite(Algorithm_t *);
153 static void MDFilter(Algorithm_t *, int);
154 static void usage(int excode);
155
156 typedef union {
157         MD5_CTX md5;
158         SHA256_CTX sha256;
159 } DIGEST_CTX;
160
161 /* max(MD5_DIGEST_LENGTH, SHA_DIGEST_LENGTH, 
162         SHA256_DIGEST_LENGTH, RIPEMD160_DIGEST_LENGTH)*2+1 */
163 #define HEX_DIGEST_LENGTH 65
164
165 /* algorithm function table */
166
167 struct Algorithm_t Algorithm[] = {
168         { "md5", "MD5", &MD5TestOutput, (DIGEST_Init*)&MD5Init,
169                 (DIGEST_Update*)&MD5Update, (DIGEST_End*)&MD5End,
170                 &MD5Data, &MD5File },
171         { "sha256", "SHA256", &SHA256_TestOutput, (DIGEST_Init*)&SHA256_Init,
172                 (DIGEST_Update*)&SHA256_Update, (DIGEST_End*)&SHA256_End,
173                 &SHA256_Data, &SHA256_File }
174 };
175
176 /*
177  * There is no need to use a huge mmap, just pick something
178  * reasonable.
179  */
180 #define MAXMMAP (32*1024*1024)
181
182 static char *
183 digestfile(const char *fname, char *buf, const Algorithm_t *alg,
184     off_t *beginp, off_t *endp)
185 {
186         int              fd;
187         struct stat      st;
188         size_t           size;
189         char            *result = NULL;
190         void            *map;
191         DIGEST_CTX       context;
192         off_t            end = *endp, begin = *beginp;
193         size_t           pagesize;
194
195         fd = open(fname, O_RDONLY);
196         if (fd == -1) {
197                 warn("can't open %s", fname);
198                 return NULL;
199         }
200
201         if (fstat(fd, &st) == -1) {
202                 warn("can't fstat %s after opening", fname);
203                 goto cleanup;
204         }
205
206         /* Non-positive end means, it has to be counted from the back:  */
207         if (end <= 0)
208                 end += st.st_size;
209         /* Negative begin means, it has to be counted from the back:    */
210         if (begin < 0)
211                 begin += st.st_size;
212
213         if (begin < 0 || end < 0 || begin > st.st_size || end > st.st_size) {
214                 warnx("%s is %jd bytes long, not large enough for the "
215                     "specified offsets [%jd-%jd]", fname,
216                     (intmax_t)st.st_size,
217                     (intmax_t)*beginp, (intmax_t)*endp);
218                 goto cleanup;
219         }
220         if (begin > end) {
221                 warnx("%s is %jd bytes long. Begin-offset %jd (%jd) is "
222                     "larger than end-offset %jd (%jd)",
223                     fname, (intmax_t)st.st_size,
224                     (intmax_t)begin, (intmax_t)*beginp,
225                     (intmax_t)end, (intmax_t)*endp);
226                 goto cleanup;
227         }
228
229         if (*endp <= 0)
230                 *endp = end;
231         if (*beginp < 0)
232                 *beginp = begin;
233
234         pagesize = getpagesize();
235
236         alg->Init(&context);
237
238         do {
239                 if (end - begin > MAXMMAP)
240                         size = MAXMMAP;
241                 else
242                         size = end - begin;
243
244 #ifdef __linux__
245                 map = mmap(NULL, size, PROT_READ, 0, fd, begin);
246 #else
247                 map = mmap(NULL, size, PROT_READ, MAP_NOCORE, fd, begin);
248 #endif
249                 if (map == MAP_FAILED) {
250                         warn("mmaping of %s between %jd and %jd ",
251                             fname, (intmax_t)begin, (intmax_t)begin + size);
252                         goto cleanup;
253                 }
254                 /*
255                  * Try to give kernel a hint. Not that it
256                  * cares at the time of this writing :-(
257                  */
258                 if (size > pagesize)
259                         madvise(map, size, MADV_SEQUENTIAL);
260                 alg->Update(&context, map, size);
261                 munmap(map, size);
262                 begin += size;
263         } while (begin < end);
264
265         result = alg->End(&context, buf);
266
267 cleanup:
268         close(fd);
269         return result;
270 }
271
272 static off_t
273 parseint(const char *arg)
274 {
275         double   result; /* Use double to allow things like 0.5Kb */
276         char    *endp;
277
278         result = strtod(arg, &endp);
279         switch (endp[0]) {
280         case 'T':
281         case 't':
282                 result *= 1024; /* FALLTHROUGH */
283         case 'M':
284         case 'm':
285                 result *= 1024; /* FALLTHROUGH */
286         case 'K':
287         case 'k':
288                 endp++;
289                 if (endp[1] == 'b' || endp[1] == 'B')
290                         endp++;
291                 result *= 1024; /* FALLTHROUGH */
292         case '\0':
293                 break;
294         default:
295                 warnx("%c (%d): unrecognized suffix", endp[0], (int)endp[0]);
296                 goto badnumber;
297         }
298
299         if (endp[0] == '\0')
300                 return result;
301
302 badnumber:
303         errx(EX_USAGE, "`%s' is not a valid offset.", arg);
304 }
305
306 /* Main driver.
307
308 Arguments (may be any combination):
309   -sstring - digests string
310   -t       - runs time trial
311   -x       - runs test script
312   filename - digests file
313   (none)   - digests standard input
314  */
315 int
316 main(int argc, char *argv[])
317 {
318         int     ch;
319         char   *p;
320         char    buf[HEX_DIGEST_LENGTH];
321         int     failed, useoffsets = 0;
322         off_t   begin = 0, end = 0; /* To shut compiler warning */
323         unsigned        digest;
324         const char*     progname;
325  
326         if ((progname = strrchr(argv[0], '/')) == NULL)
327                 progname = argv[0];
328         else
329                 progname++;
330  
331         for (digest = 0; digest < sizeof(Algorithm)/sizeof(*Algorithm); digest++)
332                 if (strcasecmp(Algorithm[digest].progname, progname) == 0)
333                         break;
334  
335         if (digest == sizeof(Algorithm)/sizeof(*Algorithm))
336                 digest = 0;
337
338         failed = 0;
339         while ((ch = getopt(argc, argv, "hb:e:pqrs:tx")) != -1) {
340                 switch (ch) {
341                 case 'b':
342                         begin = parseint(optarg);
343                         useoffsets = 1;
344                         break;
345                 case 'e':
346                         end = parseint(optarg);
347                         useoffsets = 1;
348                         break;
349                 case 'p':
350                         MDFilter(&Algorithm[digest], 1);
351                         break;
352                 case 'q':
353                         qflag = 1;
354                         break;
355                 case 'r':
356                         rflag = 1;
357                         break;
358                 case 's':
359                         sflag = 1;
360                         MDString(&Algorithm[digest], optarg);
361                         break;
362                 case 't':
363                         MDTimeTrial(&Algorithm[digest]);
364                         break;
365                 case 'x':
366                         MDTestSuite(&Algorithm[digest]);
367                         break;
368                 case 'h':
369                         usage(EX_OK);
370                 default:
371                         usage(EX_USAGE);
372                 }
373         }
374         argc -= optind;
375         argv += optind;
376
377         if (*argv) {
378                 do {
379                         if (useoffsets)
380                                 p = digestfile(*argv, buf, Algorithm + digest,
381                                     &begin, &end);
382                         else
383                                 p = Algorithm[digest].File(*argv, buf);
384                         if (!p) {
385                                 /* digestfile() outputs its own diagnostics */
386                                 if (!useoffsets)
387                                         warn("%s", *argv);
388                                 failed++;
389                         } else {
390                                 if (qflag) {
391                                         printf("%s\n", p);
392                                 } else if (rflag) {
393                                         if (useoffsets)
394                                                 printf("%s %s[%jd-%jd]\n",
395                                                        p, *argv,
396                                                        (intmax_t)begin,
397                                                        (intmax_t)end);
398                                         else
399                                                 printf("%s %s\n",
400                                                         p, *argv);
401                                 } else if (useoffsets) {
402                                         printf("%s (%s[%jd-%jd]) = %s\n",
403                                                Algorithm[digest].name, *argv,
404                                                (intmax_t)begin,
405                                                (intmax_t)end,
406                                                p);
407                                 } else {
408                                         printf("%s (%s) = %s\n",
409                                                Algorithm[digest].name,
410                                                *argv, p);
411                                 }
412                         }
413                 } while (*++argv);
414         } else if (!sflag && (optind == 1 || qflag || rflag))
415                 MDFilter(&Algorithm[digest], 0);
416
417         if (failed != 0)
418                 return (EX_NOINPUT);
419  
420         return (0);
421 }
422 /*
423  * Digests a string and prints the result.
424  */
425 static void
426 MDString(Algorithm_t *alg, const char *string)
427 {
428         size_t len = strlen(string);
429         char buf[HEX_DIGEST_LENGTH];
430
431         if (qflag)
432                 printf("%s\n", alg->Data(string, len, buf));
433         else if (rflag)
434                 printf("%s \"%s\"\n", alg->Data(string, len, buf), string);
435         else
436                 printf("%s (\"%s\") = %s\n", alg->name, string, alg->Data(string, len, buf));
437 }
438 /*
439  * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
440  */
441 static void
442 MDTimeTrial(Algorithm_t *alg)
443 {
444         DIGEST_CTX context;
445         struct rusage before, after;
446         struct timeval total;
447         float seconds;
448         unsigned char block[TEST_BLOCK_LEN];
449         unsigned int i;
450         char   *p, buf[HEX_DIGEST_LENGTH];
451
452         printf
453             ("%s time trial. Digesting %d %d-byte blocks ...",
454             alg->name, TEST_BLOCK_COUNT, TEST_BLOCK_LEN);
455         fflush(stdout);
456
457         /* Initialize block */
458         for (i = 0; i < TEST_BLOCK_LEN; i++)
459                 block[i] = (unsigned char) (i & 0xff);
460
461         /* Start timer */
462         getrusage(0, &before);
463
464         /* Digest blocks */
465         alg->Init(&context);
466         for (i = 0; i < TEST_BLOCK_COUNT; i++)
467                 alg->Update(&context, block, TEST_BLOCK_LEN);
468         p = alg->End(&context, buf);
469
470         /* Stop timer */
471         getrusage(0, &after);
472         timersub(&after.ru_utime, &before.ru_utime, &total);
473         seconds = total.tv_sec + (float) total.tv_usec / 1000000;
474
475         printf(" done\n");
476         printf("Digest = %s", p);
477         printf("\nTime = %f seconds\n", seconds);
478         printf
479             ("Speed = %f bytes/second\n",
480             (float) TEST_BLOCK_LEN * (float) TEST_BLOCK_COUNT / seconds);
481 }
482 /*
483  * Digests a reference suite of strings and prints the results.
484  */
485
486 const char *MDTestInput[MDTESTCOUNT] = {
487         "", 
488         "a",
489         "abc",
490         "message digest",
491         "abcdefghijklmnopqrstuvwxyz",
492         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
493         "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
494         "MD5 has not yet (2001-09-03) been broken, but sufficient attacks have been made \
495 that its security is in some doubt"
496 };
497
498 const char *MD5TestOutput[MDTESTCOUNT] = {
499         "d41d8cd98f00b204e9800998ecf8427e",
500         "0cc175b9c0f1b6a831c399e269772661",
501         "900150983cd24fb0d6963f7d28e17f72",
502         "f96b697d7cb7938d525a2f31aaf161d0",
503         "c3fcd3d76192e4007dfb496cca67e13b",
504         "d174ab98d277d9f5a5611c2c9f419d9f",
505         "57edf4a22be3c955ac49da2e2107b67a",
506         "b50663f41d44d92171cb9976bc118538"
507 };
508
509 const char *SHA256_TestOutput[MDTESTCOUNT] = {
510         "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
511         "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb",
512         "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
513         "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650",
514         "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73",
515         "db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0",
516         "f371bc4a311f2b009eef952dd83ca80e2b60026c8e935592d0f9c308453c813e",
517         "e6eae09f10ad4122a0e2a4075761d185a272ebd9f5aa489e998ff2f09cbfdd9f"
518 };
519
520 static void
521 MDTestSuite(Algorithm_t *alg)
522 {
523         int i;
524         char buffer[HEX_DIGEST_LENGTH];
525
526         printf("%s test suite:\n", alg->name);
527         for (i = 0; i < MDTESTCOUNT; i++) {
528                 (*alg->Data)(MDTestInput[i], strlen(MDTestInput[i]), buffer);
529                 printf("%s (\"%s\") = %s", alg->name, MDTestInput[i], buffer);
530                 if (strcmp(buffer, (*alg->TestOutput)[i]) == 0)
531                         printf(" - verified correct\n");
532                 else
533                         printf(" - INCORRECT RESULT!\n");
534         }
535 }
536
537 /*
538  * Digests the standard input and prints the result.
539  */
540 static void
541 MDFilter(Algorithm_t *alg, int tee)
542 {
543         DIGEST_CTX context;
544         unsigned int len;
545         unsigned char buffer[BUFSIZ];
546         char buf[HEX_DIGEST_LENGTH];
547
548         alg->Init(&context);
549         while ((len = fread(buffer, 1, BUFSIZ, stdin))) {
550                 if (tee && len != fwrite(buffer, 1, len, stdout))
551                         err(1, "stdout");
552                 alg->Update(&context, buffer, len);
553         }
554         printf("%s\n", alg->End(&context, buf));
555 }
556
557 static void
558 usage(int excode)
559 {
560         fprintf(stderr, "usage:\n\t%s [-pqrtx] [-b offset] [-e offset] "
561             "[-s string] [files ...]\n", getprogname());
562         exit(excode);
563 }
564
565
566 [FILE:2424:files/md5.h]
567 /*
568  * Copyright (c) 2016 The DragonFly Project.  All rights reserved.
569  *
570  * This code is derived from software contributed to The DragonFly Project
571  * by Matthew Dillon <dillon@backplane.com>
572  *
573  * Redistribution and use in source and binary forms, with or without
574  * modification, are permitted provided that the following conditions
575  * are met:
576  *
577  * 1. Redistributions of source code must retain the above copyright
578  *    notice, this list of conditions and the following disclaimer.
579  * 2. Redistributions in binary form must reproduce the above copyright
580  *    notice, this list of conditions and the following disclaimer in
581  *    the documentation and/or other materials provided with the
582  *    distribution.
583  * 3. Neither the name of The DragonFly Project nor the names of its
584  *    contributors may be used to endorse or promote products derived
585  *    from this software without specific, prior written permission.
586  *
587  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
588  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
589  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
590  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
591  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
592  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
593  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
594  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
595  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
596  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
597  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
598  * SUCH DAMAGE.
599  */
600 /*
601  * libmd shims for openssl + non-conflicting old API functions.
602  */
603
604 #ifndef _SYS_MD5_H_
605 #define _SYS_MD5_H_
606
607 #include <sys/types.h>
608 #include <openssl/md5.h>
609
610 #define MD5_BLOCK_LENGTH                MD5_CBLOCK
611 #define MD5_DIGEST_STRING_LENGTH        (MD5_DIGEST_LENGTH * 2 + 1)
612
613 __BEGIN_DECLS
614 int MD5Init (MD5_CTX *);
615 void MD5Update (MD5_CTX *, const void *, unsigned int);
616 void MD5Pad (MD5_CTX *);
617 void MD5Final (unsigned char [16], MD5_CTX *);
618 char *MD5End(MD5_CTX *, char *);
619 char *MD5File(const char *, char *);
620 char *MD5FileChunk(const char *, char *, off_t, off_t);
621 char *MD5Data(const void *, unsigned int, char *);
622 void MD5Transform (u_int32_t [4], const unsigned char [64]);
623 __END_DECLS
624
625 #endif /* _MD5_H_ */
626
627
628 [FILE:9435:files/md5c.c]
629 /*
630  * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
631  *
632  * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
633  * rights reserved.
634  *
635  * License to copy and use this software is granted provided that it
636  * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
637  * Algorithm" in all material mentioning or referencing this software
638  * or this function.
639  *
640  * License is also granted to make and use derivative works provided
641  * that such works are identified as "derived from the RSA Data
642  * Security, Inc. MD5 Message-Digest Algorithm" in all material
643  * mentioning or referencing the derived work.
644  *
645  * RSA Data Security, Inc. makes no representations concerning either
646  * the merchantability of this software or the suitability of this
647  * software for any particular purpose. It is provided "as is"
648  * without express or implied warranty of any kind.
649  *
650  * These notices must be retained in any copies of any part of this
651  * documentation and/or software.
652  *
653  * $FreeBSD: src/lib/libmd/md5c.c,v 1.17 2006/01/17 15:35:56 phk Exp $
654  *
655  * This code is the same as the code published by RSA Inc.  It has been
656  * edited for clarity and style only.
657  */
658
659 #include <sys/types.h>
660 #include <string.h>
661
662 #ifdef __linux__
663 #include <endian.h>
664 #include "md5.h"
665 #else
666 #include <machine/endian.h>
667 #include <sys/endian.h>
668 #include <sys/md5.h>
669 #endif
670
671 #if (BYTE_ORDER == LITTLE_ENDIAN)
672 #define Encode memcpy
673 #define Decode memcpy
674 #else
675
676 /*
677  * Encodes input (u_int32_t) into output (unsigned char). Assumes len is
678  * a multiple of 4.
679  */
680
681 static void
682 Encode (unsigned char *output, u_int32_t *input, unsigned int len)
683 {
684         unsigned int i;
685         u_int32_t *op = (u_int32_t *)output;
686
687         for (i = 0; i < len / 4; i++)
688                 op[i] = htole32(input[i]);
689 }
690
691 /*
692  * Decodes input (unsigned char) into output (u_int32_t). Assumes len is
693  * a multiple of 4.
694  */
695
696 static void
697 Decode (u_int32_t *output, const unsigned char *input, unsigned int len)
698 {
699         unsigned int i;
700         const u_int32_t *ip = (const u_int32_t *)input;
701
702         for (i = 0; i < len / 4; i++)
703                 output[i] = le32toh(ip[i]);
704 }
705 #endif
706
707 static unsigned char PADDING[64] = {
708   0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
709   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
710   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
711 };
712
713 /* F, G, H and I are basic MD5 functions. */
714 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
715 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
716 #define H(x, y, z) ((x) ^ (y) ^ (z))
717 #define I(x, y, z) ((y) ^ ((x) | (~z)))
718
719 /* ROTATE_LEFT rotates x left n bits. */
720 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
721
722 /*
723  * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
724  * Rotation is separate from addition to prevent recomputation.
725  */
726 #define FF(a, b, c, d, x, s, ac) { \
727         (a) += F ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
728         (a) = ROTATE_LEFT ((a), (s)); \
729         (a) += (b); \
730         }
731 #define GG(a, b, c, d, x, s, ac) { \
732         (a) += G ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
733         (a) = ROTATE_LEFT ((a), (s)); \
734         (a) += (b); \
735         }
736 #define HH(a, b, c, d, x, s, ac) { \
737         (a) += H ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
738         (a) = ROTATE_LEFT ((a), (s)); \
739         (a) += (b); \
740         }
741 #define II(a, b, c, d, x, s, ac) { \
742         (a) += I ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
743         (a) = ROTATE_LEFT ((a), (s)); \
744         (a) += (b); \
745         }
746
747 /* MD5 initialization. Begins an MD5 operation, writing a new context. */
748
749 int
750 MD5Init (MD5_CTX *context)
751 {
752
753         context->Nl = context->Nh = 0;
754
755         /* Load magic initialization constants.  */
756         context->A = 0x67452301;
757         context->B = 0xefcdab89;
758         context->C = 0x98badcfe;
759         context->D = 0x10325476;
760         return 1;
761 }
762
763 /* 
764  * MD5 block update operation. Continues an MD5 message-digest
765  * operation, processing another message block, and updating the
766  * context.
767  */
768
769 void
770 MD5Update (MD5_CTX *context, const void *in, unsigned int inputLen)
771 {
772         unsigned int i, idx, partLen;
773         const unsigned char *input = in;
774
775         /* Compute number of bytes mod 64 */
776         idx = (unsigned int)((context->Nl >> 3) & 0x3F);
777
778         /* Update number of bits */
779         if ((context->Nl += ((u_int32_t)inputLen << 3))
780             < ((u_int32_t)inputLen << 3))
781                 context->Nh++;
782         context->Nh += ((u_int32_t)inputLen >> 29);
783
784         partLen = 64 - idx;
785
786         /* Transform as many times as possible. */
787         if (inputLen >= partLen) {
788                 memcpy(&((char *)context->data)[idx], (const void *)input,
789                     partLen);
790                 MD5Transform (&context->A, (char *)context->data);
791
792                 for (i = partLen; i + 63 < inputLen; i += 64)
793                         MD5Transform (&context->A, &input[i]);
794
795                 idx = 0;
796         }
797         else
798                 i = 0;
799
800         /* Buffer remaining input */
801         memcpy (&((char *)context->data)[idx], (const void *)&input[i],
802             inputLen-i);
803 }
804
805 /*
806  * MD5 padding. Adds padding followed by original length.
807  */
808
809 void
810 MD5Pad (MD5_CTX *context)
811 {
812         unsigned char bits[8];
813         unsigned int idx, padLen;
814
815         /* Save number of bits */
816         Encode (bits, &context->Nl, 8);
817
818         /* Pad out to 56 mod 64. */
819         idx = (unsigned int)((context->Nl >> 3) & 0x3f);
820         padLen = (idx < 56) ? (56 - idx) : (120 - idx);
821         MD5Update (context, PADDING, padLen);
822
823         /* Append length (before padding) */
824         MD5Update (context, bits, 8);
825 }
826
827 /*
828  * MD5 finalization. Ends an MD5 message-digest operation, writing the
829  * the message digest and zeroizing the context.
830  */
831
832 void
833 MD5Final (unsigned char digest[16], MD5_CTX *context)
834 {
835         /* Do padding. */
836         MD5Pad (context);
837
838         /* Store state in digest */
839         Encode (digest, &context->A, 16);
840
841         /* Zeroize sensitive information. */
842         memset ((void *)context, 0, sizeof (*context));
843 }
844
845 /* MD5 basic transformation. Transforms state based on block. */
846
847 void
848 MD5Transform (u_int32_t *state, const unsigned char *block)
849 {
850         u_int32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
851
852         Decode (x, block, 64);
853
854         /* Round 1 */
855 #define S11 7
856 #define S12 12
857 #define S13 17
858 #define S14 22
859         FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
860         FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
861         FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
862         FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
863         FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
864         FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
865         FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
866         FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
867         FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
868         FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
869         FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
870         FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
871         FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
872         FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
873         FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
874         FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
875
876         /* Round 2 */
877 #define S21 5
878 #define S22 9
879 #define S23 14
880 #define S24 20
881         GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
882         GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
883         GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
884         GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
885         GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
886         GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
887         GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
888         GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
889         GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
890         GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
891         GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
892         GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
893         GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
894         GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
895         GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
896         GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
897
898         /* Round 3 */
899 #define S31 4
900 #define S32 11
901 #define S33 16
902 #define S34 23
903         HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
904         HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
905         HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
906         HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
907         HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
908         HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
909         HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
910         HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
911         HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
912         HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
913         HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
914         HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
915         HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
916         HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
917         HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
918         HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
919
920         /* Round 4 */
921 #define S41 6
922 #define S42 10
923 #define S43 15
924 #define S44 21
925         II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
926         II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
927         II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
928         II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
929         II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
930         II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
931         II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
932         II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
933         II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
934         II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
935         II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
936         II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
937         II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
938         II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
939         II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
940         II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
941
942         state[0] += a;
943         state[1] += b;
944         state[2] += c;
945         state[3] += d;
946
947         /* Zeroize sensitive information. */
948         memset ((void *)x, 0, sizeof (x));
949 }
950
951
952 [FILE:2120:files/mdXhl.c]
953 /* mdXhl.c * ----------------------------------------------------------------------------
954  * "THE BEER-WARE LICENSE" (Revision 42):
955  * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
956  * can do whatever you want with this stuff. If we meet some day, and you think
957  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
958  * ----------------------------------------------------------------------------
959  *
960  * $FreeBSD: src/lib/libmd/mdXhl.c,v 1.19 2006/01/17 15:35:56 phk Exp $
961  * $DragonFly: src/lib/libmd/mdXhl.c,v 1.3 2008/09/11 20:25:34 swildner Exp $
962  *
963  */
964
965 #include <sys/types.h>
966 #include <sys/stat.h>
967 #include <fcntl.h>
968 #include <unistd.h>
969
970 #include <errno.h>
971 #include <stdio.h>
972 #include <stdlib.h>
973
974 #include "mdX.h"
975
976 char *
977 MDXEnd(MDX_CTX *ctx, char *buf)
978 {
979         int i;
980         unsigned char digest[LENGTH];
981         static const char hex[]="0123456789abcdef";
982
983         if (!buf)
984                 buf = malloc(2*LENGTH + 1);
985         if (!buf)
986                 return 0;
987         MDXFinal(digest, ctx);
988         for (i = 0; i < LENGTH; i++) {
989                 buf[i+i] = hex[digest[i] >> 4];
990                 buf[i+i+1] = hex[digest[i] & 0x0f];
991         }
992         buf[i+i] = '\0';
993         return buf;
994 }
995
996 char *
997 MDXFile(const char *filename, char *buf)
998 {
999         return (MDXFileChunk(filename, buf, 0, 0));
1000 }
1001
1002 char *
1003 MDXFileChunk(const char *filename, char *buf, off_t ofs, off_t len)
1004 {
1005         unsigned char buffer[8192];
1006         MDX_CTX ctx;
1007         struct stat stbuf;
1008         int f, i, e;
1009         off_t n;
1010
1011         MDXInit(&ctx);
1012         f = open(filename, O_RDONLY);
1013         if (f < 0)
1014                 return 0;
1015         if (fstat(f, &stbuf) < 0)
1016                 return 0;
1017         if (ofs > stbuf.st_size)
1018                 ofs = stbuf.st_size;
1019         if ((len == 0) || (len > stbuf.st_size - ofs))
1020                 len = stbuf.st_size - ofs;
1021         if (lseek(f, ofs, SEEK_SET) < 0)
1022                 return 0;
1023         n = len;
1024         i = 0;
1025         while (n > 0) {
1026                 if (n > sizeof(buffer))
1027                         i = read(f, buffer, sizeof(buffer));
1028                 else
1029                         i = read(f, buffer, n);
1030                 if (i < 0) 
1031                         break;
1032                 MDXUpdate(&ctx, buffer, i);
1033                 n -= i;
1034         } 
1035         e = errno;
1036         close(f);
1037         errno = e;
1038         if (i < 0)
1039                 return 0;
1040         return (MDXEnd(&ctx, buf));
1041 }
1042
1043 char *
1044 MDXData (const void *data, unsigned int len, char *buf)
1045 {
1046         MDX_CTX ctx;
1047
1048         MDXInit(&ctx);
1049         MDXUpdate(&ctx,data,len);
1050         return (MDXEnd(&ctx, buf));
1051 }
1052
1053
1054 [FILE:2113:files/sha256.h]
1055 /*
1056  * Copyright (c) 2016 The DragonFly Project.  All rights reserved.
1057  *
1058  * This code is derived from software contributed to The DragonFly Project
1059  * by Matthew Dillon <dillon@backplane.com>
1060  *
1061  * Redistribution and use in source and binary forms, with or without
1062  * modification, are permitted provided that the following conditions
1063  * are met:
1064  *
1065  * 1. Redistributions of source code must retain the above copyright
1066  *    notice, this list of conditions and the following disclaimer.
1067  * 2. Redistributions in binary form must reproduce the above copyright
1068  *    notice, this list of conditions and the following disclaimer in
1069  *    the documentation and/or other materials provided with the
1070  *    distribution.
1071  * 3. Neither the name of The DragonFly Project nor the names of its
1072  *    contributors may be used to endorse or promote products derived
1073  *    from this software without specific, prior written permission.
1074  *
1075  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1076  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1077  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
1078  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
1079  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
1080  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
1081  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1082  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
1083  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
1084  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
1085  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1086  * SUCH DAMAGE.
1087  */
1088 /*
1089  * libmd shims for openssl + non-conflicting old API functions.
1090  */
1091 #ifndef _SHA256_H_
1092 #define _SHA256_H_
1093
1094 #include <sys/types.h>
1095 #include <openssl/sha.h>
1096
1097 __BEGIN_DECLS
1098 char *SHA256_End(SHA256_CTX *, char *);
1099 char *SHA256_File(const char *, char *);
1100 char *SHA256_FileChunk(const char *, char *, off_t, off_t);
1101 char *SHA256_Data(const void *, unsigned int, char *);
1102 __END_DECLS
1103
1104 #endif
1105