Cyclist.cpp 3.98 KB
Newer Older
Enrico Pozzobon committed
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/*
Implementation by Seth Hoffert, hereby denoted as "the implementer".

For more information, feedback or questions, please refer to our websites:
https://keccak.team/xoodoo.html

To the extent possible under law, the implementer has waived all copyright
and related or neighboring rights to the source code in this file.
http://creativecommons.org/publicdomain/zero/1.0/
*/

#include <algorithm>
#include <cmath>
#include <string>
#include <vector>

#include "Cyclist.h"

static void ref_assert(bool condition, const std::string &synopsis, const char *fct)
{
	if (!condition)
	{
		throw Exception((std::string(fct) + "(): " + synopsis).data());
	}
}

#undef assert

#if defined(__GNUC__)
#define assert(cond, msg)  ref_assert(cond, msg, __PRETTY_FUNCTION__)
#else
#define assert(cond, msg)  ref_assert(cond, msg, __FUNCTION__)
#endif

/* Cyclist */
Cyclist::Cyclist(BaseIterableTransformation &f,
                 unsigned int Rhash,
                 unsigned int Rkin,
                 unsigned int Rkout,
                 unsigned int lratchet,
                 const BitString &K,
                 const BitString &id,
                 const BitString &counter)
	: f(f), Rkin(Rkin), Rkout(Rkout), lratchet(lratchet)
{
	assert((f.width % 8) == 0,
		"This implementation only supports permutation width that are multiple of 8."); // Limitation of Transformation class

	fbp = f.width / 8;
	phase = PHASE_UP;
	s = BitString::zeroes(8 * fbp);
	mode = MODE_HASH;
	Rabsorb = Rhash;
	Rsqueeze = Rhash;
	if (K.size() != 0) AbsorbKey(K, id, counter);
}

void Cyclist::Absorb(const BitString &X)
{
	AbsorbAny(X, Rabsorb, CONSTANT_ABSORB);
}

BitString Cyclist::Encrypt(const BitString &P)
{
	assert(mode == MODE_KEYED, "Mode must be 'keyed'");

	return Crypt(P, false);
}

BitString Cyclist::Decrypt(const BitString &C)
{
	assert(mode == MODE_KEYED, "Mode must be 'keyed'");

	return Crypt(C, true);
}

BitString Cyclist::Squeeze(unsigned int l)
{
	return SqueezeAny(l, CONSTANT_SQUEEZE);
}

BitString Cyclist::SqueezeKey(unsigned int l)
{
	assert(mode == MODE_KEYED, "Mode must be 'keyed'");

	return SqueezeAny(l, CONSTANT_SQUEEZE_KEY);
}

void Cyclist::Ratchet()
{
	assert(mode == MODE_KEYED, "Mode must be 'keyed'");

	AbsorbAny(SqueezeAny(lratchet, CONSTANT_RATCHET), Rabsorb, CONSTANT_ZERO);
}

void Cyclist::AbsorbAny(const BitString &X, unsigned int r, UINT8 cD)
{
	Blocks Xb = Split(X, r);

	for (unsigned int i = 0; i < Xb.size(); i++)
	{
		if (phase != PHASE_UP) Up(0, CONSTANT_ZERO);
		Down(Xb[i], (i == 0) ? cD : CONSTANT_ZERO);
	}
}

void Cyclist::AbsorbKey(const BitString &K, const BitString &id, const BitString &counter)
{
	assert(K.size() + id.size() <= 8 * Rkin - 1, "|K || id| must be <= R_kin - 1 bytes");

	mode = MODE_KEYED;
	Rabsorb = Rkin;
	Rsqueeze = Rkout;

	if (K.size() != 0)
	{
		AbsorbAny(K || id || BitString(8, (UINT8)(id.size() / 8)), Rabsorb, CONSTANT_ABSORB_KEY);
		if (counter.size() != 0) AbsorbAny(counter, 1, CONSTANT_ZERO);
	}
}

BitString Cyclist::Crypt(const BitString &I, bool decrypt)
{
	Blocks Ib = Split(I, Rkout);
	Blocks Ob(8 * Rkout);

	for (unsigned int i = 0; i < Ib.size(); i++)
	{
		Ob[i] = Ib[i] ^ Up(Ib[i].size() / 8, (i == 0) ? CONSTANT_CRYPT : CONSTANT_ZERO);
		Block Pi = decrypt ? Ob[i] : Ib[i];
		Down(Pi, CONSTANT_ZERO);
	}

	return Ob.bits();
}

BitString Cyclist::SqueezeAny(unsigned int l, UINT8 cU)
{
	BitString Y = Up(std::min(l, Rsqueeze), cU);

	while (Y.size() / 8 < l)
	{
		Down(BitString(), CONSTANT_ZERO);
		Y = Y || Up(std::min(l - Y.size() / 8, Rsqueeze), CONSTANT_ZERO);
	}

	return Y;
}

void Cyclist::Down(const BitString &Xi, UINT8 cD)
{
	phase = PHASE_DOWN;
	s = s ^ (Xi || BitString(8, 0x01) || BitString::zeroes(8 * (fbp - 2) - Xi.size()) || BitString(8, (mode == MODE_HASH) ? (cD & 0x01) : cD));
}

BitString Cyclist::Up(unsigned int Yi, UINT8 cU)
{
	phase = PHASE_UP;
	s = f((mode == MODE_HASH) ? s : (s ^ (BitString::zeroes(8 * (fbp - 1)) || BitString(8, cU))));
	return BitString::substring(s, 0, 8 * Yi);
}

Blocks Cyclist::Split(const BitString &X, unsigned int n)
{
	return Blocks(X, 8 * n);
}