From cb9a034650620e4d392a2d2f978b687078e5002c Mon Sep 17 00:00:00 2001 From: lloyd Date: Fri, 3 Sep 2010 13:23:45 +0000 Subject: Update some callers that were using Hex_Encoder or Hex_Decoder but really didn't need to. The ones in symkey and big_code were actually calling accessor functions to do the encoding themselves without a Pipe (should have definitely recognized that as a code smell). These versions have changed semantically with this checkin - previously they would completely ignore bad inputs, but now invalid inputs are rejected. For instance, you cannot say SymmetricKey key("Only some of this is hex, most of it isn't"); And expect to get a valid key formed by filtering out the non-hex characters and then decoding it. This is almost certainly a good thing. Also fix include in Botan.xs --- src/math/bigint/big_code.cpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'src/math') diff --git a/src/math/bigint/big_code.cpp b/src/math/bigint/big_code.cpp index 74701e532..a8272390d 100644 --- a/src/math/bigint/big_code.cpp +++ b/src/math/bigint/big_code.cpp @@ -1,6 +1,6 @@ /* * BigInt Encoding/Decoding -* (C) 1999-2007 Jack Lloyd +* (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -23,8 +23,9 @@ void BigInt::encode(byte output[], const BigInt& n, Base base) { SecureVector binary(n.encoded_size(Binary)); n.binary_encode(binary); - for(u32bit j = 0; j != binary.size(); ++j) - Hex_Encoder::encode(binary[j], output + 2*j); + + hex_encode(reinterpret_cast(output), + &binary[0], binary.size()); } else if(base == Octal) { @@ -103,22 +104,23 @@ BigInt BigInt::decode(const byte buf[], u32bit length, Base base) r.binary_decode(buf, length); else if(base == Hexadecimal) { - SecureVector hex; - for(u32bit j = 0; j != length; ++j) - if(Hex_Decoder::is_valid(buf[j])) - hex.append(buf[j]); - - u32bit offset = (hex.size() % 2); - SecureVector binary(hex.size() / 2 + offset); + SecureVector binary; - if(offset) + if(length % 2) { - byte temp[2] = { '0', hex[0] }; - binary[0] = Hex_Decoder::decode(temp); + // Handle lack of leading 0 + const char buf0_with_leading_0[2] = { '0', buf[0] }; + binary = hex_decode(buf0_with_leading_0, 2); + + binary.append(hex_decode(reinterpret_cast(&buf[1]), + length - 1, + false)); + } + else + binary = hex_decode(reinterpret_cast(buf), + length, false); - for(u32bit j = offset; j != binary.size(); ++j) - binary[j] = Hex_Decoder::decode(hex+2*j-offset); r.binary_decode(binary, binary.size()); } else if(base == Decimal || base == Octal) -- cgit v1.2.3