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