--- /dev/null
+//
+// C++ Implementation: distkey
+//
+// Description:
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2009
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+//
+
+#include "distkey.h"
+
+#ifdef HAVE_KEYGEN
+
+#include <gmp.h>
+
+#include <QFile>
+#include <QByteArray>
+
+int keygen()
+{
+ //open /dev/random
+ QFile rf("/dev/random");
+ if(!rf.open(QIODevice::ReadOnly)){
+ qDebug("Error: cannot open /dev/random, giving up.");
+ return -1;
+ }
+ //generate p,q,n,e,d
+ mpz_t p,q,n,d,e,t;
+ mpz_init(p);
+ mpz_init(q);
+ mpz_init(n);
+ mpz_init(d);
+ mpz_init(e);
+ mpz_init(t);
+ do{
+ qDebug("generating p...");
+ QByteArray ba=rf.read(64);
+ if(ba.size()<64){
+ qDebug("Exception: p<512 bit");
+ return -1;
+ }
+ ba.prepend((char)1);
+ mpz_set_str(t,ba.toHex().data(),16);
+ mpz_nextprime(p,t);
+ qDebug("generating q...");
+ ba=rf.read(64);
+ if(ba.size()<64){
+ qDebug("Exception: q<512 bit");
+ return -1;
+ }
+ ba.prepend((char)1);
+ mpz_set_str(t,ba.toHex().data(),16);
+ mpz_nextprime(q,t);
+ qDebug("calculating n...");
+ mpz_mul(n,p,q);
+ mpz_sub_ui(d,p,1);
+ mpz_sub_ui(e,q,1);
+ mpz_mul(t,d,e);
+ qDebug("testing e=3...");
+ mpz_set_ui(e,3);
+ mpz_gcd(d,t,e);
+ if(mpz_cmp_ui(d,1)>0){
+ qDebug("testing e=7...");
+ mpz_set_ui(e,3);
+ mpz_gcd(d,t,e);
+ if(mpz_cmp_ui(d,1)>0){
+ qDebug("testing failed, restarting...");
+ continue;
+ }
+ }
+ break;
+ }while(true);
+ qDebug("calculating d...");
+ mpz_invert(d,e,t);
+ qDebug("testing key...");
+ //generate a number p < n
+ QByteArray ba;
+ ba.fill('f',260);
+ mpz_set_str(t,ba.data(),16);
+ mpz_mod(p,t,n);
+ //encrypt it
+ mpz_powm(q,p,e,n);
+ //decrypt it
+ mpz_powm(t,q,d,n);
+ //compare
+ if(mpz_cmp(p,t)!=0){
+ qDebug("test failed!");
+ return -1;
+ }
+ qDebug("writing key.h...");
+ QFile fd("key.h");
+ if(!fd.open(QIODevice::WriteOnly|QIODevice::Truncate)){
+ qDebug("Error: cannot open key.h");
+ return -1;
+ }
+ char buf[1024];//more than enough for a 1024 bit number in hex
+ fd.write("//generated file, don't edit!\n#define RSA_N \"0x");
+ fd.write(mpz_get_str(buf,16,n));
+ fd.write("\"\n#define RSA_E \"0x");
+ fd.write(mpz_get_str(buf,16,e));
+ fd.write("\"\n#define RSA_D \"0x");
+ fd.write(mpz_get_str(buf,16,d));
+ fd.write("\"\n");
+ fd.close();
+ return 0;
+}
+
+#endif
--- /dev/null
+//
+// C++ Implementation: mkdist
+//
+// Description:
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2009
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+//
+
+#include <QCoreApplication>
+#include <QStringList>
+
+#include "distkey.h"
+
+int main(int argc,char**argv)
+{
+ QCoreApplication app(argc,argv);
+ DISTKEYGEN;
+
+ return app.exec();
+}