I'm making a program that will allow the user to use multiple ciphers
together (twofish -> aes -> etc) I would store the encryption they
wanted into a vector and store the order and encryption methods in the
file. The problem is I'm not quite sure how to implement this. this is
what i have so far. I though it would work but it keeps segfaulting in
crypto++

gdb's output = 0x00007ffff762be06 in
CryptoPP::Rijndael_Enc_AdvancedProcessBlocks(void*, unsigned int
const*) () from /usr/lib/libcrypto++.so.8

@note i'm not good with gdb just the basics

#include <cstdio>
#include <iostream>
#include <fstream>
#include <fcntl.h>
#include <algorithm>

#include <boost/program_options.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/algorithm/string.hpp>

#include <cryptopp/sha.h>
#include <cryptopp/hex.h>
#include <cryptopp/filters.h>
#include <cryptopp/files.h>
#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <cryptopp/modexppc.h>
#include <cryptopp/hmac.h>
#include <cryptopp/osrng.h>
#include <cryptopp/rng.h>
#include <cryptopp/pwdbased.h>
#include <cryptopp/base64.h>
using namespace CryptoPP;

using namespace std;
using namespace boost;
namespace po = boost::program_options;

namespace std
{
        template<class A1, class A2>
        ostream& operator<<(ostream& s, vector<A1, A2> const& vec)
        {
                if(vec.size() > 0) s << vec.at(0);
                for(size_t i = 1; i < vec.size(); i++)
                        s << "," << vec.at(i);
                return s;
        }
}
//TODO split all of this into different files
int main(int argc, char * argv[])
{
        /** just for demo */
        bool encrypt = true;
        if(encrypt)
        {
                AutoSeededRandomPool rng;
                SecByteBlock salt1(64), salt2(64), passwd(10);
                rng.GenerateBlock(salt1,salt1.size());
                rng.GenerateBlock(salt2,salt2.size());
                rng.GenerateBlock(passwd,passwd.size());

                /** key derivation function */
                PKCS5_PBKDF2_HMAC<SHA512> pbkdf;

                /** derived key (storage) */
                SecByteBlock derived(512);
                pbkdf.DeriveKey(derived,derived.size(),
0,passwd,passwd.size(),salt1,salt1.size(),10);

                /** just for demo */
                vector<string> algs;
                algs.push_back("aes");
//              algs.push_back("twofish"); //TODO add this

                SecByteBlock iv(AES::BLOCKSIZE);
                rng.GenerateBlock(iv,iv.size());

                typedef boost::shared_ptr<StreamTransformation> TransPtr;
                typedef boost::shared_ptr<BlockCipher> BlockCPtr;
                vector< TransPtr > tranformations;
                vector< BlockCPtr > blockciphers;

                BufferedTransformation * prev = new FileSink(cout);
                for(size_t i = 0; i < algs.size(); i++)
                {
                        BlockCipher * cipher = NULL;
                        if(iequals("aes",algs.at(i)))
                        {
                                cerr << "using cipher 'aes'" << endl;
                                AES::Encryption * aes = new AES::Encryption();
                                blockciphers.push_back(BlockCPtr(aes));
                                cipher = aes;
                        }
                        else /** invalid cipher */
                        {
                                cerr << "invalid cipher '" << algs.at(i) << "'" 
<< endl;
                                exit(1);
                        }

                        if(cipher)
                        {
                                StreamTransformation * filt = NULL;
                                string mode = "ofb";
                                if(iequals("ofb",mode))
                                {
                                        cerr << "using block cipher mode 'ofb'" 
<< endl;
                                        OFB_Mode_ExternalCipher::Encryption * 
enc =
                                                new 
OFB_Mode_ExternalCipher::Encryption(*cipher,iv);
                                        filt = enc;
                                }
                                else /** invalid mode */
                                {
                                        cerr << "invalid cipher mode of 
operation '" << mode << "'" <<
endl;
                                        exit(1);
                                }

                                if(filt)
                                {
                                        cerr << "adding filter @ " << std::hex 
<< filt << endl;
                                        
tranformations.push_back(TransPtr(filt));
                                        prev = new 
StreamTransformationFilter(*filt,prev);
                                        cerr << "prev = " << std::hex << prev 
<< endl;
                                }
                        }
                }
                cerr << "alg = " << prev->AlgorithmName() << endl;
                cerr << "prev = " << std::hex << prev << endl;
                /** demo only */
                int fd = fileno(stdin);
                boost::iostreams::file_descriptor_source fdsrc(fd);
                
boost::iostreams::stream<boost::iostreams::file_descriptor_source>
ifstm(fdsrc);
                std::istream & std_ifstm = ifstm;
                FileSource(std_ifstm,true,prev);
        }
        else
        {
                cerr << "wtf... unknown mode" << endl;
        }

        return 0;
}

there  are uneeded header files included but thats because alot of
code has been removed. I test and compiled this using this makefile
and it crashes

TARGET = tomb-dbg
OBJS =  main.o

CFLAGS = -O2 -g -Wall
CXXFLAGS =      $(CFLAGS) -fmessage-length=0
ASFLAGS = $(CFLAGS)

INCS = -I/usr/include -I/usr/local/include -I.
LIBS = -lboost_program_options -lboost_iostreams -lcrypto++

CFLAGS += $(INCS)

$(TARGET):      $(OBJS)
        $(CXX) -o $(TARGET) $(OBJS) $(LIBS)

all:    $(TARGET)

clean:
        rm -f $(OBJS) $(TARGET)

-- 
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to cryptopp-users-unsubscr...@googlegroups.com.
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.

Reply via email to