aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Makefile16
-rw-r--r--main.cpp240
3 files changed, 8 insertions, 250 deletions
diff --git a/.gitignore b/.gitignore
index 89805cd..cdd3cd9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,4 @@
*.o
*.dSYM
-main
+cryptolib.a
*.txt
diff --git a/Makefile b/Makefile
index 5778290..87bf616 100644
--- a/Makefile
+++ b/Makefile
@@ -1,28 +1,26 @@
CXX = g++
CXXFLAGS = -Wall -Wextra -std=c++0x -fwrapv
-LDFLAGS =
ifneq ($(DEBUG),)
CXXFLAGS += -g
else
CXXFLAGS += -O2
endif
-ifeq ($(shell uname),Linux)
- LDFLAGS += -lbsd
-endif
-BIN = main
+
+LIBNAME = cryptolib.a
+
.PHONY: all clean remake
-all: $(BIN)
+all: $(LIBNAME)
clean:
- rm -rf $(BIN) *.o *.dSYM
+ rm -rf $(LIBNAME) *.o
remake: clean all
-$(BIN): $(patsubst %.cpp,%.o,$(wildcard *.cpp))
- $(CXX) -o $@ $^ $(LDFLAGS)
+$(LIBNAME): $(patsubst %.cpp,%.o,$(wildcard *.cpp))
+ ar -cr $@ $^
%.o: %.cpp $(wildcard *.h)
$(CXX) $(CXXFLAGS) -c -o $@ $<
diff --git a/main.cpp b/main.cpp
deleted file mode 100644
index 2385da8..0000000
--- a/main.cpp
+++ /dev/null
@@ -1,240 +0,0 @@
-#include <iostream>
-#include <fstream>
-#include <sstream>
-#include <stdexcept>
-#include <algorithm>
-#include <cstdlib>
-#include <cctype>
-#include <ctime>
-#include <cassert>
-#include "aes.h"
-#include "base64.h"
-#include "bigint.h"
-#include "envelope.h"
-#include "numalgo.h"
-#include "primes.h"
-#include "rng.h"
-#include "rsa.h"
-
-using namespace std;
-
-class eof_error : public runtime_error{
-public:
- eof_error()
- :runtime_error("EOF"){}
-};
-
-int64_t rand64(){
- return ((int64_t)rand()<<32)+(((int64_t)rand()%2)<<31)+rand();
-}
-
-Bigint readevalexpr(istream &is){
- Bigint a;
- is>>a;
- if(is.eof())throw eof_error();
- // cerr<<"Read "<<a<<endl;
- if(!is.fail())return a;
- is.clear();
- string s;
- is>>s;
- assert(!is.fail());
- a=readevalexpr(is);
- Bigint b=readevalexpr(is);
- //cerr<<"Operation "<<s<<" on "<<a<<" and "<<b<<endl;
- if(s=="add")return a+b;
- else if(s=="sub")return a-b;
- else if(s=="mul")return a*b;
- else if(s=="div")return a.divmod(b).first;
- else if(s=="mod")return a.divmod(b).second;
- else {
- cerr<<"Unknown operation '"<<s<<'\''<<endl;
- assert(false);
- }
-}
-
-void biginttest(){
- srand(time(NULL));
-
- // cerr<<Bigint(599428191)*Bigint(10)<<endl;
- // cerr<<hex<<Bigint(599428191)*Bigint(10)<<endl;
-
-#if 1
- {
- Bigint bi;
- assert(RAND_MAX==(1U<<31)-1);
- for(int i=0;i<500000;i++){
- int64_t a=rand64(),b=rand64();
- if(a+b<0){i--; continue;}
- stringstream s1,s2,s3;
- s1<<a+b;
- s2<<Bigint(a+b);
- s3<<Bigint(a)+Bigint(b);
- assert(s1.str()==s2.str()&&s1.str()==s3.str());
- }
- }
-#endif
-
-#if 1
- {
- for(int i=0;i<1000;i++){
- int64_t n=rand64();
- istringstream ss(to_string(n));
- Bigint bi;
- ss>>bi;
- assert(bi==Bigint(n));
- }
- }
-#endif
-
-#if 1
- {
- string s="4405994068155852661780322209877856931246944549396705884037139443014164958640201650440984581318995014";
- istringstream iss(s);
- Bigint bi;
- iss>>bi;
- uint32_t digs[11]={1752788038,953502834,2175607868,1627159508,1754291416,1207689192,3196357285,3165170272,3313904421,3194703103,2062};
- for(int i=0;i<11;i++)assert(bi._digit(i)==digs[i]);
- ostringstream oss;
- oss<<bi;
- assert(oss.str()==s);
- }
-#endif
-}
-
-void repl(int argc,char **argv){
- istream *in;
- if(argc==2)in=new ifstream(argv[1]);
- else in=&cin;
- for(int i=0;;i++){
- try {
- cout<<readevalexpr(*in)<<endl;
- } catch(eof_error){
- break;
- }
- }
- if(in!=&cin)delete in;
-}
-
-void testisqrt(int argc,char **argv){
- int randsize=argc==2?strtol(argv[1],nullptr,10):1;
- assert(randsize>=1);
- for(int i=0;i<1000;i++){
- Bigint n(rand64());
- for(int j=1;j<randsize;j++){
- n<<=63;
- n+=rand64();
- }
- // cout<<hex<<n<<dec<<endl;
- Bigint root(isqrt(n));
- assert(root*root<=n);
- root+=Bigint::one;
- assert(root*root>n);
- }
-}
-
-void performrsa(){
- RSA::PrivateKey privkey;
- Bigint p(1000000007),q(3000000019U);
- privkey.pub.mod=(int64_t)3000000040000000133LL;
- privkey.pub.exp=65537;
- {
- Bigint x;
- egcd((p-Bigint::one)*(q-Bigint::one),privkey.pub.exp,x,privkey.pexp);
- }
- cout<<"d = "<<privkey.pexp<<endl;
- Bigint msg(123456789);
- cout<<"msg = "<<msg<<endl;
- Bigint encr=RSA::encrypt(msg,privkey.pub);
- cout<<"encr = "<<encr<<endl;
- Bigint msg2=RSA::decrypt(encr,privkey);
- cout<<"msg = "<<msg2<<endl;
-}
-
-void pseudolist(bool(*func)(const Bigint&)){
- fillsmallprimes();
- for(int i=2;i<65000;i++){
- // cerr<<"TRYING "<<i<<endl;
- bool actualprime=binary_search(smallprimes.begin(),smallprimes.end(),i);
- if(!func(Bigint(i))){
- if(actualprime){
- cerr<<"Test misleadingly said that "<<i<<" isn't prime, while it is!"<<endl;
- exit(1);
- }
- continue;
- }
- if(!actualprime)cout<<i<<' ';
- }
- cout<<endl;
-}
-
-void listprimes(){
- int n=0;
- Bigint x(3);
- x*=x;
- x*=x;
- x*=x;
- x*=x;
- x*=x;
- x*=x;
- x*=x;
- Bigint y(x+10000);
- cout<<x<<' '<<y<<endl;
- for(Bigint i(x);i<=y;i+=1){
- if(bailliePSW(i)){
- cout<<i<<endl;
- n++;
- }
- }
- cout<<n<<endl;
-}
-
-int main(int argc,char **argv){
- (void)argc;
- (void)argv;
- // biginttest();
- // repl(argc,argv);
- // performrsa();
- // testisqrt(argc,argv);
- // randprime(Bigint(20),Bigint(42));
- // strongLucasPrime(Bigint(5));
- // cout<<strongLucasPrime(Bigint(5777))<<endl;
- // pseudolist(strongPseudoPrime2);
- // pseudolist(strongLucasPrime);
- // listprimes();
-
- // Rng rng("wachtwoord");
- // for(int i=0;i<100000;i++)cout<<rng.get()<<endl;
-
- // for(int i=0;i<10000;i++)cout<<arc4random()<<endl;
-
- /*KeyRng rng("wachtwoord"); //for DieHarder
- char data[4];
- for(int i=0;i<10000000;i++){
- *(uint32_t*)data=rng.get();
- fwrite(data,1,4,stdout);
- }*/
-
- /*string s;
- while(true){
- char c=cin.get();
- if(!cin)break;
- s.push_back(c);
- }
- cout<<Base64::encode(s)<<endl;
- // cout<<Base64::decode(s)<<flush;*/
-
- // AES::test();
- // cout<<Base64::encode(AES::encrypt("goeiemorgen dit is leuke data enzoo","123456789abcdefx",AES::AES_128_CBC))<<endl;
- // cout<<AES::decrypt(Base64::decode("dv2N3n2FHsD7LU2PAZnZm/bLQQSaaZoDDFjy3VrSu2JCHN5KuyBlinlh9C71IGTbT4/WvRKLp6dp1TfrKXIG2w=="),"123456789abcdefx",AES::AES_128_CBC)<<endl;
-
- RSA::PrivateKey privkey(RSA::genkey(2048,"goeiemorgen"));
- cerr<<"privkey.pexp="<<privkey.pexp<<endl;
- // stringstream ss("146142527763115590918816237475833862275078269531224490080731579415134484175830244922030574137988310092068583287060550616357026989772917226684940867892409");
- // Bigint d;
- // ss>>d;
- // cout<<RSA::decrypt(d,privkey)<<endl;
- string crypt(Envelope::encrypt("hallo hallo dit is leuke data enzo",privkey.pub));
- cout<<Base64::encode(crypt)<<endl;
- string decr(Envelope::decrypt(crypt,privkey));
- cout<<decr<<endl;
-}