/* * Copyright (C) 2016 Felix Fietkau * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * -- MD5 code: * * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. * MD5 Message-Digest Algorithm (RFC 1321). * * Homepage: * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 * * Author: * Alexander Peslyak, better known as Solar Designer * * This software was written by Alexander Peslyak in 2001. No copyright is * claimed, and the software is hereby placed in the public domain. * In case this attempt to disclaim copyright and place the software in the * public domain is deemed null and void, then the software is * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the * general public under the following terms: * * Redistribution and use in source and binary forms, with or without * modification, are permitted. * * There's ABSOLUTELY NO WARRANTY, express or implied. * * (This is a heavily cut-down "BSD license".) * * This differs from Colin Plumb's older public domain implementation in that * no exactly 32-bit integer data type is required (any 32-bit or wider * unsigned integer data type will do), there's no compile-time endianness * configuration, and the function prototypes match OpenSSL's. No code from * Colin Plumb's implementation has been reused; this comment merely compares * the properties of the two independent implementations. * * The primary goals of this implementation are portability and ease of use. * It is meant to be fast, but not as fast as possible. Some known * optimizations are not included to reduce source code size and avoid * compile-time configuration. * * -- SHA256 Code: * * Copyright 2005 Colin Percival * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include #include #define ARRAY_SIZE(_n) (sizeof(_n) / sizeof((_n)[0])) static void be32enc(void *buf, uint32_t u) { uint8_t *p = buf; p[0] = ((uint8_t) ((u >> 24) & 0xff)); p[1] = ((uint8_t) ((u >> 16) & 0xff)); p[2] = ((uint8_t) ((u >> 8) & 0xff)); p[3] = ((uint8_t) (u & 0xff)); } static void be64enc(void *buf, uint64_t u) { uint8_t *p = buf; be32enc(p, ((uint32_t) (u >> 32))); be32enc(p + 4, ((uint32_t) (u & 0xffffffffULL))); } static uint16_t be16dec(const void *buf) { const uint8_t *p = buf; return (((uint16_t) p[0]) << 8) | p[1]; } static uint32_t be32dec(const void *buf) { const uint8_t *p = buf; return (((uint32_t) be16dec(p)) << 16) | be16dec(p + 2); } #define MD5_DIGEST_LENGTH 16 typedef struct MD5_CTX { uint32_t lo, hi; uint32_t a, b, c, d; unsigned char buffer[64]; } MD5_CTX; /* * The basic MD5 functions. * * F and G are optimized compared to their RFC 1321 definitions for * architectures that lack an AND-NOT instruction, just like in Colin Plumb's * implementation. */ #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y)))) #define H(x, y, z) (((x) ^ (y)) ^ (z)) #define H2(x, y, z) ((x) ^ ((y) ^ (z))) #define I(x, y, z) ((y) ^ ((x) | ~(
#
# Copyright (C) 2010-2011 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=6to4
PKG_VERSION:=6
PKG_RELEASE:=1

include $(INCLUDE_DIR)/package.mk

define Package/6to4
  SECTION:=ipv6
  CATEGORY:=IPv6
  DEPENDS:=+ip +kmod-ipv6 +kmod-sit
  TITLE:=IPv6-to-IPv4 configuration support
  MAINTAINER:=Jo-Philipp Wich <xm@subsignal.org>
  PKGARCH:=all
endef

define Package/6to4/description
Provides support for 6to4 tunnels in /etc/config/network.
Refer to http://wiki.openwrt.org/doc/uci/network for
configuration details.
endef

define Build/Compile
endef

define Build/Configure
endef

define Package/6to4/install
	$(INSTALL_DIR) $(1)/lib/network
	$(INSTALL_DATA) ./files/6to4.sh $(1)/lib/network/6to4.sh
	$(INSTALL_DIR) $(1)/etc/hotplug.d/iface
	$(INSTALL_DATA) ./files/6to4.hotplug $(1)/etc/hotplug.d/iface/91-6to4
endef

