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