aboutsummaryrefslogtreecommitdiffstats
path: root/tools/firmware-utils/src/pc1crypt.c
blob: fe41b3dab5c0c70e55bf6327d55d12f9be8a55ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#
# Copyright (C) 2013 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

define Profile/Generic
  NAME:=Broadcom SoC, BCM43xx WiFi (b43, default)
  PACKAGES:=kmod-b43
endef

define Profile/Generic/Description
	Package set compatible with hardware any Broadcom BCM47xx or BCM535x 
	SoC with a ARM CPU like the BCM4707, BCM4708, BCM4709, BCM53010
endef

$(eval $(call Profile,Generic))
='#n226'>226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
/*
 *  Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 *
 *  This code was based on:
 *	PC1 Cipher Algorithm ( Pukall Cipher 1 )
 *	By Alexander PUKALL 1991
 *	free code no restriction to use
 *	please include the name of the Author in the final software
 *	the Key is 128 bits
 *	http://membres.lycos.fr/pc1/
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>     /* for unlink() */
#include <libgen.h>
#include <getopt.h>     /* for getopt() */
#include <stdarg.h>
#include <errno.h>
#include <sys/stat.h>

struct pc1_ctx {
	unsigned short	ax;
	unsigned short	bx;
	unsigned short	cx;
	unsigned short	dx;
	unsigned short	si;
	unsigned short	tmp;
	unsigned short	x1a2;
	unsigned short	x1a0[8];
	unsigned short	res;
	unsigned short	i;
	unsigned short	inter;
	unsigned short	cfc;
	unsigned short	cfd;
	unsigned short	compte;
	unsigned char	cle[17];
	short		c;
};

static void pc1_finish(struct pc1_ctx *pc1)
{
	/* erase all variables */
	memset(pc1, 0, sizeof(struct pc1_ctx));
}

static void pc1_code(struct pc1_ctx *pc1)
{
	pc1->dx = pc1->x1a2 + pc1->i;
	pc1->ax = pc1->x1a0[pc1->i];
	pc1->cx = 0x015a;
	pc1->bx = 0x4e35;

	pc1->tmp = pc1->ax;
	pc1->ax = pc1->si;
	pc1->si = pc1->tmp;

	pc1->tmp = pc1->ax;
	pc1->ax = pc1->dx;
	pc1->dx = pc1->tmp;

	if (pc1->ax != 0) {
		pc1->ax = pc1->ax * pc1->bx;
	}

	pc1->tmp = pc1->ax;
	pc1->ax = pc1->cx;
	pc1->cx = pc1->tmp;

	if (pc1->ax != 0) {
		pc1->ax = pc1->ax * pc1->si;
		pc1->cx = pc1->ax + pc1->cx;
	}

	pc1->tmp = pc1->ax;
	pc1->ax = pc1->si;
	pc1->si = pc1->tmp;
	pc1->ax = pc1->ax * pc1->bx;
	pc1->dx = pc1->cx + pc1->dx;

	pc1->ax = pc1->ax + 1;

	pc1->x1a2 = pc1->dx;
	pc1->x1a0[pc1->i] = pc1->ax;

	pc1->res = pc1->ax ^ pc1->dx;
	pc1->i = pc1->i + 1;
}

static void pc1_assemble(struct pc1_ctx *pc1)
{
	pc1->x1a0[0] = (pc1->cle[0] * 256) + pc1->cle[1];

	pc1_code(pc1);
	pc1->inter = pc1->res;

	pc1->x1a0[1] = pc1->x1a0[0] ^ ((pc1->cle[2]*256) + pc1->cle[3]);
	pc1_code(pc1);
	pc1->inter = pc1->inter ^ pc1->res;

	pc1->x1a0[2] = pc1->x1a0[1] ^ ((pc1->cle[4]*256) + pc1->cle[5]);
	pc1_code(pc1);
	pc1->inter = pc1->inter ^ pc1->res;

	pc1->x1a0[3] = pc1->x1a0[2] ^ ((pc1->cle[6]*256) + pc1->cle[7]);
	pc1_code(pc1);
	pc1->inter = pc1->inter ^ pc1->res;

	pc1->x1a0[4] = pc1->x1a0[3] ^ ((pc1->cle[8]*256) + pc1->cle[9]);
	pc1_code(pc1);
	pc1->inter = pc1->inter ^ pc1->res;

	pc1->x1a0[5] = pc1->x1a0[4] ^ ((pc1->cle[10]*256) + pc1->cle[11]);
	pc1_code(pc1);
	pc1->inter = pc1->inter ^ pc1->res;

	pc1->x1a0[6] = pc1->x1a0[5] ^ ((pc1->cle[12]*256) + pc1->cle[13]);
	pc1_code(pc1);
	pc1->inter = pc1->inter ^ pc1->res;

	pc1->x1a0[7] = pc1->x1a0[6] ^ ((pc1->cle[14]*256) + pc1->cle[15]);
	pc1_code(pc1);
	pc1->inter = pc1->inter ^ pc1->res;

	pc1->i = 0;
}

