On Tue, 2003-11-18 at 19:32, Stephen torri wrote:
> On Tue, 2003-11-18 at 19:14, Scott Barnhart wrote:
> > Stephen,
> > 
> > Based on the code in test.cpp for GzipFile and GunzipFile, I have always
> > used the following, and it should work for you:
> > 
> > // Gzip contents of plaintext into gzip_text;
> > StringSource(plaintext, true, new Gzip(new StringSink(gzip_text))); 
> > 
> > // Ungzip contents of gzip_text into unzip_text;
> > StringSource(gzip_text, true, new Gunzip(new StringSink(unzip_text))); 
> > 

I just decided to run the code as it stands. It core dumps on the Gunzip
line. Here is the backtrace:

#0  0x47355e81 in kill () from /lib/libc.so.6
#1  0x47355c25 in raise () from /lib/libc.so.6
#2  0x4735719b in abort () from /lib/libc.so.6
#3  0x472d3837 in __cxxabiv1::__terminate(void (*)()) ()
   from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/libstdc++.so.5
#4  0x472d3878 in std::terminate() ()
   from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/libstdc++.so.5
#5  0x472d3a1e in __cxa_throw ()
   from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/libstdc++.so.5
#6  0x470e690f in CryptoPP::Inflator::OutputPast(unsigned, unsigned) (
    this=0x8065450, length=10, distance=52) at zinflate.cpp:261
#7  0x470e822e in CryptoPP::Inflator::DecodeBody() (this=0x8065450)
    at zinflate.cpp:558
#8  0x470e6fb0 in CryptoPP::Inflator::ProcessInput(bool)
(this=0x8065450,
    flush=true) at zinflate.cpp:345
#9  0x470e6c39 in CryptoPP::Inflator::Put2(unsigned char const*,
unsigned, int, bool) (this=0x8065450, inString=0x0, length=0,
messageEnd=-1, blocking=true)
    at zinflate.cpp:294
#10 0x46f6dd39 in
CryptoPP::BufferedTransformation::ChannelPut2(std::string const&,
unsigned char const*, unsigned, int, bool) (this=0x8065450,
    [EMAIL PROTECTED], begin=0x0, length=0, messageEnd=-1,
blocking=true)
    at cryptlib.cpp:189
#11 0x46f719ec in
CryptoPP::BufferedTransformation::ChannelMessageEnd(std::string const&,
int, bool) (this=0x8065450, [EMAIL PROTECTED], propagation=-1,
    blocking=true) at cryptlib.h:881
#12 0x46f6e88c in
CryptoPP::BufferedTransformation::TransferMessagesTo2(CryptoPP::BufferedTransformation&,
 unsigned&, std::string const&, bool) (
    this=0xb76b3204, [EMAIL PROTECTED], [EMAIL PROTECTED],
    [EMAIL PROTECTED], blocking=true) at cryptlib.cpp:354
#13 0x46f6ead0 in
CryptoPP::BufferedTransformation::TransferAllTo2(CryptoPP::BufferedTransformation&, 
std::string const&, bool) (this=0xb76b3204,
---Type <return> to continue, or q <return> to quit---
    [EMAIL PROTECTED], [EMAIL PROTECTED], blocking=true) at
cryptlib.cpp:395
#14 0x08058a35 in
CryptoPP::SourceTemplate<CryptoPP::StringStore>::PumpAll2(bool) ()
#15 0x0805948f in CryptoPP::Source::PumpAll() ()
#16 0x0805809a in CryptoPP::Source::SourceInitialize(bool,
CryptoPP::NameValuePairs const&) ()
#17 0x0805314e in
CryptoPP::StringSource::StringSource<std::string>(std::string const&,
bool, CryptoPP::BufferedTransformation*) ()
#18 0x080518e9 in AES_CFB_CipherEngine::encrypt(std::string) ()
#19 0x080527a0 in save(AES_CFB_CipherEngine&, std::string, std::string)
()
#20 0x08052a26 in main ()
#21 0x473427a7 in __libc_start_main () from /lib/libc.so.6

