Browse Source

Merge branch 'factorize-key-generation' of git://github.com/ProgVal/cjdns into crashey

Caleb James DeLisle 7 years ago
parent
commit
c4cc185364
4 changed files with 43 additions and 35 deletions
  1. 9 24
      client/cjdroute2.c
  2. 7 11
      contrib/c/makekeys.c
  3. 18 0
      crypto/Key.c
  4. 9 0
      crypto/Key.h

+ 9 - 24
client/cjdroute2.c

@@ -16,6 +16,7 @@
 #include "admin/angel/Core.h"
 #include "admin/angel/InterfaceWaiter.h"
 #include "client/Configurator.h"
+#include "crypto/Key.h"
 #include "benc/Dict.h"
 #include "benc/Int.h"
 #include "benc/List.h"
@@ -34,6 +35,7 @@
 #include "io/Writer.h"
 #include "memory/Allocator.h"
 #include "memory/MallocAllocator.h"
+#include "util/AddrTools.h"
 #include "util/ArchInfo.h"
 #include "util/Assert.h"
 #include "util/Base32.h"
@@ -52,35 +54,12 @@
 #include "util/version/Version.h"
 #include "net/Benchmark.h"
 
-#include "crypto_scalarmult_curve25519.h"
-
 #include <stdint.h>
 #include <stdio.h>
 #include <unistd.h>
 
 #define DEFAULT_TUN_DEV "tun0"
 
-static int genAddress(uint8_t addressOut[40],
-                      uint8_t privateKeyHexOut[65],
-                      uint8_t publicKeyBase32Out[53],
-                      struct Random* rand)
-{
-    struct Address address;
-    uint8_t privateKey[32];
-
-    for (;;) {
-        Random_bytes(rand, privateKey, 32);
-        crypto_scalarmult_curve25519_base(address.key, privateKey);
-        // Brute force for keys until one matches FC00:/8
-        if (AddressCalc_addressForPublicKey(address.ip6.bytes, address.key)) {
-            Hex_encode(privateKeyHexOut, 65, privateKey, 32);
-            Base32_encode(publicKeyBase32Out, 53, address.key, 32);
-            Address_printShortIp(addressOut, &address);
-            return 0;
-        }
-    }
-}
-
 static int genconf(struct Random* rand, bool eth)
 {
     uint8_t password[32];
@@ -97,10 +76,16 @@ static int genconf(struct Random* rand, bool eth)
         port = Random_uint16(rand);
     }
 
+    uint8_t publicKey[32];
     uint8_t publicKeyBase32[53];
+    uint8_t ip[16];
     uint8_t address[40];
+    uint8_t privateKey[32];
     uint8_t privateKeyHex[65];
-    genAddress(address, privateKeyHex, publicKeyBase32, rand);
+    Key_gen(ip, publicKey, privateKey, rand);
+    Base32_encode(publicKeyBase32, 53, publicKey, 32);
+    Hex_encode(privateKeyHex, 65, privateKey, 32);
+    AddrTools_printIp(address, ip);
 
     printf("{\n");
     printf("    // Private key:\n"

+ 7 - 11
contrib/c/makekeys.c

@@ -12,6 +12,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
+#include "crypto/Key.h"
 #include "crypto/random/Random.h"
 #include "memory/MallocAllocator.h"
 #include "crypto/AddressCalc.h"
@@ -19,8 +20,6 @@
 #include "util/Base32.h"
 #include "util/Hex.h"
 
-#include "crypto_scalarmult_curve25519.h"
-
 #include <signal.h>
 #include <stdio.h>
 
@@ -41,15 +40,12 @@ int main(int argc, char** argv)
 #endif
 
     for (;;) {
-        Random_bytes(rand, privateKey, 32);
-        crypto_scalarmult_curve25519_base(publicKey, privateKey);
-        if (AddressCalc_addressForPublicKey(ip, publicKey)) {
-            Hex_encode(hexPrivateKey, 65, privateKey, 32);
-            Base32_encode(publicKeyBase32, 53, publicKey, 32);
-            AddrTools_printIp(printedIp, ip);
-            printf("%s %s %s.k\n", hexPrivateKey, printedIp, publicKeyBase32);
-            fflush(stdout);
-        }
+        Key_gen(ip, publicKey, privateKey, rand);
+        Hex_encode(hexPrivateKey, 65, privateKey, 32);
+        Base32_encode(publicKeyBase32, 53, publicKey, 32);
+        AddrTools_printIp(printedIp, ip);
+        printf("%s %s %s.k\n", hexPrivateKey, printedIp, publicKeyBase32);
+        fflush(stdout);
     }
     return 0;
 }

+ 18 - 0
crypto/Key.c

@@ -14,11 +14,29 @@
  */
 
 #include "crypto/Key.h"
+#include "crypto/random/Random.h"
 #include "util/Base32.h"
 #include "crypto/AddressCalc.h"
 
+#include "crypto_scalarmult_curve25519.h"
+
 #include <stddef.h>
 
+int Key_gen(uint8_t addressOut[16],
+            uint8_t publicKeyOut[32],
+            uint8_t privateKeyOut[32],
+            struct Random* rand)
+{
+    for (;;) {
+        Random_bytes(rand, privateKeyOut, 32);
+        crypto_scalarmult_curve25519_base(publicKeyOut, privateKeyOut);
+        // Brute force for keys until one matches FC00:/8
+        if (AddressCalc_addressForPublicKey(addressOut, publicKeyOut)) {
+            return 0;
+        }
+    }
+}
+
 char* Key_parse_strerror(int error)
 {
     switch (error) {

+ 9 - 0
crypto/Key.h

@@ -16,12 +16,21 @@
 #define Key_H
 
 #include "benc/String.h"
+#include "crypto/random/Random.h"
 #include "memory/Allocator.h"
 #include "util/Linker.h"
 Linker_require("crypto/Key.c");
 
 #include <stdint.h>
 
+/**
+ * Generates a new key such that its derived IP address is in fc00::/8.
+ */
+int Key_gen(uint8_t addressOut[16],
+            uint8_t publicKeyOut[32],
+            uint8_t privateKeyOut[32],
+            struct Random* rand);
+
 char* Key_parse_strerror(int error);
 
 #define Key_parse_TOO_SHORT -1