monotone

monotone Mtn Source Tree

Root/botan/def_mode.cpp

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

Archive Download this file

Branches

Tags

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