monotone

monotone Mtn Source Tree

Root/botan/def_alg.cpp

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

Archive Download this file

Branches

Tags

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