| 1 |
/* |
| 2 |
* Copyright (c) .SE (The Internet Infrastructure Foundation) |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
BotanUtil.h |
| 29 |
|
| 30 |
Botan convenience functions |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include "config.h" |
| 34 |
#include "BotanUtil.h" |
| 35 |
#include <botan/der_enc.h> |
| 36 |
#include <botan/ber_dec.h> |
| 37 |
#include <botan/asn1_obj.h> |
| 38 |
#include <botan/asn1_str.h> |
| 39 |
#include <botan/version.h> |
| 40 |
|
| 41 |
// Convert a Botan BigInt to a ByteString |
| 42 |
ByteString BotanUtil::bigInt2ByteString(const Botan::BigInt& bigInt) |
| 43 |
{ |
| 44 |
ByteString rv; |
| 45 |
|
| 46 |
rv.resize(bigInt.bytes()); |
| 47 |
bigInt.binary_encode(&rv[0]); |
| 48 |
|
| 49 |
return rv; |
| 50 |
} |
| 51 |
|
| 52 |
// Used when extracting little-endian data |
| 53 |
ByteString BotanUtil::bigInt2ByteStringPrefix(const Botan::BigInt& bigInt, size_t size) |
| 54 |
{ |
| 55 |
ByteString rv; |
| 56 |
|
| 57 |
if (size > bigInt.bytes()) |
| 58 |
{ |
| 59 |
size_t diff = size - bigInt.bytes(); |
| 60 |
rv.resize(size); |
| 61 |
|
| 62 |
memset(&rv[0], '\0', diff); |
| 63 |
|
| 64 |
bigInt.binary_encode(&rv[0] + diff); |
| 65 |
} |
| 66 |
else |
| 67 |
{ |
| 68 |
rv.resize(bigInt.bytes()); |
| 69 |
bigInt.binary_encode(&rv[0]); |
| 70 |
} |
| 71 |
|
| 72 |
return rv; |
| 73 |
} |
| 74 |
|
| 75 |
// Convert a ByteString to an Botan BigInt |
| 76 |
Botan::BigInt BotanUtil::byteString2bigInt(const ByteString& byteString) |
| 77 |
{ |
| 78 |
return Botan::BigInt(byteString.const_byte_str(), byteString.size()); |
| 79 |
} |
| 80 |
|
| 81 |
#if defined(WITH_ECC) || defined(WITH_GOST) |
| 82 |
// Convert a Botan EC group to a ByteString |
| 83 |
ByteString BotanUtil::ecGroup2ByteString(const Botan::EC_Group& ecGroup) |
| 84 |
{ |
| 85 |
std::vector<uint8_t> der = ecGroup.DER_encode(Botan::EC_DOMPAR_ENC_OID); |
| 86 |
return ByteString(&der[0], der.size()); |
| 87 |
} |
| 88 |
|
| 89 |
// Convert a ByteString to a Botan EC group |
| 90 |
Botan::EC_Group BotanUtil::byteString2ECGroup(const ByteString& byteString) |
| 91 |
{ |
| 92 |
std::vector<uint8_t> der(byteString.size()); |
| 93 |
memcpy(&der[0], byteString.const_byte_str(), byteString.size()); |
| 94 |
return Botan::EC_Group(der); |
| 95 |
} |
| 96 |
|
| 97 |
// Convert a Botan EC point to a ByteString |
| 98 |
ByteString BotanUtil::ecPoint2ByteString(const Botan::PointGFp& ecPoint) |
| 99 |
{ |
| 100 |
ByteString point; |
| 101 |
|
| 102 |
try |
| 103 |
{ |
| 104 |
#if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(2,5,0) |
| 105 |
const std::vector<uint8_t> repr = ecPoint.encode(Botan::PointGFp::UNCOMPRESSED); |
| 106 |
#else |
| 107 |
const Botan::secure_vector<uint8_t> repr = Botan::EC2OSP(ecPoint, Botan::PointGFp::UNCOMPRESSED); |
| 108 |
#endif |
| 109 |
Botan::secure_vector<uint8_t> der; |
| 110 |
|
| 111 |
der = Botan::DER_Encoder() |
| 112 |
.encode(repr, Botan::OCTET_STRING) |
| 113 |
.get_contents(); |
| 114 |
point.resize(der.size()); |
| 115 |
memcpy(&point[0], &der[0], der.size()); |
| 116 |
} |
| 117 |
catch (...) |
| 118 |
{ |
| 119 |
ERROR_MSG("Can't convert from EC point"); |
| 120 |
} |
| 121 |
return point; |
| 122 |
} |
| 123 |
|
| 124 |
// Convert a ByteString to a Botan EC point |
| 125 |
Botan::PointGFp BotanUtil::byteString2ECPoint(const ByteString& byteString, const Botan::EC_Group& ecGroup) |
| 126 |
{ |
| 127 |
std::vector<uint8_t> repr; |
| 128 |
Botan::BER_Decoder(byteString.const_byte_str(), byteString.size()) |
| 129 |
.decode(repr, Botan::OCTET_STRING) |
| 130 |
.verify_end(); |
| 131 |
#if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(2,5,0) |
| 132 |
return ecGroup.OS2ECP(&repr[0], repr.size()); |
| 133 |
#else |
| 134 |
return Botan::OS2ECP(&repr[0], repr.size(), ecGroup.get_curve()); |
| 135 |
#endif |
| 136 |
} |
| 137 |
#endif |
| 138 |
|
| 139 |
#ifdef WITH_EDDSA |
| 140 |
// Convert a Botan OID to a ByteString |
| 141 |
ByteString BotanUtil::oid2ByteString(const Botan::OID& oid) |
| 142 |
{ |
| 143 |
std::string name; |
| 144 |
|
| 145 |
if (oid == BotanUtil::x25519_oid) |
| 146 |
{ |
| 147 |
name = "curve25519"; |
| 148 |
} |
| 149 |
else if (oid == BotanUtil::ed25519_oid) |
| 150 |
{ |
| 151 |
name = "edwards25519"; |
| 152 |
} |
| 153 |
else |
| 154 |
{ |
| 155 |
return ByteString(); |
| 156 |
} |
| 157 |
|
| 158 |
Botan::ASN1_String str = Botan::ASN1_String(name, Botan::PRINTABLE_STRING); |
| 159 |
const Botan::secure_vector<uint8_t> der = Botan::DER_Encoder().encode(str).get_contents(); |
| 160 |
return ByteString(&der[0], der.size()); |
| 161 |
} |
| 162 |
|
| 163 |
// Convert a ByteString to a Botan OID |
| 164 |
Botan::OID BotanUtil::byteString2Oid(const ByteString& byteString) |
| 165 |
{ |
| 166 |
Botan::BER_Object object; |
| 167 |
|
| 168 |
Botan::BER_Decoder dec = Botan::BER_Decoder(byteString.const_byte_str(), byteString.size()) |
| 169 |
.get_next(object) |
| 170 |
.verify_end(); |
| 171 |
|
| 172 |
if (object.is_a(Botan::PRINTABLE_STRING, Botan::ASN1_Tag(0))) |
| 173 |
{ |
| 174 |
Botan::ASN1_String str; |
| 175 |
|
| 176 |
dec.push_back(std::move(object)); |
| 177 |
str.decode_from(dec); |
| 178 |
if (str.value() == "edwards25519") { |
| 179 |
return BotanUtil::ed25519_oid; |
| 180 |
} else if (str.value() == "curve25519") { |
| 181 |
return BotanUtil::x25519_oid; |
| 182 |
} |
| 183 |
|
| 184 |
/* fall through */ |
| 185 |
} |
| 186 |
else if (object.is_a(Botan::OBJECT_ID, Botan::ASN1_Tag(0))) |
| 187 |
{ |
| 188 |
Botan::OID oid; |
| 189 |
|
| 190 |
dec.push_back(std::move(object)); |
| 191 |
oid.decode_from(dec); |
| 192 |
|
| 193 |
return oid; |
| 194 |
} |
| 195 |
|
| 196 |
return Botan::OID(); |
| 197 |
} |
| 198 |
#endif |