00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "pch.h"
00016 #include "sapphire.h"
00017
00018 NAMESPACE_BEGIN(CryptoPP)
00019
00020 byte SapphireBase::keyrand(unsigned int limit,
00021 const byte *user_key,
00022 byte keysize,
00023 byte *rsum,
00024 unsigned *keypos)
00025 {
00026 unsigned u,
00027 retry_limiter,
00028 mask;
00029
00030 retry_limiter = 0;
00031 mask = 1;
00032 while (mask < limit)
00033 mask = (mask << 1) + 1;
00034 do
00035 {
00036 *rsum = cards[*rsum] + user_key[(*keypos)++];
00037 if (*keypos >= keysize)
00038 {
00039 *keypos = 0;
00040 *rsum += keysize;
00041 }
00042 u = mask & *rsum;
00043 if (++retry_limiter > 11)
00044 u %= limit;
00045 }
00046 while (u > limit);
00047 return u;
00048 }
00049
00050 SapphireBase::SapphireBase()
00051 : cards(256)
00052 {
00053 }
00054
00055 SapphireBase::SapphireBase(const byte *key, unsigned int keysize)
00056 : cards(256)
00057 {
00058 assert(keysize < 256);
00059
00060
00061
00062
00063
00064
00065
00066
00067 int i;
00068 byte rsum;
00069 unsigned keypos;
00070
00071
00072
00073 for (i=0;i<256;i++)
00074 cards[i] = i;
00075
00076
00077
00078 keypos = 0;
00079 rsum = 0;
00080 for (i=255;i;i--)
00081 std::swap(cards[i], cards[keyrand(i, key, keysize, &rsum, &keypos)]);
00082
00083
00084
00085
00086
00087
00088 rotor = cards[1];
00089 ratchet = cards[3];
00090 avalanche = cards[5];
00091 last_plain = cards[7];
00092 last_cipher = cards[rsum];
00093
00094 rsum = 0;
00095 keypos = 0;
00096 }
00097
00098 SapphireBase::~SapphireBase()
00099 {
00100 rotor = ratchet = avalanche = last_plain = last_cipher = 0;
00101 }
00102
00103 void SapphireEncryption::ProcessString(byte *outString, const byte *inString, unsigned int length)
00104 {
00105 while(length--)
00106 *outString++ = SapphireEncryption::ProcessByte(*inString++);
00107 }
00108
00109 void SapphireEncryption::ProcessString(byte *inoutString, unsigned int length)
00110 {
00111 while(length--)
00112 *inoutString++ = SapphireEncryption::ProcessByte(*inoutString);
00113 }
00114
00115 void SapphireDecryption::ProcessString(byte *outString, const byte *inString, unsigned int length)
00116 {
00117 while(length--)
00118 *outString++ = SapphireDecryption::ProcessByte(*inString++);
00119 }
00120
00121 void SapphireDecryption::ProcessString(byte *inoutString, unsigned int length)
00122 {
00123 while(length--)
00124 *inoutString++ = SapphireDecryption::ProcessByte(*inoutString);
00125 }
00126
00127 SapphireHash::SapphireHash(unsigned int hashLength)
00128 : SapphireEncryption(), hashLength(hashLength)
00129 {
00130
00131
00132
00133 int i, j;
00134
00135
00136
00137 rotor = 1;
00138 ratchet = 3;
00139 avalanche = 5;
00140 last_plain = 7;
00141 last_cipher = 11;
00142
00143
00144
00145 for (i=0, j=255;i<256;i++,j--)
00146 cards[i] = (byte) j;
00147 }
00148
00149 void SapphireHash::Update(const byte *input, unsigned int length)
00150 {
00151 while(length--)
00152 SapphireEncryption::ProcessByte(*input++);
00153 }
00154
00155 void SapphireHash::Final(byte *hash, unsigned int overrideHashLength)
00156 {
00157 for (int i=255; i>=0; i--)
00158 ProcessByte((byte) i);
00159
00160 for (unsigned int j=0; j<overrideHashLength; j++)
00161 hash[j] = ProcessByte(0);
00162 }
00163
00164 NAMESPACE_END