$(eval $(call BuildPackage,6to4))
x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 }; uint32_t W[64]; uint32_t S[8]; int i; #define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) #define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) #define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ (x >> 3)) #define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ (x >> 10)) /* SHA256 round function */ #define RND(a, b, c, d, e, f, g, h, k) \ h += S1(e) + Ch(e, f, g) + k; \ d += h; \ h += S0(a) + Maj(a, b, c); /* Adjusted round function for rotating state */ #define RNDr(S, W, i, ii) \ RND(S[(64 - i) % 8], S[(65 - i) % 8], \ S[(66 - i) % 8], S[(67 - i) % 8], \ S[(68 - i) % 8], S[(69 - i) % 8], \ S[(70 - i) % 8], S[(71 - i) % 8], \ W[i + ii] + K[i + ii]) /* Message schedule computation */ #define MSCH(W, ii, i) \ W[i + ii + 16] = s1(W[i + ii + 14]) + W[i + ii + 9] + s0(W[i + ii + 1]) + W[i + ii] /* 1. Prepare the first part of the message schedule W. */ be32dec_vect(W, block, 64); /* 2. Initialize working variables. */ memcpy(S, state, 32); /* 3. Mix. */ for (i = 0; i < 64; i += 16) { RNDr(S, W, 0, i); RNDr(S, W, 1, i); RNDr(S, W, 2, i); RNDr(S, W, 3, i); RNDr(S, W, 4, i); RNDr(S, W, 5, i); RNDr(S, W, 6, i); RNDr(S, W, 7, i); RNDr(S, W, 8, i); RNDr(S, W, 9, i); RNDr(S, W, 10, i); RNDr(S, W, 11, i); RNDr(S, W, 12, i); RNDr(S, W, 13, i); RNDr(S, W, 14, i); RNDr(S, W, 15, i); if (i == 48) break; MSCH(W, 0, i); MSCH(W, 1, i); MSCH(W, 2, i); MSCH(W, 3, i); MSCH(W, 4, i); MSCH(W, 5, i); MSCH(W, 6, i); MSCH(W, 7, i); MSCH(W, 8, i); MSCH(W, 9, i); MSCH(W, 10, i); MSCH(W, 11, i); MSCH(W, 12, i); MSCH(W, 13, i); MSCH(W, 14, i); MSCH(W, 15, i); } #undef S0 #undef s0 #undef S1 #undef s1 #undef RND #undef RNDr #undef MSCH /* 4. Mix local working variables into global state */ for (i = 0; i < 8; i++) state[i] += S[i]; } static unsigned char PAD[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; /* Add padding and terminating bit-count. */ static void SHA256_Pad(SHA256_CTX * ctx) { size_t r; /* Figure out how many bytes we have buffered. */ r = (ctx->count >> 3) & 0x3f; /* Pad to 56 mod 64, transforming if we finish a block en route. */ if (r < 56) { /* Pad to 56 mod 64. */ memcpy(&ctx->buf[r], PAD, 56 - r); } else { /* Finish the current block and mix. */ memcpy(&ctx->buf[r], PAD, 64 - r); SHA256_Transform(ctx->state, ctx->buf); /* The start of the final block is all zeroes. */ memset(&ctx->buf[0], 0, 56); } /* Add the terminating bit-count. */ be64enc(&ctx->buf[56], ctx->count); /* Mix in the final block. */ SHA256_Transform(ctx->state, ctx->buf); } /* SHA-256 initialization. Begins a SHA-256 operation. */ static void SHA256_Init(SHA256_CTX * ctx) { /* Zero bits processed so far */ ctx->count = 0; /* Magic initialization constants */ ctx->state[0] = 0x6A09E667; ctx->state[1] = 0xBB67AE85; ctx->state[2] = 0x3C6EF372; ctx->state[3] = 0xA54FF53A; ctx->state[4] = 0x510E527F; ctx->state[5] = 0x9B05688C; ctx->state[6] = 0x1F83D9AB; ctx->state[7] = 0x5BE0CD19; } /* Add bytes into the hash */ static void SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len) { uint64_t bitlen; uint32_t r; const unsigned char *src = in; /* Number of bytes left in the buffer from previous updates */ r = (ctx->count >> 3) & 0x3f; /* Convert the length into a number of bits */ bitlen = len << 3; /* Update number of bits */ ctx->count += bitlen; /* Handle the case where we don't need to perform any transforms */ if (len < 64 - r) { memcpy(&ctx->buf[r], src, len); return; } /* Finish the current block */ memcpy(&ctx->buf[r], src, 64 - r); SHA256_Transform(ctx->state, ctx->buf); src += 64 - r; len -= 64 - r; /* Perform complete blocks */ while (len >= 64) { SHA256_Transform(ctx->state, src); src += 64; len -= 64; } /* Copy left over data into buffer */ memcpy(ctx->buf, src, len); } /* * SHA-256 finalization. Pads the input data, exports the hash value, * and clears the context state. */ static void SHA256_Final(unsigned char digest[static SHA256_DIGEST_LENGTH], SHA256_CTX *ctx) { /* Add padding */ SHA256_Pad(ctx); /* Write the hash */ be32enc_vect(digest, ctx->state, SHA256_DIGEST_LENGTH); /* Clear the context state */ memset(ctx, 0, sizeof(*ctx)); } static void *hash_buf(FILE *f, int *len) { static char buf[1024]; *len = fread(buf, 1, sizeof(buf), f); return *len > 0 ? buf : NULL; } static char *hash_string(unsigned char *buf, int len) { static char str[SHA256_DIGEST_LENGTH * 2 + 1]; int i; if (len * 2 + 1 > sizeof(str)) return NULL; for (i = 0; i < len; i++) sprintf(&str[i * 2], "%02x", buf[i]); return str; } static const char *md5_hash(FILE *f) { MD5_CTX ctx; unsigned char val[MD5_DIGEST_LENGTH]; void *buf; int len; MD5_begin(&ctx); while ((buf = hash_buf(f, &len)) != NULL) MD5_hash(buf, len, &ctx); MD5_end(val, &ctx); return hash_string(val, MD5_DIGEST_LENGTH); } static const char *sha256_hash(FILE *f) { SHA256_CTX ctx; unsigned char val[SHA256_DIGEST_LENGTH]; void *buf; int len; SHA256_Init(&ctx); while ((buf = hash_buf(f, &len)) != NULL) SHA256_Update(&ctx, buf, len); SHA256_Final(val, &ctx); return hash_string(val, SHA256_DIGEST_LENGTH); } struct hash_type { const char *name; const char *(*func)(FILE *f); int len; }; struct hash_type types[] = { { "md5", md5_hash, MD5_DIGEST_LENGTH }, { "sha256", sha256_hash, SHA256_DIGEST_LENGTH }, }; static int usage(const char *progname) { int i; fprintf(stderr, "Usage: %s [...]\n" "Supported hash types:", progname); for (i = 0; i < ARRAY_SIZE(types); i++) fprintf(stderr, "%s %s", i ? "," : "", types[i].name); fprintf(stderr, "\n"); return 1; } static struct hash_type *get_hash_type(const char *name) { int i; for (i = 0; i < ARRAY_SIZE(types); i++) { struct hash_type *t = &types[i]; if (!strcmp(t->name, name)) return t; } return NULL; } static int hash_file(struct hash_type *t, const char *filename, bool add_filename) { const char *str; if (!filename || !strcmp(filename, "-")) { str = t->func(stdin); } else { FILE *f = fopen(filename, "r"); if (!f) { fprintf(stderr, "Failed to open '%s'\n", filename); return 1; } str = t->func(f); fclose(f); } if (!str) { fprintf(stderr, "Failed to generate hash\n"); return 1; } if (add_filename) printf("%s %s\n", str, filename ? filename : "-"); else printf("%s\n", str); return 0; } int main(int argc, char **argv) { struct hash_type *t; const char *progname = argv[0]; int i, ch; bool add_filename = false; while ((ch = getopt(argc, argv, "n")) != -1) { switch (ch) { case 'n': add_filename = true; break; default: return usage(progname); } } argc -= optind; argv += optind; if (argc < 1) return usage(progname); t = get_hash_type(argv[0]); if (!t) return usage(progname); if (argc < 2) return hash_file(t, NULL, add_filename); for (i = 0; i < argc - 1; i++) hash_file(t, argv[1 + i], add_filename); return 0; }