From 1092a4e4a4dfa83f05f8a84531bc92ea01b1be28 Mon Sep 17 00:00:00 2001 From: armin Date: Mon, 30 Apr 2018 10:58:18 +0200 Subject: [PATCH] Added command line options for success and dump file, window size --- .gitignore | 1 + .sonarlint/modules/bfclient/issues/index.pb | 6 ++- .../11f0bf66557a5b2474c03cfac7df8258ad7bfab9 | 5 ++ .../6bd08b7785854103940143964721a29b585a0574 | 2 +- .../ab959dedca57b39487a2217383b67b8e1da032f3 | 8 ++-- .../ecc7f16c71ca2f68bf6a65ea47837815c0ff6322 | 4 ++ .sonarlint/modules/cnc/issues/index.pb | 14 +++++- bfclient/src/bfclient.cpp | 10 ++-- cnc/src/main/java/org/btcollider/cnc/CnC.java | 12 ++++- .../org/btcollider/cnc/comm/ClientWorker.java | 22 +++++++-- .../org/btcollider/cnc/comm/CommServer.java | 47 ++++++++++++++++++- .../org/btcollider/cnc/keysrv/KSFactory.java | 2 +- cnc/src/main/resources/log4j2.xml | 34 ++++++++++---- 13 files changed, 138 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index de0ba63..64f89f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.blf +.sonarlint/ # copied from brainflyer gitignore diff --git a/.sonarlint/modules/bfclient/issues/index.pb b/.sonarlint/modules/bfclient/issues/index.pb index 4066ff5..4249aea 100644 --- a/.sonarlint/modules/bfclient/issues/index.pb +++ b/.sonarlint/modules/bfclient/issues/index.pb @@ -6,4 +6,8 @@ D L src/bf/libs/ec_pubkey_fast.c,5/9/5995e3430cea453ad68838818ad7fc05c86b119f K -src/bf/libs/ripemd160_256.c,4/b/4b8d4ad995cb3950cece8de680eb57ef0fe2921f \ No newline at end of file +src/bf/libs/ripemd160_256.c,4/b/4b8d4ad995cb3950cece8de680eb57ef0fe2921f +E +src/bf/Brainflyer.cpp,5/0/50c7097d8cf446876a37bb356d2bc4dec9444c86 +A +src/comm/Comm.cpp,1/7/17d68a7335dbcfc6e373b75382c4c139a9c31446 \ No newline at end of file diff --git a/.sonarlint/modules/cnc/issues/1/1/11f0bf66557a5b2474c03cfac7df8258ad7bfab9 b/.sonarlint/modules/cnc/issues/1/1/11f0bf66557a5b2474c03cfac7df8258ad7bfab9 index e69de29..ed5635c 100644 --- a/.sonarlint/modules/cnc/issues/1/1/11f0bf66557a5b2474c03cfac7df8258ad7bfab9 +++ b/.sonarlint/modules/cnc/issues/1/1/11f0bf66557a5b2474c03cfac7df8258ad7bfab9 @@ -0,0 +1,5 @@ + +~ +squid:S899#"BDo something with the "boolean" value returned by "createNewFile".(8̜,HRMINORZ VULNERABILITY +x +squid:S899"BDo something with the "boolean" value returned by "createNewFile".(ɩi8,HRMINORZ VULNERABILITY \ No newline at end of file diff --git a/.sonarlint/modules/cnc/issues/6/b/6bd08b7785854103940143964721a29b585a0574 b/.sonarlint/modules/cnc/issues/6/b/6bd08b7785854103940143964721a29b585a0574 index f6b8a96..254f079 100644 --- a/.sonarlint/modules/cnc/issues/6/b/6bd08b7785854103940143964721a29b585a0574 +++ b/.sonarlint/modules/cnc/issues/6/b/6bd08b7785854103940143964721a29b585a0574 @@ -1,3 +1,3 @@ - squid:S3776"RRefactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.(HӘRCRITICALZ + squid:S3776"RRefactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.(HRCRITICALZ CODE_SMELL \ No newline at end of file diff --git a/.sonarlint/modules/cnc/issues/a/b/ab959dedca57b39487a2217383b67b8e1da032f3 b/.sonarlint/modules/cnc/issues/a/b/ab959dedca57b39487a2217383b67b8e1da032f3 index b47c342..57dfd94 100644 --- a/.sonarlint/modules/cnc/issues/a/b/ab959dedca57b39487a2217383b67b8e1da032f3 +++ b/.sonarlint/modules/cnc/issues/a/b/ab959dedca57b39487a2217383b67b8e1da032f3 @@ -1,11 +1,9 @@  -squid:S135"YReduce the total number of break and continue statements in this loop to use at most one.(рHRMINORZ -CODE_SMELL - squid:S3776"RRefactor this method to reduce its Cognitive Complexity from 21 to the 15 allowed.(HRCRITICALZ +squid:S135"YReduce the total number of break and continue statements in this loop to use at most one.(рHRMINORZ CODE_SMELL  -squid:S135"YReduce the total number of break and continue statements in this loop to use at most one.(рHRMINORZ +squid:S135"YReduce the total number of break and continue statements in this loop to use at most one.(рHRMINORZ CODE_SMELL - squid:S1186"Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.(Ԓ8͒,HRCRITICALZ + squid:S3776"RRefactor this method to reduce its Cognitive Complexity from 21 to the 15 allowed.(HRCRITICALZ CODE_SMELL \ No newline at end of file diff --git a/.sonarlint/modules/cnc/issues/e/c/ecc7f16c71ca2f68bf6a65ea47837815c0ff6322 b/.sonarlint/modules/cnc/issues/e/c/ecc7f16c71ca2f68bf6a65ea47837815c0ff6322 index e69de29..a2f2ae8 100644 --- a/.sonarlint/modules/cnc/issues/e/c/ecc7f16c71ca2f68bf6a65ea47837815c0ff6322 +++ b/.sonarlint/modules/cnc/issues/e/c/ecc7f16c71ca2f68bf6a65ea47837815c0ff6322 @@ -0,0 +1,4 @@ + +l +squid:S106"9Replace this use of System.out or System.err by a logger.(W8뛱,HRMAJORZ +CODE_SMELL \ No newline at end of file diff --git a/.sonarlint/modules/cnc/issues/index.pb b/.sonarlint/modules/cnc/issues/index.pb index f705faa..dcdd9ce 100644 --- a/.sonarlint/modules/cnc/issues/index.pb +++ b/.sonarlint/modules/cnc/issues/index.pb @@ -12,4 +12,16 @@ Bsrc/main/java/org/btcollider/cnc/keysrv/keytree/KeyTreeServer.java,a/b/ab959de h 8src/main/java/org/btcollider/cnc/comm/CommException.java,8/f/8fe1dc8959c76b2c26853ccd55ee1f4bcebcbcfe g -7src/main/java/org/btcollider/cnc/comm/ClientWorker.java,f/b/fb0de740b71d2edb598c4f3deb39da1c59c1ec4f \ No newline at end of file +7src/main/java/org/btcollider/cnc/comm/ClientWorker.java,f/b/fb0de740b71d2edb598c4f3deb39da1c59c1ec4f +p +@src/main/java/org/btcollider/cnc/keysrv/keytree/KTConcierge.java,6/b/6bd08b7785854103940143964721a29b585a0574 +b +2src/main/java/org/btcollider/cnc/dto/KeyRange.java,9/6/96b215a1323591b6d4c32541aebd0f6c1683b246 +v +Fsrc/main/java/org/btcollider/cnc/keysrv/multistage/KeyRangeBuffer.java,f/5/f54562b1bb37a29e09c1a61428a7b375be679412 +y +Isrc/main/java/org/btcollider/cnc/keysrv/multistage/MultiStagedServer.java,7/8/78bca5a517888f548ba402e13adef75c72b18987 +f +6src/main/java/org/btcollider/cnc/keysrv/KSFactory.java,1/3/136c36f2bf1690ff10397e07c3d82baa9b298770 +M +src/main/resources/log4j2.xml,3/6/36b9f0d33e4b7e602e3cd701314fae81543db66f \ No newline at end of file diff --git a/bfclient/src/bfclient.cpp b/bfclient/src/bfclient.cpp index 16028eb..fdb20c8 100644 --- a/bfclient/src/bfclient.cpp +++ b/bfclient/src/bfclient.cpp @@ -18,19 +18,23 @@ using namespace std; #define BATCH_MAX 4096 -#define WIN_SIZE 21 int main(int argc, char** argv) { util::bytestr key("db53d9bbd1f3a83b094eeca7dd970bd85b492fa2"); - if (argc < 3) { + if (argc < 4) { cout << "Invalid argument count" << endl; return (EXIT_FAILURE); } + cout << "Computing private key for " << key << endl; + + int win_size = stoi(argv[3]); + cout << "Window size: " << win_size << endl; + cout << "Initializing precomputation table" << endl; // init with window size 16, not reading from file - if (secp256k1_ec_pubkey_precomp_table(WIN_SIZE, nullptr) != 0) { + if (secp256k1_ec_pubkey_precomp_table(win_size, nullptr) != 0) { cout << "Initialization of precomputation table failed" << endl; return (EXIT_FAILURE); } diff --git a/cnc/src/main/java/org/btcollider/cnc/CnC.java b/cnc/src/main/java/org/btcollider/cnc/CnC.java index ed1bebc..a4e99e2 100644 --- a/cnc/src/main/java/org/btcollider/cnc/CnC.java +++ b/cnc/src/main/java/org/btcollider/cnc/CnC.java @@ -20,8 +20,16 @@ public class CnC { public static final int MAX_BITS = 62; public static void main(String[] args) { - KeyServer kts = KSFactory.build(54, 30, 90, TimeUnit.MINUTES, 0.62f); - CommServer server = new CommServer(PORT, kts); + if (args.length != 2) { + System.out.println("USAGE: java -jar cnc.jar "); + System.exit(-1); + } + + String succFile = args[0]; + String dumpFile = args[1]; + + KeyServer kts = KSFactory.build(54, 30, 90, TimeUnit.MINUTES, 0.65f); + CommServer server = new CommServer(PORT, kts, succFile, dumpFile); try { server.listen(); } catch (CommException e) { diff --git a/cnc/src/main/java/org/btcollider/cnc/comm/ClientWorker.java b/cnc/src/main/java/org/btcollider/cnc/comm/ClientWorker.java index ed0a9e3..d9d8468 100644 --- a/cnc/src/main/java/org/btcollider/cnc/comm/ClientWorker.java +++ b/cnc/src/main/java/org/btcollider/cnc/comm/ClientWorker.java @@ -2,6 +2,8 @@ package org.btcollider.cnc.comm; import java.io.BufferedReader; import java.io.BufferedWriter; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; @@ -20,10 +22,14 @@ public class ClientWorker implements Runnable { private KeyServer keyServer; private BufferedReader in; private PrintWriter out; + private String succFile; + private String dumpFile; - public ClientWorker(Socket clientSocket, KeyServer keyServer) { + public ClientWorker(Socket clientSocket, KeyServer keyServer, String succFile, String dumpFile) { this.clientSocket = clientSocket; this.keyServer = keyServer; + this.succFile = succFile; + this.dumpFile = dumpFile; } @Override @@ -60,6 +66,15 @@ public class ClientWorker implements Runnable { case "RES": retrieveResult(); break; + case "DMP": + try { + keyServer.dump(new FileOutputStream(dumpFile)); + } catch (FileNotFoundException e) { + log.error("Dump file not found", e); + } catch (IOException e) { + log.error("Couldn't dump key state to dump file {}", dumpFile, e); + } + break; default: assert false; } @@ -136,6 +151,7 @@ public class ClientWorker implements Runnable { log.info("Found private key {}", suc); writeKey(suc); out.println("ACK"); + System.exit(0); break; default: assert (false); @@ -147,10 +163,10 @@ public class ClientWorker implements Runnable { } private void writeKey(String key) { - try (BufferedWriter writer = new BufferedWriter(new FileWriter("~/cwkey"))) { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(succFile))) { writer.write(key); } catch (IOException e) { - log.error("Couldn't write key {} to keyfile", key, e); + log.error("Couldn't write key {} to success file {}", key, succFile, e); } } diff --git a/cnc/src/main/java/org/btcollider/cnc/comm/CommServer.java b/cnc/src/main/java/org/btcollider/cnc/comm/CommServer.java index 2f4e2af..1986aa6 100644 --- a/cnc/src/main/java/org/btcollider/cnc/comm/CommServer.java +++ b/cnc/src/main/java/org/btcollider/cnc/comm/CommServer.java @@ -1,8 +1,11 @@ package org.btcollider.cnc.comm; +import java.io.File; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.btcollider.cnc.keysrv.KeyServer; @@ -15,10 +18,50 @@ public class CommServer { private int port; private ServerSocket serverSocket; private KeyServer keyServer; + private String succFile; + private String dumpFile; - public CommServer(int port, KeyServer keyServer) { + public CommServer(int port, KeyServer keyServer, String succFile, String dumpFile) { this.port = port; this.keyServer = keyServer; + + + try { + (new File(succFile)).createNewFile(); + } catch (IOException e1) { + log.error("Error while trying to create success file at: {}", e1); + } + try { + (new File(dumpFile)).createNewFile(); + } catch (IOException e1) { + log.error("Error while trying to create dump file at: {}", e1); + } + + if (!Files.isWritable(Paths.get(succFile))) { + log.warn("Success file path is not writeable: {}", succFile); + try { + this.succFile = Files.createTempFile("btcollider", ".succ").toFile().getAbsolutePath(); + } catch (IOException e) { + log.error("Couldn't create temporary success file", e); + } + log.warn("Temporary success file created. Path is: {}", this.succFile); + } else { + this.succFile = succFile; + log.info("Success file path is: {}", this.succFile); + } + + if (!Files.isWritable(Paths.get(dumpFile))) { + log.warn("Dump file path is not writeable: {}", dumpFile); + try { + this.dumpFile = Files.createTempFile("btcollider", ".dump").toFile().getAbsolutePath(); + } catch (IOException e) { + log.error("Couldn't create temporary dump file", e); + } + log.warn("Temporary dump file created. Path is: {}", this.dumpFile); + } else { + this.dumpFile = dumpFile; + log.info("Dump file path is: {}", this.dumpFile); + } } public void listen() throws CommException { @@ -32,7 +75,7 @@ public class CommServer { try { Socket socket = serverSocket.accept(); log.debug("Got connection from {}", socket.getInetAddress()); - es.execute(new ClientWorker(socket, keyServer)); + es.execute(new ClientWorker(socket, keyServer, succFile, dumpFile)); } catch (IOException e) { log.error("Couldn't establish connections on socket", e); diff --git a/cnc/src/main/java/org/btcollider/cnc/keysrv/KSFactory.java b/cnc/src/main/java/org/btcollider/cnc/keysrv/KSFactory.java index 069459c..d71f5de 100644 --- a/cnc/src/main/java/org/btcollider/cnc/keysrv/KSFactory.java +++ b/cnc/src/main/java/org/btcollider/cnc/keysrv/KSFactory.java @@ -30,7 +30,7 @@ public class KSFactory { public static KeyServer build(int index, int depth, long maxWorkSpan, TimeUnit timeUnit, long pruneKey) { - if (depth == 25) { + if (depth <= 25) { return new KeyTreeServer(index, depth, timeUnit.toMillis(maxWorkSpan), pruneKey); } else { return new MultiStagedServer(index, depth, timeUnit.toMillis(maxWorkSpan), pruneKey); diff --git a/cnc/src/main/resources/log4j2.xml b/cnc/src/main/resources/log4j2.xml index 443a5b4..5f330aa 100644 --- a/cnc/src/main/resources/log4j2.xml +++ b/cnc/src/main/resources/log4j2.xml @@ -1,13 +1,27 @@ - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file