File indexing completed on 2024-05-12 04:45:15

0001 /*
0002 Copyright (C) 1999-2007 The Botan Project. All rights reserved.
0003 
0004 Redistribution and use in source and binary forms, for any use, with or without
0005 modification, is permitted provided that the following conditions are met:
0006 
0007 1. Redistributions of source code must retain the above copyright notice, this
0008 list of conditions, and the following disclaimer.
0009 
0010 2. Redistributions in binary form must reproduce the above copyright notice,
0011 this list of conditions, and the following disclaimer in the documentation
0012 and/or other materials provided with the distribution.
0013 
0014 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED
0015 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0016 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
0017 
0018 IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT,
0019 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
0020 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0022 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
0023 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
0024 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025 */
0026 // LICENSEHEADER_END
0027 namespace QCA { // WRAPNS_LINE
0028 /*************************************************
0029  * Utility Functions Source File                  *
0030  * (C) 1999-2007 The Botan Project                *
0031  *************************************************/
0032 
0033 } // WRAPNS_LINE
0034 #include <botan/util.h>
0035 namespace QCA { // WRAPNS_LINE
0036 #ifndef BOTAN_TOOLS_ONLY
0037 } // WRAPNS_LINE
0038 #include <botan/bit_ops.h>
0039 namespace QCA { // WRAPNS_LINE
0040 #endif
0041 } // WRAPNS_LINE
0042 #include <algorithm>
0043 namespace QCA { // WRAPNS_LINE
0044 } // WRAPNS_LINE
0045 #include <cmath>
0046 namespace QCA { // WRAPNS_LINE
0047 
0048 namespace Botan {
0049 
0050 /*************************************************
0051  * Round up n to multiple of align_to             *
0052  *************************************************/
0053 u32bit round_up(u32bit n, u32bit align_to)
0054 {
0055     if (n % align_to || n == 0)
0056         n += align_to - (n % align_to);
0057     return n;
0058 }
0059 
0060 /*************************************************
0061  * Round down n to multiple of align_to           *
0062  *************************************************/
0063 u32bit round_down(u32bit n, u32bit align_to)
0064 {
0065     return (n - (n % align_to));
0066 }
0067 
0068 #ifndef BOTAN_TOOLS_ONLY
0069 /*************************************************
0070  * Return the work required for solving DL        *
0071  *************************************************/
0072 u32bit dl_work_factor(u32bit n_bits)
0073 {
0074     const u32bit MIN_ESTIMATE = 64;
0075 
0076     if (n_bits < 32)
0077         return 0;
0078 
0079     const double log_x = n_bits / 1.44;
0080 
0081     u32bit estimate = (u32bit)(2.76 * std::pow(log_x, 1.0 / 3.0) * std::pow(std::log(log_x), 2.0 / 3.0));
0082 
0083     return std::max(estimate, MIN_ESTIMATE);
0084 }
0085 
0086 /*************************************************
0087  * Estimate the entropy of the buffer             *
0088  *************************************************/
0089 u32bit entropy_estimate(const byte buffer[], u32bit length)
0090 {
0091     if (length <= 4)
0092         return 0;
0093 
0094     u32bit estimate = 0;
0095     byte   last = 0, last_delta = 0, last_delta2 = 0;
0096 
0097     for (u32bit j = 0; j != length; ++j) {
0098         byte delta = last ^ buffer[j];
0099         last       = buffer[j];
0100 
0101         byte delta2 = delta ^ last_delta;
0102         last_delta  = delta;
0103 
0104         byte delta3 = delta2 ^ last_delta2;
0105         last_delta2 = delta2;
0106 
0107         byte min_delta = delta;
0108         if (min_delta > delta2)
0109             min_delta = delta2;
0110         if (min_delta > delta3)
0111             min_delta = delta3;
0112 
0113         estimate += hamming_weight(min_delta);
0114     }
0115 
0116     return (estimate / 2);
0117 }
0118 #endif
0119 
0120 }
0121 } // WRAPNS_LINE