Botan  1.11.15
src/lib/cert/cvc/ecdsa_sig.cpp
Go to the documentation of this file.
00001 /*
00002 * ECDSA Signature
00003 * (C) 2007 Falko Strenzke, FlexSecure GmbH
00004 * (C) 2008-2010 Jack Lloyd
00005 *
00006 * Botan is released under the Simplified BSD License (see license.txt)
00007 */
00008 
00009 #include <botan/ecdsa_sig.h>
00010 
00011 namespace Botan {
00012 
00013 ECDSA_Signature::ECDSA_Signature(const std::vector<byte>& ber)
00014    {
00015    BER_Decoder(ber)
00016       .start_cons(SEQUENCE)
00017          .decode(m_r)
00018          .decode(m_s)
00019       .end_cons()
00020       .verify_end();
00021    }
00022 
00023 std::vector<byte> ECDSA_Signature::DER_encode() const
00024    {
00025    return DER_Encoder()
00026       .start_cons(SEQUENCE)
00027         .encode(get_r())
00028         .encode(get_s())
00029       .end_cons()
00030       .get_contents_unlocked();
00031    }
00032 
00033 std::vector<byte> ECDSA_Signature::get_concatenation() const
00034    {
00035    // use the larger
00036    const size_t enc_len = m_r > m_s ? m_r.bytes() : m_s.bytes();
00037 
00038    const auto sv_r = BigInt::encode_1363(m_r, enc_len);
00039    const auto sv_s = BigInt::encode_1363(m_s, enc_len);
00040 
00041    secure_vector<byte> result(sv_r);
00042    result += sv_s;
00043    return unlock(result);
00044    }
00045 
00046 ECDSA_Signature decode_concatenation(const std::vector<byte>& concat)
00047    {
00048    if(concat.size() % 2 != 0)
00049       throw Invalid_Argument("Erroneous length of signature");
00050 
00051    const size_t rs_len = concat.size() / 2;
00052 
00053    BigInt r = BigInt::decode(&concat[0], rs_len);
00054    BigInt s = BigInt::decode(&concat[rs_len], rs_len);
00055 
00056    return ECDSA_Signature(r, s);
00057    }
00058 
00059 }