static unsigned char pc1_decrypt(struct pc1_ctx *pc1, short c)
{
	pc1_assemble(pc1);
	pc1->cfc = pc1->inter >> 8;
	pc1->cfd = pc1->inter & 255; /* cfc^cfd = random byte */

	c = c ^ (pc1->cfc ^ pc1->cfd);
	for (pc1->compte = 0; pc1->compte <= 15; pc1->compte++) {
		/* we mix the plaintext byte with the key */
		pc1->cle[pc1->compte] = pc1->cle[pc1->compte] ^ c;
	}

	return c;
}

static unsigned char pc1_encrypt(struct pc1_ctx *pc1, short c)
{
	pc1_assemble(pc1);
	pc1->cfc = pc1->inter >> 8;
	pc1->cfd = pc1->inter & 255; /* cfc^cfd = random byte */

	for (pc1->compte = 0; pc1->compte <= 15; pc1->compte++) {
		/* we mix the plaintext byte with the key */
		pc1->cle[pc1->compte] = pc1->cle[pc1->compte] ^ c;
	}
	c = c ^ (pc1->cfc ^ pc1->cfd);

	return c;
}

static void pc1_init(struct pc1_ctx *pc1)
{
	memset(pc1, 0, sizeof(struct pc1_ctx));

	/* ('Remsaalps!123456') is the key used, you can change it */
	strcpy(pc1->cle, "Remsaalps!123456");
}

static void pc1_decrypt_buf(struct pc1_ctx *pc1, unsigned char *buf,
			    unsigned len)
{
	unsigned i;

	for (i = 0; i < len; i++)
		buf[i] = pc1_decrypt(pc1, buf[i]);
}

static void pc1_encrypt_buf(struct pc1_ctx *pc1, unsigned char *buf,
			    unsigned len)
{
	unsigned i;

	for (i = 0; i < len; i++)
		buf[i] = pc1_encrypt(pc1, buf[i]);
}

/*
 * Globals
 */
static char *ifname;
static char *progname;
static char *ofname;
static int decrypt;

/*
 * Message macros
 */
#define ERR(fmt, ...) do { \
	fflush(0); \
	fprintf(stderr, "[%s] *** error: " fmt "\n", \
			progname, ## __VA_ARGS__ ); \
} while (0)

#define ERRS(fmt, ...) do { \
	int save = errno; \
	fflush(0); \
	fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
			progname, ## __VA_ARGS__, strerror(save)); \
} while (0)

void usage(int status)
{
	FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
	struct board_info *board;

	fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
	fprintf(stream,
"\n"
"Options:\n"
"  -d              decrypt instead of encrypt"
"  -i <file>       read input from the file <file>\n"
"  -o <file>       write output to the file <file>\n"
"  -h              show this screen\n"
	);

	exit(status);
}

#define BUFSIZE		(64 * 1024)

int main(int argc, char *argv[])
{
	struct pc1_ctx pc1;
	int res = EXIT_FAILURE;
	int err;
	struct stat st;
	char *buf;
	unsigned total;

	FILE *outfile, *infile;

	progname = basename(argv[0]);

	while ( 1 ) {
		int c;

		c = getopt(argc, argv, "di:o:h");
		if (c == -1)
			break;

		switch (c) {
		case 'd':
			decrypt = 1;
			break;
		case 'i':
			ifname = optarg;
			break;
		case 'o':
			ofname = optarg;
			break;
		case 'h':
			usage(EXIT_SUCCESS);
			break;
		default:
			usage(EXIT_FAILURE);
			break;
		}
	}

	if (ifname == NULL) {
		ERR("no input file specified");
		goto err;
	}

	if (ofname == NULL) {
		ERR("no output file specified");
		goto err;
	}

	err = stat(ifname, &st);
	if (err){
		ERRS("stat failed on %s", ifname);
		goto err;
	}

	total = st.st_size;
	buf = malloc(BUFSIZE);
	if (!buf) {
		ERR("no memory for buffer\n");
		goto err;
	}

	infile = fopen(ifname, "r");
	if (infile == NULL) {
		ERRS("could not open \"%s\" for reading", ifname);
		goto err_free;
	}

	outfile = fopen(ofname, "w");
	if (outfile == NULL) {
		ERRS("could not open \"%s\" for writing", ofname);
		goto err_close_in;
	}

	pc1_init(&pc1);
	while (total > 0) {
		unsigned datalen;

		if (total > BUFSIZE)
			datalen = BUFSIZE;
		else
			datalen = total;

		errno = 0;
		fread(buf, datalen, 1, infile);
		if (errno != 0) {
			ERRS("unable to read from file %s", ifname);
			goto err_close_out;
		}

		if (decrypt)
			pc1_decrypt_buf(&pc1, buf, datalen);
		else
			pc1_encrypt_buf(&pc1, buf, datalen);

		errno = 0;
		fwrite(buf, datalen, 1, outfile);
		if (errno) {
			ERRS("unable to write to file %s", ofname);
			goto err_close_out;
		}

		total -= datalen;
	}
	pc1_finish(&pc1);

	res = EXIT_SUCCESS;

 out_flush:
	fflush(outfile);

 err_close_out:
	fclose(outfile);
	if (res != EXIT_SUCCESS) {
		unlink(ofname);
	}

 err_close_in:
	fclose(infile);

 err_free:
	free(buf);

 err:
	return res;
}