remove Diamond2, code size reductions
parent
db4981d479
commit
5307588c57
|
|
@ -19,7 +19,6 @@
|
||||||
#include "arc4.h"
|
#include "arc4.h"
|
||||||
#include "rc5.h"
|
#include "rc5.h"
|
||||||
#include "blowfish.h"
|
#include "blowfish.h"
|
||||||
#include "diamond.h"
|
|
||||||
#include "wake.h"
|
#include "wake.h"
|
||||||
#include "3way.h"
|
#include "3way.h"
|
||||||
#include "safer.h"
|
#include "safer.h"
|
||||||
|
|
@ -466,8 +465,6 @@ void BenchMarkAll(double t)
|
||||||
BenchMarkKeyed<RC2::Encryption>("RC2", t);
|
BenchMarkKeyed<RC2::Encryption>("RC2", t);
|
||||||
BenchMarkKeyed<RC5::Encryption>("RC5 (r=16)", t);
|
BenchMarkKeyed<RC5::Encryption>("RC5 (r=16)", t);
|
||||||
BenchMarkKeyed<Blowfish::Encryption>("Blowfish", t);
|
BenchMarkKeyed<Blowfish::Encryption>("Blowfish", t);
|
||||||
BenchMarkKeyed<Diamond2::Encryption>("Diamond2", t);
|
|
||||||
BenchMarkKeyed<Diamond2Lite::Encryption>("Diamond2 Lite", t);
|
|
||||||
BenchMarkKeyed<ThreeWayDecryption>("3-WAY", t);
|
BenchMarkKeyed<ThreeWayDecryption>("3-WAY", t);
|
||||||
BenchMarkKeyed<TEA::Encryption>("TEA", t);
|
BenchMarkKeyed<TEA::Encryption>("TEA", t);
|
||||||
BenchMarkKeyedVariable<SAFER_SK::Encryption>("SAFER (r=8)", t, 8);
|
BenchMarkKeyedVariable<SAFER_SK::Encryption>("SAFER (r=8)", t, 8);
|
||||||
|
|
|
||||||
4
config.h
4
config.h
|
|
@ -39,10 +39,6 @@
|
||||||
// and you want a (possibly) faster IDEA implementation using log tables
|
// and you want a (possibly) faster IDEA implementation using log tables
|
||||||
// #define IDEA_LARGECACHE
|
// #define IDEA_LARGECACHE
|
||||||
|
|
||||||
// Try this if you have a large cache or your CPU is slow manipulating
|
|
||||||
// individual bytes.
|
|
||||||
// #define DIAMOND_USE_PERMTABLE
|
|
||||||
|
|
||||||
// Define this if, for the linear congruential RNG, you want to use
|
// Define this if, for the linear congruential RNG, you want to use
|
||||||
// the original constants as specified in S.K. Park and K.W. Miller's
|
// the original constants as specified in S.K. Park and K.W. Miller's
|
||||||
// CACM paper.
|
// CACM paper.
|
||||||
|
|
|
||||||
119
cryptest.cpp
119
cryptest.cpp
|
|
@ -1,119 +0,0 @@
|
||||||
#include "pch.h"
|
|
||||||
|
|
||||||
#ifdef __BCPLUSPLUS__
|
|
||||||
#include <condefs.h>
|
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
|
||||||
USEUNIT("3way.cpp");
|
|
||||||
USEUNIT("algebra.cpp");
|
|
||||||
USEUNIT("asn.cpp");
|
|
||||||
USEUNIT("base64.cpp");
|
|
||||||
USEUNIT("bench.cpp");
|
|
||||||
USEUNIT("bfinit.cpp");
|
|
||||||
USEUNIT("blowfish.cpp");
|
|
||||||
USEUNIT("blumgold.cpp");
|
|
||||||
USEUNIT("blumshub.cpp");
|
|
||||||
USEUNIT("camellia.cpp");
|
|
||||||
USEUNIT("cast.cpp");
|
|
||||||
USEUNIT("cast128s.cpp");
|
|
||||||
USEUNIT("crc.cpp");
|
|
||||||
USEUNIT("cryptlib.cpp");
|
|
||||||
USEUNIT("default.cpp");
|
|
||||||
USEUNIT("des.cpp");
|
|
||||||
USEUNIT("dessp.cpp");
|
|
||||||
USEUNIT("dh.cpp");
|
|
||||||
USEUNIT("diamond.cpp");
|
|
||||||
USEUNIT("diamondt.cpp");
|
|
||||||
USEUNIT("dsa.cpp");
|
|
||||||
USEUNIT("ec2n.cpp");
|
|
||||||
USEUNIT("eccrypto.cpp");
|
|
||||||
USEUNIT("ecp.cpp");
|
|
||||||
USEUNIT("elgamal.cpp");
|
|
||||||
USEUNIT("eprecomp.cpp");
|
|
||||||
USEUNIT("files.cpp");
|
|
||||||
USEUNIT("filters.cpp");
|
|
||||||
USEUNIT("forkjoin.cpp");
|
|
||||||
USEUNIT("gf2_32.cpp");
|
|
||||||
USEUNIT("gf256.cpp");
|
|
||||||
USEUNIT("gf2n.cpp");
|
|
||||||
USEUNIT("gost.cpp");
|
|
||||||
USEUNIT("gzip.cpp");
|
|
||||||
USEUNIT("haval.cpp");
|
|
||||||
USEUNIT("hex.cpp");
|
|
||||||
USEUNIT("idea.cpp");
|
|
||||||
USEUNIT("integer.cpp");
|
|
||||||
USEUNIT("iterhash.cpp");
|
|
||||||
USEUNIT("luc.cpp");
|
|
||||||
USEUNIT("md5.cpp");
|
|
||||||
USEUNIT("md5mac.cpp");
|
|
||||||
USEUNIT("misc.cpp");
|
|
||||||
USEUNIT("modes.cpp");
|
|
||||||
USEUNIT("nbtheory.cpp");
|
|
||||||
USEUNIT("oaep.cpp");
|
|
||||||
USEUNIT("pch.cpp");
|
|
||||||
USEUNIT("pkcspad.cpp");
|
|
||||||
USEUNIT("polynomi.cpp");
|
|
||||||
USEUNIT("pubkey.cpp");
|
|
||||||
USEUNIT("queue.cpp");
|
|
||||||
USEUNIT("rabin.cpp");
|
|
||||||
USEUNIT("randpool.cpp");
|
|
||||||
USEUNIT("rc5.cpp");
|
|
||||||
USEUNIT("ripemd.cpp");
|
|
||||||
USEUNIT("rng.cpp");
|
|
||||||
USEUNIT("rsa.cpp");
|
|
||||||
USEUNIT("safer.cpp");
|
|
||||||
USEUNIT("seal.cpp");
|
|
||||||
USEUNIT("secshare.cpp");
|
|
||||||
USEUNIT("secsplit.cpp");
|
|
||||||
USEUNIT("sha.cpp");
|
|
||||||
USEUNIT("shacal2.cpp");
|
|
||||||
USEUNIT("shark.cpp");
|
|
||||||
USEUNIT("sharkbox.cpp");
|
|
||||||
USEUNIT("square.cpp");
|
|
||||||
USEUNIT("squaretb.cpp");
|
|
||||||
USEUNIT("tea.cpp");
|
|
||||||
USEUNIT("test.cpp");
|
|
||||||
USEUNIT("tiger.cpp");
|
|
||||||
USEUNIT("tigertab.cpp");
|
|
||||||
USEUNIT("ttmac.cpp");
|
|
||||||
USEUNIT("validat1.cpp");
|
|
||||||
USEUNIT("validat2.cpp");
|
|
||||||
USEUNIT("validat3.cpp");
|
|
||||||
USEUNIT("wake.cpp");
|
|
||||||
USEUNIT("whrlpool.cpp");
|
|
||||||
USEUNIT("zbits.cpp");
|
|
||||||
USEUNIT("zdeflate.cpp");
|
|
||||||
USEUNIT("zinflate.cpp");
|
|
||||||
USEUNIT("ztrees.cpp");
|
|
||||||
USEUNIT("cbc.cpp");
|
|
||||||
USEUNIT("arc4.cpp");
|
|
||||||
USEUNIT("modexppc.cpp");
|
|
||||||
USEUNIT("md2.cpp");
|
|
||||||
USEUNIT("rc2.cpp");
|
|
||||||
USEUNIT("rc6.cpp");
|
|
||||||
USEUNIT("mars.cpp");
|
|
||||||
USEUNIT("rw.cpp");
|
|
||||||
USEUNIT("marss.cpp");
|
|
||||||
USEUNIT("nr.cpp");
|
|
||||||
USEUNIT("mqv.cpp");
|
|
||||||
USEUNIT("rijndael.cpp");
|
|
||||||
USEUNIT("twofish.cpp");
|
|
||||||
USEUNIT("serpent.cpp");
|
|
||||||
USEUNIT("rdtables.cpp");
|
|
||||||
USEUNIT("tftables.cpp");
|
|
||||||
USEUNIT("xtr.cpp");
|
|
||||||
USEUNIT("skipjack.cpp");
|
|
||||||
USEUNIT("mqueue.cpp");
|
|
||||||
USEUNIT("xtrcrypt.cpp");
|
|
||||||
USEUNIT("network.cpp");
|
|
||||||
USEUNIT("osrng.cpp");
|
|
||||||
USEUNIT("socketft.cpp");
|
|
||||||
//---------------------------------------------------------------------------
|
|
||||||
int cmain(int argc, char **argv);
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
return cmain(argc, argv);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
572
diamond.cpp
572
diamond.cpp
|
|
@ -1,572 +0,0 @@
|
||||||
// diamond.cpp - modified by Wei Dai from:
|
|
||||||
|
|
||||||
/* diamond2.c - Encryption designed to exceed DES in security.
|
|
||||||
This file and the Diamond2 and Diamond2 Lite Block Ciphers
|
|
||||||
described herein are hereby dedicated to the Public Domain by the
|
|
||||||
author and inventor, Michael Paul Johnson. Feel free to use these
|
|
||||||
for any purpose that is legally and morally right. The names
|
|
||||||
"Diamond2 Block Cipher" and "Diamond2 Lite Block Cipher" should only
|
|
||||||
be used to describe the algorithms described in this file, to avoid
|
|
||||||
confusion.
|
|
||||||
|
|
||||||
Disclaimers: the following comes with no warranty, expressed or
|
|
||||||
implied. You, the user, must determine the suitability of this
|
|
||||||
information to your own uses. You must also find out what legal
|
|
||||||
requirements exist with respect to this data and programs using
|
|
||||||
it, and comply with whatever valid requirements exist.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "pch.h"
|
|
||||||
#include "diamond.h"
|
|
||||||
#include "crc.h"
|
|
||||||
|
|
||||||
NAMESPACE_BEGIN(CryptoPP)
|
|
||||||
|
|
||||||
class Diamond2SboxMaker
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Diamond2SboxMaker(const byte *external_key, unsigned int key_size,
|
|
||||||
unsigned int rounds, bool lite);
|
|
||||||
|
|
||||||
void MakeSbox(byte *sbox, CipherDir direction);
|
|
||||||
|
|
||||||
private:
|
|
||||||
unsigned int keyrand(unsigned int max_value, const byte *prevSbox);
|
|
||||||
void makeonebox(byte *s, unsigned int i, unsigned int j);
|
|
||||||
|
|
||||||
CRC32 crc;
|
|
||||||
const byte *const key;
|
|
||||||
const unsigned keysize;
|
|
||||||
unsigned keyindex;
|
|
||||||
const unsigned numrounds;
|
|
||||||
const unsigned roundsize; // Number of bytes in one round of substitution boxes
|
|
||||||
const unsigned blocksize;
|
|
||||||
};
|
|
||||||
|
|
||||||
Diamond2SboxMaker::Diamond2SboxMaker(const byte *external_key, unsigned int key_size, unsigned int rounds,
|
|
||||||
bool lite)
|
|
||||||
: key(external_key),
|
|
||||||
keysize(key_size),
|
|
||||||
keyindex(0),
|
|
||||||
numrounds(rounds),
|
|
||||||
roundsize(lite ? 2048 : 4096),
|
|
||||||
blocksize(lite ? 8 : 16)
|
|
||||||
{
|
|
||||||
assert((rounds * blocksize) <= 255);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns uniformly distributed pseudorandom value based on key[], sized keysize
|
|
||||||
inline unsigned int Diamond2SboxMaker::keyrand(unsigned int max_value, const byte *prevSbox)
|
|
||||||
{
|
|
||||||
assert(max_value <= 255);
|
|
||||||
|
|
||||||
if (!max_value) return 0;
|
|
||||||
|
|
||||||
unsigned int mask, prandvalue, i;
|
|
||||||
|
|
||||||
// Create a mask to get the minimum number of
|
|
||||||
// bits to cover the range 0 to max_value.
|
|
||||||
for (i=max_value, mask=0; i > 0; i = i >> 1)
|
|
||||||
mask = (mask << 1) | 1;
|
|
||||||
|
|
||||||
assert(i==0);
|
|
||||||
do
|
|
||||||
{
|
|
||||||
if (prevSbox)
|
|
||||||
crc.UpdateByte(prevSbox[key[keyindex++]]);
|
|
||||||
else
|
|
||||||
crc.UpdateByte(key[keyindex++]);
|
|
||||||
|
|
||||||
if (keyindex >= keysize)
|
|
||||||
{
|
|
||||||
keyindex = 0; /* Recycle thru the key */
|
|
||||||
crc.UpdateByte(byte(keysize));
|
|
||||||
crc.UpdateByte(byte(keysize >> 8));
|
|
||||||
}
|
|
||||||
prandvalue = crc.GetCrcByte(0) & mask;
|
|
||||||
if ((++i>97) && (prandvalue > max_value)) /* Don't loop forever. */
|
|
||||||
prandvalue -= max_value; /* Introduce negligible bias. */
|
|
||||||
}
|
|
||||||
while (prandvalue > max_value); /* Discard out of range values. */
|
|
||||||
return prandvalue;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Diamond2SboxMaker::makeonebox(byte *s, unsigned int i, unsigned int j)
|
|
||||||
{
|
|
||||||
bool filled[256];
|
|
||||||
byte *sbox = s + (roundsize*i) + (256*j);
|
|
||||||
byte *prevSbox = (i||j) ? sbox-256 : 0;
|
|
||||||
|
|
||||||
unsigned m;
|
|
||||||
for (m = 0; m < 256; m++) /* The filled array is used to make sure that */
|
|
||||||
filled[m] = false; /* each byte of the array is filled only once. */
|
|
||||||
for (int n = 255; n >= 0 ; n--) /* n counts the number of bytes left to fill */
|
|
||||||
{
|
|
||||||
// pos is the position among the UNFILLED
|
|
||||||
// components of the s array that the number n should be placed.
|
|
||||||
unsigned pos = keyrand(n, prevSbox);
|
|
||||||
unsigned p=0;
|
|
||||||
while (filled[p]) p++;
|
|
||||||
for (m=0; m<pos; m++)
|
|
||||||
{
|
|
||||||
p++;
|
|
||||||
while (filled[p]) p++;
|
|
||||||
}
|
|
||||||
assert(p<256);
|
|
||||||
sbox[p] = n;
|
|
||||||
filled[p] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Diamond2SboxMaker::MakeSbox(byte *s, CipherDir direction)
|
|
||||||
{
|
|
||||||
unsigned int i, j, k;
|
|
||||||
|
|
||||||
for (i = 0; i < numrounds; i++)
|
|
||||||
for (j = 0; j < blocksize; j++)
|
|
||||||
makeonebox(s, i, j);
|
|
||||||
|
|
||||||
if (direction==DECRYPTION)
|
|
||||||
{
|
|
||||||
SecByteBlock si(numrounds * roundsize);
|
|
||||||
for (i = 0; i < numrounds; i++)
|
|
||||||
for (j = 0; j < blocksize; j++)
|
|
||||||
for (k = 0; k < 256; k++)
|
|
||||||
*(si + (roundsize * i) + (256 * j) + *(s + (roundsize * i) + (256 * j) + k)) = k;
|
|
||||||
memcpy(s, si, numrounds * roundsize);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Diamond2::Base::UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length, unsigned int rounds)
|
|
||||||
{
|
|
||||||
AssertValidKeyLength(length);
|
|
||||||
|
|
||||||
numrounds = rounds;
|
|
||||||
s.New(numrounds * ROUNDSIZE);
|
|
||||||
|
|
||||||
Diamond2SboxMaker m(userKey, length, rounds, false);
|
|
||||||
m.MakeSbox(s, direction);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void Diamond2::Base::substitute(int round, byte *x, const byte *y) const
|
|
||||||
{
|
|
||||||
const byte *sbox = s + (ROUNDSIZE*round);
|
|
||||||
x[0] = sbox[0*256+y[0]];
|
|
||||||
x[1] = sbox[1*256+y[1]];
|
|
||||||
x[2] = sbox[2*256+y[2]];
|
|
||||||
x[3] = sbox[3*256+y[3]];
|
|
||||||
x[4] = sbox[4*256+y[4]];
|
|
||||||
x[5] = sbox[5*256+y[5]];
|
|
||||||
x[6] = sbox[6*256+y[6]];
|
|
||||||
x[7] = sbox[7*256+y[7]];
|
|
||||||
x[8] = sbox[8*256+y[8]];
|
|
||||||
x[9] = sbox[9*256+y[9]];
|
|
||||||
x[10] = sbox[10*256+y[10]];
|
|
||||||
x[11] = sbox[11*256+y[11]];
|
|
||||||
x[12] = sbox[12*256+y[12]];
|
|
||||||
x[13] = sbox[13*256+y[13]];
|
|
||||||
x[14] = sbox[14*256+y[14]];
|
|
||||||
x[15] = sbox[15*256+y[15]];
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef DIAMOND_USE_PERMTABLE
|
|
||||||
|
|
||||||
inline void Diamond2::Base::permute(byte *a)
|
|
||||||
{
|
|
||||||
#ifdef IS_LITTLE_ENDIAN
|
|
||||||
word32 temp0 = (a[0] | (word32(a[10])<<24)) & 0x80000001;
|
|
||||||
#else
|
|
||||||
word32 temp0 = ((word32(a[0])<<24) | a[10]) & 0x01000080;
|
|
||||||
#endif
|
|
||||||
temp0 |= permtable[0][a[1]] |
|
|
||||||
permtable[1][a[2]] | permtable[2][a[3]] |
|
|
||||||
permtable[3][a[4]] | permtable[4][a[5]] |
|
|
||||||
permtable[5][a[6]] | permtable[6][a[7]] |
|
|
||||||
permtable[7][a[8]] | permtable[8][a[9]];
|
|
||||||
|
|
||||||
#ifdef IS_LITTLE_ENDIAN
|
|
||||||
word32 temp1 = (a[4] | (word32(a[14])<<24)) & 0x80000001;
|
|
||||||
#else
|
|
||||||
word32 temp1 = ((word32(a[4])<<24) | a[14]) & 0x01000080;
|
|
||||||
#endif
|
|
||||||
temp1 |= permtable[0][a[5]] |
|
|
||||||
permtable[1][a[6]] | permtable[2][a[7]] |
|
|
||||||
permtable[3][a[8]] | permtable[4][a[9]] |
|
|
||||||
permtable[5][a[10]] | permtable[6][a[11]] |
|
|
||||||
permtable[7][a[12]] | permtable[8][a[13]];
|
|
||||||
|
|
||||||
#ifdef IS_LITTLE_ENDIAN
|
|
||||||
word32 temp2 = (a[8] | (word32(a[2])<<24)) & 0x80000001;
|
|
||||||
#else
|
|
||||||
word32 temp2 = ((word32(a[8])<<24) | a[2]) & 0x01000080;
|
|
||||||
#endif
|
|
||||||
temp2 |= permtable[0][a[9]] |
|
|
||||||
permtable[1][a[10]] | permtable[2][a[11]] |
|
|
||||||
permtable[3][a[12]] | permtable[4][a[13]] |
|
|
||||||
permtable[5][a[14]] | permtable[6][a[15]] |
|
|
||||||
permtable[7][a[0]] | permtable[8][a[1]];
|
|
||||||
|
|
||||||
#ifdef IS_LITTLE_ENDIAN
|
|
||||||
word32 temp3 = (a[12] | (word32(a[6])<<24)) & 0x80000001;
|
|
||||||
#else
|
|
||||||
word32 temp3 = ((word32(a[12])<<24) | a[6]) & 0x01000080;
|
|
||||||
#endif
|
|
||||||
((word32 *)a)[3] = temp3 | permtable[0][a[13]] |
|
|
||||||
permtable[1][a[14]] | permtable[2][a[15]] |
|
|
||||||
permtable[3][a[0]] | permtable[4][a[1]] |
|
|
||||||
permtable[5][a[2]] | permtable[6][a[3]] |
|
|
||||||
permtable[7][a[4]] | permtable[8][a[5]];
|
|
||||||
|
|
||||||
((word32 *)a)[0] = temp0;
|
|
||||||
((word32 *)a)[1] = temp1;
|
|
||||||
((word32 *)a)[2] = temp2;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void Diamond2::Base::ipermute(byte *a)
|
|
||||||
{
|
|
||||||
#ifdef IS_LITTLE_ENDIAN
|
|
||||||
word32 temp0 = (a[9] | (word32(a[3])<<24)) & 0x01000080;
|
|
||||||
#else
|
|
||||||
word32 temp0 = ((word32(a[9])<<24) | a[3]) & 0x80000001;
|
|
||||||
#endif
|
|
||||||
temp0 |= ipermtable[0][a[2]] |
|
|
||||||
ipermtable[1][a[1]] | ipermtable[2][a[0]] |
|
|
||||||
ipermtable[3][a[15]] | ipermtable[4][a[14]] |
|
|
||||||
ipermtable[5][a[13]] | ipermtable[6][a[12]] |
|
|
||||||
ipermtable[7][a[11]] | ipermtable[8][a[10]];
|
|
||||||
|
|
||||||
#ifdef IS_LITTLE_ENDIAN
|
|
||||||
word32 temp1 = (a[13] | (word32(a[7])<<24)) & 0x01000080;
|
|
||||||
#else
|
|
||||||
word32 temp1 = ((word32(a[13])<<24) | a[7]) & 0x80000001;
|
|
||||||
#endif
|
|
||||||
temp1 |= ipermtable[0][a[6]] |
|
|
||||||
ipermtable[1][a[5]] | ipermtable[2][a[4]] |
|
|
||||||
ipermtable[3][a[3]] | ipermtable[4][a[2]] |
|
|
||||||
ipermtable[5][a[1]] | ipermtable[6][a[0]] |
|
|
||||||
ipermtable[7][a[15]] | ipermtable[8][a[14]];
|
|
||||||
|
|
||||||
#ifdef IS_LITTLE_ENDIAN
|
|
||||||
word32 temp2 = (a[1] | (word32(a[11])<<24)) & 0x01000080;
|
|
||||||
#else
|
|
||||||
word32 temp2 = ((word32(a[1])<<24) | a[11]) & 0x80000001;
|
|
||||||
#endif
|
|
||||||
temp2 |= ipermtable[0][a[10]] |
|
|
||||||
ipermtable[1][a[9]] | ipermtable[2][a[8]] |
|
|
||||||
ipermtable[3][a[7]] | ipermtable[4][a[6]] |
|
|
||||||
ipermtable[5][a[5]] | ipermtable[6][a[4]] |
|
|
||||||
ipermtable[7][a[3]] | ipermtable[8][a[2]];
|
|
||||||
|
|
||||||
#ifdef IS_LITTLE_ENDIAN
|
|
||||||
word32 temp3 = (a[5] | (word32(a[15])<<24)) & 0x01000080;
|
|
||||||
#else
|
|
||||||
word32 temp3 = ((word32(a[5])<<24) | a[15]) & 0x80000001;
|
|
||||||
#endif
|
|
||||||
((word32 *)a)[3] = temp3 | ipermtable[0][a[14]] |
|
|
||||||
ipermtable[1][a[13]] | ipermtable[2][a[12]] |
|
|
||||||
ipermtable[3][a[11]] | ipermtable[4][a[10]] |
|
|
||||||
ipermtable[5][a[9]] | ipermtable[6][a[8]] |
|
|
||||||
ipermtable[7][a[7]] | ipermtable[8][a[6]];
|
|
||||||
|
|
||||||
((word32 *)a)[0] = temp0;
|
|
||||||
((word32 *)a)[1] = temp1;
|
|
||||||
((word32 *)a)[2] = temp2;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else // DIAMOND_USE_PERMTABLE
|
|
||||||
|
|
||||||
inline void Diamond2::Base::permute(byte *x)
|
|
||||||
{
|
|
||||||
byte y[16];
|
|
||||||
|
|
||||||
y[0] = (x[0] & 1) | (x[1] & 2) | (x[2] & 4) |
|
|
||||||
(x[3] & 8) | (x[4] & 16) | (x[5] & 32) |
|
|
||||||
(x[6] & 64) | (x[7] & 128);
|
|
||||||
y[1] = (x[1] & 1) | (x[2] & 2) | (x[3] & 4) |
|
|
||||||
(x[4] & 8) | (x[5] & 16) | (x[6] & 32) |
|
|
||||||
(x[7] & 64) | (x[8] & 128);
|
|
||||||
y[2] = (x[2] & 1) | (x[3] & 2) | (x[4] & 4) |
|
|
||||||
(x[5] & 8) | (x[6] & 16) | (x[7] & 32) |
|
|
||||||
(x[8] & 64) | (x[9] & 128);
|
|
||||||
y[3] = (x[3] & 1) | (x[4] & 2) | (x[5] & 4) |
|
|
||||||
(x[6] & 8) | (x[7] & 16) | (x[8] & 32) |
|
|
||||||
(x[9] & 64) | (x[10] & 128);
|
|
||||||
y[4] = (x[4] & 1) | (x[5] & 2) | (x[6] & 4) |
|
|
||||||
(x[7] & 8) | (x[8] & 16) | (x[9] & 32) |
|
|
||||||
(x[10] & 64) | (x[11] & 128);
|
|
||||||
y[5] = (x[5] & 1) | (x[6] & 2) | (x[7] & 4) |
|
|
||||||
(x[8] & 8) | (x[9] & 16) | (x[10] & 32) |
|
|
||||||
(x[11] & 64) | (x[12] & 128);
|
|
||||||
y[6] = (x[6] & 1) | (x[7] & 2) | (x[8] & 4) |
|
|
||||||
(x[9] & 8) | (x[10] & 16) | (x[11] & 32) |
|
|
||||||
(x[12] & 64) | (x[13] & 128);
|
|
||||||
y[7] = (x[7] & 1) | (x[8] & 2) | (x[9] & 4) |
|
|
||||||
(x[10] & 8) | (x[11] & 16) | (x[12] & 32) |
|
|
||||||
(x[13] & 64) | (x[14] & 128);
|
|
||||||
y[8] = (x[8] & 1) | (x[9] & 2) | (x[10] & 4) |
|
|
||||||
(x[11] & 8) | (x[12] & 16) | (x[13] & 32) |
|
|
||||||
(x[14] & 64) | (x[15] & 128);
|
|
||||||
y[9] = (x[9] & 1) | (x[10] & 2) | (x[11] & 4) |
|
|
||||||
(x[12] & 8) | (x[13] & 16) | (x[14] & 32) |
|
|
||||||
(x[15] & 64) | (x[0] & 128);
|
|
||||||
y[10] = (x[10] & 1) | (x[11] & 2) | (x[12] & 4) |
|
|
||||||
(x[13] & 8) | (x[14] & 16) | (x[15] & 32) |
|
|
||||||
(x[0] & 64) | (x[1] & 128);
|
|
||||||
y[11] = (x[11] & 1) | (x[12] & 2) | (x[13] & 4) |
|
|
||||||
(x[14] & 8) | (x[15] & 16) | (x[0] & 32) |
|
|
||||||
(x[1] & 64) | (x[2] & 128);
|
|
||||||
y[12] = (x[12] & 1) | (x[13] & 2) | (x[14] & 4) |
|
|
||||||
(x[15] & 8) | (x[0] & 16) | (x[1] & 32) |
|
|
||||||
(x[2] & 64) | (x[3] & 128);
|
|
||||||
y[13] = (x[13] & 1) | (x[14] & 2) | (x[15] & 4) |
|
|
||||||
(x[0] & 8) | (x[1] & 16) | (x[2] & 32) |
|
|
||||||
(x[3] & 64) | (x[4] & 128);
|
|
||||||
y[14] = (x[14] & 1) | (x[15] & 2) | (x[0] & 4) |
|
|
||||||
(x[1] & 8) | (x[2] & 16) | (x[3] & 32) |
|
|
||||||
(x[4] & 64) | (x[5] & 128);
|
|
||||||
y[15] = (x[15] & 1) | (x[0] & 2) | (x[1] & 4) |
|
|
||||||
(x[2] & 8) | (x[3] & 16) | (x[4] & 32) |
|
|
||||||
(x[5] & 64) | (x[6] & 128);
|
|
||||||
|
|
||||||
memcpy(x, y, 16);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void Diamond2::Base::ipermute(byte *x)
|
|
||||||
{
|
|
||||||
byte y[16];
|
|
||||||
|
|
||||||
y[0] = (x[0] & 1) | (x[15] & 2) | (x[14] & 4) |
|
|
||||||
(x[13] & 8) | (x[12] & 16) | (x[11] & 32) |
|
|
||||||
(x[10] & 64) | (x[9] & 128);
|
|
||||||
y[1] = (x[1] & 1) | (x[0] & 2) | (x[15] & 4) |
|
|
||||||
(x[14] & 8) | (x[13] & 16) | (x[12] & 32) |
|
|
||||||
(x[11] & 64) | (x[10] & 128);
|
|
||||||
y[2] = (x[2] & 1) | (x[1] & 2) | (x[0] & 4) |
|
|
||||||
(x[15] & 8) | (x[14] & 16) | (x[13] & 32) |
|
|
||||||
(x[12] & 64) | (x[11] & 128);
|
|
||||||
y[3] = (x[3] & 1) | (x[2] & 2) | (x[1] & 4) |
|
|
||||||
(x[0] & 8) | (x[15] & 16) | (x[14] & 32) |
|
|
||||||
(x[13] & 64) | (x[12] & 128);
|
|
||||||
y[4] = (x[4] & 1) | (x[3] & 2) | (x[2] & 4) |
|
|
||||||
(x[1] & 8) | (x[0] & 16) | (x[15] & 32) |
|
|
||||||
(x[14] & 64) | (x[13] & 128);
|
|
||||||
y[5] = (x[5] & 1) | (x[4] & 2) | (x[3] & 4) |
|
|
||||||
(x[2] & 8) | (x[1] & 16) | (x[0] & 32) |
|
|
||||||
(x[15] & 64) | (x[14] & 128);
|
|
||||||
y[6] = (x[6] & 1) | (x[5] & 2) | (x[4] & 4) |
|
|
||||||
(x[3] & 8) | (x[2] & 16) | (x[1] & 32) |
|
|
||||||
(x[0] & 64) | (x[15] & 128);
|
|
||||||
y[7] = (x[7] & 1) | (x[6] & 2) | (x[5] & 4) |
|
|
||||||
(x[4] & 8) | (x[3] & 16) | (x[2] & 32) |
|
|
||||||
(x[1] & 64) | (x[0] & 128);
|
|
||||||
y[8] = (x[8] & 1) | (x[7] & 2) | (x[6] & 4) |
|
|
||||||
(x[5] & 8) | (x[4] & 16) | (x[3] & 32) |
|
|
||||||
(x[2] & 64) | (x[1] & 128);
|
|
||||||
y[9] = (x[9] & 1) | (x[8] & 2) | (x[7] & 4) |
|
|
||||||
(x[6] & 8) | (x[5] & 16) | (x[4] & 32) |
|
|
||||||
(x[3] & 64) | (x[2] & 128);
|
|
||||||
y[10] = (x[10] & 1) | (x[9] & 2) | (x[8] & 4) |
|
|
||||||
(x[7] & 8) | (x[6] & 16) | (x[5] & 32) |
|
|
||||||
(x[4] & 64) | (x[3] & 128);
|
|
||||||
y[11] = (x[11] & 1) | (x[10] & 2) | (x[9] & 4) |
|
|
||||||
(x[8] & 8) | (x[7] & 16) | (x[6] & 32) |
|
|
||||||
(x[5] & 64) | (x[4] & 128);
|
|
||||||
y[12] = (x[12] & 1) | (x[11] & 2) | (x[10] & 4) |
|
|
||||||
(x[9] & 8) | (x[8] & 16) | (x[7] & 32) |
|
|
||||||
(x[6] & 64) | (x[5] & 128);
|
|
||||||
y[13] = (x[13] & 1) | (x[12] & 2) | (x[11] & 4) |
|
|
||||||
(x[10] & 8) | (x[9] & 16) | (x[8] & 32) |
|
|
||||||
(x[7] & 64) | (x[6] & 128);
|
|
||||||
y[14] = (x[14] & 1) | (x[13] & 2) | (x[12] & 4) |
|
|
||||||
(x[11] & 8) | (x[10] & 16) | (x[9] & 32) |
|
|
||||||
(x[8] & 64) | (x[7] & 128);
|
|
||||||
y[15] = (x[15] & 1) | (x[14] & 2) | (x[13] & 4) |
|
|
||||||
(x[12] & 8) | (x[11] & 16) | (x[10] & 32) |
|
|
||||||
(x[9] & 64) | (x[8] & 128);
|
|
||||||
|
|
||||||
memcpy(x, y, 16);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // DIAMOND_USE_PERMTABLE
|
|
||||||
|
|
||||||
void Diamond2::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
|
|
||||||
{
|
|
||||||
const byte *x = inBlock;
|
|
||||||
byte y[16];
|
|
||||||
|
|
||||||
substitute(0, y, x);
|
|
||||||
for (int round=1; round < numrounds; round++)
|
|
||||||
{
|
|
||||||
permute(y);
|
|
||||||
substitute(round, y, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (xorBlock)
|
|
||||||
xorbuf(outBlock, xorBlock, y, BLOCKSIZE);
|
|
||||||
else
|
|
||||||
memcpy(outBlock, y, BLOCKSIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Diamond2::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
|
|
||||||
{
|
|
||||||
const byte *x = inBlock;
|
|
||||||
byte y[16];
|
|
||||||
|
|
||||||
substitute(numrounds-1, y, x);
|
|
||||||
for (int round=numrounds-2; round >= 0; round--)
|
|
||||||
{
|
|
||||||
ipermute(y);
|
|
||||||
substitute(round, y, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (xorBlock)
|
|
||||||
xorbuf(outBlock, xorBlock, y, BLOCKSIZE);
|
|
||||||
else
|
|
||||||
memcpy(outBlock, y, BLOCKSIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Diamond2Lite::Base::UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length, unsigned int rounds)
|
|
||||||
{
|
|
||||||
AssertValidKeyLength(length);
|
|
||||||
|
|
||||||
numrounds = rounds;
|
|
||||||
s.New(numrounds * ROUNDSIZE);
|
|
||||||
|
|
||||||
Diamond2SboxMaker m(userKey, length, rounds, true);
|
|
||||||
m.MakeSbox(s, direction);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void Diamond2Lite::Base::substitute(int round, byte *x, const byte *y) const
|
|
||||||
{
|
|
||||||
const byte *sbox = s + (ROUNDSIZE*round);
|
|
||||||
x[0] = sbox[0*256+y[0]];
|
|
||||||
x[1] = sbox[1*256+y[1]];
|
|
||||||
x[2] = sbox[2*256+y[2]];
|
|
||||||
x[3] = sbox[3*256+y[3]];
|
|
||||||
x[4] = sbox[4*256+y[4]];
|
|
||||||
x[5] = sbox[5*256+y[5]];
|
|
||||||
x[6] = sbox[6*256+y[6]];
|
|
||||||
x[7] = sbox[7*256+y[7]];
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef DIAMOND_USE_PERMTABLE
|
|
||||||
|
|
||||||
inline void Diamond2Lite::Base::permute(byte *a)
|
|
||||||
{
|
|
||||||
word32 temp = permtable[0][a[0]] | permtable[1][a[1]] |
|
|
||||||
permtable[2][a[2]] | permtable[3][a[3]] |
|
|
||||||
permtable[4][a[4]] | permtable[5][a[5]] |
|
|
||||||
permtable[6][a[6]] | permtable[7][a[7]];
|
|
||||||
|
|
||||||
((word32 *)a)[1] = permtable[0][a[4]] | permtable[1][a[5]] |
|
|
||||||
permtable[2][a[6]] | permtable[3][a[7]] |
|
|
||||||
permtable[4][a[0]] | permtable[5][a[1]] |
|
|
||||||
permtable[6][a[2]] | permtable[7][a[3]];
|
|
||||||
|
|
||||||
((word32 *)a)[0] = temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void Diamond2Lite::Base::ipermute(byte *a)
|
|
||||||
{
|
|
||||||
word32 temp = ipermtable[0][a[0]] | ipermtable[1][a[1]] |
|
|
||||||
ipermtable[2][a[2]] | ipermtable[3][a[3]] |
|
|
||||||
ipermtable[4][a[4]] | ipermtable[5][a[5]] |
|
|
||||||
ipermtable[6][a[6]] | ipermtable[7][a[7]];
|
|
||||||
|
|
||||||
((word32 *)a)[1] = ipermtable[0][a[4]] | ipermtable[1][a[5]] |
|
|
||||||
ipermtable[2][a[6]] | ipermtable[3][a[7]] |
|
|
||||||
ipermtable[4][a[0]] | ipermtable[5][a[1]] |
|
|
||||||
ipermtable[6][a[2]] | ipermtable[7][a[3]];
|
|
||||||
|
|
||||||
((word32 *)a)[0] = temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
inline void Diamond2Lite::Base::permute(byte *a)
|
|
||||||
{
|
|
||||||
byte b[8];
|
|
||||||
|
|
||||||
b[0] = (a[0] & 1) + (a[1] & 2) + (a[2] & 4) + (a[3] & 8) + (a[4] & 0x10) +
|
|
||||||
(a[5] & 0x20) + (a[6] & 0x40) + (a[7] & 0x80);
|
|
||||||
b[1] = (a[1] & 1) + (a[2] & 2) + (a[3] & 4) + (a[4] & 8) + (a[5] & 0x10) +
|
|
||||||
(a[6] & 0x20) + (a[7] & 0x40) + (a[0] & 0x80);
|
|
||||||
b[2] = (a[2] & 1) + (a[3] & 2) + (a[4] & 4) + (a[5] & 8) + (a[6] & 0x10) +
|
|
||||||
(a[7] & 0x20) + (a[0] & 0x40) + (a[1] & 0x80);
|
|
||||||
b[3] = (a[3] & 1) + (a[4] & 2) + (a[5] & 4) + (a[6] & 8) + (a[7] & 0x10) +
|
|
||||||
(a[0] & 0x20) + (a[1] & 0x40) + (a[2] & 0x80);
|
|
||||||
b[4] = (a[4] & 1) + (a[5] & 2) + (a[6] & 4) + (a[7] & 8) + (a[0] & 0x10) +
|
|
||||||
(a[1] & 0x20) + (a[2] & 0x40) + (a[3] & 0x80);
|
|
||||||
b[5] = (a[5] & 1) + (a[6] & 2) + (a[7] & 4) + (a[0] & 8) + (a[1] & 0x10) +
|
|
||||||
(a[2] & 0x20) + (a[3] & 0x40) + (a[4] & 0x80);
|
|
||||||
b[6] = (a[6] & 1) + (a[7] & 2) + (a[0] & 4) + (a[1] & 8) + (a[2] & 0x10) +
|
|
||||||
(a[3] & 0x20) + (a[4] & 0x40) + (a[5] & 0x80);
|
|
||||||
b[7] = (a[7] & 1) + (a[0] & 2) + (a[1] & 4) + (a[2] & 8) + (a[3] & 0x10) +
|
|
||||||
(a[4] & 0x20) + (a[5] & 0x40) + (a[6] & 0x80);
|
|
||||||
|
|
||||||
memcpy(a, b, 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void Diamond2Lite::Base::ipermute(byte *b)
|
|
||||||
{
|
|
||||||
byte a[8];
|
|
||||||
|
|
||||||
a[0] = (b[0] & 1) + (b[7] & 2) + (b[6] & 4) + (b[5] & 8) + (b[4] & 0x10) +
|
|
||||||
(b[3] & 0x20) + (b[2] & 0x40) + (b[1] & 0x80);
|
|
||||||
a[1] = (b[1] & 1) + (b[0] & 2) + (b[7] & 4) + (b[6] & 8) + (b[5] & 0x10) +
|
|
||||||
(b[4] & 0x20) + (b[3] & 0x40) + (b[2] & 0x80);
|
|
||||||
a[2] = (b[2] & 1) + (b[1] & 2) + (b[0] & 4) + (b[7] & 8) + (b[6] & 0x10) +
|
|
||||||
(b[5] & 0x20) + (b[4] & 0x40) + (b[3] & 0x80);
|
|
||||||
a[3] = (b[3] & 1) + (b[2] & 2) + (b[1] & 4) + (b[0] & 8) + (b[7] & 0x10) +
|
|
||||||
(b[6] & 0x20) + (b[5] & 0x40) + (b[4] & 0x80);
|
|
||||||
a[4] = (b[4] & 1) + (b[3] & 2) + (b[2] & 4) + (b[1] & 8) + (b[0] & 0x10) +
|
|
||||||
(b[7] & 0x20) + (b[6] & 0x40) + (b[5] & 0x80);
|
|
||||||
a[5] = (b[5] & 1) + (b[4] & 2) + (b[3] & 4) + (b[2] & 8) + (b[1] & 0x10) +
|
|
||||||
(b[0] & 0x20) + (b[7] & 0x40) + (b[6] & 0x80);
|
|
||||||
a[6] = (b[6] & 1) + (b[5] & 2) + (b[4] & 4) + (b[3] & 8) + (b[2] & 0x10) +
|
|
||||||
(b[1] & 0x20) + (b[0] & 0x40) + (b[7] & 0x80);
|
|
||||||
a[7] = (b[7] & 1) + (b[6] & 2) + (b[5] & 4) + (b[4] & 8) + (b[3] & 0x10) +
|
|
||||||
(b[2] & 0x20) + (b[1] & 0x40) + (b[0] & 0x80);
|
|
||||||
|
|
||||||
memcpy(b, a, 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // DIAMOND_USE_PERMTABLE
|
|
||||||
|
|
||||||
void Diamond2Lite::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
|
|
||||||
{
|
|
||||||
const byte *x = inBlock;
|
|
||||||
byte y[8];
|
|
||||||
|
|
||||||
substitute(0, y, x);
|
|
||||||
for (int round=1; round < numrounds; round++)
|
|
||||||
{
|
|
||||||
permute(y);
|
|
||||||
substitute(round, y, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (xorBlock)
|
|
||||||
xorbuf(outBlock, xorBlock, y, BLOCKSIZE);
|
|
||||||
else
|
|
||||||
memcpy(outBlock, y, BLOCKSIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Diamond2Lite::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
|
|
||||||
{
|
|
||||||
const byte *x = inBlock;
|
|
||||||
byte y[8];
|
|
||||||
|
|
||||||
substitute(numrounds-1, y, x);
|
|
||||||
for (int round=numrounds-2; round >= 0; round--)
|
|
||||||
{
|
|
||||||
ipermute(y);
|
|
||||||
substitute(round, y, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (xorBlock)
|
|
||||||
xorbuf(outBlock, xorBlock, y, BLOCKSIZE);
|
|
||||||
else
|
|
||||||
memcpy(outBlock, y, BLOCKSIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
NAMESPACE_END
|
|
||||||
109
diamond.h
109
diamond.h
|
|
@ -1,109 +0,0 @@
|
||||||
#ifndef CRYPTOPP_DIAMOND_H
|
|
||||||
#define CRYPTOPP_DIAMOND_H
|
|
||||||
|
|
||||||
/** \file
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "seckey.h"
|
|
||||||
#include "secblock.h"
|
|
||||||
|
|
||||||
NAMESPACE_BEGIN(CryptoPP)
|
|
||||||
|
|
||||||
struct Diamond2_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 1, 256>, public VariableRounds<10>
|
|
||||||
{
|
|
||||||
static const char *StaticAlgorithmName() {return "Diamond2";}
|
|
||||||
};
|
|
||||||
|
|
||||||
/// <a href="http://www.weidai.com/scan-mirror/cs.html#Diamond2">Diamond2</a>
|
|
||||||
class Diamond2 : public Diamond2_Info, public BlockCipherDocumentation
|
|
||||||
{
|
|
||||||
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<Diamond2_Info>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length, unsigned int rounds);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
enum {ROUNDSIZE=4096};
|
|
||||||
inline void substitute(int round, byte *x, const byte *y) const;
|
|
||||||
|
|
||||||
int numrounds;
|
|
||||||
SecByteBlock s; // Substitution boxes
|
|
||||||
|
|
||||||
static inline void permute(byte *);
|
|
||||||
static inline void ipermute(byte *);
|
|
||||||
#ifdef DIAMOND_USE_PERMTABLE
|
|
||||||
static const word32 permtable[9][256];
|
|
||||||
static const word32 ipermtable[9][256];
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
class CRYPTOPP_NO_VTABLE Enc : public Base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
class CRYPTOPP_NO_VTABLE Dec : public Base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
public:
|
|
||||||
typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
|
|
||||||
typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef Diamond2::Encryption Diamond2Encryption;
|
|
||||||
typedef Diamond2::Decryption Diamond2Decryption;
|
|
||||||
|
|
||||||
struct Diamond2Lite_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 1, 256>, public VariableRounds<8>
|
|
||||||
{
|
|
||||||
static const char *StaticAlgorithmName() {return "Diamond2Lite";}
|
|
||||||
};
|
|
||||||
|
|
||||||
/// <a href="http://www.weidai.com/scan-mirror/cs.html#Diamond2">Diamond2Lite</a>
|
|
||||||
class Diamond2Lite : public Diamond2Lite_Info, public BlockCipherDocumentation
|
|
||||||
{
|
|
||||||
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<Diamond2Lite_Info>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length, unsigned int rounds);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
enum {ROUNDSIZE=2048};
|
|
||||||
inline void substitute(int round, byte *x, const byte *y) const;
|
|
||||||
int numrounds;
|
|
||||||
SecByteBlock s; // Substitution boxes
|
|
||||||
|
|
||||||
static inline void permute(byte *);
|
|
||||||
static inline void ipermute(byte *);
|
|
||||||
#ifdef DIAMOND_USE_PERMTABLE
|
|
||||||
static const word32 permtable[8][256];
|
|
||||||
static const word32 ipermtable[8][256];
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
class CRYPTOPP_NO_VTABLE Enc : public Base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
class CRYPTOPP_NO_VTABLE Dec : public Base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
public:
|
|
||||||
typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
|
|
||||||
typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef Diamond2Lite::Encryption Diamond2LiteEncryption;
|
|
||||||
typedef Diamond2Lite::Decryption Diamond2LiteDecryption;
|
|
||||||
|
|
||||||
NAMESPACE_END
|
|
||||||
|
|
||||||
#endif
|
|
||||||
2270
diamondt.cpp
2270
diamondt.cpp
File diff suppressed because it is too large
Load Diff
|
|
@ -106,14 +106,14 @@ struct ElGamal
|
||||||
|
|
||||||
static const char * StaticAlgorithmName() {return "ElgamalEnc/Crypto++Padding";}
|
static const char * StaticAlgorithmName() {return "ElgamalEnc/Crypto++Padding";}
|
||||||
|
|
||||||
class CRYPTOPP_NO_VTABLE EncryptorImpl : public ElGamalObjectImpl<DL_EncryptorBase<Integer>, SchemeOptions, SchemeOptions::PublicKey>, public PublicKeyCopier<SchemeOptions>
|
class EncryptorImpl : public ElGamalObjectImpl<DL_EncryptorBase<Integer>, SchemeOptions, SchemeOptions::PublicKey>, public PublicKeyCopier<SchemeOptions>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void CopyKeyInto(SchemeOptions::PublicKey &key) const
|
void CopyKeyInto(SchemeOptions::PublicKey &key) const
|
||||||
{key = GetKey();}
|
{key = GetKey();}
|
||||||
};
|
};
|
||||||
|
|
||||||
class CRYPTOPP_NO_VTABLE DecryptorImpl : public ElGamalObjectImpl<DL_DecryptorBase<Integer>, SchemeOptions, SchemeOptions::PrivateKey>, public PrivateKeyCopier<SchemeOptions>
|
class DecryptorImpl : public ElGamalObjectImpl<DL_DecryptorBase<Integer>, SchemeOptions, SchemeOptions::PrivateKey>, public PrivateKeyCopier<SchemeOptions>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void CopyKeyInto(SchemeOptions::PublicKey &key) const
|
void CopyKeyInto(SchemeOptions::PublicKey &key) const
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ public:
|
||||||
{
|
{
|
||||||
for (CPP_TYPENAME Map::iterator i = m_map.begin(); i != m_map.end(); ++i)
|
for (CPP_TYPENAME Map::iterator i = m_map.begin(); i != m_map.end(); ++i)
|
||||||
{
|
{
|
||||||
delete i->second;
|
delete (ObjectFactory<AbstractClass> *)i->second;
|
||||||
i->second = NULL;
|
i->second = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -45,7 +45,7 @@ public:
|
||||||
const ObjectFactory<AbstractClass> * GetFactory(const char *name) const
|
const ObjectFactory<AbstractClass> * GetFactory(const char *name) const
|
||||||
{
|
{
|
||||||
CPP_TYPENAME Map::const_iterator i = m_map.find(name);
|
CPP_TYPENAME Map::const_iterator i = m_map.find(name);
|
||||||
return i == m_map.end() ? NULL : i->second;
|
return i == m_map.end() ? NULL : (ObjectFactory<AbstractClass> *)i->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
AbstractClass *CreateObject(const char *name) const
|
AbstractClass *CreateObject(const char *name) const
|
||||||
|
|
@ -58,7 +58,8 @@ public:
|
||||||
static ObjectFactoryRegistry<AbstractClass, instance> & Registry(...);
|
static ObjectFactoryRegistry<AbstractClass, instance> & Registry(...);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef std::map<std::string, ObjectFactory<AbstractClass> *> Map;
|
// use void * instead of ObjectFactory<AbstractClass> * to save code size
|
||||||
|
typedef std::map<std::string, void *> Map;
|
||||||
Map m_map;
|
Map m_map;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
28
misc.h
28
misc.h
|
|
@ -33,12 +33,12 @@ class CRYPTOPP_DLL Empty
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class BASE1, class BASE2>
|
template <class BASE1, class BASE2>
|
||||||
class TwoBases : public BASE1, public BASE2
|
class CRYPTOPP_NO_VTABLE TwoBases : public BASE1, public BASE2
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class BASE1, class BASE2, class BASE3>
|
template <class BASE1, class BASE2, class BASE3>
|
||||||
class ThreeBases : public BASE1, public BASE2, public BASE3
|
class CRYPTOPP_NO_VTABLE ThreeBases : public BASE1, public BASE2, public BASE3
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -203,6 +203,30 @@ inline CipherDir GetCipherDir(const T &obj)
|
||||||
return obj.IsForwardTransformation() ? ENCRYPTION : DECRYPTION;
|
return obj.IsForwardTransformation() ? ENCRYPTION : DECRYPTION;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function safely initializes a static object in a multithreaded environment without using locks.
|
||||||
|
// It may leak memory when two threads try to initialize the static object at the same time
|
||||||
|
// but this should be acceptable since each static object is only initialized once per session.
|
||||||
|
template <class T, class F>
|
||||||
|
T & StaticObject(F NewT, T *dummy=NULL)
|
||||||
|
{
|
||||||
|
static member_ptr<T> s_pObject;
|
||||||
|
static char s_objectState = 0;
|
||||||
|
|
||||||
|
switch (s_objectState)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
s_objectState = 1;
|
||||||
|
s_pObject.reset(NewT());
|
||||||
|
s_objectState = 2;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
while (s_objectState == 1) {}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return *s_pObject;
|
||||||
|
};
|
||||||
|
|
||||||
// ************** rotate functions ***************
|
// ************** rotate functions ***************
|
||||||
|
|
||||||
template <class T> inline T rotlFixed(T x, unsigned int y)
|
template <class T> inline T rotlFixed(T x, unsigned int y)
|
||||||
|
|
|
||||||
143
nbtheory.cpp
143
nbtheory.cpp
|
|
@ -13,103 +13,45 @@
|
||||||
|
|
||||||
NAMESPACE_BEGIN(CryptoPP)
|
NAMESPACE_BEGIN(CryptoPP)
|
||||||
|
|
||||||
const unsigned int maxPrimeTableSize = 3511; // last prime 32719
|
const word s_lastSmallPrime = 32719;
|
||||||
const word lastSmallPrime = 32719;
|
|
||||||
unsigned int primeTableSize=552;
|
|
||||||
|
|
||||||
word primeTable[maxPrimeTableSize] =
|
std::vector<word> * NewPrimeTable()
|
||||||
{2, 3, 5, 7, 11, 13, 17, 19,
|
{
|
||||||
23, 29, 31, 37, 41, 43, 47, 53,
|
const unsigned int maxPrimeTableSize = 3511;
|
||||||
59, 61, 67, 71, 73, 79, 83, 89,
|
|
||||||
97, 101, 103, 107, 109, 113, 127, 131,
|
|
||||||
137, 139, 149, 151, 157, 163, 167, 173,
|
|
||||||
179, 181, 191, 193, 197, 199, 211, 223,
|
|
||||||
227, 229, 233, 239, 241, 251, 257, 263,
|
|
||||||
269, 271, 277, 281, 283, 293, 307, 311,
|
|
||||||
313, 317, 331, 337, 347, 349, 353, 359,
|
|
||||||
367, 373, 379, 383, 389, 397, 401, 409,
|
|
||||||
419, 421, 431, 433, 439, 443, 449, 457,
|
|
||||||
461, 463, 467, 479, 487, 491, 499, 503,
|
|
||||||
509, 521, 523, 541, 547, 557, 563, 569,
|
|
||||||
571, 577, 587, 593, 599, 601, 607, 613,
|
|
||||||
617, 619, 631, 641, 643, 647, 653, 659,
|
|
||||||
661, 673, 677, 683, 691, 701, 709, 719,
|
|
||||||
727, 733, 739, 743, 751, 757, 761, 769,
|
|
||||||
773, 787, 797, 809, 811, 821, 823, 827,
|
|
||||||
829, 839, 853, 857, 859, 863, 877, 881,
|
|
||||||
883, 887, 907, 911, 919, 929, 937, 941,
|
|
||||||
947, 953, 967, 971, 977, 983, 991, 997,
|
|
||||||
1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049,
|
|
||||||
1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097,
|
|
||||||
1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163,
|
|
||||||
1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223,
|
|
||||||
1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283,
|
|
||||||
1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321,
|
|
||||||
1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423,
|
|
||||||
1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459,
|
|
||||||
1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511,
|
|
||||||
1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571,
|
|
||||||
1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619,
|
|
||||||
1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693,
|
|
||||||
1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747,
|
|
||||||
1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811,
|
|
||||||
1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877,
|
|
||||||
1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949,
|
|
||||||
1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003,
|
|
||||||
2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069,
|
|
||||||
2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129,
|
|
||||||
2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203,
|
|
||||||
2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267,
|
|
||||||
2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311,
|
|
||||||
2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377,
|
|
||||||
2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423,
|
|
||||||
2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503,
|
|
||||||
2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579,
|
|
||||||
2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657,
|
|
||||||
2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693,
|
|
||||||
2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741,
|
|
||||||
2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801,
|
|
||||||
2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861,
|
|
||||||
2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939,
|
|
||||||
2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011,
|
|
||||||
3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079,
|
|
||||||
3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167,
|
|
||||||
3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221,
|
|
||||||
3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301,
|
|
||||||
3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347,
|
|
||||||
3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413,
|
|
||||||
3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491,
|
|
||||||
3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541,
|
|
||||||
3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607,
|
|
||||||
3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671,
|
|
||||||
3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727,
|
|
||||||
3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797,
|
|
||||||
3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863,
|
|
||||||
3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923,
|
|
||||||
3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003};
|
|
||||||
|
|
||||||
void BuildPrimeTable()
|
std::auto_ptr<std::vector<word> > pPrimeTable(new std::vector<word>);
|
||||||
|
std::vector<word> &primeTable = *pPrimeTable;
|
||||||
|
primeTable.reserve(maxPrimeTableSize);
|
||||||
|
|
||||||
|
primeTable.push_back(2);
|
||||||
|
unsigned int testEntriesEnd = 1;
|
||||||
|
|
||||||
|
for (unsigned int p=3; p<=s_lastSmallPrime; p+=2)
|
||||||
{
|
{
|
||||||
unsigned int p=primeTable[primeTableSize-1];
|
for (unsigned int j=1; j<testEntriesEnd; j++)
|
||||||
for (unsigned int i=primeTableSize; i<maxPrimeTableSize; i++)
|
|
||||||
{
|
|
||||||
int j;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
p+=2;
|
|
||||||
for (j=1; j<54; j++)
|
|
||||||
if (p%primeTable[j] == 0)
|
if (p%primeTable[j] == 0)
|
||||||
break;
|
break;
|
||||||
} while (j!=54);
|
if (j == testEntriesEnd)
|
||||||
primeTable[i] = p;
|
{
|
||||||
|
primeTable.push_back(p);
|
||||||
|
testEntriesEnd = STDMIN(54U, primeTable.size());
|
||||||
}
|
}
|
||||||
primeTableSize = maxPrimeTableSize;
|
}
|
||||||
assert(primeTable[primeTableSize-1] == lastSmallPrime);
|
|
||||||
|
return pPrimeTable.release();
|
||||||
|
}
|
||||||
|
|
||||||
|
const word * GetPrimeTable(unsigned int &size)
|
||||||
|
{
|
||||||
|
std::vector<word> &primeTable = StaticObject<std::vector<word> >(&NewPrimeTable);
|
||||||
|
size = primeTable.size();
|
||||||
|
return &primeTable[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsSmallPrime(const Integer &p)
|
bool IsSmallPrime(const Integer &p)
|
||||||
{
|
{
|
||||||
BuildPrimeTable();
|
unsigned int primeTableSize;
|
||||||
|
const word * primeTable = GetPrimeTable(primeTableSize);
|
||||||
|
|
||||||
if (p.IsPositive() && p <= primeTable[primeTableSize-1])
|
if (p.IsPositive() && p <= primeTable[primeTableSize-1])
|
||||||
return std::binary_search(primeTable, primeTable+primeTableSize, (word)p.ConvertToLong());
|
return std::binary_search(primeTable, primeTable+primeTableSize, (word)p.ConvertToLong());
|
||||||
|
|
@ -119,6 +61,9 @@ bool IsSmallPrime(const Integer &p)
|
||||||
|
|
||||||
bool TrialDivision(const Integer &p, unsigned bound)
|
bool TrialDivision(const Integer &p, unsigned bound)
|
||||||
{
|
{
|
||||||
|
unsigned int primeTableSize;
|
||||||
|
const word * primeTable = GetPrimeTable(primeTableSize);
|
||||||
|
|
||||||
assert(primeTable[primeTableSize-1] >= bound);
|
assert(primeTable[primeTableSize-1] >= bound);
|
||||||
|
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
@ -134,7 +79,8 @@ bool TrialDivision(const Integer &p, unsigned bound)
|
||||||
|
|
||||||
bool SmallDivisorsTest(const Integer &p)
|
bool SmallDivisorsTest(const Integer &p)
|
||||||
{
|
{
|
||||||
BuildPrimeTable();
|
unsigned int primeTableSize;
|
||||||
|
const word * primeTable = GetPrimeTable(primeTableSize);
|
||||||
return !TrialDivision(p, primeTable[primeTableSize-1]);
|
return !TrialDivision(p, primeTable[primeTableSize-1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -273,9 +219,9 @@ bool IsStrongLucasProbablePrime(const Integer &n)
|
||||||
|
|
||||||
bool IsPrime(const Integer &p)
|
bool IsPrime(const Integer &p)
|
||||||
{
|
{
|
||||||
static const Integer lastSmallPrimeSquared = Integer(lastSmallPrime).Squared();
|
static const Integer lastSmallPrimeSquared = Integer(s_lastSmallPrime).Squared();
|
||||||
|
|
||||||
if (p <= lastSmallPrime)
|
if (p <= s_lastSmallPrime)
|
||||||
return IsSmallPrime(p);
|
return IsSmallPrime(p);
|
||||||
else if (p <= lastSmallPrimeSquared)
|
else if (p <= lastSmallPrimeSquared)
|
||||||
return SmallDivisorsTest(p);
|
return SmallDivisorsTest(p);
|
||||||
|
|
@ -384,7 +330,8 @@ void PrimeSieve::SieveSingle(std::vector<bool> &sieve, word p, const Integer &fi
|
||||||
|
|
||||||
void PrimeSieve::DoSieve()
|
void PrimeSieve::DoSieve()
|
||||||
{
|
{
|
||||||
BuildPrimeTable();
|
unsigned int primeTableSize;
|
||||||
|
const word * primeTable = GetPrimeTable(primeTableSize);
|
||||||
|
|
||||||
const unsigned int maxSieveSize = 32768;
|
const unsigned int maxSieveSize = 32768;
|
||||||
unsigned int sieveSize = STDMIN(Integer(maxSieveSize), (m_last-m_first)/m_step+1).ConvertToLong();
|
unsigned int sieveSize = STDMIN(Integer(maxSieveSize), (m_last-m_first)/m_step+1).ConvertToLong();
|
||||||
|
|
@ -431,11 +378,12 @@ bool FirstPrime(Integer &p, const Integer &max, const Integer &equiv, const Inte
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
BuildPrimeTable();
|
unsigned int primeTableSize;
|
||||||
|
const word * primeTable = GetPrimeTable(primeTableSize);
|
||||||
|
|
||||||
if (p <= primeTable[primeTableSize-1])
|
if (p <= primeTable[primeTableSize-1])
|
||||||
{
|
{
|
||||||
word *pItr;
|
const word *pItr;
|
||||||
|
|
||||||
--p;
|
--p;
|
||||||
if (p.IsPositive())
|
if (p.IsPositive())
|
||||||
|
|
@ -491,6 +439,9 @@ static bool ProvePrime(const Integer &p, const Integer &q)
|
||||||
if (((r%q).Squared()-4*(r/q)).IsSquare())
|
if (((r%q).Squared()-4*(r/q)).IsSquare())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
unsigned int primeTableSize;
|
||||||
|
const word * primeTable = GetPrimeTable(primeTableSize);
|
||||||
|
|
||||||
assert(primeTableSize >= 50);
|
assert(primeTableSize >= 50);
|
||||||
for (int i=0; i<50; i++)
|
for (int i=0; i<50; i++)
|
||||||
{
|
{
|
||||||
|
|
@ -507,7 +458,7 @@ Integer MihailescuProvablePrime(RandomNumberGenerator &rng, unsigned int pbits)
|
||||||
Integer minP = Integer::Power2(pbits-1);
|
Integer minP = Integer::Power2(pbits-1);
|
||||||
Integer maxP = Integer::Power2(pbits) - 1;
|
Integer maxP = Integer::Power2(pbits) - 1;
|
||||||
|
|
||||||
if (maxP <= Integer(lastSmallPrime).Squared())
|
if (maxP <= Integer(s_lastSmallPrime).Squared())
|
||||||
{
|
{
|
||||||
// Randomize() will generate a prime provable by trial division
|
// Randomize() will generate a prime provable by trial division
|
||||||
p.Randomize(rng, minP, maxP, Integer::PRIME);
|
p.Randomize(rng, minP, maxP, Integer::PRIME);
|
||||||
|
|
@ -546,7 +497,9 @@ Integer MaurerProvablePrime(RandomNumberGenerator &rng, unsigned int bits)
|
||||||
const unsigned smallPrimeBound = 29, c_opt=10;
|
const unsigned smallPrimeBound = 29, c_opt=10;
|
||||||
Integer p;
|
Integer p;
|
||||||
|
|
||||||
BuildPrimeTable();
|
unsigned int primeTableSize;
|
||||||
|
const word * primeTable = GetPrimeTable(primeTableSize);
|
||||||
|
|
||||||
if (bits < smallPrimeBound)
|
if (bits < smallPrimeBound)
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
|
|
|
||||||
10
nbtheory.h
10
nbtheory.h
|
|
@ -8,14 +8,8 @@
|
||||||
|
|
||||||
NAMESPACE_BEGIN(CryptoPP)
|
NAMESPACE_BEGIN(CryptoPP)
|
||||||
|
|
||||||
// export a table of small primes
|
// obtain pointer to small prime table and get its size
|
||||||
extern const unsigned int maxPrimeTableSize;
|
CRYPTOPP_DLL const word * GetPrimeTable(unsigned int &size);
|
||||||
extern const word lastSmallPrime;
|
|
||||||
extern unsigned int primeTableSize;
|
|
||||||
extern word primeTable[];
|
|
||||||
|
|
||||||
// build up the table to maxPrimeTableSize
|
|
||||||
CRYPTOPP_DLL void BuildPrimeTable();
|
|
||||||
|
|
||||||
// ************ primality testing ****************
|
// ************ primality testing ****************
|
||||||
|
|
||||||
|
|
|
||||||
2
pubkey.h
2
pubkey.h
|
|
@ -1507,7 +1507,7 @@ public:
|
||||||
|
|
||||||
//! A template implementing constructors for public key algorithm classes
|
//! A template implementing constructors for public key algorithm classes
|
||||||
template <class BASE>
|
template <class BASE>
|
||||||
class PK_FinalTemplate : public BASE
|
class CRYPTOPP_NO_VTABLE PK_FinalTemplate : public BASE
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PK_FinalTemplate() {}
|
PK_FinalTemplate() {}
|
||||||
|
|
|
||||||
2
test.cpp
2
test.cpp
|
|
@ -742,7 +742,7 @@ bool Validate(int alg, bool thorough, const char *seed)
|
||||||
case 7: result = ValidateARC4(); break;
|
case 7: result = ValidateARC4(); break;
|
||||||
case 8: result = ValidateRC5(); break;
|
case 8: result = ValidateRC5(); break;
|
||||||
case 9: result = ValidateBlowfish(); break;
|
case 9: result = ValidateBlowfish(); break;
|
||||||
case 10: result = ValidateDiamond2(); break;
|
// case 10: result = ValidateDiamond2(); break;
|
||||||
case 11: result = ValidateThreeWay(); break;
|
case 11: result = ValidateThreeWay(); break;
|
||||||
case 12: result = ValidateBBS(); break;
|
case 12: result = ValidateBBS(); break;
|
||||||
case 13: result = ValidateDH(); break;
|
case 13: result = ValidateDH(); break;
|
||||||
|
|
|
||||||
51
validat1.cpp
51
validat1.cpp
|
|
@ -15,7 +15,6 @@
|
||||||
#include "arc4.h"
|
#include "arc4.h"
|
||||||
#include "rc5.h"
|
#include "rc5.h"
|
||||||
#include "blowfish.h"
|
#include "blowfish.h"
|
||||||
#include "diamond.h"
|
|
||||||
#include "wake.h"
|
#include "wake.h"
|
||||||
#include "3way.h"
|
#include "3way.h"
|
||||||
#include "safer.h"
|
#include "safer.h"
|
||||||
|
|
@ -78,7 +77,6 @@ bool ValidateAll(bool thorough)
|
||||||
pass=ValidateARC4() && pass;
|
pass=ValidateARC4() && pass;
|
||||||
pass=ValidateRC5() && pass;
|
pass=ValidateRC5() && pass;
|
||||||
pass=ValidateBlowfish() && pass;
|
pass=ValidateBlowfish() && pass;
|
||||||
pass=ValidateDiamond2() && pass;
|
|
||||||
pass=ValidateThreeWay() && pass;
|
pass=ValidateThreeWay() && pass;
|
||||||
pass=ValidateGOST() && pass;
|
pass=ValidateGOST() && pass;
|
||||||
pass=ValidateSHARK() && pass;
|
pass=ValidateSHARK() && pass;
|
||||||
|
|
@ -1123,55 +1121,6 @@ bool ValidateBlowfish()
|
||||||
return pass;
|
return pass;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ValidateDiamond2()
|
|
||||||
{
|
|
||||||
cout << "\nDiamond2 validation suite running...\n\n";
|
|
||||||
|
|
||||||
FileSource valdata("diamond.dat", true, new HexDecoder);
|
|
||||||
HexEncoder output(new FileSink(cout));
|
|
||||||
byte key[32], plain[16], cipher[16], out[16], outplain[16];
|
|
||||||
byte blocksize, rounds, keysize;
|
|
||||||
bool pass=true, fail;
|
|
||||||
member_ptr<BlockTransformation> diamond; // VC60 workaround: auto_ptr lacks reset
|
|
||||||
|
|
||||||
while (valdata.MaxRetrievable() >= 1)
|
|
||||||
{
|
|
||||||
valdata.Get(blocksize);
|
|
||||||
valdata.Get(rounds);
|
|
||||||
valdata.Get(keysize);
|
|
||||||
valdata.Get(key, keysize);
|
|
||||||
valdata.Get(plain, blocksize);
|
|
||||||
valdata.Get(cipher, blocksize);
|
|
||||||
|
|
||||||
if (blocksize==16)
|
|
||||||
diamond.reset(new Diamond2Encryption(key, keysize, rounds));
|
|
||||||
else
|
|
||||||
diamond.reset(new Diamond2LiteEncryption(key, keysize, rounds));
|
|
||||||
|
|
||||||
diamond->ProcessBlock(plain, out);
|
|
||||||
fail=memcmp(out, cipher, blocksize) != 0;
|
|
||||||
|
|
||||||
if (blocksize==16)
|
|
||||||
diamond.reset(new Diamond2Decryption(key, keysize, rounds));
|
|
||||||
else
|
|
||||||
diamond.reset(new Diamond2LiteDecryption(key, keysize, rounds));
|
|
||||||
|
|
||||||
diamond->ProcessBlock(out, outplain);
|
|
||||||
fail=fail || memcmp(outplain, plain, blocksize);
|
|
||||||
|
|
||||||
pass = pass && !fail;
|
|
||||||
|
|
||||||
cout << (fail ? "FAILED " : "passed ");
|
|
||||||
output.Put(key, keysize);
|
|
||||||
cout << "\n ";
|
|
||||||
output.Put(outplain, blocksize);
|
|
||||||
cout << " ";
|
|
||||||
output.Put(out, blocksize);
|
|
||||||
cout << endl;
|
|
||||||
}
|
|
||||||
return pass;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ValidateThreeWay()
|
bool ValidateThreeWay()
|
||||||
{
|
{
|
||||||
cout << "\n3-WAY validation suite running...\n\n";
|
cout << "\n3-WAY validation suite running...\n\n";
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,6 @@ bool ValidateARC4();
|
||||||
|
|
||||||
bool ValidateRC5();
|
bool ValidateRC5();
|
||||||
bool ValidateBlowfish();
|
bool ValidateBlowfish();
|
||||||
bool ValidateDiamond2();
|
|
||||||
bool ValidateThreeWay();
|
bool ValidateThreeWay();
|
||||||
bool ValidateGOST();
|
bool ValidateGOST();
|
||||||
bool ValidateSHARK();
|
bool ValidateSHARK();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue