aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2011-05-12 14:47:53 +0000
committerlloyd <[email protected]>2011-05-12 14:47:53 +0000
commit44aa55d0aeb96d452450df5c024db7275a560925 (patch)
treeaadfe42a7f9bd2c546a8d3c47fb5a1b639baf43d
parent6b97d4b061b07f036dc3f823d6cd082fa67a71bb (diff)
Add base64_decode. Seems to work OK, though incremental decoding is
not well tested.
-rw-r--r--doc/log.txt3
-rw-r--r--src/codec/base64/base64.cpp151
-rw-r--r--src/codec/base64/base64.h70
-rw-r--r--src/math/bigint/bigint.cpp1
-rw-r--r--src/passhash/bcrypt/bcrypt.cpp10
5 files changed, 225 insertions, 10 deletions
diff --git a/doc/log.txt b/doc/log.txt
index 1cbe2b4df..988e4d71d 100644
--- a/doc/log.txt
+++ b/doc/log.txt
@@ -40,6 +40,9 @@ Version 1.9.18, Not Yet Released
* TR1 support is not longer automatically assumed under older versions
of GCC
+* Functions for base64 decoding that work standalone (without needing
+ to use a pipe) have been added to ``base64.h``
+
* The function ``BigInt::to_u32bit`` was inadvertently removed in 1.9.11
and has been added back.
diff --git a/src/codec/base64/base64.cpp b/src/codec/base64/base64.cpp
index d6c5ec7c0..27e6853bd 100644
--- a/src/codec/base64/base64.cpp
+++ b/src/codec/base64/base64.cpp
@@ -97,5 +97,156 @@ std::string base64_encode(const MemoryRegion<byte>& input)
return base64_encode(&input[0], input.size());
}
+size_t base64_decode(byte output[],
+ const char input[],
+ size_t input_length,
+ size_t& input_consumed,
+ bool final_inputs,
+ bool ignore_ws)
+ {
+ /*
+ * Base64 Decoder Lookup Table
+ * Warning: assumes ASCII encodings
+ */
+ static const byte BASE64_TO_BIN[256] = {
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80,
+ 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 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xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
+
+ byte* out_ptr = output;
+ byte decode_buf[4];
+ size_t decode_buf_pos = 0;
+ size_t final_truncate = 0;
+ bool seen_pad = false;
+
+ clear_mem(output, input_length * 3 / 4);
+
+ for(size_t i = 0; i != input_length; ++i)
+ {
+ const byte bin = BASE64_TO_BIN[static_cast<byte>(input[i])];
+
+ if(seen_pad && bin != 0x81)
+ throw std::invalid_argument("base64_decode: invalid padding");
+
+ if(bin <= 0x3F)
+ {
+ decode_buf[decode_buf_pos] = bin;
+ decode_buf_pos += 1;
+ }
+ else if(!(final_inputs && bin == 0x81))
+ {
+ if(bin == 0x80 && ignore_ws)
+ continue;
+
+ std::string bad_char(1, input[i]);
+ if(bad_char == "\t")
+ bad_char = "\\t";
+ else if(bad_char == "\n")
+ bad_char = "\\n";
+
+ throw std::invalid_argument(
+ std::string("base64_decode: invalid base64 character '") +
+ bad_char + "'");
+ }
+
+ /*
+ * If we either see a pad character, or we are the the end
+ * of the input
+ */
+ if(final_inputs && (bin == 0x81 || i == input_length - 1))
+ {
+ seen_pad = true;
+ if(decode_buf_pos)
+ {
+ for(size_t i = decode_buf_pos; i != 4; ++i)
+ decode_buf[i] = 0;
+ final_truncate = (4 - decode_buf_pos);
+ decode_buf_pos = 4;
+ }
+ }
+
+ if(decode_buf_pos == 4)
+ {
+ out_ptr[0] = (decode_buf[0] << 2) | (decode_buf[1] >> 4);
+ out_ptr[1] = (decode_buf[1] << 4) | (decode_buf[2] >> 2);
+ out_ptr[2] = (decode_buf[2] << 6) | decode_buf[3];
+
+ out_ptr += 3;
+ decode_buf_pos = 0;
+ }
+ }
+
+ input_consumed = input_length - decode_buf_pos;
+ size_t written = (out_ptr - output) - final_truncate;
+
+ return written;
+ }
+
+size_t base64_decode(byte output[],
+ const char input[],
+ size_t input_length,
+ bool ignore_ws)
+ {
+ size_t consumed = 0;
+ size_t written = base64_decode(output, input, input_length,
+ consumed, true, ignore_ws);
+
+ if(consumed != input_length)
+ throw std::invalid_argument("base64_decode: input did not have full bytes");
+
+ return written;
+ }
+
+size_t base64_decode(byte output[],
+ const std::string& input,
+ bool ignore_ws)
+ {
+ return base64_decode(output, &input[0], input.length(), ignore_ws);
+ }
+
+SecureVector<byte> base64_decode(const char input[],
+ size_t input_length,
+ bool ignore_ws)
+ {
+ SecureVector<byte> bin(1 + (input_length * 3) / 4);
+
+ size_t written = base64_decode(&bin[0],
+ input,
+ input_length,
+ ignore_ws);
+
+ bin.resize(written);
+ return bin;
+ }
+
+SecureVector<byte> base64_decode(const std::string& input,
+ bool ignore_ws)
+ {
+ return base64_decode(&input[0], input.size(), ignore_ws);
+ }
+
}
diff --git a/src/codec/base64/base64.h b/src/codec/base64/base64.h
index 6daac73d8..23a2558bd 100644
--- a/src/codec/base64/base64.h
+++ b/src/codec/base64/base64.h
@@ -48,6 +48,76 @@ std::string BOTAN_DLL base64_encode(const byte input[],
*/
std::string BOTAN_DLL base64_encode(const MemoryRegion<byte>& input);
+/**
+* Perform base64 decoding
+* @param output an array of at least input_length*3/4 bytes
+* @param input some base64 input
+* @param input_length length of input in bytes
+* @param input_consumed is an output parameter which says how many
+* bytes of input were actually consumed. If less than
+* input_length, then the range input[consumed:length]
+* should be passed in later along with more input.
+* @param final_inputs true iff this is the last input, in which case
+ padding is allowed
+* @param ignore_ws ignore whitespace on input; if false, throw an
+ exception if whitespace is encountered
+* @return number of bytes written to output
+*/
+size_t BOTAN_DLL base64_decode(byte output[],
+ const char input[],
+ size_t input_length,
+ size_t& input_consumed,
+ bool final_inputs,
+ bool ignore_ws = true);
+
+/**
+* Perform base64 decoding
+* @param output an array of at least input_length*3/4 bytes
+* @param input some base64 input
+* @param input_length length of input in bytes
+* @param ignore_ws ignore whitespace on input; if false, throw an
+ exception if whitespace is encountered
+* @return number of bytes written to output
+*/
+size_t BOTAN_DLL base64_decode(byte output[],
+ const char input[],
+ size_t input_length,
+ bool ignore_ws = true);
+
+/**
+* Perform base64 decoding
+* @param output an array of at least input_length/3*4 bytes
+* @param input some base64 input
+* @param ignore_ws ignore whitespace on input; if false, throw an
+ exception if whitespace is encountered
+* @return number of bytes written to output
+*/
+size_t BOTAN_DLL base64_decode(byte output[],
+ const std::string& input,
+ bool ignore_ws = true);
+
+/**
+* Perform base64 decoding
+* @param input some base64 input
+* @param input_length the length of input in bytes
+* @param ignore_ws ignore whitespace on input; if false, throw an
+ exception if whitespace is encountered
+* @return decoded base64 output
+*/
+SecureVector<byte> BOTAN_DLL base64_decode(const char input[],
+ size_t input_length,
+ bool ignore_ws = true);
+
+/**
+* Perform base64 decoding
+* @param input some base64 input
+* @param ignore_ws ignore whitespace on input; if false, throw an
+ exception if whitespace is encountered
+* @return decoded base64 output
+*/
+SecureVector<byte> BOTAN_DLL base64_decode(const std::string& input,
+ bool ignore_ws = true);
+
}
#endif
diff --git a/src/math/bigint/bigint.cpp b/src/math/bigint/bigint.cpp
index 38838e286..e2e062f2d 100644
--- a/src/math/bigint/bigint.cpp
+++ b/src/math/bigint/bigint.cpp
@@ -10,7 +10,6 @@
#include <botan/get_byte.h>
#include <botan/parsing.h>
#include <botan/internal/rounding.h>
-#include <stdio.h>
namespace Botan {
diff --git a/src/passhash/bcrypt/bcrypt.cpp b/src/passhash/bcrypt/bcrypt.cpp
index e533c6081..bb2e9095a 100644
--- a/src/passhash/bcrypt/bcrypt.cpp
+++ b/src/passhash/bcrypt/bcrypt.cpp
@@ -11,11 +11,6 @@
#include <botan/blowfish.h>
#include <botan/base64.h>
-#include <botan/pipe.h>
-#include <botan/b64_filt.h>
-#include <iostream>
-#include <stdio.h>
-
namespace Botan {
namespace {
@@ -89,10 +84,7 @@ MemoryVector<byte> bcrypt_base64_decode(std::string input)
for(size_t i = 0; i != input.size(); ++i)
input[i] = OPENBSD_BASE64_SUB[static_cast<byte>(input[i])];
- //return base64_decode(input);
- Pipe pipe(new Base64_Decoder);
- pipe.process_msg(input);
- return pipe.read_all();
+ return base64_decode(input);
}
std::string make_bcrypt(const std::string& pass,