Start at in.tokend (not writing files yet)

This commit is contained in:
Neale Pickett 2010-09-02 17:10:11 -06:00
parent d3707d912e
commit 8d4fea620b
5 changed files with 192 additions and 24 deletions

3
src/Makefile Normal file
View File

@ -0,0 +1,3 @@
all: in.tokend
in.tokend: in.tokend.c xxtea.c

136
src/in.tokend.c Normal file
View File

@ -0,0 +1,136 @@
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <ctype.h>
#include "xxtea.h"
#define itokenlen 3
char const consonants[] = "bcdfghklmnprstvz";
char const vowels[] = "aeiouy";
#define bubblebabble_len(n) (6*(((n)/2)+1))
/** Compute bubble babble for input buffer.
*
* The generated output will be of length 6*((inlen/2)+1), including the
* trailing NULL.
*
* Test vectors:
* `' (empty string) `xexax'
* `1234567890' `xesef-disof-gytuf-katof-movif-baxux'
* `Pineapple' `xigak-nyryk-humil-bosek-sonax'
*/
void
bubblebabble(char *out, char const *in, const size_t inlen)
{
size_t pos = 0;
int seed = 1;
size_t i = 0;
out[pos++] = 'x';
while (1) {
unsigned char c;
if (i == inlen) {
out[pos++] = vowels[seed % 6];
out[pos++] = 'x';
out[pos++] = vowels[seed / 6];
break;
}
c = in[i++];
out[pos++] = vowels[(((c >> 6) & 3) + seed) % 6];
out[pos++] = consonants[(c >> 2) & 15];
out[pos++] = vowels[((c & 3) + (seed / 6)) % 6];
if (i == inlen) {
break;
}
seed = ((seed * 5) + (c * 7) + in[i]) % 36;
c = in[i++];
out[pos++] = consonants[(c >> 4) & 15];
out[pos++] = '-';
out[pos++] = consonants[c & 15];
}
out[pos++] = 'x';
out[pos] = '\0';
}
int
main(int argc, char *argv[])
{
char service[50];
char token[80];
uint32_t key[4];
size_t tokenlen;
/* This ought to be unpredictable enough for a contest */
srand((int)time(NULL) * (int)getpid());
/* Read service name */
{
size_t len;
int i;
len = fread(service, 1, sizeof(service) - 1, stdin);
for (i = 0; (i < len) && isalnum(service[i]); i += 1);
service[i] = '\0';
}
/* Read in that service's key */
{
FILE *f = fopen(service, "r");
size_t len;
if (! f) {
printf("Unregistered service");
return 0;
}
len = fread(&key, sizeof(uint32_t), 4, f);
fclose(f);
if (4 != len) {
printf("Key file screwed up");
return 0;
}
}
/* Create the token */
{
uint8_t crap[itokenlen];
char digest[bubblebabble_len(itokenlen)];
int i;
/* Digest some random junk */
for (i = 0; i < itokenlen; i += 1) {
crap[i] = (uint8_t)random();
}
bubblebabble(digest, crap, itokenlen);
/* Append digest to service name */
tokenlen = (size_t)snprintf(token, sizeof(token),
"%s:%s",
service, digest);
}
/* Encrypt the token */
/* Note that now tokenlen is in uint32_ts, not chars! */
{
tokenlen = (tokenlen + (tokenlen % 4)) / 4;
tea_encode(key, (uint32_t *)token, tokenlen);
}
/* Send it back */
fwrite(token, tokenlen, sizeof(uint32_t), stdout);
return 0;
}

51
src/xxtea.c Normal file
View File

@ -0,0 +1,51 @@
#include <stdint.h>
#include <stddef.h>
#define DELTA 0x9e3779b9
#define MX ((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (k[(p&3)^e] ^ z));
void
btea(uint32_t *v, int n, uint32_t const k[4])
{
uint32_t y, z, sum;
unsigned p, rounds, e;
if (n > 1) { /* Coding Part */
rounds = 6 + 52/n;
sum = 0;
z = v[n-1];
do {
sum += DELTA;
e = (sum >> 2) & 3;
for (p=0; p<n-1; p++)
y = v[p+1], z = v[p] += MX;
y = v[0];
z = v[n-1] += MX;
} while (--rounds);
} else if (n < -1) { /* Decoding Part */
n = -n;
rounds = 6 + 52/n;
sum = rounds*DELTA;
y = v[0];
do {
e = (sum >> 2) & 3;
for (p=n-1; p>0; p--)
z = v[p-1], y = v[p] -= MX;
z = v[n-1];
y = v[0] -= MX;
} while ((sum -= DELTA) != 0);
}
}
void
tea_encode(uint32_t const key[4], uint32_t *buf, size_t buflen)
{
btea(buf, buflen, key);
}
void
tea_decode(uint32_t const key[4], uint32_t *buf, size_t buflen)
{
btea(buf, -buflen, key);
}

2
src/xxtea.h Normal file
View File

@ -0,0 +1,2 @@
void tea_encode(uint32_t const key[4], uint32_t *buf, size_t buflen);
void tea_decode(uint32_t const key[4], uint32_t *buf, size_t buflen);

View File

@ -1,24 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>$title</title>
<link rel="stylesheet" href="${base}ctf.css" type="text/css" />
$hdr
</head>
<body class="$body_class" onload="$onload">
<h1>$title</h1>
<div id="navigation">
<ul>
<li><a href="${base}">Home</a></li>
<li><a href="${base}tanks/results.cgi">Tanks</a></li>
<li><a href="${base}puzzler.cgi">Puzzles</a></li>
<li><a href="${base}scoreboard.html">Scoreboard</a></li>
</ul>
$links
</div>
$body
</body>
</html>