aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples/encrypt.cpp
blob: 8f53c85e3e0c442fc363b25ac9d5b42b19e3ce9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* (C) 2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

/*
Encrypt a file using a block cipher in CBC mode. Compresses the plaintext
with Zlib, MACs with HMAC(SHA-1). Stores the block cipher used in the file,
so you don't have to specify it when decrypting.

What a real application would do (and what this example should do), is test for
the presence of the Zlib module, and use it only if it's available. Then add
some marker to the stream so the other side knows whether or not the plaintext
was compressed. Bonus points for supporting multiple compression schemes.

Another flaw is that is stores the entire ciphertext in memory, so if the file
you're encrypting is 1 Gb... you better have a lot of RAM.
*/

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <memory>

#include <botan/botan.h>

#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
  #include <botan/zlib.h>
#endif

using namespace Botan;

std::string b64_encode(const SecureVector<byte>&);

int main(int argc, char* argv[])
   {
   if(argc < 2)
      {
      std::cout << "Usage: " << argv[0] << " [-c algo] -p passphrase file\n"
                   "   -p : Use this passphrase to encrypt\n"
                   "   -c : Encrypt with block cipher 'algo' (default 3DES)\n";
      return 1;
      }

   Botan::LibraryInitializer init;

   std::string algo = "TripleDES";
   std::string filename, passphrase;

   // Holy hell, argument processing is a PITA
   for(int j = 1; argv[j] != 0; j++)
      {
      if(std::strcmp(argv[j], "-c") == 0)
         {
         if(argv[j+1])
            {
            algo = argv[j+1];
            j++;
            }
         else
            {
            std::cout << "No argument for -c option" << std::endl;
            return 1;
            }
         }
      else if(std::strcmp(argv[j], "-p") == 0)
         {
         if(argv[j+1])
            {
            passphrase = argv[j+1];
            j++;
            }
         else
            {
            std::cout << "No argument for -p option" << std::endl;
            return 1;
            }
         }
      else
         {
         if(filename != "")
            {
            std::cout << "You can only specify one file at a time\n";
            return 1;
            }
         filename = argv[j];
         }
      }

   if(passphrase == "")
      {
      std::cout << "You have to specify a passphrase!" << std::endl;
      return 1;
      }

   std::ifstream in(filename.c_str(), std::ios::binary);
   if(!in)
      {
      std::cout << "ERROR: couldn't open " << filename << std::endl;
      return 1;
      }

   std::string outfile = filename + ".enc";
   std::ofstream out(outfile.c_str());
   if(!out)
      {
      std::cout << "ERROR: couldn't open " << outfile << std::endl;
      return 1;
      }

   try
      {
      if(!have_block_cipher(algo))
         {
         std::cout << "Don't know about the block cipher \"" << algo << "\"\n";
         return 1;
         }

      const u32bit key_len = max_keylength_of(algo);
      const u32bit iv_len = block_size_of(algo);

      AutoSeeded_RNG rng;

      std::auto_ptr<PBKDF> pbkdf(get_pbkdf("PBKDF2(SHA-1)"));

      SecureVector<byte> salt(8);
      rng.randomize(&salt[0], salt.size());

      const u32bit PBKDF2_ITERATIONS = 8192;

      SymmetricKey bc_key = pbkdf->derive_key(key_len, "BLK" + passphrase,
                                              &salt[0], salt.size(),
                                              PBKDF2_ITERATIONS);

      InitializationVector iv = pbkdf->derive_key(iv_len, "IVL" + passphrase,
                                                  &salt[0], salt.size(),
                                                  PBKDF2_ITERATIONS);

      SymmetricKey mac_key = pbkdf->derive_key(16, "MAC" + passphrase,
                                               &salt[0], salt.size(),
                                               PBKDF2_ITERATIONS);

      // Just to be all fancy we even write a (simple) header.
      out << "-------- ENCRYPTED FILE --------" << std::endl;
      out << algo << std::endl;
      out << b64_encode(salt) << std::endl;

      Pipe pipe(new Fork(
                   new Chain(new MAC_Filter("HMAC(SHA-1)", mac_key),
                             new Base64_Encoder
                      ),
                   new Chain(
#ifdef BOTAN_HAS_COMPRESSOR_ZLIB
                             new Zlib_Compression,
#endif
                             get_cipher(algo + "/CBC", bc_key, iv, ENCRYPTION),
                             new Base64_Encoder(true)
                      )
                   )
         );

      pipe.start_msg();
      in >> pipe;
      pipe.end_msg();

      out << pipe.read_all_as_string(0) << std::endl;
      out << pipe.read_all_as_string(1);

      }
   catch(Algorithm_Not_Found)
      {
      std::cout << "Don't know about the block cipher \"" << algo << "\"\n";
      return 1;
      }
   catch(std::exception& e)
      {
      std::cout << "Exception caught: " << e.what() << std::endl;
      return 1;
      }
   return 0;
   }

std::string b64_encode(const SecureVector<byte>& in)
   {
   Pipe pipe(new Base64_Encoder);
   pipe.process_msg(in);
   return pipe.read_all_as_string();
   }