opentnl-cvs Mailing List for Torque Network Library
Brought to you by:
mark_frohnmayer,
s_alanet
You can subscribe to this list here.
2004 |
Jan
|
Feb
|
Mar
|
Apr
(141) |
May
(232) |
Jun
(103) |
Jul
(36) |
Aug
(35) |
Sep
(19) |
Oct
(20) |
Nov
|
Dec
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
2005 |
Jan
|
Feb
(26) |
Mar
(2) |
Apr
(84) |
May
(5) |
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
From: Mark F. <mar...@us...> - 2005-05-15 22:50:18
|
Update of /cvsroot/opentnl/tnl/tnl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22723/tnl Modified Files: tnl.overview.txt Log Message: Fixed "Hello World" example in the TNL docs to reflect RPC changes. Index: tnl.overview.txt =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/tnl.overview.txt,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** tnl.overview.txt 19 Aug 2004 21:26:32 -0000 1.3 --- tnl.overview.txt 15 May 2005 22:50:00 -0000 1.4 *************** *** 408,419 **** // declare the client to server message ! TNL_DECLARE_RPC(rpcMessageClientToServer, (const char *theMessageString)); // declare the server to client message ! TNL_DECLARE_RPC(rpcMessageServerToClient, (const char *theMessageString)); }; @endcode ! The first macro invoked, TNL_DECLARE_NETCONNECTION declares members necessary to make this class work as a connection in TNL. The RPC methods are then declared using the TNL_DECLARE_RPC macro. The first parameter to the macro is the name of the RPC method, which can be invoked like any other member function on the object. The second argument is a parenthesized argument list to the RPC. Although these functions are both declared to take only a single string, RPC method parameter lists can contain all the basic TNL types, as well as StringTableEntry, ByteBuffer and Vector objects. Next, define the implementation of the SimpleEventConnection class: --- 408,419 ---- // declare the client to server message ! TNL_DECLARE_RPC(rpcMessageClientToServer, (StringPtr theMessageString)); // declare the server to client message ! TNL_DECLARE_RPC(rpcMessageServerToClient, (StringPtr theMessageString)); }; @endcode ! The first macro invoked, TNL_DECLARE_NETCONNECTION declares members necessary to make this class work as a connection in TNL. The RPC methods are then declared using the TNL_DECLARE_RPC macro. The first parameter to the macro is the name of the RPC method, which can be invoked like any other member function on the object. The second argument is a parenthesized argument list to the RPC. Although these functions are both declared to take only a single string, RPC method parameter lists can contain all the basic TNL types, as well as StringTableEntry, ByteBufferPtr and Vector objects. Next, define the implementation of the SimpleEventConnection class: *************** *** 422,439 **** TNL_IMPLEMENT_NETCONNECTION(SimpleEventConnection, NetClassGroupGame, true); ! TNL_IMPLEMENT_RPC(SimpleEventConnection, rpcMessageClientToServer, (const char *messageString), NetClassGroupGameMask, RPCGuaranteedOrdered, RPCDirClientToServer, 0) { // display the message the client sent ! printf("Got message from client: %s\n", messageString); // send a hello world back to the client. rpcMessageServerToClient("Hello, World!"); } ! TNL_IMPLEMENT_RPC(SimpleEventConnection, rpcMessageServerToClient, (const char *messageString), NetClassGroupGameMask, RPCGuaranteedOrdered, RPCDirServerToClient, 0) { // display the message the server sent ! printf("Got a message from server: %s\n", messageString); // once the client has heard back from the server, it should quit. --- 422,439 ---- TNL_IMPLEMENT_NETCONNECTION(SimpleEventConnection, NetClassGroupGame, true); ! TNL_IMPLEMENT_RPC(SimpleEventConnection, rpcMessageClientToServer, (StringPtr messageString), (messageString), NetClassGroupGameMask, RPCGuaranteedOrdered, RPCDirClientToServer, 0) { // display the message the client sent ! printf("Got message from client: %s\n", messageString.getString()); // send a hello world back to the client. rpcMessageServerToClient("Hello, World!"); } ! TNL_IMPLEMENT_RPC(SimpleEventConnection, rpcMessageServerToClient, (StringPtr messageString), (messageString), NetClassGroupGameMask, RPCGuaranteedOrdered, RPCDirServerToClient, 0) { // display the message the server sent ! printf("Got a message from server: %s\n", messageString.getString()); // once the client has heard back from the server, it should quit. |
From: Mark F. <mar...@us...> - 2005-05-05 16:08:56
|
Update of /cvsroot/opentnl/tnl/tnl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv523/tnl Modified Files: tnl.vcproj Log Message: Updated release build project settings for VC7 to properly compile new libtomcrypt Index: tnl.vcproj =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/tnl.vcproj,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** tnl.vcproj 28 Apr 2005 17:30:30 -0000 1.7 --- tnl.vcproj 5 May 2005 16:08:29 -0000 1.8 *************** *** 74,78 **** Optimization="2" InlineFunctionExpansion="1" ! AdditionalIncludeDirectories="../libtomcrypt" PreprocessorDefinitions="WIN32;NDEBUG;_LIB;TNL_ENABLE_LOGGING" StringPooling="TRUE" --- 74,78 ---- Optimization="2" InlineFunctionExpansion="1" ! AdditionalIncludeDirectories="../libtomcrypt/headers" PreprocessorDefinitions="WIN32;NDEBUG;_LIB;TNL_ENABLE_LOGGING" StringPooling="TRUE" |
From: Mark F. <mar...@us...> - 2005-05-05 16:08:54
|
Update of /cvsroot/opentnl/tnl/libtomcrypt In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv523/libtomcrypt Modified Files: libtomcrypt.vcproj Log Message: Updated release build project settings for VC7 to properly compile new libtomcrypt Index: libtomcrypt.vcproj =================================================================== RCS file: /cvsroot/opentnl/tnl/libtomcrypt/libtomcrypt.vcproj,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** libtomcrypt.vcproj 28 Apr 2005 17:27:50 -0000 1.2 --- libtomcrypt.vcproj 5 May 2005 16:08:29 -0000 1.3 *************** *** 75,79 **** Optimization="2" InlineFunctionExpansion="1" ! AdditionalIncludeDirectories="." PreprocessorDefinitions="WIN32;NDEBUG;_LIB" StringPooling="TRUE" --- 75,79 ---- Optimization="2" InlineFunctionExpansion="1" ! AdditionalIncludeDirectories="headers" PreprocessorDefinitions="WIN32;NDEBUG;_LIB" StringPooling="TRUE" |
From: Mark F. <mar...@us...> - 2005-05-01 00:30:05
|
Update of /cvsroot/opentnl/tnl/tnl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13600/tnl Modified Files: bitStream.cpp netConnection.cpp tnlBitStream.h udp.cpp Log Message: Zeroed out port in unix version of getInterfaceAddresses Fixed BitStream overwrite issue Checked parameters on setFixedRateParams on NetConnection Index: bitStream.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/bitStream.cpp,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** bitStream.cpp 28 Apr 2005 17:30:30 -0000 1.8 --- bitStream.cpp 1 May 2005 00:29:55 -0000 1.9 *************** *** 51,55 **** { bitNum = 0; ! error = false; mCompressRelative = false; mStringBuffer[0] = 0; --- 51,55 ---- { bitNum = 0; ! mError = false; mCompressRelative = false; mStringBuffer[0] = 0; *************** *** 83,87 **** if(!resize(newSize)) { ! error = true; return false; } --- 83,87 ---- if(!resize(newSize)) { ! mError = true; return false; } *************** *** 98,102 **** --- 98,106 ---- if(bitCount + bitNum > maxWriteBitNum) if(!resizeBits(bitCount + bitNum - maxWriteBitNum)) + { + // fill to the end, so the higher level code knows the BitStream is full. + bitNum = maxWriteBitNum; return false; + } U32 upShift = bitNum & 0x7; *************** *** 167,171 **** if(bitCount + bitNum > maxReadBitNum) { ! error = true; return false; } --- 171,175 ---- if(bitCount + bitNum > maxReadBitNum) { ! setError(); return false; } Index: udp.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/udp.cpp,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** udp.cpp 28 Apr 2005 20:49:36 -0000 1.8 --- udp.cpp 1 May 2005 00:29:55 -0000 1.9 *************** *** 576,581 **** Address theAddress; SocketToTNLAddress((struct sockaddr *) sin, &theAddress); ! if(theAddress.netNum[0] != INADDR_ANY && theAddress.netNum[0] != 0x7F000001) ! { addressVector->push_back(theAddress); } --- 576,582 ---- Address theAddress; SocketToTNLAddress((struct sockaddr *) sin, &theAddress); ! theAddress.port = 0; ! if(theAddress.netNum[0] != INADDR_ANY && theAddress.netNum[0] != 0x7F000001) ! { addressVector->push_back(theAddress); } Index: tnlBitStream.h =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/tnlBitStream.h,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** tnlBitStream.h 28 Apr 2005 18:01:38 -0000 1.9 --- tnlBitStream.h 1 May 2005 00:29:55 -0000 1.10 *************** *** 68,72 **** }; U32 bitNum; ///< The current bit position for reading/writing in the bit stream. ! bool error; ///< Flag set if a user operation attempts to read or write past the max read/write sizes. bool mCompressRelative; ///< Flag set if the bit stream should compress points relative to a compression point. Point3F mCompressPoint; ///< Reference point for relative point compression. --- 68,72 ---- }; U32 bitNum; ///< The current bit position for reading/writing in the bit stream. ! bool mError; ///< Flag set if a user operation attempts to read or write past the max read/write sizes. bool mCompressRelative; ///< Flag set if the bit stream should compress points relative to a compression point. Point3F mCompressPoint; ///< Reference point for relative point compression. *************** *** 114,118 **** /// clears the error state from an attempted read or write overrun ! void clearError() { error = false; } /// Returns a pointer to the next byte in the BitStream from the current bit position --- 114,118 ---- /// clears the error state from an attempted read or write overrun ! void clearError() { mError = false; } /// Returns a pointer to the next byte in the BitStream from the current bit position *************** *** 238,243 **** bool readFlag(); ! bool write(bool value) { writeFlag(value); return !error; } ! bool read(bool *value) { *value = readFlag(); return !error; } /// Writes a huffman compressed string into the stream. --- 238,243 ---- bool readFlag(); ! bool write(bool value) { writeFlag(value); return !mError; } ! bool read(bool *value) { *value = readFlag(); return !mError; } /// Writes a huffman compressed string into the stream. *************** *** 282,286 **** bool isFull() { return bitNum > (getBufferSize() << 3); } /// Returns whether the stream has generated an error condition due to reading or writing past the end of the buffer. ! bool isValid() { return !error; } /// Hashes the BitStream, writing the hash digest into the end of the buffer, and then encrypts with the given cipher --- 282,293 ---- bool isFull() { return bitNum > (getBufferSize() << 3); } /// Returns whether the stream has generated an error condition due to reading or writing past the end of the buffer. ! bool isValid() { return !mError; } ! ! /// Sets the error condition on the bit stream. ! void setError() ! { ! TNLAssert(0, "BitStream error set."); ! mError = true; ! } /// Hashes the BitStream, writing the hash digest into the end of the buffer, and then encrypts with the given cipher *************** *** 340,345 **** if(bitNum > maxReadBitNum) { ! error = true; ! TNLAssert(false, "Out of range read"); return false; } --- 347,351 ---- if(bitNum > maxReadBitNum) { ! setError(); return false; } *************** *** 379,384 **** if(val > rangeEnd) { ! TNLAssert(0, "Value out of range in readRangedU32"); ! error = true; return rangeStart; } --- 385,389 ---- if(val > rangeEnd) { ! setError(); return rangeStart; } Index: netConnection.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/netConnection.cpp,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** netConnection.cpp 28 Apr 2005 20:42:55 -0000 1.14 --- netConnection.cpp 1 May 2005 00:29:55 -0000 1.15 *************** *** 544,547 **** --- 544,551 ---- { mTypeFlags.clear(ConnectionAdaptive); + if(maxRecvBandwidth > MaxFixedBandwidth) + maxRecvBandwidth = MaxFixedBandwidth; + if(maxSendBandwidth > MaxFixedBandwidth) + maxSendBandwidth = MaxFixedBandwidth; mLocalRate.maxRecvBandwidth = maxRecvBandwidth; |
From: Mark F. <mar...@us...> - 2005-05-01 00:30:04
|
Update of /cvsroot/opentnl/tnl/test In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13600/test Modified Files: server.cpp Log Message: Zeroed out port in unix version of getInterfaceAddresses Fixed BitStream overwrite issue Checked parameters on setFixedRateParams on NetConnection Index: server.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/test/server.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** server.cpp 21 Feb 2005 07:48:13 -0000 1.4 --- server.cpp 1 May 2005 00:29:55 -0000 1.5 *************** *** 34,39 **** using namespace TNL; //#define TNLTest_SERVER ! #define TNL_TEST_RPC //#define TNL_TEST_PROTOCOL //#define TNL_TEST_RPC --- 34,40 ---- using namespace TNL; + #define TNL_TEST_BANDWIDTH //#define TNLTest_SERVER ! //#define TNL_TEST_RPC //#define TNL_TEST_PROTOCOL //#define TNL_TEST_RPC *************** *** 58,63 **** } gDedicatedServerLogConsumer; ! #if defined(TNLTest_SERVER) // The TNLTest_SERVER test is an example dedicated server for the --- 59,133 ---- } gDedicatedServerLogConsumer; + #if defined(TNL_TEST_BANDWIDTH) + class RPCTestConnection : public EventConnection + { + public: + static U32 totalRecvd; + TNL_DECLARE_RPC(rpcFoo, (ByteBufferPtr buffer)); + TNL_DECLARE_RPC(rpcGotit, (U32 bytes)); + TNL_DECLARE_NETCONNECTION(RPCTestConnection); + void onConnectionEstablished() + { + setFixedRateParameters(20, 20, 150000, 150000); + } + }; ! TNL_IMPLEMENT_NETCONNECTION(RPCTestConnection, NetClassGroupGame, true); ! ! U32 RPCTestConnection::totalRecvd = 0; ! ! TNL_IMPLEMENT_RPC(RPCTestConnection, rpcFoo, (ByteBufferPtr buffer), (buffer), ! NetClassGroupGameMask, RPCGuaranteedOrdered, RPCDirAny, 0) ! { ! rpcGotit(buffer->getBufferSize()); ! } ! ! TNL_IMPLEMENT_RPC(RPCTestConnection, rpcGotit, (U32 bytes), (bytes), ! NetClassGroupGameMask, RPCGuaranteedOrdered, RPCDirAny, 0) ! { ! totalRecvd += bytes; ! rpcFoo(new ByteBuffer(bytes)); ! } ! ! int main(int argc, const char **argv) ! { ! TNLLogEnable(LogNetInterface, true); ! NetInterface *clientInterface = new NetInterface(Address(IPProtocol, Address::Any, 25000)); ! NetInterface *serverInterface = new NetInterface(Address(IPProtocol, Address::Any, 25001)); ! ! serverInterface->setAllowsConnections(true); ! serverInterface->setRequiresKeyExchange(true); ! ! serverInterface->setPrivateKey(new AsymmetricKey(32)); ! RPCTestConnection *clientConnection = new RPCTestConnection; ! ! Address addr("IP:127.0.0.1:25001"); ! clientConnection->connect(clientInterface, addr, true, false); ! for(S32 i = 0; i < 400; i++) ! clientConnection->rpcFoo(new ByteBuffer(Random::readI(15, 750))); ! ! U32 startTime = Platform::getRealMilliseconds(); ! U32 currentTime = startTime; ! for(;;) ! { ! U32 time = Platform::getRealMilliseconds(); ! ! if(time - currentTime > 1000) ! { ! currentTime = time; ! logprintf("Rate: %d bytes per second, %d total bytes.", ! RPCTestConnection::totalRecvd * 1000 / (currentTime - startTime), RPCTestConnection::totalRecvd); ! } ! ! serverInterface->checkIncomingPackets(); ! serverInterface->processConnections(); ! clientInterface->checkIncomingPackets(); ! clientInterface->processConnections(); ! Platform::sleep(1); ! } ! ! } ! ! #elif defined(TNLTest_SERVER) // The TNLTest_SERVER test is an example dedicated server for the |
From: Mark F. <mar...@us...> - 2005-04-28 20:49:53
|
Update of /cvsroot/opentnl/tnl/tnl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9500/tnl Modified Files: tnlNetInterface.h udp.cpp Log Message: Virtualized addConnection and removeConnection on NetInterface bind() failure now creates an invalid socket Index: udp.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/udp.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** udp.cpp 14 Feb 2005 06:15:28 -0000 1.7 --- udp.cpp 28 Apr 2005 20:49:36 -0000 1.8 *************** *** 237,240 **** --- 237,241 ---- if(mPlatformSocket != INVALID_SOCKET) { + TNLLogMessageV(LogUDP, ("%s socket created.", socketType)); S32 error = 0; SOCKADDR address; *************** *** 243,257 **** TNLToSocketAddress(bindAddress, &address, &addressSize); error = bind(mPlatformSocket, &address, addressSize); ! Address boundAddress; - addressSize = sizeof(address); ! getsockname(mPlatformSocket, (PSOCKADDR) &address, &addressSize); ! SocketToTNLAddress(&address, &boundAddress); ! TNLLogMessageV(LogUDP, ("%s socket created - bound to address: %s", socketType, boundAddress.toString())); - // set the send and receive buffer sizes - error = setsockopt(mPlatformSocket, SOL_SOCKET, SO_RCVBUF, (char *) &recvBufferSize, sizeof(recvBufferSize)); if(!error) { --- 244,264 ---- TNLToSocketAddress(bindAddress, &address, &addressSize); error = bind(mPlatformSocket, &address, addressSize); ! Address boundAddress; ! if(!error) ! { ! addressSize = sizeof(address); ! getsockname(mPlatformSocket, (PSOCKADDR) &address, &addressSize); ! SocketToTNLAddress(&address, &boundAddress); ! TNLLogMessageV(LogUDP, ("%s socket bound to address: %s", socketType, boundAddress.toString())); ! // set the send and receive buffer sizes ! error = setsockopt(mPlatformSocket, SOL_SOCKET, SO_RCVBUF, (char *) &recvBufferSize, sizeof(recvBufferSize)); ! } ! else ! TNLLogMessageV(LogUDP, ("%s socket error: unable to bind the socket to the specified address.", socketType)); ! if(!error) { Index: tnlNetInterface.h =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/tnlNetInterface.h,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** tnlNetInterface.h 3 Jun 2004 00:55:20 -0000 1.5 --- tnlNetInterface.h 28 Apr 2005 20:49:36 -0000 1.6 *************** *** 241,248 **** /// Adds a connection to the internal connection list. ! void addConnection(NetConnection *connection); /// Remove a connection from the list. ! void removeConnection(NetConnection *connection); /// Begins the connection handshaking process for a connection. Called from NetConnection::connect() --- 241,248 ---- /// Adds a connection to the internal connection list. ! virtual void addConnection(NetConnection *connection); /// Remove a connection from the list. ! virtual void removeConnection(NetConnection *connection); /// Begins the connection handshaking process for a connection. Called from NetConnection::connect() |
From: Mark F. <mar...@us...> - 2005-04-28 20:43:15
|
Update of /cvsroot/opentnl/tnl/zap In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5682/zap Modified Files: masterConnection.cpp Log Message: Fixed Zap dedicated server crash when MOTD arrives Symmetric cipher can be created from any size byte buffer (not all zeroes if size isn't exactly right) Arranged NetConnection can now use the arranged secret data as the shared secret data for encryption purposes Index: masterConnection.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/zap/masterConnection.cpp,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** masterConnection.cpp 22 Feb 2005 09:46:36 -0000 1.14 --- masterConnection.cpp 28 Apr 2005 20:42:55 -0000 1.15 *************** *** 142,146 **** conn->connectArranged(getInterface(), fullPossibleAddresses, ! nonce, serverNonce, theSharedData,false); } --- 142,146 ---- conn->connectArranged(getInterface(), fullPossibleAddresses, ! nonce, serverNonce, theSharedData,false, false, false, true); } *************** *** 175,179 **** conn->connectArranged(getInterface(), fullPossibleAddresses, ! nonce, serverNonce, theSharedData,true); } } --- 175,179 ---- conn->connectArranged(getInterface(), fullPossibleAddresses, ! nonce, serverNonce, theSharedData,true, false, false, true); } } *************** *** 191,195 **** TNL_IMPLEMENT_RPC_OVERRIDE(MasterServerConnection, m2cSetMOTD, (StringPtr motdString)) { ! gMainMenuUserInterface.setMOTD(motdString); } --- 191,196 ---- TNL_IMPLEMENT_RPC_OVERRIDE(MasterServerConnection, m2cSetMOTD, (StringPtr motdString)) { ! if(!mIsGameServer) ! gMainMenuUserInterface.setMOTD(motdString); } |
From: Mark F. <mar...@us...> - 2005-04-28 20:43:14
|
Update of /cvsroot/opentnl/tnl/tnl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5682/tnl Modified Files: netConnection.cpp netInterface.cpp symmetricCipher.cpp tnlNetConnection.h Log Message: Fixed Zap dedicated server crash when MOTD arrives Symmetric cipher can be created from any size byte buffer (not all zeroes if size isn't exactly right) Arranged NetConnection can now use the arranged secret data as the shared secret data for encryption purposes Index: symmetricCipher.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/symmetricCipher.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** symmetricCipher.cpp 28 Apr 2005 17:30:30 -0000 1.3 --- symmetricCipher.cpp 28 Apr 2005 20:42:55 -0000 1.4 *************** *** 41,52 **** } SymmetricCipher::SymmetricCipher(const ByteBuffer *theByteBuffer) { if(theByteBuffer->getBufferSize() != KeySize * 2) { ! U8 buffer[KeySize]; ! memset(buffer, 0, KeySize); rijndael_setup(buffer, KeySize, 0, (symmetric_key *) &mSymmetricKey); ! memcpy(mInitVector, buffer, BlockSize); } else --- 41,72 ---- } + static void copyWrapBuffer(U8 *destBuffer, U32 destSize, const U8 *srcBuffer, U32 srcSize) + { + if(!srcSize) + { + memset(destBuffer, 0, destSize); + return; + } + U32 copyPos = 0; + while(copyPos < destSize) + { + U32 copyBytes = getMin(destSize - copyPos, srcSize); + memcpy(destBuffer + copyPos, srcBuffer, copyBytes); + copyPos += copyBytes; + } + } + SymmetricCipher::SymmetricCipher(const ByteBuffer *theByteBuffer) { + // if the buffer passed isn't exactly the right size, + // wrap copy it into the dest buffer and init vector. if(theByteBuffer->getBufferSize() != KeySize * 2) { ! U8 buffer[KeySize * 2]; ! memset(buffer, 0, KeySize * 2); ! memcpy(buffer, theByteBuffer->getBuffer(), getMin(U32(KeySize * 2), theByteBuffer->getBufferSize())); ! rijndael_setup(buffer, KeySize, 0, (symmetric_key *) &mSymmetricKey); ! memcpy(mInitVector, buffer + KeySize, BlockSize); } else Index: tnlNetConnection.h =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/tnlNetConnection.h,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** tnlNetConnection.h 21 Feb 2005 07:48:14 -0000 1.10 --- tnlNetConnection.h 28 Apr 2005 20:42:55 -0000 1.11 *************** *** 92,95 **** --- 92,96 ---- bool mIsArranged; ///< True if this is an arranged connection bool mUsingCrypto; ///< Set to true if this connection is using crypto (public key and symmetric) + bool mUseArrangedSecretAsSharedSecret; ///< Set to true if the arranged connection secret data should be used as the symmetric key for symmetric crypto bool mPuzzleRetried; ///< True if a puzzle solution was already rejected by the server once. Nonce mNonce; ///< Unique nonce generated for this connection to send to the server. *************** *** 117,120 **** --- 118,122 ---- mPuzzleRetried = false; mUsingCrypto = false; + mUseArrangedSecretAsSharedSecret = false; mIsArranged = false; #ifdef TNL_DEBUG *************** *** 335,339 **** /// Connects to a remote host that is also connecting to this connection (negotiated by a third party) ! void connectArranged(NetInterface *connectionInterface, const Vector<Address> &possibleAddresses, Nonce &myNonce, Nonce &remoteNonce, ByteBufferPtr sharedSecret, bool isInitiator, bool requestsKeyExchange = false, bool requestsCertificate = false); /// Sends a disconnect packet to notify the remote host that this side is terminating the connection for the specified reason. --- 337,341 ---- /// Connects to a remote host that is also connecting to this connection (negotiated by a third party) ! void connectArranged(NetInterface *connectionInterface, const Vector<Address> &possibleAddresses, Nonce &myNonce, Nonce &remoteNonce, ByteBufferPtr sharedSecret, bool isInitiator, bool requestsKeyExchange = false, bool requestsCertificate = false, bool useArrangedSecretAsSharedSecret = false); /// Sends a disconnect packet to notify the remote host that this side is terminating the connection for the specified reason. Index: netConnection.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/netConnection.cpp,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** netConnection.cpp 21 Feb 2005 07:48:14 -0000 1.13 --- netConnection.cpp 28 Apr 2005 20:42:55 -0000 1.14 *************** *** 799,803 **** } ! void NetConnection::connectArranged(NetInterface *connectionInterface, const Vector<Address> &possibleAddresses, Nonce &nonce, Nonce &serverNonce, ByteBufferPtr sharedSecret, bool isInitiator, bool requestsKeyExchange, bool requestsCertificate) { mConnectionParameters.mRequestKeyExchange = requestsKeyExchange; --- 799,803 ---- } ! void NetConnection::connectArranged(NetInterface *connectionInterface, const Vector<Address> &possibleAddresses, Nonce &nonce, Nonce &serverNonce, ByteBufferPtr sharedSecret, bool isInitiator, bool requestsKeyExchange, bool requestsCertificate, bool useArrangedSecretAsSharedSecret) { mConnectionParameters.mRequestKeyExchange = requestsKeyExchange; *************** *** 810,813 **** --- 810,816 ---- mConnectionParameters.mArrangedSecret = sharedSecret; mConnectionParameters.mArrangedSecret->takeOwnership(); + mConnectionParameters.mUseArrangedSecretAsSharedSecret = useArrangedSecretAsSharedSecret; + if(useArrangedSecretAsSharedSecret) + mConnectionParameters.mSharedSecret = mConnectionParameters.mArrangedSecret; setInterface(connectionInterface); Index: netInterface.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/netInterface.cpp,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** netInterface.cpp 28 Apr 2005 17:30:30 -0000 1.10 --- netInterface.cpp 28 Apr 2005 20:42:55 -0000 1.11 *************** *** 1076,1084 **** else theParams.mPrivateKey = mPrivateKey; theParams.mSharedSecret = theParams.mPrivateKey->computeSharedSecretKey(theParams.mPublicKey); //logprintf("shared secret (client) %s", theParams.mSharedSecret->encodeBase64()->getBuffer()); Random::read(theParams.mSymmetricKey, SymmetricCipher::KeySize); - theParams.mUsingCrypto = true; } conn->setNetAddress(theAddress); TNLLogMessageV(LogNetInterface, ("Punch from %s matched nonces - connecting...", theAddress.toString())); --- 1076,1091 ---- else theParams.mPrivateKey = mPrivateKey; + theParams.mUsingCrypto = true; theParams.mSharedSecret = theParams.mPrivateKey->computeSharedSecretKey(theParams.mPublicKey); + } + else if(theParams.mUseArrangedSecretAsSharedSecret) + theParams.mUsingCrypto = true; + + if(theParams.mUsingCrypto) + { //logprintf("shared secret (client) %s", theParams.mSharedSecret->encodeBase64()->getBuffer()); Random::read(theParams.mSymmetricKey, SymmetricCipher::KeySize); } + conn->setNetAddress(theAddress); TNLLogMessageV(LogNetInterface, ("Punch from %s matched nonces - connecting...", theAddress.toString())); *************** *** 1108,1114 **** if(out.writeFlag(theParams.mUsingCrypto)) { ! out.write(theParams.mPrivateKey->getPublicKey()); ! innerEncryptPos = out.getBytePosition(); ! out.setBytePosition(innerEncryptPos); out.write(SymmetricCipher::KeySize, theParams.mSymmetricKey); } --- 1115,1127 ---- if(out.writeFlag(theParams.mUsingCrypto)) { ! if(!theParams.mUseArrangedSecretAsSharedSecret) ! { ! // if this arranged connection doesn't already have the shared ! // secret (from the arranged secret), send the public key ! // to the remote host. ! out.write(theParams.mPrivateKey->getPublicKey()); ! innerEncryptPos = out.getBytePosition(); ! out.setBytePosition(innerEncryptPos); ! } out.write(SymmetricCipher::KeySize, theParams.mSymmetricKey); } *************** *** 1187,1207 **** if(stream->readFlag()) { - if(mPrivateKey.isNull()) - return; theParams.mUsingCrypto = true; ! theParams.mPublicKey = new AsymmetricKey(stream); ! theParams.mPrivateKey = mPrivateKey; ! ! U32 decryptPos = stream->getBytePosition(); ! stream->setBytePosition(decryptPos); ! theParams.mSharedSecret = theParams.mPrivateKey->computeSharedSecretKey(theParams.mPublicKey); ! SymmetricCipher theCipher(theParams.mSharedSecret); ! ! if(!stream->decryptAndCheckHash(NetConnection::MessageSignatureBytes, decryptPos, &theCipher)) ! return; // now read the first part of the connection's session (symmetric) key stream->read(SymmetricCipher::KeySize, theParams.mSymmetricKey); Random::read(theParams.mInitVector, SymmetricCipher::KeySize); } --- 1200,1228 ---- if(stream->readFlag()) { theParams.mUsingCrypto = true; ! if(!theParams.mUseArrangedSecretAsSharedSecret) ! { ! if(mPrivateKey.isNull()) ! return; ! theParams.mPublicKey = new AsymmetricKey(stream); ! theParams.mPrivateKey = mPrivateKey; + U32 decryptPos = stream->getBytePosition(); + stream->setBytePosition(decryptPos); + theParams.mSharedSecret = theParams.mPrivateKey->computeSharedSecretKey(theParams.mPublicKey); + SymmetricCipher theCipher(theParams.mSharedSecret); + + if(!stream->decryptAndCheckHash(NetConnection::MessageSignatureBytes, decryptPos, &theCipher)) + return; + } // now read the first part of the connection's session (symmetric) key stream->read(SymmetricCipher::KeySize, theParams.mSymmetricKey); + } + if(theParams.mUsingCrypto) + { + // if this connection is using crypto, it is the connection + // receiver who computes the InitVector for the symmetric cipher. Random::read(theParams.mInitVector, SymmetricCipher::KeySize); + conn->setSymmetricCipher(new SymmetricCipher(theParams.mSymmetricKey, theParams.mInitVector)); } *************** *** 1216,1221 **** conn->setNetAddress(theAddress); conn->setInitialRecvSequence(connectSequence); - if(theParams.mUsingCrypto) - conn->setSymmetricCipher(new SymmetricCipher(theParams.mSymmetricKey, theParams.mInitVector)); const char *errorString = NULL; --- 1237,1240 ---- |
From: Mark F. <mar...@us...> - 2005-04-28 18:01:50
|
Update of /cvsroot/opentnl/tnl/tnl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15416/tnl Modified Files: eventConnection.cpp tnlBitStream.h tnlEventConnection.h Log Message: Added a packet fill fraction for EventConnection -- so that events can be set to only take up some fixed fraction of each packet, so as to not starve the ghosting Index: eventConnection.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/eventConnection.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** eventConnection.cpp 21 Feb 2005 07:48:13 -0000 1.7 --- eventConnection.cpp 28 Apr 2005 18:01:37 -0000 1.8 *************** *** 51,54 **** --- 51,55 ---- mEventClassCount = 0; mEventClassBitSize = 0; + mPacketFillFraction = 1.0f; } *************** *** 240,246 **** EventNote *packQueueHead = NULL, *packQueueTail = NULL; while(mUnorderedSendEventQueueHead) { ! if(bstream->isFull()) break; // get the first event --- 241,248 ---- EventNote *packQueueHead = NULL, *packQueueTail = NULL; + F32 totalPacketSpaceFraction = 1.0f / bstream->getMaxWriteBitPosition(); while(mUnorderedSendEventQueueHead) { ! if(bstream->isFull() || bstream->getBitPosition() * totalPacketSpaceFraction > mPacketFillFraction) break; // get the first event Index: tnlBitStream.h =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/tnlBitStream.h,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** tnlBitStream.h 28 Apr 2005 17:30:30 -0000 1.8 --- tnlBitStream.h 28 Apr 2005 18:01:38 -0000 1.9 *************** *** 133,136 **** --- 133,139 ---- U32 getMaxReadBitPosition() const { return maxReadBitNum; } + /// Returns the maximum writable bit position + U32 getMaxWriteBitPosition() const { return maxWriteBitNum; } + /// Returns the number of bits that can be written into the BitStream without resizing U32 getBitSpaceAvailable() const { return maxWriteBitNum - bitNum; } Index: tnlEventConnection.h =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/tnlEventConnection.h,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** tnlEventConnection.h 21 Feb 2005 07:48:14 -0000 1.4 --- tnlEventConnection.h 28 Apr 2005 18:01:38 -0000 1.5 *************** *** 127,130 **** --- 127,131 ---- protected: + F32 mPacketFillFraction; ///< Percentage of each packet to fill with NetEvents. Defaults to 1.0 U32 mEventClassCount; ///< Number of NetEvent classes supported by this connection U32 mEventClassBitSize; ///< Bit field width of NetEvent class count. *************** *** 147,150 **** --- 148,157 ---- bool readConnectAccept(BitStream *stream, const char **errorString); public: + /// Sets the amount of each packet that should be filled with NetEvents (if there are any). + /// Defaults to 1.0 meaning that if there are enough events, it will fill the entire packet with + /// event data. This can be lowered during normal simulation to allow the GhostConnection to have + /// some space to send ghost updates. + inline void setPacketFillFraction(F32 fraction) { mPacketFillFraction = fraction; } + /// returns the highest event version number supported on this connection. U32 getEventClassVersion() { return mEventClassVersion; } |
Update of /cvsroot/opentnl/tnl/tnl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29044/tnl Modified Files: asymmetricKey.cpp bitStream.cpp byteBuffer.cpp clientPuzzle.cpp netInterface.cpp random.cpp symmetricCipher.cpp tnl.vcproj tnlBitStream.h Log Message: Updated TNL to build with new libtomcrypt Added number in range checking on BitStream::readRangedU32 Index: random.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/random.cpp,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** random.cpp 14 Feb 2005 06:15:28 -0000 1.5 --- random.cpp 28 Apr 2005 17:30:30 -0000 1.6 *************** *** 26,30 **** ! #include <mycrypt.h> #include "tnl.h" #include "tnlRandom.h" --- 26,30 ---- ! #include "tomcrypt.h" #include "tnl.h" #include "tnlRandom.h" Index: byteBuffer.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/byteBuffer.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** byteBuffer.cpp 14 Feb 2005 06:15:25 -0000 1.3 --- byteBuffer.cpp 28 Apr 2005 17:30:30 -0000 1.4 *************** *** 26,30 **** #include "tnlByteBuffer.h" ! #include <mycrypt.h> namespace TNL { --- 26,30 ---- #include "tnlByteBuffer.h" ! #include "tomcrypt.h" namespace TNL { Index: asymmetricKey.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/asymmetricKey.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** asymmetricKey.cpp 20 Apr 2004 04:27:18 -0000 1.1 --- asymmetricKey.cpp 28 Apr 2005 17:30:30 -0000 1.2 *************** *** 27,31 **** #include "tnlAsymmetricKey.h" #include "tnlRandom.h" ! #include <mycrypt.h> #include "tnlLog.h" --- 27,31 ---- #include "tnlAsymmetricKey.h" #include "tnlRandom.h" ! #include "tomcrypt.h" #include "tnlLog.h" Index: netInterface.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/netInterface.cpp,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** netInterface.cpp 14 Feb 2005 06:15:28 -0000 1.9 --- netInterface.cpp 28 Apr 2005 17:30:30 -0000 1.10 *************** *** 34,38 **** #include "tnlClientPuzzle.h" #include "tnlCertificate.h" ! #include <mycrypt.h> namespace TNL { --- 34,38 ---- #include "tnlClientPuzzle.h" #include "tnlCertificate.h" ! #include "tomcrypt.h" namespace TNL { Index: clientPuzzle.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/clientPuzzle.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** clientPuzzle.cpp 23 Apr 2004 22:02:38 -0000 1.4 --- clientPuzzle.cpp 28 Apr 2005 17:30:30 -0000 1.5 *************** *** 29,33 **** #include "tnlRandom.h" ! #include <mycrypt.h> namespace TNL { --- 29,33 ---- #include "tnlRandom.h" ! #include "tomcrypt.h" namespace TNL { Index: bitStream.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/bitStream.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** bitStream.cpp 21 Feb 2005 07:48:13 -0000 1.7 --- bitStream.cpp 28 Apr 2005 17:30:30 -0000 1.8 *************** *** 30,34 **** #include "tnlHuffmanStringProcessor.h" #include "tnlSymmetricCipher.h" ! #include <mycrypt.h> #include <math.h> --- 30,34 ---- #include "tnlHuffmanStringProcessor.h" #include "tnlSymmetricCipher.h" ! #include "tomcrypt.h" #include <math.h> Index: tnl.vcproj =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/tnl.vcproj,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** tnl.vcproj 14 Feb 2005 06:15:28 -0000 1.6 --- tnl.vcproj 28 Apr 2005 17:30:30 -0000 1.7 *************** *** 22,26 **** Name="VCCLCompilerTool" Optimization="0" ! AdditionalIncludeDirectories="../libtomcrypt" PreprocessorDefinitions="TNL_DEBUG;TNL_ENABLE_LOGGING;WIN32;_DEBUG;_LIB;" BasicRuntimeChecks="3" --- 22,26 ---- Name="VCCLCompilerTool" Optimization="0" ! AdditionalIncludeDirectories="../libtomcrypt/headers" PreprocessorDefinitions="TNL_DEBUG;TNL_ENABLE_LOGGING;WIN32;_DEBUG;_LIB;" BasicRuntimeChecks="3" Index: tnlBitStream.h =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/tnlBitStream.h,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** tnlBitStream.h 21 Feb 2005 07:48:14 -0000 1.7 --- tnlBitStream.h 28 Apr 2005 17:30:30 -0000 1.8 *************** *** 373,378 **** U32 rangeBits = getNextBinLog2(rangeSize); ! U32 val = U32(readInt(S32(rangeBits))); ! return val + rangeStart; } --- 373,384 ---- U32 rangeBits = getNextBinLog2(rangeSize); ! U32 val = U32(readInt(S32(rangeBits))) + rangeStart; ! if(val > rangeEnd) ! { ! TNLAssert(0, "Value out of range in readRangedU32"); ! error = true; ! return rangeStart; ! } ! return val; } Index: symmetricCipher.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/symmetricCipher.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** symmetricCipher.cpp 14 Feb 2005 06:15:28 -0000 1.2 --- symmetricCipher.cpp 28 Apr 2005 17:30:30 -0000 1.3 *************** *** 28,32 **** #include "tnlSymmetricCipher.h" #include "tnlByteBuffer.h" ! #include "mycrypt.h" namespace TNL { --- 28,32 ---- #include "tnlSymmetricCipher.h" #include "tnlByteBuffer.h" ! #include "tomcrypt.h" namespace TNL { |
From: Mark F. <mar...@us...> - 2005-04-28 17:29:38
|
Update of /cvsroot/opentnl/tnl/libtomcrypt In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28197/libtomcrypt Added Files: makefile Log Message: Added linux makefile --- NEW FILE: makefile --- # MAKEFILE for linux GCC # # Tom St Denis # Modified by Clay Culver # The version VERSION=1.02 # Compiler and Linker Names #CC=gcc #LD=ld # Archiver [makes .a files] #AR=ar #ARFLAGS=r # Compilation flags. Note the += does not write over the user's CFLAGS! CFLAGS += -c -I./testprof/ -I./headers/ -Wall -Wsign-compare -W -Wshadow -Wno-unused-parameter # additional warnings (newer GCC 3.4 and higher) #CFLAGS += -Wsystem-headers -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align -Wstrict-prototypes -Wmissing-prototypes \ # -Wmissing-declarations -Wpointer-arith # optimize for SPEED CFLAGS += -O3 -funroll-loops # add -fomit-frame-pointer. hinders debugging! CFLAGS += -fomit-frame-pointer # optimize for SIZE #CFLAGS += -Os -DLTC_SMALL_CODE # older GCCs can't handle the "rotate with immediate" ROLc/RORc/etc macros # define this to help #CFLAGS += -DLTC_NO_ROLC # compile for DEBUGING (required for ccmalloc checking!!!) #CFLAGS += -g3 -DLTC_NO_ASM #Output filenames for various targets. LIBNAME=libtomcrypt.a LIBTEST=testprof/libtomcrypt_prof.a HASH=hashsum CRYPT=encrypt SMALL=small PROF=x86_prof TV=tv_gen MULTI=multi TIMING=timing TEST=test #LIBPATH-The directory for libtomcrypt to be installed to. #INCPATH-The directory to install the header files for libtomcrypt. #DATAPATH-The directory to install the pdf docs. DESTDIR= LIBPATH=/usr/lib INCPATH=/usr/include DATAPATH=/usr/share/doc/libtomcrypt/pdf #Who do we install as? USER=root GROUP=wheel #List of objects to compile. #Leave MPI built-in or force developer to link against libtommath? MPIOBJECT=misc/mpi/mpi.o OBJECTS=ciphers/aes/aes_enc.o $(MPIOBJECT) ciphers/aes/aes.o ciphers/anubis.o \ ciphers/blowfish.o ciphers/cast5.o ciphers/des.o ciphers/khazad.o ciphers/noekeon.o \ ciphers/rc2.o ciphers/rc5.o ciphers/rc6.o ciphers/safer/safer.o \ ciphers/safer/safer_tab.o ciphers/safer/saferp.o ciphers/skipjack.o \ ciphers/twofish/twofish.o ciphers/xtea.o encauth/ccm/ccm_memory.o \ encauth/ccm/ccm_test.o encauth/eax/eax_addheader.o encauth/eax/eax_decrypt.o \ encauth/eax/eax_decrypt_verify_memory.o encauth/eax/eax_done.o encauth/eax/eax_encrypt.o \ encauth/eax/eax_encrypt_authenticate_memory.o encauth/eax/eax_init.o \ encauth/eax/eax_test.o encauth/gcm/gcm_add_aad.o encauth/gcm/gcm_add_iv.o \ encauth/gcm/gcm_done.o encauth/gcm/gcm_gf_mult.o encauth/gcm/gcm_init.o \ encauth/gcm/gcm_memory.o encauth/gcm/gcm_process.o encauth/gcm/gcm_reset.o \ encauth/gcm/gcm_test.o encauth/ocb/ocb_decrypt.o encauth/ocb/ocb_decrypt_verify_memory.o \ encauth/ocb/ocb_done_decrypt.o encauth/ocb/ocb_done_encrypt.o encauth/ocb/ocb_encrypt.o \ encauth/ocb/ocb_encrypt_authenticate_memory.o encauth/ocb/ocb_init.o encauth/ocb/ocb_ntz.o \ encauth/ocb/ocb_shift_xor.o encauth/ocb/ocb_test.o encauth/ocb/s_ocb_done.o \ hashes/chc/chc.o hashes/helper/hash_file.o hashes/helper/hash_filehandle.o \ hashes/helper/hash_memory.o hashes/helper/hash_memory_multi.o hashes/md2.o hashes/md4.o \ hashes/md5.o hashes/rmd128.o hashes/rmd160.o hashes/sha1.o hashes/sha2/sha256.o \ hashes/sha2/sha512.o hashes/tiger.o hashes/whirl/whirl.o mac/hmac/hmac_done.o \ mac/hmac/hmac_file.o mac/hmac/hmac_init.o mac/hmac/hmac_memory.o \ mac/hmac/hmac_memory_multi.o mac/hmac/hmac_process.o mac/hmac/hmac_test.o \ mac/omac/omac_done.o mac/omac/omac_file.o mac/omac/omac_init.o mac/omac/omac_memory.o \ mac/omac/omac_memory_multi.o mac/omac/omac_process.o mac/omac/omac_test.o \ mac/pelican/pelican.o mac/pelican/pelican_memory.o mac/pelican/pelican_test.o \ mac/pmac/pmac_done.o mac/pmac/pmac_file.o mac/pmac/pmac_init.o mac/pmac/pmac_memory.o \ mac/pmac/pmac_memory_multi.o mac/pmac/pmac_ntz.o mac/pmac/pmac_process.o \ mac/pmac/pmac_shift_xor.o mac/pmac/pmac_test.o misc/base64/base64_decode.o \ misc/base64/base64_encode.o misc/burn_stack.o misc/crypt/crypt.o \ misc/crypt/crypt_argchk.o misc/crypt/crypt_cipher_descriptor.o \ misc/crypt/crypt_cipher_is_valid.o misc/crypt/crypt_find_cipher.o \ misc/crypt/crypt_find_cipher_any.o misc/crypt/crypt_find_cipher_id.o \ misc/crypt/crypt_find_hash.o misc/crypt/crypt_find_hash_any.o \ misc/crypt/crypt_find_hash_id.o misc/crypt/crypt_find_prng.o \ misc/crypt/crypt_hash_descriptor.o misc/crypt/crypt_hash_is_valid.o \ misc/crypt/crypt_prng_descriptor.o misc/crypt/crypt_prng_is_valid.o \ misc/crypt/crypt_register_cipher.o misc/crypt/crypt_register_hash.o \ misc/crypt/crypt_register_prng.o misc/crypt/crypt_unregister_cipher.o \ misc/crypt/crypt_unregister_hash.o misc/crypt/crypt_unregister_prng.o \ misc/error_to_string.o misc/mpi/is_prime.o misc/mpi/mpi_to_ltc_error.o \ misc/mpi/rand_prime.o misc/pkcs5/pkcs_5_1.o misc/pkcs5/pkcs_5_2.o misc/zeromem.o \ modes/cbc/cbc_decrypt.o modes/cbc/cbc_done.o modes/cbc/cbc_encrypt.o \ modes/cbc/cbc_getiv.o modes/cbc/cbc_setiv.o modes/cbc/cbc_start.o \ modes/cfb/cfb_decrypt.o modes/cfb/cfb_done.o modes/cfb/cfb_encrypt.o \ modes/cfb/cfb_getiv.o modes/cfb/cfb_setiv.o modes/cfb/cfb_start.o \ modes/ctr/ctr_decrypt.o modes/ctr/ctr_done.o modes/ctr/ctr_encrypt.o \ modes/ctr/ctr_getiv.o modes/ctr/ctr_setiv.o modes/ctr/ctr_start.o \ modes/ecb/ecb_decrypt.o modes/ecb/ecb_done.o modes/ecb/ecb_encrypt.o \ modes/ecb/ecb_start.o modes/ofb/ofb_decrypt.o modes/ofb/ofb_done.o \ modes/ofb/ofb_encrypt.o modes/ofb/ofb_getiv.o modes/ofb/ofb_setiv.o \ modes/ofb/ofb_start.o pk/asn1/der/der_decode_integer.o pk/asn1/der/der_encode_integer.o \ pk/asn1/der/der_get_multi_integer.o pk/asn1/der/der_length_integer.o \ pk/asn1/der/der_put_multi_integer.o pk/dh/dh.o pk/dsa/dsa_export.o pk/dsa/dsa_free.o \ pk/dsa/dsa_import.o pk/dsa/dsa_make_key.o pk/dsa/dsa_sign_hash.o \ pk/dsa/dsa_verify_hash.o pk/dsa/dsa_verify_key.o pk/ecc/ecc.o pk/packet_store_header.o \ pk/packet_valid_header.o pk/pkcs1/pkcs_1_i2osp.o pk/pkcs1/pkcs_1_mgf1.o \ pk/pkcs1/pkcs_1_oaep_decode.o pk/pkcs1/pkcs_1_oaep_encode.o pk/pkcs1/pkcs_1_os2ip.o \ pk/pkcs1/pkcs_1_pss_decode.o pk/pkcs1/pkcs_1_pss_encode.o pk/pkcs1/pkcs_1_v15_es_decode.o \ pk/pkcs1/pkcs_1_v15_es_encode.o pk/pkcs1/pkcs_1_v15_sa_decode.o \ pk/pkcs1/pkcs_1_v15_sa_encode.o pk/rsa/rsa_decrypt_key.o pk/rsa/rsa_encrypt_key.o \ pk/rsa/rsa_export.o pk/rsa/rsa_exptmod.o pk/rsa/rsa_free.o pk/rsa/rsa_import.o \ pk/rsa/rsa_make_key.o pk/rsa/rsa_sign_hash.o pk/rsa/rsa_v15_decrypt_key.o \ pk/rsa/rsa_v15_encrypt_key.o pk/rsa/rsa_v15_sign_hash.o pk/rsa/rsa_v15_verify_hash.o \ pk/rsa/rsa_verify_hash.o prngs/fortuna.o prngs/rc4.o prngs/rng_get_bytes.o \ prngs/rng_make_prng.o prngs/sober128.o prngs/sprng.o prngs/yarrow.o HEADERS=headers/tommath_superclass.h headers/tomcrypt_cfg.h \ headers/tomcrypt_mac.h headers/tomcrypt_macros.h \ headers/tomcrypt_custom.h headers/tomcrypt_argchk.h \ headers/tomcrypt_cipher.h headers/tomcrypt_pk.h \ headers/tommath_class.h headers/ltc_tommath.h headers/tomcrypt_hash.h \ headers/tomcrypt_misc.h headers/tomcrypt.h headers/tomcrypt_pkcs.h \ headers/tomcrypt_prng.h testprof/tomcrypt_test.h TESTOBJECTS=demos/test.o HASHOBJECTS=demos/hashsum.o CRYPTOBJECTS=demos/encrypt.o SMALLOBJECTS=demos/small.o TVS=demos/tv_gen.o MULTIS=demos/multi.o TIMINGS=demos/timing.o TESTS=demos/test.o #Files left over from making the crypt.pdf. LEFTOVERS=*.dvi *.log *.aux *.toc *.idx *.ilg *.ind *.out #Compressed filenames COMPRESSED=crypt-$(VERSION).tar.bz2 crypt-$(VERSION).zip #The default rule for make builds the libtomcrypt library. default:library #ciphers come in two flavours... enc+dec and enc ciphers/aes/aes_enc.o: ciphers/aes/aes.c ciphers/aes/aes_tab.c $(CC) $(CFLAGS) -DENCRYPT_ONLY -c ciphers/aes/aes.c -o ciphers/aes/aes_enc.o #These are the rules to make certain object files. ciphers/aes/aes.o: ciphers/aes/aes.c ciphers/aes/aes_tab.c ciphers/twofish/twofish.o: ciphers/twofish/twofish.c ciphers/twofish/twofish_tab.c hashes/whirl/whirl.o: hashes/whirl/whirl.c hashes/whirl/whirltab.c pk/ecc/ecc.o: pk/ecc/ecc.c pk/ecc/ecc_sys.c pk/dh/dh.o: pk/dh/dh.c pk/dh/dh_sys.c hashes/sha2/sha512.o: hashes/sha2/sha512.c hashes/sha2/sha384.c hashes/sha2/sha256.o: hashes/sha2/sha256.c hashes/sha2/sha224.c #This rule makes the libtomcrypt library. library: $(LIBTEST) $(LIBNAME) $(LIBTEST): cd testprof ; CFLAGS="$(CFLAGS)" make $(LIBNAME): $(OBJECTS) $(AR) $(ARFLAGS) $@ $(OBJECTS) ranlib $(LIBNAME) #This rule makes the hash program included with libtomcrypt hashsum: library $(HASHOBJECTS) $(CC) $(HASHOBJECTS) $(LIBNAME) -o $(HASH) $(WARN) #makes the crypt program crypt: library $(CRYPTOBJECTS) $(CC) $(CRYPTOBJECTS) $(LIBNAME) -o $(CRYPT) $(WARN) #makes the small program small: library $(SMALLOBJECTS) $(CC) $(SMALLOBJECTS) $(LIBNAME) -o $(SMALL) $(WARN) tv_gen: library $(TVS) $(CC) $(TVS) $(LIBNAME) $(EXTRALIBS) -o $(TV) multi: library $(MULTIS) $(CC) $(MULTIS) $(LIBNAME) -o $(MULTI) timing: library $(TIMINGS) $(CC) $(TIMINGS) $(LIBTEST) $(LIBNAME) -o $(TIMING) test: library $(TESTS) $(CC) $(TESTS) $(LIBTEST) $(LIBNAME) -o $(TEST) #This rule installs the library and the header files. This must be run #as root in order to have a high enough permission to write to the correct #directories and to set the owner and group to root. install: library docs install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(LIBPATH) install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(INCPATH) install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(DATAPATH) install -g $(GROUP) -o $(USER) $(LIBNAME) $(DESTDIR)$(LIBPATH) install -g $(GROUP) -o $(USER) $(HEADERS) $(DESTDIR)$(INCPATH) install -g $(GROUP) -o $(USER) doc/crypt.pdf $(DESTDIR)$(DATAPATH) install_lib: library install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(LIBPATH) install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(INCPATH) install -g $(GROUP) -o $(USER) $(LIBNAME) $(DESTDIR)$(LIBPATH) install -g $(GROUP) -o $(USER) $(HEADERS) $(DESTDIR)$(INCPATH) #This rule cleans the source tree of all compiled code, not including the pdf #documentation. clean: rm -f `find . -type f | grep "[.]o" | xargs` rm -f `find . -type f | grep "[.]lo" | xargs` rm -f `find . -type f | grep "[.]a" | xargs` rm -f `find . -type f | grep "[.]la" | xargs` rm -f `find . -type f | grep "[.]obj" | xargs` rm -f `find . -type f | grep "[.]lib" | xargs` rm -f `find . -type f | grep "[.]exe" | xargs` rm -f `find . -type f | grep "[.]gcda" | xargs` rm -f `find . -type f | grep "[.]gcno" | xargs` rm -f `find . -type f | grep "[.]il" | xargs` rm -f `find . -type f | grep "[.]dyn" | xargs` rm -f `find . -type f | grep "[.]dpi" | xargs` rm -rf `find . -type d | grep "[.]libs" | xargs` rm -f crypt.aux crypt.dvi crypt.idx crypt.ilg crypt.ind crypt.log crypt.toc rm -f $(TV) $(PROF) $(SMALL) $(CRYPT) $(HASHSUM) $(MULTI) $(TIMING) $(TEST) rm -rf doc/doxygen rm -f doc/*.pdf #build the doxy files (requires Doxygen, tetex and patience) doxy: doxygen cd doc/doxygen/latex ; make ; mv -f refman.pdf ../../. echo The huge doxygen PDF should be available as doc/refman.pdf #This builds the crypt.pdf file. Note that the rm -f *.pdf has been removed #from the clean command! This is because most people would like to keep the #nice pre-compiled crypt.pdf that comes with libtomcrypt! We only need to #delete it if we are rebuilding it. docs: crypt.tex rm -f doc/crypt.pdf $(LEFTOVERS) echo "hello" > crypt.ind latex crypt > /dev/null latex crypt > /dev/null makeindex crypt.idx > /dev/null latex crypt > /dev/null dvipdf crypt mv -ivf crypt.pdf doc/crypt.pdf rm -f $(LEFTOVERS) docdvi: crypt.tex echo hello > crypt.ind latex crypt > /dev/null latex crypt > /dev/null makeindex crypt.idx latex crypt > /dev/null #zipup the project (take that!) no_oops: clean cd .. ; cvs commit zipup: no_oops docs cd .. ; rm -rf crypt* libtomcrypt-$(VERSION) ; mkdir libtomcrypt-$(VERSION) ; \ cp -R ./libtomcrypt/* ./libtomcrypt-$(VERSION)/ ; \ cd libtomcrypt-$(VERSION) ; rm -rf `find . -type d | grep CVS | xargs` ; cd .. ; \ tar -cjvf crypt-$(VERSION).tar.bz2 libtomcrypt-$(VERSION) ; \ zip -9r crypt-$(VERSION).zip libtomcrypt-$(VERSION) ; \ gpg -b -a crypt-$(VERSION).tar.bz2 ; gpg -b -a crypt-$(VERSION).zip ; \ mv -fv crypt* ~ ; rm -rf libtomcrypt-$(VERSION) |
Update of /cvsroot/opentnl/tnl/libtomcrypt In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt Modified Files: libtomcrypt.vcproj Removed Files: aes.c aes_tab.c authors base64.c bits.c blowfish.c cast5.c cbc.c cfb.c crypt.c crypt.pdf ctr.c des.c dh.c dh_sys.c dsa.c ecb.c ecc.c ecc_sys.c gf.c hash.c hmac.c keyring.c makefile md2.c md4.c md5.c mem.c mpi.c mycrypt.h mycrypt_argchk.h mycrypt_cfg.h mycrypt_cipher.h mycrypt_custom.h mycrypt_gf.h mycrypt_hash.h mycrypt_kr.h mycrypt_macros.h mycrypt_misc.h mycrypt_pk.h mycrypt_prng.h noekeon.c ofb.c omac.c packet.c prime.c rc2.c rc4.c rc5.c rc6.c rmd128.c rmd160.c rsa.c rsa_sys.c safer+.c safer.c safer_tab.c sha1.c sha224.c sha256.c sha384.c sha512.c skipjack.c sprng.c strings.c tiger.c tommath.h twofish.c twofish_tab.c xtea.c yarrow.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- sha512.c DELETED --- --- twofish_tab.c DELETED --- --- rc5.c DELETED --- --- aes.c DELETED --- --- hmac.c DELETED --- --- mycrypt_argchk.h DELETED --- --- safer+.c DELETED --- --- mycrypt_gf.h DELETED --- --- ecb.c DELETED --- --- cast5.c DELETED --- --- tiger.c DELETED --- --- crypt.c DELETED --- --- cfb.c DELETED --- --- sha384.c DELETED --- --- cbc.c DELETED --- --- crypt.pdf DELETED --- --- sha256.c DELETED --- --- mycrypt_misc.h DELETED --- --- safer_tab.c DELETED --- --- des.c DELETED --- --- strings.c DELETED --- --- mycrypt_pk.h DELETED --- --- rsa.c DELETED --- --- rc4.c DELETED --- --- makefile DELETED --- --- rmd160.c DELETED --- --- hash.c DELETED --- --- yarrow.c DELETED --- --- blowfish.c DELETED --- --- sha1.c DELETED --- --- mycrypt.h DELETED --- --- mpi.c DELETED --- --- skipjack.c DELETED --- --- mycrypt_macros.h DELETED --- Index: libtomcrypt.vcproj =================================================================== RCS file: /cvsroot/opentnl/tnl/libtomcrypt/libtomcrypt.vcproj,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** libtomcrypt.vcproj 20 Apr 2004 04:27:14 -0000 1.1 --- libtomcrypt.vcproj 28 Apr 2005 17:27:50 -0000 1.2 *************** *** 23,27 **** Name="VCCLCompilerTool" Optimization="0" ! AdditionalIncludeDirectories="." PreprocessorDefinitions="TNL_ENABLE_LOGGING;TNL_DEBUG;WIN32;_DEBUG;_LIB;" BasicRuntimeChecks="3" --- 23,27 ---- Name="VCCLCompilerTool" Optimization="0" ! AdditionalIncludeDirectories="headers" PreprocessorDefinitions="TNL_ENABLE_LOGGING;TNL_DEBUG;WIN32;_DEBUG;_LIB;" BasicRuntimeChecks="3" *************** *** 122,309 **** <Files> <File ! RelativePath=".\aes.c"> </File> <File ! RelativePath=".\base64.c"> </File> <File ! RelativePath=".\bits.c"> </File> <File ! RelativePath=".\blowfish.c"> </File> <File ! RelativePath=".\cast5.c"> </File> <File ! RelativePath=".\cbc.c"> </File> <File ! RelativePath=".\cfb.c"> </File> <File ! RelativePath=".\crypt.c"> </File> <File ! RelativePath=".\ctr.c"> </File> <File ! RelativePath=".\des.c"> </File> <File ! RelativePath=".\dh.c"> </File> <File ! RelativePath=".\dsa.c"> </File> <File ! RelativePath=".\ecb.c"> </File> <File ! RelativePath=".\ecc.c"> </File> <File ! RelativePath=".\gf.c"> </File> <File ! RelativePath=".\hash.c"> </File> <File ! RelativePath=".\hmac.c"> </File> <File ! RelativePath=".\keyring.c"> </File> <File ! RelativePath=".\md2.c"> </File> <File ! RelativePath=".\md4.c"> </File> <File ! RelativePath=".\md5.c"> </File> <File ! RelativePath=".\mem.c"> </File> <File ! RelativePath=".\mpi.c"> </File> <File ! RelativePath=".\mycrypt.h"> </File> <File ! RelativePath=".\mycrypt_argchk.h"> </File> <File ! RelativePath=".\mycrypt_cfg.h"> </File> <File ! RelativePath=".\mycrypt_cipher.h"> </File> <File ! RelativePath=".\mycrypt_custom.h"> </File> <File ! RelativePath=".\mycrypt_gf.h"> </File> <File ! RelativePath=".\mycrypt_hash.h"> </File> <File ! RelativePath=".\mycrypt_kr.h"> </File> <File ! RelativePath=".\mycrypt_macros.h"> </File> <File ! RelativePath=".\mycrypt_misc.h"> </File> <File ! RelativePath=".\mycrypt_pk.h"> </File> <File ! RelativePath=".\mycrypt_prng.h"> </File> <File ! RelativePath=".\noekeon.c"> </File> <File ! RelativePath=".\ofb.c"> </File> <File ! RelativePath=".\omac.c"> </File> <File ! RelativePath=".\packet.c"> </File> <File ! RelativePath=".\prime.c"> </File> <File ! RelativePath=".\rc2.c"> </File> <File ! RelativePath=".\rc4.c"> </File> <File ! RelativePath=".\rc5.c"> </File> <File ! RelativePath=".\rc6.c"> </File> <File ! RelativePath=".\rmd128.c"> </File> <File ! RelativePath=".\rmd160.c"> </File> <File ! RelativePath=".\rsa.c"> </File> <File ! RelativePath=".\safer+.c"> </File> <File ! RelativePath=".\safer.c"> </File> <File ! RelativePath=".\safer_tab.c"> </File> <File ! RelativePath=".\sha1.c"> </File> <File ! RelativePath=".\sha256.c"> </File> <File ! RelativePath=".\sha512.c"> </File> <File ! RelativePath=".\skipjack.c"> </File> <File ! RelativePath=".\sprng.c"> </File> <File ! RelativePath=".\strings.c"> </File> <File ! RelativePath=".\tiger.c"> </File> <File ! RelativePath=".\tommath.h"> </File> <File ! RelativePath=".\twofish.c"> </File> <File ! RelativePath=".\twofish_tab.c"> </File> <File ! RelativePath=".\xtea.c"> </File> <File ! RelativePath=".\yarrow.c"> </File> </Files> --- 122,315 ---- <Files> <File ! RelativePath=".\ciphers\aes\aes.c"> </File> <File ! RelativePath=".\misc\base64\base64_decode.c"> </File> <File ! RelativePath=".\misc\base64\base64_encode.c"> </File> <File ! RelativePath=".\misc\burn_stack.c"> </File> <File ! RelativePath=".\misc\crypt\crypt.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_argchk.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_cipher_descriptor.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_cipher_is_valid.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_find_cipher.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_find_cipher_any.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_find_cipher_id.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_find_hash.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_find_hash_any.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_find_hash_id.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_find_prng.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_hash_descriptor.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_hash_is_valid.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_prng_descriptor.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_prng_is_valid.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_register_cipher.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_register_hash.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_register_prng.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_unregister_cipher.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_unregister_hash.c"> </File> <File ! RelativePath=".\misc\crypt\crypt_unregister_prng.c"> </File> <File ! RelativePath=".\modes\ctr\ctr_decrypt.c"> </File> <File ! RelativePath=".\modes\ctr\ctr_done.c"> </File> <File ! RelativePath=".\modes\ctr\ctr_encrypt.c"> </File> <File ! RelativePath=".\modes\ctr\ctr_getiv.c"> </File> <File ! RelativePath=".\modes\ctr\ctr_setiv.c"> </File> <File ! RelativePath=".\modes\ctr\ctr_start.c"> </File> <File ! RelativePath=".\pk\ecc\ecc.c"> </File> <File ! RelativePath=".\misc\error_to_string.c"> </File> <File ! RelativePath=".\hashes\helper\hash_memory.c"> </File> <File ! RelativePath=".\hashes\helper\hash_memory_multi.c"> </File> <File ! RelativePath=".\misc\mpi\is_prime.c"> </File> <File ! RelativePath=".\headers\ltc_tommath.h"> </File> <File ! RelativePath=".\hashes\md2.c"> </File> <File ! RelativePath=".\hashes\md4.c"> </File> <File ! RelativePath=".\hashes\md5.c"> </File> <File ! RelativePath=".\misc\mpi\mpi.c"> </File> <File ! RelativePath=".\misc\mpi\mpi_to_ltc_error.c"> </File> <File ! RelativePath=".\pk\packet_store_header.c"> </File> <File ! RelativePath=".\pk\packet_valid_header.c"> </File> <File ! RelativePath=".\misc\mpi\rand_prime.c"> </File> <File ! RelativePath=".\hashes\sha1.c"> </File> <File ! RelativePath=".\hashes\sha2\sha256.c"> </File> <File ! RelativePath=".\hashes\sha2\sha512.c"> </File> <File ! RelativePath=".\headers\tomcrypt.h"> </File> <File ! RelativePath=".\headers\tomcrypt_argchk.h"> </File> <File ! RelativePath=".\headers\tomcrypt_cfg.h"> </File> <File ! RelativePath=".\headers\tomcrypt_cipher.h"> </File> <File ! RelativePath=".\headers\tomcrypt_custom.h"> </File> <File ! RelativePath=".\headers\tomcrypt_hash.h"> </File> <File ! RelativePath=".\headers\tomcrypt_mac.h"> </File> <File ! RelativePath=".\headers\tomcrypt_macros.h"> </File> <File ! RelativePath=".\headers\tomcrypt_misc.h"> </File> <File ! RelativePath=".\headers\tomcrypt_pk.h"> </File> <File ! RelativePath=".\headers\tomcrypt_pkcs.h"> </File> <File ! RelativePath=".\headers\tomcrypt_prng.h"> </File> <File ! RelativePath=".\headers\tommath_class.h"> </File> <File ! RelativePath=".\headers\tommath_superclass.h"> ! </File> ! <File ! RelativePath=".\prngs\yarrow.c"> ! </File> ! <File ! RelativePath=".\misc\zeromem.c"> </File> </Files> --- ecc_sys.c DELETED --- --- mycrypt_kr.h DELETED --- --- dh.c DELETED --- --- mem.c DELETED --- --- sprng.c DELETED --- --- sha224.c DELETED --- --- md5.c DELETED --- --- packet.c DELETED --- --- rsa_sys.c DELETED --- --- xtea.c DELETED --- --- omac.c DELETED --- --- rmd128.c DELETED --- --- keyring.c DELETED --- --- mycrypt_custom.h DELETED --- --- authors DELETED --- --- rc6.c DELETED --- --- md2.c DELETED --- --- rc2.c DELETED --- --- dh_sys.c DELETED --- --- ecc.c DELETED --- --- base64.c DELETED --- --- gf.c DELETED --- --- ctr.c DELETED --- --- noekeon.c DELETED --- --- twofish.c DELETED --- --- mycrypt_cfg.h DELETED --- --- prime.c DELETED --- --- tommath.h DELETED --- --- safer.c DELETED --- --- dsa.c DELETED --- --- mycrypt_prng.h DELETED --- --- ofb.c DELETED --- --- mycrypt_cipher.h DELETED --- --- bits.c DELETED --- --- mycrypt_hash.h DELETED --- --- md4.c DELETED --- --- aes_tab.c DELETED --- |
From: Mark F. <mar...@us...> - 2005-04-28 17:28:13
|
Update of /cvsroot/opentnl/tnl/libtomcrypt/pk/rsa In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/pk/rsa Added Files: rsa_decrypt_key.c rsa_encrypt_key.c rsa_export.c rsa_exptmod.c rsa_free.c rsa_import.c rsa_make_key.c rsa_sign_hash.c rsa_v15_decrypt_key.c rsa_v15_encrypt_key.c rsa_v15_sign_hash.c rsa_v15_verify_hash.c rsa_verify_hash.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- NEW FILE: rsa_free.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rsa_free.c Free an RSA key, Tom St Denis */ #ifdef MRSA /** Free an RSA key from memory @param key The RSA key to free */ void rsa_free(rsa_key *key) { LTC_ARGCHK(key != NULL); mp_clear_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, NULL); } #endif --- NEW FILE: rsa_encrypt_key.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rsa_encrypt_key.c RSA PKCS OAEP encryption, Tom St Denis */ #ifdef MRSA /** (PKCS #1 v2.0) OAEP pad then encrypt @param in The plaintext @param inlen The length of the plaintext (octets) @param out [out] The ciphertext @param outlen [in/out] The max size and resulting size of the ciphertext @param lparam The system "lparam" for the encryption @param lparamlen The length of lparam (octets) @param prng An active PRNG @param prng_idx The index of the desired prng @param hash_idx The index of the desired hash @param key The RSA key to encrypt to @return CRYPT_OK if successful */ int rsa_encrypt_key(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, const unsigned char *lparam, unsigned long lparamlen, prng_state *prng, int prng_idx, int hash_idx, rsa_key *key) { unsigned long modulus_bitlen, modulus_bytelen, x; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* valid prng and hash ? */ if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { return err; } if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { return err; } /* get modulus len in bits */ modulus_bitlen = mp_count_bits(&(key->N)); /* outlen must be at least the size of the modulus */ modulus_bytelen = mp_unsigned_bin_size(&(key->N)); if (modulus_bytelen > *outlen) { return CRYPT_BUFFER_OVERFLOW; } /* OAEP pad the key */ x = *outlen; if ((err = pkcs_1_oaep_encode(in, inlen, lparam, lparamlen, modulus_bitlen, prng, prng_idx, hash_idx, out, &x)) != CRYPT_OK) { return err; } /* rsa exptmod the OAEP pad */ return rsa_exptmod(out, x, out, outlen, PK_PUBLIC, key); } #endif /* MRSA */ --- NEW FILE: rsa_exptmod.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rsa_exptmod.c RSA PKCS exptmod, Tom St Denis */ #ifdef MRSA /** Compute an RSA modular exponentiation @param in The input data to send into RSA @param inlen The length of the input (octets) @param out [out] The destination @param outlen [in/out] The max size and resulting size of the output @param which Which exponent to use, e.g. PK_PRIVATE or PK_PUBLIC @param key The RSA key to use @return CRYPT_OK if successful */ int rsa_exptmod(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, int which, rsa_key *key) { mp_int tmp, tmpa, tmpb; unsigned long x; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* is the key of the right type for the operation? */ if (which == PK_PRIVATE && (key->type != PK_PRIVATE)) { return CRYPT_PK_NOT_PRIVATE; } /* must be a private or public operation */ if (which != PK_PRIVATE && which != PK_PUBLIC) { return CRYPT_PK_INVALID_TYPE; } /* init and copy into tmp */ if ((err = mp_init_multi(&tmp, &tmpa, &tmpb, NULL)) != MP_OKAY) { return mpi_to_ltc_error(err); } if ((err = mp_read_unsigned_bin(&tmp, (unsigned char *)in, (int)inlen)) != MP_OKAY) { goto error; } /* sanity check on the input */ if (mp_cmp(&key->N, &tmp) == MP_LT) { err = CRYPT_PK_INVALID_SIZE; goto done; } /* are we using the private exponent and is the key optimized? */ if (which == PK_PRIVATE) { /* tmpa = tmp^dP mod p */ if ((err = mp_exptmod(&tmp, &key->dP, &key->p, &tmpa)) != MP_OKAY) { goto error; } /* tmpb = tmp^dQ mod q */ if ((err = mp_exptmod(&tmp, &key->dQ, &key->q, &tmpb)) != MP_OKAY) { goto error; } /* tmp = (tmpa - tmpb) * qInv (mod p) */ if ((err = mp_sub(&tmpa, &tmpb, &tmp)) != MP_OKAY) { goto error; } if ((err = mp_mulmod(&tmp, &key->qP, &key->p, &tmp)) != MP_OKAY) { goto error; } /* tmp = tmpb + q * tmp */ if ((err = mp_mul(&tmp, &key->q, &tmp)) != MP_OKAY) { goto error; } if ((err = mp_add(&tmp, &tmpb, &tmp)) != MP_OKAY) { goto error; } } else { /* exptmod it */ if ((err = mp_exptmod(&tmp, &key->e, &key->N, &tmp)) != MP_OKAY) { goto error; } } /* read it back */ x = (unsigned long)mp_unsigned_bin_size(&key->N); if (x > *outlen) { err = CRYPT_BUFFER_OVERFLOW; goto done; } *outlen = x; /* convert it */ zeromem(out, x); if ((err = mp_to_unsigned_bin(&tmp, out+(x-mp_unsigned_bin_size(&tmp)))) != MP_OKAY) { goto error; } /* clean up and return */ err = CRYPT_OK; goto done; error: err = mpi_to_ltc_error(err); done: mp_clear_multi(&tmp, &tmpa, &tmpb, NULL); return err; } #endif --- NEW FILE: rsa_make_key.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rsa_make_key.c RSA key generation, Tom St Denis */ #ifdef MRSA /** Create an RSA key @param prng An active PRNG state @param wprng The index of the PRNG desired @param size The size of the modulus (key size) desired (octets) @param e The "e" value (public key). e==65537 is a good choice @param key [out] Destination of a newly created private key pair @return CRYPT_OK if successful, upon error all allocated ram is freed */ int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key) { mp_int p, q, tmp1, tmp2, tmp3; int err; LTC_ARGCHK(key != NULL); if ((size < (MIN_RSA_SIZE/8)) || (size > (MAX_RSA_SIZE/8))) { return CRYPT_INVALID_KEYSIZE; } if ((e < 3) || ((e & 1) == 0)) { return CRYPT_INVALID_ARG; } if ((err = prng_is_valid(wprng)) != CRYPT_OK) { return err; } if ((err = mp_init_multi(&p, &q, &tmp1, &tmp2, &tmp3, NULL)) != MP_OKAY) { return mpi_to_ltc_error(err); } /* make primes p and q (optimization provided by Wayne Scott) */ if ((err = mp_set_int(&tmp3, e)) != MP_OKAY) { goto error; } /* tmp3 = e */ /* make prime "p" */ do { if ((err = rand_prime(&p, size*4, prng, wprng)) != CRYPT_OK) { goto done; } if ((err = mp_sub_d(&p, 1, &tmp1)) != MP_OKAY) { goto error; } /* tmp1 = p-1 */ if ((err = mp_gcd(&tmp1, &tmp3, &tmp2)) != MP_OKAY) { goto error; } /* tmp2 = gcd(p-1, e) */ } while (mp_cmp_d(&tmp2, 1) != 0); /* while e divides p-1 */ /* make prime "q" */ do { if ((err = rand_prime(&q, size*4, prng, wprng)) != CRYPT_OK) { goto done; } if ((err = mp_sub_d(&q, 1, &tmp1)) != MP_OKAY) { goto error; } /* tmp1 = q-1 */ if ((err = mp_gcd(&tmp1, &tmp3, &tmp2)) != MP_OKAY) { goto error; } /* tmp2 = gcd(q-1, e) */ } while (mp_cmp_d(&tmp2, 1) != 0); /* while e divides q-1 */ /* tmp1 = lcm(p-1, q-1) */ if ((err = mp_sub_d(&p, 1, &tmp2)) != MP_OKAY) { goto error; } /* tmp2 = p-1 */ /* tmp1 = q-1 (previous do/while loop) */ if ((err = mp_lcm(&tmp1, &tmp2, &tmp1)) != MP_OKAY) { goto error; } /* tmp1 = lcm(p-1, q-1) */ /* make key */ if ((err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, NULL)) != MP_OKAY) { goto error; } if ((err = mp_set_int(&key->e, e)) != MP_OKAY) { goto error2; } /* key->e = e */ if ((err = mp_invmod(&key->e, &tmp1, &key->d)) != MP_OKAY) { goto error2; } /* key->d = 1/e mod lcm(p-1,q-1) */ if ((err = mp_mul(&p, &q, &key->N)) != MP_OKAY) { goto error2; } /* key->N = pq */ /* optimize for CRT now */ /* find d mod q-1 and d mod p-1 */ if ((err = mp_sub_d(&p, 1, &tmp1)) != MP_OKAY) { goto error2; } /* tmp1 = q-1 */ if ((err = mp_sub_d(&q, 1, &tmp2)) != MP_OKAY) { goto error2; } /* tmp2 = p-1 */ if ((err = mp_mod(&key->d, &tmp1, &key->dP)) != MP_OKAY) { goto error2; } /* dP = d mod p-1 */ if ((err = mp_mod(&key->d, &tmp2, &key->dQ)) != MP_OKAY) { goto error2; } /* dQ = d mod q-1 */ if ((err = mp_invmod(&q, &p, &key->qP)) != MP_OKAY) { goto error2; } /* qP = 1/q mod p */ if ((err = mp_copy(&p, &key->p)) != MP_OKAY) { goto error2; } if ((err = mp_copy(&q, &key->q)) != MP_OKAY) { goto error2; } /* shrink ram required */ if ((err = mp_shrink(&key->e)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->d)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->N)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->dQ)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->dP)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->qP)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->p)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->q)) != MP_OKAY) { goto error2; } /* set key type (in this case it's CRT optimized) */ key->type = PK_PRIVATE; /* return ok and free temps */ err = CRYPT_OK; goto done; error2: mp_clear_multi(&key->d, &key->e, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, NULL); error: err = mpi_to_ltc_error(err); done: mp_clear_multi(&tmp3, &tmp2, &tmp1, &p, &q, NULL); return err; } #endif --- NEW FILE: rsa_v15_verify_hash.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rsa_v15_verify_hash.c RSA PKCS v1.5 Signature verification, Tom St Denis */ #ifdef MRSA /** RSA de-sign then PKCS v1.5 signature depad @param sig The signature data @param siglen The length of the signature (octets) @param hash The hash of the message that was signed @param hashlen The length of the hash of the message that was signed (octets) @param hash_idx The index of the desired hash @param stat [out] The result of the signature comparison, 1==valid, 0==invalid @param key The corresponding public RSA key that performed the signature @return CRYPT_OK if successful (even if the signature is invalid) */ int rsa_v15_verify_hash(const unsigned char *sig, unsigned long siglen, const unsigned char *hash, unsigned long hashlen, int hash_idx, int *stat, rsa_key *key) { unsigned long modulus_bitlen, modulus_bytelen, x; int err; unsigned char *tmpbuf; LTC_ARGCHK(hash != NULL); LTC_ARGCHK(sig != NULL); LTC_ARGCHK(stat != NULL); LTC_ARGCHK(key != NULL); /* default to invalid */ *stat = 0; /* valid hash ? */ if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { return err; } /* get modulus len in bits */ modulus_bitlen = mp_count_bits(&(key->N)); /* outlen must be at least the size of the modulus */ modulus_bytelen = mp_unsigned_bin_size(&(key->N)); if (modulus_bytelen != siglen) { return CRYPT_INVALID_PACKET; } /* allocate temp buffer for decoded sig */ tmpbuf = XMALLOC(siglen); if (tmpbuf == NULL) { return CRYPT_MEM; } /* RSA decode it */ x = siglen; if ((err = rsa_exptmod(sig, siglen, tmpbuf, &x, PK_PUBLIC, key)) != CRYPT_OK) { XFREE(tmpbuf); return err; } /* PSS decode it */ err = pkcs_1_v15_sa_decode(hash, hashlen, tmpbuf, x, hash_idx, modulus_bitlen, stat); XFREE(tmpbuf); return err; } #endif --- NEW FILE: rsa_verify_hash.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rsa_verify_hash.c RSA PKCS v2 PSS signature verification, Tom St Denis */ #ifdef MRSA /** (PKCS #1, v2.0) de-sign then PSS depad @param sig The signature data @param siglen The length of the signature data (octets) @param hash The hash of the message that was signed @param hashlen The length of the hash of the message that was signed (octets) @param hash_idx The index of the desired hash @param saltlen The length of the salt used during signature @param stat [out] The result of the signature comparison, 1==valid, 0==invalid @param key The public RSA key corresponding to the key that performed the signature @return CRYPT_OK on success (even if the signature is invalid) */ int rsa_verify_hash(const unsigned char *sig, unsigned long siglen, const unsigned char *hash, unsigned long hashlen, int hash_idx, unsigned long saltlen, int *stat, rsa_key *key) { unsigned long modulus_bitlen, modulus_bytelen, x; int err; unsigned char *tmpbuf; LTC_ARGCHK(hash != NULL); LTC_ARGCHK(sig != NULL); LTC_ARGCHK(stat != NULL); LTC_ARGCHK(key != NULL); /* default to invalid */ *stat = 0; /* valid hash ? */ if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { return err; } /* get modulus len in bits */ modulus_bitlen = mp_count_bits(&(key->N)); /* outlen must be at least the size of the modulus */ modulus_bytelen = mp_unsigned_bin_size(&(key->N)); if (modulus_bytelen != siglen) { return CRYPT_INVALID_PACKET; } /* allocate temp buffer for decoded sig */ tmpbuf = XMALLOC(siglen); if (tmpbuf == NULL) { return CRYPT_MEM; } /* RSA decode it */ x = siglen; if ((err = rsa_exptmod(sig, siglen, tmpbuf, &x, PK_PUBLIC, key)) != CRYPT_OK) { XFREE(tmpbuf); return err; } /* PSS decode it */ err = pkcs_1_pss_decode(hash, hashlen, tmpbuf, x, saltlen, hash_idx, modulus_bitlen, stat); XFREE(tmpbuf); return err; } #endif /* MRSA */ --- NEW FILE: rsa_import.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rsa_import.c Import a PKCS RSA key, Tom St Denis */ #ifdef MRSA /** Import an RSAPublicKey or RSAPrivateKey [two-prime only, defined in PKCS #1 v2.1] @param in The packet to import from @param inlen It's length (octets) @param key [out] Destination for newly imported key @return CRYPT_OK if successful, upon error allocated memory is freed */ int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key) { unsigned long x, y; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(key != NULL); /* init key */ if ((err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, NULL)) != MP_OKAY) { return mpi_to_ltc_error(err); } /* check the header */ if (inlen < 4) { return CRYPT_INVALID_PACKET; } /* should be 0x30 0x8{1|2} LL LL */ if ((in[0] != 0x30) || ((in[1] != 0x81) && (in[1] != 0x82))) { return CRYPT_INVALID_PACKET; } /* ok all the ASN.1 params are fine so far, let's move up */ x = ((unsigned long)in[2]); y = 0; if ((in[1] & ~0x80) == 2) { x = (x << 8) + ((unsigned long)in[3]) + 1; in += 1; y = 1; } in += 3; /* advance input */ x += 3; /* size of packet according to header */ y += 3; /* used input */ if (x != inlen) { return CRYPT_INVALID_PACKET; } /* decrement inlen by the header size */ inlen -= y; /* read first number, it's either N or 0 [0 == private key] */ x = inlen; if ((err = der_get_multi_integer(in, &x, &key->N, NULL)) != CRYPT_OK) { goto LBL_ERR; } /* advance */ inlen -= x; in += x; if (mp_cmp_d(&key->N, 0) == MP_EQ) { /* it's a private key */ if ((err = der_get_multi_integer(in, &inlen, &key->N, &key->e, &key->d, &key->p, &key->q, &key->dP, &key->dQ, &key->qP, NULL)) != CRYPT_OK) { goto LBL_ERR; } key->type = PK_PRIVATE; } else if (mp_cmp_d(&key->N, 1) == MP_EQ) { /* we don't support multi-prime RSA */ err = CRYPT_PK_INVALID_TYPE; goto LBL_ERR; } else { /* it's a public key and we lack e */ if ((err = der_get_multi_integer(in, &inlen, &key->e, NULL)) != CRYPT_OK) { goto LBL_ERR; } /* free up some ram */ mp_clear_multi(&key->p, &key->q, &key->qP, &key->dP, &key->dQ, NULL); key->type = PK_PUBLIC; } return CRYPT_OK; LBL_ERR: mp_clear_multi(&key->d, &key->e, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, NULL); return err; } #endif /* MRSA */ --- NEW FILE: rsa_v15_encrypt_key.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rsa_v15_encrypt_key.c RSA PKCS v1.5 Encryption, Tom St Denis */ #ifdef MRSA /** PKCS #1 v1.5 pad then encrypt @param in The plaintext @param inlen The length of the plaintext (octets) @param out [out] The ciphertext @param outlen [in/out] The max size and resulting size of the ciphertext @param prng An active PRNG @param prng_idx The index of the desired PRNG @param key The public RSA key @return CRYPT_OK if successful */ int rsa_v15_encrypt_key(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, prng_state *prng, int prng_idx, rsa_key *key) { unsigned long modulus_bitlen, modulus_bytelen, x; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* valid prng? */ if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { return err; } /* get modulus len in bits */ modulus_bitlen = mp_count_bits(&(key->N)); /* outlen must be at least the size of the modulus */ modulus_bytelen = mp_unsigned_bin_size(&(key->N)); if (modulus_bytelen > *outlen) { return CRYPT_BUFFER_OVERFLOW; } /* pad it */ x = *outlen; if ((err = pkcs_1_v15_es_encode(in, inlen, modulus_bitlen, prng, prng_idx, out, &x)) != CRYPT_OK) { return err; } /* encrypt it */ return rsa_exptmod(out, x, out, outlen, PK_PUBLIC, key); } #endif --- NEW FILE: rsa_v15_sign_hash.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rsa_v15_sign_hash.c RSA PKCS v1.5 Signature, Tom St Denis */ #ifdef MRSA /** PKCS #1 v1.5 pad then sign @param in The hash to sign @param inlen The length of the message hash (octets) @param out [out] The signature @param siglen [in/out] The max size and resulting size of the signature @param hash_idx The index of the hash desired @param key The private RSA key to perform the signature with @return CRYPT_OK if successful */ int rsa_v15_sign_hash(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *siglen, int hash_idx, rsa_key *key) { unsigned long modulus_bitlen, modulus_bytelen, x; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(siglen != NULL); LTC_ARGCHK(key != NULL); /* valid hash ? */ if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { return err; } /* get modulus len in bits */ modulus_bitlen = mp_count_bits(&(key->N)); /* outlen must be at least the size of the modulus */ modulus_bytelen = mp_unsigned_bin_size(&(key->N)); if (modulus_bytelen > *siglen) { return CRYPT_BUFFER_OVERFLOW; } /* PKCS #1 v1.5 pad the key */ x = *siglen; if ((err = pkcs_1_v15_sa_encode(in, inlen, hash_idx, modulus_bitlen, out, &x)) != CRYPT_OK) { return err; } /* RSA encode it */ return rsa_exptmod(out, x, out, siglen, PK_PRIVATE, key); } #endif --- NEW FILE: rsa_export.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rsa_export.c Export RSA PKCS keys, Tom St Denis */ #ifdef MRSA /** This will export either an RSAPublicKey or RSAPrivateKey [defined in PKCS #1 v2.1] @param out [out] Destination of the packet @param outlen [in/out] The max size and resulting size of the packet @param type The type of exported key (PK_PRIVATE or PK_PUBLIC) @param key The RSA key to export @return CRYPT_OK if successful */ int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key) { int err, x; LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* type valid? */ if (!(key->type == PK_PRIVATE) && (type == PK_PRIVATE)) { return CRYPT_PK_INVALID_TYPE; } if (*outlen < 4) { return CRYPT_BUFFER_OVERFLOW; } /* Mental Note: push space for the header 0x30 0x82 LL LL (LL = length of packet EXcluding 4 bytes) * we assume LL > 255 which is true since the smallest RSA key has a 128-byte modulus (1024-bit) */ *outlen -= 4; if (type == PK_PRIVATE) { /* private key */ mp_int zero; /* first INTEGER == 0 to signify two-prime RSA */ if ((err = mp_init(&zero)) != MP_OKAY) { return mpi_to_ltc_error(err); } /* output is Version, n, e, d, p, q, d mod (p-1), d mod (q - 1), 1/q mod p */ if ((err = der_put_multi_integer( out+4, outlen, &zero, &key->N, &key->e, &key->d, &key->p, &key->q, &key->dP, &key->dQ, &key->qP, NULL)) != CRYPT_OK) { mp_clear(&zero); return err; } /* clear zero and return */ mp_clear(&zero); } else { /* public key */ if ((err = der_put_multi_integer(out+4, outlen, &key->N, &key->e, NULL)) != CRYPT_OK) { return err; } } /* store the header */ out[0] = 0x30; if (*outlen < 256) { /* shift the output up one byte if the header is only 3 bytes */ for (x = 0; x < *outlen; x++) { out[x+3] = out[x+4]; } out[1] = 0x81; out[2] = (*outlen & 255); *outlen += 3; } else { out[1] = 0x82; out[2] = (*outlen >> 8) & 255; out[3] = (*outlen & 255); *outlen += 4; } return err; } #endif /* MRSA */ --- NEW FILE: rsa_decrypt_key.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rsa_decrypt_key.c RSA PKCS #1 OAEP Decryption, Tom St Denis */ #ifdef MRSA /** (PKCS #1 v2.0) decrypt then OAEP depad @param in The ciphertext @param inlen The length of the ciphertext (octets) @param out [out] The plaintext @param outlen [in/out] The max size and resulting size of the plaintext (octets) @param lparam The system "lparam" value @param lparamlen The length of the lparam value (octets) @param hash_idx The index of the hash desired @param stat [out] Result of the decryption, 1==valid, 0==invalid @param key The corresponding private RSA key @return CRYPT_OK if succcessul (even if invalid) */ int rsa_decrypt_key(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, const unsigned char *lparam, unsigned long lparamlen, int hash_idx, int *stat, rsa_key *key) { unsigned long modulus_bitlen, modulus_bytelen, x; int err; unsigned char *tmp; LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); LTC_ARGCHK(stat != NULL); /* default to invalid */ *stat = 0; /* valid hash ? */ if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { return err; } /* get modulus len in bits */ modulus_bitlen = mp_count_bits(&(key->N)); /* outlen must be at least the size of the modulus */ modulus_bytelen = mp_unsigned_bin_size(&(key->N)); if (modulus_bytelen != inlen) { return CRYPT_INVALID_PACKET; } /* allocate ram */ tmp = XMALLOC(inlen); if (tmp == NULL) { return CRYPT_MEM; } /* rsa decode the packet */ x = inlen; if ((err = rsa_exptmod(in, inlen, tmp, &x, PK_PRIVATE, key)) != CRYPT_OK) { XFREE(tmp); return err; } /* now OAEP decode the packet */ err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx, out, outlen, stat); XFREE(tmp); return err; } #endif /* MRSA */ --- NEW FILE: rsa_v15_decrypt_key.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rsa_v15_decrypt_key.c RSA PKCS v1.5 Decryption, Tom St Denis */ #ifdef MRSA /** RSA decrypt then PKCS #1 v1.5 depad @param in The ciphertext @param inlen The length of the ciphertext (octets) @param out [out] The plaintext @param outlen The length of the plaintext (you have to tell this function as it's not part of PKCS #1 v1.0 padding!) @param stat [out] Status of decryption, 1==valid, 0==invalid @param key The corresponding private RSA key @return CRYPT_OK if successful (even if invalid) */ int rsa_v15_decrypt_key(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long outlen, int *stat, rsa_key *key) { unsigned long modulus_bitlen, modulus_bytelen, x; int err; unsigned char *tmp; LTC_ARGCHK(out != NULL); LTC_ARGCHK(key != NULL); LTC_ARGCHK(stat != NULL); /* default to invalid */ *stat = 0; /* get modulus len in bits */ modulus_bitlen = mp_count_bits(&(key->N)); /* outlen must be at least the size of the modulus */ modulus_bytelen = mp_unsigned_bin_size(&(key->N)); if (modulus_bytelen != inlen) { return CRYPT_INVALID_PACKET; } /* allocate ram */ tmp = XMALLOC(inlen); if (tmp == NULL) { return CRYPT_MEM; } /* rsa decode the packet */ x = inlen; if ((err = rsa_exptmod(in, inlen, tmp, &x, PK_PRIVATE, key)) != CRYPT_OK) { XFREE(tmp); return err; } /* PKCS #1 v1.5 depad */ err = pkcs_1_v15_es_decode(tmp, x, modulus_bitlen, out, outlen, stat); XFREE(tmp); return err; } #endif --- NEW FILE: rsa_sign_hash.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rsa_sign_hash.c RSA PKCS v2 PSS sign hash, Tom St Denis */ #ifdef MRSA /** (PKCS #1, v2.0) PSS pad then sign @param in The hash to sign @param inlen The length of the hash to sign (octets) @param out [out] The signature @param outlen [in/out] The max size and resulting size of the signature @param prng An active PRNG state @param prng_idx The index of the PRNG desired @param hash_idx The index of the hash desired @param saltlen The length of the salt desired (octets) @param key The private RSA key to use @return CRYPT_OK if successful */ int rsa_sign_hash(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, prng_state *prng, int prng_idx, int hash_idx, unsigned long saltlen, rsa_key *key) { unsigned long modulus_bitlen, modulus_bytelen, x; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* valid prng and hash ? */ if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { return err; } if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { return err; } /* get modulus len in bits */ modulus_bitlen = mp_count_bits(&(key->N)); /* outlen must be at least the size of the modulus */ modulus_bytelen = mp_unsigned_bin_size(&(key->N)); if (modulus_bytelen > *outlen) { return CRYPT_BUFFER_OVERFLOW; } /* PSS pad the key */ x = *outlen; if ((err = pkcs_1_pss_encode(in, inlen, saltlen, prng, prng_idx, hash_idx, modulus_bitlen, out, &x)) != CRYPT_OK) { return err; } /* RSA encode it */ return rsa_exptmod(out, x, out, outlen, PK_PRIVATE, key); } #endif /* MRSA */ |
From: Mark F. <mar...@us...> - 2005-04-28 17:28:13
|
Update of /cvsroot/opentnl/tnl/libtomcrypt/pk/dsa In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/pk/dsa Added Files: dsa_export.c dsa_free.c dsa_import.c dsa_make_key.c dsa_sign_hash.c dsa_verify_hash.c dsa_verify_key.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- NEW FILE: dsa_make_key.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file dsa_make_key.c DSA implementation, generate a DSA key, Tom St Denis */ #ifdef MDSA /** Create a DSA key @param prng An active PRNG state @param wprng The index of the PRNG desired @param group_size Size of the multiplicative group (octets) @param modulus_size Size of the modulus (octets) @param key [out] Where to store the created key @return CRYPT_OK if successful, upon error this function will free all allocated memory */ int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key) { mp_int tmp, tmp2; int err, res; unsigned char *buf; LTC_ARGCHK(key != NULL); /* check prng */ if ((err = prng_is_valid(wprng)) != CRYPT_OK) { return err; } /* check size */ if (group_size >= MDSA_MAX_GROUP || group_size <= 15 || group_size >= modulus_size || (modulus_size - group_size) >= MDSA_DELTA) { return CRYPT_INVALID_ARG; } /* allocate ram */ buf = XMALLOC(MDSA_DELTA); if (buf == NULL) { return CRYPT_MEM; } /* init mp_ints */ if ((err = mp_init_multi(&tmp, &tmp2, &key->g, &key->q, &key->p, &key->x, &key->y, NULL)) != MP_OKAY) { err = mpi_to_ltc_error(err); goto LBL_ERR; } /* make our prime q */ if ((err = rand_prime(&key->q, group_size*8, prng, wprng)) != CRYPT_OK) { goto LBL_ERR; } /* double q */ if ((err = mp_mul_2(&key->q, &tmp)) != MP_OKAY) { goto error; } /* now make a random string and multply it against q */ if (prng_descriptor[wprng].read(buf+1, modulus_size - group_size, prng) != (unsigned long)(modulus_size - group_size)) { err = CRYPT_ERROR_READPRNG; goto LBL_ERR; } /* force magnitude */ buf[0] = 1; /* force even */ buf[modulus_size - group_size] &= ~1; if ((err = mp_read_unsigned_bin(&tmp2, buf, modulus_size - group_size+1)) != MP_OKAY) { goto error; } if ((err = mp_mul(&key->q, &tmp2, &key->p)) != MP_OKAY) { goto error; } if ((err = mp_add_d(&key->p, 1, &key->p)) != MP_OKAY) { goto error; } /* now loop until p is prime */ for (;;) { if ((err = is_prime(&key->p, &res)) != CRYPT_OK) { goto LBL_ERR; } if (res == MP_YES) break; /* add 2q to p and 2 to tmp2 */ if ((err = mp_add(&tmp, &key->p, &key->p)) != MP_OKAY) { goto error; } if ((err = mp_add_d(&tmp2, 2, &tmp2)) != MP_OKAY) { goto error; } } /* now p = (q * tmp2) + 1 is prime, find a value g for which g^tmp2 != 1 */ mp_set(&key->g, 1); do { if ((err = mp_add_d(&key->g, 1, &key->g)) != MP_OKAY) { goto error; } if ((err = mp_exptmod(&key->g, &tmp2, &key->p, &tmp)) != MP_OKAY) { goto error; } } while (mp_cmp_d(&tmp, 1) == MP_EQ); /* at this point tmp generates a group of order q mod p */ mp_exch(&tmp, &key->g); /* so now we have our DH structure, generator g, order q, modulus p Now we need a random exponent [mod q] and it's power g^x mod p */ do { if (prng_descriptor[wprng].read(buf, group_size, prng) != (unsigned long)group_size) { err = CRYPT_ERROR_READPRNG; goto LBL_ERR; } if ((err = mp_read_unsigned_bin(&key->x, buf, group_size)) != MP_OKAY) { goto error; } } while (mp_cmp_d(&key->x, 1) != MP_GT); if ((err = mp_exptmod(&key->g, &key->x, &key->p, &key->y)) != MP_OKAY) { goto error; } key->type = PK_PRIVATE; key->qord = group_size; /* shrink the ram required */ if ((err = mp_shrink(&key->g)) != MP_OKAY) { goto error; } if ((err = mp_shrink(&key->p)) != MP_OKAY) { goto error; } if ((err = mp_shrink(&key->q)) != MP_OKAY) { goto error; } if ((err = mp_shrink(&key->x)) != MP_OKAY) { goto error; } if ((err = mp_shrink(&key->y)) != MP_OKAY) { goto error; } #ifdef LTC_CLEAN_STACK zeromem(buf, MDSA_DELTA); #endif err = CRYPT_OK; goto done; error: err = mpi_to_ltc_error(err); LBL_ERR: mp_clear_multi(&key->g, &key->q, &key->p, &key->x, &key->y, NULL); done: mp_clear_multi(&tmp, &tmp2, NULL); XFREE(buf); return err; } #endif --- NEW FILE: dsa_export.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file dsa_export.c DSA implementation, export key, Tom St Denis */ #ifdef MDSA /** Export a DSA key to a binary packet @param out [out] Where to store the packet @param outlen [in/out] The max size and resulting size of the packet @param type The type of key to export (PK_PRIVATE or PK_PUBLIC) @param key The key to export @return CRYPT_OK if successful */ int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key) { unsigned long y, z; int err; LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* can we store the static header? */ if (*outlen < (PACKET_SIZE + 1 + 2)) { return CRYPT_BUFFER_OVERFLOW; } if (type == PK_PRIVATE && key->type != PK_PRIVATE) { return CRYPT_PK_TYPE_MISMATCH; } if (type != PK_PUBLIC && type != PK_PRIVATE) { return CRYPT_INVALID_ARG; } /* store header */ packet_store_header(out, PACKET_SECT_DSA, PACKET_SUB_KEY); y = PACKET_SIZE; /* store g, p, q, qord */ out[y++] = type; out[y++] = (key->qord>>8)&255; out[y++] = key->qord & 255; OUTPUT_BIGNUM(&key->g,out,y,z); OUTPUT_BIGNUM(&key->p,out,y,z); OUTPUT_BIGNUM(&key->q,out,y,z); /* public exponent */ OUTPUT_BIGNUM(&key->y,out,y,z); if (type == PK_PRIVATE) { OUTPUT_BIGNUM(&key->x,out,y,z); } *outlen = y; return CRYPT_OK; } #endif --- NEW FILE: dsa_verify_key.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file dsa_verify_key.c DSA implementation, verify a key, Tom St Denis */ #ifdef MDSA /** Verify a DSA key for validity @param key The key to verify @param stat [out] Result of test, 1==valid, 0==invalid @return CRYPT_OK if successful */ int dsa_verify_key(dsa_key *key, int *stat) { mp_int tmp, tmp2; int res, err; LTC_ARGCHK(key != NULL); LTC_ARGCHK(stat != NULL); /* default to an invalid key */ *stat = 0; /* first make sure key->q and key->p are prime */ if ((err = is_prime(&key->q, &res)) != CRYPT_OK) { return err; } if (res == 0) { return CRYPT_OK; } if ((err = is_prime(&key->p, &res)) != CRYPT_OK) { return err; } if (res == 0) { return CRYPT_OK; } /* now make sure that g is not -1, 0 or 1 and <p */ if (mp_cmp_d(&key->g, 0) == MP_EQ || mp_cmp_d(&key->g, 1) == MP_EQ) { return CRYPT_OK; } if ((err = mp_init_multi(&tmp, &tmp2, NULL)) != MP_OKAY) { goto error; } if ((err = mp_sub_d(&key->p, 1, &tmp)) != MP_OKAY) { goto error; } if (mp_cmp(&tmp, &key->g) == MP_EQ || mp_cmp(&key->g, &key->p) != MP_LT) { err = CRYPT_OK; goto done; } /* 1 < y < p-1 */ if (!(mp_cmp_d(&key->y, 1) == MP_GT && mp_cmp(&key->y, &tmp) == MP_LT)) { err = CRYPT_OK; goto done; } /* now we have to make sure that g^q = 1, and that p-1/q gives 0 remainder */ if ((err = mp_div(&tmp, &key->q, &tmp, &tmp2)) != MP_OKAY) { goto error; } if (mp_iszero(&tmp2) != MP_YES) { err = CRYPT_OK; goto done; } if ((err = mp_exptmod(&key->g, &key->q, &key->p, &tmp)) != MP_OKAY) { goto error; } if (mp_cmp_d(&tmp, 1) != MP_EQ) { err = CRYPT_OK; goto done; } /* now we have to make sure that y^q = 1, this makes sure y \in g^x mod p */ if ((err = mp_exptmod(&key->y, &key->q, &key->p, &tmp)) != MP_OKAY) { goto error; } if (mp_cmp_d(&tmp, 1) != MP_EQ) { err = CRYPT_OK; goto done; } /* at this point we are out of tests ;-( */ err = CRYPT_OK; *stat = 1; goto done; error: err = mpi_to_ltc_error(err); done : mp_clear_multi(&tmp, &tmp2, NULL); return err; } #endif --- NEW FILE: dsa_sign_hash.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file dsa_sign_hash.c DSA implementation, sign a hash, Tom St Denis */ #ifdef MDSA /** Sign a hash with DSA @param in The hash to sign @param inlen The length of the hash to sign @param out [out] Where to store the signature @param outlen [in/out] The max size and resulting size of the signature @param prng An active PRNG state @param wprng The index of the PRNG desired @param key A private DSA key @return CRYPT_OK if successful */ int dsa_sign_hash(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, prng_state *prng, int wprng, dsa_key *key) { mp_int k, kinv, tmp, r, s; unsigned char *buf; int err; unsigned long out1, out2; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); if ((err = prng_is_valid(wprng)) != CRYPT_OK) { return err; } if (key->type != PK_PRIVATE) { return CRYPT_PK_NOT_PRIVATE; } /* check group order size */ if (key->qord >= MDSA_MAX_GROUP) { return CRYPT_INVALID_ARG; } buf = XMALLOC(MDSA_MAX_GROUP); if (buf == NULL) { return CRYPT_MEM; } /* Init our temps */ if ((err = mp_init_multi(&k, &kinv, &r, &s, &tmp, NULL)) != MP_OKAY) { goto error; } retry: do { /* gen random k */ if (prng_descriptor[wprng].read(buf, key->qord, prng) != (unsigned long)key->qord) { err = CRYPT_ERROR_READPRNG; goto LBL_ERR; } /* read k */ if ((err = mp_read_unsigned_bin(&k, buf, key->qord)) != MP_OKAY) { goto error; } /* k > 1 ? */ if (mp_cmp_d(&k, 1) != MP_GT) { goto retry; } /* test gcd */ if ((err = mp_gcd(&k, &key->q, &tmp)) != MP_OKAY) { goto error; } } while (mp_cmp_d(&tmp, 1) != MP_EQ); /* now find 1/k mod q */ if ((err = mp_invmod(&k, &key->q, &kinv)) != MP_OKAY) { goto error; } /* now find r = g^k mod p mod q */ if ((err = mp_exptmod(&key->g, &k, &key->p, &r)) != MP_OKAY) { goto error; } if ((err = mp_mod(&r, &key->q, &r)) != MP_OKAY) { goto error; } if (mp_iszero(&r) == MP_YES) { goto retry; } /* now find s = (in + xr)/k mod q */ if ((err = mp_read_unsigned_bin(&tmp, (unsigned char *)in, inlen)) != MP_OKAY) { goto error; } if ((err = mp_mul(&key->x, &r, &s)) != MP_OKAY) { goto error; } if ((err = mp_add(&s, &tmp, &s)) != MP_OKAY) { goto error; } if ((err = mp_mulmod(&s, &kinv, &key->q, &s)) != MP_OKAY) { goto error; } if (mp_iszero(&s) == MP_YES) { goto retry; } /* now store em both */ /* first check that we have enough room */ if ((err = der_length_integer(&s, &out1)) != CRYPT_OK) { goto LBL_ERR; } if ((err = der_length_integer(&r, &out2)) != CRYPT_OK) { goto LBL_ERR; } if (*outlen < (out1+out2)) { err = CRYPT_BUFFER_OVERFLOW; goto LBL_ERR; } /* store ints */ err = der_put_multi_integer(out, outlen, &r, &s, NULL); goto LBL_ERR; error: err = mpi_to_ltc_error(err); LBL_ERR: mp_clear_multi(&k, &kinv, &r, &s, &tmp, NULL); #ifdef LTC_CLEAN_STACK zeromem(buf, MDSA_MAX_GROUP); #endif XFREE(buf); return err; } #endif --- NEW FILE: dsa_free.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file dsa_free.c DSA implementation, free a DSA key, Tom St Denis */ #ifdef MDSA /** Free a DSA key @param key The key to free from memory */ void dsa_free(dsa_key *key) { LTC_ARGCHK(key != NULL); mp_clear_multi(&key->g, &key->q, &key->p, &key->x, &key->y, NULL); } #endif --- NEW FILE: dsa_import.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file dsa_import.c DSA implementation, import a DSA key, Tom St Denis */ #ifdef MDSA /** Import a DSA key @param in The binary packet to import from @param inlen The length of the binary packet @param key [out] Where to store the imported key @return CRYPT_OK if successful, upon error this function will free all allocated memory */ int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key) { unsigned long x, y; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(key != NULL); /* check length */ if ((1+2+PACKET_SIZE) > inlen) { return CRYPT_INVALID_PACKET; } /* check type */ if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_DSA, PACKET_SUB_KEY)) != CRYPT_OK) { return err; } y = PACKET_SIZE; /* init key */ if (mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL) != MP_OKAY) { return CRYPT_MEM; } /* read type/qord */ key->type = in[y++]; key->qord = ((unsigned)in[y]<<8)|((unsigned)in[y+1]); y += 2; /* input publics */ INPUT_BIGNUM(&key->g,in,x,y, inlen); INPUT_BIGNUM(&key->p,in,x,y, inlen); INPUT_BIGNUM(&key->q,in,x,y, inlen); INPUT_BIGNUM(&key->y,in,x,y, inlen); if (key->type == PK_PRIVATE) { INPUT_BIGNUM(&key->x,in,x,y, inlen); } return CRYPT_OK; error: mp_clear_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL); return err; } #endif --- NEW FILE: dsa_verify_hash.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file dsa_verify_hash.c DSA implementation, verify a signature, Tom St Denis */ #ifdef MDSA /** Verify a DSA signature @param sig The signature @param siglen The length of the signature (octets) @param hash The hash that was signed @param hashlen The length of the hash that was signed @param stat [out] The result of the signature verification, 1==valid, 0==invalid @param key The corresponding public DH key @return CRYPT_OK if successful (even if the signature is invalid) */ int dsa_verify_hash(const unsigned char *sig, unsigned long siglen, const unsigned char *hash, unsigned long hashlen, int *stat, dsa_key *key) { mp_int r, s, w, v, u1, u2; int err; LTC_ARGCHK(sig != NULL); LTC_ARGCHK(hash != NULL); LTC_ARGCHK(stat != NULL); LTC_ARGCHK(key != NULL); /* default to invalid signature */ *stat = 0; /* init our variables */ if ((err = mp_init_multi(&r, &s, &w, &v, &u1, &u2, NULL)) != MP_OKAY) { return mpi_to_ltc_error(err); } /* read in r followed by s */ if ((err = der_get_multi_integer(sig, &siglen, &r, &s, NULL)) != CRYPT_OK) { goto done; } /* neither r or s can be null */ if (mp_iszero(&r) == MP_YES || mp_iszero(&s) == MP_YES) { err = CRYPT_INVALID_PACKET; goto done; } /* w = 1/s mod q */ if ((err = mp_invmod(&s, &key->q, &w)) != MP_OKAY) { goto error; } /* u1 = m * w mod q */ if ((err = mp_read_unsigned_bin(&u1, (unsigned char *)hash, hashlen)) != MP_OKAY) { goto error; } if ((err = mp_mulmod(&u1, &w, &key->q, &u1)) != MP_OKAY) { goto error; } /* u2 = r*w mod q */ if ((err = mp_mulmod(&r, &w, &key->q, &u2)) != MP_OKAY) { goto error; } /* v = g^u1 * y^u2 mod p mod q */ if ((err = mp_exptmod(&key->g, &u1, &key->p, &u1)) != MP_OKAY) { goto error; } if ((err = mp_exptmod(&key->y, &u2, &key->p, &u2)) != MP_OKAY) { goto error; } if ((err = mp_mulmod(&u1, &u2, &key->p, &v)) != MP_OKAY) { goto error; } if ((err = mp_mod(&v, &key->q, &v)) != MP_OKAY) { goto error; } /* if r = v then we're set */ if (mp_cmp(&r, &v) == MP_EQ) { *stat = 1; } err = CRYPT_OK; goto done; error : err = mpi_to_ltc_error(err); done : mp_clear_multi(&r, &s, &w, &v, &u1, &u2, NULL); return err; } #endif |
From: Mark F. <mar...@us...> - 2005-04-28 17:28:13
|
Update of /cvsroot/opentnl/tnl/libtomcrypt/pk/ecc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/pk/ecc Added Files: ecc.c ecc_sys.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- NEW FILE: ecc_sys.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ /** @file ecc_sys.c ECC Crypto, Tom St Denis */ /** Encrypt a symmetric key with ECC @param in The symmetric key you want to encrypt @param inlen The length of the key to encrypt (octets) @param out [out] The destination for the ciphertext @param outlen [in/out] The max size and resulting size of the ciphertext @param prng An active PRNG state @param wprng The index of the PRNG you wish to use @param hash The index of the hash you want to use @param key The ECC key you want to encrypt to @return CRYPT_OK if successful */ int ecc_encrypt_key(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, prng_state *prng, int wprng, int hash, ecc_key *key) { unsigned char *pub_expt, *ecc_shared, *skey; ecc_key pubkey; unsigned long x, y, z, hashsize, pubkeysize; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* check that wprng/cipher/hash are not invalid */ if ((err = prng_is_valid(wprng)) != CRYPT_OK) { return err; } if ((err = hash_is_valid(hash)) != CRYPT_OK) { return err; } if (inlen > hash_descriptor[hash].hashsize) { return CRYPT_INVALID_HASH; } /* make a random key and export the public copy */ if ((err = ecc_make_key(prng, wprng, ecc_get_size(key), &pubkey)) != CRYPT_OK) { return err; } pub_expt = XMALLOC(ECC_BUF_SIZE); ecc_shared = XMALLOC(ECC_BUF_SIZE); skey = XMALLOC(MAXBLOCKSIZE); if (pub_expt == NULL || ecc_shared == NULL || skey == NULL) { if (pub_expt != NULL) { XFREE(pub_expt); } if (ecc_shared != NULL) { XFREE(ecc_shared); } if (skey != NULL) { XFREE(skey); } ecc_free(&pubkey); return CRYPT_MEM; } pubkeysize = ECC_BUF_SIZE; if ((err = ecc_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) { ecc_free(&pubkey); goto LBL_ERR; } /* now check if the out buffer is big enough */ if (*outlen < (9 + PACKET_SIZE + pubkeysize + hash_descriptor[hash].hashsize)) { ecc_free(&pubkey); err = CRYPT_BUFFER_OVERFLOW; goto LBL_ERR; } /* make random key */ hashsize = hash_descriptor[hash].hashsize; x = ECC_BUF_SIZE; if ((err = ecc_shared_secret(&pubkey, key, ecc_shared, &x)) != CRYPT_OK) { ecc_free(&pubkey); goto LBL_ERR; } ecc_free(&pubkey); z = MAXBLOCKSIZE; if ((err = hash_memory(hash, ecc_shared, x, skey, &z)) != CRYPT_OK) { goto LBL_ERR; } /* store header */ packet_store_header(out, PACKET_SECT_ECC, PACKET_SUB_ENC_KEY); /* output header */ y = PACKET_SIZE; /* size of hash name and the name itself */ out[y++] = hash_descriptor[hash].ID; /* length of ECC pubkey and the key itself */ STORE32L(pubkeysize, out+y); y += 4; for (x = 0; x < pubkeysize; x++, y++) { out[y] = pub_expt[x]; } STORE32L(inlen, out+y); y += 4; /* Encrypt/Store the encrypted key */ for (x = 0; x < inlen; x++, y++) { out[y] = skey[x] ^ in[x]; } *outlen = y; err = CRYPT_OK; LBL_ERR: #ifdef LTC_CLEAN_STACK /* clean up */ zeromem(pub_expt, ECC_BUF_SIZE); zeromem(ecc_shared, ECC_BUF_SIZE); zeromem(skey, MAXBLOCKSIZE); #endif XFREE(skey); XFREE(ecc_shared); XFREE(pub_expt); return err; } /** Decrypt an ECC encrypted key @param in The ciphertext @param inlen The length of the ciphertext (octets) @param out [out] The plaintext @param outlen [in/out] The max size and resulting size of the plaintext @param key The corresponding private ECC key @return CRYPT_OK if successful */ int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, ecc_key *key) { unsigned char *shared_secret, *skey; unsigned long x, y, z, hashsize, keysize; int hash, err; ecc_key pubkey; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* right key type? */ if (key->type != PK_PRIVATE) { return CRYPT_PK_NOT_PRIVATE; } /* correct length ? */ if (inlen < PACKET_SIZE+1+4+4) { return CRYPT_INVALID_PACKET; } else { inlen -= PACKET_SIZE+1+4+4; } /* is header correct? */ if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_ECC, PACKET_SUB_ENC_KEY)) != CRYPT_OK) { return err; } /* now lets get the hash name */ y = PACKET_SIZE; hash = find_hash_id(in[y++]); if (hash == -1) { return CRYPT_INVALID_HASH; } /* common values */ hashsize = hash_descriptor[hash].hashsize; /* get public key */ LOAD32L(x, in+y); if (inlen < x) { return CRYPT_INVALID_PACKET; } else { inlen -= x; } y += 4; if ((err = ecc_import(in+y, x, &pubkey)) != CRYPT_OK) { return err; } y += x; /* allocate memory */ shared_secret = XMALLOC(ECC_BUF_SIZE); skey = XMALLOC(MAXBLOCKSIZE); if (shared_secret == NULL || skey == NULL) { if (shared_secret != NULL) { XFREE(shared_secret); } if (skey != NULL) { XFREE(skey); } ecc_free(&pubkey); return CRYPT_MEM; } /* make shared key */ x = ECC_BUF_SIZE; if ((err = ecc_shared_secret(key, &pubkey, shared_secret, &x)) != CRYPT_OK) { ecc_free(&pubkey); goto LBL_ERR; } ecc_free(&pubkey); z = MAXBLOCKSIZE; if ((err = hash_memory(hash, shared_secret, x, skey, &z)) != CRYPT_OK) { goto LBL_ERR; } LOAD32L(keysize, in+y); if (inlen < keysize) { err = CRYPT_INVALID_PACKET; goto LBL_ERR; } else { inlen -= keysize; } y += 4; if (*outlen < keysize) { err = CRYPT_BUFFER_OVERFLOW; goto LBL_ERR; } /* Decrypt the key */ for (x = 0; x < keysize; x++, y++) { out[x] = skey[x] ^ in[y]; } *outlen = keysize; err = CRYPT_OK; LBL_ERR: #ifdef LTC_CLEAN_STACK zeromem(shared_secret, ECC_BUF_SIZE); zeromem(skey, MAXBLOCKSIZE); #endif XFREE(skey); XFREE(shared_secret); return err; } /** Sign a message digest @param in The message digest to sign @param inlen The length of the digest @param out [out] The destination for the signature @param outlen [in/out] The max size and resulting size of the signature @param prng An active PRNG state @param wprng The index of the PRNG you wish to use @param key A private ECC key @return CRYPT_OK if successful */ int ecc_sign_hash(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, prng_state *prng, int wprng, ecc_key *key) { ecc_key pubkey; mp_int b, p; unsigned char *epubkey, *er; unsigned long x, y, pubkeysize, rsize; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* is this a private key? */ if (key->type != PK_PRIVATE) { return CRYPT_PK_NOT_PRIVATE; } /* is the IDX valid ? */ if (is_valid_idx(key->idx) != 1) { return CRYPT_PK_INVALID_TYPE; } if ((err = prng_is_valid(wprng)) != CRYPT_OK) { return err; } /* make up a key and export the public copy */ if ((err = ecc_make_key(prng, wprng, ecc_get_size(key), &pubkey)) != CRYPT_OK) { return err; } /* allocate ram */ epubkey = XMALLOC(ECC_BUF_SIZE); er = XMALLOC(ECC_BUF_SIZE); if (epubkey == NULL || er == NULL) { if (epubkey != NULL) { XFREE(epubkey); } if (er != NULL) { XFREE(er); } ecc_free(&pubkey); return CRYPT_MEM; } pubkeysize = ECC_BUF_SIZE; if ((err = ecc_export(epubkey, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) { ecc_free(&pubkey); goto LBL_ERR; } /* get the hash and load it as a bignum into 'b' */ /* init the bignums */ if ((err = mp_init_multi(&b, &p, NULL)) != MP_OKAY) { ecc_free(&pubkey); err = mpi_to_ltc_error(err); goto LBL_ERR; } if ((err = mp_read_radix(&p, (char *)sets[key->idx].order, 64)) != MP_OKAY) { goto error; } if ((err = mp_read_unsigned_bin(&b, (unsigned char *)in, (int)inlen)) != MP_OKAY) { goto error; } /* find b = (m - x)/k */ if ((err = mp_invmod(&pubkey.k, &p, &pubkey.k)) != MP_OKAY) { goto error; } /* k = 1/k */ if ((err = mp_submod(&b, &key->k, &p, &b)) != MP_OKAY) { goto error; } /* b = m - x */ if ((err = mp_mulmod(&b, &pubkey.k, &p, &b)) != MP_OKAY) { goto error; } /* b = (m - x)/k */ /* export it */ rsize = (unsigned long)mp_unsigned_bin_size(&b); if (rsize > ECC_BUF_SIZE) { err = CRYPT_BUFFER_OVERFLOW; goto error; } if ((err = mp_to_unsigned_bin(&b, er)) != MP_OKAY) { goto error; } /* now lets check the outlen before we write */ if (*outlen < (12 + rsize + pubkeysize)) { err = CRYPT_BUFFER_OVERFLOW; goto LBL_ERR; } /* lets output */ y = PACKET_SIZE; /* size of public key */ STORE32L(pubkeysize, out+y); y += 4; /* copy the public key */ for (x = 0; x < pubkeysize; x++, y++) { out[y] = epubkey[x]; } /* size of 'r' */ STORE32L(rsize, out+y); y += 4; /* copy r */ for (x = 0; x < rsize; x++, y++) { out[y] = er[x]; } /* store header */ packet_store_header(out, PACKET_SECT_ECC, PACKET_SUB_SIGNED); *outlen = y; /* all ok */ err = CRYPT_OK; goto LBL_ERR; error: err = mpi_to_ltc_error(err); LBL_ERR: mp_clear_multi(&b, &p, NULL); ecc_free(&pubkey); #ifdef LTC_CLEAN_STACK zeromem(er, ECC_BUF_SIZE); zeromem(epubkey, ECC_BUF_SIZE); #endif XFREE(epubkey); XFREE(er); return err; } /* verify that mG = (bA + Y) * * The signatures work by making up a fresh key "a" with a public key "A". Now we want to sign so the * public key Y = xG can verify it. * * b = (m - x)/k, A is the public key embedded and Y is the users public key [who signed it] * A = kG therefore bA == ((m-x)/k)kG == (m-x)G * * Adding Y = xG to the bA gives us (m-x)G + xG == mG * * The user given only xG, kG and b cannot determine k or x which means they can't find the private key. * */ /** Verify an ECC signature @param sig The signature to verify @param siglen The length of the signature (octets) @param hash The hash (message digest) that was signed @param hashlen The length of the hash (octets) @param stat Result of signature, 1==valid, 0==invalid @param key The corresponding public ECC key @return CRYPT_OK if successful (even if the signature is not valid) */ int ecc_verify_hash(const unsigned char *sig, unsigned long siglen, const unsigned char *hash, unsigned long hashlen, int *stat, ecc_key *key) { ecc_point *mG; ecc_key pubkey; mp_int b, p, m, mu; unsigned long x, y; int err; LTC_ARGCHK(sig != NULL); LTC_ARGCHK(hash != NULL); LTC_ARGCHK(stat != NULL); LTC_ARGCHK(key != NULL); /* default to invalid signature */ *stat = 0; if (siglen < PACKET_SIZE+4+4) { return CRYPT_INVALID_PACKET; } else { siglen -= PACKET_SIZE+4+4; } /* is the message format correct? */ if ((err = packet_valid_header((unsigned char *)sig, PACKET_SECT_ECC, PACKET_SUB_SIGNED)) != CRYPT_OK) { return err; } /* get hash name */ y = PACKET_SIZE; /* get size of public key */ LOAD32L(x, sig+y); if (siglen < x) { return CRYPT_INVALID_PACKET; } else { siglen -= x; } y += 4; /* load the public key */ if ((err = ecc_import((unsigned char*)sig+y, x, &pubkey)) != CRYPT_OK) { return err; } y += x; /* load size of 'b' */ LOAD32L(x, sig+y); if (siglen < x) { return CRYPT_INVALID_PACKET; } else { siglen -= x; } y += 4; /* init values */ if ((err = mp_init_multi(&b, &m, &p, &mu, NULL)) != MP_OKAY) { ecc_free(&pubkey); return mpi_to_ltc_error(err); } mG = new_point(); if (mG == NULL) { mp_clear_multi(&b, &m, &p, &mu, NULL); ecc_free(&pubkey); return CRYPT_MEM; } /* load b */ if ((err = mp_read_unsigned_bin(&b, (unsigned char *)sig+y, (int)x)) != MP_OKAY) { goto error; } y += x; /* get m in binary a bignum */ if ((err = mp_read_unsigned_bin(&m, (unsigned char *)hash, (int)hashlen)) != MP_OKAY) { goto error; } /* load prime */ if ((err = mp_read_radix(&p, (char *)sets[key->idx].prime, 64)) != MP_OKAY) { goto error; } /* calculate barrett stuff */ mp_set(&mu, 1); mp_lshd(&mu, 2 * USED(&p)); if ((err = mp_div(&mu, &p, &mu, NULL)) != MP_OKAY) { goto error; } /* get bA */ if ((err = ecc_mulmod(&b, &pubkey.pubkey, &pubkey.pubkey, &p)) != CRYPT_OK) { goto done; } /* get bA + Y */ if ((err = add_point(&pubkey.pubkey, &key->pubkey, &pubkey.pubkey, &p, &mu)) != CRYPT_OK) { goto done; } /* we have to transform it */ if ((err = ecc_map(&pubkey.pubkey, &p, &mu)) != CRYPT_OK) { goto done; } /* get mG */ if ((err = mp_read_radix(&mG->x, (char *)sets[key->idx].Gx, 64)) != MP_OKAY) { goto error; } if ((err = mp_read_radix(&mG->y, (char *)sets[key->idx].Gy, 64)) != MP_OKAY) { goto error; } mp_set(&mG->z, 1); if ((err = ecc_mulmod(&m, mG, mG, &p)) != CRYPT_OK) { goto done; } /* compare mG to bA + Y */ if (mp_cmp(&mG->x, &pubkey.pubkey.x) == MP_EQ && mp_cmp(&mG->y, &pubkey.pubkey.y) == MP_EQ) { *stat = 1; } /* clear up and return */ err = CRYPT_OK; goto done; error: err = mpi_to_ltc_error(err); done: del_point(mG); ecc_free(&pubkey); mp_clear_multi(&p, &m, &b, &mu, NULL); return err; } --- NEW FILE: ecc.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b * * All curves taken from NIST recommendation paper of July 1999 * Available at http://csrc.nist.gov/cryptval/dss.htm */ #include "tomcrypt.h" /** [...1155 lines suppressed...] /** Get the size of an ECC key @param key The key to get the size of @return The size (octets) of the key or INT_MAX on error */ int ecc_get_size(ecc_key *key) { LTC_ARGCHK(key != NULL); if (is_valid_idx(key->idx)) return sets[key->idx].size; else return INT_MAX; /* large value known to cause it to fail when passed to ecc_make_key() */ } #include "ecc_sys.c" #endif |
From: Mark F. <mar...@us...> - 2005-04-28 17:28:13
|
Update of /cvsroot/opentnl/tnl/libtomcrypt/pk/dh In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/pk/dh Added Files: dh.c dh_sys.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- NEW FILE: dh.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file dh.c DH crypto, Tom St Denis */ #ifdef MDH /* max export size we'll encounter (smaller than this but lets round up a bit) */ #define DH_BUF_SIZE 1200 /* This holds the key settings. ***MUST*** be organized by size from smallest to largest. */ static const struct { int size; char *name, *base, *prime; } sets[] = { #ifdef DH768 { 96, "DH-768", "4", "F///////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "//////m3wvV" }, #endif #ifdef DH1024 { 128, "DH-1024", "4", "F///////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////m3C47" }, #endif #ifdef DH1280 { 160, "DH-1280", "4", "F///////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "//////////////////////////////m4kSN" }, #endif #ifdef DH1536 { 192, "DH-1536", "4", "F///////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////m5uqd" }, #endif #ifdef DH1792 { 224, "DH-1792", "4", "F///////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "//////////////////////////////////////////////////////mT/sd" }, #endif #ifdef DH2048 { 256, "DH-2048", "4", "3///////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "/////////////////////////////////////////m8MPh" }, #endif #ifdef DH2560 { 320, "DH-2560", "4", "3///////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "/////mKFpF" }, #endif #ifdef DH3072 { 384, "DH-3072", "4", "3///////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "/////////////////////////////m32nN" }, #endif #ifdef DH4096 { 512, "DH-4096", "4", "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "////////////////////////////////////////////////////////////" "/////////////////////m8pOF" }, #endif { 0, NULL, NULL, NULL } }; static int is_valid_idx(int n) { int x; for (x = 0; sets[x].size; x++); if ((n < 0) || (n >= x)) { return 0; } return 1; } /** Test the DH sub-system (can take a while) @return CRYPT_OK if successful */ int dh_test(void) { mp_int p, g, tmp; int x, err, primality; if ((err = mp_init_multi(&p, &g, &tmp, NULL)) != MP_OKAY) { goto error; } for (x = 0; sets[x].size != 0; x++) { #if 0 printf("dh_test():testing size %d-bits\n", sets[x].size * 8); #endif if ((err = mp_read_radix(&g,(char *)sets[x].base, 64)) != MP_OKAY) { goto error; } if ((err = mp_read_radix(&p,(char *)sets[x].prime, 64)) != MP_OKAY) { goto error; } /* ensure p is prime */ if ((err = is_prime(&p, &primality)) != CRYPT_OK) { goto done; } if (primality == 0) { err = CRYPT_FAIL_TESTVECTOR; goto done; } if ((err = mp_sub_d(&p, 1, &tmp)) != MP_OKAY) { goto error; } if ((err = mp_div_2(&tmp, &tmp)) != MP_OKAY) { goto error; } /* ensure (p-1)/2 is prime */ if ((err = is_prime(&tmp, &primality)) != CRYPT_OK) { goto done; } if (primality == 0) { err = CRYPT_FAIL_TESTVECTOR; goto done; } /* now see if g^((p-1)/2) mod p is in fact 1 */ if ((err = mp_exptmod(&g, &tmp, &p, &tmp)) != MP_OKAY) { goto error; } if (mp_cmp_d(&tmp, 1)) { err = CRYPT_FAIL_TESTVECTOR; goto done; } } err = CRYPT_OK; goto done; error: err = mpi_to_ltc_error(err); done: mp_clear_multi(&tmp, &g, &p, NULL); return err; } /** Get the min and max DH key sizes (octets) @param low [out] The smallest key size supported @param high [out] The largest key size supported */ void dh_sizes(int *low, int *high) { int x; LTC_ARGCHK(low != NULL); LTC_ARGCHK(high != NULL); *low = INT_MAX; *high = 0; for (x = 0; sets[x].size != 0; x++) { if (*low > sets[x].size) *low = sets[x].size; if (*high < sets[x].size) *high = sets[x].size; } } /** Returns the key size of a given DH key (octets) @param key The DH key to get the size of @return The size if valid or INT_MAX if not */ int dh_get_size(dh_key *key) { LTC_ARGCHK(key != NULL); if (is_valid_idx(key->idx) == 1) { return sets[key->idx].size; } else { return INT_MAX; /* large value that would cause dh_make_key() to fail */ } } /** Make a DH key [private key pair] @param prng An active PRNG state @param wprng The index for the PRNG you desire to use @param keysize The key size (octets) desired @param key [out] Where the newly created DH key will be stored @return CRYPT_OK if successful, note: on error all allocated memory will be freed automatically. */ int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key) { unsigned char *buf; unsigned long x; mp_int p, g; int err; LTC_ARGCHK(key != NULL); /* good prng? */ if ((err = prng_is_valid(wprng)) != CRYPT_OK) { return err; } /* find key size */ for (x = 0; (keysize > sets[x].size) && (sets[x].size != 0); x++); #ifdef FAST_PK keysize = MIN(sets[x].size, 32); #else keysize = sets[x].size; #endif if (sets[x].size == 0) { return CRYPT_INVALID_KEYSIZE; } key->idx = x; /* allocate buffer */ buf = XMALLOC(keysize); if (buf == NULL) { return CRYPT_MEM; } /* make up random string */ if (prng_descriptor[wprng].read(buf, keysize, prng) != (unsigned long)keysize) { err = CRYPT_ERROR_READPRNG; goto error2; } /* init parameters */ if ((err = mp_init_multi(&g, &p, &key->x, &key->y, NULL)) != MP_OKAY) { goto error; } if ((err = mp_read_radix(&g, sets[key->idx].base, 64)) != MP_OKAY) { goto error; } if ((err = mp_read_radix(&p, sets[key->idx].prime, 64)) != MP_OKAY) { goto error; } /* load the x value */ if ((err = mp_read_unsigned_bin(&key->x, buf, keysize)) != MP_OKAY) { goto error; } if ((err = mp_exptmod(&g, &key->x, &p, &key->y)) != MP_OKAY) { goto error; } key->type = PK_PRIVATE; if ((err = mp_shrink(&key->x)) != MP_OKAY) { goto error; } if ((err = mp_shrink(&key->y)) != MP_OKAY) { goto error; } /* free up ram */ err = CRYPT_OK; goto done; error: err = mpi_to_ltc_error(err); error2: mp_clear_multi(&key->x, &key->y, NULL); done: #ifdef LTC_CLEAN_STACK zeromem(buf, keysize); #endif mp_clear_multi(&p, &g, NULL); XFREE(buf); return err; } /** Free the allocated ram for a DH key @param key The key which you wish to free */ void dh_free(dh_key *key) { LTC_ARGCHK(key != NULL); mp_clear_multi(&key->x, &key->y, NULL); } /** Export a DH key to a binary packet @param out [out] The destination for the key @param outlen [in/out] The max size and resulting size of the DH key @param type Which type of key (PK_PRIVATE or PK_PUBLIC) @param key The key you wish to export @return CRYPT_OK if successful */ int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key) { unsigned long y, z; int err; LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* can we store the static header? */ if (*outlen < (PACKET_SIZE + 2)) { return CRYPT_BUFFER_OVERFLOW; } if (type == PK_PRIVATE && key->type != PK_PRIVATE) { return CRYPT_PK_NOT_PRIVATE; } /* header */ y = PACKET_SIZE; /* header */ out[y++] = type; out[y++] = (unsigned char)(sets[key->idx].size / 8); /* export y */ OUTPUT_BIGNUM(&key->y, out, y, z); if (type == PK_PRIVATE) { /* export x */ OUTPUT_BIGNUM(&key->x, out, y, z); } /* store header */ packet_store_header(out, PACKET_SECT_DH, PACKET_SUB_KEY); /* store len */ *outlen = y; return CRYPT_OK; } /** Import a DH key from a binary packet @param in The packet to read @param inlen The length of the input packet @param key [out] Where to import the key to @return CRYPT_OK if successful, on error all allocated memory is freed automatically */ int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key) { unsigned long x, y, s; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(key != NULL); /* make sure valid length */ if ((2+PACKET_SIZE) > inlen) { return CRYPT_INVALID_PACKET; } /* check type byte */ if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_DH, PACKET_SUB_KEY)) != CRYPT_OK) { return err; } /* init */ if ((err = mp_init_multi(&key->x, &key->y, NULL)) != MP_OKAY) { return mpi_to_ltc_error(err); } /* advance past packet header */ y = PACKET_SIZE; /* key type, e.g. private, public */ key->type = (int)in[y++]; /* key size in bytes */ s = (unsigned long)in[y++] * 8; for (x = 0; (s > (unsigned long)sets[x].size) && (sets[x].size != 0); x++); if (sets[x].size == 0) { err = CRYPT_INVALID_KEYSIZE; goto error; } key->idx = (int)x; /* type check both values */ if ((key->type != PK_PUBLIC) && (key->type != PK_PRIVATE)) { err = CRYPT_PK_TYPE_MISMATCH; goto error; } /* is the key idx valid? */ if (is_valid_idx(key->idx) != 1) { err = CRYPT_PK_TYPE_MISMATCH; goto error; } /* load public value g^x mod p*/ INPUT_BIGNUM(&key->y, in, x, y, inlen); if (key->type == PK_PRIVATE) { INPUT_BIGNUM(&key->x, in, x, y, inlen); } /* eliminate private key if public */ if (key->type == PK_PUBLIC) { mp_clear(&key->x); } return CRYPT_OK; error: mp_clear_multi(&key->y, &key->x, NULL); return err; } /** Create a DH shared secret. @param private_key The private DH key in the pair @param public_key The public DH key in the pair @param out [out] The destination of the shared data @param outlen [in/out] The max size and resulting size of the shared data. @return CRYPT_OK if successful */ int dh_shared_secret(dh_key *private_key, dh_key *public_key, unsigned char *out, unsigned long *outlen) { mp_int tmp, p; unsigned long x; int err; LTC_ARGCHK(private_key != NULL); LTC_ARGCHK(public_key != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); /* types valid? */ if (private_key->type != PK_PRIVATE) { return CRYPT_PK_NOT_PRIVATE; } /* same idx? */ if (private_key->idx != public_key->idx) { return CRYPT_PK_TYPE_MISMATCH; } /* compute y^x mod p */ if ((err = mp_init_multi(&tmp, &p, NULL)) != MP_OKAY) { return mpi_to_ltc_error(err); } if ((err = mp_read_radix(&p, (char *)sets[private_key->idx].prime, 64)) != MP_OKAY) { goto error; } if ((err = mp_exptmod(&public_key->y, &private_key->x, &p, &tmp)) != MP_OKAY) { goto error; } /* enough space for output? */ x = (unsigned long)mp_unsigned_bin_size(&tmp); if (*outlen < x) { err = CRYPT_BUFFER_OVERFLOW; goto done; } if ((err = mp_to_unsigned_bin(&tmp, out)) != MP_OKAY) { goto error; } *outlen = x; err = CRYPT_OK; goto done; error: err = mpi_to_ltc_error(err); done: mp_clear_multi(&p, &tmp, NULL); return err; } #include "dh_sys.c" #endif --- NEW FILE: dh_sys.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ /** @file dh_sys.c DH Crypto, Tom St Denis */ /** Encrypt a short symmetric key with a public DH key @param in The symmetric key to encrypt @param inlen The length of the key (octets) @param out [out] The ciphertext @param outlen [in/out] The max size and resulting size of the ciphertext @param prng An active PRNG state @param wprng The index of the PRNG desired @param hash The index of the hash desired (must produce a digest of size >= the size of the plaintext) @param key The public key you wish to encrypt with. @return CRYPT_OK if successful */ int dh_encrypt_key(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, prng_state *prng, int wprng, int hash, dh_key *key) { unsigned char *pub_expt, *dh_shared, *skey; dh_key pubkey; unsigned long x, y, z, hashsize, pubkeysize; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* check that wprng/hash are not invalid */ if ((err = prng_is_valid(wprng)) != CRYPT_OK) { return err; } if ((err = hash_is_valid(hash)) != CRYPT_OK) { return err; } if (inlen > hash_descriptor[hash].hashsize) { return CRYPT_INVALID_HASH; } /* allocate memory */ pub_expt = XMALLOC(DH_BUF_SIZE); dh_shared = XMALLOC(DH_BUF_SIZE); skey = XMALLOC(MAXBLOCKSIZE); if (pub_expt == NULL || dh_shared == NULL || skey == NULL) { if (pub_expt != NULL) { XFREE(pub_expt); } if (dh_shared != NULL) { XFREE(dh_shared); } if (skey != NULL) { XFREE(skey); } return CRYPT_MEM; } /* make a random key and export the public copy */ if ((err = dh_make_key(prng, wprng, dh_get_size(key), &pubkey)) != CRYPT_OK) { goto LBL_ERR; } pubkeysize = DH_BUF_SIZE; if ((err = dh_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) { dh_free(&pubkey); goto LBL_ERR; } /* now check if the out buffer is big enough */ if (*outlen < (1 + 4 + 4 + PACKET_SIZE + pubkeysize + inlen)) { dh_free(&pubkey); err = CRYPT_BUFFER_OVERFLOW; goto LBL_ERR; } /* make random key */ hashsize = hash_descriptor[hash].hashsize; x = DH_BUF_SIZE; if ((err = dh_shared_secret(&pubkey, key, dh_shared, &x)) != CRYPT_OK) { dh_free(&pubkey); goto LBL_ERR; } dh_free(&pubkey); z = MAXBLOCKSIZE; if ((err = hash_memory(hash, dh_shared, x, skey, &z)) != CRYPT_OK) { goto LBL_ERR; } /* store header */ packet_store_header(out, PACKET_SECT_DH, PACKET_SUB_ENC_KEY); /* output header */ y = PACKET_SIZE; /* size of hash name and the name itself */ out[y++] = hash_descriptor[hash].ID; /* length of DH pubkey and the key itself */ STORE32L(pubkeysize, out+y); y += 4; for (x = 0; x < pubkeysize; x++, y++) { out[y] = pub_expt[x]; } /* Store the encrypted key */ STORE32L(inlen, out+y); y += 4; for (x = 0; x < inlen; x++, y++) { out[y] = skey[x] ^ in[x]; } *outlen = y; err = CRYPT_OK; LBL_ERR: #ifdef LTC_CLEAN_STACK /* clean up */ zeromem(pub_expt, DH_BUF_SIZE); zeromem(dh_shared, DH_BUF_SIZE); zeromem(skey, MAXBLOCKSIZE); #endif XFREE(skey); XFREE(dh_shared); XFREE(pub_expt); return err; } /** Decrypt a DH encrypted symmetric key @param in The DH encrypted packet @param inlen The length of the DH encrypted packet @param out The plaintext @param outlen [in/out] The max size and resulting size of the plaintext @param key The private DH key corresponding to the public key that encrypted the plaintext @return CRYPT_OK if successful */ int dh_decrypt_key(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, dh_key *key) { unsigned char *shared_secret, *skey; unsigned long x, y, z, hashsize, keysize; int hash, err; dh_key pubkey; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* right key type? */ if (key->type != PK_PRIVATE) { return CRYPT_PK_NOT_PRIVATE; } /* allocate ram */ shared_secret = XMALLOC(DH_BUF_SIZE); skey = XMALLOC(MAXBLOCKSIZE); if (shared_secret == NULL || skey == NULL) { if (shared_secret != NULL) { XFREE(shared_secret); } if (skey != NULL) { XFREE(skey); } return CRYPT_MEM; } /* check if initial header should fit */ if (inlen < PACKET_SIZE+1+4+4) { err = CRYPT_INVALID_PACKET; goto LBL_ERR; } else { inlen -= PACKET_SIZE+1+4+4; } /* is header correct? */ if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_DH, PACKET_SUB_ENC_KEY)) != CRYPT_OK) { goto LBL_ERR; } /* now lets get the hash name */ y = PACKET_SIZE; hash = find_hash_id(in[y++]); if (hash == -1) { err = CRYPT_INVALID_HASH; goto LBL_ERR; } /* common values */ hashsize = hash_descriptor[hash].hashsize; /* get public key */ LOAD32L(x, in+y); /* now check if the imported key will fit */ if (inlen < x) { err = CRYPT_INVALID_PACKET; goto LBL_ERR; } else { inlen -= x; } y += 4; if ((err = dh_import(in+y, x, &pubkey)) != CRYPT_OK) { goto LBL_ERR; } y += x; /* make shared key */ x = DH_BUF_SIZE; if ((err = dh_shared_secret(key, &pubkey, shared_secret, &x)) != CRYPT_OK) { dh_free(&pubkey); goto LBL_ERR; } dh_free(&pubkey); z = MAXBLOCKSIZE; if ((err = hash_memory(hash, shared_secret, x, skey, &z)) != CRYPT_OK) { goto LBL_ERR; } /* load in the encrypted key */ LOAD32L(keysize, in+y); /* will the out fit as part of the input */ if (inlen < keysize) { err = CRYPT_INVALID_PACKET; goto LBL_ERR; } else { inlen -= keysize; } if (keysize > *outlen) { err = CRYPT_BUFFER_OVERFLOW; goto LBL_ERR; } y += 4; *outlen = keysize; for (x = 0; x < keysize; x++, y++) { out[x] = skey[x] ^ in[y]; } err = CRYPT_OK; LBL_ERR: #ifdef LTC_CLEAN_STACK zeromem(shared_secret, DH_BUF_SIZE); zeromem(skey, MAXBLOCKSIZE); #endif XFREE(skey); XFREE(shared_secret); return err; } /* perform an ElGamal Signature of a hash * * The math works as follows. x is the private key, M is the message to sign 1. pick a random k 2. compute a = g^k mod p 3. compute b = (M - xa)/k mod p 4. Send (a,b) Now to verify with y=g^x mod p, a and b 1. compute y^a * a^b = g^(xa) * g^(k*(M-xa)/k) = g^(xa + (M - xa)) = g^M [all mod p] 2. Compare against g^M mod p [based on input hash]. 3. If result of #2 == result of #1 then signature valid */ /** Sign a message digest using a DH private key @param in The data to sign @param inlen The length of the input (octets) @param out [out] The destination of the signature @param outlen [in/out] The max size and resulting size of the output @param prng An active PRNG state @param wprng The index of the PRNG desired @param key A private DH key @return CRYPT_OK if successful */ int dh_sign_hash(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, prng_state *prng, int wprng, dh_key *key) { mp_int a, b, k, m, g, p, p1, tmp; unsigned char *buf; unsigned long x, y; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* check parameters */ if (key->type != PK_PRIVATE) { return CRYPT_PK_NOT_PRIVATE; } if ((err = prng_is_valid(wprng)) != CRYPT_OK) { return err; } /* is the IDX valid ? */ if (is_valid_idx(key->idx) != 1) { return CRYPT_PK_INVALID_TYPE; } /* allocate ram for buf */ buf = XMALLOC(520); /* make up a random value k, * since the order of the group is prime * we need not check if gcd(k, r) is 1 */ if (prng_descriptor[wprng].read(buf, sets[key->idx].size, prng) != (unsigned long)(sets[key->idx].size)) { err = CRYPT_ERROR_READPRNG; goto LBL_ERR; } /* init bignums */ if ((err = mp_init_multi(&a, &b, &k, &m, &p, &g, &p1, &tmp, NULL)) != MP_OKAY) { err = mpi_to_ltc_error(err); goto LBL_ERR; } /* load k and m */ if ((err = mp_read_unsigned_bin(&m, (unsigned char *)in, inlen)) != MP_OKAY) { goto error; } if ((err = mp_read_unsigned_bin(&k, buf, sets[key->idx].size)) != MP_OKAY) { goto error; } /* load g, p and p1 */ if ((err = mp_read_radix(&g, sets[key->idx].base, 64)) != MP_OKAY) { goto error; } if ((err = mp_read_radix(&p, sets[key->idx].prime, 64)) != MP_OKAY) { goto error; } if ((err = mp_sub_d(&p, 1, &p1)) != MP_OKAY) { goto error; } if ((err = mp_div_2(&p1, &p1)) != MP_OKAY) { goto error; } /* p1 = (p-1)/2 */ /* now get a = g^k mod p */ if ((err = mp_exptmod(&g, &k, &p, &a)) != MP_OKAY) { goto error; } /* now find M = xa + kb mod p1 or just b = (M - xa)/k mod p1 */ if ((err = mp_invmod(&k, &p1, &k)) != MP_OKAY) { goto error; } /* k = 1/k mod p1 */ if ((err = mp_mulmod(&a, &key->x, &p1, &tmp)) != MP_OKAY) { goto error; } /* tmp = xa */ if ((err = mp_submod(&m, &tmp, &p1, &tmp)) != MP_OKAY) { goto error; } /* tmp = M - xa */ if ((err = mp_mulmod(&k, &tmp, &p1, &b)) != MP_OKAY) { goto error; } /* b = (M - xa)/k */ /* check for overflow */ if ((unsigned long)(PACKET_SIZE + 4 + 4 + mp_unsigned_bin_size(&a) + mp_unsigned_bin_size(&b)) > *outlen) { err = CRYPT_BUFFER_OVERFLOW; goto LBL_ERR; } /* store header */ y = PACKET_SIZE; /* now store them both (a,b) */ x = (unsigned long)mp_unsigned_bin_size(&a); STORE32L(x, out+y); y += 4; if ((err = mp_to_unsigned_bin(&a, out+y)) != MP_OKAY) { goto error; } y += x; x = (unsigned long)mp_unsigned_bin_size(&b); STORE32L(x, out+y); y += 4; if ((err = mp_to_unsigned_bin(&b, out+y)) != MP_OKAY) { goto error; } y += x; /* check if size too big */ if (*outlen < y) { err = CRYPT_BUFFER_OVERFLOW; goto LBL_ERR; } /* store header */ packet_store_header(out, PACKET_SECT_DH, PACKET_SUB_SIGNED); *outlen = y; err = CRYPT_OK; goto LBL_ERR; error: err = mpi_to_ltc_error(err); LBL_ERR: mp_clear_multi(&tmp, &p1, &g, &p, &m, &k, &b, &a, NULL); XFREE(buf); return err; } /** Verify the signature given @param sig The signature @param siglen The length of the signature (octets) @param hash The hash that was signed @param hashlen The length of the hash (octets) @param stat [out] Result of signature comparison, 1==valid, 0==invalid @param key The public DH key that signed the hash @return CRYPT_OK if succsessful (even if signature is invalid) */ int dh_verify_hash(const unsigned char *sig, unsigned long siglen, const unsigned char *hash, unsigned long hashlen, int *stat, dh_key *key) { mp_int a, b, p, g, m, tmp; unsigned long x, y; int err; LTC_ARGCHK(sig != NULL); LTC_ARGCHK(hash != NULL); LTC_ARGCHK(stat != NULL); LTC_ARGCHK(key != NULL); /* default to invalid */ *stat = 0; /* check initial input length */ if (siglen < PACKET_SIZE+4+4) { return CRYPT_INVALID_PACKET; } /* header ok? */ if ((err = packet_valid_header((unsigned char *)sig, PACKET_SECT_DH, PACKET_SUB_SIGNED)) != CRYPT_OK) { return err; } /* get hash out of packet */ y = PACKET_SIZE; /* init all bignums */ if ((err = mp_init_multi(&a, &p, &b, &g, &m, &tmp, NULL)) != MP_OKAY) { return mpi_to_ltc_error(err); } /* load a and b */ INPUT_BIGNUM(&a, sig, x, y, siglen); INPUT_BIGNUM(&b, sig, x, y, siglen); /* load p and g */ if ((err = mp_read_radix(&p, sets[key->idx].prime, 64)) != MP_OKAY) { goto error1; } if ((err = mp_read_radix(&g, sets[key->idx].base, 64)) != MP_OKAY) { goto error1; } /* load m */ if ((err = mp_read_unsigned_bin(&m, (unsigned char *)hash, hashlen)) != MP_OKAY) { goto error1; } /* find g^m mod p */ if ((err = mp_exptmod(&g, &m, &p, &m)) != MP_OKAY) { goto error1; } /* m = g^m mod p */ /* find y^a * a^b */ if ((err = mp_exptmod(&key->y, &a, &p, &tmp)) != MP_OKAY) { goto error1; } /* tmp = y^a mod p */ if ((err = mp_exptmod(&a, &b, &p, &a)) != MP_OKAY) { goto error1; } /* a = a^b mod p */ if ((err = mp_mulmod(&a, &tmp, &p, &a)) != MP_OKAY) { goto error1; } /* a = y^a * a^b mod p */ /* y^a * a^b == g^m ??? */ if (mp_cmp(&a, &m) == 0) { *stat = 1; } /* clean up */ err = CRYPT_OK; goto done; error1: err = mpi_to_ltc_error(err); error: done: mp_clear_multi(&tmp, &m, &g, &p, &b, &a, NULL); return err; } |
From: Mark F. <mar...@us...> - 2005-04-28 17:28:13
|
Update of /cvsroot/opentnl/tnl/libtomcrypt/pk/asn1/der In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/pk/asn1/der Added Files: der_decode_integer.c der_encode_integer.c der_get_multi_integer.c der_length_integer.c der_put_multi_integer.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- NEW FILE: der_put_multi_integer.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include <stdarg.h> #include "tomcrypt.h" /** @file der_put_multi_integer.c ASN.1 DER, store multiple integers, Tom St Denis */ #ifdef LTC_DER /* store multiple mp_ints in DER INTEGER format to the out, will not * overflow the length you give it [outlen] and store the number of * bytes used in [outlen] */ /** Store multiple mp_int integers one after another @param out [out] The destination for the DER encoded integers @param outlen [in/out] The max size and resulting size of the DER encoded integers @param num The first mp_int to encode @param ... A NULL terminated list of mp_ints to encode @return CRYPT_OK if successful */ int der_put_multi_integer(unsigned char *out, unsigned long *outlen, mp_int *num, ...) { va_list args; mp_int *next; unsigned long wrote, len; int err; LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); /* setup va list */ next = num; len = *outlen; wrote = 0; va_start(args, num); while (next != NULL) { if ((err = der_encode_integer(next, out, outlen)) != CRYPT_OK) { va_end(args); return err; } wrote += *outlen; out += *outlen; len -= *outlen; *outlen = len; next = va_arg(args, mp_int*); } va_end(args); *outlen = wrote; return CRYPT_OK; } #endif --- NEW FILE: der_encode_integer.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file der_encode_integer.c ASN.1 DER, encode an integer, Tom St Denis */ #ifdef LTC_DER /* Exports a positive bignum as DER format (upto 2^32 bytes in size) */ /** Store a mp_int integer @param num The first mp_int to encode @param out [out] The destination for the DER encoded integers @param outlen [in/out] The max size and resulting size of the DER encoded integers @return CRYPT_OK if successful */ int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen) { unsigned long tmplen, x, y, z; int err, leading_zero; LTC_ARGCHK(num != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); /* find out how big this will be */ if ((err = der_length_integer(num, &tmplen)) != CRYPT_OK) { return err; } if (*outlen < tmplen) { return CRYPT_BUFFER_OVERFLOW; } /* we only need a leading zero if the msb of the first byte is one */ if ((mp_count_bits(num) & 7) == 7 || mp_iszero(num) == MP_YES) { leading_zero = 1; } else { leading_zero = 0; } /* get length of num in bytes (plus 1 since we force the msbyte to zero) */ y = mp_unsigned_bin_size(num) + leading_zero; /* now store initial data */ *out++ = 0x02; if (y < 128) { /* short form */ *out++ = (unsigned char)y; } else { /* long form (relies on y != 0) */ /* get length of length... ;-) */ x = y; z = 0; while (x) { ++z; x >>= 8; } /* store length of length */ *out++ = 0x80 | ((unsigned char)z); /* now store length */ /* first shift length up so msbyte != 0 */ x = y; while ((x & 0xFF000000) == 0) { x <<= 8; } /* now store length */ while (z--) { *out++ = (unsigned char)((x >> 24) & 0xFF); x <<= 8; } } /* now store msbyte of zero if num is non-zero */ if (leading_zero) { *out++ = 0x00; } /* if it's not zero store it as big endian */ if (mp_iszero(num) == MP_NO) { /* now store the mpint */ if ((err = mp_to_unsigned_bin(num, out)) != MP_OKAY) { return mpi_to_ltc_error(err); } } /* we good */ *outlen = tmplen; return CRYPT_OK; } #endif --- NEW FILE: der_decode_integer.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file der_decode_integer.c ASN.1 DER, decode an integer, Tom St Denis */ #ifdef LTC_DER /* decodes a DER INTEGER in [in]. You have to tell this function * how many bytes are available [inlen]. It will then attempt to * read the INTEGER. If all goes well it stores the number of bytes * read in [inlen] and the number in [num]. */ /** Read a mp_int integer @param in The DER encoded data @param inlen [in] Size of the in data, [out] number of bytes read @param num The first mp_int to decode @return CRYPT_OK if successful */ int der_decode_integer(const unsigned char *in, unsigned long *inlen, mp_int *num) { unsigned long tmplen, y, z; LTC_ARGCHK(num != NULL); LTC_ARGCHK(in != NULL); LTC_ARGCHK(inlen != NULL); /* save copy of max output size */ tmplen = *inlen; *inlen = 0; /* min DER INTEGER is 0x02 01 00 == 0 */ if (tmplen < (1 + 1 + 1)) { return CRYPT_INVALID_PACKET; } /* ok expect 0x02 when we AND with 0011 1111 [3F] */ if ((*in++ & 0x3F) != 0x02) { return CRYPT_INVALID_PACKET; } ++(*inlen); /* now decode the len stuff */ z = *in++; ++(*inlen); if ((z & 0x80) == 0x00) { /* short form */ /* will it overflow? */ if (*inlen + z > tmplen) { return CRYPT_INVALID_PACKET; } /* no so read it */ (*inlen) += z; return mpi_to_ltc_error(mp_read_unsigned_bin(num, (unsigned char *)in, z)); } else { /* long form */ z &= 0x7F; /* will number of length bytes overflow? (or > 4) */ if (((*inlen + z) > tmplen) || (z > 4)) { return CRYPT_INVALID_PACKET; } /* now read it in */ y = 0; while (z--) { y = ((unsigned long)(*in++)) | (y << 8); ++(*inlen); } /* now will reading y bytes overrun? */ if ((*inlen + y) > tmplen) { return CRYPT_INVALID_PACKET; } /* no so read it */ (*inlen) += y; return mpi_to_ltc_error(mp_read_unsigned_bin(num, (unsigned char *)in, y)); } } #endif --- NEW FILE: der_length_integer.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file der_length_integer.c ASN.1 DER, get length of encoding, Tom St Denis */ #ifdef LTC_DER /** Gets length of DER encoding of num @param num The mp_int to get the size of @param outlen [out] The length of the DER encoding for the given integer @return CRYPT_OK if successful */ int der_length_integer(mp_int *num, unsigned long *outlen) { unsigned long z, len; int leading_zero; LTC_ARGCHK(num != NULL); LTC_ARGCHK(outlen != NULL); /* we only need a leading zero if the msb of the first byte is one */ if ((mp_count_bits(num) & 7) == 7 || mp_iszero(num) == MP_YES) { leading_zero = 1; } else { leading_zero = 0; } /* size for bignum */ z = len = leading_zero + mp_unsigned_bin_size(num); /* we need a 0x02 */ ++len; /* now we need a length */ if (z < 128) { /* short form */ ++len; } else { /* long form (relies on z != 0) */ ++len; while (z) { ++len; z >>= 8; } } *outlen = len; return CRYPT_OK; } #endif --- NEW FILE: der_get_multi_integer.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include <stdarg.h> #include "tomcrypt.h" /** @file der_get_multi_integer.c ASN.1 DER, read multiple integers, Tom St Denis */ #ifdef LTC_DER /* will read multiple DER INTEGER encoded mp_ints from src * of upto [inlen] bytes. It will store the number of bytes * read back into [inlen]. */ /** Read multiple mp_int integers one after another @param src The DER encoded integers @param inlen [in] The length of the src buffer, [out] the amount of bytes read @param num The first mp_int to decode @param ... A NULL terminated list of mp_ints to decode @return CRYPT_OK if successful */ int der_get_multi_integer(const unsigned char *src, unsigned long *inlen, mp_int *num, ...) { va_list args; mp_int *next; unsigned long wrote, len; int err; LTC_ARGCHK(src != NULL); LTC_ARGCHK(inlen != NULL); /* setup va list */ next = num; len = *inlen; wrote = 0; va_start(args, num); while (next != NULL) { if ((err = der_decode_integer(src, inlen, next)) != CRYPT_OK) { va_end(args); return err; } wrote += *inlen; src += *inlen; len -= *inlen; *inlen = len; next = va_arg(args, mp_int*); } va_end(args); *inlen = wrote; return CRYPT_OK; } #endif |
From: Mark F. <mar...@us...> - 2005-04-28 17:28:13
|
Update of /cvsroot/opentnl/tnl/libtomcrypt/mac/pelican In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/mac/pelican Added Files: pelican.c pelican_memory.c pelican_test.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- NEW FILE: pelican_memory.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file pelican_memory.c Pelican MAC, MAC a block of memory, by Tom St Denis */ #ifdef PELICAN /** Pelican block of memory @param key The key for the MAC @param keylen The length of the key (octets) @param in The input to MAC @param inlen The length of the input (octets) @param out [out] The output TAG @return CRYPT_OK on success */ int pelican_memory(const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *out) { pelican_state *pel; int err; pel = XMALLOC(sizeof(*pel)); if (pel == NULL) { return CRYPT_MEM; } if ((err = pelican_init(pel, key, keylen)) != CRYPT_OK) { XFREE(pel); return err; } if ((err = pelican_process(pel, in ,inlen)) != CRYPT_OK) { XFREE(pel); return err; } err = pelican_done(pel, out); XFREE(pel); return err; } #endif --- NEW FILE: pelican_test.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file pelican_test.c Pelican MAC, test, by Tom St Denis */ #ifdef PELICAN int pelican_test(void) { #ifndef LTC_TEST return CRYPT_NOP; #else static const struct { unsigned char K[32], MSG[64], T[16]; int keylen, ptlen; } tests[] = { /* K=16, M=0 */ { { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }, { 0 }, { 0xeb, 0x58, 0x37, 0x15, 0xf8, 0x34, 0xde, 0xe5, 0xa4, 0xd1, 0x6e, 0xe4, 0xb9, 0xd7, 0x76, 0x0e, }, 16, 0 }, /* K=16, M=3 */ { { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }, { 0x00, 0x01, 0x02 }, { 0x1c, 0x97, 0x40, 0x60, 0x6c, 0x58, 0x17, 0x2d, 0x03, 0x94, 0x19, 0x70, 0x81, 0xc4, 0x38, 0x54, }, 16, 3 }, /* K=16, M=16 */ { { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }, { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }, { 0x03, 0xcc, 0x46, 0xb8, 0xac, 0xa7, 0x9c, 0x36, 0x1e, 0x8c, 0x6e, 0xa6, 0x7b, 0x89, 0x32, 0x49, }, 16, 16 }, /* K=16, M=32 */ { { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }, { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F }, { 0x89, 0xcc, 0x36, 0x58, 0x1b, 0xdd, 0x4d, 0xb5, 0x78, 0xbb, 0xac, 0xf0, 0xff, 0x8b, 0x08, 0x15, }, 16, 32 }, /* K=16, M=35 */ { { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }, { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x23 }, { 0x4a, 0x7d, 0x45, 0x4d, 0xcd, 0xb5, 0xda, 0x8d, 0x48, 0x78, 0x16, 0x48, 0x5d, 0x45, 0x95, 0x99, }, 16, 35 }, }; int x, err; unsigned char out[16]; pelican_state pel; for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { if ((err = pelican_init(&pel, tests[x].K, tests[x].keylen)) != CRYPT_OK) { return err; } if ((err = pelican_process(&pel, tests[x].MSG, tests[x].ptlen)) != CRYPT_OK) { return err; } if ((err = pelican_done(&pel, out)) != CRYPT_OK) { return err; } if (memcmp(out, tests[x].T, 16)) { #if 0 int y; printf("\nFailed test %d\n", x); printf("{ "); for (y = 0; y < 16; ) { printf("0x%02x, ", out[y]); if (!(++y & 7)) printf("\n"); } printf(" }\n"); #endif return CRYPT_FAIL_TESTVECTOR; } } return CRYPT_OK; #endif } #endif --- NEW FILE: pelican.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file pelican.c Pelican MAC, initialize state, by Tom St Denis */ #ifdef PELICAN #define ENCRYPT_ONLY #include "../../ciphers/aes/aes_tab.c" /** Initialize a Pelican state @param pelmac The Pelican state to initialize @param key The secret key @param keylen The length of the secret key (octets) @return CRYPT_OK if successful */ int pelican_init(pelican_state *pelmac, const unsigned char *key, unsigned long keylen) { int err; LTC_ARGCHK(pelmac != NULL); LTC_ARGCHK(key != NULL); #ifdef LTC_FAST if (16 % sizeof(LTC_FAST_TYPE)) { return CRYPT_INVALID_ARG; } #endif if ((err = aes_setup(key, keylen, 0, &pelmac->K)) != CRYPT_OK) { return err; } zeromem(pelmac->state, 16); aes_ecb_encrypt(pelmac->state, pelmac->state, &pelmac->K); pelmac->buflen = 0; return CRYPT_OK; } static void four_rounds(pelican_state *pelmac) { ulong32 s0, s1, s2, s3, t0, t1, t2, t3; int r; LOAD32H(s0, pelmac->state ); LOAD32H(s1, pelmac->state + 4); LOAD32H(s2, pelmac->state + 8); LOAD32H(s3, pelmac->state + 12); for (r = 0; r < 4; r++) { t0 = Te0(byte(s0, 3)) ^ Te1(byte(s1, 2)) ^ Te2(byte(s2, 1)) ^ Te3(byte(s3, 0)); t1 = Te0(byte(s1, 3)) ^ Te1(byte(s2, 2)) ^ Te2(byte(s3, 1)) ^ Te3(byte(s0, 0)); t2 = Te0(byte(s2, 3)) ^ Te1(byte(s3, 2)) ^ Te2(byte(s0, 1)) ^ Te3(byte(s1, 0)); t3 = Te0(byte(s3, 3)) ^ Te1(byte(s0, 2)) ^ Te2(byte(s1, 1)) ^ Te3(byte(s2, 0)); s0 = t0; s1 = t1; s2 = t2; s3 = t3; } STORE32H(s0, pelmac->state ); STORE32H(s1, pelmac->state + 4); STORE32H(s2, pelmac->state + 8); STORE32H(s3, pelmac->state + 12); } /** Process a block of text through Pelican @param pelmac The Pelican MAC state @param in The input @param inlen The length input (octets) @return CRYPT_OK on success */ int pelican_process(pelican_state *pelmac, const unsigned char *in, unsigned long inlen) { LTC_ARGCHK(pelmac != NULL); LTC_ARGCHK(in != NULL); #ifdef LTC_FAST if (pelmac->buflen == 0) { while (inlen & ~15) { int x; for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) { *((LTC_FAST_TYPE*)((unsigned char *)pelmac->state + x)) ^= *((LTC_FAST_TYPE*)((unsigned char *)in + x)); } four_rounds(pelmac); in += 16; inlen -= 16; } } #endif while (inlen--) { pelmac->state[pelmac->buflen++] ^= *in++; if (pelmac->buflen == 16) { four_rounds(pelmac); pelmac->buflen = 0; } } return CRYPT_OK; } /** Terminate Pelican MAC @param pelmac The Pelican MAC state @param out [out] The TAG @return CRYPT_OK on sucess */ int pelican_done(pelican_state *pelmac, unsigned char *out) { LTC_ARGCHK(pelmac != NULL); LTC_ARGCHK(out != NULL); if (pelmac->buflen == 16) { four_rounds(pelmac); pelmac->buflen = 0; } pelmac->state[pelmac->buflen++] ^= 0x80; aes_ecb_encrypt(pelmac->state, out, &pelmac->K); aes_done(&pelmac->K); return CRYPT_OK; } #endif |
From: Mark F. <mar...@us...> - 2005-04-28 17:28:13
|
Update of /cvsroot/opentnl/tnl/libtomcrypt/prngs In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/prngs Added Files: fortuna.c rc4.c rng_get_bytes.c rng_make_prng.c sober128.c sober128tab.c sprng.c yarrow.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- NEW FILE: yarrow.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file yarrow.c Yarrow PRNG, Tom St Denis */ #ifdef YARROW const struct ltc_prng_descriptor yarrow_desc = { "yarrow", 64, &yarrow_start, &yarrow_add_entropy, &yarrow_ready, &yarrow_read, &yarrow_done, &yarrow_export, &yarrow_import, &yarrow_test }; /** Start the PRNG @param prng [out] The PRNG state to initialize @return CRYPT_OK if successful */ int yarrow_start(prng_state *prng) { int err; LTC_ARGCHK(prng != NULL); /* these are the default hash/cipher combo used */ #ifdef RIJNDAEL #if YARROW_AES==0 prng->yarrow.cipher = register_cipher(&rijndael_enc_desc); #elif YARROW_AES==1 prng->yarrow.cipher = register_cipher(&aes_enc_desc); #elif YARROW_AES==2 prng->yarrow.cipher = register_cipher(&rijndael_desc); #elif YARROW_AES==3 prng->yarrow.cipher = register_cipher(&aes_desc); #endif #elif defined(BLOWFISH) prng->yarrow.cipher = register_cipher(&blowfish_desc); #elif defined(TWOFISH) prng->yarrow.cipher = register_cipher(&twofish_desc); #elif defined(RC6) prng->yarrow.cipher = register_cipher(&rc6_desc); #elif defined(RC5) prng->yarrow.cipher = register_cipher(&rc5_desc); #elif defined(SAFERP) prng->yarrow.cipher = register_cipher(&saferp_desc); #elif defined(RC2) prng->yarrow.cipher = register_cipher(&rc2_desc); #elif defined(NOEKEON) prng->yarrow.cipher = register_cipher(&noekeon_desc); #elif defined(CAST5) prng->yarrow.cipher = register_cipher(&cast5_desc); #elif defined(XTEA) prng->yarrow.cipher = register_cipher(&xtea_desc); #elif defined(SAFER) prng->yarrow.cipher = register_cipher(&safer_sk128_desc); #elif defined(DES) prng->yarrow.cipher = register_cipher(&des3_desc); #else #error YARROW needs at least one CIPHER #endif if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) { return err; } #ifdef SHA256 prng->yarrow.hash = register_hash(&sha256_desc); #elif defined(SHA512) prng->yarrow.hash = register_hash(&sha512_desc); #elif defined(TIGER) prng->yarrow.hash = register_hash(&tiger_desc); #elif defined(SHA1) prng->yarrow.hash = register_hash(&sha1_desc); #elif defined(RIPEMD160) prng->yarrow.hash = register_hash(&rmd160_desc); #elif defined(RIPEMD128) prng->yarrow.hash = register_hash(&rmd128_desc); #elif defined(MD5) prng->yarrow.hash = register_hash(&md5_desc); #elif defined(MD4) prng->yarrow.hash = register_hash(&md4_desc); #elif defined(MD2) prng->yarrow.hash = register_hash(&md2_desc); #elif defined(WHIRLPOOL) prng->yarrow.hash = register_hash(&whirlpool_desc); #else #error YARROW needs at least one HASH #endif if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) { return err; } /* zero the memory used */ zeromem(prng->yarrow.pool, sizeof(prng->yarrow.pool)); return CRYPT_OK; } /** Add entropy to the PRNG state @param in The data to add @param inlen Length of the data to add @param prng PRNG state to update @return CRYPT_OK if successful */ int yarrow_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng) { hash_state md; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(prng != NULL); if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) { return err; } /* start the hash */ if ((err = hash_descriptor[prng->yarrow.hash].init(&md)) != CRYPT_OK) { return err; } /* hash the current pool */ if ((err = hash_descriptor[prng->yarrow.hash].process(&md, prng->yarrow.pool, hash_descriptor[prng->yarrow.hash].hashsize)) != CRYPT_OK) { return err; } /* add the new entropy */ if ((err = hash_descriptor[prng->yarrow.hash].process(&md, in, inlen)) != CRYPT_OK) { return err; } /* store result */ if ((err = hash_descriptor[prng->yarrow.hash].done(&md, prng->yarrow.pool)) != CRYPT_OK) { return err; } return CRYPT_OK; } /** Make the PRNG ready to read from @param prng The PRNG to make active @return CRYPT_OK if successful */ int yarrow_ready(prng_state *prng) { int ks, err; LTC_ARGCHK(prng != NULL); if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) { return err; } if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) { return err; } /* setup CTR mode using the "pool" as the key */ ks = (int)hash_descriptor[prng->yarrow.hash].hashsize; if ((err = cipher_descriptor[prng->yarrow.cipher].keysize(&ks)) != CRYPT_OK) { return err; } if ((err = ctr_start(prng->yarrow.cipher, /* what cipher to use */ prng->yarrow.pool, /* IV */ prng->yarrow.pool, ks, /* KEY and key size */ 0, /* number of rounds */ &prng->yarrow.ctr)) != CRYPT_OK) { return err; } return CRYPT_OK; } /** Read from the PRNG @param out Destination @param outlen Length of output @param prng The active PRNG to read from @return Number of octets read */ unsigned long yarrow_read(unsigned char *out, unsigned long outlen, prng_state *prng) { LTC_ARGCHK(out != NULL); LTC_ARGCHK(prng != NULL); /* put out in predictable state first */ zeromem(out, outlen); /* now randomize it */ if (ctr_encrypt(out, out, outlen, &prng->yarrow.ctr) != CRYPT_OK) { return 0; } return outlen; } /** Terminate the PRNG @param prng The PRNG to terminate @return CRYPT_OK if successful */ int yarrow_done(prng_state *prng) { LTC_ARGCHK(prng != NULL); /* call cipher done when we invent one ;-) */ /* we invented one */ return ctr_done(&prng->yarrow.ctr); } /** Export the PRNG state @param out [out] Destination @param outlen [in/out] Max size and resulting size of the state @param prng The PRNG to export @return CRYPT_OK if successful */ int yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng) { LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(prng != NULL); /* we'll write 64 bytes for s&g's */ if (*outlen < 64) { return CRYPT_BUFFER_OVERFLOW; } if (yarrow_read(out, 64, prng) != 64) { return CRYPT_ERROR_READPRNG; } *outlen = 64; return CRYPT_OK; } /** Import a PRNG state @param in The PRNG state @param inlen Size of the state @param prng The PRNG to import @return CRYPT_OK if successful */ int yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng) { int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(prng != NULL); if (inlen != 64) { return CRYPT_INVALID_ARG; } if ((err = yarrow_start(prng)) != CRYPT_OK) { return err; } return yarrow_add_entropy(in, 64, prng); } /** PRNG self-test @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled */ int yarrow_test(void) { #ifndef LTC_TEST return CRYPT_NOP; #else int err; prng_state prng; if ((err = yarrow_start(&prng)) != CRYPT_OK) { return err; } /* now let's test the hash/cipher that was chosen */ if ((err = cipher_descriptor[prng.yarrow.cipher].test()) != CRYPT_OK) { return err; } if ((err = hash_descriptor[prng.yarrow.hash].test()) != CRYPT_OK) { return err; } yarrow_done(&prng); return CRYPT_OK; #endif } #endif --- NEW FILE: rc4.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rc4.c RC4 PRNG, Tom St Denis */ #ifdef RC4 const struct ltc_prng_descriptor rc4_desc = { "rc4", 32, &rc4_start, &rc4_add_entropy, &rc4_ready, &rc4_read, &rc4_done, &rc4_export, &rc4_import, &rc4_test }; /** Start the PRNG @param prng [out] The PRNG state to initialize @return CRYPT_OK if successful */ int rc4_start(prng_state *prng) { LTC_ARGCHK(prng != NULL); /* set keysize to zero */ prng->rc4.x = 0; return CRYPT_OK; } /** Add entropy to the PRNG state @param in The data to add @param inlen Length of the data to add @param prng PRNG state to update @return CRYPT_OK if successful */ int rc4_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng) { LTC_ARGCHK(in != NULL); LTC_ARGCHK(prng != NULL); /* trim as required */ if (prng->rc4.x + inlen > 256) { if (prng->rc4.x == 256) { /* I can't possibly accept another byte, ok maybe a mint wafer... */ return CRYPT_OK; } else { /* only accept part of it */ inlen = 256 - prng->rc4.x; } } while (inlen--) { prng->rc4.buf[prng->rc4.x++] = *in++; } return CRYPT_OK; } /** Make the PRNG ready to read from @param prng The PRNG to make active @return CRYPT_OK if successful */ int rc4_ready(prng_state *prng) { unsigned char key[256], tmp, *s; int keylen, x, y, j; LTC_ARGCHK(prng != NULL); /* extract the key */ s = prng->rc4.buf; XMEMCPY(key, s, 256); keylen = prng->rc4.x; /* make RC4 perm and shuffle */ for (x = 0; x < 256; x++) { s[x] = x; } for (j = x = y = 0; x < 256; x++) { y = (y + prng->rc4.buf[x] + key[j++]) & 255; if (j == keylen) { j = 0; } tmp = s[x]; s[x] = s[y]; s[y] = tmp; } prng->rc4.x = 0; prng->rc4.y = 0; #ifdef LTC_CLEAN_STACK zeromem(key, sizeof(key)); #endif return CRYPT_OK; } /** Read from the PRNG @param out Destination @param outlen Length of output @param prng The active PRNG to read from @return Number of octets read */ unsigned long rc4_read(unsigned char *out, unsigned long outlen, prng_state *prng) { unsigned char x, y, *s, tmp; unsigned long n; LTC_ARGCHK(out != NULL); LTC_ARGCHK(prng != NULL); n = outlen; x = prng->rc4.x; y = prng->rc4.y; s = prng->rc4.buf; while (outlen--) { x = (x + 1) & 255; y = (y + s[x]) & 255; tmp = s[x]; s[x] = s[y]; s[y] = tmp; tmp = (s[x] + s[y]) & 255; *out++ ^= s[tmp]; } prng->rc4.x = x; prng->rc4.y = y; return n; } /** Terminate the PRNG @param prng The PRNG to terminate @return CRYPT_OK if successful */ int rc4_done(prng_state *prng) { LTC_ARGCHK(prng != NULL); return CRYPT_OK; } /** Export the PRNG state @param out [out] Destination @param outlen [in/out] Max size and resulting size of the state @param prng The PRNG to export @return CRYPT_OK if successful */ int rc4_export(unsigned char *out, unsigned long *outlen, prng_state *prng) { LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(prng != NULL); if (*outlen < 32) { return CRYPT_BUFFER_OVERFLOW; } if (rc4_read(out, 32, prng) != 32) { return CRYPT_ERROR_READPRNG; } *outlen = 32; return CRYPT_OK; } /** Import a PRNG state @param in The PRNG state @param inlen Size of the state @param prng The PRNG to import @return CRYPT_OK if successful */ int rc4_import(const unsigned char *in, unsigned long inlen, prng_state *prng) { int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(prng != NULL); if (inlen != 32) { return CRYPT_INVALID_ARG; } if ((err = rc4_start(prng)) != CRYPT_OK) { return err; } return rc4_add_entropy(in, 32, prng); } /** PRNG self-test @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled */ int rc4_test(void) { #ifndef LTC_TEST return CRYPT_NOP; #else static const struct { unsigned char key[8], pt[8], ct[8]; } tests[] = { { { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }, { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }, { 0x75, 0xb7, 0x87, 0x80, 0x99, 0xe0, 0xc5, 0x96 } } }; prng_state prng; unsigned char dst[8]; int err, x; for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { if ((err = rc4_start(&prng)) != CRYPT_OK) { return err; } if ((err = rc4_add_entropy(tests[x].key, 8, &prng)) != CRYPT_OK) { return err; } if ((err = rc4_ready(&prng)) != CRYPT_OK) { return err; } XMEMCPY(dst, tests[x].pt, 8); if (rc4_read(dst, 8, &prng) != 8) { return CRYPT_ERROR_READPRNG; } rc4_done(&prng); if (memcmp(dst, tests[x].ct, 8)) { #if 0 int y; printf("\n\nRC4 failed, I got:\n"); for (y = 0; y < 8; y++) printf("%02x ", dst[y]); printf("\n"); #endif return CRYPT_FAIL_TESTVECTOR; } } return CRYPT_OK; #endif } #endif --- NEW FILE: sober128tab.c --- /** @file sober128tab.c SOBER-128 Tables */ /* $Id: sober128tab.c,v 1.1 2005/04/28 17:28:01 mark_frohnmayer Exp $ */ /* @(#)TuringMultab.h 1.3 (QUALCOMM) 02/09/03 */ /* Multiplication table for Turing using 0xD02B4367 */ static const ulong32 Multab[256] = { 0x00000000, 0xD02B4367, 0xED5686CE, 0x3D7DC5A9, 0x97AC41D1, 0x478702B6, 0x7AFAC71F, 0xAAD18478, 0x631582EF, 0xB33EC188, 0x8E430421, 0x5E684746, 0xF4B9C33E, 0x24928059, 0x19EF45F0, 0xC9C40697, 0xC62A4993, 0x16010AF4, 0x2B7CCF5D, 0xFB578C3A, 0x51860842, 0x81AD4B25, 0xBCD08E8C, 0x6CFBCDEB, 0xA53FCB7C, 0x7514881B, 0x48694DB2, 0x98420ED5, 0x32938AAD, 0xE2B8C9CA, 0xDFC50C63, 0x0FEE4F04, 0xC154926B, 0x117FD10C, 0x2C0214A5, 0xFC2957C2, 0x56F8D3BA, 0x86D390DD, 0xBBAE5574, 0x6B851613, 0xA2411084, 0x726A53E3, 0x4F17964A, 0x9F3CD52D, 0x35ED5155, 0xE5C61232, 0xD8BBD79B, 0x089094FC, 0x077EDBF8, 0xD755989F, 0xEA285D36, 0x3A031E51, 0x90D29A29, 0x40F9D94E, 0x7D841CE7, 0xADAF5F80, 0x646B5917, 0xB4401A70, 0x893DDFD9, 0x59169CBE, 0xF3C718C6, 0x23EC5BA1, 0x1E919E08, 0xCEBADD6F, 0xCFA869D6, 0x1F832AB1, 0x22FEEF18, 0xF2D5AC7F, 0x58042807, 0x882F6B60, 0xB552AEC9, 0x6579EDAE, 0xACBDEB39, 0x7C96A85E, 0x41EB6DF7, 0x91C02E90, 0x3B11AAE8, 0xEB3AE98F, 0xD6472C26, 0x066C6F41, 0x09822045, 0xD9A96322, 0xE4D4A68B, 0x34FFE5EC, 0x9E2E6194, 0x4E0522F3, 0x7378E75A, 0xA353A43D, 0x6A97A2AA, 0xBABCE1CD, 0x87C12464, 0x57EA6703, 0xFD3BE37B, 0x2D10A01C, 0x106D65B5, 0xC04626D2, 0x0EFCFBBD, 0xDED7B8DA, 0xE3AA7D73, 0x33813E14, 0x9950BA6C, 0x497BF90B, 0x74063CA2, 0xA42D7FC5, 0x6DE97952, 0xBDC23A35, 0x80BFFF9C, 0x5094BCFB, 0xFA453883, 0x2A6E7BE4, 0x1713BE4D, 0xC738FD2A, 0xC8D6B22E, 0x18FDF149, 0x258034E0, 0xF5AB7787, 0x5F7AF3FF, 0x8F51B098, 0xB22C7531, 0x62073656, 0xABC330C1, 0x7BE873A6, 0x4695B60F, 0x96BEF568, 0x3C6F7110, 0xEC443277, 0xD139F7DE, 0x0112B4B9, 0xD31DD2E1, 0x03369186, 0x3E4B542F, 0xEE601748, 0x44B19330, 0x949AD057, 0xA9E715FE, 0x79CC5699, 0xB008500E, 0x60231369, 0x5D5ED6C0, 0x8D7595A7, 0x27A411DF, 0xF78F52B8, 0xCAF29711, 0x1AD9D476, 0x15379B72, 0xC51CD815, 0xF8611DBC, 0x284A5EDB, 0x829BDAA3, 0x52B099C4, 0x6FCD5C6D, 0xBFE61F0A, 0x7622199D, 0xA6095AFA, 0x9B749F53, 0x4B5FDC34, 0xE18E584C, 0x31A51B2B, 0x0CD8DE82, 0xDCF39DE5, 0x1249408A, 0xC26203ED, 0xFF1FC644, 0x2F348523, 0x85E5015B, 0x55CE423C, 0x68B38795, 0xB898C4F2, 0x715CC265, 0xA1778102, 0x9C0A44AB, 0x4C2107CC, 0xE6F083B4, 0x36DBC0D3, 0x0BA6057A, 0xDB8D461D, 0xD4630919, 0x04484A7E, 0x39358FD7, 0xE91ECCB0, 0x43CF48C8, 0x93E40BAF, 0xAE99CE06, 0x7EB28D61, 0xB7768BF6, 0x675DC891, 0x5A200D38, 0x8A0B4E5F, 0x20DACA27, 0xF0F18940, 0xCD8C4CE9, 0x1DA70F8E, 0x1CB5BB37, 0xCC9EF850, 0xF1E33DF9, 0x21C87E9E, 0x8B19FAE6, 0x5B32B981, 0x664F7C28, 0xB6643F4F, 0x7FA039D8, 0xAF8B7ABF, 0x92F6BF16, 0x42DDFC71, 0xE80C7809, 0x38273B6E, 0x055AFEC7, 0xD571BDA0, 0xDA9FF2A4, 0x0AB4B1C3, 0x37C9746A, 0xE7E2370D, 0x4D33B375, 0x9D18F012, 0xA06535BB, 0x704E76DC, 0xB98A704B, 0x69A1332C, 0x54DCF685, 0x84F7B5E2, 0x2E26319A, 0xFE0D72FD, 0xC370B754, 0x135BF433, 0xDDE1295C, 0x0DCA6A3B, 0x30B7AF92, 0xE09CECF5, 0x4A4D688D, 0x9A662BEA, 0xA71BEE43, 0x7730AD24, 0xBEF4ABB3, 0x6EDFE8D4, 0x53A22D7D, 0x83896E1A, 0x2958EA62, 0xF973A905, 0xC40E6CAC, 0x14252FCB, 0x1BCB60CF, 0xCBE023A8, 0xF69DE601, 0x26B6A566, 0x8C67211E, 0x5C4C6279, 0x6131A7D0, 0xB11AE4B7, 0x78DEE220, 0xA8F5A147, 0x958864EE, 0x45A32789, 0xEF72A3F1, 0x3F59E096, 0x0224253F, 0xD20F6658, }; /* $Id: sober128tab.c,v 1.1 2005/04/28 17:28:01 mark_frohnmayer Exp $ */ /* Sbox for SOBER-128 */ /* * This is really the combination of two SBoxes; the least significant * 24 bits comes from: * 8->32 Sbox generated by Millan et. al. at Queensland University of * Technology. See: E. Dawson, W. Millan, L. Burnett, G. Carter, * "On the Design of 8*32 S-boxes". Unpublished report, by the * Information Systems Research Centre, * Queensland University of Technology, 1999. * * The most significant 8 bits are the Skipjack "F table", which can be * found at http://csrc.nist.gov/CryptoToolkit/skipjack/skipjack.pdf . * In this optimised table, though, the intent is to XOR the word from * the table selected by the high byte with the input word. Thus, the * high byte is actually the Skipjack F-table entry XORED with its * table index. */ static const ulong32 Sbox[256] = { 0xa3aa1887, 0xd65e435c, 0x0b65c042, 0x800e6ef4, 0xfc57ee20, 0x4d84fed3, 0xf066c502, 0xf354e8ae, 0xbb2ee9d9, 0x281f38d4, 0x1f829b5d, 0x735cdf3c, 0x95864249, 0xbc2e3963, 0xa1f4429f, 0xf6432c35, 0xf7f40325, 0x3cc0dd70, 0x5f973ded, 0x9902dc5e, 0xda175b42, 0x590012bf, 0xdc94d78c, 0x39aab26b, 0x4ac11b9a, 0x8c168146, 0xc3ea8ec5, 0x058ac28f, 0x52ed5c0f, 0x25b4101c, 0x5a2db082, 0x370929e1, 0x2a1843de, 0xfe8299fc, 0x202fbc4b, 0x833915dd, 0x33a803fa, 0xd446b2de, 0x46233342, 0x4fcee7c3, 0x3ad607ef, 0x9e97ebab, 0x507f859b, 0xe81f2e2f, 0xc55b71da, 0xd7e2269a, 0x1339c3d1, 0x7ca56b36, 0xa6c9def2, 0xb5c9fc5f, 0x5927b3a3, 0x89a56ddf, 0xc625b510, 0x560f85a7, 0xace82e71, 0x2ecb8816, 0x44951e2a, 0x97f5f6af, 0xdfcbc2b3, 0xce4ff55d, 0xcb6b6214, 0x2b0b83e3, 0x549ea6f5, 0x9de041af, 0x792f1f17, 0xf73b99ee, 0x39a65ec0, 0x4c7016c6, 0x857709a4, 0xd6326e01, 0xc7b280d9, 0x5cfb1418, 0xa6aff227, 0xfd548203, 0x506b9d96, 0xa117a8c0, 0x9cd5bf6e, 0xdcee7888, 0x61fcfe64, 0xf7a193cd, 0x050d0184, 0xe8ae4930, 0x88014f36, 0xd6a87088, 0x6bad6c2a, 0x1422c678, 0xe9204de7, 0xb7c2e759, 0x0200248e, 0x013b446b, 0xda0d9fc2, 0x0414a895, 0x3a6cc3a1, 0x56fef170, 0x86c19155, 0xcf7b8a66, 0x551b5e69, 0xb4a8623e, 0xa2bdfa35, 0xc4f068cc, 0x573a6acd, 0x6355e936, 0x03602db9, 0x0edf13c1, 0x2d0bb16d, 0x6980b83c, 0xfeb23763, 0x3dd8a911, 0x01b6bc13, 0xf55579d7, 0xf55c2fa8, 0x19f4196e, 0xe7db5476, 0x8d64a866, 0xc06e16ad, 0xb17fc515, 0xc46feb3c, 0x8bc8a306, 0xad6799d9, 0x571a9133, 0x992466dd, 0x92eb5dcd, 0xac118f50, 0x9fafb226, 0xa1b9cef3, 0x3ab36189, 0x347a19b1, 0x62c73084, 0xc27ded5c, 0x6c8bc58f, 0x1cdde421, 0xed1e47fb, 0xcdcc715e, 0xb9c0ff99, 0x4b122f0f, 0xc4d25184, 0xaf7a5e6c, 0x5bbf18bc, 0x8dd7c6e0, 0x5fb7e420, 0x521f523f, 0x4ad9b8a2, 0xe9da1a6b, 0x97888c02, 0x19d1e354, 0x5aba7d79, 0xa2cc7753, 0x8c2d9655, 0x19829da1, 0x531590a7, 0x19c1c149, 0x3d537f1c, 0x50779b69, 0xed71f2b7, 0x463c58fa, 0x52dc4418, 0xc18c8c76, 0xc120d9f0, 0xafa80d4d, 0x3b74c473, 0xd09410e9, 0x290e4211, 0xc3c8082b, 0x8f6b334a, 0x3bf68ed2, 0xa843cc1b, 0x8d3c0ff3, 0x20e564a0, 0xf8f55a4f, 0x2b40f8e7, 0xfea7f15f, 0xcf00fe21, 0x8a6d37d6, 0xd0d506f1, 0xade00973, 0xefbbde36, 0x84670fa8, 0xfa31ab9e, 0xaedab618, 0xc01f52f5, 0x6558eb4f, 0x71b9e343, 0x4b8d77dd, 0x8cb93da6, 0x740fd52d, 0x425412f8, 0xc5a63360, 0x10e53ad0, 0x5a700f1c, 0x8324ed0b, 0xe53dc1ec, 0x1a366795, 0x6d549d15, 0xc5ce46d7, 0xe17abe76, 0x5f48e0a0, 0xd0f07c02, 0x941249b7, 0xe49ed6ba, 0x37a47f78, 0xe1cfffbd, 0xb007ca84, 0xbb65f4da, 0xb59f35da, 0x33d2aa44, 0x417452ac, 0xc0d674a7, 0x2d61a46a, 0xdc63152a, 0x3e12b7aa, 0x6e615927, 0xa14fb118, 0xa151758d, 0xba81687b, 0xe152f0b3, 0x764254ed, 0x34c77271, 0x0a31acab, 0x54f94aec, 0xb9e994cd, 0x574d9e81, 0x5b623730, 0xce8a21e8, 0x37917f0b, 0xe8a9b5d6, 0x9697adf8, 0xf3d30431, 0x5dcac921, 0x76b35d46, 0xaa430a36, 0xc2194022, 0x22bca65e, 0xdaec70ba, 0xdfaea8cc, 0x777bae8b, 0x242924d5, 0x1f098a5a, 0x4b396b81, 0x55de2522, 0x435c1cb8, 0xaeb8fe1d, 0x9db3c697, 0x5b164f83, 0xe0c16376, 0xa319224c, 0xd0203b35, 0x433ac0fe, 0x1466a19a, 0x45f0b24f, 0x51fda998, 0xc0d52d71, 0xfa0896a8, 0xf9e6053f, 0xa4b0d300, 0xd499cbcc, 0xb95e3d40, }; --- NEW FILE: sprng.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file sprng.c Secure PRNG, Tom St Denis */ /* A secure PRNG using the RNG functions. Basically this is a * wrapper that allows you to use a secure RNG as a PRNG * in the various other functions. */ #ifdef SPRNG const struct ltc_prng_descriptor sprng_desc = { "sprng", 0, &sprng_start, &sprng_add_entropy, &sprng_ready, &sprng_read, &sprng_done, &sprng_export, &sprng_import, &sprng_test }; /** Start the PRNG @param prng [out] The PRNG state to initialize @return CRYPT_OK if successful */ int sprng_start(prng_state *prng) { return CRYPT_OK; } /** Add entropy to the PRNG state @param in The data to add @param inlen Length of the data to add @param prng PRNG state to update @return CRYPT_OK if successful */ int sprng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng) { return CRYPT_OK; } /** Make the PRNG ready to read from @param prng The PRNG to make active @return CRYPT_OK if successful */ int sprng_ready(prng_state *prng) { return CRYPT_OK; } /** Read from the PRNG @param out Destination @param outlen Length of output @param prng The active PRNG to read from @return Number of octets read */ unsigned long sprng_read(unsigned char *out, unsigned long outlen, prng_state *prng) { LTC_ARGCHK(out != NULL); return rng_get_bytes(out, outlen, NULL); } /** Terminate the PRNG @param prng The PRNG to terminate @return CRYPT_OK if successful */ int sprng_done(prng_state *prng) { return CRYPT_OK; } /** Export the PRNG state @param out [out] Destination @param outlen [in/out] Max size and resulting size of the state @param prng The PRNG to export @return CRYPT_OK if successful */ int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng) { LTC_ARGCHK(outlen != NULL); *outlen = 0; return CRYPT_OK; } /** Import a PRNG state @param in The PRNG state @param inlen Size of the state @param prng The PRNG to import @return CRYPT_OK if successful */ int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng) { return CRYPT_OK; } /** PRNG self-test @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled */ int sprng_test(void) { return CRYPT_OK; } #endif --- NEW FILE: rng_get_bytes.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rng_get_bytes.c portable way to get secure random bits to feed a PRNG (Tom St Denis) */ #ifdef DEVRANDOM /* on *NIX read /dev/random */ static unsigned long rng_nix(unsigned char *buf, unsigned long len, void (*callback)(void)) { #ifdef LTC_NO_FILE return 0; #else FILE *f; unsigned long x; #ifdef TRY_URANDOM_FIRST f = fopen("/dev/urandom", "rb"); if (f == NULL) #endif /* TRY_URANDOM_FIRST */ f = fopen("/dev/random", "rb"); if (f == NULL) { return 0; } /* disable buffering */ if (setvbuf(f, NULL, _IONBF, 0) != 0) { fclose(f); return 0; } x = (unsigned long)fread(buf, 1, (size_t)len, f); fclose(f); return x; #endif /* LTC_NO_FILE */ } #endif /* DEVRANDOM */ /* on ANSI C platforms with 100 < CLOCKS_PER_SEC < 10000 */ #if defined(CLOCKS_PER_SEC) #define ANSI_RNG static unsigned long rng_ansic(unsigned char *buf, unsigned long len, void (*callback)(void)) { clock_t t1; int l, acc, bits, a, b; if (XCLOCKS_PER_SEC < 100 || XCLOCKS_PER_SEC > 10000) { return 0; } l = len; bits = 8; acc = a = b = 0; while (len--) { if (callback != NULL) callback(); while (bits--) { do { t1 = XCLOCK(); while (t1 == XCLOCK()) a ^= 1; t1 = XCLOCK(); while (t1 == XCLOCK()) b ^= 1; } while (a == b); acc = (acc << 1) | a; } *buf++ = acc; acc = 0; bits = 8; } acc = bits = a = b = 0; return l; } #endif /* Try the Microsoft CSP */ #ifdef WIN32 #define _WIN32_WINNT 0x0400 #include <windows.h> #include <wincrypt.h> static unsigned long rng_win32(unsigned char *buf, unsigned long len, void (*callback)(void)) { HCRYPTPROV hProv = 0; if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) && !CryptAcquireContext (&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) return 0; if (CryptGenRandom(hProv, len, buf) == TRUE) { CryptReleaseContext(hProv, 0); return len; } else { CryptReleaseContext(hProv, 0); return 0; } } #endif /* WIN32 */ /** Read the system RNG @param out Destination @param outlen Length desired (octets) @param callback Pointer to void function to act as "callback" when RNG is slow. This can be NULL @return Number of octets read */ unsigned long rng_get_bytes(unsigned char *out, unsigned long outlen, void (*callback)(void)) { unsigned long x; LTC_ARGCHK(out != NULL); #if defined(DEVRANDOM) x = rng_nix(out, outlen, callback); if (x != 0) { return x; } #endif #ifdef WIN32 x = rng_win32(out, outlen, callback); if (x != 0) { return x; } #endif #ifdef ANSI_RNG x = rng_ansic(out, outlen, callback); if (x != 0) { return x; } #endif return 0; } --- NEW FILE: fortuna.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file fortuna.c Fortuna PRNG, Tom St Denis */ /* Implementation of Fortuna by Tom St Denis We deviate slightly here for reasons of simplicity [and to fit in the API]. First all "sources" in the AddEntropy function are fixed to 0. Second since no reliable timer is provided we reseed automatically when len(pool0) >= 64 or every FORTUNA_WD calls to the read function */ #ifdef FORTUNA /* requries SHA256 and AES */ #if !(defined(RIJNDAEL) && defined(SHA256)) #error FORTUNA requires SHA256 and RIJNDAEL (AES) #endif #ifndef FORTUNA_POOLS #warning FORTUNA_POOLS was not previously defined (old headers?) #define FORTUNA_POOLS 32 #endif #if FORTUNA_POOLS < 4 || FORTUNA_POOLS > 32 #error FORTUNA_POOLS must be in [4..32] #endif const struct ltc_prng_descriptor fortuna_desc = { "fortuna", 1024, &fortuna_start, &fortuna_add_entropy, &fortuna_ready, &fortuna_read, &fortuna_done, &fortuna_export, &fortuna_import, &fortuna_test }; /* update the IV */ static void fortuna_update_iv(prng_state *prng) { int x; unsigned char *IV; /* update IV */ IV = prng->fortuna.IV; for (x = 0; x < 16; x++) { IV[x] = (IV[x] + 1) & 255; if (IV[x] != 0) break; } } /* reseed the PRNG */ static int fortuna_reseed(prng_state *prng) { unsigned char tmp[MAXBLOCKSIZE]; hash_state md; int err, x; ++prng->fortuna.reset_cnt; /* new K == SHA256(K || s) where s == SHA256(P0) || SHA256(P1) ... */ sha256_init(&md); if ((err = sha256_process(&md, prng->fortuna.K, 32)) != CRYPT_OK) { return err; } for (x = 0; x < FORTUNA_POOLS; x++) { if (x == 0 || ((prng->fortuna.reset_cnt >> (x-1)) & 1) == 0) { /* terminate this hash */ if ((err = sha256_done(&prng->fortuna.pool[x], tmp)) != CRYPT_OK) { return err; } /* add it to the string */ if ((err = sha256_process(&md, tmp, 32)) != CRYPT_OK) { return err; } /* reset this pool */ sha256_init(&prng->fortuna.pool[x]); } else { break; } } /* finish key */ if ((err = sha256_done(&md, prng->fortuna.K)) != CRYPT_OK) { return err; } if ((err = rijndael_setup(prng->fortuna.K, 32, 0, &prng->fortuna.skey)) != CRYPT_OK) { return err; } fortuna_update_iv(prng); /* reset pool len */ prng->fortuna.pool0_len = 0; prng->fortuna.wd = 0; #ifdef LTC_CLEAN_STACK zeromem(&md, sizeof(md)); zeromem(tmp, sizeof(tmp)); #endif return CRYPT_OK; } /** Start the PRNG @param prng [out] The PRNG state to initialize @return CRYPT_OK if successful */ int fortuna_start(prng_state *prng) { int err, x; LTC_ARGCHK(prng != NULL); /* initialize the pools */ for (x = 0; x < FORTUNA_POOLS; x++) { sha256_init(&prng->fortuna.pool[x]); } prng->fortuna.pool_idx = prng->fortuna.pool0_len = prng->fortuna.reset_cnt = prng->fortuna.wd = 0; /* reset bufs */ zeromem(prng->fortuna.K, 32); if ((err = rijndael_setup(prng->fortuna.K, 32, 0, &prng->fortuna.skey)) != CRYPT_OK) { return err; } zeromem(prng->fortuna.IV, 16); return CRYPT_OK; } /** Add entropy to the PRNG state @param in The data to add @param inlen Length of the data to add @param prng PRNG state to update @return CRYPT_OK if successful */ int fortuna_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng) { unsigned char tmp[2]; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(prng != NULL); /* ensure inlen <= 32 */ if (inlen > 32) { return CRYPT_INVALID_ARG; } /* add s || length(in) || in to pool[pool_idx] */ tmp[0] = 0; tmp[1] = inlen; if ((err = sha256_process(&prng->fortuna.pool[prng->fortuna.pool_idx], tmp, 2)) != CRYPT_OK) { return err; } if ((err = sha256_process(&prng->fortuna.pool[prng->fortuna.pool_idx], in, inlen)) != CRYPT_OK) { return err; } if (prng->fortuna.pool_idx == 0) { prng->fortuna.pool0_len += inlen; } if (++(prng->fortuna.pool_idx) == FORTUNA_POOLS) { prng->fortuna.pool_idx = 0; } return CRYPT_OK; } /** Make the PRNG ready to read from @param prng The PRNG to make active @return CRYPT_OK if successful */ int fortuna_ready(prng_state *prng) { return fortuna_reseed(prng); } /** Read from the PRNG @param out Destination @param outlen Length of output @param prng The active PRNG to read from @return Number of octets read */ unsigned long fortuna_read(unsigned char *out, unsigned long outlen, prng_state *prng) { unsigned char tmp[16]; int err; unsigned long tlen; LTC_ARGCHK(out != NULL); LTC_ARGCHK(prng != NULL); /* do we have to reseed? */ if (++prng->fortuna.wd == FORTUNA_WD || prng->fortuna.pool0_len >= 64) { if ((err = fortuna_reseed(prng)) != CRYPT_OK) { return 0; } } /* now generate the blocks required */ tlen = outlen; /* handle whole blocks without the extra memcpy */ while (outlen >= 16) { /* encrypt the IV and store it */ rijndael_ecb_encrypt(prng->fortuna.IV, out, &prng->fortuna.skey); out += 16; outlen -= 16; fortuna_update_iv(prng); } /* left over bytes? */ if (outlen > 0) { rijndael_ecb_encrypt(prng->fortuna.IV, tmp, &prng->fortuna.skey); XMEMCPY(out, tmp, outlen); fortuna_update_iv(prng); } /* generate new key */ rijndael_ecb_encrypt(prng->fortuna.IV, prng->fortuna.K , &prng->fortuna.skey); fortuna_update_iv(prng); rijndael_ecb_encrypt(prng->fortuna.IV, prng->fortuna.K+16, &prng->fortuna.skey); fortuna_update_iv(prng); if ((err = rijndael_setup(prng->fortuna.K, 32, 0, &prng->fortuna.skey)) != CRYPT_OK) { return 0; } #ifdef LTC_CLEAN_STACK zeromem(tmp, sizeof(tmp)); #endif return tlen; } /** Terminate the PRNG @param prng The PRNG to terminate @return CRYPT_OK if successful */ int fortuna_done(prng_state *prng) { int err, x; unsigned char tmp[32]; LTC_ARGCHK(prng != NULL); /* terminate all the hashes */ for (x = 0; x < FORTUNA_POOLS; x++) { if ((err = sha256_done(&(prng->fortuna.pool[x]), tmp)) != CRYPT_OK) { return err; } } /* call cipher done when we invent one ;-) */ #ifdef LTC_CLEAN_STACK zeromem(tmp, sizeof(tmp)); #endif return CRYPT_OK; } /** Export the PRNG state @param out [out] Destination @param outlen [in/out] Max size and resulting size of the state @param prng The PRNG to export @return CRYPT_OK if successful */ int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng) { int x, err; hash_state *md; LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(prng != NULL); /* we'll write bytes for s&g's */ if (*outlen < 32*FORTUNA_POOLS) { return CRYPT_BUFFER_OVERFLOW; } md = XMALLOC(sizeof(hash_state)); if (md == NULL) { return CRYPT_MEM; } /* to emit the state we copy each pool, terminate it then hash it again so * an attacker who sees the state can't determine the current state of the PRNG */ for (x = 0; x < FORTUNA_POOLS; x++) { /* copy the PRNG */ XMEMCPY(md, &(prng->fortuna.pool[x]), sizeof(*md)); /* terminate it */ if ((err = sha256_done(md, out+x*32)) != CRYPT_OK) { goto LBL_ERR; } /* now hash it */ if ((err = sha256_init(md)) != CRYPT_OK) { goto LBL_ERR; } if ((err = sha256_process(md, out+x*32, 32)) != CRYPT_OK) { goto LBL_ERR; } if ((err = sha256_done(md, out+x*32)) != CRYPT_OK) { goto LBL_ERR; } } *outlen = 32*FORTUNA_POOLS; err = CRYPT_OK; LBL_ERR: #ifdef LTC_CLEAN_STACK zeromem(md, sizeof(*md)); #endif XFREE(md); return err; } /** Import a PRNG state @param in The PRNG state @param inlen Size of the state @param prng The PRNG to import @return CRYPT_OK if successful */ int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng) { int err, x; LTC_ARGCHK(in != NULL); LTC_ARGCHK(prng != NULL); if (inlen != 32*FORTUNA_POOLS) { return CRYPT_INVALID_ARG; } if ((err = fortuna_start(prng)) != CRYPT_OK) { return err; } for (x = 0; x < FORTUNA_POOLS; x++) { if ((err = fortuna_add_entropy(in+x*32, 32, prng)) != CRYPT_OK) { return err; } } return err; } /** PRNG self-test @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled */ int fortuna_test(void) { #ifndef LTC_TEST return CRYPT_NOP; #else int err; if ((err = sha256_test()) != CRYPT_OK) { return err; } return rijndael_test(); #endif } #endif --- NEW FILE: sober128.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file sober128.c Implementation of SOBER-128 by Tom St Denis. Based on s128fast.c reference code supplied by Greg Rose of QUALCOMM. */ #ifdef SOBER128 #include "sober128tab.c" const struct ltc_prng_descriptor sober128_desc = { "sober128", 64, &sober128_start, &sober128_add_entropy, &sober128_ready, &sober128_read, &sober128_done, &sober128_export, &sober128_import, &sober128_test }; /* don't change these... */ #define N 17 #define FOLD N /* how many iterations of folding to do */ #define INITKONST 0x6996c53a /* value of KONST to use during key loading */ #define KEYP 15 /* where to insert key words */ #define FOLDP 4 /* where to insert non-linear feedback */ #define B(x,i) ((unsigned char)(((x) >> (8*i)) & 0xFF)) static ulong32 BYTE2WORD(unsigned char *b) { ulong32 t; LOAD32L(t, b); return t; } #define WORD2BYTE(w, b) STORE32L(b, w) static void XORWORD(ulong32 w, unsigned char *b) { ulong32 t; LOAD32L(t, b); t ^= w; STORE32L(t, b); } /* give correct offset for the current position of the register, * where logically R[0] is at position "zero". */ #define OFF(zero, i) (((zero)+(i)) % N) /* step the LFSR */ /* After stepping, "zero" moves right one place */ #define STEP(R,z) \ R[OFF(z,0)] = R[OFF(z,15)] ^ R[OFF(z,4)] ^ (R[OFF(z,0)] << 8) ^ Multab[(R[OFF(z,0)] >> 24) & 0xFF]; static void cycle(ulong32 *R) { ulong32 t; int i; STEP(R,0); t = R[0]; for (i = 1; i < N; ++i) { R[i-1] = R[i]; } R[N-1] = t; } /* Return a non-linear function of some parts of the register. */ #define NLFUNC(c,z) \ { \ t = c->R[OFF(z,0)] + c->R[OFF(z,16)]; \ t ^= Sbox[(t >> 24) & 0xFF]; \ t = RORc(t, 8); \ t = ((t + c->R[OFF(z,1)]) ^ c->konst) + c->R[OFF(z,6)]; \ t ^= Sbox[(t >> 24) & 0xFF]; \ t = t + c->R[OFF(z,13)]; \ } static ulong32 nltap(struct sober128_prng *c) { ulong32 t; NLFUNC(c, 0); return t; } /** Start the PRNG @param prng [out] The PRNG state to initialize @return CRYPT_OK if successful */ int sober128_start(prng_state *prng) { int i; struct sober128_prng *c; LTC_ARGCHK(prng != NULL); c = &(prng->sober128); /* Register initialised to Fibonacci numbers */ c->R[0] = 1; c->R[1] = 1; for (i = 2; i < N; ++i) { c->R[i] = c->R[i-1] + c->R[i-2]; } c->konst = INITKONST; /* next add_entropy will be the key */ c->flag = 1; c->set = 0; return CRYPT_OK; } /* Save the current register state */ static void s128_savestate(struct sober128_prng *c) { int i; for (i = 0; i < N; ++i) { c->initR[i] = c->R[i]; } } /* initialise to previously saved register state */ static void s128_reloadstate(struct sober128_prng *c) { int i; for (i = 0; i < N; ++i) { c->R[i] = c->initR[i]; } } /* Initialise "konst" */ static void s128_genkonst(struct sober128_prng *c) { ulong32 newkonst; do { cycle(c->R); newkonst = nltap(c); } while ((newkonst & 0xFF000000) == 0); c->konst = newkonst; } /* Load key material into the register */ #define ADDKEY(k) \ c->R[KEYP] += (k); #define XORNL(nl) \ c->R[FOLDP] ^= (nl); /* nonlinear diffusion of register for key */ #define DROUND(z) STEP(c->R,z); NLFUNC(c,(z+1)); c->R[OFF((z+1),FOLDP)] ^= t; static void s128_diffuse(struct sober128_prng *c) { ulong32 t; /* relies on FOLD == N == 17! */ DROUND(0); DROUND(1); DROUND(2); DROUND(3); DROUND(4); DROUND(5); DROUND(6); DROUND(7); DROUND(8); DROUND(9); DROUND(10); DROUND(11); DROUND(12); DROUND(13); DROUND(14); DROUND(15); DROUND(16); } /** Add entropy to the PRNG state @param in The data to add @param inlen Length of the data to add @param prng PRNG state to update @return CRYPT_OK if successful */ int sober128_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng) { struct sober128_prng *c; ulong32 i, k; LTC_ARGCHK(in != NULL); LTC_ARGCHK(prng != NULL); c = &(prng->sober128); if (c->flag == 1) { /* this is the first call to the add_entropy so this input is the key */ /* inlen must be multiple of 4 bytes */ if ((inlen & 3) != 0) { return CRYPT_INVALID_KEYSIZE; } for (i = 0; i < inlen; i += 4) { k = BYTE2WORD((unsigned char *)&in[i]); ADDKEY(k); cycle(c->R); XORNL(nltap(c)); } /* also fold in the length of the key */ ADDKEY(inlen); /* now diffuse */ s128_diffuse(c); s128_genkonst(c); s128_savestate(c); c->nbuf = 0; c->flag = 0; c->set = 1; } else { /* ok we are adding an IV then... */ s128_reloadstate(c); /* inlen must be multiple of 4 bytes */ if ((inlen & 3) != 0) { return CRYPT_INVALID_KEYSIZE; } for (i = 0; i < inlen; i += 4) { k = BYTE2WORD((unsigned char *)&in[i]); ADDKEY(k); cycle(c->R); XORNL(nltap(c)); } /* also fold in the length of the key */ ADDKEY(inlen); /* now diffuse */ s128_diffuse(c); c->nbuf = 0; } return CRYPT_OK; } /** Make the PRNG ready to read from @param prng The PRNG to make active @return CRYPT_OK if successful */ int sober128_ready(prng_state *prng) { return prng->sober128.set == 1 ? CRYPT_OK : CRYPT_ERROR; } /* XOR pseudo-random bytes into buffer */ #define SROUND(z) STEP(c->R,z); NLFUNC(c,(z+1)); XORWORD(t, out+(z*4)); /** Read from the PRNG @param out Destination @param outlen Length of output @param prng The active PRNG to read from @return Number of octets read */ unsigned long sober128_read(unsigned char *out, unsigned long outlen, prng_state *prng) { struct sober128_prng *c; ulong32 t, tlen; LTC_ARGCHK(out != NULL); LTC_ARGCHK(prng != NULL); c = &(prng->sober128); t = 0; tlen = outlen; /* handle any previously buffered bytes */ while (c->nbuf != 0 && outlen != 0) { *out++ ^= c->sbuf & 0xFF; c->sbuf >>= 8; c->nbuf -= 8; --outlen; } #ifndef LTC_SMALL_CODE /* do lots at a time, if there's enough to do */ while (outlen >= N*4) { SROUND(0); SROUND(1); SROUND(2); SROUND(3); SROUND(4); SROUND(5); SROUND(6); SROUND(7); SROUND(8); SROUND(9); SROUND(10); SROUND(11); SROUND(12); SROUND(13); SROUND(14); SROUND(15); SROUND(16); out += 4*N; outlen -= 4*N; } #endif /* do small or odd size buffers the slow way */ while (4 <= outlen) { cycle(c->R); t = nltap(c); XORWORD(t, out); out += 4; outlen -= 4; } /* handle any trailing bytes */ if (outlen != 0) { cycle(c->R); c->sbuf = nltap(c); c->nbuf = 32; while (c->nbuf != 0 && outlen != 0) { *out++ ^= c->sbuf & 0xFF; c->sbuf >>= 8; c->nbuf -= 8; --outlen; } } return tlen; } /** Terminate the PRNG @param prng The PRNG to terminate @return CRYPT_OK if successful */ int sober128_done(prng_state *prng) { LTC_ARGCHK(prng != NULL); return CRYPT_OK; } /** Export the PRNG state @param out [out] Destination @param outlen [in/out] Max size and resulting size of the state @param prng The PRNG to export @return CRYPT_OK if successful */ int sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng) { LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(prng != NULL); if (*outlen < 64) { return CRYPT_BUFFER_OVERFLOW; } if (sober128_read(out, 64, prng) != 64) { return CRYPT_ERROR_READPRNG; } *outlen = 64; return CRYPT_OK; } /** Import a PRNG state @param in The PRNG state @param inlen Size of the state @param prng The PRNG to import @return CRYPT_OK if successful */ int sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng) { int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(prng != NULL); if (inlen != 64) { return CRYPT_INVALID_ARG; } if ((err = sober128_start(prng)) != CRYPT_OK) { return err; } if ((err = sober128_add_entropy(in, 64, prng)) != CRYPT_OK) { return err; } return sober128_ready(prng); } /** PRNG self-test @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled */ int sober128_test(void) { #ifndef LTC_TEST return CRYPT_NOP; #else static const struct { int keylen, ivlen, len; unsigned char key[16], iv[4], out[20]; } tests[] = { { 16, 4, 20, /* key */ { 't', 'e', 's', 't', ' ', 'k', 'e', 'y', ' ', '1', '2', '8', 'b', 'i', 't', 's' }, /* IV */ { 0x00, 0x00, 0x00, 0x0 }, /* expected output */ { 0x43, 0x50, 0x0c, 0xcf, 0x89, 0x91, 0x9f, 0x1d, 0xaa, 0x37, 0x74, 0x95, 0xf4, 0xb4, 0x58, 0xc2, 0x40, 0x37, 0x8b, 0xbb } } }; prng_state prng; unsigned char dst[20]; int err, x; for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { if ((err = sober128_start(&prng)) != CRYPT_OK) { return err; } if ((err = sober128_add_entropy(tests[x].key, tests[x].keylen, &prng)) != CRYPT_OK) { return err; } /* add IV */ if ((err = sober128_add_entropy(tests[x].iv, tests[x].ivlen, &prng)) != CRYPT_OK) { return err; } /* ready up */ if ((err = sober128_ready(&prng)) != CRYPT_OK) { return err; } memset(dst, 0, tests[x].len); if (sober128_read(dst, tests[x].len, &prng) != (unsigned long)tests[x].len) { return CRYPT_ERROR_READPRNG; } sober128_done(&prng); if (memcmp(dst, tests[x].out, tests[x].len)) { #if 0 printf("\n\nSOBER128 failed, I got:\n"); for (y = 0; y < tests[x].len; y++) printf("%02x ", dst[y]); printf("\n"); #endif return CRYPT_FAIL_TESTVECTOR; } } return CRYPT_OK; #endif } #endif --- NEW FILE: rng_make_prng.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file rng_make_prng.c portable way to get secure random bits to feed a PRNG (Tom St Denis) */ /** Create a PRNG from a RNG @param bits Number of bits of entropy desired (64 ... 1024) @param wprng Index of which PRNG to setup @param prng [out] PRNG state to initialize @param callback A pointer to a void function for when the RNG is slow, this can be NULL @return CRYPT_OK if successful */ int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void)) { unsigned char buf[256]; int err; LTC_ARGCHK(prng != NULL); /* check parameter */ if ((err = prng_is_valid(wprng)) != CRYPT_OK) { return err; } if (bits < 64 || bits > 1024) { return CRYPT_INVALID_PRNGSIZE; } if ((err = prng_descriptor[wprng].start(prng)) != CRYPT_OK) { return err; } bits = ((bits/8)+((bits&7)!=0?1:0)) * 2; if (rng_get_bytes(buf, (unsigned long)bits, callback) != (unsigned long)bits) { return CRYPT_ERROR_READPRNG; } if ((err = prng_descriptor[wprng].add_entropy(buf, (unsigned long)bits, prng)) != CRYPT_OK) { return err; } if ((err = prng_descriptor[wprng].ready(prng)) != CRYPT_OK) { return err; } #ifdef LTC_CLEAN_STACK zeromem(buf, sizeof(buf)); #endif return CRYPT_OK; } |
From: Mark F. <mar...@us...> - 2005-04-28 17:28:13
|
Update of /cvsroot/opentnl/tnl/libtomcrypt/modes/ecb In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/modes/ecb Added Files: ecb_decrypt.c ecb_done.c ecb_encrypt.c ecb_start.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- NEW FILE: ecb_encrypt.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ecb_encrypt.c ECB implementation, encrypt a block, Tom St Denis */ #ifdef ECB /** ECB encrypt @param pt Plaintext @param ct [out] Ciphertext @param len The number of octets to process (must be multiple of the cipher block size) @param ecb ECB state @return CRYPT_OK if successful */ int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_ECB *ecb) { int err; LTC_ARGCHK(pt != NULL); LTC_ARGCHK(ct != NULL); LTC_ARGCHK(ecb != NULL); if ((err = cipher_is_valid(ecb->cipher)) != CRYPT_OK) { return err; } if (len % cipher_descriptor[ecb->cipher].block_length) { return CRYPT_INVALID_ARG; } /* check for accel */ if (cipher_descriptor[ecb->cipher].accel_ecb_encrypt != NULL) { cipher_descriptor[ecb->cipher].accel_ecb_encrypt(pt, ct, len / cipher_descriptor[ecb->cipher].block_length, &ecb->key); } else { while (len) { cipher_descriptor[ecb->cipher].ecb_encrypt(pt, ct, &ecb->key); pt += cipher_descriptor[ecb->cipher].block_length; ct += cipher_descriptor[ecb->cipher].block_length; len -= cipher_descriptor[ecb->cipher].block_length; } } return CRYPT_OK; } #endif --- NEW FILE: ecb_start.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ecb_start.c ECB implementation, start chain, Tom St Denis */ #ifdef ECB /** Initialize a ECB context @param cipher The index of the cipher desired @param key The secret key @param keylen The length of the secret key (octets) @param num_rounds Number of rounds in the cipher desired (0 for default) @param ecb The ECB state to initialize @return CRYPT_OK if successful */ int ecb_start(int cipher, const unsigned char *key, int keylen, int num_rounds, symmetric_ECB *ecb) { int err; LTC_ARGCHK(key != NULL); LTC_ARGCHK(ecb != NULL); if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { return err; } ecb->cipher = cipher; ecb->blocklen = cipher_descriptor[cipher].block_length; return cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ecb->key); } #endif --- NEW FILE: ecb_decrypt.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ecb_decrypt.c ECB implementation, decrypt a block, Tom St Denis */ #ifdef ECB /** ECB decrypt @param ct Ciphertext @param pt [out] Plaintext @param len The number of octets to process (must be multiple of the cipher block size) @param ecb ECB state @return CRYPT_OK if successful */ int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_ECB *ecb) { int err; LTC_ARGCHK(pt != NULL); LTC_ARGCHK(ct != NULL); LTC_ARGCHK(ecb != NULL); if ((err = cipher_is_valid(ecb->cipher)) != CRYPT_OK) { return err; } if (len % cipher_descriptor[ecb->cipher].block_length) { return CRYPT_INVALID_ARG; } /* check for accel */ if (cipher_descriptor[ecb->cipher].accel_ecb_decrypt != NULL) { cipher_descriptor[ecb->cipher].accel_ecb_decrypt(ct, pt, len / cipher_descriptor[ecb->cipher].block_length, &ecb->key); } else { while (len) { cipher_descriptor[ecb->cipher].ecb_decrypt(ct, pt, &ecb->key); pt += cipher_descriptor[ecb->cipher].block_length; ct += cipher_descriptor[ecb->cipher].block_length; len -= cipher_descriptor[ecb->cipher].block_length; } } return CRYPT_OK; } #endif --- NEW FILE: ecb_done.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ecb_done.c ECB implementation, finish chain, Tom St Denis */ #ifdef ECB /** Terminate the chain @param rcb The ECB chain to terminate @return CRYPT_OK on success */ int ecb_done(symmetric_ECB *ecb) { int err; LTC_ARGCHK(ecb != NULL); if ((err = cipher_is_valid(ecb->cipher)) != CRYPT_OK) { return err; } cipher_descriptor[ecb->cipher].done(&ecb->key); return CRYPT_OK; } #endif |
From: Mark F. <mar...@us...> - 2005-04-28 17:28:13
|
Update of /cvsroot/opentnl/tnl/libtomcrypt/modes/ctr In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/modes/ctr Added Files: ctr_decrypt.c ctr_done.c ctr_encrypt.c ctr_getiv.c ctr_setiv.c ctr_start.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- NEW FILE: ctr_decrypt.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ctr_decrypt.c CTR implementation, decrypt data, Tom St Denis */ #ifdef CTR /** CTR decrypt @param ct Ciphertext @param pt [out] Plaintext @param len Length of ciphertext (octets) @param ctr CTR state @return CRYPT_OK if successful */ int ctr_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CTR *ctr) { LTC_ARGCHK(pt != NULL); LTC_ARGCHK(ct != NULL); LTC_ARGCHK(ctr != NULL); return ctr_encrypt(ct, pt, len, ctr); } #endif --- NEW FILE: ctr_done.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ctr_done.c CTR implementation, finish chain, Tom St Denis */ #ifdef CTR /** Terminate the chain @param ctr The CTR chain to terminate @return CRYPT_OK on success */ int ctr_done(symmetric_CTR *ctr) { int err; LTC_ARGCHK(ctr != NULL); if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) { return err; } cipher_descriptor[ctr->cipher].done(&ctr->key); return CRYPT_OK; } #endif --- NEW FILE: ctr_setiv.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ctr_setiv.c CTR implementation, set IV, Tom St Denis */ #ifdef CTR /** Set an initial vector @param IV The initial vector @param len The length of the vector (in octets) @param ctr The CTR state @return CRYPT_OK if successful */ int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr) { int err; LTC_ARGCHK(IV != NULL); LTC_ARGCHK(ctr != NULL); /* bad param? */ if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) { return err; } if (len != (unsigned long)ctr->blocklen) { return CRYPT_INVALID_ARG; } /* set IV */ XMEMCPY(ctr->ctr, IV, len); /* force next block */ ctr->padlen = 0; cipher_descriptor[ctr->cipher].ecb_encrypt(IV, ctr->pad, &ctr->key); return CRYPT_OK; } #endif --- NEW FILE: ctr_encrypt.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ctr_encrypt.c CTR implementation, encrypt data, Tom St Denis */ #ifdef CTR /** CTR encrypt @param pt Plaintext @param ct [out] Ciphertext @param len Length of plaintext (octets) @param ctr CTR state @return CRYPT_OK if successful */ int ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr) { int x, err; LTC_ARGCHK(pt != NULL); LTC_ARGCHK(ct != NULL); LTC_ARGCHK(ctr != NULL); if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) { return err; } /* is blocklen/padlen valid? */ if (ctr->blocklen < 0 || ctr->blocklen > (int)sizeof(ctr->ctr) || ctr->padlen < 0 || ctr->padlen > (int)sizeof(ctr->pad)) { return CRYPT_INVALID_ARG; } #ifdef LTC_FAST if (ctr->blocklen % sizeof(LTC_FAST_TYPE)) { return CRYPT_INVALID_ARG; } #endif /* handle acceleration only if pad is empty, accelerator is present and length is >= a block size */ if ((ctr->padlen == ctr->blocklen) && cipher_descriptor[ctr->cipher].accel_ctr_encrypt != NULL && (len >= (unsigned long)ctr->blocklen)) { cipher_descriptor[ctr->cipher].accel_ctr_encrypt(pt, ct, len/ctr->blocklen, ctr->ctr, ctr->mode, &ctr->key); len %= ctr->blocklen; } while (len) { /* is the pad empty? */ if (ctr->padlen == ctr->blocklen) { /* increment counter */ if (ctr->mode == 0) { /* little-endian */ for (x = 0; x < ctr->blocklen; x++) { ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255; if (ctr->ctr[x] != (unsigned char)0) { break; } } } else { /* big-endian */ for (x = ctr->blocklen-1; x >= 0; x--) { ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255; if (ctr->ctr[x] != (unsigned char)0) { break; } } } /* encrypt it */ cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key); ctr->padlen = 0; } #ifdef LTC_FAST if (ctr->padlen == 0 && len >= (unsigned long)ctr->blocklen) { for (x = 0; x < ctr->blocklen; x += sizeof(LTC_FAST_TYPE)) { *((LTC_FAST_TYPE*)((unsigned char *)ct + x)) = *((LTC_FAST_TYPE*)((unsigned char *)pt + x)) ^ *((LTC_FAST_TYPE*)((unsigned char *)ctr->pad + x)); } pt += ctr->blocklen; ct += ctr->blocklen; len -= ctr->blocklen; ctr->padlen = ctr->blocklen; continue; } #endif *ct++ = *pt++ ^ ctr->pad[ctr->padlen++]; --len; } return CRYPT_OK; } #endif --- NEW FILE: ctr_getiv.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ctr_getiv.c CTR implementation, get IV, Tom St Denis */ #ifdef CTR /** Get the current initial vector @param IV [out] The destination of the initial vector @param len [in/out] The max size and resulting size of the initial vector @param ctr The CTR state @return CRYPT_OK if successful */ int ctr_getiv(unsigned char *IV, unsigned long *len, symmetric_CTR *ctr) { LTC_ARGCHK(IV != NULL); LTC_ARGCHK(len != NULL); LTC_ARGCHK(ctr != NULL); if ((unsigned long)ctr->blocklen > *len) { return CRYPT_BUFFER_OVERFLOW; } XMEMCPY(IV, ctr->ctr, ctr->blocklen); *len = ctr->blocklen; return CRYPT_OK; } #endif --- NEW FILE: ctr_start.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ctr_start.c CTR implementation, start chain, Tom St Denis */ #ifdef CTR /** Initialize a CTR context @param cipher The index of the cipher desired @param count The initial vector @param key The secret key @param keylen The length of the secret key (octets) @param num_rounds Number of rounds in the cipher desired (0 for default) @param ctr The CTR state to initialize @return CRYPT_OK if successful */ int ctr_start(int cipher, const unsigned char *count, const unsigned char *key, int keylen, int num_rounds, symmetric_CTR *ctr) { int x, err; LTC_ARGCHK(count != NULL); LTC_ARGCHK(key != NULL); LTC_ARGCHK(ctr != NULL); /* bad param? */ if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { return err; } /* setup cipher */ if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ctr->key)) != CRYPT_OK) { return err; } /* copy ctr */ ctr->blocklen = cipher_descriptor[cipher].block_length; ctr->cipher = cipher; ctr->padlen = 0; ctr->mode = 0; for (x = 0; x < ctr->blocklen; x++) { ctr->ctr[x] = count[x]; } cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key); return CRYPT_OK; } #endif |
Update of /cvsroot/opentnl/tnl/libtomcrypt/misc/crypt In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/misc/crypt Added Files: crypt.c crypt_argchk.c crypt_cipher_descriptor.c crypt_cipher_is_valid.c crypt_find_cipher.c crypt_find_cipher_any.c crypt_find_cipher_id.c crypt_find_hash.c crypt_find_hash_any.c crypt_find_hash_id.c crypt_find_prng.c crypt_hash_descriptor.c crypt_hash_is_valid.c crypt_prng_descriptor.c crypt_prng_is_valid.c crypt_register_cipher.c crypt_register_hash.c crypt_register_prng.c crypt_unregister_cipher.c crypt_unregister_hash.c crypt_unregister_prng.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- NEW FILE: crypt_find_cipher.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_find_cipher.c Find a cipher in the descriptor tables, Tom St Denis */ /** Find a registered cipher by name @param name The name of the cipher to look for @return >= 0 if found, -1 if not present */ int find_cipher(const char *name) { int x; LTC_ARGCHK(name != NULL); for (x = 0; x < TAB_SIZE; x++) { if (cipher_descriptor[x].name != NULL && !strcmp(cipher_descriptor[x].name, name)) { return x; } } return -1; } --- NEW FILE: crypt_prng_is_valid.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_prng_is_valid.c Determine if PRNG is valid, Tom St Denis */ /* Test if a PRNG index is valid @param idx The index of the PRNG to search for @return CRYPT_OK if valid */ int prng_is_valid(int idx) { if (idx < 0 || idx >= TAB_SIZE || prng_descriptor[idx].name == NULL) { return CRYPT_INVALID_PRNG; } return CRYPT_OK; } --- NEW FILE: crypt_find_hash_any.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_find_hash_any.c Find a hash, Tom St Denis */ /** Find a hash flexibly. First by name then if not present by digest size @param name The name of the hash desired @param digestlen The minimum length of the digest size (octets) @return >= 0 if found, -1 if not present */int find_hash_any(const char *name, int digestlen) { int x, y, z; LTC_ARGCHK(name != NULL); x = find_hash(name); if (x != -1) return x; y = MAXBLOCKSIZE+1; z = -1; for (x = 0; x < TAB_SIZE; x++) { if (hash_descriptor[x].name == NULL) { continue; } if ((int)hash_descriptor[x].hashsize >= digestlen && (int)hash_descriptor[x].hashsize < y) { z = x; y = hash_descriptor[x].hashsize; } } return z; } --- NEW FILE: crypt_cipher_is_valid.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_cipher_is_valid.c Determine if cipher is valid, Tom St Denis */ /* Test if a cipher index is valid @param idx The index of the cipher to search for @return CRYPT_OK if valid */ int cipher_is_valid(int idx) { if (idx < 0 || idx >= TAB_SIZE || cipher_descriptor[idx].name == NULL) { return CRYPT_INVALID_CIPHER; } return CRYPT_OK; } --- NEW FILE: crypt_hash_is_valid.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_hash_is_valid.c Determine if hash is valid, Tom St Denis */ /* Test if a hash index is valid @param idx The index of the hash to search for @return CRYPT_OK if valid */ int hash_is_valid(int idx) { if (idx < 0 || idx >= TAB_SIZE || hash_descriptor[idx].name == NULL) { return CRYPT_INVALID_HASH; } return CRYPT_OK; } --- NEW FILE: crypt_cipher_descriptor.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_cipher_descriptor.c Stores the cipher descriptor table, Tom St Denis */ struct ltc_cipher_descriptor cipher_descriptor[TAB_SIZE] = { { NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } }; --- NEW FILE: crypt_find_hash_id.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_find_hash_id.c Find hash by ID, Tom St Denis */ /** Find a hash by ID number @param ID The ID (not same as index) of the hash to find @return >= 0 if found, -1 if not present */ int find_hash_id(unsigned char ID) { int x; for (x = 0; x < TAB_SIZE; x++) { if (hash_descriptor[x].ID == ID) { return (hash_descriptor[x].name == NULL) ? -1 : x; } } return -1; } --- NEW FILE: crypt_unregister_hash.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_unregister_hash.c Unregister a hash, Tom St Denis */ /** Unregister a hash from the descriptor table @param hash The hash descriptor to remove @return CRYPT_OK on success */ int unregister_hash(const struct ltc_hash_descriptor *hash) { int x; LTC_ARGCHK(hash != NULL); /* is it already registered? */ for (x = 0; x < TAB_SIZE; x++) { if (memcmp(&hash_descriptor[x], hash, sizeof(struct ltc_hash_descriptor)) == 0) { hash_descriptor[x].name = NULL; return CRYPT_OK; } } return CRYPT_ERROR; } --- NEW FILE: crypt_find_cipher_any.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_find_cipher_any.c Find a cipher in the descriptor tables, Tom St Denis */ /** Find a cipher flexibly. First by name then if not present by block and key size @param name The name of the cipher desired @param blocklen The minimum length of the block cipher desired (octets) @param keylen The minimum length of the key size desired (octets) @return >= 0 if found, -1 if not present */ int find_cipher_any(const char *name, int blocklen, int keylen) { int x; LTC_ARGCHK(name != NULL); x = find_cipher(name); if (x != -1) return x; for (x = 0; x < TAB_SIZE; x++) { if (cipher_descriptor[x].name == NULL) { continue; } if (blocklen <= (int)cipher_descriptor[x].block_length && keylen <= (int)cipher_descriptor[x].max_key_length) { return x; } } return -1; } --- NEW FILE: crypt_argchk.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" #include <signal.h> /** @file crypt_argchk.c Perform argument checking, Tom St Denis */ #if (ARGTYPE == 0) void crypt_argchk(char *v, char *s, int d) { fprintf(stderr, "LTC_ARGCHK '%s' failure on line %d of file %s\n", v, d, s); (void)raise(SIGABRT); } #endif --- NEW FILE: crypt_find_prng.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_find_prng.c Find a PRNG, Tom St Denis */ /** Find a registered PRNG by name @param name The name of the PRNG to look for @return >= 0 if found, -1 if not present */ int find_prng(const char *name) { int x; LTC_ARGCHK(name != NULL); for (x = 0; x < TAB_SIZE; x++) { if ((prng_descriptor[x].name != NULL) && strcmp(prng_descriptor[x].name, name) == 0) { return x; } } return -1; } --- NEW FILE: crypt_register_prng.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_register_prng.c Register a PRNG, Tom St Denis */ /** Register a PRNG with the descriptor table @param prng The PRNG you wish to register @return value >= 0 if successfully added (or already present), -1 if unsuccessful */ int register_prng(const struct ltc_prng_descriptor *prng) { int x; LTC_ARGCHK(prng != NULL); /* is it already registered? */ for (x = 0; x < TAB_SIZE; x++) { if (memcmp(&prng_descriptor[x], prng, sizeof(struct ltc_prng_descriptor)) == 0) { return x; } } /* find a blank spot */ for (x = 0; x < TAB_SIZE; x++) { if (prng_descriptor[x].name == NULL) { XMEMCPY(&prng_descriptor[x], prng, sizeof(struct ltc_prng_descriptor)); return x; } } /* no spot */ return -1; } --- NEW FILE: crypt_find_hash.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_find_hash.c Find a hash, Tom St Denis */ /** Find a registered hash by name @param name The name of the hash to look for @return >= 0 if found, -1 if not present */ int find_hash(const char *name) { int x; LTC_ARGCHK(name != NULL); for (x = 0; x < TAB_SIZE; x++) { if (hash_descriptor[x].name != NULL && strcmp(hash_descriptor[x].name, name) == 0) { return x; } } return -1; } --- NEW FILE: crypt_prng_descriptor.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_prng_descriptor.c Stores the PRNG descriptors, Tom St Denis */ struct ltc_prng_descriptor prng_descriptor[TAB_SIZE] = { { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } }; --- NEW FILE: crypt_register_hash.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_register_hash.c Register a HASH, Tom St Denis */ /** Register a hash with the descriptor table @param hash The hash you wish to register @return value >= 0 if successfully added (or already present), -1 if unsuccessful */ int register_hash(const struct ltc_hash_descriptor *hash) { int x; LTC_ARGCHK(hash != NULL); /* is it already registered? */ for (x = 0; x < TAB_SIZE; x++) { if (memcmp(&hash_descriptor[x], hash, sizeof(struct ltc_hash_descriptor)) == 0) { return x; } } /* find a blank spot */ for (x = 0; x < TAB_SIZE; x++) { if (hash_descriptor[x].name == NULL) { XMEMCPY(&hash_descriptor[x], hash, sizeof(struct ltc_hash_descriptor)); return x; } } /* no spot */ return -1; } --- NEW FILE: crypt_unregister_cipher.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_unregister_cipher.c Unregister a cipher, Tom St Denis */ /** Unregister a cipher from the descriptor table @param cipher The cipher descriptor to remove @return CRYPT_OK on success */ int unregister_cipher(const struct ltc_cipher_descriptor *cipher) { int x; LTC_ARGCHK(cipher != NULL); /* is it already registered? */ for (x = 0; x < TAB_SIZE; x++) { if (memcmp(&cipher_descriptor[x], cipher, sizeof(struct ltc_cipher_descriptor)) == 0) { cipher_descriptor[x].name = NULL; cipher_descriptor[x].ID = 255; return CRYPT_OK; } } return CRYPT_ERROR; } --- NEW FILE: crypt.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt.c Build strings, Tom St Denis */ const char *crypt_build_settings = "LibTomCrypt " SCRYPT " (Tom St Denis, tom...@gm...)\n" "LibTomCrypt is public domain software.\n" "Built on " __DATE__ " at " __TIME__ "\n\n\n" "Endianess: " #if defined(ENDIAN_NEUTRAL) "neutral\n" #elif defined(ENDIAN_LITTLE) "little" #if defined(ENDIAN_32BITWORD) " (32-bit words)\n" #else " (64-bit words)\n" #endif #elif defined(ENDIAN_BIG) "big" #if defined(ENDIAN_32BITWORD) " (32-bit words)\n" #else " (64-bit words)\n" #endif #endif "Clean stack: " #if defined(LTC_CLEAN_STACK) "enabled\n" #else "disabled\n" #endif "Ciphers built-in:\n" #if defined(BLOWFISH) " Blowfish\n" #endif #if defined(RC2) " RC2\n" #endif #if defined(RC5) " RC5\n" #endif #if defined(RC6) " RC6\n" #endif #if defined(SAFERP) " Safer+\n" #endif #if defined(SAFER) " Safer\n" #endif #if defined(RIJNDAEL) " Rijndael\n" #endif #if defined(XTEA) " XTEA\n" #endif #if defined(TWOFISH) " Twofish " #if defined(TWOFISH_SMALL) && defined(TWOFISH_TABLES) && defined(TWOFISH_ALL_TABLES) "(small, tables, all_tables)\n" #elif defined(TWOFISH_SMALL) && defined(TWOFISH_TABLES) "(small, tables)\n" #elif defined(TWOFISH_SMALL) && defined(TWOFISH_ALL_TABLES) "(small, all_tables)\n" #elif defined(TWOFISH_TABLES) && defined(TWOFISH_ALL_TABLES) "(tables, all_tables)\n" #elif defined(TWOFISH_SMALL) "(small)\n" #elif defined(TWOFISH_TABLES) "(tables)\n" #elif defined(TWOFISH_ALL_TABLES) "(all_tables)\n" #else "\n" #endif #endif #if defined(DES) " DES\n" #endif #if defined(CAST5) " CAST5\n" #endif #if defined(NOEKEON) " Noekeon\n" #endif #if defined(SKIPJACK) " Skipjack\n" #endif #if defined(KHAZAD) " Khazad\n" #endif #if defined(ANUBIS) " Anubis " #endif #if defined(ANUBIS_TWEAK) " (tweaked)" #endif "\n" "\nHashes built-in:\n" #if defined(SHA512) " SHA-512\n" #endif #if defined(SHA384) " SHA-384\n" #endif #if defined(SHA256) " SHA-256\n" #endif #if defined(SHA224) " SHA-224\n" #endif #if defined(TIGER) " TIGER\n" #endif #if defined(SHA1) " SHA1\n" #endif #if defined(MD5) " MD5\n" #endif #if defined(MD4) " MD4\n" #endif #if defined(MD2) " MD2\n" #endif #if defined(RIPEMD128) " RIPEMD128\n" #endif #if defined(RIPEMD160) " RIPEMD160\n" #endif #if defined(WHIRLPOOL) " WHIRLPOOL\n" #endif #if defined(CHC_HASH) " CHC_HASH \n" #endif "\nBlock Chaining Modes:\n" #if defined(CFB) " CFB\n" #endif #if defined(OFB) " OFB\n" #endif #if defined(ECB) " ECB\n" #endif #if defined(CBC) " CBC\n" #endif #if defined(CTR) " CTR\n" #endif "\nMACs:\n" #if defined(HMAC) " HMAC\n" #endif #if defined(OMAC) " OMAC\n" #endif #if defined(PMAC) " PMAC\n" #endif #if defined(PELICAN) " PELICAN\n" #endif "\nENC + AUTH modes:\n" #if defined(EAX_MODE) " EAX_MODE\n" #endif #if defined(OCB_MODE) " OCB_MODE\n" #endif #if defined(CCM_MODE) " CCM_MODE\n" #endif #if defined(GCM_MODE) " GCM_MODE " #endif #if defined(GCM_TABLES) " (GCM_TABLES) " #endif "\n" "\nPRNG:\n" #if defined(YARROW) " Yarrow\n" #endif #if defined(SPRNG) " SPRNG\n" #endif #if defined(RC4) " RC4\n" #endif #if defined(FORTUNA) " Fortuna\n" #endif #if defined(SOBER128) " SOBER128\n" #endif "\nPK Algs:\n" #if defined(MRSA) " RSA \n" #endif #if defined(MDH) " DH\n" #endif #if defined(MECC) " ECC\n" #endif #if defined(MDSA) " DSA\n" #endif "\nCompiler:\n" #if defined(WIN32) " WIN32 platform detected.\n" #endif #if defined(LBL_CYGWIN__) " CYGWIN Detected.\n" #endif #if defined(LBL_DJGPP__) " DJGPP Detected.\n" #endif #if defined(_MSC_VER) " MSVC compiler detected.\n" #endif #if defined(__GNUC__) " GCC compiler detected.\n" #endif #if defined(INTEL_CC) " Intel C Compiler detected.\n" #endif #if defined(LBL_x86_64__) " x86-64 detected.\n" #endif "\nVarious others: " #if defined(BASE64) " BASE64 " #endif #if defined(MPI) " MPI " #endif #if defined(TRY_UNRANDOM_FIRST) " TRY_UNRANDOM_FIRST " #endif #if defined(LTC_TEST) " LTC_TEST " #endif #if defined(PKCS_1) " PKCS#1 " #endif #if defined(PKCS_5) " PKCS#5 " #endif #if defined(LTC_SMALL_CODE) " LTC_SMALL_CODE " #endif #if defined(LTC_NO_FILE) " LTC_NO_FILE " #endif #if defined(LTC_DER) " LTC_DER " #endif #if defined(LTC_FAST) " LTC_FAST " #endif #if defined(LTC_NO_FAST) " LTC_NO_FAST " #endif #if defined(LTC_NO_BSWAP) " LTC_NO_BSWAP " #endif #if defined(LTC_NO_ASM) " LTC_NO_ASM " #endif "\n" "\n\n\n" ; --- NEW FILE: crypt_hash_descriptor.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_hash_descriptor.c Stores the hash descriptor table, Tom St Denis */ struct ltc_hash_descriptor hash_descriptor[TAB_SIZE] = { { NULL, 0, 0, 0, { 0 }, 0, NULL, NULL, NULL, NULL } }; --- NEW FILE: crypt_register_cipher.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_register_cipher.c Register a cipher, Tom St Denis */ /** Register a cipher with the descriptor table @param cipher The cipher you wish to register @return value >= 0 if successfully added (or already present), -1 if unsuccessful */ int register_cipher(const struct ltc_cipher_descriptor *cipher) { int x; LTC_ARGCHK(cipher != NULL); /* is it already registered? */ for (x = 0; x < TAB_SIZE; x++) { if (cipher_descriptor[x].name != NULL && cipher_descriptor[x].ID == cipher->ID) { return x; } } /* find a blank spot */ for (x = 0; x < TAB_SIZE; x++) { if (cipher_descriptor[x].name == NULL) { XMEMCPY(&cipher_descriptor[x], cipher, sizeof(struct ltc_cipher_descriptor)); return x; } } /* no spot */ return -1; } --- NEW FILE: crypt_unregister_prng.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_unregister_prng.c Unregister a PRNG, Tom St Denis */ /** Unregister a PRNG from the descriptor table @param prng The PRNG descriptor to remove @return CRYPT_OK on success */ int unregister_prng(const struct ltc_prng_descriptor *prng) { int x; LTC_ARGCHK(prng != NULL); /* is it already registered? */ for (x = 0; x < TAB_SIZE; x++) { if (memcmp(&prng_descriptor[x], prng, sizeof(struct ltc_prng_descriptor)) != 0) { prng_descriptor[x].name = NULL; return CRYPT_OK; } } return CRYPT_ERROR; } --- NEW FILE: crypt_find_cipher_id.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file crypt_find_cipher_id.c Find cipher by ID, Tom St Denis */ /** Find a cipher by ID number @param ID The ID (not same as index) of the cipher to find @return >= 0 if found, -1 if not present */ int find_cipher_id(unsigned char ID) { int x; for (x = 0; x < TAB_SIZE; x++) { if (cipher_descriptor[x].ID == ID) { return (cipher_descriptor[x].name == NULL) ? -1 : x; } } return -1; } |
From: Mark F. <mar...@us...> - 2005-04-28 17:28:12
|
Update of /cvsroot/opentnl/tnl/libtomcrypt/misc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/misc Added Files: burn_stack.c error_to_string.c zeromem.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- NEW FILE: zeromem.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file zeromem.c Zero a block of memory, Tom St Denis */ /** Zero a block of memory @param out The destination of the area to zero @param outlen The length of the area to zero (octets) */ void zeromem(void *out, size_t outlen) { unsigned char *mem = out; LTC_ARGCHK(out != NULL); while (outlen-- > 0) { *mem++ = 0; } } --- NEW FILE: error_to_string.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file error_to_string.c Convert error codes to ASCII strings, Tom St Denis */ static const char *err_2_str[] = { "CRYPT_OK", "CRYPT_ERROR", "Non-fatal 'no-operation' requested.", "Invalid keysize for block cipher.", "Invalid number of rounds for block cipher.", "Algorithm failed test vectors.", "Buffer overflow.", "Invalid input packet.", "Invalid number of bits for a PRNG.", "Error reading the PRNG.", "Invalid cipher specified.", "Invalid hash specified.", "Invalid PRNG specified.", "Out of memory.", "Invalid PK key or key type specified for function.", "A private PK key is required.", "Invalid argument provided.", "File Not Found", "Invalid PK type.", "Invalid PK system.", "Duplicate PK key found on keyring.", "Key not found in keyring.", "Invalid sized parameter.", "Invalid size for prime.", }; /** Convert an LTC error code to ASCII @param err The error code @return A pointer to the ASCII NUL terminated string for the error or "Invalid error code." if the err code was not valid. */ const char *error_to_string(int err) { if (err < 0 || err >= (int)(sizeof(err_2_str)/sizeof(err_2_str[0]))) { return "Invalid error code."; } else { return err_2_str[err]; } } --- NEW FILE: burn_stack.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file burn_stack.c Burn stack, Tom St Denis */ /** Burn some stack memory @param len amount of stack to burn in bytes */ void burn_stack(unsigned long len) { unsigned char buf[32]; zeromem(buf, sizeof(buf)); if (len > (unsigned long)sizeof(buf)) burn_stack(len - sizeof(buf)); } |
From: Mark F. <mar...@us...> - 2005-04-28 17:28:12
|
Update of /cvsroot/opentnl/tnl/libtomcrypt/modes/ofb In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/modes/ofb Added Files: ofb_decrypt.c ofb_done.c ofb_encrypt.c ofb_getiv.c ofb_setiv.c ofb_start.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- NEW FILE: ofb_encrypt.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ofb_encrypt.c OFB implementation, encrypt data, Tom St Denis */ #ifdef OFB /** OFB encrypt @param pt Plaintext @param ct [out] Ciphertext @param len Length of plaintext (octets) @param ofb OFB state @return CRYPT_OK if successful */ int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb) { int err; LTC_ARGCHK(pt != NULL); LTC_ARGCHK(ct != NULL); LTC_ARGCHK(ofb != NULL); if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) { return err; } /* is blocklen/padlen valid? */ if (ofb->blocklen < 0 || ofb->blocklen > (int)sizeof(ofb->IV) || ofb->padlen < 0 || ofb->padlen > (int)sizeof(ofb->IV)) { return CRYPT_INVALID_ARG; } while (len-- > 0) { if (ofb->padlen == ofb->blocklen) { cipher_descriptor[ofb->cipher].ecb_encrypt(ofb->IV, ofb->IV, &ofb->key); ofb->padlen = 0; } *ct++ = *pt++ ^ ofb->IV[ofb->padlen++]; } return CRYPT_OK; } #endif --- NEW FILE: ofb_start.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ofb_start.c OFB implementation, start chain, Tom St Denis */ #ifdef OFB /** Initialize a OFB context @param cipher The index of the cipher desired @param IV The initial vector @param key The secret key @param keylen The length of the secret key (octets) @param num_rounds Number of rounds in the cipher desired (0 for default) @param ofb The OFB state to initialize @return CRYPT_OK if successful */ int ofb_start(int cipher, const unsigned char *IV, const unsigned char *key, int keylen, int num_rounds, symmetric_OFB *ofb) { int x, err; LTC_ARGCHK(IV != NULL); LTC_ARGCHK(key != NULL); LTC_ARGCHK(ofb != NULL); if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { return err; } /* copy details */ ofb->cipher = cipher; ofb->blocklen = cipher_descriptor[cipher].block_length; for (x = 0; x < ofb->blocklen; x++) { ofb->IV[x] = IV[x]; } /* init the cipher */ ofb->padlen = ofb->blocklen; return cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ofb->key); } #endif --- NEW FILE: ofb_done.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ofb_done.c OFB implementation, finish chain, Tom St Denis */ #ifdef OFB /** Terminate the chain @param ofb The OFB chain to terminate @return CRYPT_OK on success */ int ofb_done(symmetric_OFB *ofb) { int err; LTC_ARGCHK(ofb != NULL); if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) { return err; } cipher_descriptor[ofb->cipher].done(&ofb->key); return CRYPT_OK; } #endif --- NEW FILE: ofb_setiv.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ofb_setiv.c OFB implementation, set IV, Tom St Denis */ #ifdef OFB /** Set an initial vector @param IV The initial vector @param len The length of the vector (in octets) @param ofb The OFB state @return CRYPT_OK if successful */ int ofb_setiv(const unsigned char *IV, unsigned long len, symmetric_OFB *ofb) { int err; LTC_ARGCHK(IV != NULL); LTC_ARGCHK(ofb != NULL); if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) { return err; } if (len != (unsigned long)ofb->blocklen) { return CRYPT_INVALID_ARG; } /* force next block */ ofb->padlen = 0; cipher_descriptor[ofb->cipher].ecb_encrypt(IV, ofb->IV, &ofb->key); return CRYPT_OK; } #endif --- NEW FILE: ofb_decrypt.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ofb_decrypt.c OFB implementation, decrypt data, Tom St Denis */ #ifdef OFB /** OFB decrypt @param ct Ciphertext @param pt [out] Plaintext @param len Length of ciphertext (octets) @param ofb OFB state @return CRYPT_OK if successful */ int ofb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_OFB *ofb) { LTC_ARGCHK(pt != NULL); LTC_ARGCHK(ct != NULL); LTC_ARGCHK(ofb != NULL); return ofb_encrypt(ct, pt, len, ofb); } #endif --- NEW FILE: ofb_getiv.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file ofb_getiv.c OFB implementation, get IV, Tom St Denis */ #ifdef OFB /** Get the current initial vector @param IV [out] The destination of the initial vector @param len [in/out] The max size and resulting size of the initial vector @param ofb The OFB state @return CRYPT_OK if successful */ int ofb_getiv(unsigned char *IV, unsigned long *len, symmetric_OFB *ofb) { LTC_ARGCHK(IV != NULL); LTC_ARGCHK(len != NULL); LTC_ARGCHK(ofb != NULL); if ((unsigned long)ofb->blocklen > *len) { return CRYPT_BUFFER_OVERFLOW; } XMEMCPY(IV, ofb->IV, ofb->blocklen); *len = ofb->blocklen; return CRYPT_OK; } #endif |
From: Mark F. <mar...@us...> - 2005-04-28 17:28:12
|
Update of /cvsroot/opentnl/tnl/libtomcrypt/misc/pkcs5 In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/misc/pkcs5 Added Files: pkcs_5_1.c pkcs_5_2.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- NEW FILE: pkcs_5_2.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include <tomcrypt.h> /** @file pkcs_5_2.c PKCS #5, Algorithm #2, Tom St Denis */ #ifdef PKCS_5 /** Execute PKCS #5 v2 @param password The input password (or key) @param password_len The length of the password (octets) @param salt The salt (or nonce) @param salt_len The length of the salt (octets) @param iteration_count # of iterations desired for PKCS #5 v2 [read specs for more] @param hash_idx The index of the hash desired @param out [out] The destination for this algorithm @param outlen [in/out] The max size and resulting size of the algorithm output @return CRYPT_OK if successful */ int pkcs_5_alg2(const unsigned char *password, unsigned long password_len, const unsigned char *salt, unsigned long salt_len, int iteration_count, int hash_idx, unsigned char *out, unsigned long *outlen) { int err, itts; ulong32 blkno; unsigned long stored, left, x, y; unsigned char *buf[2]; hmac_state *hmac; LTC_ARGCHK(password != NULL); LTC_ARGCHK(salt != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); /* test hash IDX */ if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { return err; } buf[0] = XMALLOC(MAXBLOCKSIZE * 2); hmac = XMALLOC(sizeof(hmac_state)); if (hmac == NULL || buf[0] == NULL) { if (hmac != NULL) { XFREE(hmac); } if (buf[0] != NULL) { XFREE(buf[0]); } return CRYPT_MEM; } /* buf[1] points to the second block of MAXBLOCKSIZE bytes */ buf[1] = buf[0] + MAXBLOCKSIZE; left = *outlen; blkno = 1; stored = 0; while (left != 0) { /* process block number blkno */ zeromem(buf[0], MAXBLOCKSIZE*2); /* store current block number and increment for next pass */ STORE32H(blkno, buf[1]); ++blkno; /* get PRF(P, S||int(blkno)) */ if ((err = hmac_init(hmac, hash_idx, password, password_len)) != CRYPT_OK) { goto LBL_ERR; } if ((err = hmac_process(hmac, salt, salt_len)) != CRYPT_OK) { goto LBL_ERR; } if ((err = hmac_process(hmac, buf[1], 4)) != CRYPT_OK) { goto LBL_ERR; } x = MAXBLOCKSIZE; if ((err = hmac_done(hmac, buf[0], &x)) != CRYPT_OK) { goto LBL_ERR; } /* now compute repeated and XOR it in buf[1] */ XMEMCPY(buf[1], buf[0], x); for (itts = 1; itts < iteration_count; ++itts) { if ((err = hmac_memory(hash_idx, password, password_len, buf[0], x, buf[0], &x)) != CRYPT_OK) { goto LBL_ERR; } for (y = 0; y < x; y++) { buf[1][y] ^= buf[0][y]; } } /* now emit upto x bytes of buf[1] to output */ for (y = 0; y < x && left != 0; ++y) { out[stored++] = buf[1][y]; --left; } } *outlen = stored; err = CRYPT_OK; LBL_ERR: #ifdef LTC_CLEAN_STACK zeromem(buf[0], MAXBLOCKSIZE*2); zeromem(hmac, sizeof(hmac_state)); #endif XFREE(hmac); XFREE(buf[0]); return err; } #endif --- NEW FILE: pkcs_5_1.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include <tomcrypt.h> /** @file pkcs_5_1.c PKCS #5, Algorithm #1, Tom St Denis */ #ifdef PKCS_5 /** Execute PKCS #5 v1 @param password The password (or key) @param password_len The length of the password (octet) @param salt The salt (or nonce) which is 8 octets long @param iteration_count The PKCS #5 v1 iteration count @param hash_idx The index of the hash desired @param out [out] The destination for this algorithm @param outlen [in/out] The max size and resulting size of the algorithm output @return CRYPT_OK if successful */ int pkcs_5_alg1(const unsigned char *password, unsigned long password_len, const unsigned char *salt, int iteration_count, int hash_idx, unsigned char *out, unsigned long *outlen) { int err; unsigned long x; hash_state *md; unsigned char *buf; LTC_ARGCHK(password != NULL); LTC_ARGCHK(salt != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); /* test hash IDX */ if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { return err; } /* allocate memory */ md = XMALLOC(sizeof(hash_state)); buf = XMALLOC(MAXBLOCKSIZE); if (md == NULL || buf == NULL) { if (md != NULL) { XFREE(md); } if (buf != NULL) { XFREE(buf); } return CRYPT_MEM; } /* hash initial password + salt */ if ((err = hash_descriptor[hash_idx].init(md)) != CRYPT_OK) { goto LBL_ERR; } if ((err = hash_descriptor[hash_idx].process(md, password, password_len)) != CRYPT_OK) { goto LBL_ERR; } if ((err = hash_descriptor[hash_idx].process(md, salt, 8)) != CRYPT_OK) { goto LBL_ERR; } if ((err = hash_descriptor[hash_idx].done(md, buf)) != CRYPT_OK) { goto LBL_ERR; } while (--iteration_count) { /* code goes here. */ x = MAXBLOCKSIZE; if ((err = hash_memory(hash_idx, buf, hash_descriptor[hash_idx].hashsize, buf, &x)) != CRYPT_OK) { goto LBL_ERR; } } /* copy upto outlen bytes */ for (x = 0; x < hash_descriptor[hash_idx].hashsize && x < *outlen; x++) { out[x] = buf[x]; } *outlen = x; err = CRYPT_OK; LBL_ERR: #ifdef LTC_CLEAN_STACK zeromem(buf, MAXBLOCKSIZE); zeromem(md, sizeof(hash_state)); #endif XFREE(buf); XFREE(md); return err; } #endif |