├── README.md ├── bitcoin-0.1.0.tgz ├── bitcoin.pdf ├── bitcoin0.1 ├── bitcoin.exe ├── libeay32.dll ├── license.txt ├── mingwm10.dll ├── readme.txt └── src │ ├── base58.h │ ├── bignum.h │ ├── db.cpp │ ├── db.h │ ├── headers.h │ ├── irc.cpp │ ├── irc.h │ ├── key.h │ ├── license.txt │ ├── main.cpp │ ├── main.h │ ├── makefile │ ├── makefile.vc │ ├── market.cpp │ ├── market.h │ ├── net.cpp │ ├── net.h │ ├── rc │ ├── addressbook16.bmp │ ├── addressbook16mask.bmp │ ├── addressbook20.bmp │ ├── addressbook20mask.bmp │ ├── bitcoin.ico │ ├── check.ico │ ├── send16.bmp │ ├── send16mask.bmp │ ├── send16masknoshadow.bmp │ ├── send20.bmp │ └── send20mask.bmp │ ├── readme.txt │ ├── script.cpp │ ├── script.h │ ├── serialize.h │ ├── sha.cpp │ ├── sha.h │ ├── ui.cpp │ ├── ui.h │ ├── ui.rc │ ├── uibase.cpp │ ├── uibase.h │ ├── uint256.h │ ├── uiproject.fbp │ ├── util.cpp │ └── util.h ├── nov08 ├── main.cpp ├── main.h └── node.cpp ├── posts ├── p2p.md ├── paper.md └── release.md ├── precursor ├── bitgold.txt ├── bmoney.txt ├── hashcash.pdf └── rpow-1.2.0 │ ├── .DS_Store │ ├── client │ ├── Makefile │ ├── b64.c │ ├── certvalid.c │ ├── certvalid.h │ ├── connio.c │ ├── cryptchan.c │ ├── cryptchan.h │ ├── gbignum.c │ ├── gbignum.h │ ├── keys.c │ ├── rpio.c │ ├── rpow.i │ ├── rpowcli.c │ ├── rpowcli.h │ ├── rpowclient.c │ ├── rpowclient.h │ ├── rpowutil.c │ ├── scc.h │ ├── util4758.c │ └── util4758.h │ ├── common │ ├── commands.h │ ├── errors.h │ └── rpow.h │ ├── scc │ ├── Makefile │ ├── certvalid.c │ ├── cryptchan.c │ ├── cryptchan.h │ ├── dbverify.c │ ├── fixrod.pl │ ├── gbignum.c │ ├── gbignum.h │ ├── hmac.c │ ├── hmac.h │ ├── installed │ │ ├── link.map │ │ ├── rpow.rod │ │ └── rpow.xld │ ├── keygen.c │ ├── persist.c │ ├── rpio.c │ ├── rpow.c │ ├── rpowin.txt │ ├── rpowscc.h │ ├── rpowsign.c │ └── rpowutil.c │ └── server │ ├── Makefile │ ├── dbproof.c │ ├── dbproof.h │ ├── gbignum.h │ ├── rpowsrv.c │ ├── sha.h │ └── sha1.c ├── satoshinakamoto.asc └── study ├── db.cpp ├── irc.cpp ├── main.cpp ├── net.cpp ├── script.cpp ├── sha.cpp └── util.cpp /README.md: -------------------------------------------------------------------------------- 1 | # bitcoin original sources 2 | 3 | archive of original bitcoin material 4 | 5 | On Sat, 01 Nov 2008 16:16:33 -0700 Satoshi Nakamoto posted the whitepaper bitcoin.pdf. (md5sum d56d71ecadf2137be09d8b1d35c6c042). 6 | 7 | On Thu Jan 8 14:27:40 EST 2009 the first version was released. (md5sum dca1095f053a0c2dc90b19c92bd1ec00 ). http://www.metzdowd.com/pipermail/cryptography/2009-January/014994.html. 8 | 9 | In november 2008 some sources were distributed privately 10 | 11 | "This is the Bitcoin sources from November 16, 2008 - a few months before the current blockchain began.". "I was on the Metzdowd cryptography list at that time.", https://bitcointalk.org/index.php?topic=382374.0 12 | 13 | the private november 2008 version is in the nov08 folder. 14 | 15 | the study folder contains the main files of the 0.1 version, roughly 7000 LOC. 16 | 17 | * 2660 ./main.cpp 18 | * 1127 ./script.cpp 19 | * 1020 ./net.cpp 20 | * 604 ./db.cpp 21 | * 554 ./sha.cpp 22 | * 373 ./util.cpp 23 | * 265 ./irc.cpp 24 | * 6603 total -------------------------------------------------------------------------------- /bitcoin-0.1.0.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin-0.1.0.tgz -------------------------------------------------------------------------------- /bitcoin.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin.pdf -------------------------------------------------------------------------------- /bitcoin0.1/bitcoin.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/bitcoin.exe -------------------------------------------------------------------------------- /bitcoin0.1/libeay32.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/libeay32.dll -------------------------------------------------------------------------------- /bitcoin0.1/license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009 Satoshi Nakamoto 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /bitcoin0.1/mingwm10.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/mingwm10.dll -------------------------------------------------------------------------------- /bitcoin0.1/readme.txt: -------------------------------------------------------------------------------- 1 | BitCoin v0.01 ALPHA 2 | 3 | Copyright (c) 2009 Satoshi Nakamoto 4 | Distributed under the MIT/X11 software license, see the accompanying 5 | file license.txt or http://www.opensource.org/licenses/mit-license.php. 6 | This product includes software developed by the OpenSSL Project for use in 7 | the OpenSSL Toolkit (http://www.openssl.org/). This product includes 8 | cryptographic software written by Eric Young (eay@cryptsoft.com). 9 | 10 | 11 | Intro 12 | ----- 13 | Bitcoin is an electronic cash system that uses a peer-to-peer network to 14 | prevent double-spending. It's completely decentralized with no server or 15 | central authority. 16 | 17 | 18 | Operating Systems 19 | ----------------- 20 | Windows NT/2000/XP (and probably Vista) 21 | 22 | Vista hasn't been tested yet. All the libraries used are cross-platform, so 23 | there's nothing preventing future Linux and Mac builds. 24 | 25 | 26 | Setup 27 | ----- 28 | Unpack the files into a directory and run bitcoin.exe. 29 | 30 | The software automatically finds other nodes to connect to. You should set 31 | your firewall to forward port 8333 to your computer so you can receive incoming 32 | connections, otherwise the nodes you can connect with will be limited. 33 | 34 | To support the network by running a node, select: 35 | 36 | Options->Generate Coins 37 | 38 | and keep the program open or minimized. It runs at idle priority when no other 39 | programs are using the CPU. Your computer will be solving a very difficult 40 | computational problem that is used to lock in blocks of transactions. The time 41 | to generate a block varies each time, but may take days or months, depending 42 | on the speed of your computer and the competition on the network. It's not a 43 | computation that has to start over from the beginning if you stop and restart 44 | it. A solution might be found at any given moment it's running. As a reward 45 | for supporting the network, you receive coins when you successfully generate a 46 | block. 47 | -------------------------------------------------------------------------------- /bitcoin0.1/src/base58.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009 Satoshi Nakamoto 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file license.txt or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | 6 | // 7 | // Why base-58 instead of standard base-64 encoding? 8 | // - Don't want 0OIl characters that look the same in some fonts and 9 | // could be used to create visually identical looking account numbers. 10 | // - A string with non-alphanumeric characters is not as easily accepted as an account number. 11 | // - E-mail usually won't line-break if there's no punctuation to break at. 12 | // - Doubleclicking selects the whole number as one word if it's all alphanumeric. 13 | // 14 | 15 | 16 | static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; 17 | 18 | 19 | inline string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) 20 | { 21 | CAutoBN_CTX pctx; 22 | CBigNum bn58 = 58; 23 | CBigNum bn0 = 0; 24 | 25 | // Convert big endian data to little endian 26 | // Extra zero at the end make sure bignum will interpret as a positive number 27 | vector vchTmp(pend-pbegin+1, 0); 28 | reverse_copy(pbegin, pend, vchTmp.begin()); 29 | 30 | // Convert little endian data to bignum 31 | CBigNum bn; 32 | bn.setvch(vchTmp); 33 | 34 | // Convert bignum to string 35 | string str; 36 | str.reserve((pend - pbegin) * 138 / 100 + 1); 37 | CBigNum dv; 38 | CBigNum rem; 39 | while (bn > bn0) 40 | { 41 | if (!BN_div(&dv, &rem, &bn, &bn58, pctx)) 42 | throw bignum_error("EncodeBase58 : BN_div failed"); 43 | bn = dv; 44 | unsigned int c = rem.getulong(); 45 | str += pszBase58[c]; 46 | } 47 | 48 | // Leading zeroes encoded as base58 zeros 49 | for (const unsigned char* p = pbegin; p < pend && *p == 0; p++) 50 | str += pszBase58[0]; 51 | 52 | // Convert little endian string to big endian 53 | reverse(str.begin(), str.end()); 54 | return str; 55 | } 56 | 57 | inline string EncodeBase58(const vector& vch) 58 | { 59 | return EncodeBase58(&vch[0], &vch[0] + vch.size()); 60 | } 61 | 62 | inline bool DecodeBase58(const char* psz, vector& vchRet) 63 | { 64 | CAutoBN_CTX pctx; 65 | vchRet.clear(); 66 | CBigNum bn58 = 58; 67 | CBigNum bn = 0; 68 | CBigNum bnChar; 69 | while (isspace(*psz)) 70 | psz++; 71 | 72 | // Convert big endian string to bignum 73 | for (const char* p = psz; *p; p++) 74 | { 75 | const char* p1 = strchr(pszBase58, *p); 76 | if (p1 == NULL) 77 | { 78 | while (isspace(*p)) 79 | p++; 80 | if (*p != '\0') 81 | return false; 82 | break; 83 | } 84 | bnChar.setulong(p1 - pszBase58); 85 | if (!BN_mul(&bn, &bn, &bn58, pctx)) 86 | throw bignum_error("DecodeBase58 : BN_mul failed"); 87 | bn += bnChar; 88 | } 89 | 90 | // Get bignum as little endian data 91 | vector vchTmp = bn.getvch(); 92 | 93 | // Trim off sign byte if present 94 | if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80) 95 | vchTmp.erase(vchTmp.end()-1); 96 | 97 | // Restore leading zeros 98 | int nLeadingZeros = 0; 99 | for (const char* p = psz; *p == pszBase58[0]; p++) 100 | nLeadingZeros++; 101 | vchRet.assign(nLeadingZeros + vchTmp.size(), 0); 102 | 103 | // Convert little endian data to big endian 104 | reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size()); 105 | return true; 106 | } 107 | 108 | inline bool DecodeBase58(const string& str, vector& vchRet) 109 | { 110 | return DecodeBase58(str.c_str(), vchRet); 111 | } 112 | 113 | 114 | 115 | 116 | 117 | inline string EncodeBase58Check(const vector& vchIn) 118 | { 119 | // add 4-byte hash check to the end 120 | vector vch(vchIn); 121 | uint256 hash = Hash(vch.begin(), vch.end()); 122 | vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4); 123 | return EncodeBase58(vch); 124 | } 125 | 126 | inline bool DecodeBase58Check(const char* psz, vector& vchRet) 127 | { 128 | if (!DecodeBase58(psz, vchRet)) 129 | return false; 130 | if (vchRet.size() < 4) 131 | { 132 | vchRet.clear(); 133 | return false; 134 | } 135 | uint256 hash = Hash(vchRet.begin(), vchRet.end()-4); 136 | if (memcmp(&hash, &vchRet.end()[-4], 4) != 0) 137 | { 138 | vchRet.clear(); 139 | return false; 140 | } 141 | vchRet.resize(vchRet.size()-4); 142 | return true; 143 | } 144 | 145 | inline bool DecodeBase58Check(const string& str, vector& vchRet) 146 | { 147 | return DecodeBase58Check(str.c_str(), vchRet); 148 | } 149 | 150 | 151 | 152 | 153 | 154 | 155 | static const unsigned char ADDRESSVERSION = 0; 156 | 157 | inline string Hash160ToAddress(uint160 hash160) 158 | { 159 | // add 1-byte version number to the front 160 | vector vch(1, ADDRESSVERSION); 161 | vch.insert(vch.end(), UBEGIN(hash160), UEND(hash160)); 162 | return EncodeBase58Check(vch); 163 | } 164 | 165 | inline bool AddressToHash160(const char* psz, uint160& hash160Ret) 166 | { 167 | vector vch; 168 | if (!DecodeBase58Check(psz, vch)) 169 | return false; 170 | if (vch.empty()) 171 | return false; 172 | unsigned char nVersion = vch[0]; 173 | if (vch.size() != sizeof(hash160Ret) + 1) 174 | return false; 175 | memcpy(&hash160Ret, &vch[1], sizeof(hash160Ret)); 176 | return (nVersion <= ADDRESSVERSION); 177 | } 178 | 179 | inline bool AddressToHash160(const string& str, uint160& hash160Ret) 180 | { 181 | return AddressToHash160(str.c_str(), hash160Ret); 182 | } 183 | 184 | inline bool IsValidBitcoinAddress(const char* psz) 185 | { 186 | uint160 hash160; 187 | return AddressToHash160(psz, hash160); 188 | } 189 | 190 | inline bool IsValidBitcoinAddress(const string& str) 191 | { 192 | return IsValidBitcoinAddress(str.c_str()); 193 | } 194 | 195 | 196 | 197 | 198 | inline string PubKeyToAddress(const vector& vchPubKey) 199 | { 200 | return Hash160ToAddress(Hash160(vchPubKey)); 201 | } 202 | -------------------------------------------------------------------------------- /bitcoin0.1/src/headers.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009 Satoshi Nakamoto 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file license.txt or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifdef _MSC_VER 6 | #pragma warning(disable:4786) 7 | #pragma warning(disable:4804) 8 | #pragma warning(disable:4717) 9 | #endif 10 | #ifdef _WIN32_WINNT 11 | #undef _WIN32_WINNT 12 | #endif 13 | #define _WIN32_WINNT 0x0400 14 | #define WIN32_LEAN_AND_MEAN 1 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #define BOUNDSCHECK 1 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include 52 | #pragma hdrstop 53 | using namespace std; 54 | using namespace boost; 55 | 56 | 57 | 58 | #include "serialize.h" 59 | #include "uint256.h" 60 | #include "util.h" 61 | #include "key.h" 62 | #include "bignum.h" 63 | #include "base58.h" 64 | #include "script.h" 65 | #include "db.h" 66 | #include "net.h" 67 | #include "irc.h" 68 | #include "main.h" 69 | #include "market.h" 70 | #include "uibase.h" 71 | #include "ui.h" 72 | -------------------------------------------------------------------------------- /bitcoin0.1/src/irc.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009 Satoshi Nakamoto 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file license.txt or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #include "headers.h" 6 | 7 | 8 | 9 | 10 | #pragma pack(1) 11 | struct ircaddr 12 | { 13 | int ip; 14 | short port; 15 | }; 16 | 17 | string EncodeAddress(const CAddress& addr) 18 | { 19 | struct ircaddr tmp; 20 | tmp.ip = addr.ip; 21 | tmp.port = addr.port; 22 | 23 | vector vch(UBEGIN(tmp), UEND(tmp)); 24 | return string("u") + EncodeBase58Check(vch); 25 | } 26 | 27 | bool DecodeAddress(string str, CAddress& addr) 28 | { 29 | vector vch; 30 | if (!DecodeBase58Check(str.substr(1), vch)) 31 | return false; 32 | 33 | struct ircaddr tmp; 34 | if (vch.size() != sizeof(tmp)) 35 | return false; 36 | memcpy(&tmp, &vch[0], sizeof(tmp)); 37 | 38 | addr = CAddress(tmp.ip, tmp.port); 39 | return true; 40 | } 41 | 42 | 43 | 44 | 45 | 46 | 47 | static bool Send(SOCKET hSocket, const char* pszSend) 48 | { 49 | if (strstr(pszSend, "PONG") != pszSend) 50 | printf("SENDING: %s\n", pszSend); 51 | const char* psz = pszSend; 52 | const char* pszEnd = psz + strlen(psz); 53 | while (psz < pszEnd) 54 | { 55 | int ret = send(hSocket, psz, pszEnd - psz, 0); 56 | if (ret < 0) 57 | return false; 58 | psz += ret; 59 | } 60 | return true; 61 | } 62 | 63 | bool RecvLine(SOCKET hSocket, string& strLine) 64 | { 65 | strLine = ""; 66 | loop 67 | { 68 | char c; 69 | int nBytes = recv(hSocket, &c, 1, 0); 70 | if (nBytes > 0) 71 | { 72 | if (c == '\n') 73 | continue; 74 | if (c == '\r') 75 | return true; 76 | strLine += c; 77 | } 78 | else if (nBytes <= 0) 79 | { 80 | if (!strLine.empty()) 81 | return true; 82 | // socket closed 83 | printf("IRC socket closed\n"); 84 | return false; 85 | } 86 | else 87 | { 88 | // socket error 89 | int nErr = WSAGetLastError(); 90 | if (nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS) 91 | { 92 | printf("IRC recv failed: %d\n", nErr); 93 | return false; 94 | } 95 | } 96 | } 97 | } 98 | 99 | bool RecvLineIRC(SOCKET hSocket, string& strLine) 100 | { 101 | loop 102 | { 103 | bool fRet = RecvLine(hSocket, strLine); 104 | if (fRet) 105 | { 106 | if (fShutdown) 107 | return false; 108 | vector vWords; 109 | ParseString(strLine, ' ', vWords); 110 | if (vWords[0] == "PING") 111 | { 112 | strLine[1] = 'O'; 113 | strLine += '\r'; 114 | Send(hSocket, strLine.c_str()); 115 | continue; 116 | } 117 | } 118 | return fRet; 119 | } 120 | } 121 | 122 | bool RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL) 123 | { 124 | loop 125 | { 126 | string strLine; 127 | if (!RecvLineIRC(hSocket, strLine)) 128 | return false; 129 | printf("IRC %s\n", strLine.c_str()); 130 | if (psz1 && strLine.find(psz1) != -1) 131 | return true; 132 | if (psz2 && strLine.find(psz2) != -1) 133 | return true; 134 | if (psz3 && strLine.find(psz3) != -1) 135 | return true; 136 | } 137 | } 138 | 139 | 140 | 141 | 142 | bool fRestartIRCSeed = false; 143 | 144 | void ThreadIRCSeed(void* parg) 145 | { 146 | loop 147 | { 148 | struct hostent* phostent = gethostbyname("chat.freenode.net"); 149 | CAddress addrConnect(*(u_long*)phostent->h_addr_list[0], htons(6667)); 150 | 151 | SOCKET hSocket; 152 | if (!ConnectSocket(addrConnect, hSocket)) 153 | { 154 | printf("IRC connect failed\n"); 155 | return; 156 | } 157 | 158 | if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname")) 159 | { 160 | closesocket(hSocket); 161 | return; 162 | } 163 | 164 | string strMyName = EncodeAddress(addrLocalHost); 165 | 166 | if (!addrLocalHost.IsRoutable()) 167 | strMyName = strprintf("x%u", GetRand(1000000000)); 168 | 169 | Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str()); 170 | Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str()); 171 | 172 | if (!RecvUntil(hSocket, " 004 ")) 173 | { 174 | closesocket(hSocket); 175 | return; 176 | } 177 | Sleep(500); 178 | 179 | Send(hSocket, "JOIN #bitcoin\r"); 180 | Send(hSocket, "WHO #bitcoin\r"); 181 | 182 | while (!fRestartIRCSeed) 183 | { 184 | string strLine; 185 | if (fShutdown || !RecvLineIRC(hSocket, strLine)) 186 | { 187 | closesocket(hSocket); 188 | return; 189 | } 190 | if (strLine.empty() || strLine[0] != ':') 191 | continue; 192 | printf("IRC %s\n", strLine.c_str()); 193 | 194 | vector vWords; 195 | ParseString(strLine, ' ', vWords); 196 | if (vWords.size() < 2) 197 | continue; 198 | 199 | char pszName[10000]; 200 | pszName[0] = '\0'; 201 | 202 | if (vWords[1] == "352" && vWords.size() >= 8) 203 | { 204 | // index 7 is limited to 16 characters 205 | // could get full length name at index 10, but would be different from join messages 206 | strcpy(pszName, vWords[7].c_str()); 207 | printf("GOT WHO: [%s] ", pszName); 208 | } 209 | 210 | if (vWords[1] == "JOIN") 211 | { 212 | // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname 213 | strcpy(pszName, vWords[0].c_str() + 1); 214 | if (strchr(pszName, '!')) 215 | *strchr(pszName, '!') = '\0'; 216 | printf("GOT JOIN: [%s] ", pszName); 217 | } 218 | 219 | if (pszName[0] == 'u') 220 | { 221 | CAddress addr; 222 | if (DecodeAddress(pszName, addr)) 223 | { 224 | CAddrDB addrdb; 225 | if (AddAddress(addrdb, addr)) 226 | printf("new "); 227 | addr.print(); 228 | } 229 | else 230 | { 231 | printf("decode failed\n"); 232 | } 233 | } 234 | } 235 | 236 | fRestartIRCSeed = false; 237 | closesocket(hSocket); 238 | } 239 | } 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | #ifdef TEST 251 | int main(int argc, char *argv[]) 252 | { 253 | WSADATA wsadata; 254 | if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR) 255 | { 256 | printf("Error at WSAStartup()\n"); 257 | return false; 258 | } 259 | 260 | ThreadIRCSeed(NULL); 261 | 262 | WSACleanup(); 263 | return 0; 264 | } 265 | #endif 266 | -------------------------------------------------------------------------------- /bitcoin0.1/src/irc.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009 Satoshi Nakamoto 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file license.txt or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | extern bool RecvLine(SOCKET hSocket, string& strLine); 6 | extern void ThreadIRCSeed(void* parg); 7 | extern bool fRestartIRCSeed; 8 | -------------------------------------------------------------------------------- /bitcoin0.1/src/key.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009 Satoshi Nakamoto 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file license.txt or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | 6 | // secp160k1 7 | // const unsigned int PRIVATE_KEY_SIZE = 192; 8 | // const unsigned int PUBLIC_KEY_SIZE = 41; 9 | // const unsigned int SIGNATURE_SIZE = 48; 10 | // 11 | // secp192k1 12 | // const unsigned int PRIVATE_KEY_SIZE = 222; 13 | // const unsigned int PUBLIC_KEY_SIZE = 49; 14 | // const unsigned int SIGNATURE_SIZE = 57; 15 | // 16 | // secp224k1 17 | // const unsigned int PRIVATE_KEY_SIZE = 250; 18 | // const unsigned int PUBLIC_KEY_SIZE = 57; 19 | // const unsigned int SIGNATURE_SIZE = 66; 20 | // 21 | // secp256k1: 22 | // const unsigned int PRIVATE_KEY_SIZE = 279; 23 | // const unsigned int PUBLIC_KEY_SIZE = 65; 24 | // const unsigned int SIGNATURE_SIZE = 72; 25 | // 26 | // see www.keylength.com 27 | // script supports up to 75 for single byte push 28 | 29 | 30 | 31 | class key_error : public std::runtime_error 32 | { 33 | public: 34 | explicit key_error(const std::string& str) : std::runtime_error(str) {} 35 | }; 36 | 37 | 38 | // secure_allocator is defined is serialize.h 39 | typedef vector > CPrivKey; 40 | 41 | 42 | 43 | class CKey 44 | { 45 | protected: 46 | EC_KEY* pkey; 47 | 48 | public: 49 | CKey() 50 | { 51 | pkey = EC_KEY_new_by_curve_name(NID_secp256k1); 52 | if (pkey == NULL) 53 | throw key_error("CKey::CKey() : EC_KEY_new_by_curve_name failed"); 54 | } 55 | 56 | CKey(const CKey& b) 57 | { 58 | pkey = EC_KEY_dup(b.pkey); 59 | if (pkey == NULL) 60 | throw key_error("CKey::CKey(const CKey&) : EC_KEY_dup failed"); 61 | } 62 | 63 | CKey& operator=(const CKey& b) 64 | { 65 | if (!EC_KEY_copy(pkey, b.pkey)) 66 | throw key_error("CKey::operator=(const CKey&) : EC_KEY_copy failed"); 67 | return (*this); 68 | } 69 | 70 | ~CKey() 71 | { 72 | EC_KEY_free(pkey); 73 | } 74 | 75 | void MakeNewKey() 76 | { 77 | if (!EC_KEY_generate_key(pkey)) 78 | throw key_error("CKey::MakeNewKey() : EC_KEY_generate_key failed"); 79 | } 80 | 81 | bool SetPrivKey(const CPrivKey& vchPrivKey) 82 | { 83 | const unsigned char* pbegin = &vchPrivKey[0]; 84 | if (!d2i_ECPrivateKey(&pkey, &pbegin, vchPrivKey.size())) 85 | return false; 86 | return true; 87 | } 88 | 89 | CPrivKey GetPrivKey() const 90 | { 91 | unsigned int nSize = i2d_ECPrivateKey(pkey, NULL); 92 | if (!nSize) 93 | throw key_error("CKey::GetPrivKey() : i2d_ECPrivateKey failed"); 94 | CPrivKey vchPrivKey(nSize, 0); 95 | unsigned char* pbegin = &vchPrivKey[0]; 96 | if (i2d_ECPrivateKey(pkey, &pbegin) != nSize) 97 | throw key_error("CKey::GetPrivKey() : i2d_ECPrivateKey returned unexpected size"); 98 | return vchPrivKey; 99 | } 100 | 101 | bool SetPubKey(const vector& vchPubKey) 102 | { 103 | const unsigned char* pbegin = &vchPubKey[0]; 104 | if (!o2i_ECPublicKey(&pkey, &pbegin, vchPubKey.size())) 105 | return false; 106 | return true; 107 | } 108 | 109 | vector GetPubKey() const 110 | { 111 | unsigned int nSize = i2o_ECPublicKey(pkey, NULL); 112 | if (!nSize) 113 | throw key_error("CKey::GetPubKey() : i2o_ECPublicKey failed"); 114 | vector vchPubKey(nSize, 0); 115 | unsigned char* pbegin = &vchPubKey[0]; 116 | if (i2o_ECPublicKey(pkey, &pbegin) != nSize) 117 | throw key_error("CKey::GetPubKey() : i2o_ECPublicKey returned unexpected size"); 118 | return vchPubKey; 119 | } 120 | 121 | bool Sign(uint256 hash, vector& vchSig) 122 | { 123 | vchSig.clear(); 124 | unsigned char pchSig[10000]; 125 | unsigned int nSize = 0; 126 | if (!ECDSA_sign(0, (unsigned char*)&hash, sizeof(hash), pchSig, &nSize, pkey)) 127 | return false; 128 | vchSig.resize(nSize); 129 | memcpy(&vchSig[0], pchSig, nSize); 130 | return true; 131 | } 132 | 133 | bool Verify(uint256 hash, const vector& vchSig) 134 | { 135 | // -1 = error, 0 = bad sig, 1 = good 136 | if (ECDSA_verify(0, (unsigned char*)&hash, sizeof(hash), &vchSig[0], vchSig.size(), pkey) != 1) 137 | return false; 138 | return true; 139 | } 140 | 141 | static bool Sign(const CPrivKey& vchPrivKey, uint256 hash, vector& vchSig) 142 | { 143 | CKey key; 144 | if (!key.SetPrivKey(vchPrivKey)) 145 | return false; 146 | return key.Sign(hash, vchSig); 147 | } 148 | 149 | static bool Verify(const vector& vchPubKey, uint256 hash, const vector& vchSig) 150 | { 151 | CKey key; 152 | if (!key.SetPubKey(vchPubKey)) 153 | return false; 154 | return key.Verify(hash, vchSig); 155 | } 156 | }; 157 | -------------------------------------------------------------------------------- /bitcoin0.1/src/license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009 Satoshi Nakamoto 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /bitcoin0.1/src/makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 Satoshi Nakamoto 2 | # Distributed under the MIT/X11 software license, see the accompanying 3 | # file license.txt or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | 6 | ifneq "$(BUILD)" "debug" 7 | ifneq "$(BUILD)" "release" 8 | BUILD=debug 9 | endif 10 | endif 11 | ifeq "$(BUILD)" "debug" 12 | D=d 13 | # note: gcc 3.x profile doesn't work 14 | #DEBUGFLAGS=-O0 -g -pg -D__WXDEBUG__ 15 | DEBUGFLAGS=-g -D__WXDEBUG__ 16 | endif 17 | 18 | 19 | 20 | INCLUDEPATHS=-I"/boost" -I"/DB/build_unix" -I"/OpenSSL/include" -I"/wxWidgets/lib/vc_lib/mswd" -I"/wxWidgets/include" 21 | LIBPATHS=-L"/DB/build_unix" -L"/OpenSSL/out" -L"/wxWidgets/lib/gcc_lib" 22 | LIBS= \ 23 | -l db_cxx \ 24 | -l eay32 \ 25 | -l wxmsw28$(D)_richtext -l wxmsw28$(D)_html -l wxmsw28$(D)_core -l wxbase28$(D) -l wxtiff$(D) -l wxjpeg$(D) -l wxpng$(D) -l wxzlib$(D) -l wxregex$(D) -l wxexpat$(D) \ 26 | -l kernel32 -l user32 -l gdi32 -l comdlg32 -l winspool -l winmm -l shell32 -l comctl32 -l ole32 -l oleaut32 -l uuid -l rpcrt4 -l advapi32 -l ws2_32 27 | WXDEFS=-DWIN32 -D__WXMSW__ -D_WINDOWS -DNOPCH 28 | CFLAGS=-mthreads -O0 -w -Wno-invalid-offsetof -Wformat $(DEBUGFLAGS) $(WXDEFS) $(INCLUDEPATHS) 29 | HEADERS=headers.h util.h main.h serialize.h uint256.h key.h bignum.h script.h db.h base58.h 30 | 31 | 32 | 33 | all: bitcoin.exe 34 | 35 | 36 | headers.h.gch: headers.h $(HEADERS) net.h irc.h market.h uibase.h ui.h 37 | g++ -c $(CFLAGS) -o $@ $< 38 | 39 | obj/util.o: util.cpp $(HEADERS) 40 | g++ -c $(CFLAGS) -o $@ $< 41 | 42 | obj/script.o: script.cpp $(HEADERS) 43 | g++ -c $(CFLAGS) -o $@ $< 44 | 45 | obj/db.o: db.cpp $(HEADERS) market.h 46 | g++ -c $(CFLAGS) -o $@ $< 47 | 48 | obj/net.o: net.cpp $(HEADERS) net.h 49 | g++ -c $(CFLAGS) -o $@ $< 50 | 51 | obj/main.o: main.cpp $(HEADERS) net.h market.h sha.h 52 | g++ -c $(CFLAGS) -o $@ $< 53 | 54 | obj/market.o: market.cpp $(HEADERS) market.h 55 | g++ -c $(CFLAGS) -o $@ $< 56 | 57 | obj/ui.o: ui.cpp $(HEADERS) net.h uibase.h ui.h market.h 58 | g++ -c $(CFLAGS) -o $@ $< 59 | 60 | obj/uibase.o: uibase.cpp uibase.h 61 | g++ -c $(CFLAGS) -o $@ $< 62 | 63 | obj/sha.o: sha.cpp sha.h 64 | g++ -c $(CFLAGS) -O3 -o $@ $< 65 | 66 | obj/irc.o: irc.cpp $(HEADERS) 67 | g++ -c $(CFLAGS) -o $@ $< 68 | 69 | obj/ui_res.o: ui.rc rc/bitcoin.ico rc/check.ico rc/send16.bmp rc/send16mask.bmp rc/send16masknoshadow.bmp rc/send20.bmp rc/send20mask.bmp rc/addressbook16.bmp rc/addressbook16mask.bmp rc/addressbook20.bmp rc/addressbook20mask.bmp 70 | windres $(WXDEFS) $(INCLUDEPATHS) -o $@ -i $< 71 | 72 | 73 | 74 | OBJS=obj/util.o obj/script.o obj/db.o obj/net.o obj/main.o obj/market.o \ 75 | obj/ui.o obj/uibase.o obj/sha.o obj/irc.o obj/ui_res.o 76 | 77 | bitcoin.exe: headers.h.gch $(OBJS) 78 | -kill /f bitcoin.exe 79 | g++ $(CFLAGS) -mwindows -Wl,--subsystem,windows -o $@ $(LIBPATHS) $(OBJS) $(LIBS) 80 | 81 | clean: 82 | -del /Q obj\* 83 | -del /Q headers.h.gch 84 | -------------------------------------------------------------------------------- /bitcoin0.1/src/makefile.vc: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 Satoshi Nakamoto 2 | # Distributed under the MIT/X11 software license, see the accompanying 3 | # file license.txt or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | 6 | !IF "$(BUILD)" != "debug" && "$(BUILD)" != "release" 7 | BUILD=debug 8 | !ENDIF 9 | !IF "$(BUILD)" == "debug" 10 | D=d 11 | DEBUGFLAGS=/Zi /Od /D__WXDEBUG__ 12 | !ENDIF 13 | 14 | 15 | 16 | INCLUDEPATHS=/I"/boost" /I"/DB/build_windows" /I"/OpenSSL/include" /I"/wxWidgets/lib/vc_lib/mswd" /I"/wxWidgets/include" 17 | LIBPATHS=/LIBPATH:"/DB/build_windows/$(BUILD)" /LIBPATH:"/OpenSSL/out" /LIBPATH:"/wxWidgets/lib/vc_lib" 18 | LIBS= \ 19 | libdb47s$(D).lib \ 20 | libeay32.lib \ 21 | wxmsw28$(D)_richtext.lib wxmsw28$(D)_html.lib wxmsw28$(D)_core.lib wxbase28$(D).lib wxtiff$(D).lib wxjpeg$(D).lib wxpng$(D).lib wxzlib$(D).lib wxregex$(D).lib wxexpat$(D).lib \ 22 | kernel32.lib user32.lib gdi32.lib comdlg32.lib winspool.lib winmm.lib shell32.lib comctl32.lib ole32.lib oleaut32.lib uuid.lib rpcrt4.lib advapi32.lib ws2_32.lib 23 | WXDEFS=/DWIN32 /D__WXMSW__ /D_WINDOWS /DNOPCH 24 | CFLAGS=/c /nologo /Ob0 /MD$(D) /EHsc /GR /Zm300 /YX /Fpobj/headers.pch $(DEBUGFLAGS) $(WXDEFS) $(INCLUDEPATHS) 25 | HEADERS=headers.h util.h main.h serialize.h uint256.h key.h bignum.h script.h db.h base58.h 26 | 27 | 28 | 29 | all: bitcoin.exe 30 | 31 | 32 | obj\util.obj: util.cpp $(HEADERS) 33 | cl $(CFLAGS) /Fo$@ %s 34 | 35 | obj\script.obj: script.cpp $(HEADERS) 36 | cl $(CFLAGS) /Fo$@ %s 37 | 38 | obj\db.obj: db.cpp $(HEADERS) market.h 39 | cl $(CFLAGS) /Fo$@ %s 40 | 41 | obj\net.obj: net.cpp $(HEADERS) net.h 42 | cl $(CFLAGS) /Fo$@ %s 43 | 44 | obj\main.obj: main.cpp $(HEADERS) net.h market.h 45 | cl $(CFLAGS) /Fo$@ %s 46 | 47 | obj\market.obj: market.cpp $(HEADERS) market.h 48 | cl $(CFLAGS) /Fo$@ %s 49 | 50 | obj\ui.obj: ui.cpp $(HEADERS) net.h uibase.h ui.h market.h 51 | cl $(CFLAGS) /Fo$@ %s 52 | 53 | obj\uibase.obj: uibase.cpp uibase.h 54 | cl $(CFLAGS) /Fo$@ %s 55 | 56 | obj\sha.obj: sha.cpp sha.h 57 | cl $(CFLAGS) /O2 /Fo$@ %s 58 | 59 | obj\irc.obj: irc.cpp $(HEADERS) 60 | cl $(CFLAGS) /Fo$@ %s 61 | 62 | obj\ui.res: ui.rc rc/bitcoin.ico rc/check.ico rc/send16.bmp rc/send16mask.bmp rc/send16masknoshadow.bmp rc/send20.bmp rc/send20mask.bmp rc/addressbook16.bmp rc/addressbook16mask.bmp rc/addressbook20.bmp rc/addressbook20mask.bmp 63 | rc $(INCLUDEPATHS) $(WXDEFS) /Fo$@ %s 64 | 65 | 66 | 67 | OBJS=obj\util.obj obj\script.obj obj\db.obj obj\net.obj obj\main.obj obj\market.obj \ 68 | obj\ui.obj obj\uibase.obj obj\sha.obj obj\irc.obj obj\ui.res 69 | 70 | bitcoin.exe: $(OBJS) 71 | -kill /f bitcoin.exe & sleep 1 72 | link /nologo /DEBUG /SUBSYSTEM:WINDOWS /OUT:$@ $(LIBPATHS) $** $(LIBS) 73 | 74 | clean: 75 | -del /Q obj\* 76 | -del *.ilk 77 | -del *.pdb 78 | -------------------------------------------------------------------------------- /bitcoin0.1/src/market.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009 Satoshi Nakamoto 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file license.txt or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #include "headers.h" 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | // 17 | // Global state variables 18 | // 19 | 20 | //// later figure out how these are persisted 21 | map mapMyProducts; 22 | 23 | 24 | 25 | 26 | map mapProducts; 27 | CCriticalSection cs_mapProducts; 28 | 29 | bool AdvertInsert(const CProduct& product) 30 | { 31 | uint256 hash = product.GetHash(); 32 | bool fNew = false; 33 | bool fUpdated = false; 34 | 35 | CRITICAL_BLOCK(cs_mapProducts) 36 | { 37 | // Insert or find existing product 38 | pair::iterator, bool> item = mapProducts.insert(make_pair(hash, product)); 39 | CProduct* pproduct = &(*(item.first)).second; 40 | fNew = item.second; 41 | 42 | // Update if newer 43 | if (product.nSequence > pproduct->nSequence) 44 | { 45 | *pproduct = product; 46 | fUpdated = true; 47 | } 48 | } 49 | 50 | //if (fNew) 51 | // NotifyProductAdded(hash); 52 | //else if (fUpdated) 53 | // NotifyProductUpdated(hash); 54 | 55 | return (fNew || fUpdated); 56 | } 57 | 58 | void AdvertErase(const CProduct& product) 59 | { 60 | uint256 hash = product.GetHash(); 61 | CRITICAL_BLOCK(cs_mapProducts) 62 | mapProducts.erase(hash); 63 | //NotifyProductDeleted(hash); 64 | } 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | template 85 | unsigned int Union(T& v1, T& v2) 86 | { 87 | // v1 = v1 union v2 88 | // v1 and v2 must be sorted 89 | // returns the number of elements added to v1 90 | 91 | ///// need to check that this is equivalent, then delete this comment 92 | //vector vUnion(v1.size() + v2.size()); 93 | //vUnion.erase(set_union(v1.begin(), v1.end(), 94 | // v2.begin(), v2.end(), 95 | // vUnion.begin()), 96 | // vUnion.end()); 97 | 98 | T vUnion; 99 | vUnion.reserve(v1.size() + v2.size()); 100 | set_union(v1.begin(), v1.end(), 101 | v2.begin(), v2.end(), 102 | back_inserter(vUnion)); 103 | unsigned int nAdded = vUnion.size() - v1.size(); 104 | if (nAdded > 0) 105 | v1 = vUnion; 106 | return nAdded; 107 | } 108 | 109 | void CUser::AddAtom(unsigned short nAtom, bool fOrigin) 110 | { 111 | // Ignore duplicates 112 | if (binary_search(vAtomsIn.begin(), vAtomsIn.end(), nAtom) || 113 | find(vAtomsNew.begin(), vAtomsNew.end(), nAtom) != vAtomsNew.end()) 114 | return; 115 | 116 | //// instead of zero atom, should change to free atom that propagates, 117 | //// limited to lower than a certain value like 5 so conflicts quickly 118 | // The zero atom never propagates, 119 | // new atoms always propagate through the user that created them 120 | if (nAtom == 0 || fOrigin) 121 | { 122 | vector vTmp(1, nAtom); 123 | Union(vAtomsIn, vTmp); 124 | if (fOrigin) 125 | vAtomsOut.push_back(nAtom); 126 | return; 127 | } 128 | 129 | vAtomsNew.push_back(nAtom); 130 | 131 | if (vAtomsNew.size() >= nFlowthroughRate || vAtomsOut.empty()) 132 | { 133 | // Select atom to flow through to vAtomsOut 134 | vAtomsOut.push_back(vAtomsNew[GetRand(vAtomsNew.size())]); 135 | 136 | // Merge vAtomsNew into vAtomsIn 137 | sort(vAtomsNew.begin(), vAtomsNew.end()); 138 | Union(vAtomsIn, vAtomsNew); 139 | vAtomsNew.clear(); 140 | } 141 | } 142 | 143 | bool AddAtomsAndPropagate(uint256 hashUserStart, const vector& vAtoms, bool fOrigin) 144 | { 145 | CReviewDB reviewdb; 146 | map > pmapPropagate[2]; 147 | pmapPropagate[0][hashUserStart] = vAtoms; 148 | 149 | for (int side = 0; !pmapPropagate[side].empty(); side = 1 - side) 150 | { 151 | map >& mapFrom = pmapPropagate[side]; 152 | map >& mapTo = pmapPropagate[1 - side]; 153 | 154 | for (map >::iterator mi = mapFrom.begin(); mi != mapFrom.end(); ++mi) 155 | { 156 | const uint256& hashUser = (*mi).first; 157 | const vector& vReceived = (*mi).second; 158 | 159 | ///// this would be a lot easier on the database if it put the new atom at the beginning of the list, 160 | ///// so the change would be right next to the vector size. 161 | 162 | // Read user 163 | CUser user; 164 | reviewdb.ReadUser(hashUser, user); 165 | unsigned int nIn = user.vAtomsIn.size(); 166 | unsigned int nNew = user.vAtomsNew.size(); 167 | unsigned int nOut = user.vAtomsOut.size(); 168 | 169 | // Add atoms received 170 | foreach(unsigned short nAtom, vReceived) 171 | user.AddAtom(nAtom, fOrigin); 172 | fOrigin = false; 173 | 174 | // Don't bother writing to disk if no changes 175 | if (user.vAtomsIn.size() == nIn && user.vAtomsNew.size() == nNew) 176 | continue; 177 | 178 | // Propagate 179 | if (user.vAtomsOut.size() > nOut) 180 | foreach(const uint256& hash, user.vLinksOut) 181 | mapTo[hash].insert(mapTo[hash].end(), user.vAtomsOut.begin() + nOut, user.vAtomsOut.end()); 182 | 183 | // Write back 184 | if (!reviewdb.WriteUser(hashUser, user)) 185 | return false; 186 | } 187 | mapFrom.clear(); 188 | } 189 | return true; 190 | } 191 | 192 | 193 | 194 | 195 | 196 | 197 | bool CReview::AcceptReview() 198 | { 199 | // Timestamp 200 | nTime = GetTime(); 201 | 202 | // Check signature 203 | if (!CKey::Verify(vchPubKeyFrom, GetSigHash(), vchSig)) 204 | return false; 205 | 206 | CReviewDB reviewdb; 207 | 208 | // Add review text to recipient 209 | vector vReviews; 210 | reviewdb.ReadReviews(hashTo, vReviews); 211 | vReviews.push_back(*this); 212 | if (!reviewdb.WriteReviews(hashTo, vReviews)) 213 | return false; 214 | 215 | // Add link from sender 216 | CUser user; 217 | uint256 hashFrom = Hash(vchPubKeyFrom.begin(), vchPubKeyFrom.end()); 218 | reviewdb.ReadUser(hashFrom, user); 219 | user.vLinksOut.push_back(hashTo); 220 | if (!reviewdb.WriteUser(hashFrom, user)) 221 | return false; 222 | 223 | reviewdb.Close(); 224 | 225 | // Propagate atoms to recipient 226 | vector vZeroAtom(1, 0); 227 | if (!AddAtomsAndPropagate(hashTo, user.vAtomsOut.size() ? user.vAtomsOut : vZeroAtom, false)) 228 | return false; 229 | 230 | return true; 231 | } 232 | 233 | 234 | 235 | 236 | 237 | bool CProduct::CheckSignature() 238 | { 239 | return (CKey::Verify(vchPubKeyFrom, GetSigHash(), vchSig)); 240 | } 241 | 242 | bool CProduct::CheckProduct() 243 | { 244 | if (!CheckSignature()) 245 | return false; 246 | 247 | // Make sure it's a summary product 248 | if (!mapDetails.empty() || !vOrderForm.empty()) 249 | return false; 250 | 251 | // Look up seller's atom count 252 | CReviewDB reviewdb("r"); 253 | CUser user; 254 | reviewdb.ReadUser(GetUserHash(), user); 255 | nAtoms = user.GetAtomCount(); 256 | reviewdb.Close(); 257 | 258 | ////// delme, this is now done by AdvertInsert 259 | //// Store to memory 260 | //CRITICAL_BLOCK(cs_mapProducts) 261 | // mapProducts[GetHash()] = *this; 262 | 263 | return true; 264 | } 265 | -------------------------------------------------------------------------------- /bitcoin0.1/src/market.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009 Satoshi Nakamoto 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file license.txt or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | class CUser; 6 | class CReview; 7 | class CProduct; 8 | 9 | static const unsigned int nFlowthroughRate = 2; 10 | 11 | 12 | 13 | 14 | bool AdvertInsert(const CProduct& product); 15 | void AdvertErase(const CProduct& product); 16 | bool AddAtomsAndPropagate(uint256 hashUserStart, const vector& vAtoms, bool fOrigin); 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | class CUser 26 | { 27 | public: 28 | vector vAtomsIn; 29 | vector vAtomsNew; 30 | vector vAtomsOut; 31 | vector vLinksOut; 32 | 33 | CUser() 34 | { 35 | } 36 | 37 | IMPLEMENT_SERIALIZE 38 | ( 39 | if (!(nType & SER_GETHASH)) 40 | READWRITE(nVersion); 41 | READWRITE(vAtomsIn); 42 | READWRITE(vAtomsNew); 43 | READWRITE(vAtomsOut); 44 | READWRITE(vLinksOut); 45 | ) 46 | 47 | void SetNull() 48 | { 49 | vAtomsIn.clear(); 50 | vAtomsNew.clear(); 51 | vAtomsOut.clear(); 52 | vLinksOut.clear(); 53 | } 54 | 55 | uint256 GetHash() const { return SerializeHash(*this); } 56 | 57 | 58 | int GetAtomCount() const 59 | { 60 | return (vAtomsIn.size() + vAtomsNew.size()); 61 | } 62 | 63 | void AddAtom(unsigned short nAtom, bool fOrigin); 64 | }; 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | class CReview 73 | { 74 | public: 75 | int nVersion; 76 | uint256 hashTo; 77 | map mapValue; 78 | vector vchPubKeyFrom; 79 | vector vchSig; 80 | 81 | // memory only 82 | unsigned int nTime; 83 | int nAtoms; 84 | 85 | 86 | CReview() 87 | { 88 | nVersion = 1; 89 | hashTo = 0; 90 | nTime = 0; 91 | nAtoms = 0; 92 | } 93 | 94 | IMPLEMENT_SERIALIZE 95 | ( 96 | READWRITE(this->nVersion); 97 | nVersion = this->nVersion; 98 | if (!(nType & SER_DISK)) 99 | READWRITE(hashTo); 100 | READWRITE(mapValue); 101 | READWRITE(vchPubKeyFrom); 102 | if (!(nType & SER_GETHASH)) 103 | READWRITE(vchSig); 104 | ) 105 | 106 | uint256 GetHash() const { return SerializeHash(*this); } 107 | uint256 GetSigHash() const { return SerializeHash(*this, SER_GETHASH|SER_SKIPSIG); } 108 | uint256 GetUserHash() const { return Hash(vchPubKeyFrom.begin(), vchPubKeyFrom.end()); } 109 | 110 | 111 | bool AcceptReview(); 112 | }; 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | class CProduct 121 | { 122 | public: 123 | int nVersion; 124 | CAddress addr; 125 | map mapValue; 126 | map mapDetails; 127 | vector > vOrderForm; 128 | unsigned int nSequence; 129 | vector vchPubKeyFrom; 130 | vector vchSig; 131 | 132 | // disk only 133 | int nAtoms; 134 | 135 | // memory only 136 | set setSources; 137 | 138 | CProduct() 139 | { 140 | nVersion = 1; 141 | nAtoms = 0; 142 | nSequence = 0; 143 | } 144 | 145 | IMPLEMENT_SERIALIZE 146 | ( 147 | READWRITE(this->nVersion); 148 | nVersion = this->nVersion; 149 | READWRITE(addr); 150 | READWRITE(mapValue); 151 | if (!(nType & SER_GETHASH)) 152 | { 153 | READWRITE(mapDetails); 154 | READWRITE(vOrderForm); 155 | READWRITE(nSequence); 156 | } 157 | READWRITE(vchPubKeyFrom); 158 | if (!(nType & SER_GETHASH)) 159 | READWRITE(vchSig); 160 | if (nType & SER_DISK) 161 | READWRITE(nAtoms); 162 | ) 163 | 164 | uint256 GetHash() const { return SerializeHash(*this); } 165 | uint256 GetSigHash() const { return SerializeHash(*this, SER_GETHASH|SER_SKIPSIG); } 166 | uint256 GetUserHash() const { return Hash(vchPubKeyFrom.begin(), vchPubKeyFrom.end()); } 167 | 168 | 169 | bool CheckSignature(); 170 | bool CheckProduct(); 171 | }; 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | extern map mapProducts; 181 | extern CCriticalSection cs_mapProducts; 182 | extern map mapMyProducts; 183 | -------------------------------------------------------------------------------- /bitcoin0.1/src/rc/addressbook16.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/src/rc/addressbook16.bmp -------------------------------------------------------------------------------- /bitcoin0.1/src/rc/addressbook16mask.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/src/rc/addressbook16mask.bmp -------------------------------------------------------------------------------- /bitcoin0.1/src/rc/addressbook20.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/src/rc/addressbook20.bmp -------------------------------------------------------------------------------- /bitcoin0.1/src/rc/addressbook20mask.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/src/rc/addressbook20mask.bmp -------------------------------------------------------------------------------- /bitcoin0.1/src/rc/bitcoin.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/src/rc/bitcoin.ico -------------------------------------------------------------------------------- /bitcoin0.1/src/rc/check.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/src/rc/check.ico -------------------------------------------------------------------------------- /bitcoin0.1/src/rc/send16.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/src/rc/send16.bmp -------------------------------------------------------------------------------- /bitcoin0.1/src/rc/send16mask.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/src/rc/send16mask.bmp -------------------------------------------------------------------------------- /bitcoin0.1/src/rc/send16masknoshadow.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/src/rc/send16masknoshadow.bmp -------------------------------------------------------------------------------- /bitcoin0.1/src/rc/send20.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/src/rc/send20.bmp -------------------------------------------------------------------------------- /bitcoin0.1/src/rc/send20mask.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/src/rc/send20mask.bmp -------------------------------------------------------------------------------- /bitcoin0.1/src/readme.txt: -------------------------------------------------------------------------------- 1 | BitCoin v0.01 ALPHA 2 | 3 | Copyright (c) 2009 Satoshi Nakamoto 4 | Distributed under the MIT/X11 software license, see the accompanying 5 | file license.txt or http://www.opensource.org/licenses/mit-license.php. 6 | This product includes software developed by the OpenSSL Project for use in 7 | the OpenSSL Toolkit (http://www.openssl.org/). This product includes 8 | cryptographic software written by Eric Young (eay@cryptsoft.com). 9 | 10 | 11 | Compilers Supported 12 | ------------------- 13 | MinGW GCC (v3.4.5) 14 | Microsoft Visual C++ 6.0 SP6 15 | 16 | 17 | Dependencies 18 | ------------ 19 | Libraries you need to obtain separately to build: 20 | 21 | default path download 22 | wxWidgets \wxWidgets http://www.wxwidgets.org/downloads/ 23 | OpenSSL \OpenSSL http://www.openssl.org/source/ 24 | Berkeley DB \DB http://www.oracle.com/technology/software/products/berkeley-db/index.html 25 | Boost \Boost http://www.boost.org/users/download/ 26 | 27 | Their licenses: 28 | wxWidgets LGPL 2.1 with very liberal exceptions 29 | OpenSSL Old BSD license with the problematic advertising requirement 30 | Berkeley DB New BSD license with additional requirement that linked software must be free open source 31 | Boost MIT-like license 32 | 33 | 34 | OpenSSL 35 | ------- 36 | Bitcoin does not use any encryption. If you want to do a no-everything 37 | build of OpenSSL to exclude encryption routines, a few patches are required. 38 | (OpenSSL v0.9.8h) 39 | 40 | Edit engines\e_gmp.c and put this #ifndef around #include 41 | #ifndef OPENSSL_NO_RSA 42 | #include 43 | #endif 44 | 45 | Add this to crypto\err\err_all.c before the ERR_load_crypto_strings line: 46 | void ERR_load_RSA_strings(void) { } 47 | 48 | Edit ms\mingw32.bat and replace the Configure line's parameters with this 49 | no-everything list. You have to put this in the batch file because batch 50 | files can't handle more than 9 parameters. 51 | perl Configure mingw threads no-rc2 no-rc4 no-rc5 no-idea no-des no-bf no-cast no-aes no-camellia no-seed no-rsa no-dh 52 | 53 | Also REM out the following line in ms\mingw32.bat. The build fails after it's 54 | already finished building libeay32, which is all we care about, but the 55 | failure aborts the script before it runs dllwrap to generate libeay32.dll. 56 | REM if errorlevel 1 goto end 57 | 58 | Build 59 | ms\mingw32.bat 60 | 61 | If you want to use it with MSVC, generate the .lib file 62 | lib /machine:i386 /def:ms\libeay32.def /out:out\libeay32.lib 63 | 64 | 65 | Berkeley DB 66 | ----------- 67 | MinGW with MSYS: 68 | cd \DB\build_unix 69 | sh ../dist/configure --enable-mingw --enable-cxx 70 | make 71 | 72 | 73 | Boost 74 | ----- 75 | You may need Boost version 1.35 to build with MSVC 6.0. I couldn't get 76 | version 1.37 to compile with MSVC 6.0. 77 | -------------------------------------------------------------------------------- /bitcoin0.1/src/sha.h: -------------------------------------------------------------------------------- 1 | // This file is public domain 2 | // SHA routines extracted as a standalone file from: 3 | // Crypto++: a C++ Class Library of Cryptographic Schemes 4 | // Version 5.5.2 (9/24/2007) 5 | // http://www.cryptopp.com 6 | #ifndef CRYPTOPP_SHA_H 7 | #define CRYPTOPP_SHA_H 8 | #include 9 | 10 | namespace CryptoPP 11 | { 12 | 13 | // 14 | // Dependencies 15 | // 16 | 17 | typedef unsigned char byte; 18 | typedef unsigned short word16; 19 | typedef unsigned int word32; 20 | #if defined(_MSC_VER) || defined(__BORLANDC__) 21 | typedef unsigned __int64 word64; 22 | #else 23 | typedef unsigned long long word64; 24 | #endif 25 | 26 | template inline T rotlFixed(T x, unsigned int y) 27 | { 28 | assert(y < sizeof(T)*8); 29 | return T((x<>(sizeof(T)*8-y))); 30 | } 31 | 32 | template inline T rotrFixed(T x, unsigned int y) 33 | { 34 | assert(y < sizeof(T)*8); 35 | return T((x>>y) | (x<<(sizeof(T)*8-y))); 36 | } 37 | 38 | // ************** endian reversal *************** 39 | 40 | #ifdef _MSC_VER 41 | #if _MSC_VER >= 1400 42 | #define CRYPTOPP_FAST_ROTATE(x) 1 43 | #elif _MSC_VER >= 1300 44 | #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32 | (x) == 64) 45 | #else 46 | #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32) 47 | #endif 48 | #elif (defined(__MWERKS__) && TARGET_CPU_PPC) || \ 49 | (defined(__GNUC__) && (defined(_ARCH_PWR2) || defined(_ARCH_PWR) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || defined(_ARCH_COM))) 50 | #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32) 51 | #elif defined(__GNUC__) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86) // depend on GCC's peephole optimization to generate rotate instructions 52 | #define CRYPTOPP_FAST_ROTATE(x) 1 53 | #else 54 | #define CRYPTOPP_FAST_ROTATE(x) 0 55 | #endif 56 | 57 | inline byte ByteReverse(byte value) 58 | { 59 | return value; 60 | } 61 | 62 | inline word16 ByteReverse(word16 value) 63 | { 64 | #ifdef CRYPTOPP_BYTESWAP_AVAILABLE 65 | return bswap_16(value); 66 | #elif defined(_MSC_VER) && _MSC_VER >= 1300 67 | return _byteswap_ushort(value); 68 | #else 69 | return rotlFixed(value, 8U); 70 | #endif 71 | } 72 | 73 | inline word32 ByteReverse(word32 value) 74 | { 75 | #if defined(__GNUC__) 76 | __asm__ ("bswap %0" : "=r" (value) : "0" (value)); 77 | return value; 78 | #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE) 79 | return bswap_32(value); 80 | #elif defined(__MWERKS__) && TARGET_CPU_PPC 81 | return (word32)__lwbrx(&value,0); 82 | #elif _MSC_VER >= 1400 || (_MSC_VER >= 1300 && !defined(_DLL)) 83 | return _byteswap_ulong(value); 84 | #elif CRYPTOPP_FAST_ROTATE(32) 85 | // 5 instructions with rotate instruction, 9 without 86 | return (rotrFixed(value, 8U) & 0xff00ff00) | (rotlFixed(value, 8U) & 0x00ff00ff); 87 | #else 88 | // 6 instructions with rotate instruction, 8 without 89 | value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); 90 | return rotlFixed(value, 16U); 91 | #endif 92 | } 93 | 94 | #ifdef WORD64_AVAILABLE 95 | inline word64 ByteReverse(word64 value) 96 | { 97 | #if defined(__GNUC__) && defined(__x86_64__) 98 | __asm__ ("bswap %0" : "=r" (value) : "0" (value)); 99 | return value; 100 | #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE) 101 | return bswap_64(value); 102 | #elif defined(_MSC_VER) && _MSC_VER >= 1300 103 | return _byteswap_uint64(value); 104 | #elif defined(CRYPTOPP_SLOW_WORD64) 105 | return (word64(ByteReverse(word32(value))) << 32) | ByteReverse(word32(value>>32)); 106 | #else 107 | value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) | ((value & W64LIT(0x00FF00FF00FF00FF)) << 8); 108 | value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) | ((value & W64LIT(0x0000FFFF0000FFFF)) << 16); 109 | return rotlFixed(value, 32U); 110 | #endif 111 | } 112 | #endif 113 | 114 | 115 | // 116 | // SHA 117 | // 118 | 119 | // http://www.weidai.com/scan-mirror/md.html#SHA-1 120 | class SHA1 121 | { 122 | public: 123 | typedef word32 HashWordType; 124 | static void InitState(word32 *state); 125 | static void Transform(word32 *digest, const word32 *data); 126 | static const char * StaticAlgorithmName() {return "SHA-1";} 127 | }; 128 | 129 | typedef SHA1 SHA; // for backwards compatibility 130 | 131 | // implements the SHA-256 standard 132 | class SHA256 133 | { 134 | public: 135 | typedef word32 HashWordType; 136 | static void InitState(word32 *state); 137 | static void Transform(word32 *digest, const word32 *data); 138 | static const char * StaticAlgorithmName() {return "SHA-256";} 139 | }; 140 | 141 | // implements the SHA-224 standard 142 | class SHA224 143 | { 144 | public: 145 | typedef word32 HashWordType; 146 | static void InitState(word32 *state); 147 | static void Transform(word32 *digest, const word32 *data) {SHA256::Transform(digest, data);} 148 | static const char * StaticAlgorithmName() {return "SHA-224";} 149 | }; 150 | 151 | #ifdef WORD64_AVAILABLE 152 | 153 | // implements the SHA-512 standard 154 | class SHA512 155 | { 156 | public: 157 | typedef word64 HashWordType; 158 | static void InitState(word64 *state); 159 | static void Transform(word64 *digest, const word64 *data); 160 | static const char * StaticAlgorithmName() {return "SHA-512";} 161 | }; 162 | 163 | // implements the SHA-384 standard 164 | class SHA384 165 | { 166 | public: 167 | typedef word64 HashWordType; 168 | static void InitState(word64 *state); 169 | static void Transform(word64 *digest, const word64 *data) {SHA512::Transform(digest, data);} 170 | static const char * StaticAlgorithmName() {return "SHA-384";} 171 | }; 172 | 173 | #endif 174 | 175 | } 176 | 177 | #endif 178 | -------------------------------------------------------------------------------- /bitcoin0.1/src/ui.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/bitcoin0.1/src/ui.cpp -------------------------------------------------------------------------------- /bitcoin0.1/src/ui.rc: -------------------------------------------------------------------------------- 1 | bitcoin ICON "rc/bitcoin.ico" 2 | 3 | #include "wx/msw/wx.rc" 4 | 5 | check ICON "rc/check.ico" 6 | send16 BITMAP "rc/send16.bmp" 7 | send16mask BITMAP "rc/send16mask.bmp" 8 | send16masknoshadow BITMAP "rc/send16masknoshadow.bmp" 9 | send20 BITMAP "rc/send20.bmp" 10 | send20mask BITMAP "rc/send20mask.bmp" 11 | addressbook16 BITMAP "rc/addressbook16.bmp" 12 | addressbook16mask BITMAP "rc/addressbook16mask.bmp" 13 | addressbook20 BITMAP "rc/addressbook20.bmp" 14 | addressbook20mask BITMAP "rc/addressbook20mask.bmp" 15 | -------------------------------------------------------------------------------- /posts/p2p.md: -------------------------------------------------------------------------------- 1 | http://p2pfoundation.ning.com/forum/topics/bitcoin-open-source?xg_source=activity 2 | 3 | I've developed a new open source P2P e-cash system called Bitcoin. It's completely decentralized, with no central server or trusted parties, because everything is based on crypto proof instead of trust. Give it a try, or take a look at the screenshots and design paper: 4 | 5 | Download Bitcoin v0.1 at http://www.bitcoin.org 6 | 7 | The root problem with conventional currency is all the trust that's required to make it work. The central bank must be trusted not to debase the currency, but the history of fiat currencies is full of breaches of that trust. Banks must be trusted to hold our money and transfer it electronically, but they lend it out in waves of credit bubbles with barely a fraction in reserve. We have to trust them with our privacy, trust them not to let identity thieves drain our accounts. Their massive overhead costs make micropayments impossible. 8 | 9 | A generation ago, multi-user time-sharing computer systems had a similar problem. Before strong encryption, users had to rely on password protection to secure their files, placing trust in the system administrator to keep their information private. Privacy could always be overridden by the admin based on his judgment call weighing the principle of privacy against other concerns, or at the behest of his superiors. Then strong encryption became available to the masses, and trust was no longer required. Data could be secured in a way that was physically impossible for others to access, no matter for what reason, no matter how good the excuse, no matter what. 10 | 11 | It's time we had the same thing for money. With e-currency based on cryptographic proof, without the need to trust a third party middleman, money can be secure and transactions effortless. 12 | 13 | One of the fundamental building blocks for such a system is digital signatures. A digital coin contains the public key of its owner. To transfer it, the owner signs the coin together with the public key of the next owner. Anyone can check the signatures to verify the chain of ownership. It works well to secure ownership, but leaves one big problem unsolved: double-spending. Any owner could try to re-spend an already spent coin by signing it again to another owner. The usual solution is for a trusted company with a central database to check for double-spending, but that just gets back to the trust model. In its central position, the company can override the users, and the fees needed to support the company make micropayments impractical. 14 | 15 | Bitcoin's solution is to use a peer-to-peer network to check for double-spending. In a nutshell, the network works like a distributed timestamp server, stamping the first transaction to spend a coin. It takes advantage of the nature of information being easy to spread but hard to stifle. For details on how it works, see the design paper at http://www.bitcoin.org/bitcoin.pdf 16 | 17 | The result is a distributed system with no single point of failure. Users hold the crypto keys to their own money and transact directly with each other, with the help of the P2P network to check for double-spending. 18 | 19 | Satoshi Nakamoto 20 | http://www.bitcoin.org 21 | 22 | 23 | Great stuff. 24 | 25 | This is the first real innovation in money since the Bank of England started to issue its promissory notes for gold in the vaults, which then became known as banknotes. 26 | 27 | I believe an open source currency has great potential. A bit like Google becoming the default search engine for many of us. 28 | 29 | ▶ Reply 30 | 31 | Permalink Reply by Sepp Hasslberger on February 14, 2009 at 15:30 32 | Dante, in an email, has mentioned a UK project called Open Coin. It seems to go in a similar direction. 33 | 34 | Could there be synergies with bitcoin? 35 | 36 | http://opencoin.org/ 37 | 38 | ▶ Reply 39 | 40 | Permalink Reply by Satoshi Nakamoto on February 15, 2009 at 16:42 41 | Could be. They're talking about the old Chaumian central mint stuff, but maybe only because that was the only thing available. Maybe they would be interested in going in a new direction. 42 | 43 | A lot of people automatically dismiss e-currency as a lost cause because of all the companies that failed since the 1990's. I hope it's obvious it was only the centrally controlled nature of those systems that doomed them. I think this is the first time we're trying a decentralized, non-trust-based system. 44 | 45 | ▶ Reply 46 | 47 | Permalink Reply by Joerg Baach on February 17, 2009 at 10:42 48 | Hi Satoshi, 49 | 50 | we are actually really talking about the old Chaumian central stuff. That was because a) it was there b) it was patent free (we have to think a bit about the US). I had a read of your paper on the weekend - thanks a lot for doing that work. Interesting read. 51 | 52 | What I did not understand about your system - how would you use it for a currency of any sort? Everybody can create a coin as they like, as far as I understood, so therefore there is no trusted supply of tokens / coins. 53 | Or the other way around: if you don't trust the double spending database, because its a central instance, you surely couldn't trust a central issuer to issue and redeem. How would a currency work otherwise? Would you use it for a mutual credit system in which the transactions are shown online? 54 | 55 | Cheers, 56 | 57 | Joerg 58 | 59 | ▶ Reply 60 | 61 | Permalink Reply by Sepp Hasslberger on February 18, 2009 at 14:41 62 | I have two questions, Satoshi. 63 | 64 | the first one ties in with Joerg's doubts about the trusted supply of tokens/coins. 65 | 66 | As far as I understand, there will be a limit of the total amount of tokens that can be created, and a changing gradient of difficulty in making the tokens, where the elaboration gets more and more difficult with time. Is that correct? 67 | 68 | It is important that there be a limit in the amount of tokens/coins. But it is also important that this limit be adjustable to take account of how many people adopt the system. If the number of users changes with time, it will also be necessary to change the total amount of coins. 69 | 70 | Is there a formula to decide on what should be the total amount of tokens, and if so, what is the formula? 71 | 72 | If there is no formula, who gets to make that decision and based on what criteria will it be made? 73 | 74 | I will keep my second question for later. One thing at a time... 75 | 76 | ▶ Reply 77 | 78 | Permalink Reply by Satoshi Nakamoto on February 18, 2009 at 20:50 79 | It is a global distributed database, with additions to the database by consent of the majority, based on a set of rules they follow: 80 | 81 | - Whenever someone finds proof-of-work to generate a block, they get some new coins 82 | - The proof-of-work difficulty is adjusted every two weeks to target an average of 6 blocks per hour (for the whole network) 83 | - The coins given per block is cut in half every 4 years 84 | 85 | You could say coins are issued by the majority. They are issued in a limited, predetermined amount. 86 | 87 | As an example, if there are 1000 nodes, and 6 get coins each hour, it would likely take a week before you get anything. 88 | 89 | To Sepp's question, indeed there is nobody to act as central bank or federal reserve to adjust the money supply as the population of users grows. That would have required a trusted party to determine the value, because I don't know a way for software to know the real world value of things. If there was some clever way, or if we wanted to trust someone to actively manage the money supply to peg it to something, the rules could have been programmed for that. 90 | 91 | In this sense, it's more typical of a precious metal. Instead of the supply changing to keep the value the same, the supply is predetermined and the value changes. As the number of users grows, the value per coin increases. It has the potential for a positive feedback loop; as users increase, the value goes up, which could attract more users to take advantage of the increasing value. 92 | -------------------------------------------------------------------------------- /posts/paper.md: -------------------------------------------------------------------------------- 1 | https://www.mail-archive.com/cryptography%40metzdowd.com/msg09959.html 2 | 3 | Bitcoin P2P e-cash paper 4 | 5 | Satoshi Nakamoto Sat, 01 Nov 2008 16:16:33 -0700 6 | 7 | I've been working on a new electronic cash system that's fully 8 | peer-to-peer, with no trusted third party. 9 | 10 | 11 | The paper is available at: 12 | http://www.bitcoin.org/bitcoin.pdf 13 | 14 | The main properties: 15 | Double-spending is prevented with a peer-to-peer network. 16 | No mint or other trusted parties. 17 | Participants can be anonymous. 18 | New coins are made from Hashcash style proof-of-work. 19 | The proof-of-work for new coin generation also powers the 20 | network to prevent double-spending. 21 | 22 | Bitcoin: A Peer-to-Peer Electronic Cash System 23 | 24 | Abstract. A purely peer-to-peer version of electronic cash would 25 | allow online payments to be sent directly from one party to another 26 | without the burdens of going through a financial institution. 27 | Digital signatures provide part of the solution, but the main 28 | benefits are lost if a trusted party is still required to prevent 29 | double-spending. We propose a solution to the double-spending 30 | problem using a peer-to-peer network. The network timestamps 31 | transactions by hashing them into an ongoing chain of hash-based 32 | proof-of-work, forming a record that cannot be changed without 33 | redoing the proof-of-work. The longest chain not only serves as 34 | proof of the sequence of events witnessed, but proof that it came 35 | from the largest pool of CPU power. As long as honest nodes control 36 | the most CPU power on the network, they can generate the longest 37 | chain and outpace any attackers. The network itself requires 38 | minimal structure. Messages are broadcasted on a best effort basis, 39 | and nodes can leave and rejoin the network at will, accepting the 40 | longest proof-of-work chain as proof of what happened while they 41 | were gone. 42 | 43 | Full paper at: 44 | http://www.bitcoin.org/bitcoin.pdf 45 | 46 | Satoshi Nakamoto 47 | -------------------------------------------------------------------------------- /posts/release.md: -------------------------------------------------------------------------------- 1 | Bitcoin v0.1 released 2 | 3 | Satoshi Nakamoto Fri, 09 Jan 2009 17:05:49 -0800 4 | 5 | Announcing the first release of Bitcoin, a new electronic cash 6 | system that uses a peer-to-peer network to prevent double-spending. 7 | It's completely decentralized with no server or central authority. 8 | 9 | 10 | See bitcoin.org for screenshots. 11 | 12 | Download link: 13 | http://downloads.sourceforge.net/bitcoin/bitcoin-0.1.0.rar 14 | 15 | Windows only for now. Open source C++ code is included. 16 | 17 | - Unpack the files into a directory 18 | - Run BITCOIN.EXE 19 | - It automatically connects to other nodes 20 | 21 | If you can keep a node running that accepts incoming connections, 22 | you'll really be helping the network a lot. Port 8333 on your 23 | firewall needs to be open to receive incoming connections. 24 | 25 | The software is still alpha and experimental. There's no guarantee 26 | the system's state won't have to be restarted at some point if it 27 | becomes necessary, although I've done everything I can to build in 28 | extensibility and versioning. 29 | 30 | You can get coins by getting someone to send you some, or turn on 31 | Options->Generate Coins to run a node and generate blocks. I made 32 | the proof-of-work difficulty ridiculously easy to start with, so 33 | for a little while in the beginning a typical PC will be able to 34 | generate coins in just a few hours. It'll get a lot harder when 35 | competition makes the automatic adjustment drive up the difficulty. 36 | Generated coins must wait 120 blocks to mature before they can be 37 | spent. 38 | 39 | There are two ways to send money. If the recipient is online, you 40 | can enter their IP address and it will connect, get a new public 41 | key and send the transaction with comments. If the recipient is 42 | not online, it is possible to send to their Bitcoin address, which 43 | is a hash of their public key that they give you. They'll receive 44 | the transaction the next time they connect and get the block it's 45 | in. This method has the disadvantage that no comment information 46 | is sent, and a bit of privacy may be lost if the address is used 47 | multiple times, but it is a useful alternative if both users can't 48 | be online at the same time or the recipient can't receive incoming 49 | connections. 50 | 51 | Total circulation will be 21,000,000 coins. It'll be distributed 52 | to network nodes when they make blocks, with the amount cut in half 53 | every 4 years. 54 | 55 | first 4 years: 10,500,000 coins 56 | next 4 years: 5,250,000 coins 57 | next 4 years: 2,625,000 coins 58 | next 4 years: 1,312,500 coins 59 | etc... 60 | 61 | When that runs out, the system can support transaction fees if 62 | needed. It's based on open market competition, and there will 63 | probably always be nodes willing to process transactions for free. 64 | 65 | Satoshi Nakamoto 66 | 67 | 68 | --------------------------------------------------------------------- 69 | The Cryptography Mailing List 70 | Unsubscribe by sending "unsubscribe cryptography" to majord...@metzdowd.com 71 | 72 | Previous message 73 | View by thread 74 | View by date 75 | Next message 76 | 77 | Bitcoin v0.1 releasedSatoshi Nakamoto 78 | Re: Bitcoin v0.1 released "Hal Finney" 79 | Re: Bitcoin v0.1 released Satoshi Nakamoto 80 | Re: Bitcoin v0.1 released Jonathan Thornburg 81 | Re: Bitcoin v0.1 released "Hal Finney" 82 | Re: Bitcoin v0.1 released Bill Frantz 83 | Re: Bitcoin v0.1 released dan 84 | Re: Bitcoin v0.1 released Satoshi Nakamoto 85 | Re: Proof of Work -> atmospheric carbon John Gilmore 86 | Re: Proof of Work -> atmospheric ca... John Levine 87 | Re: Proof of Work -> atmospheri... Russ Nelson 88 | 89 | 90 | -------------------------------------------------------------------------------- /precursor/bitgold.txt: -------------------------------------------------------------------------------- 1 | Bit Gold 2 | 3 | Bit gold A long time ago I hit upon the idea of bit gold. The problem, in a nutshell, is that our money currently depends on trust in a third party for its value. As many inflationary and hyperinflationary episodes during the 20th century demonstrated, this is not an ideal state of affairs. Similarly, private bank note issue, while it had various advantages as well as disadvantages, similarly depended on a trusted third party. 4 | 5 | Precious metals and collectibles have an unforgeable scarcity due to the costliness of their creation. This once provided money the value of which was largely independent of any trusted third party. Precious metals have problems, however. It's too costly to assay metals repeatedly for common transactions. Thus a trusted third party (usually associated with a tax collector who accepted the coins as payment) was invoked to stamp a standard amount of the metal into a coin. Transporting large values of metal can be a rather insecure affair, as the British found when transporting gold across a U-boat infested Atlantic to Canada during World War I to support their gold standard. What's worse, you can't pay online with metal. 6 | 7 | Thus, it would be very nice if there were a protocol whereby unforgeably costly bits could be created online with minimal dependence on trusted third parties, and then securely stored, transferred, and assayed with similar minimal trust. Bit gold. 8 | 9 | My proposal for bit gold is based on computing a string of bits from a string of challenge bits, using functions called variously "client puzzle function," "proof of work function," or "secure benchmark function.". The resulting string of bits is the proof of work. Where a one-way function is prohibitively difficult to compute backwards, a secure benchmark function ideally comes with a specific cost, measured in compute cycles, to compute backwards. 10 | 11 | Here are the main steps of the bit gold system that I envision: 12 | 13 | (1) A public string of bits, the "challenge string," is created (see step 5). 14 | 15 | (2) Alice on her computer generates the proof of work string from the challenge bits using a benchmark function. 16 | 17 | (3) The proof of work is securely timestamped. This should work in a distributed fashion, with several different timestamp services so that no particular timestamp service need be substantially relied on. 18 | 19 | (4) Alice adds the challenge string and the timestamped proof of work string to a distributed property title registryfor bit gold. Here, too, no single server is substantially relied on to properly operate the registry. 20 | 21 | (5) The last-created string of bit gold provides the challenge bits for the next-created string. 22 | 23 | (6) To verify that Alice is the owner of a particular string of bit gold, Bob checks the unforgeable chain of title in the bit gold title registry. 24 | 25 | (7) To assay the value of a string of bit gold, Bob checks and verifies the challenge bits, the proof of work string, and the timestamp. 26 | 27 | Note that Alice's control over her bit gold does not depend on her sole possession of the bits, but rather on her lead position in the unforgeable chain of title (chain of digital signatures) in the title registry. 28 | 29 | All of this can be automated by software. The main limits to the security of the scheme are how well trust can be distributed in steps (3) and (4), and the problem of machine architecture which will be discussed below. 30 | 31 | Hal Finney has implemented a variant of bit gold called RPOW (Reusable Proofs of Work). This relies on publishing the computer code for the "mint," which runs on a remote tamper-evident computer. The purchaser of of bit gold can then use remote attestation, which Finney calls the transparent server technique, to verify that a particular number of cycles were actually performed. 32 | 33 | The main problem with all these schemes is that proof of work schemes depend on computer architecture, not just an abstract mathematics based on an abstract "compute cycle." (I wrote about this obscurely several years ago.) Thus, it might be possible to be a very low cost producer (by several orders of magnitude) and swamp the market with bit gold. However, since bit gold is timestamped, the time created as well as the mathematical difficulty of the work can be automatically proven. From this, it can usually be inferred what the cost of producing during that time period was. 34 | 35 | Unlike fungible atoms of gold, but as with collector's items, a large supply during a given time period will drive down the value of those particular items. In this respect "bit gold" acts more like collector's items than like gold. However, the match between this ex post market and the auction determining the initial value might create a very substantial profit for the "bit gold miner" who invents and deploys an optimized computer architecture. 36 | 37 | Thus, bit gold will not be fungible based on a simple function of, for example, the length of the string. Instead, to create fungible units dealers will have to combine different-valued pieces of bit gold into larger units of approximately equal value. This is analogous to what many commodity dealers do today to make commodity markets possible. Trust is still distributed because the estimated values of such bundles can be independently verified by many other parties in a largely or entirely automated fashion. 38 | 39 | In summary, all money mankind has ever used has been insecure in one way or another. This insecurity has been manifested in a wide variety of ways, from counterfeiting to theft, but the most pernicious of which has probably been inflation. Bit gold may provide us with a money of unprecedented security from these dangers. The potential for initially hidden supply gluts due to hidden innovations in machine architecture is a potential flaw in bit gold, or at least an imperfection which the initial auctions and ex post exchanges of bit gold will have to address. 40 | 41 | -------------------------------------------------------------------------------- /precursor/bmoney.txt: -------------------------------------------------------------------------------- 1 | I am fascinated by Tim May's crypto-anarchy. Unlike the communities 2 | traditionally associated with the word "anarchy", in a crypto-anarchy the 3 | government is not temporarily destroyed but permanently forbidden and 4 | permanently unnecessary. It's a community where the threat of violence is 5 | impotent because violence is impossible, and violence is impossible 6 | because its participants cannot be linked to their true names or physical 7 | locations. 8 | 9 | Until now it's not clear, even theoretically, how such a community could 10 | operate. A community is defined by the cooperation of its participants, 11 | and efficient cooperation requires a medium of exchange (money) and a way 12 | to enforce contracts. Traditionally these services have been provided by 13 | the government or government sponsored institutions and only to legal 14 | entities. In this article I describe a protocol by which these services 15 | can be provided to and by untraceable entities. 16 | 17 | I will actually describe two protocols. The first one is impractical, 18 | because it makes heavy use of a synchronous and unjammable anonymous 19 | broadcast channel. However it will motivate the second, more practical 20 | protocol. In both cases I will assume the existence of an untraceable 21 | network, where senders and receivers are identified only by digital 22 | pseudonyms (i.e. public keys) and every messages is signed by its sender 23 | and encrypted to its receiver. 24 | 25 | In the first protocol, every participant maintains a (seperate) database 26 | of how much money belongs to each pseudonym. These accounts collectively 27 | define the ownership of money, and how these accounts are updated is the 28 | subject of this protocol. 29 | 30 | 1. The creation of money. Anyone can create money by broadcasting the 31 | solution to a previously unsolved computational problem. The only 32 | conditions are that it must be easy to determine how much computing effort 33 | it took to solve the problem and the solution must otherwise have no 34 | value, either practical or intellectual. The number of monetary units 35 | created is equal to the cost of the computing effort in terms of a 36 | standard basket of commodities. For example if a problem takes 100 hours 37 | to solve on the computer that solves it most economically, and it takes 3 38 | standard baskets to purchase 100 hours of computing time on that computer 39 | on the open market, then upon the broadcast of the solution to that 40 | problem everyone credits the broadcaster's account by 3 units. 41 | 42 | 2. The transfer of money. If Alice (owner of pseudonym K_A) wishes to 43 | transfer X units of money to Bob (owner of pseudonym K_B), she broadcasts 44 | the message "I give X units of money to K_B" signed by K_A. Upon the 45 | broadcast of this message, everyone debits K_A's account by X units and 46 | credits K_B's account by X units, unless this would create a negative 47 | balance in K_A's account in which case the message is ignored. 48 | 49 | 3. The effecting of contracts. A valid contract must include a maximum 50 | reparation in case of default for each participant party to it. It should 51 | also include a party who will perform arbitration should there be a 52 | dispute. All parties to a contract including the arbitrator must broadcast 53 | their signatures of it before it becomes effective. Upon the broadcast of 54 | the contract and all signatures, every participant debits the account of 55 | each party by the amount of his maximum reparation and credits a special 56 | account identified by a secure hash of the contract by the sum the maximum 57 | reparations. The contract becomes effective if the debits succeed for 58 | every party without producing a negative balance, otherwise the contract 59 | is ignored and the accounts are rolled back. A sample contract might look 60 | like this: 61 | 62 | K_A agrees to send K_B the solution to problem P before 0:0:0 1/1/2000. 63 | K_B agrees to pay K_A 100 MU (monetary units) before 0:0:0 1/1/2000. K_C 64 | agrees to perform arbitration in case of dispute. K_A agrees to pay a 65 | maximum of 1000 MU in case of default. K_B agrees to pay a maximum of 200 66 | MU in case of default. K_C agrees to pay a maximum of 500 MU in case of 67 | default. 68 | 69 | 4. The conclusion of contracts. If a contract concludes without dispute, 70 | each party broadcasts a signed message "The contract with SHA-1 hash H 71 | concludes without reparations." or possibly "The contract with SHA-1 hash 72 | H concludes with the following reparations: ..." Upon the broadcast of all 73 | signatures, every participant credits the account of each party by the 74 | amount of his maximum reparation, removes the contract account, then 75 | credits or debits the account of each party according to the reparation 76 | schedule if there is one. 77 | 78 | 5. The enforcement of contracts. If the parties to a contract cannot agree 79 | on an appropriate conclusion even with the help of the arbitrator, each 80 | party broadcasts a suggested reparation/fine schedule and any arguments or 81 | evidence in his favor. Each participant makes a determination as to the 82 | actual reparations and/or fines, and modifies his accounts accordingly. 83 | 84 | In the second protocol, the accounts of who has how much money are kept by 85 | a subset of the participants (called servers from now on) instead of 86 | everyone. These servers are linked by a Usenet-style broadcast channel. 87 | The format of transaction messages broadcasted on this channel remain the 88 | same as in the first protocol, but the affected participants of each 89 | transaction should verify that the message has been received and 90 | successfully processed by a randomly selected subset of the servers. 91 | 92 | Since the servers must be trusted to a degree, some mechanism is needed to 93 | keep them honest. Each server is required to deposit a certain amount of 94 | money in a special account to be used as potential fines or rewards for 95 | proof of misconduct. Also, each server must periodically publish and 96 | commit to its current money creation and money ownership databases. Each 97 | participant should verify that his own account balances are correct and 98 | that the sum of the account balances is not greater than the total amount 99 | of money created. This prevents the servers, even in total collusion, from 100 | permanently and costlessly expanding the money supply. New servers can 101 | also use the published databases to synchronize with existing servers. 102 | 103 | The protocol proposed in this article allows untraceable pseudonymous 104 | entities to cooperate with each other more efficiently, by providing them 105 | with a medium of exchange and a method of enforcing contracts. The 106 | protocol can probably be made more efficient and secure, but I hope this 107 | is a step toward making crypto-anarchy a practical as well as theoretical 108 | possibility. 109 | 110 | ------- 111 | 112 | Appendix A: alternative b-money creation 113 | 114 | One of the more problematic parts in the b-money protocol is money 115 | creation. This part of the protocol requires that all of the account 116 | keepers decide and agree on the cost of particular computations. 117 | Unfortunately because computing technology tends to advance rapidly and 118 | not always publicly, this information may be unavailable, inaccurate, or 119 | outdated, all of which would cause serious problems for the protocol. 120 | 121 | So I propose an alternative money creation subprotocol, in which account 122 | keepers (everyone in the first protocol, or the servers in the second 123 | protocol) instead decide and agree on the amount of b-money to be created 124 | each period, with the cost of creating that money determined by an 125 | auction. Each money creation period is divided up into four phases, as 126 | follows: 127 | 128 | 1. Planning. The account keepers compute and negotiate with each other to 129 | determine an optimal increase in the money supply for the next period. 130 | Whether or not the account keepers can reach a consensus, they each 131 | broadcast their money creation quota and any macroeconomic calculations 132 | done to support the figures. 133 | 134 | 2. Bidding. Anyone who wants to create b-money broadcasts a bid in the 135 | form of where x is the amount of b-money he wants to create, and y 136 | is an unsolved problem from a predetermined problem class. Each problem in 137 | this class should have a nominal cost (in MIPS-years say) which is 138 | publicly agreed on. 139 | 140 | 3. Computation. After seeing the bids, the ones who placed bids in the 141 | bidding phase may now solve the problems in their bids and broadcast the 142 | solutions. 143 | 144 | 4. Money creation. Each account keeper accepts the highest bids (among 145 | those who actually broadcasted solutions) in terms of nominal cost per 146 | unit of b-money created and credits the bidders' accounts accordingly. 147 | -------------------------------------------------------------------------------- /precursor/hashcash.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/precursor/hashcash.pdf -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/precursor/rpow-1.2.0/.DS_Store -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/client/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for RPOW client 2 | 3 | HCDIR = ../../hashcash 4 | 5 | # For swig 6 | # Mac OSX 7 | PYDIR_OSX = /System/Library/Frameworks/Python.framework/Versions/2.3 8 | PERLDIR_OSX = /System/Library/Perl/5.8.1/darwin-thread-multi-2level/CORE 9 | # Linux 10 | PYDIR_LIN = /usr/include/python2.3 11 | PERLDIR_LIN = /usr/lib/perl/5.8.4/CORE 12 | 13 | CFLAGS = -g -c -I. -I../common -I$(HCDIR) 14 | LDFLAGS = -g 15 | 16 | HCLIB = $(HCDIR)/libhashcash.a 17 | CLILIB = rpowclient.a 18 | 19 | CLIOBJS = rpowclient.o cryptchan.o certvalid.o util4758.o b64.o keys.o \ 20 | rpio.o rpowutil.o connio.o gbignum.o 21 | 22 | all: rpowcli 23 | 24 | rpowcli: rpowcli.o $(CLILIB) $(HCLIB) 25 | gcc $(LDFLAGS) -o rpowcli rpowcli.o $(CLILIB) $(HCLIB) -lcrypto 26 | 27 | $(CLILIB): $(CLIOBJS) 28 | ar rcs $(CLILIB) $(CLIOBJS) 29 | 30 | clean: 31 | -rm rpowcli rpowcli.o $(CLIOBJS) $(CLILIB) rpow_wrap.* \ 32 | _rpow.so rpow.so rpow.bundle rpow.py rpow.pyc rpow.pm 33 | 34 | swig_python_osx: 35 | swig -python rpow.i 36 | $(CC) $(CFLAGS) -I$(PYDIR_OSX)/Headers -c rpow_wrap.c 37 | $(CC) -framework Python -bundle -bundle_loader $(PYDIR_OSX)/bin/python \ 38 | rpow_wrap.o $(CLILIB) $(HCLIB) -lcrypto -o _rpow.so 39 | 40 | swig_python_linux: 41 | swig -python rpow.i 42 | $(CC) $(CFLAGS) -I$(PYDIR_LIN) -c rpow_wrap.c 43 | $(CC) -shared rpow_wrap.o $(CLILIB) $(HCLIB) -lcrypto -o _rpow.so 44 | 45 | swig_perl_osx: 46 | swig -perl rpow.i 47 | $(CC) $(CFLAGS) -I$(PERLDIR_OSX) -c rpow_wrap.c 48 | $(CC) -bundle -flat_namespace -L$(PERLDIR_OSX) -lperl \ 49 | rpow_wrap.o $(CLILIB) $(HCLIB) -lcrypto -o rpow.bundle 50 | 51 | swig_perl_linux: 52 | swig -perl rpow.i 53 | $(CC) $(CFLAGS) -D_GNU_SOURCE -I$(PERLDIR_LIN) -c rpow_wrap.c 54 | $(CC) -shared rpow_wrap.o $(CLILIB) $(HCLIB) -lcrypto -o rpow.so 55 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/client/b64.c: -------------------------------------------------------------------------------- 1 | /* 2 | * b64.c 3 | * Base64 encoding and decoding, concise. 4 | * 5 | * Copyright (C) 2004 Hal Finney 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | 32 | static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 33 | 34 | int /* outlen */ 35 | enc64 (char *out, unsigned char *in, int inlen) 36 | { 37 | unsigned char c; 38 | unsigned char pc = 0; 39 | int st = 0; /* counts 0, 2, 4 */ 40 | char *iout = out; 41 | 42 | while (inlen--) 43 | { 44 | c = *in++; 45 | *out++ = cb64[pc | (c >> (2+st))]; 46 | pc = (c << (4-st)) & 0x3f; 47 | if ((st+=2) == 6) 48 | { 49 | *out++ = cb64[pc]; 50 | pc = st = 0; 51 | } 52 | } 53 | if (st > 0) 54 | { 55 | *out++ = cb64[pc]; 56 | *out++ = '='; 57 | if (st == 2) 58 | *out++ = '='; 59 | } 60 | return out - iout; 61 | } 62 | 63 | static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq"; 64 | 65 | int /* outlen */ 66 | dec64 (unsigned char *out, char *in, int inlen) 67 | { 68 | unsigned char c; 69 | unsigned char pc = 0; 70 | int st = 0; /* Counts 0, 2, 4, 6 */ 71 | unsigned char *iout = out; 72 | 73 | while (inlen--) 74 | { 75 | c = (unsigned char)*in++; 76 | c = (c < '+' || c > 'z') ? '$' : cd64[c - '+']; 77 | if( c == '$') 78 | continue; 79 | c = c - 62; 80 | if (st > 0) 81 | *out++ = pc | (c >> (6-st)); 82 | pc = c << (2+st); 83 | if ((st+=2) == 8) 84 | pc = st = 0; 85 | } 86 | /* assert (pc == 0); */ 87 | return out - iout; 88 | } 89 | 90 | #if B64TEST 91 | 92 | /* Test the above */ 93 | #include 94 | #include 95 | 96 | typedef unsigned char uchar; 97 | 98 | int 99 | main (int ac, char **av) 100 | { 101 | int decode = 0; 102 | int ibufsize, ibuflen, obuflen; 103 | uchar *ibuf, *obuf; 104 | 105 | if (ac > 1 && 0==strcmp(av[1], "-d")) 106 | decode = 1; 107 | 108 | ibufsize = 1000; 109 | ibuf = malloc (ibufsize); 110 | ibuflen = 0; 111 | for ( ; ; ) 112 | { 113 | int nr = fread (ibuf+ibuflen, 1, ibufsize-ibuflen, stdin); 114 | ibuflen += nr; 115 | if (ibuflen < ibufsize) 116 | break; 117 | ibuf = realloc (ibuf, 2*ibufsize); 118 | ibufsize *= 2; 119 | } 120 | 121 | if (decode) 122 | { 123 | obuf = malloc (ibuflen); 124 | obuflen = dec64 (obuf, ibuf, ibuflen); 125 | } else { 126 | obuf = malloc (2*obuflen); 127 | obuflen = enc64 (obuf, ibuf, ibuflen); 128 | } 129 | 130 | fwrite (obuf, 1, obuflen, stdout); 131 | if (!decode) 132 | putchar ('\n'); 133 | } 134 | #endif 135 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/client/certvalid.h: -------------------------------------------------------------------------------- 1 | /* 2 | * certvalid.h 3 | * Header file for 4758 certificate chain validation 4 | * 5 | * Copyright (C) 2004 Hal Finney 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | #ifndef CERTVALID_H 31 | #define CERTVALID_H 32 | 33 | #include 34 | #include 35 | 36 | int 37 | certvalidate (RSA **key, int *epochflag, 38 | unsigned char **innerbuf, unsigned long *innerbuflen, 39 | unsigned char *md1, unsigned char *md2, unsigned char *md3, 40 | unsigned char *certbuf, unsigned long certbuflen, FILE *fout); 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/client/cryptchan.c: -------------------------------------------------------------------------------- 1 | /* 2 | * cryptchan.c 3 | * Client side of a secure encrypted channel to the 4758. 4 | * 5 | * Copyright (C) 2004 Hal Finney 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | #include "scc.h" 40 | 41 | #include "util4758.h" 42 | #include "cryptchan.h" 43 | 44 | /* Size of our master secret, matches HMAC input */ 45 | #define SHAINTERNALBYTES 64 46 | 47 | 48 | /* 49 | * Given an RSA key, create a random master secret, encrypt it using 50 | * the key, and put it in the output. Also generate our TDES 51 | * I/O keys and associated values and return those in encdata. 52 | */ 53 | int 54 | encryptmaster (struct encstate *encdata, RSA *rsa, 55 | unsigned char **outbuf, unsigned long *outbuflen) 56 | { 57 | unsigned char masterkeybuf[SHAINTERNALBYTES]; 58 | unsigned char mac[SHABYTES]; 59 | static unsigned char enckeybuf[RSAKEYBYTES]; 60 | 61 | if (RSA_size(rsa) != sizeof(enckeybuf)) 62 | { 63 | fprintf (stderr, "Key size %d not expected\n", RSA_size(rsa)); 64 | return -1; 65 | } 66 | 67 | RAND_bytes (masterkeybuf, sizeof(masterkeybuf)); 68 | 69 | if (RSA_public_encrypt (sizeof(masterkeybuf), masterkeybuf, enckeybuf, rsa, 70 | RSA_PKCS1_OAEP_PADDING) < 0) 71 | { 72 | fprintf (stderr, "RSA encryption failed\n"); 73 | return -1; 74 | } 75 | 76 | *outbuf = enckeybuf; 77 | *outbuflen = sizeof(enckeybuf); 78 | 79 | /* Generate the shared keys */ 80 | memset (encdata, 0, sizeof(*encdata)); 81 | HMAC (EVP_sha1(), masterkeybuf, sizeof(masterkeybuf), "EKI1", 4, mac, NULL); 82 | memcpy (encdata->tdeskeyin, mac, SHABYTES); 83 | HMAC (EVP_sha1(), masterkeybuf, sizeof(masterkeybuf), "EKI2", 4, mac, NULL); 84 | memcpy (encdata->tdeskeyin+SHABYTES, mac, TDESKEYBYTES-SHABYTES); 85 | HMAC (EVP_sha1(), masterkeybuf, sizeof(masterkeybuf), "EKO1", 4, mac, NULL); 86 | memcpy (encdata->tdeskeyout, mac, SHABYTES); 87 | HMAC (EVP_sha1(), masterkeybuf, sizeof(masterkeybuf), "EKO2", 4, mac, NULL); 88 | memcpy (encdata->tdeskeyout+SHABYTES, mac, TDESKEYBYTES-SHABYTES); 89 | HMAC (EVP_sha1(), masterkeybuf, sizeof(masterkeybuf), "MKI1", 4, encdata->hmackeyin, NULL); 90 | HMAC (EVP_sha1(), masterkeybuf, sizeof(masterkeybuf), "MKO1", 4, encdata->hmackeyout, NULL); 91 | 92 | return 0; 93 | } 94 | 95 | 96 | /* Increment a seqno */ 97 | static void 98 | incrementseqno (unsigned char *seqno) 99 | { 100 | int i; 101 | for (i=SEQNOBYTES-1; i>=0; i--) 102 | if (++seqno[i]) 103 | break; 104 | } 105 | 106 | 107 | /* Do a TDES decrypt for coming from the card. This also unpads. */ 108 | int 109 | tdesdecrypt (unsigned char *obuf, unsigned long *outlen, 110 | struct encstate *encdata, unsigned char *ibuf, unsigned long buflen) 111 | { 112 | EVP_CIPHER_CTX ctx; 113 | int outl; 114 | 115 | EVP_CIPHER_CTX_init (&ctx); 116 | EVP_DecryptInit_ex (&ctx, EVP_des_ede3_cbc(), NULL, encdata->tdeskeyin, 117 | ibuf); 118 | EVP_DecryptUpdate (&ctx, obuf, &outl, ibuf+TDESBYTES, buflen-TDESBYTES); 119 | *outlen = outl; 120 | if (EVP_DecryptFinal_ex (&ctx, obuf+outl, &outl) == 0) 121 | { 122 | fprintf (stderr, "Bad format on decrypted data from card\n"); 123 | return -1; 124 | } 125 | EVP_CIPHER_CTX_cleanup (&ctx); 126 | *outlen += outl; 127 | return 0; 128 | } 129 | 130 | 131 | /* Do a TDES encrypt for going to the card. This also pads, so the output 132 | * buffer should be 8 bytes bigger than the input buffer. 133 | */ 134 | int 135 | tdesencrypt (unsigned char *obuf, unsigned long *outlen, 136 | struct encstate *encdata, unsigned char *ibuf, unsigned long buflen) 137 | { 138 | EVP_CIPHER_CTX ctx; 139 | unsigned char iv[TDESBYTES]; 140 | int outl; 141 | 142 | RAND_bytes (iv, TDESBYTES); 143 | memcpy (obuf, iv, TDESBYTES); 144 | EVP_CIPHER_CTX_init (&ctx); 145 | EVP_EncryptInit_ex (&ctx, EVP_des_ede3_cbc(), NULL, encdata->tdeskeyout, 146 | iv); 147 | EVP_EncryptUpdate (&ctx, obuf+TDESBYTES, &outl, ibuf, buflen); 148 | *outlen = outl + TDESBYTES; 149 | if (EVP_EncryptFinal_ex (&ctx, obuf+TDESBYTES+outl, &outl) == 0) 150 | { 151 | fprintf (stderr, "Internal error encrypting data for card\n"); 152 | return -1; 153 | } 154 | EVP_CIPHER_CTX_cleanup (&ctx); 155 | *outlen += outl; 156 | return 0; 157 | } 158 | 159 | /* TDES encrypt the buffer and put in the output */ 160 | int 161 | encryptoutput (struct encstate *encdata, unsigned char *buf, 162 | unsigned long buflen, unsigned char **outbuf, unsigned long *outbuflen) 163 | { 164 | long rc; 165 | HMAC_CTX hmac; 166 | unsigned char *encbuf; 167 | unsigned long encbuflen; 168 | 169 | encbuf = malloc (buflen + 2*TDESBYTES + SHABYTES); 170 | if ((rc = tdesencrypt (encbuf, &encbuflen, encdata, buf, buflen)) < 0) 171 | return rc; 172 | 173 | HMAC_CTX_init (&hmac); 174 | HMAC_Init_ex (&hmac, encdata->hmackeyout, sizeof(encdata->hmackeyout), 175 | EVP_sha1(), NULL); 176 | HMAC_Update (&hmac, encdata->seqnoout, sizeof(encdata->seqnoout)); 177 | HMAC_Update (&hmac, encbuf, encbuflen); 178 | HMAC_Final (&hmac, encbuf+encbuflen, NULL); 179 | HMAC_CTX_cleanup (&hmac); 180 | 181 | encbuflen += SHABYTES; 182 | 183 | incrementseqno (encdata->seqnoout); 184 | 185 | *outbuf = encbuf; 186 | *outbuflen = encbuflen; 187 | return 0; 188 | } 189 | 190 | 191 | 192 | /* TDES decrypt input buffer, return in *buf and *buflen */ 193 | int 194 | decryptinput (unsigned char **buf, unsigned long *buflen, 195 | struct encstate *encdata, unsigned char *inbuf, unsigned long inbuflen) 196 | { 197 | long rc; 198 | HMAC_CTX hmac; 199 | unsigned char *clrbuf; 200 | unsigned long clrbuflen; 201 | unsigned char mac[SHABYTES]; 202 | 203 | *buf = NULL; 204 | *buflen = 0; 205 | 206 | HMAC_CTX_init (&hmac); 207 | HMAC_Init_ex (&hmac, encdata->hmackeyin, sizeof(encdata->hmackeyin), 208 | EVP_sha1(), NULL); 209 | HMAC_Update (&hmac, encdata->seqnoin, sizeof(encdata->seqnoin)); 210 | HMAC_Update (&hmac, inbuf, inbuflen-SHABYTES); 211 | HMAC_Final (&hmac, mac, NULL); 212 | HMAC_CTX_cleanup (&hmac); 213 | 214 | if (memcmp (mac, inbuf+inbuflen-SHABYTES, SHABYTES) != 0) 215 | { 216 | fprintf (stderr, "Invalid MAC on message from card\n"); 217 | return -1; 218 | } 219 | 220 | inbuflen -= SHABYTES; 221 | 222 | clrbuf = malloc (inbuflen); 223 | if ((rc = tdesdecrypt (clrbuf, &clrbuflen, encdata, inbuf, inbuflen)) < 0) 224 | return rc; 225 | 226 | incrementseqno (encdata->seqnoin); 227 | 228 | *buf = clrbuf; 229 | *buflen = clrbuflen; 230 | return 0; 231 | } 232 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/client/cryptchan.h: -------------------------------------------------------------------------------- 1 | /* 2 | * cryptchan.h 3 | * Header file for secure channel to 4758. 4 | * 5 | * Copyright (C) 2004 Hal Finney 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | #ifndef CRYPTCHAN_H 31 | #define CRYPTCHAN_H 32 | 33 | #include 34 | 35 | /* Host side secure channel to IBM 4758 */ 36 | 37 | #define TDESBYTES 8 38 | #define TDESKEYBYTES 24 39 | #define RSAKEYBYTES 128 40 | #define RSAKEYBITS (8*RSAKEYBYTES) 41 | #define SHABYTES 20 42 | #define SEQNOBYTES 8 43 | 44 | struct encstate { 45 | unsigned char tdeskeyin[TDESKEYBYTES]; 46 | unsigned char tdeskeyout[TDESKEYBYTES]; 47 | unsigned char hmackeyin[SHABYTES]; 48 | unsigned char hmackeyout[SHABYTES]; 49 | unsigned char seqnoin[SEQNOBYTES]; 50 | unsigned char seqnoout[SEQNOBYTES]; 51 | int failed; 52 | }; 53 | 54 | 55 | /* Functions for general buffers */ 56 | int encryptmaster (struct encstate *encdata, RSA *rsa, 57 | unsigned char **outbuf, unsigned long *outbuflen); 58 | int decryptinput (unsigned char **buf, unsigned long *buflen, 59 | struct encstate *encdata, unsigned char *inbuf, unsigned long inbuflen); 60 | int encryptoutput (struct encstate *encdata, unsigned char *buf, 61 | unsigned long buflen, unsigned char **outbuf, unsigned long *outbuflen); 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/client/gbignum.c: -------------------------------------------------------------------------------- 1 | /* 2 | * gbignum.c 3 | * Generic bignum module implemented via OpenSSL 4 | * 5 | * Copyright (C) 2004 Hal Finney 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #include 32 | #include "gbignum.h" 33 | 34 | BN_CTX *bnctx; 35 | 36 | BIGNUM gbig_value_zero; 37 | BIGNUM gbig_value_one; 38 | BIGNUM gbig_value_two; 39 | BIGNUM gbig_value_three; 40 | 41 | int 42 | gbig_initialize () 43 | { 44 | bnctx = BN_CTX_new(); 45 | BN_init (&gbig_value_zero); 46 | BN_init (&gbig_value_one); 47 | BN_init (&gbig_value_two); 48 | BN_init (&gbig_value_three); 49 | BN_set_word (&gbig_value_one, 1); 50 | BN_set_word (&gbig_value_two, 2); 51 | BN_set_word (&gbig_value_three, 3); 52 | return 0; 53 | } 54 | 55 | int 56 | gbig_finalize () 57 | { 58 | if (bnctx) 59 | BN_CTX_free (bnctx); 60 | BN_free (&gbig_value_zero); 61 | BN_free (&gbig_value_one); 62 | BN_free (&gbig_value_two); 63 | BN_free (&gbig_value_three); 64 | return 0; 65 | } 66 | 67 | /* Return number of bytes requested on success, less on failure */ 68 | int 69 | gbig_rand_bytes (void *buf, unsigned len) 70 | { 71 | if (RAND_bytes(buf, len) == 1) 72 | return len; 73 | return -1; 74 | } 75 | 76 | 77 | /* Return a random in range min to maxp1 - 1 */ 78 | void 79 | _gbig_rand_range (BIGNUM *gbnr, BIGNUM *gbnmin, BIGNUM *gbnmaxp1) 80 | { 81 | BIGNUM diff; 82 | 83 | BN_init(&diff); 84 | 85 | BN_sub (&diff, gbnmaxp1, gbnmin); 86 | BN_rand_range (gbnr, &diff); 87 | BN_add (gbnr, gbnr, gbnmin); 88 | BN_free (&diff); 89 | } 90 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/client/gbignum.h: -------------------------------------------------------------------------------- 1 | /* 2 | * gbignum.h 3 | * Generic bignum module, wrapper around OpenSSL library. 4 | * 5 | * Copyright (C) 2004 Hal Finney 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #ifndef GBIGNUM_H 32 | #define GBIGNUM_H 33 | 34 | #include 35 | #include 36 | 37 | extern BN_CTX *bnctx; 38 | typedef BIGNUM gbignum; 39 | typedef SHA_CTX gbig_sha1ctx; 40 | 41 | #ifndef SHA1_DIGEST_LENGTH 42 | #define SHA1_DIGEST_LENGTH 20 43 | #endif /* SHA1_DIGEST_LENGTH */ 44 | 45 | extern int gbig_initialize(void); 46 | extern int gbig_finalize(void); 47 | extern int gbig_rand_bytes(void *,unsigned); 48 | extern void _gbig_rand_range (BIGNUM *, BIGNUM *, BIGNUM *); 49 | extern gbignum gbig_value_zero; 50 | extern gbignum gbig_value_one; 51 | extern gbignum gbig_value_two; 52 | extern gbignum gbig_value_three; 53 | 54 | #define gbig_sha1_buf(md,buf,len) \ 55 | SHA1 (buf,len,md) 56 | #define gbig_sha1_init(ctx) \ 57 | SHA1_Init (ctx) 58 | #define gbig_sha1_update(ctx,buf,len) \ 59 | SHA1_Update (ctx, buf, len) 60 | #define gbig_sha1_final(md,ctx) \ 61 | SHA1_Final (md, ctx) 62 | 63 | #define gbig_init(gbna) BN_init(gbna) 64 | #define gbig_free(gbna) BN_free(gbna) 65 | #define gbig_copy(gbnb,gbna) \ 66 | BN_copy(gbnb, gbna) 67 | #define gbig_add(gbnc,gbna,gbnb) \ 68 | BN_add(gbnc,gbna,gbnb) 69 | #define gbig_sub(gbnc,gbna,gbnb) \ 70 | BN_sub(gbnc,gbna,gbnb) 71 | #define gbig_mul(gbnc,gbna,gbnb) \ 72 | BN_mul(gbnc,gbna,gbnb,bnctx) 73 | #define gbig_div(gbnc,gbna,gbnb) \ 74 | BN_div(gbnc,NULL,gbna,gbnb,bnctx) 75 | #define gbig_mod(gbnc,gbna,gbnb) \ 76 | BN_mod(gbnc,gbna,gbnb,bnctx) 77 | #define gbig_div_mod(gbnd,gbnr,gbna,gbnb) \ 78 | BN_div(gbnd,gbnr,gbna,gbnb,bnctx) 79 | #define gbig_mod_add(gbnc,gbna,gbnb,gbnm) \ 80 | BN_mod_add(gbnc,gbna,gbnb,gbnm,bnctx) 81 | #define gbig_mod_sub(gbnc,gbna,gbnb,gbnm) \ 82 | BN_mod_sub(gbnc,gbna,gbnb,gbnm,bnctx) 83 | #define gbig_mod_mul(gbnc,gbna,gbnb,gbnm) \ 84 | BN_mod_mul(gbnc,gbna,gbnb,gbnm,bnctx) 85 | #define gbig_mod_exp(gbnc,gbna,gbnb,gbnm) \ 86 | BN_mod_exp(gbnc,gbna,gbnb,gbnm,bnctx) 87 | #define gbig_mod_inverse(gbnc,gbna,gbnm) \ 88 | BN_mod_inverse(gbnc,gbna,gbnm,bnctx) 89 | #define gbig_gcd(gbnc,gbna,gbnb) \ 90 | BN_gcd(gbnc,gbna,gbnb,bnctx) 91 | 92 | #define gbig_cmp(gbna,gbnb) \ 93 | BN_cmp(gbna,gbnb) 94 | #define gbig_from_word(gbna,n) \ 95 | BN_set_word(gbna,(n)) 96 | #define gbig_to_word(gbna) \ 97 | BN_get_word(gbna) 98 | #define gbig_set_bit(gbna,bit) \ 99 | BN_set_bit(gbna,bit) 100 | #define gbig_clear_bit(gbna,bit) \ 101 | BN_clear_bit(gbna,bit) 102 | #define gbig_shift_left(gbnb,gbna,bit) \ 103 | BN_lshift(gbnb,gbna,bit) 104 | #define gbig_shift_right(gbnb,gbna,bit) \ 105 | BN_rshift(gbnb,gbna,bit) 106 | 107 | #define gbig_to_buf(buf,gbna) \ 108 | BN_bn2bin(gbna,buf) 109 | #define gbig_from_buf(gbna,buf,buflen) \ 110 | BN_bin2bn(buf,buflen,gbna) 111 | #define gbig_buflen(gbna) \ 112 | BN_num_bytes(gbna) 113 | 114 | #define gbig_rand_range(gbnr,gbna,gbnb) \ 115 | _gbig_rand_range(gbnr,gbna,gbnb) 116 | #define gbig_is_prime(gbna) \ 117 | BN_is_prime(gbna,0,NULL,bnctx,NULL) 118 | #define gbig_generate_prime(gbnr,bits) \ 119 | BN_generate_prime (gbnr,bits,0,NULL,NULL,NULL,NULL) 120 | 121 | 122 | 123 | 124 | #endif /* GBIGNUM_H */ 125 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/client/keys.c: -------------------------------------------------------------------------------- 1 | /* 2 | * keys.c 3 | * Key related functions for RPOW package 4 | * 5 | * Copyright (C) 2004 Hal Finney 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #include "rpowcli.h" 37 | 38 | #if defined(_WIN32) 39 | #define ntohl(x) ((((x)>>24)&0xff)|(((x)>>8)&0xff00)| \ 40 | (((x)&0xff00)<<8)|(((x)&0xff)<<24)) 41 | #define htonl ntohl 42 | #endif 43 | 44 | 45 | /* Math functions */ 46 | 47 | 48 | /* I/O functions */ 49 | 50 | /* Compute keyid for pubkey */ 51 | static void 52 | pk_to_keyid (pubkey *key, unsigned char *keyid) 53 | { 54 | gbig_sha1ctx ctx; 55 | unsigned char *p; 56 | int len; 57 | int nlen; 58 | 59 | gbig_sha1_init (&ctx); 60 | 61 | len = gbig_buflen (&key->n); 62 | p = malloc (len); 63 | gbig_to_buf (p, &key->n); 64 | nlen = htonl(len); 65 | gbig_sha1_update (&ctx, &nlen, sizeof(nlen)); 66 | gbig_sha1_update (&ctx, p, len); 67 | 68 | len = gbig_buflen (&key->e); 69 | p = realloc (p, len); 70 | gbig_to_buf (p, &key->e); 71 | nlen = htonl(len); 72 | gbig_sha1_update (&ctx, &nlen, sizeof(nlen)); 73 | gbig_sha1_update (&ctx, p, len); 74 | 75 | gbig_sha1_final (keyid, &ctx); 76 | free (p); 77 | } 78 | 79 | void 80 | pubkey_read (pubkey *key, char *file) 81 | { 82 | rpowio *rpio; 83 | FILE *f = fopen (file, "rb"); 84 | if (f == NULL) 85 | { 86 | fprintf (stderr, "Unable to open file %s for input\n", file); 87 | exit (1); 88 | } 89 | 90 | rpio = rp_new_from_file (f); 91 | 92 | gbig_init (&key->n); 93 | gbig_init (&key->e); 94 | 95 | if (bnread (&key->n, rpio) < 0 96 | || bnread (&key->e, rpio) < 0 97 | || rp_read (rpio, key->cardid, CARDID_LENGTH) != CARDID_LENGTH) 98 | { 99 | fprintf (stderr, "Error reading public key from file %s\n", file); 100 | exit (1); 101 | } 102 | fclose (f); 103 | rp_free (rpio); 104 | 105 | pk_to_keyid (key, key->keyid); 106 | key->state = PUBKEY_STATE_ACTIVE; 107 | } 108 | 109 | void 110 | pubkey_write (pubkey *key, char *file) 111 | { 112 | rpowio *rpio; 113 | FILE *f = fopen (file, "wb"); 114 | if (f == NULL) 115 | { 116 | fprintf (stderr, "Unable to open file %s for output\n", file); 117 | exit (1); 118 | } 119 | 120 | rpio = rp_new_from_file (f); 121 | 122 | if (bnwrite (&key->n, rpio) < 0 123 | || bnwrite (&key->e, rpio) < 0 124 | || rp_write (rpio, key->cardid, CARDID_LENGTH) != CARDID_LENGTH) 125 | { 126 | fprintf (stderr, "Error writing public key to file %s\n", file); 127 | exit (1); 128 | } 129 | fclose (f); 130 | rp_free (rpio); 131 | } 132 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/client/rpio.c: -------------------------------------------------------------------------------- 1 | /* 2 | * rpio.c 3 | * Low level I/O for rpow package 4 | * 5 | * Copyright (C) 2004 Hal Finney 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #include "rpowcli.h" 37 | 38 | #if defined(_WIN32) 39 | #define ntohl(x) ((((x)>>24)&0xff)|(((x)>>8)&0xff00)| \ 40 | (((x)&0xff00)<<8)|(((x)&0xff)<<24)) 41 | #define htonl ntohl 42 | #endif 43 | 44 | 45 | static rpowio * 46 | rp_newx (FILE *f, BIO *bio) 47 | { 48 | rpowio *rp = malloc (sizeof(rpowio)); 49 | rp->f = f; 50 | rp->bio = bio; 51 | return rp; 52 | } 53 | 54 | rpowio * 55 | rp_new_from_file (FILE *f) 56 | { 57 | return rp_newx (f, NULL); 58 | } 59 | 60 | rpowio * 61 | rp_new_from_bio (BIO *bio) 62 | { 63 | return rp_newx (NULL, bio); 64 | } 65 | 66 | rpowio * 67 | rp_new_from_buf (unsigned char *buf, unsigned buflen) 68 | { 69 | BIO *bio = BIO_new(BIO_s_mem()); 70 | rpowio *rpio = rp_new_from_bio (bio); 71 | BIO_write (bio, buf, buflen); 72 | return rpio; 73 | } 74 | 75 | 76 | void 77 | rp_free (rpowio *rp) 78 | { 79 | if (rp->bio) 80 | BIO_free (rp->bio); 81 | free (rp); 82 | } 83 | 84 | int 85 | rp_write (rpowio *rp, void *buf, unsigned len) 86 | { 87 | if (rp->f != NULL) 88 | return fwrite (buf, 1, len, rp->f); 89 | else if (rp->bio != NULL) 90 | return BIO_write (rp->bio, buf, len); 91 | else 92 | assert (0); 93 | } 94 | 95 | int 96 | rp_read (rpowio *rp, void *buf, unsigned len) 97 | { 98 | if (rp->f != NULL) 99 | return fread (buf, 1, len, rp->f); 100 | else if (rp->bio != NULL) 101 | return BIO_read (rp->bio, buf, len); 102 | else 103 | assert (0); 104 | } 105 | 106 | 107 | /* gbignum I/O */ 108 | 109 | int 110 | bnwrite (gbignum *bn, rpowio *rpio) 111 | { 112 | unsigned char *p; 113 | int len; 114 | int nlen; 115 | 116 | len = gbig_buflen (bn); 117 | p = malloc (len); 118 | gbig_to_buf (p, bn); 119 | nlen = htonl(len); 120 | if (rp_write (rpio, &nlen, sizeof(nlen)) != sizeof(nlen) 121 | || rp_write (rpio, p, len) != len) 122 | { 123 | return -1; 124 | } 125 | free (p); 126 | return 0; 127 | } 128 | 129 | int 130 | bnread (gbignum *bn, rpowio *rpio) 131 | { 132 | unsigned char *p; 133 | int len; 134 | 135 | if (rp_read (rpio, &len, 4) != 4) 136 | return -1; 137 | len = ntohl(len); 138 | if (len > 0x1000) 139 | return -1; 140 | 141 | p = malloc (len); 142 | if (p==NULL) 143 | return -1; 144 | if (rp_read (rpio, p, len) != len) 145 | return -1; 146 | gbig_from_buf (bn, p, len); 147 | free (p); 148 | return 0; 149 | } 150 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/client/rpowcli.h: -------------------------------------------------------------------------------- 1 | /* 2 | * rpowcli.h 3 | * Internal header file for reusable proof of work tokens 4 | * 5 | * Copyright (C) 2004 Hal Finney 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #ifndef RPOWCLI_H 32 | #define RPOWCLI_H 33 | 34 | #include "rpow.h" 35 | #include "rpowclient.h" 36 | 37 | /* Default file names */ 38 | #define RPOWDIR ".rpow" 39 | #define SIGNFILE "rpowkey.pub" 40 | #define COMMFILE "commkey.pub" 41 | #define RPOWFILE "rpows.dat" 42 | #define CONFIGFILE "config" 43 | 44 | #define DEFAULTPORT 4902 45 | 46 | 47 | /*typedef unsigned long ulong;*/ 48 | typedef unsigned char uchar; 49 | 50 | 51 | /* "Pending" RPOW, one waiting to be signed by the server */ 52 | /* rpow is in rpowclient.h */ 53 | typedef struct rpowpend { 54 | gbignum rpow; 55 | gbignum rpowhidden; 56 | gbignum invhider; 57 | int value; 58 | uchar id[RPOW_ID_LENGTH + CARDID_LENGTH]; 59 | int idlen; 60 | } rpowpend; 61 | 62 | 63 | /* rpow.c */ 64 | /* rpow functions are declared in rpowclient.h */ 65 | 66 | char * powresource (unsigned char *cardid); 67 | rpowpend *rpowpend_gen (int value, int dohide, pubkey *); 68 | int rpowpend_write (rpowpend *, rpowio *rpio); 69 | rpowpend *rpowpend_read (rpowio *rpio); 70 | rpow * rpowpend_rpow (rpowpend *, pubkey *, rpowio *rpio); 71 | void rpowpend_free (rpowpend *); 72 | 73 | int valuetoexp (gbignum *exp, int value, pubkey *pk); 74 | 75 | #endif /* RPOWCLI_H */ 76 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/client/rpowclient.h: -------------------------------------------------------------------------------- 1 | /* 2 | * rpowclient.h 3 | * External header file for reusable proof of work tokens 4 | * 5 | * Copyright (C) 2004 Hal Finney 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #ifndef RPOWCLIENT_H 32 | #define RPOWCLIENT_H 33 | 34 | #include 35 | 36 | #include "gbignum.h" 37 | 38 | #ifndef KEYID_LENGTH 39 | # define KEYID_LENGTH 20 40 | #endif 41 | #ifndef CARDID_LENGTH 42 | # define CARDID_LENGTH 14 43 | #endif 44 | 45 | /* Public keys, for communication and rpow signature verification */ 46 | typedef struct pubkey { 47 | gbignum n; 48 | gbignum e; 49 | unsigned char keyid[KEYID_LENGTH]; 50 | #define PUBKEY_STATE_SIGNING 1 51 | #define PUBKEY_STATE_ACTIVE 2 52 | #define PUBKEY_STATE_INACTIVE 3 53 | int state; 54 | unsigned char cardid[CARDID_LENGTH]; 55 | } pubkey; 56 | 57 | 58 | /* Reusable proof of work */ 59 | typedef struct rpow { 60 | unsigned char type; 61 | int value; 62 | gbignum bn; 63 | unsigned char keyid[KEYID_LENGTH]; 64 | unsigned char *id; 65 | int idlen; 66 | } rpow; 67 | 68 | /* Generic I/O channel representative */ 69 | typedef struct rpowio { 70 | FILE *f; 71 | BIO *bio; 72 | } rpowio; 73 | 74 | 75 | /* File names for keys */ 76 | extern char *rpowfile; 77 | extern char *signfile; 78 | extern char *commfile; 79 | 80 | /* Host and port for server */ 81 | extern char targethost[256]; 82 | extern int targetport; 83 | 84 | /* Optional SOCKS V5 host and port */ 85 | extern int usesocks; 86 | extern char sockshost[256]; 87 | extern int socksport; 88 | 89 | 90 | /* rpowclient.c */ 91 | 92 | int server_exchange (rpow **rpout, char *target, int port, int nin, rpow **rpin, 93 | int nout, int *outvals, pubkey *signkey); 94 | void initfilenames (void); 95 | 96 | /* connio.c */ 97 | 98 | int getkeys (char *target, int port, int firsttime); 99 | int getstat (char *target, int port, FILE *fout); 100 | int comm4758 (BIO *bio, char *target, int port, pubkey *signkey); 101 | 102 | /* rpio.c */ 103 | 104 | rpowio *rp_new_from_file (FILE *f); 105 | rpowio *rp_new_from_bio (BIO *bio); 106 | rpowio *rp_new_from_buf (unsigned char *buf, unsigned buflen); 107 | void rp_free (rpowio *); 108 | int rp_write (rpowio *, void *, unsigned); 109 | int rp_read (rpowio *, void *, unsigned); 110 | 111 | int bnread( gbignum *bn, rpowio *rpio ); 112 | int bnwrite( gbignum *bn, rpowio *rpio ); 113 | 114 | 115 | /* keys.c */ 116 | 117 | void pubkey_read (pubkey *key, char *file); 118 | void pubkey_write (pubkey *key, char *file); 119 | 120 | /* rpowutil.c */ 121 | 122 | rpow *rpow_gen (int value, unsigned char *cardid); 123 | int rpow_write(rpow *, rpowio *); 124 | rpow *rpow_read (rpowio *rpio); 125 | void rpow_free (rpow *); 126 | rpow *rpow_from_string (char *str); 127 | rpow *rpow_from_buf (unsigned *bytesused, unsigned char *buf, unsigned inlen); 128 | unsigned char *rpow_to_buf (unsigned *outlen, rpow *rp); 129 | char *rpow_to_string (rpow *rp); 130 | rpow * rpow_from_store (int value); 131 | int rpow_to_store (rpow *rp); 132 | int rpow_count (int counts[]); 133 | 134 | unsigned char * hc_to_buffer (char *buf, int *pbuflen); 135 | 136 | #endif /* RPOWCLIENT_H */ 137 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/client/scc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * scc.h 3 | * Definitions for IBM 4758 Secure Crypto Coprocessor data structures 4 | */ 5 | 6 | #ifndef SCC_H 7 | #define SCC_H 8 | 9 | 10 | #if defined(_WIN32) 11 | #pragma pack(push,1) 12 | #define PACK_DATA 13 | #else 14 | #define PACK_DATA __attribute__ ((__packed__)) 15 | #endif 16 | 17 | #define NSEGS 3 18 | #define ADAPTERID_LENGTH 8 19 | #define VPD_LENGTH 256 20 | #define IMAGE_NAME_LENGTH 80 21 | #ifndef SHA1_HASH_LENGTH 22 | #define SHA1_HASH_LENGTH 20 23 | #endif 24 | 25 | #define KEYTYPE_RSA 0x00 26 | #define CERT_IBM_ROOT 0x8000 27 | #define CERT_CLASS_ROOT 0x0001 28 | #define CERT_MB 0x0101 29 | #define CERT_SEG2_SEG3 0x0201 30 | #define CERT_SEG3_CONFIG 0x0301 31 | #define CERT_SEG3_EPOCH 0x0302 32 | 33 | #define SCCLAYERDESC_T 0x54 34 | #define SCCLAYERDESC_VER 0x00 35 | #define SCCHEAD_T 0x56 36 | #define SCCHEAD_VER 0x00 37 | #define SCCBODY_T 0x57 38 | #define SCCBODY_VER 0x00 39 | 40 | 41 | /* SCC current time */ 42 | typedef struct 43 | { 44 | unsigned long ticksperday; 45 | unsigned long totalticks; 46 | unsigned char hundredths; 47 | unsigned char second; 48 | unsigned char minute; 49 | unsigned char hour; 50 | unsigned short year; 51 | unsigned char month; 52 | unsigned char day; 53 | unsigned short tick; 54 | unsigned short tickhertz; 55 | unsigned short daynumber; 56 | unsigned char weekday; 57 | unsigned char tickrate; 58 | } PACK_DATA sccClockTime_t; 59 | 60 | 61 | /* RSA Key */ 62 | /* This is just part of the SCC structure, relating to public keys */ 63 | /* The actual structure is longer */ 64 | typedef struct 65 | { unsigned long type; 66 | unsigned long length; 67 | unsigned long n_BitLength; 68 | unsigned long n_Length; 69 | unsigned long e_Length; 70 | unsigned long unused[8]; 71 | unsigned long n_Offset; 72 | unsigned long e_Offset; 73 | } PACK_DATA sccRSAKey_t; 74 | 75 | 76 | 77 | 78 | typedef struct 79 | { unsigned long off; 80 | unsigned long len; 81 | } PACK_DATA ptr_t; 82 | 83 | typedef struct 84 | { unsigned char name; 85 | unsigned char version; 86 | } PACK_DATA struct_id_t; 87 | 88 | typedef struct 89 | { struct_id_t struct_id; 90 | unsigned short pic_version; 91 | unsigned short rom_version; 92 | unsigned char page1_certified; 93 | unsigned short boot_count_left; 94 | unsigned long boot_count_right; 95 | unsigned char adapterID[ADAPTERID_LENGTH]; 96 | unsigned char vpd[VPD_LENGTH]; 97 | unsigned char init_state; 98 | unsigned char seg2_state; 99 | unsigned char seg3_state; 100 | unsigned short owner2; 101 | unsigned short owner3; 102 | unsigned char active_seg1; 103 | } PACK_DATA rom_status_t; 104 | 105 | 106 | 107 | 108 | typedef struct { 109 | unsigned long creation_boot; 110 | unsigned short name_type; 111 | unsigned short index; 112 | } PACK_DATA sccName_t; 113 | 114 | typedef struct { 115 | struct_id_t struct_id; 116 | short year; 117 | char month; 118 | char day; 119 | char hour; 120 | char minute; 121 | } PACK_DATA sccTime_t; 122 | 123 | typedef struct { 124 | struct_id_t struct_id; 125 | unsigned char padbytes[2]; 126 | unsigned char adapterID[ADAPTERID_LENGTH]; 127 | sccTime_t when_certified; 128 | } PACK_DATA sccDeviceName_t; 129 | 130 | typedef struct { 131 | struct_id_t struct_id; 132 | unsigned char padbytes[2]; 133 | unsigned long epoch_start; 134 | unsigned long config_start; 135 | unsigned long config_count; 136 | } PACK_DATA sccLayerName_t; 137 | 138 | typedef struct { 139 | struct_id_t struct_id; 140 | unsigned char padbyte; 141 | unsigned char layer_number; 142 | unsigned long ownerID; 143 | unsigned char image_name[IMAGE_NAME_LENGTH]; 144 | unsigned long image_revision; 145 | unsigned char image_hash[SHA1_HASH_LENGTH]; 146 | sccLayerName_t layer_name; 147 | } PACK_DATA sccLayerDesc_t; 148 | 149 | typedef struct { 150 | struct_id_t struct_id; 151 | unsigned char padbytes[2]; 152 | rom_status_t rom_status; 153 | ptr_t vSeg_ids[NSEGS]; 154 | long free_space[NSEGS]; 155 | sccLayerName_t layer_names[NSEGS]; 156 | sccDeviceName_t device_name; 157 | } PACK_DATA sccStatus_t; 158 | 159 | /* Certificate header; vData field points to body */ 160 | typedef struct { 161 | struct_id_t struct_id; 162 | unsigned char padbytes[2]; 163 | unsigned long tData; 164 | ptr_t vData; 165 | ptr_t vSig; 166 | unsigned long tSig; 167 | sccName_t cko_name; 168 | unsigned long cko_type; 169 | unsigned long cko_status; 170 | sccName_t parent_name; 171 | } PACK_DATA sccHead_t; 172 | 173 | /* Certificate body */ 174 | typedef struct { 175 | struct_id_t struct_id; 176 | unsigned char padbytes[2]; 177 | unsigned long tPublic; 178 | ptr_t vPublic; 179 | ptr_t vDescA; 180 | ptr_t vDescB; 181 | sccDeviceName_t device_name; 182 | sccName_t cko_name; 183 | unsigned long cko_type; 184 | sccName_t parent_name; 185 | } PACK_DATA sccBody_t; 186 | 187 | typedef struct 188 | { char signature[4]; 189 | unsigned char vpd_length; 190 | unsigned short crc; 191 | char pn_tag[3]; 192 | unsigned char pn_length; 193 | char pn[8]; 194 | char ec_tag[3]; 195 | unsigned char ec_length; 196 | char ec[8]; 197 | char sn_tag[3]; 198 | unsigned char sn_length; 199 | char sn[8]; 200 | char fn_tag[3]; 201 | unsigned char fn_length; 202 | char fn[8]; 203 | char mf_tag[3]; 204 | unsigned char mf_length; 205 | char mf[6]; 206 | char ds_tag[3]; 207 | unsigned char ds_length; 208 | char ds[42]; 209 | char reserved[17]; 210 | } PACK_DATA vpd_t; 211 | 212 | 213 | typedef struct 214 | { 215 | unsigned short id; 216 | unsigned short length; 217 | unsigned char AMCC_EEPROM[128]; 218 | vpd_t VPD; 219 | unsigned char rsvd[7]; 220 | unsigned char POST0Version; 221 | unsigned char POST1Version; 222 | unsigned char MiniBoot0Version; 223 | unsigned char MiniBoot1Version; 224 | unsigned char OS_Name[6]; 225 | unsigned short OS_Version; 226 | unsigned short CPU_Speed; 227 | unsigned char DES_level; 228 | unsigned char RSA_level; 229 | unsigned char hwreserved[2]; 230 | unsigned char HardwareStatus; 231 | unsigned char AdapterID[8]; 232 | unsigned char flashSize; 233 | unsigned char bbramSize; 234 | unsigned long dramSize; 235 | unsigned char reserved[2]; 236 | } PACK_DATA sccAdapterInfo_t; 237 | 238 | 239 | #if defined(_WIN32) 240 | #pragma pack(pop) 241 | #endif 242 | 243 | #endif 244 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/client/util4758.c: -------------------------------------------------------------------------------- 1 | /* 2 | * util4758.c 3 | * General utility functions for dealing with 4758 data. 4 | * 5 | * Copyright (C) 2004 Hal Finney 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #include 32 | #include "util4758.h" 33 | 34 | #if defined(_WIN32) 35 | #define ntohl(x) ((((x)>>24)&0xff)|(((x)>>8)&0xff00)| \ 36 | (((x)&0xff00)<<8)|(((x)&0xff)<<24)) 37 | #define htonl ntohl 38 | #endif 39 | 40 | static union { 41 | int i; 42 | unsigned char uc[4]; 43 | } endtest = {1}; 44 | 45 | #define UTIL4758_BIGENDIAN (endtest.uc[3] == 1) 46 | #define UTIL4758_LITTLEENDIAN (endtest.uc[0] == 1) 47 | #define TESTENDIAN assert (UTIL4758_BIGENDIAN || UTIL4758_LITTLEENDIAN) 48 | 49 | unsigned long 50 | _scctohl(unsigned long x) 51 | { 52 | TESTENDIAN; 53 | if (UTIL4758_LITTLEENDIAN) 54 | return x; 55 | return (((x)>>24)&0xff)|(((x)>>8)&0xff00)|(((x)&0xff00)<<8)|(((x)&0xff)<<24); 56 | } 57 | unsigned long 58 | _htosccl(unsigned long x) 59 | { 60 | TESTENDIAN; 61 | if (UTIL4758_LITTLEENDIAN) 62 | return x; 63 | return (((x)>>24)&0xff)|(((x)>>8)&0xff00)|(((x)&0xff00)<<8)|(((x)&0xff)<<24); 64 | } 65 | unsigned short 66 | _scctohs(unsigned short x) 67 | { 68 | TESTENDIAN; 69 | if (UTIL4758_LITTLEENDIAN) 70 | return x; 71 | return (((x)>>8)&0xff)|(((x)&0xff)<<8); 72 | } 73 | unsigned short 74 | _htosccs(unsigned short x) 75 | { 76 | TESTENDIAN; 77 | if (UTIL4758_LITTLEENDIAN) 78 | return x; 79 | return (((x)>>8)&0xff)|(((x)&0xff)<<8); 80 | } 81 | 82 | /* Convert an RSA public key from 4758 format to openssl format */ 83 | RSA * 84 | rsafrom4758 (sccRSAKey_t *tok) 85 | { 86 | RSA *rsa = RSA_new(); 87 | unsigned off, len; 88 | 89 | off = scctohl(tok->n_Offset); 90 | len = scctohl(tok->n_Length); 91 | rsa->n = BN_bin2bn ((unsigned char *)tok + off, len, NULL); 92 | 93 | off = scctohl(tok->e_Offset); 94 | len = scctohl(tok->e_Length); 95 | rsa->e = BN_bin2bn ((unsigned char *)tok + off, len, NULL); 96 | return rsa; 97 | } 98 | 99 | 100 | /* Extract an RSA key from the buffer the card embeds in the cert chain */ 101 | /* Keys are stored as (n, e) pairs with each bignum preceded by 4 byte len */ 102 | RSA * 103 | rsafrombuf(unsigned char *keybuf, unsigned long keybuflen, int index) 104 | { 105 | RSA *rsa = RSA_new(); 106 | unsigned long kblen; 107 | unsigned len; 108 | 109 | /* Skip to the nth entry */ 110 | keybuf = keyptrfrombuf(&kblen, keybuf, keybuflen, index); 111 | 112 | if (kblen < sizeof(unsigned)) 113 | return NULL; 114 | len = ntohl (*(unsigned *)keybuf); 115 | rsa->n = BN_bin2bn (keybuf+sizeof(unsigned), len, NULL); 116 | keybuf += len + sizeof(unsigned); 117 | kblen -= len + sizeof(unsigned); 118 | if (kblen < sizeof(unsigned)) 119 | return NULL; 120 | len = ntohl (*(unsigned *)keybuf); 121 | rsa->e = BN_bin2bn (keybuf+sizeof(unsigned), len, NULL); 122 | keybuf += len + sizeof(unsigned); 123 | kblen -= len + sizeof(unsigned); 124 | if (kblen < 0) 125 | return NULL; 126 | 127 | return rsa; 128 | } 129 | 130 | /* 131 | * Find pointer to nth RSA key in the buffer the card embeds in the 132 | * cert chain. 133 | * Keys are stored as (n, e) pairs with each bignum preceded by 4 byte len 134 | */ 135 | unsigned char * 136 | keyptrfrombuf(unsigned long *klen, unsigned char *keybuf, 137 | unsigned long keybuflen, int index) 138 | { 139 | int kblen = keybuflen; 140 | unsigned len; 141 | 142 | /* Skip as needed */ 143 | while (index-- > 0) 144 | { 145 | if (kblen < sizeof(unsigned)) 146 | return NULL; 147 | len = ntohl (*(unsigned *)keybuf); 148 | keybuf += len + sizeof(unsigned); 149 | kblen -= len + sizeof(unsigned); 150 | if (kblen < sizeof(unsigned)) 151 | return NULL; 152 | len = ntohl (*(unsigned *)keybuf); 153 | keybuf += len + sizeof(unsigned); 154 | kblen -= len + sizeof(unsigned); 155 | } 156 | 157 | if (klen) 158 | *klen = kblen; 159 | return keybuf; 160 | } 161 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/client/util4758.h: -------------------------------------------------------------------------------- 1 | /* 2 | * util4758.h 3 | * General utility functions for dealing with 4758 data 4 | * 5 | * Copyright (C) 2004 Hal Finney 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #ifndef UTIL4758_H 32 | #define UTIL4758_H 33 | 34 | #include 35 | 36 | #include 37 | #include "scc.h" 38 | 39 | /* Don't know how portable this will be... */ 40 | 41 | #if (defined(__BYTE_ORDER) && (__BYTE_ORDER == __LITTLE_ENDIAN)) || defined(_LITTLE_ENDIAN) 42 | /* The host byte order is the same as 4758 byte order, 43 | so these functions are all just identity. */ 44 | # define scctohl(x) (x) 45 | # define scctohs(x) (x) 46 | # define htosccl(x) (x) 47 | # define htosccs(x) (x) 48 | #else 49 | # if defined(__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN) 50 | # define scctohl(x) bswap_32 (x) 51 | # define scctohs(x) bswap_16 (x) 52 | # define htosccl(x) bswap_32 (x) 53 | # define htosccs(x) bswap_16 (x) 54 | # else 55 | # ifdef _BIG_ENDIAN 56 | # define scctohl(x) ((((x)>>24)&0xff)|(((x)>>8)&0xff00)|(((x)&0xff00)<<8)|(((x)&0xff)<<24)) 57 | # define scctohs(x) ((((x)>>8)&0xff)|(((x)&0xff)<<8)) 58 | # define htosccl(x) scctohl (x) 59 | # define htosccs(x) scctohs (x) 60 | # else 61 | /* Cannot determine endianness, do it as a call */ 62 | extern unsigned long _scctohl(unsigned long x); 63 | extern unsigned long _htosccl(unsigned long x); 64 | extern unsigned short _scctohs(unsigned short x); 65 | extern unsigned short _htosccs(unsigned short x); 66 | # define scctohl(x) _scctohl(x) 67 | # define htosccl(x) _htosccl(x) 68 | # define scctohs(x) _scctohs(x) 69 | # define htosccs(x) _htosccs(x) 70 | # endif 71 | # endif 72 | #endif 73 | 74 | RSA *rsafrom4758 (sccRSAKey_t *tok); 75 | RSA * rsafrombuf(unsigned char *keybuf, unsigned long keybuflen, int index); 76 | unsigned char * keyptrfrombuf(unsigned long *klen, unsigned char *keybuf, 77 | unsigned long keybuflen, int index); 78 | 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/common/commands.h: -------------------------------------------------------------------------------- 1 | /* 2 | * commands.h 3 | * 4 | * Copyright (C) 2004 Hal Finney 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions 9 | * are met: 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 3. The name of the author may not be used to endorse or promote products 16 | * derived from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef COMMANDS_H 31 | #define COMMANDS_H 32 | 33 | /* Initial keygen for the parent node in the family */ 34 | #define CMD_INITKEYGEN 1 35 | 36 | /* Roll over node keys and create new ones */ 37 | #define CMD_ROLLOVER 2 38 | 39 | /* Add a new key from another RPOW node */ 40 | #define CMD_ADDKEY 3 41 | 42 | /* Activate or de-activate another node's key */ 43 | #define CMD_CHANGEKEYSTATE 4 44 | 45 | /* Return this node's certificate chain */ 46 | #define CMD_GETCHAIN 5 47 | 48 | /* The main event, receive rpows and sign rpends */ 49 | #define CMD_SIGN 6 50 | 51 | /* Provide database authentication message as node checks for re-use */ 52 | #define CMD_DBAUTH 7 53 | 54 | /* Return general node status information, memory usage, etc. */ 55 | #define CMD_STAT 8 56 | 57 | /* Clear the low battery latche */ 58 | #define CMD_CLEARLOWBATT 9 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/common/errors.h: -------------------------------------------------------------------------------- 1 | /* 2 | * errors.h 3 | * 4 | * Copyright (C) 2004 Hal Finney 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions 9 | * are met: 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 3. The name of the author may not be used to endorse or promote products 16 | * derived from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef ERRORS_H 31 | #define ERRORS_H 32 | 33 | #define ERR_UNKNOWNCMD (-1) 34 | #define ERR_BADINPUT (-2) 35 | #define ERR_NOMEM (-3) 36 | #define ERR_INVALID (-4) 37 | #define ERR_DBFAILED (-5) 38 | #define ERR_UNINITIALIZED (-6) 39 | 40 | #define ERR_FAILEDOTHER (-20) 41 | #define ERR_FAILEDPUTBUFFER (-21) 42 | #define ERR_FAILEDGETBUFFER (-22) 43 | #define ERR_FAILEDGENERATE (-23) 44 | #define ERR_FAILEDGETCERT (-24) 45 | #define ERR_FAILEDSHA1 (-25) 46 | #define ERR_FAILEDRSADECRYPT (-26) 47 | #define ERR_FAILEDRSAENCRYPT (-27) 48 | #define ERR_FAILEDRSASIGN (-28) 49 | #define ERR_FAILEDTDESDECRYPT (-29) 50 | #define ERR_FAILEDTDESENCRYPT (-30) 51 | #define ERR_FAILEDPPD (-31) 52 | #define ERR_FAILEDOA (-32) 53 | #define ERR_FAILEDBLIND (-33) 54 | 55 | /* Not an error, but a database query */ 56 | #define ERR_DBQUERY (-100) 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/common/rpow.h: -------------------------------------------------------------------------------- 1 | /* 2 | * rpow.h 3 | * 4 | * Copyright (C) 2004 Hal Finney 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions 9 | * are met: 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 3. The name of the author may not be used to endorse or promote products 16 | * derived from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef RPOW_H 31 | #define RPOW_H 32 | 33 | #include "commands.h" 34 | #include "errors.h" 35 | 36 | #define DEFAGENT \ 37 | static sccAgentID_t agentID = {{'\0','\0'}, \ 38 | {'r','e','u','s','a','b','l','e','p','o','w'}, \ 39 | {'\0'}, \ 40 | {'\0'}, \ 41 | {'\0'}} 42 | 43 | 44 | #define UP4(n) ((((n)+3)/4)*4) 45 | 46 | /* Our hashcash resource string, preceded by cardid in hex */ 47 | #define POW_RESOURCE_TAIL ".rpow.net" 48 | 49 | 50 | /* 51 | * Exponent used for all rpow keys; signing keys use consecutive 52 | * primes starting from here. 53 | */ 54 | #define RPOW_EXP 65537 55 | 56 | 57 | #define RPOW_TYPE_RPOW 1 58 | #define RPOW_TYPE_HASHCASH 2 59 | 60 | /* Status codes */ 61 | #define RPOW_STAT_OK 0 62 | #define RPOW_STAT_REUSED 1 63 | #define RPOW_STAT_INVALID 2 64 | #define RPOW_STAT_INSUFFICIENT 3 65 | #define RPOW_STAT_BADTIME 4 66 | #define RPOW_STAT_MISMATCH 5 67 | #define RPOW_STAT_WRONGKEY 6 68 | #define RPOW_STAT_BADFORMAT 7 69 | #define RPOW_STAT_BADRESOURCE 8 70 | #define RPOW_STAT_UNKNOWNKEY 9 71 | #define RPOW_STAT_BADRPEND 10 72 | #define RPOW_STAT_BADCARDID 11 73 | 74 | #define KEYID_LENGTH 20 75 | #define CARDID_LENGTH 14 76 | #define RPOW_ID_LENGTH (20+CARDID_LENGTH) 77 | 78 | #define RPOW_VALUE_MIN 20 79 | #define RPOW_VALUE_MAX 50 80 | #define RPOW_VALUE_COUNT (RPOW_VALUE_MAX-RPOW_VALUE_MIN+1) 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/scc/Makefile: -------------------------------------------------------------------------------- 1 | #********************************************************************** 2 | #* 3 | #* Makefile for 4758 RPOW server program 4 | #* 5 | #********************************************************************** 6 | 7 | # Root directory of IBM Alphaworks toolkit 8 | SCCTK_FS_ROOT=/scctk 9 | 10 | # The compiler 11 | CC=i386-linux-gnu-gcc 12 | 13 | # The linker 14 | link=$(CC) 15 | 16 | 17 | 18 | bld_env=linux/gcc 19 | 20 | cpq_root=$(SCCTK_FS_ROOT) 21 | 22 | # 23 | # Directory macros 24 | # 25 | cpqenv=$(cpq_root)/cpqenv/$(bld_env) 26 | libpath=$(cpq_root)/lib/scc/$(bld_env) 27 | cpqxlt_path=$(cpq_root)/bin/linux 28 | osinc=$(cpq_root)/include/env/linux 29 | 30 | 31 | # The usual libraries 32 | libs1= \ 33 | $(libpath)/clib.a \ 34 | $(libpath)/cstartex.o \ 35 | $(libpath)/cpqlib.a \ 36 | $(libpath)/scclib.a \ 37 | $(libpath)/smlib.a 38 | 39 | # need libs several times for Linux 40 | libs=$(libs1) $(libs1) $(libs1) 41 | 42 | 43 | 44 | # The translator 45 | xlat=$(cpqxlt_path)/cpqxlt 46 | 47 | # specify suffices for MAKE 48 | .SUFFIXES: .c .cpp .o .xld .a 49 | 50 | # Compiler args 51 | #COM_DEF=-D_SCCTK -D_LINUX_ -DPACK_DATA="__attribute__ ((__packed__))" 52 | COM_DEF=-D_SCCTK -D_LINUX_ 53 | com_inc= -I. -I../common -I$(cpqenv) -I$(cpq_root)/include/scc \ 54 | -I$(cpq_root)/include/common -I$(osinc) 55 | # 56 | # The next variable controls whether C compiles produce optimised (the 57 | # second, commented version) or non-optimised (the first, un-commented 58 | # version) code. 59 | # 60 | # linux -o0 is no optimization. -o3 is max optimization 61 | #OPTIMIZE=-o0 62 | OPTIMIZE=-O3 -finline-functions -funroll-loops 63 | 64 | # 65 | com_opts=-c 66 | COM_WARN=-Wall 67 | comopts=$(COM_DEF) $(com_inc) $(com_opts) $(COM_WARN) $(OPTIMIZE) $(COM_USER) 68 | 69 | # Linker args 70 | # 71 | # 72 | #link_opts=-static -nostdlib -Xlinker -e -Xlinker _startup 73 | link_opts=-shared -nostdlib -Xlinker -e -Xlinker startup -Xlinker -Map -Xlinker link.map 74 | linkopts=$(link_opts) $(LINK_USER) 75 | 76 | # Translator args 77 | # 78 | xlatopts=$(XLAT_USER) 79 | 80 | # Inference Rules 81 | # Note - the "@" on the front of these commands is to suppress the 82 | # echo of the command to the screen by the command processor 83 | # 84 | 85 | %.o: %.c 86 | # @echo --------- In Source.c to Target.o---in cpqenvtk.mak----------- 87 | $(CC) $(comopts) -o $*.o $< 88 | 89 | %.xld: %.exe 90 | $(xlat) $< $@ $(xlatopts) 91 | 92 | 93 | # compiler settings 94 | # 95 | # Linux 96 | # -DDEBUG puts program in spin loop 97 | # -gstabs generates debug info 98 | #COM_USER=-fno-builtin -DDEBUG 99 | #COM_USER=-fno-builtin -gstabs 100 | COM_USER=-fno-builtin 101 | 102 | #LINK_USER= 103 | #OPTIMIZE= 104 | 105 | # translator options 106 | # type cpqxlt for options 107 | XLAT_USER= 108 | 109 | #********************************************************************** 110 | #* target dependencies 111 | #********************************************************************** 112 | 113 | OBJS = rpow.o keygen.o cryptchan.o dbverify.o hmac.o gbignum.o \ 114 | rpowsign.o rpowutil.o rpio.o persist.o certvalid.o 115 | 116 | all: rpow.rod 117 | 118 | rpow.exe: $(OBJS) 119 | $(link) $(linkopts) -o rpow.exe $(OBJS) $(libs) 120 | 121 | rpow.rod : rpow.xld rpowin.txt 122 | $(cpq_root)/bin/linux/sccrodsk rpowin.txt $@ 123 | chmod 666 rpow.rod 124 | ./fixrod.pl rpow.rod # Set date to canonical value 125 | 126 | 127 | 128 | #********************************************************************** 129 | # 130 | # MAKE rule to clean out the target directory. 131 | # 132 | clean: 133 | -rm *.o *.exe *.rod *.xld link.map 134 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/scc/cryptchan.h: -------------------------------------------------------------------------------- 1 | /* 2 | * cryptchan.h 3 | * Header file for SCC side of secure crypto channel 4 | */ 5 | 6 | #ifndef CRYPTCHAN_H 7 | #define CRYPTCHAN_H 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "hmac.h" 16 | #include "errors.h" 17 | 18 | #define TDESBYTES 8 19 | #define TDESKEYBYTES 24 20 | #define MAXRSAKEYBYTES 128 21 | #define MAXRSAKEYBITS (8*MAXRSAKEYBYTES) 22 | #define SEQNOBYTES 8 23 | 24 | /* Limit our input size to guard against memory exhaustion */ 25 | #define MAXINPUTLEN 10000 26 | 27 | struct encstate { 28 | unsigned char tdeskeyin[TDESKEYBYTES]; 29 | unsigned char tdeskeyout[TDESKEYBYTES]; 30 | unsigned char hmackeyin[SHABYTES]; 31 | unsigned char hmackeyout[SHABYTES]; 32 | unsigned char seqnoin[SEQNOBYTES]; 33 | unsigned char seqnoout[SEQNOBYTES]; 34 | int failed; 35 | }; 36 | 37 | 38 | int keyfromcert (sccRSAKeyToken_t **pkey, unsigned long *pkeylen, 39 | sccOA_CKO_Name_t *certname); 40 | int decryptmaster (struct encstate *encdata, sccRequestHeader_t *req, 41 | sccRSAKeyToken_t *key, unsigned long keylen, int bufidx); 42 | int encryptoutput (struct encstate *encdata, unsigned char *buf, unsigned long buflen, 43 | sccRequestHeader_t *req, int bufidx); 44 | int decryptinput (unsigned char **buf, unsigned long *buflen, struct encstate *encdata, 45 | sccRequestHeader_t *req, int bufidx); 46 | int tdesdecrypt (unsigned char *obuf, unsigned char *key, unsigned char *ibuf, 47 | unsigned long buflen); 48 | int tdesencrypt (unsigned char *obuf, unsigned char *key, unsigned char *ibuf, 49 | unsigned long buflen); 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/scc/fixrod.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | open (FILE, "+<$ARGV[0]") or die("Unable to open file $ARGV[0]"); 3 | seek (FILE, 9, 0); 4 | # Code for January 1, 2001 5 | $var = "\01\01\01\00\00\00"; 6 | print FILE $var; 7 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/scc/gbignum.h: -------------------------------------------------------------------------------- 1 | /* Generic bignum module for IBM 4758 */ 2 | /* Also includes some crypto functions */ 3 | 4 | #ifndef GBIGNUM_H 5 | #define GBIGNUM_H 6 | 7 | 8 | #include "scc_int.h" 9 | 10 | typedef sccModMath_Int_t gbignum; 11 | struct gbig_sha1ctx { 12 | sccSHA_RB_t sha_rb; 13 | unsigned buflen; 14 | unsigned char buf[64]; 15 | }; 16 | typedef struct gbig_sha1ctx gbig_sha1ctx; 17 | 18 | #ifndef SHA1_DIGEST_LENGTH 19 | #define SHA1_DIGEST_LENGTH 20 20 | #endif /* SHA1_DIGEST_LENGTH */ 21 | 22 | extern int gbig_initialize(void); 23 | extern int gbig_finalize(void); 24 | extern int gbig_rand_bytes(void *,unsigned); 25 | extern gbignum gbig_value_zero; 26 | extern gbignum gbig_value_one; 27 | extern gbignum gbig_value_two; 28 | extern gbignum gbig_value_three; 29 | 30 | extern void gbig_sha1_buf(unsigned char *md, void *buf, unsigned len); 31 | extern void gbig_sha1_init(gbig_sha1ctx *ctx); 32 | extern void gbig_sha1_update(gbig_sha1ctx *ctx, void *buf, unsigned len); 33 | extern void gbig_sha1_final(unsigned char *md, gbig_sha1ctx *ctx); 34 | 35 | extern int gbig_rand_bytes (void *buf, unsigned len); 36 | extern void gbig_init (gbignum *bn); 37 | extern void gbig_free (gbignum *bn); 38 | extern void gbig_copy (gbignum *bnb, gbignum *bna); 39 | extern void gbig_add (gbignum *bnc, gbignum *bna, gbignum *bnb); 40 | extern void gbig_sub (gbignum *bnc, gbignum *bna, gbignum *bnb); 41 | extern void gbig_mul (gbignum *bnc, gbignum *bna, gbignum *bnb); 42 | extern void gbig_div (gbignum *bnc, gbignum *bna, gbignum *bnb); 43 | extern void gbig_mod (gbignum *bnc, gbignum *bna, gbignum *bnb); 44 | extern void gbig_div_mod (gbignum *bnq, gbignum *bnr, gbignum *bna, 45 | gbignum *bnb); 46 | extern void gbig_mod_add (gbignum *bnc, gbignum *bna, gbignum *bnb, 47 | gbignum *bnm); 48 | extern void gbig_mod_sub (gbignum *bnc, gbignum *bna, gbignum *bnb, 49 | gbignum *bnm); 50 | extern void gbig_mod_mul (gbignum *bnc, gbignum *bna, gbignum *bnb, 51 | gbignum *bnm); 52 | extern void gbig_mod_exp (gbignum *bnc, gbignum *bna, gbignum *bnb, 53 | gbignum *bnm); 54 | extern void gbig_mod_inverse (gbignum *bnb, gbignum *bna, gbignum *bnm); 55 | extern int gbig_cmp (gbignum *bna, gbignum *bnb); 56 | extern void gbig_from_word (gbignum *bna, unsigned n); 57 | extern unsigned gbig_to_word (gbignum *bna); 58 | extern void gbig_set_bit (gbignum *bna, unsigned n); 59 | extern void gbig_clear_bit (gbignum *bna, unsigned n); 60 | extern void gbig_to_buf (void *buf, gbignum *bna); 61 | extern void gbig_to_buf_len (void *buf, unsigned len, gbignum *bna); 62 | extern unsigned gbig_buflen (gbignum *bna); 63 | extern void gbig_from_buf (gbignum *bna, void *buf, int buflen); 64 | extern void gbig_rand_range (gbignum *bnr, gbignum *bna, gbignum *bnb); 65 | 66 | 67 | 68 | 69 | #endif /* GBIGNUM_H */ 70 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/scc/hmac.c: -------------------------------------------------------------------------------- 1 | /* HMAC function for IBM 4758 */ 2 | 3 | #include "gbignum.h" 4 | #include "hmac.h" 5 | 6 | void 7 | gbig_hmac_buf (unsigned char *mac, void *key, 8 | unsigned long keylen, void *buf, unsigned long buflen) 9 | { 10 | gbig_sha1ctx ctx; 11 | unsigned char keybuf[SHAINTERNALBYTES]; 12 | unsigned char keyxorbuf[SHAINTERNALBYTES]; 13 | unsigned char md[SHABYTES]; 14 | int i; 15 | 16 | if (keylen > SHAINTERNALBYTES) 17 | { 18 | gbig_sha1_buf (keybuf, key, keylen); 19 | memset (keybuf+SHABYTES, 0, SHAINTERNALBYTES-SHABYTES); 20 | } 21 | else 22 | { 23 | memcpy (keybuf, key, keylen); 24 | memset (keybuf+keylen, 0, SHAINTERNALBYTES-keylen); 25 | } 26 | 27 | gbig_sha1_init (&ctx); 28 | for (i=0; i SHAINTERNALBYTES) 49 | { 50 | gbig_sha1_buf (ctx->key, key, keylen); 51 | memset (ctx->key+SHABYTES, 0, SHAINTERNALBYTES-SHABYTES); 52 | } 53 | else 54 | { 55 | memcpy (ctx->key, key, keylen); 56 | memset (ctx->key+keylen, 0, SHAINTERNALBYTES-keylen); 57 | } 58 | 59 | gbig_sha1_init (&ctx->sha1ctx); 60 | 61 | for (i=0; ikey[i] ^ 0x36; 63 | gbig_sha1_update (&ctx->sha1ctx, keyxorbuf, SHAINTERNALBYTES); 64 | } 65 | 66 | void 67 | gbig_hmac_update(gbig_hmacctx *ctx, void *buf, unsigned long len) 68 | { 69 | gbig_sha1_update (&ctx->sha1ctx, buf, len); 70 | } 71 | 72 | void 73 | gbig_hmac_final(unsigned char *mac, gbig_hmacctx *ctx) 74 | { 75 | unsigned char md[SHABYTES]; 76 | unsigned char keyxorbuf[SHAINTERNALBYTES]; 77 | int i; 78 | 79 | gbig_sha1_final (md, &ctx->sha1ctx); 80 | 81 | gbig_sha1_init (&ctx->sha1ctx); 82 | for (i=0; ikey[i] ^ 0x5c; 84 | gbig_sha1_update (&ctx->sha1ctx, keyxorbuf, SHAINTERNALBYTES); 85 | gbig_sha1_update (&ctx->sha1ctx, md, SHABYTES); 86 | gbig_sha1_final (mac, &ctx->sha1ctx); 87 | } 88 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/scc/hmac.h: -------------------------------------------------------------------------------- 1 | /* hmac.h */ 2 | #ifndef SHA1_H 3 | #define SHA1_H 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "gbignum.h" 10 | #include "errors.h" 11 | 12 | #define SHABYTES 20 13 | #define SHAINTERNALBYTES 64 14 | 15 | typedef struct gbig_hmacctx { 16 | gbig_sha1ctx sha1ctx; 17 | unsigned char key[SHAINTERNALBYTES]; 18 | } gbig_hmacctx; 19 | 20 | 21 | void gbig_hmac_buf (unsigned char *mac, void *key, 22 | unsigned long keylen, void *buf, unsigned long buflen); 23 | void gbig_hmac_init(gbig_hmacctx *ctx, void *key, unsigned long keylen); 24 | void gbig_hmac_update(gbig_hmacctx *ctx, void *buf, unsigned long len); 25 | void gbig_hmac_final(unsigned char *mac, gbig_hmacctx *ctx); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/scc/installed/rpow.rod: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/precursor/rpow-1.2.0/scc/installed/rpow.rod -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/scc/installed/rpow.xld: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjiqq/bitcoinArchive/7c398e20ff7d69d91465cee58c5f8c52117df6b6/precursor/rpow-1.2.0/scc/installed/rpow.xld -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/scc/rpio.c: -------------------------------------------------------------------------------- 1 | /* 2 | * rpio.c 3 | * Low level I/O for rpow package 4 | */ 5 | 6 | #include "rpowscc.h" 7 | 8 | 9 | rpowio * 10 | rp_new () 11 | { 12 | rpowio *rp = malloc (sizeof(rpowio)); 13 | memset (rp, 0, sizeof (rpowio)); 14 | rp->buf = malloc (1); 15 | return rp; 16 | } 17 | 18 | rpowio * 19 | rp_new_from_buf (unsigned char *buf, unsigned len) 20 | { 21 | rpowio *rp = malloc (sizeof(rpowio)); 22 | memset (rp, 0, sizeof (rpowio)); 23 | rp->buf = malloc(len); 24 | if (rp->buf == NULL) 25 | { 26 | free (rp); 27 | return NULL; 28 | } 29 | memcpy (rp->buf, buf, len); 30 | rp->len = len; 31 | return rp; 32 | } 33 | 34 | rpowio * 35 | rp_new_from_malloc_buf (unsigned char *buf, unsigned len) 36 | { 37 | rpowio *rp = malloc (sizeof(rpowio)); 38 | memset (rp, 0, sizeof (rpowio)); 39 | rp->buf = buf; 40 | rp->len = len; 41 | return rp; 42 | } 43 | 44 | unsigned char * 45 | rp_buf (rpowio *rp, unsigned *len) 46 | { 47 | if (len) 48 | *len = rp->off; 49 | return rp->buf; 50 | } 51 | 52 | void 53 | rp_free (rpowio *rp) 54 | { 55 | if (rp->buf) 56 | free (rp->buf); 57 | free (rp); 58 | } 59 | 60 | int 61 | rp_write (rpowio *rp, void *buf, unsigned len) 62 | { 63 | if (rp->off + len > rp->len) 64 | { 65 | rp->buf = realloc (rp->buf, 2*(rp->off + len)); 66 | if (rp->buf == NULL) 67 | return -1; 68 | rp->len = 2*(rp->off + len); 69 | } 70 | memcpy (rp->buf+rp->off, buf, len); 71 | rp->off += len; 72 | return len; 73 | } 74 | 75 | int 76 | rp_read (rpowio *rp, void *buf, unsigned len) 77 | { 78 | int rlen = rp->len - rp->off; 79 | 80 | rlen = (len < rlen) ? len : rlen; 81 | memcpy (buf, rp->buf+rp->off, rlen); 82 | rp->off += rlen; 83 | return rlen; 84 | } 85 | 86 | 87 | /* gbignum I/O */ 88 | 89 | int 90 | bnwrite (gbignum *bn, rpowio *rpio) 91 | { 92 | unsigned char *p; 93 | int len; 94 | int nlen; 95 | 96 | len = gbig_buflen (bn); 97 | p = malloc (len); 98 | gbig_to_buf (p, bn); 99 | nlen = ntohl(len); 100 | if (rp_write (rpio, &nlen, sizeof(nlen)) != sizeof(nlen) 101 | || rp_write (rpio, p, len) != len) 102 | { 103 | free (p); 104 | return -1; 105 | } 106 | free (p); 107 | return 0; 108 | } 109 | 110 | int 111 | bnread (gbignum *bn, rpowio *rpio) 112 | { 113 | unsigned char *p; 114 | unsigned len; 115 | 116 | if (rp_read (rpio, &len, 4) != 4) 117 | return -1; 118 | len = ntohl(len); 119 | if (len > 2048/8) /* Limit size of data we try to read */ 120 | return -1; 121 | 122 | p = malloc (len); 123 | if (p==NULL) 124 | return -1; 125 | if (rp_read (rpio, p, len) != len) 126 | { 127 | free (p); 128 | return -1; 129 | } 130 | gbig_from_buf (bn, p, len); 131 | free (p); 132 | return 0; 133 | } 134 | 135 | int 136 | pubkey_read (pubkey *pk, rpowio *rpio) 137 | { 138 | int rc; 139 | memset (pk, 0, sizeof(pubkey)); 140 | if ((rc = bnread (&pk->n, rpio)) != 0) 141 | return rc; 142 | if ((rc = bnread (&pk->e, rpio)) != 0) 143 | return rc; 144 | if (rp_read (rpio, pk->keyid, KEYID_LENGTH) != KEYID_LENGTH) 145 | return -1; 146 | if (rp_read (rpio, &pk->state, sizeof(pk->state)) != sizeof(pk->state)) 147 | return -1; 148 | if (rp_read (rpio, &pk->fileid, sizeof(pk->fileid)) != sizeof(pk->fileid)) 149 | return -1; 150 | return 0; 151 | } 152 | 153 | int 154 | pubkey_write (pubkey *pk, rpowio *rpio) 155 | { 156 | int rc; 157 | if ((rc = bnwrite (&pk->n, rpio)) != 0) 158 | return rc; 159 | if ((rc = bnwrite (&pk->e, rpio)) != 0) 160 | return rc; 161 | if (rp_write (rpio, pk->keyid, KEYID_LENGTH) != KEYID_LENGTH) 162 | return -1; 163 | if (rp_write (rpio, &pk->state, sizeof(pk->state)) != sizeof(pk->state)) 164 | return -1; 165 | if (rp_write (rpio, &pk->fileid, sizeof(pk->fileid)) != sizeof(pk->fileid)) 166 | return -1; 167 | return 0; 168 | } 169 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/scc/rpowin.txt: -------------------------------------------------------------------------------- 1 | rpow.xld 32768 2 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/scc/rpowscc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * rpowscc.h 3 | * Header file for SCC code of RPOW 4 | */ 5 | 6 | #ifndef SECSCC_H 7 | #define SECSCC_H 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include "rpow.h" 21 | #include "gbignum.h" 22 | #include "hmac.h" 23 | #include "cryptchan.h" 24 | 25 | #define ntohl rswapl 26 | #define htonl rswapl 27 | #define ntohs rswaps 28 | #define htons rswaps 29 | 30 | /* Prefix used for hashing data we store in database */ 31 | #define PREFIXSIZE SHA1_DIGEST_LENGTH 32 | 33 | /* Hash size for data we store in database */ 34 | #define HASHSIZE SHA1_DIGEST_LENGTH 35 | 36 | 37 | typedef struct rpowio { 38 | unsigned char *buf; 39 | unsigned len; 40 | unsigned off; 41 | } rpowio; 42 | 43 | typedef struct pubkey { 44 | gbignum n; 45 | gbignum e; 46 | unsigned char keyid[KEYID_LENGTH]; 47 | #define PUBKEY_STATE_SIGNING 1 48 | #define PUBKEY_STATE_ACTIVE 2 49 | #define PUBKEY_STATE_INACTIVE 3 50 | int state; 51 | int fileid; 52 | } pubkey; 53 | 54 | /* Reusable proof of work */ 55 | typedef struct rpow { 56 | unsigned char type; 57 | int value; 58 | gbignum bn; 59 | unsigned char keyid[KEYID_LENGTH]; 60 | unsigned int fileid; 61 | unsigned char *id; 62 | int idlen; 63 | } rpow; 64 | 65 | /* "Pending" RPOW, one waiting to be signed by the server */ 66 | typedef struct rpowpend { 67 | gbignum rpow; 68 | int value; 69 | } rpowpend; 70 | 71 | /* rpio.c */ 72 | rpowio * rp_new (void); 73 | rpowio * rp_new_from_buf (unsigned char *buf, unsigned len); 74 | rpowio * rp_new_from_malloc_buf (unsigned char *buf, unsigned len); 75 | unsigned char *rp_buf (rpowio *rp, unsigned *len); 76 | void rp_free (rpowio *rp); 77 | int rp_write (rpowio *rp, void *buf, unsigned len); 78 | int rp_read (rpowio *rp, void *buf, unsigned len); 79 | int bnwrite (gbignum *bn, rpowio *rpio); 80 | int bnread (gbignum *bn, rpowio *rpio); 81 | int pubkey_read (pubkey *pk, rpowio *rpio); 82 | int pubkey_write (pubkey *pk, rpowio *rpio); 83 | 84 | /* keygen.c */ 85 | 86 | /* 87 | * Persistent data. 88 | * This data is sensitive but hardly ever changes. 89 | * We store it in flash rom, encrypted with the OA key. 90 | * The tdkey is used to encrypt our rpow signature data we store in 91 | * flash. 92 | */ 93 | struct rpowdata { 94 | unsigned char rsaprefix[2]; 95 | unsigned char tdkey[TDESKEYBYTES]; 96 | unsigned char pad[MAXRSAKEYBYTES-(2+TDESKEYBYTES)]; 97 | } sdata; 98 | 99 | /* 100 | * We store persistent data encrypted in flash, and also two copies in 101 | * DRAM memory. One copy is xored with FF's. Every so often we switch 102 | * copies. This is hoped to prevent memory burn-in. 103 | */ 104 | extern struct persistdata { 105 | sccRSAKeyToken_t commkey; 106 | unsigned char commkeydata[10*MAXRSAKEYBYTES]; 107 | unsigned long commkeylen; 108 | 109 | sccRSAKeyToken_t rpowkey; 110 | unsigned char rpowkeydata[10*MAXRSAKEYBYTES]; 111 | unsigned long rpowkeylen; 112 | unsigned char rpowdpq[2*RPOW_VALUE_COUNT*MAXRSAKEYBYTES/2]; 113 | 114 | unsigned long nprefixes; 115 | unsigned char prefix[PREFIXSIZE]; /* actually nprefixes*PREFIXSIZE */ 116 | } *pdata1, *pdata2, *pdata; 117 | 118 | #define UP8(x) ((((x)+7)/8)*8) 119 | #define PDATALEN(p) UP8(sizeof(struct persistdata) + ((p)->nprefixes-1)*PREFIXSIZE) 120 | 121 | extern unsigned char rpowblind[RPOW_VALUE_COUNT*2*MAXRSAKEYBYTES]; 122 | 123 | /* Pubkey version of our signing key, includes our signing keyid */ 124 | extern pubkey rpowsignpk; 125 | 126 | /* Card ID is unique among all cards; taken from AdapterInfo_t structure */ 127 | extern unsigned char cardid[CARDID_LENGTH]; 128 | 129 | /* Our hashcash resource string, based on cardid, null terminated */ 130 | extern char powresource[]; 131 | 132 | /* Flag values for dokeygen */ 133 | #define KEYGEN_ROLL 0 134 | #define KEYGEN_NEW 1 135 | int dokeygen (sccOA_CKO_Name_t *certname, int size, int fileid, int newflag); 136 | int getcertchain (unsigned char **pcertbuf, unsigned long *pcertbuflen, 137 | sccOA_CKO_Name_t *certname); 138 | int dochain (sccRequestHeader_t *req, sccOA_CKO_Name_t *certname, int bufidx); 139 | void setrpowsignpk (sccRSAKeyToken_t *key); 140 | void blindgenall (void); 141 | 142 | /* rpowsign.c */ 143 | int dosign (sccRequestHeader_t *req, sccOA_CKO_Name_t *certname, 144 | sccRSAKeyToken_t *commkey, unsigned long commkeylen, 145 | sccRSAKeyToken_t *key, unsigned long keylen); 146 | 147 | /* rpowutil.c */ 148 | int issmallprime (int x); 149 | int valuetoexp (gbignum *exp, int value, pubkey *pk); 150 | rpow * rpow_read (rpowio *rpio); 151 | int rpow_write (rpow *rp, rpowio *rpio); 152 | void rpow_free (rpow *rp); 153 | rpowpend * rpowpend_read (rpowio *rpio); 154 | int rpowpend_write (rpowpend *rpend, rpowio *rpio); 155 | void rpowpend_free (rpowpend *rpend); 156 | int rpow_validate (rpow *rp); 157 | 158 | /* persist.c */ 159 | void pk_to_keyid (pubkey *key); 160 | int rebootpubkeys (sccOA_CKO_Name_t *certname); 161 | int initpubkeys (void); 162 | int addpubkey (gbignum *n, int fileid, int status); 163 | int doaddkey (sccRequestHeader_t *req, sccOA_CKO_Name_t *certname); 164 | int dochangekeystate (sccRequestHeader_t *req, sccOA_CKO_Name_t *certname); 165 | int setcardid (sccOA_CKO_Name_t *certname); 166 | pubkey * pk_from_keyid (unsigned char *keyid); 167 | pubkey * pk_from_index (int i); 168 | int savesecrets (sccOA_CKO_Name_t *certname); 169 | int rebootsecrets (sccOA_CKO_Name_t *certname); 170 | void swappdata (void); 171 | 172 | /* dbverify.c */ 173 | int initdb (void); 174 | int newdb (sccOA_CKO_Name_t *certname, int fileid); 175 | int checkdbfileid (int fileid, int newflag); 176 | int rebootdb (sccOA_CKO_Name_t *certname); 177 | int dbresetpow (sccOA_CKO_Name_t *certname); 178 | int testdbandset (int *found, sccRequestHeader_t *req, unsigned char *data, 179 | unsigned long datalen, int fileid); 180 | 181 | /* certvalid.c */ 182 | int certvalidate ( unsigned char **innerbuf, unsigned long *innerbuflen, 183 | unsigned char *certbuf, unsigned long certbuflen, sccOA_CKO_Name_t *certname); 184 | 185 | #endif 186 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/scc/rpowsign.c: -------------------------------------------------------------------------------- 1 | /* 2 | * rpowsign.c 3 | * Perform the signature function for RPOW 4 | */ 5 | 6 | #include "rpowscc.h" 7 | 8 | /* Maximum allowed rpows at a time */ 9 | #define MAXCOUNT 10 10 | 11 | 12 | int privkey_rawsign (gbignum *rslt, gbignum *val, sccRSAKeyToken_t *key, int keylen, int expnum); 13 | 14 | 15 | /* Implement the rpow signature function */ 16 | int 17 | dosign (sccRequestHeader_t *req, sccOA_CKO_Name_t *certname, 18 | sccRSAKeyToken_t *commkey, unsigned long commkeylen, 19 | sccRSAKeyToken_t *key, unsigned long keylen) 20 | { 21 | long rc; 22 | struct encstate encdata; 23 | rpow **rp = NULL; 24 | rpowpend **rpend = NULL; 25 | rpowio *rpio; 26 | gbignum *reply = NULL; 27 | gbignum tmp1; 28 | gbignum invalue; 29 | gbignum outvalue; 30 | int rpicount, rpocount; 31 | int i; 32 | int found; 33 | unsigned char stat; 34 | unsigned char *buf = NULL; 35 | unsigned long buflen; 36 | unsigned char signkeyid[KEYID_LENGTH]; 37 | 38 | gbig_init (&tmp1); 39 | gbig_init (&invalue); 40 | gbig_init (&outvalue); 41 | 42 | /* First do the RSA decryption on input data */ 43 | if ((rc = decryptmaster (&encdata, req, commkey, commkeylen, 0)) < 0) 44 | return rc; 45 | 46 | /* Then the TDES decryption on the rest */ 47 | if ((rc = decryptinput (&buf, &buflen, &encdata, req, 1)) < 0) 48 | return rc; 49 | 50 | stat = RPOW_STAT_BADFORMAT; 51 | 52 | if (buflen == 0) 53 | { 54 | if (buf) 55 | free (buf); 56 | goto input_error1; 57 | } 58 | 59 | /* Create our pointer for reading from this buffer */ 60 | /* buf now belongs to this rpio */ 61 | rpio = rp_new_from_malloc_buf (buf, buflen); 62 | 63 | if (rp_read (rpio, &signkeyid, sizeof(signkeyid)) != sizeof(signkeyid)) 64 | goto input_error; 65 | 66 | if (memcmp (signkeyid, rpowsignpk.keyid, sizeof(signkeyid)) != 0) 67 | { 68 | stat = RPOW_STAT_WRONGKEY; 69 | goto input_error; 70 | } 71 | 72 | if (rp_read (rpio, &rpicount, sizeof(rpicount)) != sizeof(rpicount)) 73 | goto input_error; 74 | rpicount = ntohl(rpicount); 75 | if (rpicount > MAXCOUNT || rpicount <= 0) 76 | goto input_error; 77 | rp = calloc (rpicount * sizeof (rpow *), 1); 78 | if (rp == NULL) 79 | goto input_error; 80 | gbig_from_word (&invalue, 0); 81 | 82 | /* Read and verify the incoming rpows */ 83 | for (i=0; iid, rp[i]->idlen, 93 | rp[i]->fileid)) != 0) 94 | return rc; /* host lied, should not happen */ 95 | if (found) 96 | stat = RPOW_STAT_REUSED; 97 | } 98 | 99 | if (stat != RPOW_STAT_OK) 100 | goto input_error; 101 | stat = RPOW_STAT_BADFORMAT; 102 | gbig_from_word (&tmp1, 0); 103 | gbig_set_bit (&tmp1, rp[i]->value); 104 | gbig_add (&invalue, &invalue, &tmp1); 105 | } 106 | if (rp_read (rpio, &rpocount, sizeof(rpocount)) != sizeof(rpocount)) 107 | goto input_error; 108 | rpocount = ntohl(rpocount); 109 | if (rpocount > MAXCOUNT || rpocount <= 0) 110 | goto input_error; 111 | rpend = calloc (rpocount * sizeof (rpowpend *), 1); 112 | if (rpend == NULL) 113 | goto input_error; 114 | reply = calloc (rpocount * sizeof (gbignum), 1); 115 | if (reply == NULL) 116 | goto input_error; 117 | for (i=0; ivalue); 129 | gbig_add (&outvalue, &outvalue, &tmp1); 130 | } 131 | 132 | /* Make sure the incoming value == outgoing */ 133 | if (gbig_cmp (&invalue, &outvalue) != 0) 134 | { 135 | stat = RPOW_STAT_MISMATCH; 136 | goto input_error; 137 | } 138 | 139 | /* Everything is OK, sign the requested values */ 140 | for (i=0; irpow^d mod n using the CRT */ 143 | if ((rc = privkey_rawsign (&reply[i], &rpend[i]->rpow, key, keylen, 144 | rpend[i]->value-RPOW_VALUE_MIN)) != 0) 145 | { 146 | stat = RPOW_STAT_BADRPEND; 147 | goto input_error; 148 | } 149 | } 150 | 151 | stat = RPOW_STAT_OK; 152 | 153 | input_error: 154 | 155 | /* Prepare to write results to caller */ 156 | rp_free (rpio); 157 | input_error1: 158 | rpio = rp_new (); 159 | 160 | if (rp_write (rpio, &stat, 1) < 0) 161 | { 162 | rc = ERR_NOMEM; 163 | goto done; 164 | } 165 | if (stat == RPOW_STAT_OK) 166 | { 167 | for (i=0; idpOffset, pdata->rpowdpq+(2*expnum)*key->n_Length/2, 232 | key->n_Length/2); 233 | memcpy (ckey+key->dqOffset, pdata->rpowdpq+(2*expnum+1)*key->n_Length/2, 234 | key->n_Length/2); 235 | 236 | /* Copy blinding factors to the key */ 237 | memcpy (ckey+key->r_Offset, rpowblind+(2*expnum)*key->n_Length, 238 | key->n_Length); 239 | memcpy (ckey+key->r1Offset, rpowblind+(2*expnum+1)*key->n_Length, 240 | key->n_Length); 241 | 242 | /* Check for input value of 0 - defense against timing attacks */ 243 | gbig_mod (val, val, &rpowsignpk.n); 244 | if (gbig_cmp (val, &gbig_value_zero) == 0) 245 | { 246 | gbig_free (rslt); 247 | return 0; 248 | } 249 | 250 | /* Put val into buffer */ 251 | len = gbig_buflen (val); 252 | if (len > sizeof (data)) 253 | return ERR_INVALID; 254 | gbig_to_buf_len (data, key->n_Length, val); 255 | 256 | memset (&rb, 0, sizeof(rb)); 257 | rb.options = RSA_PRIVATE | RSA_DECRYPT | RSA_BLIND_NO_UPDATE; 258 | rb.key_token = key; 259 | rb.key_size = keylen; 260 | rb.data_in = data; 261 | rb.data_out = data; 262 | rb.data_size = key->n_BitLength; 263 | if ((rc = sccRSA(&rb)) != 0) 264 | return ERR_FAILEDRSASIGN; 265 | 266 | /* Copy blinding factors out of the key */ 267 | memcpy (rpowblind+(2*expnum)*key->n_Length, ckey+key->r_Offset, 268 | key->n_Length); 269 | memcpy (rpowblind+(2*expnum+1)*key->n_Length, ckey+key->r1Offset, 270 | key->n_Length); 271 | 272 | gbig_from_buf (rslt, data, key->n_Length); 273 | return 0; 274 | } 275 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/scc/rpowutil.c: -------------------------------------------------------------------------------- 1 | /* 2 | * rpowutil.c 3 | * Generate, read and write reusable proof of work tokens 4 | */ 5 | 6 | #include "rpowscc.h" 7 | 8 | /* 9 | * RPOW tokens come in two types. In transit they are preceded by a type 10 | * byte and then a four byte value field, which is the equivalent of the 11 | * hashcash collision size, and must be in the range RPOW_VALUE_MIN to 12 | * RPOW_VALUE_MAX. The hashcash type (type 2) then has a four byte length 13 | * field, and then a version 1 hashcash stamp. The value in the stamp 14 | * should equal the earlier value field. 15 | * 16 | * The reusable type (type 1) then has a 20 byte keyid. This is the hash of 17 | * the public key which issued the token. It then has a 34 byte token id, 18 | * of which the last 14 bytes must match the cardid of this card. Then comes 19 | * a value signed by the public key identified by the keyid. The signed 20 | * value is in a bignum format where it is preceded by a 4-byte byte count. 21 | * The plaintext of that value consists of the 20 byte SHA-1 hash of the 22 | * token id, then the byte 2, then is padded to the width of the signing key's 23 | * modulus modulus. The padding is done by repeatedly SHA-1 hashing what 24 | * we have so far and appending the hash, until we have the width we need 25 | * (the last append just uses the leftmost bytes of the hash). We then 26 | * take that value mod the signing key's modulus. This is what is signed. 27 | */ 28 | 29 | #define RPOW_PK_VAL 2 30 | #define POW_EXPIRYSECONDS 14*86400 31 | #define POW_GRACESECONDS 86400 32 | #define MAX_TOK 530 33 | 34 | 35 | static int rpow_valid_pk (rpow *rp); 36 | static int rpow_valid_pow (rpow *rp); 37 | 38 | 39 | /* Quick and dirty test for primality */ 40 | int 41 | issmallprime (int x) 42 | { 43 | int p; 44 | 45 | if (x != 2 && (x & 1) == 0) 46 | return 0; 47 | for (p=3; p<=x/p ; p+=2) 48 | if (x % p == 0) 49 | return 0; 50 | return 1; 51 | } 52 | 53 | 54 | /* Find the exponent corresponding to the given value */ 55 | /* Exponents are consecutive primes starting with pk->e */ 56 | int 57 | valuetoexp (gbignum *exp, int value, pubkey *pk) 58 | { 59 | static int exptab[RPOW_VALUE_MAX-RPOW_VALUE_MIN+1]; 60 | int i; 61 | 62 | if (exptab[0] == 0) 63 | { 64 | /* First time; fill exptab with consecutive primes */ 65 | exptab[0] = gbig_to_word (&pk->e); 66 | for (i=1; i RPOW_VALUE_MAX) 76 | return -1; 77 | gbig_from_word (exp, exptab[value-RPOW_VALUE_MIN]); 78 | return 0; 79 | } 80 | 81 | 82 | /* Read an rpow value */ 83 | rpow * 84 | rpow_read (rpowio *rpio) 85 | { 86 | rpow *rp = calloc (sizeof(rpow), 1); 87 | int hclen; 88 | int value; 89 | 90 | gbig_init (&rp->bn); 91 | 92 | if (rp_read (rpio, &rp->type, 1) != 1) 93 | goto error; 94 | if (rp_read (rpio, &value, sizeof(value)) != sizeof(value)) 95 | goto error; 96 | rp->value = ntohl (value); 97 | if (rp->value < RPOW_VALUE_MIN || rp->value > RPOW_VALUE_MAX) 98 | goto error; 99 | if (rp->type == RPOW_TYPE_HASHCASH) 100 | { 101 | if (rp_read (rpio, &hclen, sizeof(hclen)) != sizeof(hclen)) 102 | goto error; 103 | rp->idlen = ntohl(hclen); 104 | if (rp->idlen > MAX_TOK) 105 | goto error; 106 | rp->id = malloc (rp->idlen + 1); 107 | if (rp_read (rpio, rp->id, rp->idlen) != rp->idlen) 108 | goto error; 109 | rp->id[rp->idlen] = '\0'; 110 | } 111 | else if (rp->type == RPOW_TYPE_RPOW) 112 | { 113 | if (rp_read (rpio, rp->keyid, KEYID_LENGTH) != KEYID_LENGTH) 114 | goto error; 115 | rp->id = malloc (RPOW_ID_LENGTH); 116 | if (rp_read (rpio, rp->id, RPOW_ID_LENGTH) != RPOW_ID_LENGTH) 117 | goto error; 118 | if (bnread (&rp->bn, rpio) < 0) 119 | goto error; 120 | rp->idlen = RPOW_ID_LENGTH; 121 | } 122 | else 123 | goto error; 124 | return rp; 125 | error: 126 | gbig_free (&rp->bn); 127 | free (rp); 128 | return NULL; 129 | } 130 | 131 | /* Write out an rpow value */ 132 | int 133 | rpow_write (rpow *rp, rpowio *rpio) 134 | { 135 | int value = htonl(rp->value); 136 | if (rp_write (rpio, &rp->type, 1) != 1) 137 | return -1; 138 | if (rp_write (rpio, &value, sizeof(value)) != sizeof(value)) 139 | return -1; 140 | if (rp->type == RPOW_TYPE_HASHCASH) 141 | { 142 | int hclen = htonl(rp->idlen); 143 | if (rp_write (rpio, &hclen, sizeof(hclen)) != sizeof(hclen)) 144 | return -1; 145 | if (rp_write (rpio, rp->id, rp->idlen) != rp->idlen) 146 | return -1; 147 | } else { /* rp->type == RPOW_TYPE_RPOW */ 148 | if (rp_write (rpio, rp->keyid, KEYID_LENGTH) != KEYID_LENGTH) 149 | return -1; 150 | if (rp_write (rpio, rp->id, rp->idlen) != rp->idlen) 151 | return -1; 152 | if (bnwrite (&rp->bn, rpio) < 0) 153 | return -1; 154 | } 155 | return 0; 156 | } 157 | 158 | 159 | /* Free an rpow */ 160 | void 161 | rpow_free (rpow *rp) 162 | { 163 | if (rp->id) 164 | free (rp->id); 165 | gbig_free (&rp->bn); 166 | free (rp); 167 | } 168 | 169 | 170 | /* Generate the rpow field of an rpowpend */ 171 | static void 172 | rpowpend_bn_gen (gbignum *bn, unsigned char *id, unsigned idlen, pubkey *pk) 173 | { 174 | unsigned char md[SHA1_DIGEST_LENGTH]; 175 | unsigned char buf[MAXRSAKEYBYTES]; 176 | int nlen = gbig_buflen (&pk->n); 177 | int off; 178 | 179 | gbig_sha1_buf (buf, id, idlen); 180 | buf[SHA1_DIGEST_LENGTH] = RPOW_PK_VAL; 181 | off = SHA1_DIGEST_LENGTH + 1; 182 | while (off < nlen) 183 | { 184 | gbig_sha1_buf (md, buf, off); 185 | memcpy (buf+off, md, MIN(SHA1_DIGEST_LENGTH, nlen-off)); 186 | off += SHA1_DIGEST_LENGTH; 187 | } 188 | gbig_from_buf (bn, buf, nlen); 189 | gbig_mod (bn, bn, &pk->n); 190 | } 191 | 192 | 193 | /* Read an rpowpend written by rpowpend_write */ 194 | rpowpend * 195 | rpowpend_read (rpowio *rpio) 196 | { 197 | rpowpend *rpend = calloc (sizeof(rpowpend), 1); 198 | int value; 199 | 200 | gbig_init (&rpend->rpow); 201 | 202 | rp_read (rpio, &value, sizeof(value)); 203 | rpend->value = ntohl(value); 204 | if (rpend->value < RPOW_VALUE_MIN || rpend->value > RPOW_VALUE_MAX) 205 | goto error; 206 | if (bnread (&rpend->rpow, rpio) < 0) 207 | goto error; 208 | return rpend; 209 | error: 210 | gbig_free (&rpend->rpow); 211 | free (rpend); 212 | return NULL; 213 | } 214 | 215 | 216 | /* Free an rpowpend */ 217 | void 218 | rpowpend_free (rpowpend *rpend) 219 | { 220 | gbig_free (&rpend->rpow); 221 | free (rpend); 222 | } 223 | 224 | 225 | /* 226 | * Validate a POW or RPOW token. As a side effect, set the fileid. 227 | */ 228 | int 229 | rpow_validate (rpow *rp) 230 | { 231 | if (rp->type == RPOW_TYPE_HASHCASH) 232 | return rpow_valid_pow (rp); 233 | else 234 | return rpow_valid_pk (rp); 235 | } 236 | 237 | /* Given a POW token (hashcash version 1), parse out the fields */ 238 | /* Example: 1:15:040719:rpow.net::9e6c82f8e4727a6d:1ec4 */ 239 | /* The pointers returned are pointers into the input str */ 240 | /* str does not have to be null terminated */ 241 | /* Return error if no good */ 242 | #define MAXFIELDS 6 243 | static int 244 | pow_parse (const char *str, int len, int *pvalue, time_t *ptime, 245 | struct tm *ptm, char **presource, char **pparams) 246 | { 247 | static char str2[MAX_TOK]; 248 | char *pstr = str2; 249 | char *field[MAXFIELDS]; 250 | int nfields = 0; 251 | int timelen; 252 | char *powtime; 253 | char tbuf[3]; 254 | 255 | if (len > MAX_TOK || len < MAXFIELDS 256 | || str[0] != '1' || str[1] != ':') 257 | return RPOW_STAT_INVALID; 258 | 259 | strncpy (str2, str, len); 260 | 261 | while (len--) 262 | { 263 | if (*pstr == '\0') 264 | return RPOW_STAT_INVALID; 265 | if (*pstr == ':') 266 | { 267 | if (nfields+1 > MAXFIELDS) 268 | return RPOW_STAT_INVALID; 269 | field[nfields++] = pstr+1; 270 | *pstr = '\0'; 271 | } 272 | ++pstr; 273 | } 274 | 275 | if (nfields != MAXFIELDS) 276 | return RPOW_STAT_INVALID; 277 | 278 | powtime = field[1]; 279 | timelen = strlen (powtime); 280 | if (timelen < 6) 281 | return RPOW_STAT_INVALID; 282 | 283 | if (pvalue) 284 | *pvalue = atoi(field[0]); 285 | if (presource) 286 | *presource = field[2]; 287 | if (pparams) 288 | *pparams = field[3]; 289 | if (ptime) 290 | { 291 | memset (ptm, 0, sizeof(*ptm)); 292 | memset (tbuf, 0, sizeof(tbuf)); 293 | strncpy (tbuf, powtime, 2); 294 | ptm->tm_year = atoi(tbuf) + 100; 295 | strncpy (tbuf, powtime+2, 2); 296 | ptm->tm_mon = atoi(tbuf) - 1; 297 | strncpy (tbuf, powtime+4, 2); 298 | ptm->tm_mday = atoi(tbuf); 299 | *ptime = mktime(ptm); 300 | } 301 | return RPOW_STAT_OK; 302 | } 303 | 304 | static int 305 | rpow_valid_pow (rpow *rp) 306 | { 307 | int rslt; 308 | time_t nowtime, powtime; 309 | struct tm powtm; 310 | int powvalue; 311 | char *powresource1; 312 | unsigned char md[SHA1_DIGEST_LENGTH]; 313 | int i; 314 | 315 | if (rp->value < RPOW_VALUE_MIN || rp->value > RPOW_VALUE_MAX) 316 | return RPOW_STAT_INVALID; 317 | 318 | /* Parse the POW and see if its fields are legal */ 319 | rslt = pow_parse (rp->id, rp->idlen, &powvalue, &powtime, &powtm, 320 | &powresource1, NULL); 321 | if (rslt < 0) 322 | return rslt; 323 | if (powvalue != rp->value) 324 | return RPOW_STAT_INVALID; 325 | if (strcmp (powresource1, powresource) != 0) 326 | return RPOW_STAT_BADRESOURCE; 327 | nowtime = time(0); 328 | if (powtime > nowtime + POW_GRACESECONDS || 329 | powtime < nowtime - POW_EXPIRYSECONDS) 330 | return RPOW_STAT_BADTIME; 331 | 332 | /* Now test the hash to see if it has the right number of high 0's */ 333 | gbig_sha1_buf (md, rp->id, rp->idlen); 334 | for (i=0; (i+1)*8> (powvalue&7))) 338 | return RPOW_STAT_INVALID; 339 | 340 | /* Set the fileid from the month field */ 341 | rp->fileid = powtm.tm_mon % 3; 342 | 343 | return RPOW_STAT_OK; 344 | } 345 | 346 | 347 | static int 348 | rpow_valid_pk (rpow *rp) 349 | { 350 | pubkey *pk; 351 | gbignum paddedid; 352 | gbignum pow; 353 | gbignum exp; 354 | int stat = RPOW_STAT_OK; 355 | 356 | if ((pk = pk_from_keyid (rp->keyid)) == NULL) 357 | return RPOW_STAT_UNKNOWNKEY; 358 | if (pk->state == PUBKEY_STATE_INACTIVE) 359 | return RPOW_STAT_UNKNOWNKEY; 360 | rp->fileid = pk->fileid; 361 | 362 | /* We only accept id's for our cardid */ 363 | if (memcmp (rp->id + rp->idlen - CARDID_LENGTH, cardid, CARDID_LENGTH) 364 | != 0) 365 | return RPOW_STAT_BADCARDID; 366 | 367 | gbig_init (&paddedid); 368 | gbig_init (&pow); 369 | gbig_init (&exp); 370 | 371 | if (valuetoexp (&exp, rp->value, pk) < 0) 372 | { 373 | stat = RPOW_STAT_INVALID; 374 | goto done; 375 | } 376 | gbig_mod_exp (&pow, &rp->bn, &exp, &pk->n); 377 | rpowpend_bn_gen (&paddedid, rp->id, rp->idlen, pk); 378 | if (gbig_cmp (&pow, &paddedid) != 0) 379 | stat = RPOW_STAT_INVALID; 380 | done: 381 | gbig_free (&exp); 382 | gbig_free (&pow); 383 | gbig_free (&paddedid); 384 | return stat; 385 | } 386 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/server/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for host 4758 applications 2 | # Remember to set SCCTK_FS_ROOT!! 3 | 4 | SCCTK_FS_ROOT=../../scctk 5 | 6 | CFLAGS = -g -c -D_LINUX_ -I. -I../common \ 7 | -I$(SCCTK_FS_ROOT)/include/env/linux -I$(SCCTK_FS_ROOT)/include/host \ 8 | -I$(SCCTK_FS_ROOT)/include/common 9 | 10 | SCCLIB = /usr/local/lib/libscc.a 11 | 12 | SRVOBJS = rpowsrv.o dbproof.o sha1.o 13 | 14 | all: rpowsrv 15 | 16 | rpowsrv: $(SRVOBJS) 17 | gcc -g $(SRVOBJS) $(SCCLIB) -lcrypto -o rpowsrv 18 | 19 | clean: 20 | -rm rpowsrv $(SRVOBJS) 21 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/server/dbproof.h: -------------------------------------------------------------------------------- 1 | #ifndef DBPROOF_H 2 | #define DBPROOF_H 3 | 4 | /* 5 | * dbproof.h 6 | * Maintain database for remote host in a provable way 7 | */ 8 | 9 | struct dbproof; 10 | 11 | typedef struct dbproof dbproof; 12 | 13 | /* The database holds values of size HASHSIZE */ 14 | #define HASHSIZE 20 15 | 16 | /* 17 | * Return 1 if present, 0 if was absent. If set is true, add it if absent. 18 | * Return *proof and *prooflen as a buffer that proves our correct operation, 19 | * suitable for presenting to the remote host where a verification algorithm 20 | * can confirm that we are operating properly. 21 | */ 22 | int testdbandmaybeset (dbproof *db, unsigned char **proof, unsigned *prooflen, 23 | unsigned char *hash, int set); 24 | 25 | #define testdbandset(db,p,pl,h) testdbandmaybeset(db,p,pl,h,1) 26 | #define testdb(db,p,pl,h) testdbandmaybeset(db,p,pl,h,0) 27 | 28 | /* Return the depth of the DB btree */ 29 | int testdb_depth (dbproof *db); 30 | 31 | /* 32 | * Locally test the validity proof; the exact same algorithm should be 33 | * used by the remote host. 34 | */ 35 | void testvalid (void *proof, unsigned prooflen, unsigned char *treehash, 36 | int *maxdepth, unsigned char *hash, int shouldbefound, int set); 37 | 38 | /* 39 | * Open the database file of the specified name. 40 | * Create it if it doesn't exist. 41 | * Another file with extension .vals added is also used. 42 | */ 43 | dbproof * opendb (char *name, int *created); 44 | 45 | void freedb (dbproof *db); 46 | 47 | #endif /* DBPROOF_H */ 48 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/server/gbignum.h: -------------------------------------------------------------------------------- 1 | /* Generic bignum module, wrappers around other bignum libraries */ 2 | 3 | #ifndef GBIGNUM_H 4 | #define GBIGNUM_H 5 | 6 | #include 7 | #include 8 | 9 | extern BN_CTX *bnctx; 10 | typedef BIGNUM gbignum; 11 | typedef SHA_CTX gbig_sha1ctx; 12 | 13 | #ifndef SHA1_DIGEST_LENGTH 14 | #define SHA1_DIGEST_LENGTH 20 15 | #endif /* SHA1_DIGEST_LENGTH */ 16 | 17 | extern int gbig_initialize(void); 18 | extern int gbig_finalize(void); 19 | extern int gbig_rand_bytes(void *,unsigned); 20 | extern void _gbig_rand_range (BIGNUM *, BIGNUM *, BIGNUM *); 21 | extern gbignum gbig_value_zero; 22 | extern gbignum gbig_value_one; 23 | extern gbignum gbig_value_two; 24 | extern gbignum gbig_value_three; 25 | 26 | #define gbig_sha1_buf(md,buf,len) \ 27 | SHA1 (buf,len,md) 28 | #define gbig_sha1_init(ctx) \ 29 | SHA1_Init (ctx) 30 | #define gbig_sha1_update(ctx,buf,len) \ 31 | SHA1_Update (ctx, buf, len) 32 | #define gbig_sha1_final(md,ctx) \ 33 | SHA1_Final (md, ctx) 34 | 35 | #define gbig_init(gbna) BN_init(gbna) 36 | #define gbig_free(gbna) BN_free(gbna) 37 | #define gbig_copy(gbnb,gbna) \ 38 | BN_copy(gbnb, gbna) 39 | #define gbig_add(gbnc,gbna,gbnb) \ 40 | BN_add(gbnc,gbna,gbnb) 41 | #define gbig_sub(gbnc,gbna,gbnb) \ 42 | BN_sub(gbnc,gbna,gbnb) 43 | #define gbig_mul(gbnc,gbna,gbnb) \ 44 | BN_mul(gbnc,gbna,gbnb,bnctx) 45 | #define gbig_div(gbnc,gbna,gbnb) \ 46 | BN_div(gbnc,NULL,gbna,gbnb,bnctx) 47 | #define gbig_mod(gbnc,gbna,gbnb) \ 48 | BN_mod(gbnc,gbna,gbnb,bnctx) 49 | #define gbig_div_mod(gbnd,gbnr,gbna,gbnb) \ 50 | BN_div(gbnd,gbnr,gbna,gbnb,bnctx) 51 | #define gbig_mod_add(gbnc,gbna,gbnb,gbnm) \ 52 | BN_mod_add(gbnc,gbna,gbnb,gbnm,bnctx) 53 | #define gbig_mod_sub(gbnc,gbna,gbnb,gbnm) \ 54 | BN_mod_sub(gbnc,gbna,gbnb,gbnm,bnctx) 55 | #define gbig_mod_mul(gbnc,gbna,gbnb,gbnm) \ 56 | BN_mod_mul(gbnc,gbna,gbnb,gbnm,bnctx) 57 | #define gbig_mod_exp(gbnc,gbna,gbnb,gbnm) \ 58 | BN_mod_exp(gbnc,gbna,gbnb,gbnm,bnctx) 59 | #define gbig_mod_inverse(gbnc,gbna,gbnm) \ 60 | BN_mod_inverse(gbnc,gbna,gbnm,bnctx) 61 | #define gbig_gcd(gbnc,gbna,gbnb) \ 62 | BN_gcd(gbnc,gbna,gbnb,bnctx) 63 | 64 | #define gbig_cmp(gbna,gbnb) \ 65 | BN_cmp(gbna,gbnb) 66 | #define gbig_from_word(gbna,n) \ 67 | BN_set_word(gbna,(n)) 68 | #define gbig_to_word(gbna) \ 69 | BN_get_word(gbna) 70 | #define gbig_set_bit(gbna,bit) \ 71 | BN_set_bit(gbna,bit) 72 | #define gbig_clear_bit(gbna,bit) \ 73 | BN_clear_bit(gbna,bit) 74 | #define gbig_shift_left(gbnb,gbna,bit) \ 75 | BN_lshift(gbnb,gbna,bit) 76 | #define gbig_shift_right(gbnb,gbna,bit) \ 77 | BN_rshift(gbnb,gbna,bit) 78 | 79 | #define gbig_to_buf(buf,gbna) \ 80 | BN_bn2bin(gbna,buf) 81 | #define gbig_from_buf(gbna,buf,buflen) \ 82 | BN_bin2bn(buf,buflen,gbna) 83 | #define gbig_buflen(gbna) \ 84 | BN_num_bytes(gbna) 85 | 86 | #define gbig_rand_range(gbnr,gbna,gbnb) \ 87 | _gbig_rand_range(gbnr,gbna,gbnb) 88 | #define gbig_is_prime(gbna) \ 89 | BN_is_prime(gbna,0,NULL,bnctx,NULL) 90 | #define gbig_generate_prime(gbnr,bits) \ 91 | BN_generate_prime (gbnr,bits,0,NULL,NULL,NULL,NULL) 92 | 93 | 94 | 95 | 96 | #endif /* GBIGNUM_H */ 97 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/server/sha.h: -------------------------------------------------------------------------------- 1 | /* 2 | * sha.h 3 | * 4 | * Originally taken from the public domain SHA1 implementation 5 | * written by by Steve Reid 6 | * 7 | * Modified by Aaron D. Gifford 8 | * 9 | * NO COPYRIGHT - THIS IS 100% IN THE PUBLIC DOMAIN 10 | * 11 | * The original unmodified version is available at: 12 | * ftp://ftp.funet.fi/pub/crypt/hash/sha/sha1.c 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef __SHA1_H__ 28 | #define __SHA1_H__ 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | /* Define this if your machine is LITTLE_ENDIAN, otherwise #undef it: */ 35 | #ifndef LITTLE_ENDIAN 36 | #define LITTLE_ENDIAN 37 | #endif 38 | 39 | /* Make sure you define these types for your architecture: */ 40 | typedef unsigned long sha1_quadbyte; /* 4 byte type */ 41 | typedef unsigned char sha1_byte; /* single byte type */ 42 | 43 | /* 44 | * Be sure to get the above definitions right. For instance, on my 45 | * x86 based FreeBSD box, I define LITTLE_ENDIAN and use the type 46 | * "unsigned long" for the quadbyte. On FreeBSD on the Alpha, however, 47 | * while I still use LITTLE_ENDIAN, I must define the quadbyte type 48 | * as "unsigned int" instead. 49 | */ 50 | 51 | #define SHA1_BLOCK_LENGTH 64 52 | #define SHA1_DIGEST_LENGTH 20 53 | 54 | /* The SHA1 structure: */ 55 | typedef struct _SHA_CTX { 56 | sha1_quadbyte state[5]; 57 | sha1_quadbyte count[2]; 58 | sha1_byte buffer[SHA1_BLOCK_LENGTH]; 59 | } SHA_CTX; 60 | 61 | #ifndef NOPROTO 62 | void SHA1_Init(SHA_CTX *context); 63 | void SHA1_Update(SHA_CTX *context, sha1_byte *data, unsigned int len); 64 | void SHA1_Final(sha1_byte digest[SHA1_DIGEST_LENGTH], SHA_CTX* context); 65 | #else 66 | void SHA1_Init(); 67 | void SHA1_Update(); 68 | void SHA1_Final(); 69 | #endif 70 | 71 | #ifdef __cplusplus 72 | } 73 | #endif 74 | 75 | #endif 76 | 77 | -------------------------------------------------------------------------------- /precursor/rpow-1.2.0/server/sha1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * sha1.c 3 | * 4 | * Originally witten by Steve Reid 5 | * 6 | * Modified by Aaron D. Gifford 7 | * 8 | * NO COPYRIGHT - THIS IS 100% IN THE PUBLIC DOMAIN 9 | * 10 | * The original unmodified version is available at: 11 | * ftp://ftp.funet.fi/pub/crypt/hash/sha/sha1.c 12 | * 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND 14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE 17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 | * SUCH DAMAGE. 24 | */ 25 | 26 | #include "sha.h" 27 | 28 | #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) 29 | 30 | /* blk0() and blk() perform the initial expand. */ 31 | /* I got the idea of expanding during the round function from SSLeay */ 32 | 33 | #ifdef LITTLE_ENDIAN 34 | #define blk0(i) (block->l[i] = (rol(block->l[i],24)&(sha1_quadbyte)0xFF00FF00) \ 35 | |(rol(block->l[i],8)&(sha1_quadbyte)0x00FF00FF)) 36 | #else 37 | #define blk0(i) block->l[i] 38 | #endif 39 | 40 | #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ 41 | ^block->l[(i+2)&15]^block->l[i&15],1)) 42 | 43 | /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ 44 | #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); 45 | #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); 46 | #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); 47 | #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); 48 | #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); 49 | 50 | typedef union _BYTE64QUAD16 { 51 | sha1_byte c[64]; 52 | sha1_quadbyte l[16]; 53 | } BYTE64QUAD16; 54 | 55 | /* Hash a single 512-bit block. This is the core of the algorithm. */ 56 | void SHA1_Transform(sha1_quadbyte state[5], sha1_byte buffer[64]) { 57 | sha1_quadbyte a, b, c, d, e; 58 | BYTE64QUAD16 *block; 59 | 60 | block = (BYTE64QUAD16*)buffer; 61 | /* Copy context->state[] to working vars */ 62 | a = state[0]; 63 | b = state[1]; 64 | c = state[2]; 65 | d = state[3]; 66 | e = state[4]; 67 | /* 4 rounds of 20 operations each. Loop unrolled. */ 68 | R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); 69 | R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); 70 | R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); 71 | R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); 72 | R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); 73 | R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); 74 | R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); 75 | R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); 76 | R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); 77 | R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); 78 | R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); 79 | R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); 80 | R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); 81 | R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); 82 | R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); 83 | R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); 84 | R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); 85 | R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); 86 | R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); 87 | R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); 88 | /* Add the working vars back into context.state[] */ 89 | state[0] += a; 90 | state[1] += b; 91 | state[2] += c; 92 | state[3] += d; 93 | state[4] += e; 94 | /* Wipe variables */ 95 | a = b = c = d = e = 0; 96 | } 97 | 98 | 99 | /* SHA1_Init - Initialize new context */ 100 | void SHA1_Init(SHA_CTX* context) { 101 | /* SHA1 initialization constants */ 102 | context->state[0] = 0x67452301; 103 | context->state[1] = 0xEFCDAB89; 104 | context->state[2] = 0x98BADCFE; 105 | context->state[3] = 0x10325476; 106 | context->state[4] = 0xC3D2E1F0; 107 | context->count[0] = context->count[1] = 0; 108 | } 109 | 110 | /* Run your data through this. */ 111 | void SHA1_Update(SHA_CTX *context, sha1_byte *data, unsigned int len) { 112 | unsigned int i, j; 113 | 114 | j = (context->count[0] >> 3) & 63; 115 | if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; 116 | context->count[1] += (len >> 29); 117 | if ((j + len) > 63) { 118 | memcpy(&context->buffer[j], data, (i = 64-j)); 119 | SHA1_Transform(context->state, context->buffer); 120 | for ( ; i + 63 < len; i += 64) { 121 | SHA1_Transform(context->state, &data[i]); 122 | } 123 | j = 0; 124 | } 125 | else i = 0; 126 | memcpy(&context->buffer[j], &data[i], len - i); 127 | } 128 | 129 | 130 | /* Add padding and return the message digest. */ 131 | void SHA1_Final(sha1_byte digest[SHA1_DIGEST_LENGTH], SHA_CTX *context) { 132 | sha1_quadbyte i, j; 133 | sha1_byte finalcount[8]; 134 | 135 | for (i = 0; i < 8; i++) { 136 | finalcount[i] = (sha1_byte)((context->count[(i >= 4 ? 0 : 1)] 137 | >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ 138 | } 139 | SHA1_Update(context, (sha1_byte *)"\200", 1); 140 | while ((context->count[0] & 504) != 448) { 141 | SHA1_Update(context, (sha1_byte *)"\0", 1); 142 | } 143 | /* Should cause a SHA1_Transform() */ 144 | SHA1_Update(context, finalcount, 8); 145 | for (i = 0; i < SHA1_DIGEST_LENGTH; i++) { 146 | digest[i] = (sha1_byte) 147 | ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); 148 | } 149 | /* Wipe variables */ 150 | i = j = 0; 151 | memset(context->buffer, 0, SHA1_BLOCK_LENGTH); 152 | memset(context->state, 0, SHA1_DIGEST_LENGTH); 153 | memset(context->count, 0, 8); 154 | memset(&finalcount, 0, 8); 155 | } 156 | 157 | -------------------------------------------------------------------------------- /satoshinakamoto.asc: -------------------------------------------------------------------------------- 1 | -----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v1.4.7 (MingW32) 2 | mQGiBEkJ+qcRBADKDTcZlYDRtP1Q7/ShuzBJzUh9hoVVowogf2W07U6G9BqKW24r piOxYmErjMFfvNtozNk+33cd/sq3gi05O1IMmZzg2rbF4ne5t3iplXnNuzNh+j+6 VxxA16GPhBRprvnng8r9GYALLUpo9Xk17KE429YYKFgVvtTPtEGUlpO1EwCg7FmW dBbRp4mn5GfxQNT1hzp9WgkD/3pZ0cB5m4enzfylOHXmRfJKBMF02ZDnsY1GqeHv /LjkhCusTp2qz4thLycYOFKGmAddpVnMsE/TYZLgpsxjrJsrEPNSdoXk3IgEStow mXjTfr9xNOrB20Qk0ZOO1mipOWMgse4PmIu02X24OapWtyhdHsX3oBLcwDdke8aE gAh8A/sHlK7fL1Bi8rFzx6hb+2yIlD/fazMBVZUe0r2uo7ldqEz5+GeEiBFignd5 HHhqjJw8rUJkfeZBoTKYlDKo7XDrTRxfyzNuZZPxBLTj+keY8WgYhQ5MWsSC2MX7 FZHaJddYa0pzUmFZmQh0ydulVUQnLKzRSunsjGOnmxiWBZwb6bQjU2F0b3NoaSBO YWthbW90byA8c2F0b3NoaW5AZ214LmNvbT6IYAQTEQIAIAUCSQn6pwIbAwYLCQgH AwIEFQIIAwQWAgMBAh4BAheAAAoJEBjAnoZeyUihXGMAnjiWJ0fvmSgSM3o6Tu3q RME9GN7QAKCGrFw9SUD0e9/YDcqhX1aPMrYue7kCDQRJCfqnEAgA9OTCjLa6Sj7t dZcQxNufsDSCSB+yznIGzFGXXpJk7GgKmX3H9Zl4E6zJTQGXL2GAV4klkSfNtvgs SGJKqCnebuZVwutyq1vXRNVFPQFvLVVo2jJCBHWjb03fmXmavIUtRCHoc8xgVJMQ LrwvS943GgsqSbdoKZWdTnfnEq+UaGo+Qfv66NpT3Yl0CXUiNBITZOJcJdjHDTBO XRqomX2WSguv+btYdhQGGQiaEx73XMftXNCxbOpqwsODQns7xTcl2ENru9BNIQME I7L9FYBQUiKHm1k6RrBy1as8XElS2jEos7GAmlfF1wShFUX+NF1VOPdbN3ZdFoWq sUjKk+QbrwADBQgA9DiD4+uuRhwk2B1TmtrXnwwhcdkE7ZbLHjxBfCsLPAZiPh8c ICfV3S418i4H1YCz2ItcnC8KAPoS6mipyS28AU1B7zJYPODBn8E7aPSPzHJfudMK MqiCHljVJrE23xsKTC0sIhhSKcr2G+6ARoG5lwuoqJqEyDrblVQQFpVxBNPHSTqu O5PoLXQc7PKgC5SyQuZbEALEkItl2SL2yBRRGOlVJLnvZ6eaovkAlgsbGdlieOr0 UwWuJCwzZuBDruMYAfyQBvYfXZun3Zm84rW7Jclp18mXITwGCVHg/P5n7QMbBfZQ A25ymkuj636Nqh+c4zRnSINfyrDcID7AcqEb6IhJBBgRAgAJBQJJCfqnAhsMAAoJ EBjAnoZeyUihPrcAniVWl5M44RuGctJe+IMNX4eVkC08AJ9v7cXsp5uDdQNo8q3R 8RHwN4Gk8w== =3FTe -----END PGP PUBLIC KEY BLOCK----- -------------------------------------------------------------------------------- /study/irc.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009 Satoshi Nakamoto 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file license.txt or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #include "headers.h" 6 | 7 | 8 | 9 | 10 | #pragma pack(1) 11 | struct ircaddr 12 | { 13 | int ip; 14 | short port; 15 | }; 16 | 17 | string EncodeAddress(const CAddress& addr) 18 | { 19 | struct ircaddr tmp; 20 | tmp.ip = addr.ip; 21 | tmp.port = addr.port; 22 | 23 | vector vch(UBEGIN(tmp), UEND(tmp)); 24 | return string("u") + EncodeBase58Check(vch); 25 | } 26 | 27 | bool DecodeAddress(string str, CAddress& addr) 28 | { 29 | vector vch; 30 | if (!DecodeBase58Check(str.substr(1), vch)) 31 | return false; 32 | 33 | struct ircaddr tmp; 34 | if (vch.size() != sizeof(tmp)) 35 | return false; 36 | memcpy(&tmp, &vch[0], sizeof(tmp)); 37 | 38 | addr = CAddress(tmp.ip, tmp.port); 39 | return true; 40 | } 41 | 42 | 43 | 44 | 45 | 46 | 47 | static bool Send(SOCKET hSocket, const char* pszSend) 48 | { 49 | if (strstr(pszSend, "PONG") != pszSend) 50 | printf("SENDING: %s\n", pszSend); 51 | const char* psz = pszSend; 52 | const char* pszEnd = psz + strlen(psz); 53 | while (psz < pszEnd) 54 | { 55 | int ret = send(hSocket, psz, pszEnd - psz, 0); 56 | if (ret < 0) 57 | return false; 58 | psz += ret; 59 | } 60 | return true; 61 | } 62 | 63 | bool RecvLine(SOCKET hSocket, string& strLine) 64 | { 65 | strLine = ""; 66 | loop 67 | { 68 | char c; 69 | int nBytes = recv(hSocket, &c, 1, 0); 70 | if (nBytes > 0) 71 | { 72 | if (c == '\n') 73 | continue; 74 | if (c == '\r') 75 | return true; 76 | strLine += c; 77 | } 78 | else if (nBytes <= 0) 79 | { 80 | if (!strLine.empty()) 81 | return true; 82 | // socket closed 83 | printf("IRC socket closed\n"); 84 | return false; 85 | } 86 | else 87 | { 88 | // socket error 89 | int nErr = WSAGetLastError(); 90 | if (nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS) 91 | { 92 | printf("IRC recv failed: %d\n", nErr); 93 | return false; 94 | } 95 | } 96 | } 97 | } 98 | 99 | bool RecvLineIRC(SOCKET hSocket, string& strLine) 100 | { 101 | loop 102 | { 103 | bool fRet = RecvLine(hSocket, strLine); 104 | if (fRet) 105 | { 106 | if (fShutdown) 107 | return false; 108 | vector vWords; 109 | ParseString(strLine, ' ', vWords); 110 | if (vWords[0] == "PING") 111 | { 112 | strLine[1] = 'O'; 113 | strLine += '\r'; 114 | Send(hSocket, strLine.c_str()); 115 | continue; 116 | } 117 | } 118 | return fRet; 119 | } 120 | } 121 | 122 | bool RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL) 123 | { 124 | loop 125 | { 126 | string strLine; 127 | if (!RecvLineIRC(hSocket, strLine)) 128 | return false; 129 | printf("IRC %s\n", strLine.c_str()); 130 | if (psz1 && strLine.find(psz1) != -1) 131 | return true; 132 | if (psz2 && strLine.find(psz2) != -1) 133 | return true; 134 | if (psz3 && strLine.find(psz3) != -1) 135 | return true; 136 | } 137 | } 138 | 139 | 140 | 141 | 142 | bool fRestartIRCSeed = false; 143 | 144 | void ThreadIRCSeed(void* parg) 145 | { 146 | loop 147 | { 148 | struct hostent* phostent = gethostbyname("chat.freenode.net"); 149 | CAddress addrConnect(*(u_long*)phostent->h_addr_list[0], htons(6667)); 150 | 151 | SOCKET hSocket; 152 | if (!ConnectSocket(addrConnect, hSocket)) 153 | { 154 | printf("IRC connect failed\n"); 155 | return; 156 | } 157 | 158 | if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname")) 159 | { 160 | closesocket(hSocket); 161 | return; 162 | } 163 | 164 | string strMyName = EncodeAddress(addrLocalHost); 165 | 166 | if (!addrLocalHost.IsRoutable()) 167 | strMyName = strprintf("x%u", GetRand(1000000000)); 168 | 169 | Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str()); 170 | Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str()); 171 | 172 | if (!RecvUntil(hSocket, " 004 ")) 173 | { 174 | closesocket(hSocket); 175 | return; 176 | } 177 | Sleep(500); 178 | 179 | Send(hSocket, "JOIN #bitcoin\r"); 180 | Send(hSocket, "WHO #bitcoin\r"); 181 | 182 | while (!fRestartIRCSeed) 183 | { 184 | string strLine; 185 | if (fShutdown || !RecvLineIRC(hSocket, strLine)) 186 | { 187 | closesocket(hSocket); 188 | return; 189 | } 190 | if (strLine.empty() || strLine[0] != ':') 191 | continue; 192 | printf("IRC %s\n", strLine.c_str()); 193 | 194 | vector vWords; 195 | ParseString(strLine, ' ', vWords); 196 | if (vWords.size() < 2) 197 | continue; 198 | 199 | char pszName[10000]; 200 | pszName[0] = '\0'; 201 | 202 | if (vWords[1] == "352" && vWords.size() >= 8) 203 | { 204 | // index 7 is limited to 16 characters 205 | // could get full length name at index 10, but would be different from join messages 206 | strcpy(pszName, vWords[7].c_str()); 207 | printf("GOT WHO: [%s] ", pszName); 208 | } 209 | 210 | if (vWords[1] == "JOIN") 211 | { 212 | // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname 213 | strcpy(pszName, vWords[0].c_str() + 1); 214 | if (strchr(pszName, '!')) 215 | *strchr(pszName, '!') = '\0'; 216 | printf("GOT JOIN: [%s] ", pszName); 217 | } 218 | 219 | if (pszName[0] == 'u') 220 | { 221 | CAddress addr; 222 | if (DecodeAddress(pszName, addr)) 223 | { 224 | CAddrDB addrdb; 225 | if (AddAddress(addrdb, addr)) 226 | printf("new "); 227 | addr.print(); 228 | } 229 | else 230 | { 231 | printf("decode failed\n"); 232 | } 233 | } 234 | } 235 | 236 | fRestartIRCSeed = false; 237 | closesocket(hSocket); 238 | } 239 | } 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | #ifdef TEST 251 | int main(int argc, char *argv[]) 252 | { 253 | WSADATA wsadata; 254 | if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR) 255 | { 256 | printf("Error at WSAStartup()\n"); 257 | return false; 258 | } 259 | 260 | ThreadIRCSeed(NULL); 261 | 262 | WSACleanup(); 263 | return 0; 264 | } 265 | #endif 266 | --------------------------------------------------------------------------------