Gcc: 3.2.3
Crypto++: 5.1

-----------------
strace
-----------------
bash-2.05b$ strace ./encryption_test
execve("./encryption_test", ["./encryption_test"], [/* 56 vars */]) = 0
uname({sys="Linux", node="base.torri.org", ...}) = 0
brk(0)                                  = 0x8062660
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0x4082b000
open("/etc/ld.so.preload", O_RDONLY)    = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
close(3)                                = 0
open("cryptopp/.libs/i686/mmx/libcryptopp.so.0", O_RDONLY) = -1 ENOENT
(No such file or directory)
open("cryptopp/.libs/i686/libcryptopp.so.0", O_RDONLY) = -1 ENOENT (No
such file or directory)
open("cryptopp/.libs/mmx/libcryptopp.so.0", O_RDONLY) = -1 ENOENT (No
such file or directory)
open("cryptopp/.libs/libcryptopp.so.0", O_RDONLY) = 3
read(3, "[EMAIL PROTECTED],\000"...,
1024) = 1024
fstat64(3, {st_mode=S_IFREG|0755, st_size=15678342, ...}) = 0
getcwd("/home/storri/Documents/StephenTorri/cs502_project/src", 128) =
54
mmap2(NULL, 5857216, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) =
0x4082c000
mprotect(0x40d4e000, 475072, PROT_NONE) = 0
mmap2(0x40d4e000, 466944, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED,
3, 0x521) = 0x40d4e000
mmap2(0x40dc0000, 8128, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x40dc0000
close(3)                                = 0
open("cryptopp/.libs/i686/mmx/libstdc++.so.5", O_RDONLY) = -1 ENOENT (No
such file or directory)
open("cryptopp/.libs/i686/libstdc++.so.5", O_RDONLY) = -1 ENOENT (No
such file or directory)
open("cryptopp/.libs/mmx/libstdc++.so.5", O_RDONLY) = -1 ENOENT (No such
file or directory)
open("cryptopp/.libs/libstdc++.so.5", O_RDONLY) = -1 ENOENT (No such
file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=99875, ...}) = 0
mmap2(NULL, 99875, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40dc2000
close(3)                                = 0
open("/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/libstdc++.so.5",
O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0p\0\4\000"...,
1024) = 1024
fstat64(3, {st_mode=S_IFREG|0755, st_size=1068068, ...}) = 0
mmap2(NULL, 830140, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x40ddb000
mprotect(0x40e9c000, 39612, PROT_NONE)  = 0
mmap2(0x40e9c000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3,
0xc1) = 0x40e9c000
mmap2(0x40ea1000, 19132, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x40ea1000
close(3)                                = 0
open("cryptopp/.libs/i686/mmx/libm.so.6", O_RDONLY) = -1 ENOENT (No such
file or directory)
open("cryptopp/.libs/i686/libm.so.6", O_RDONLY) = -1 ENOENT (No such
file or directory)
open("cryptopp/.libs/mmx/libm.so.6", O_RDONLY) = -1 ENOENT (No such file
or directory)
open("cryptopp/.libs/libm.so.6", O_RDONLY) = -1 ENOENT (No such file or
directory)
open("/lib/libm.so.6", O_RDONLY)        = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\2405\0"...,
1024) = 1024
fstat64(3, {st_mode=S_IFREG|0755, st_size=189625, ...}) = 0
mmap2(NULL, 135616, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x40ea6000
mprotect(0x40ec7000, 448, PROT_NONE)    = 0
mmap2(0x40ec7000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3,
0x21) = 0x40ec7000
close(3)                                = 0
open("cryptopp/.libs/i686/mmx/libgcc_s.so.1", O_RDONLY) = -1 ENOENT (No
such file or directory)
open("cryptopp/.libs/i686/libgcc_s.so.1", O_RDONLY) = -1 ENOENT (No such
file or directory)
open("cryptopp/.libs/mmx/libgcc_s.so.1", O_RDONLY) = -1 ENOENT (No such
file or directory)
open("cryptopp/.libs/libgcc_s.so.1", O_RDONLY) = -1 ENOENT (No such file
or directory)
open("/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/libgcc_s.so.1", O_RDONLY)
= 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0 \27\0\000"...,
1024) = 1024
fstat64(3, {st_mode=S_IFREG|0644, st_size=153827, ...}) = 0
mmap2(NULL, 35744, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x40ec8000
mprotect(0x40ed0000, 2976, PROT_NONE)   = 0
mmap2(0x40ed0000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3,
0x7) = 0x40ed0000
close(3)                                = 0
open("cryptopp/.libs/i686/mmx/libc.so.6", O_RDONLY) = -1 ENOENT (No such
file or directory)
open("cryptopp/.libs/i686/libc.so.6", O_RDONLY) = -1 ENOENT (No such
file or directory)
open("cryptopp/.libs/mmx/libc.so.6", O_RDONLY) = -1 ENOENT (No such file
or directory)
open("cryptopp/.libs/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or
directory)
open("/lib/libc.so.6", O_RDONLY)        = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0`X\1\000"...,
1024) = 1024
fstat64(3, {st_mode=S_IFREG|0755, st_size=1466302, ...}) = 0
mmap2(NULL, 1231748, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) =
0x40ed1000
mprotect(0x40ff8000, 23428, PROT_NONE)  = 0
mmap2(0x40ff8000, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3,
0x126) = 0x40ff8000
mmap2(0x40ffc000, 7044, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x40ffc000
close(3)                                = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0x40ffe000
munmap(0x40dc2000, 99875)               = 0
open("/dev/urandom", O_RDONLY)          = 3
read(3, "\3\7#\375\32\331\274o\352/\205w\6\222\375\221\267\342\310"...,
32) = 32
close(3)                                = 0
brk(0)                                  = 0x8062660
brk(0x8063660)                          = 0x8063660
brk(0x8064000)                          = 0x8064000
open("Foo.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
brk(0)                                  = 0x8064000
brk(0x8065000)                          = 0x8065000
brk(0)                                  = 0x8065000
brk(0x8066000)                          = 0x8066000
fstat64(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(136, 1), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0x40dc2000
write(1, "Plaintext char:  B  o  o     H  "..., 83Plaintext char:  B  o 
o     H  o  o     I     a  m     s  o     s  c  a  r  e  d
) = 83
write(1, "Plaintext hex:  0x42  0x6f  0x6f"..., 148Plaintext hex:  0x42 
0x6f  0x6f  0x20  0x48  0x6f  0x6f  0x20  0x49  0x20  0x61  0x6d  0x20 
0x73  0x6f  0x20  0x73  0x63  0x61  0x72  0x65  0x64
) = 148
brk(0)                                  = 0x8066000
brk(0x8067000)                          = 0x8067000
brk(0)                                  = 0x8067000
brk(0x8077000)                          = 0x8077000
brk(0)                                  = 0x8077000
brk(0x8087000)                          = 0x8087000
brk(0)                                  = 0x8087000
brk(0x8097000)                          = 0x8097000
brk(0)                                  = 0x8097000
brk(0x80a7000)                          = 0x80a7000
brk(0)                                  = 0x80a7000
brk(0x80a8000)                          = 0x80a8000
write(1, "Compressed text2:  0x1f  0xfffff"..., 318Compressed text2: 
0x1f  0xffffff8b  0x8  0  0  0  0  0  0  0  0x73  0xffffffca 
0xffffffcf  0x57  0xfffffff0  0  0x62  0x4f  0xffffff85  0xffffffc4 
0x5c  0xffffff85  0xffffffe2  0x7c  0xffffff85  0xffffffe2  0xffffffe4 
0xffffffc4  0xffffffa2  0xffffffd4  0x14  0  0x42  0xffffff8f  0x7c 
0xffffffd0  0x16  0  0  0
) = 318
rt_sigprocmask(SIG_UNBLOCK, [ABRT], NULL, 8) = 0
getpid()                                = 15300
kill(15300, SIGABRT)                    = 0
--- SIGABRT (Aborted) @ 0 (0) ---
+++ killed by SIGABRT +++

The code file is attached. Curious.

Stephen
#include <modes.h>
#include <aes.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <boost/format.hpp>
#include <base64.h>
#include <filters.h>
#include <sha.h>
#include <gzip.h>

using namespace std;
using namespace CryptoPP;
using boost::format;
using boost::io::group;
using boost::io::str;

/*---------------------
 * CipherText class
 *---------------------*/
class CipherText {
 public:
  static void hex_output (std::string prefix, std::string input);
  static void char_output (std::string prefix, std::string input);
};

void
CipherText::hex_output (string prefix, string input) {
  cout << prefix;
  for (unsigned int i = 0; i < input.size(); ++i) {
    cout << format(" %1% ") % group(hex, showbase, (int)input[i]);
  }
  cout << endl;
}

void
CipherText::char_output (string prefix, string input) {
  cout << prefix;
  for (unsigned int i = 0; i < input.size(); ++i) {
    cout << format(" %1% ") % group(hex, showbase, input[i]);
  }
  cout << endl;
}

/*---------------------
 * AES_CFB_CipherEngien class
 *---------------------*/
class AES_CFB_CipherEngine {
 public:
  AES_CFB_CipherEngine (std::string cipher_name, unsigned int cipher_type);
  virtual ~AES_CFB_CipherEngine ();
  virtual std::string encrypt (std::string text);
  virtual std::string decrypt (std::string text);
  virtual std::string algorithm_name (void) {
    return m_algorithm_name;
  }
  virtual unsigned int algorithm_type (void) {
    return m_algorithm_type;
  }

 private:
  static const byte m_encryKey[32];
  static const byte m_iv[8];
  std::string m_algorithm_name;
  unsigned int m_algorithm_type;
};


const byte AES_CFB_CipherEngine::m_encryKey[32] = {
    0x3F, 0x6F, 0x6B, 0x69, 0x20, 0x5E, 0x5F, 0x34, 0x3F,
    0x6F, 0x6B, 0x69, 0x20, 0x5E, 0x5F, 0x34, 0x3F, 0x6F,
    0x6B, 0x69, 0x20, 0x5E, 0x5F, 0x34, 0x3F, 0x6F, 0x6B,
    0x69, 0x20, 0x5E, 0x5F, 0x34};

const byte AES_CFB_CipherEngine::m_iv[8] = {0x48, 0x34, 0x95, 0xA4, 0x49, 0xFF, 0x0F, 0x19};

AES_CFB_CipherEngine::AES_CFB_CipherEngine (string cipher_name, unsigned int cipher_type) 
  : m_algorithm_name (cipher_name),
    m_algorithm_type (cipher_type)
{}

AES_CFB_CipherEngine::~AES_CFB_CipherEngine (){}

std::string AES_CFB_CipherEngine::encrypt (std::string plaintext) {

  stringstream cipherstream;
  std::string ciphertext;
 
  try {
    
    CipherText::char_output ("Plaintext char: ", plaintext);
    CipherText::hex_output ("Plaintext hex: ", plaintext);

    /*
      plaintext -> compressedtext
      compresstext -> ciphertext
      ciphertext + MAC -> verified ciphertext
      verified ciphertext -> base64 encoded text
      base64 encoded text -> write to file
    */

    /* plaintext->compresstext */
    /* This is a guess. I am not sure. */
    string gzip_text;
    StringSource (plaintext, true, new Gzip (new StringSink (gzip_text)));

    /*
    aes_compressor2.Put(reinterpret_cast<const unsigned char*> (plaintext.data()), plaintext.length());
    aes_compressor2.MessageEnd();
    aes_compressor2.MessageSeriesEnd();
    */


    CipherText::hex_output ("Compressed text2: ", gzip_text);

    string unzip_text;
    StringSource (gzip_text, true, new Gunzip (new StringSink (unzip_text)));

    /*
    aes_decompressor2.Put ((const byte*)(gzip_text.c_str()),gzip_text.length());
    aes_decompressor2.MessageEnd();
    aes_decompressor2.MessageSeriesEnd();
    */

    CipherText::hex_output ("Decompressed text2: ", unzip_text);
    /* compressedtext -> encryptedtext */

    /* This does:
       compressedtext -> ciphertext
    */
    /* This does:
       plaintext -> compressedtext
    */
    
    /* This does
       MAC
    */

    /* This does
       ciphertext + MAC -> verified ciphertext
    */

    /* This does
       verified cipher text -> base64 encoded text
    */

    CipherText::char_output ("Ciphertext char: " , ciphertext);
    CipherText::hex_output ("Ciphertext hex: ", ciphertext);
  }
    
  catch ( InvalidDataFormat err ) {
    cout << "AES_CFB_CipherEngine FAILURE" << endl;
    cout << "Cipher text length: " << ciphertext.size() << endl;
    cout << "Plain text length:  " << plaintext.size() << endl;
    cout << "InvalidDataFormat: " << err.GetWhat() << endl;
    exit(0);
  }

  return ciphertext; 

}
    
std::string AES_CFB_CipherEngine::decrypt (std::string ciphertext) {

  std::string plaintext;

  try {

    CipherText::char_output ("Ciphertext char: " , ciphertext);
    CipherText::hex_output ("Ciphertext hex: ", ciphertext);

    /*
      read from file -> base64_encoded_text
      base64_encoded_text -> ciphertext + MAC
      ciphertext + MAC(size=64) -> verified ciphertext
      verified ciphertext -> compressedtext
      compressedtext -> plaintext
    */

    /* This does:
       Get MAC from file ciphertext + MAC string (size=64)
    */

    /* This does:
       Calculate MAC for cipher_text
    */

    CipherText::hex_output ("Plaintext hex: ", plaintext);
    CipherText::char_output ("Plaintext char: ", plaintext);
  }
    
  catch ( InvalidDataFormat err ) {
    cout << "AES_CFB_CipherEngine FAILURE" << endl;
    cout << "Cipher text length: " << ciphertext.length() << endl;
    cout << "Plain text length:  " << plaintext.length() << endl;
    cout << "InvalidDataFormat: " << err.GetWhat() << endl;
    exit(0);
  }

  return plaintext; 
}

/*---------------------
 * general functions
 *---------------------*/
void load (AES_CFB_CipherEngine& m_cipher, string filename) {

  ifstream input_file;
  stringstream input_data;

  input_file.open (filename.c_str(), ios::binary);

  if (input_file.is_open()) {
    string input_line;
    while (! input_file.eof()) {
      std::getline (input_file, input_line);
      input_data << input_line;
    }
    input_file.close();

    /* Line Feed (0xa) is ignored when using std::getline */
    input_data << (char)10;
    string plain_text = m_cipher.decrypt ( input_data.str() );
    cout << "Decrypted output:" << endl;
    cout << plain_text << endl;
  }
  else {
    cout << "Failed to load " << filename << endl;
    exit(0);
  }
}

void save (AES_CFB_CipherEngine& m_cipher, string filename, string plaintext) {

  ofstream output_file;
  output_file.open (filename.c_str(), ios::binary);
  if (output_file.is_open()) {
    string cipher_text = m_cipher.encrypt ( plaintext );
    output_file << cipher_text << endl;
  }
  else {
    cout << "Failed to save " << filename << endl;
    exit(0);
  }
}

int main () {
  string filename = "Foo.txt";
  string plaintext = "Boo Hoo I am so scared";
  AES_CFB_CipherEngine m_cipher ("AES (CFB)", 0);

  save (m_cipher, filename, plaintext);
  load (m_cipher, filename);
  
  return 0;
}

Reply via email to