~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to checks/dolook2.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <vector>
2
 
#include <string>
3
 
#include <cstdlib>
4
 
 
5
 
#include <botan/botan.h>
6
 
#include <botan/lookup.h>
7
 
#include <botan/look_pk.h>
8
 
#include <botan/filters.h>
9
 
#include <botan/randpool.h>
10
 
#include <botan/x931_rng.h>
11
 
#include <botan/rng.h>
12
 
using namespace Botan;
13
 
 
14
 
/* A weird little hack to fit S2K algorithms into the validation suite
15
 
   You probably wouldn't ever want to actually use the S2K algorithms like
16
 
   this, the raw S2K interface is more convenient for actually using them
17
 
*/
18
 
class S2K_Filter : public Filter
19
 
   {
20
 
   public:
21
 
      void write(const byte in[], u32bit len)
22
 
         { passphrase += std::string((const char*)in, len); }
23
 
      void end_msg()
24
 
         {
25
 
         s2k->change_salt(salt, salt.size());
26
 
         s2k->set_iterations(iterations);
27
 
         SymmetricKey x = s2k->derive_key(outlen, passphrase);
28
 
         send(x.bits_of());
29
 
         }
30
 
      S2K_Filter(S2K* algo, const SymmetricKey& s, u32bit o, u32bit i)
31
 
         {
32
 
         s2k = algo;
33
 
         outlen = o;
34
 
         iterations = i;
35
 
         salt = s.bits_of();
36
 
         }
37
 
      ~S2K_Filter() { delete s2k; }
38
 
   private:
39
 
      std::string passphrase;
40
 
      S2K* s2k;
41
 
      SecureVector<byte> salt;
42
 
      u32bit outlen, iterations;
43
 
   };
44
 
 
45
 
/* Not too useful generally; just dumps random bits for benchmarking */
46
 
class RNG_Filter : public Filter
47
 
   {
48
 
   public:
49
 
      void write(const byte[], u32bit);
50
 
      RNG_Filter(RandomNumberGenerator* r) : rng(r), buffer(1024)
51
 
         {
52
 
         Global_RNG::randomize(buffer, buffer.size());
53
 
         rng->add_entropy(buffer, buffer.size());
54
 
         }
55
 
      ~RNG_Filter() { delete rng; }
56
 
   private:
57
 
      RandomNumberGenerator* rng;
58
 
      SecureVector<byte> buffer;
59
 
   };
60
 
 
61
 
class KDF_Filter : public Filter
62
 
   {
63
 
   public:
64
 
      void write(const byte in[], u32bit len)
65
 
         { secret.append(in, len); }
66
 
      void end_msg()
67
 
         {
68
 
         SymmetricKey x = kdf->derive_key(outlen,
69
 
                                          secret, secret.size(),
70
 
                                          salt, salt.size());
71
 
         send(x.bits_of(), x.length());
72
 
         }
73
 
      KDF_Filter(KDF* algo, const SymmetricKey& s, u32bit o)
74
 
         {
75
 
         kdf = algo;
76
 
         outlen = o;
77
 
         salt = s.bits_of();
78
 
         }
79
 
      ~KDF_Filter() { delete kdf; }
80
 
   private:
81
 
      SecureVector<byte> secret;
82
 
      SecureVector<byte> salt;
83
 
      KDF* kdf;
84
 
      u32bit outlen;
85
 
   };
86
 
 
87
 
Filter* lookup_s2k(const std::string& algname,
88
 
                   const std::vector<std::string>& params)
89
 
   {
90
 
   S2K* s2k = 0;
91
 
 
92
 
   try {
93
 
      s2k = get_s2k(algname);
94
 
      }
95
 
   catch(...) { }
96
 
 
97
 
   if(s2k)
98
 
      return new S2K_Filter(s2k, params[0], to_u32bit(params[1]),
99
 
                            to_u32bit(params[2]));
100
 
   return 0;
101
 
   }
102
 
 
103
 
void RNG_Filter::write(const byte[], u32bit length)
104
 
   {
105
 
   while(length)
106
 
      {
107
 
      u32bit gen = std::min(buffer.size(), length);
108
 
      rng->randomize(buffer, gen);
109
 
      length -= gen;
110
 
      }
111
 
   }
112
 
 
113
 
Filter* lookup_rng(const std::string& algname)
114
 
   {
115
 
   if(algname == "X9.31-RNG")
116
 
      return new RNG_Filter(new ANSI_X931_RNG);
117
 
   if(algname == "Randpool")
118
 
      return new RNG_Filter(new Randpool);
119
 
   return 0;
120
 
   }
121
 
 
122
 
Filter* lookup_kdf(const std::string& algname, const std::string& salt,
123
 
                   const std::string& params)
124
 
   {
125
 
   KDF* kdf = 0;
126
 
   try {
127
 
      kdf = get_kdf(algname);
128
 
      }
129
 
   catch(...) { return 0; }
130
 
 
131
 
   if(kdf)
132
 
      return new KDF_Filter(kdf, salt, to_u32bit(params));
133
 
   return 0;
134
 
   }