monotone

monotone Mtn Source Tree

Root/botan/def_alg.cpp

1/*************************************************
2* Default Engine Algorithms Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/eng_def.h>
7#include <botan/lookup.h>
8#include <botan/parsing.h>
9
10#include <botan/aes.h>
11#include <botan/des.h>
12
13#include <botan/arc4.h>
14
15#include <botan/crc32.h>
16#include <botan/sha160.h>
17#include <botan/sha256.h>
18
19#include <botan/hmac.h>
20
21#include <botan/mode_pad.h>
22#include <botan/pkcs5.h>
23
24namespace Botan {
25
26/*************************************************
27* Some macros to simplify control flow *
28*************************************************/
29#define HANDLE_TYPE_NO_ARGS(NAME, TYPE) \
30 if(algo_name == NAME) \
31 { \
32 if(name.size() == 1) \
33 return new TYPE; \
34 throw Invalid_Algorithm_Name(algo_spec); \
35 }
36
37#define HANDLE_TYPE_ONE_U32BIT(NAME, TYPE, DEFAULT) \
38 if(algo_name == NAME) \
39 { \
40 if(name.size() == 1) \
41 return new TYPE(DEFAULT); \
42 if(name.size() == 2) \
43 return new TYPE(to_u32bit(name[1])); \
44 throw Invalid_Algorithm_Name(algo_spec); \
45 }
46
47#define HANDLE_TYPE_TWO_U32BIT(NAME, TYPE, DEFAULT) \
48 if(algo_name == NAME) \
49 { \
50 if(name.size() == 1) \
51 return new TYPE(DEFAULT); \
52 if(name.size() == 2) \
53 return new TYPE(to_u32bit(name[1])); \
54 if(name.size() == 3) \
55 return new TYPE(to_u32bit(name[1]), to_u32bit(name[2])); \
56 throw Invalid_Algorithm_Name(algo_spec); \
57 }
58
59#define HANDLE_TYPE_ONE_STRING(NAME, TYPE) \
60 if(algo_name == NAME) \
61 { \
62 if(name.size() == 2) \
63 return new TYPE(name[1]); \
64 throw Invalid_Algorithm_Name(algo_spec); \
65 }
66
67/*************************************************
68* Look for an algorithm with this name *
69*************************************************/
70BlockCipher*
71Default_Engine::find_block_cipher(const std::string& algo_spec) const
72 {
73 std::vector<std::string> name = parse_algorithm_name(algo_spec);
74 if(name.empty())
75 return 0;
76 const std::string algo_name = deref_alias(name[0]);
77
78 HANDLE_TYPE_NO_ARGS("AES", AES);
79 HANDLE_TYPE_NO_ARGS("AES-128", AES_128);
80 HANDLE_TYPE_NO_ARGS("AES-192", AES_192);
81 HANDLE_TYPE_NO_ARGS("AES-256", AES_256);
82 HANDLE_TYPE_NO_ARGS("DES", DES);
83 HANDLE_TYPE_NO_ARGS("DESX", DESX);
84 HANDLE_TYPE_NO_ARGS("TripleDES", TripleDES);
85
86 return 0;
87 }
88
89/*************************************************
90* Look for an algorithm with this name *
91*************************************************/
92StreamCipher*
93Default_Engine::find_stream_cipher(const std::string& algo_spec) const
94 {
95 std::vector<std::string> name = parse_algorithm_name(algo_spec);
96 if(name.empty())
97 return 0;
98 const std::string algo_name = deref_alias(name[0]);
99
100 HANDLE_TYPE_ONE_U32BIT("ARC4", ARC4, 0);
101 HANDLE_TYPE_ONE_U32BIT("RC4_drop", ARC4, 768);
102
103 return 0;
104 }
105
106/*************************************************
107* Look for an algorithm with this name *
108*************************************************/
109HashFunction*
110Default_Engine::find_hash(const std::string& algo_spec) const
111 {
112 std::vector<std::string> name = parse_algorithm_name(algo_spec);
113 if(name.empty())
114 return 0;
115 const std::string algo_name = deref_alias(name[0]);
116
117 HANDLE_TYPE_NO_ARGS("CRC32", CRC32);
118 HANDLE_TYPE_NO_ARGS("SHA-160", SHA_160);
119 HANDLE_TYPE_NO_ARGS("SHA-256", SHA_256);
120 return 0;
121 }
122
123/*************************************************
124* Look for an algorithm with this name *
125*************************************************/
126MessageAuthenticationCode*
127Default_Engine::find_mac(const std::string& algo_spec) const
128 {
129 std::vector<std::string> name = parse_algorithm_name(algo_spec);
130 if(name.empty())
131 return 0;
132 const std::string algo_name = deref_alias(name[0]);
133
134 HANDLE_TYPE_ONE_STRING("HMAC", HMAC);
135
136 return 0;
137 }
138
139/*************************************************
140* Look for an algorithm with this name *
141*************************************************/
142S2K* Default_Engine::find_s2k(const std::string& algo_spec) const
143 {
144 std::vector<std::string> name = parse_algorithm_name(algo_spec);
145 if(name.empty())
146 return 0;
147
148 const std::string algo_name = deref_alias(name[0]);
149
150 HANDLE_TYPE_ONE_STRING("PBKDF1", PKCS5_PBKDF1);
151 HANDLE_TYPE_ONE_STRING("PBKDF2", PKCS5_PBKDF2);
152
153 return 0;
154 }
155
156/*************************************************
157* Look for an algorithm with this name *
158*************************************************/
159BlockCipherModePaddingMethod*
160Default_Engine::find_bc_pad(const std::string& algo_spec) const
161 {
162 std::vector<std::string> name = parse_algorithm_name(algo_spec);
163 if(name.empty())
164 return 0;
165
166 const std::string algo_name = deref_alias(name[0]);
167
168 HANDLE_TYPE_NO_ARGS("PKCS7", PKCS7_Padding);
169 HANDLE_TYPE_NO_ARGS("OneAndZeros", OneAndZeros_Padding);
170 HANDLE_TYPE_NO_ARGS("X9.23", ANSI_X923_Padding);
171 HANDLE_TYPE_NO_ARGS("NoPadding", Null_Padding);
172
173 return 0;
174 }
175
176}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status