monotone

monotone Mtn Source Tree

Root/botan/def_mode.cpp

1/*************************************************
2* Default Engine Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/eng_def.h>
7#include <botan/parsing.h>
8#include <botan/filters.h>
9#include <botan/lookup.h>
10#include <botan/ecb.h>
11#include <botan/cbc.h>
12#include <botan/cts.h>
13#include <botan/cfb.h>
14#include <botan/ofb.h>
15#include <botan/ctr.h>
16#include <botan/eax.h>
17
18namespace Botan {
19
20/*************************************************
21* Get a cipher object *
22*************************************************/
23Keyed_Filter* Default_Engine::get_cipher(const std::string& algo_spec,
24 Cipher_Dir direction)
25 {
26 std::vector<std::string> algo_parts = split_on(algo_spec, '/');
27 if(algo_parts.empty())
28 throw Invalid_Algorithm_Name(algo_spec);
29
30 const std::string cipher = algo_parts[0];
31
32 if(have_stream_cipher(cipher))
33 {
34 if(algo_parts.size() == 1)
35 return new StreamCipher_Filter(cipher);
36 return 0;
37 }
38 else if(have_block_cipher(cipher))
39 {
40 if(algo_parts.size() != 2 && algo_parts.size() != 3)
41 return 0;
42
43 std::string mode = algo_parts[1];
44 u32bit bits = 0;
45
46 if(mode.find("CFB") != std::string::npos ||
47 mode.find("EAX") != std::string::npos)
48 {
49 std::vector<std::string> algo_info = parse_algorithm_name(mode);
50 mode = algo_info[0];
51 if(algo_info.size() == 1)
52 bits = 8*block_size_of(cipher);
53 else if(algo_info.size() == 2)
54 bits = to_u32bit(algo_info[1]);
55 else
56 throw Invalid_Algorithm_Name(algo_spec);
57 }
58
59 std::string padding;
60 if(algo_parts.size() == 3)
61 padding = algo_parts[2];
62 else
63 padding = (mode == "CBC") ? "PKCS7" : "NoPadding";
64
65 if(mode == "ECB" && padding == "CTS")
66 return 0;
67 else if((mode != "CBC" && mode != "ECB") && padding != "NoPadding")
68 throw Invalid_Algorithm_Name(algo_spec);
69
70 if(mode == "OFB")
71 return new OFB(cipher);
72 else if(mode == "CTR-BE")
73 return new CTR_BE(cipher);
74 else if(mode == "ECB" || mode == "CBC" || mode == "CTS" ||
75 mode == "CFB" || mode == "EAX")
76 {
77 if(mode == "ECB")
78 {
79 if(direction == ENCRYPTION)
80 return new ECB_Encryption(cipher, padding);
81 else
82 return new ECB_Decryption(cipher, padding);
83 }
84 else if(mode == "CFB")
85 {
86 if(direction == ENCRYPTION)
87 return new CFB_Encryption(cipher, bits);
88 else
89 return new CFB_Decryption(cipher, bits);
90 }
91 else if(mode == "CBC")
92 {
93 if(padding == "CTS")
94 {
95 if(direction == ENCRYPTION)
96 return new CTS_Encryption(cipher);
97 else
98 return new CTS_Decryption(cipher);
99 }
100 if(direction == ENCRYPTION)
101 return new CBC_Encryption(cipher, padding);
102 else
103 return new CBC_Decryption(cipher, padding);
104 }
105 else if(mode == "EAX")
106 {
107 if(direction == ENCRYPTION)
108 return new EAX_Encryption(cipher, bits);
109 else
110 return new EAX_Decryption(cipher, bits);
111 }
112 else
113 throw Internal_Error("get_mode: " + cipher + "/"
114 + mode + "/" + padding);
115 }
116 else
117 return 0;
118 }
119
120 return 0;
121 }
122
123}

Archive Download this file

Branches

Tags

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