aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-10-08 21:57:51 +0200
committertomsmeding <tom.smeding@gmail.com>2016-10-08 21:57:59 +0200
commit93b1a18fd61890eb4976a7f52d17e0990ccca661 (patch)
tree50cb66ad5dcc6c098bea1422a1237dfbefb7d65a
parent27472e604eeb74b3b60313d38d537c7e0e83151b (diff)
Fix some serious bugs with casting
-rw-r--r--bigint.cpp24
-rw-r--r--bigint.h4
-rw-r--r--rsa.cpp4
3 files changed, 24 insertions, 8 deletions
diff --git a/bigint.cpp b/bigint.cpp
index 639d263..8cd06ab 100644
--- a/bigint.cpp
+++ b/bigint.cpp
@@ -16,8 +16,15 @@ Bigint Bigint::two(2);
Bigint::Bigint()
:sign(1){}
+Bigint::Bigint(Bigint &&o)
+ :digits(move(o.digits)),sign(o.sign){
+ o.normalise();
+ checkconsistent();
+}
+
Bigint::Bigint(const string &repr){
stringstream(repr)>>*this;
+ checkconsistent();
}
Bigint::Bigint(slongdigit_t v)
@@ -145,6 +152,15 @@ void Bigint::checkconsistent(){
assert(digits.size()!=0||sign==1);
}
+Bigint& Bigint::operator=(Bigint &&o){
+ sign=o.sign;
+ digits=move(o.digits);
+ o.normalise();
+ normalise();
+ checkconsistent();
+ return *this;
+}
+
Bigint& Bigint::operator=(slongdigit_t v){
digits.clear();
if(v==0){
@@ -498,21 +514,21 @@ string Bigint::serialiseMantissa() const {
int sz=digits.size();
for(int i=0;i<sz;i++){
for(int j=0;j<(int)sizeof(digit_t);j++){
- s[i*sizeof(digit_t)+j]=(digits[i]>>(8*j))&255;
+ s[i*sizeof(digit_t)+j]=(digits[i]>>(8*j))&0xff;
}
}
return s;
}
void Bigint::deserialiseMantissa(const string &s){
- assert(s.size()%4==0);
+ assert(s.size()%sizeof(digit_t)==0);
sign=1;
- int sz=s.size()/4;
+ int sz=s.size()/sizeof(digit_t);
digits.resize(sz);
for(int i=0;i<sz;i++){
digits[i]=0;
for(int j=0;j<(int)sizeof(digit_t);j++){
- digits[i]|=s[i*sizeof(digit_t)+j]<<(8*j);
+ digits[i]|=(uint8_t)s[i*sizeof(digit_t)+j]<<(8*j);
}
}
shrink();
diff --git a/bigint.h b/bigint.h
index 1e8515a..a4c29d6 100644
--- a/bigint.h
+++ b/bigint.h
@@ -31,7 +31,7 @@ private:
public:
Bigint();
Bigint(const Bigint&)=default;
- Bigint(Bigint&&)=default;
+ Bigint(Bigint&&);
explicit Bigint(const std::string&);
explicit Bigint(sdigit_t);
explicit Bigint(digit_t);
@@ -39,7 +39,7 @@ public:
explicit Bigint(longdigit_t);
Bigint& operator=(const Bigint&)=default;
- Bigint& operator=(Bigint&&)=default;
+ Bigint& operator=(Bigint&&);
Bigint& operator=(slongdigit_t);
Bigint& operator=(longdigit_t);
Bigint& operator=(sdigit_t);
diff --git a/rsa.cpp b/rsa.cpp
index 77e1350..e1bf3a8 100644
--- a/rsa.cpp
+++ b/rsa.cpp
@@ -51,8 +51,8 @@ namespace RSA{
PrivateKey importkey(const string &pub,const string &priv){
string pubdeser=Base64::decode(pub);
assert(pubdeser.size()>4);
- int modlen=(unsigned char)(pubdeser[0])+(unsigned char)(pubdeser[1]<<8)+
- (unsigned char)(pubdeser[2]<<16)+(unsigned char)(pubdeser[3]<<24);
+ int modlen=(uint8_t)pubdeser[0]+((uint8_t)pubdeser[1]<<8)+
+ ((uint8_t)pubdeser[2]<<16)+((uint8_t)pubdeser[3]<<24);
assert((int)pubdeser.size()-4>modlen);
PrivateKey key;
key.pub.mod.deserialiseMantissa(string(pubdeser.begin()+4,pubdeser.begin()+(4+modlen)));