1 /* $NetBSD: h_hash.c,v 1.1 2011/01/02 22:03:25 pgoyette Exp $ */ 2 3 /*- 4 * Copyright (c) 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * Combined MD5/SHA1 time and regression test. 31 */ 32 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <unistd.h> 36 #include <string.h> 37 #include <md5.h> 38 #ifdef __NetBSD__ 39 #include <sha1.h> 40 #endif 41 42 #ifdef __FreeBSD__ 43 #include <sha.h> 44 #endif 45 46 int mflag, rflag, sflag, tflag; 47 48 static void 49 usage(void) 50 { 51 (void)fprintf(stderr, 52 "Usage:\t%s -r[ms] < test-file\n" 53 "\t%s -t[ms]\n", 54 getprogname(), getprogname()); 55 exit(1); 56 /* NOTREACHED */ 57 } 58 59 static void 60 hexdump (unsigned char *buf, int len) 61 { 62 int i; 63 for (i=0; i<len; i++) { 64 printf("%02x", buf[i]); 65 } 66 printf("\n"); 67 } 68 69 70 static void 71 timetest(void) 72 { 73 printf("sorry, not yet\n"); 74 } 75 76 #define CHOMP(buf, len, last) \ 77 if ((len > 0) && \ 78 (buf[len-1] == '\n')) { \ 79 buf[len-1] = '\0'; \ 80 len--; \ 81 last = 1; \ 82 } 83 84 static void 85 regress(void) 86 { 87 unsigned char buf[1024]; 88 unsigned char out[20]; 89 int len, outlen, last; 90 91 while (fgets((char *)buf, sizeof(buf), stdin) != NULL) { 92 last = 0; 93 94 len = strlen((char *)buf); 95 CHOMP(buf, len, last); 96 if (mflag) { 97 MD5_CTX ctx; 98 99 MD5Init(&ctx); 100 MD5Update(&ctx, buf, len); 101 while (!last && 102 fgets((char *)buf, sizeof(buf), stdin) != NULL) { 103 len = strlen((char *)buf); 104 CHOMP(buf, len, last); 105 MD5Update(&ctx, buf, len); 106 } 107 MD5Final(out, &ctx); 108 outlen = 16; 109 } else { 110 #ifdef __FreeBSD__ 111 SHA_CTX ctx; 112 113 SHA1_Init(&ctx); 114 SHA1_Update(&ctx, buf, len); 115 #else 116 SHA1_CTX ctx; 117 118 SHA1Init(&ctx); 119 SHA1Update(&ctx, buf, len); 120 #endif 121 while (!last && 122 fgets((char *)buf, sizeof(buf), stdin) != NULL) { 123 len = strlen((char *)buf); 124 CHOMP(buf, len, last); 125 #ifdef __FreeBSD__ 126 SHA1_Update(&ctx, buf, len); 127 #else 128 SHA1Update(&ctx, buf, len); 129 #endif 130 } 131 #ifdef __FreeBSD__ 132 SHA1_Final(out, &ctx); 133 #else 134 SHA1Final(out, &ctx); 135 #endif 136 outlen = 20; 137 } 138 hexdump(out, outlen); 139 } 140 } 141 142 int 143 main(int argc, char **argv) 144 { 145 int ch; 146 147 while ((ch = getopt(argc, argv, "mrst")) != -1) 148 switch (ch) { 149 case 'm': 150 mflag = 1; 151 break; 152 case 'r': 153 rflag = 1; 154 break; 155 case 's': 156 sflag = 1; 157 break; 158 case 't': 159 tflag = 1; 160 break; 161 case '?': 162 default: 163 usage(); 164 } 165 argc -= optind; 166 argv += optind; 167 if (argc > 0) 168 usage(); 169 170 if (!(mflag || sflag)) 171 mflag = 1; 172 173 if ((mflag ^ sflag) != 1) 174 usage(); 175 176 if ((tflag ^ rflag) != 1) 177 usage(); 178 179 if (tflag) 180 timetest(); 181 182 if (rflag) 183 regress(); 184 185 exit(0); 186 187 } 188