1 // Copyright 2021 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 /////////////////////////////////////////////////////////////////////////////// 16 #ifndef TINK_INTERNAL_BN_UTIL_H_ 17 #define TINK_INTERNAL_BN_UTIL_H_ 18 19 #include <stddef.h> 20 21 #include <string> 22 23 #include "absl/strings/string_view.h" 24 #include "absl/types/span.h" 25 #include "openssl/bn.h" 26 #include "tink/internal/ssl_unique_ptr.h" 27 #include "tink/util/secret_data.h" 28 #include "tink/util/status.h" 29 #include "tink/util/statusor.h" 30 31 namespace crypto { 32 namespace tink { 33 namespace internal { 34 35 // Compares `bignum` with the given `word`. It returns a result < 0 if `bignum` 36 // < `word`, 0 if `bignum` == `word`, and > 0 if `bignum` > `word`. 37 int CompareBignumWithWord(const BIGNUM* bignum, BN_ULONG word); 38 39 // Converts the absolute value of `bignum` into a big-endian form, and writes it 40 // in `buffer`. 41 crypto::tink::util::Status BignumToBinaryPadded(absl::Span<char> buffer, 42 const BIGNUM* bignum); 43 44 // Retuns a string that encodes `bn` in big-endian form of size `len` with 45 // leading zeroes. 46 crypto::tink::util::StatusOr<std::string> BignumToString(const BIGNUM* bn, 47 size_t len); 48 49 // Retuns a SecretData object that encodes `bn` in big-endian form of size `len` 50 // with leading zeroes. 51 crypto::tink::util::StatusOr<crypto::tink::util::SecretData> BignumToSecretData( 52 const BIGNUM* bn, size_t len); 53 54 // Returns an OpenSSL/BoringSSL BIGNUM constructed from a bigendian string 55 // representation `bigendian_bn_str`. 56 crypto::tink::util::StatusOr<internal::SslUniquePtr<BIGNUM>> StringToBignum( 57 absl::string_view bigendian_bn_str); 58 59 } // namespace internal 60 } // namespace tink 61 } // namespace crypto 62 63 #endif // TINK_INTERNAL_BN_UTIL_H_ 64