Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

src/CodeFusion.Security.Bcrypt/BlowfishConstants.cs is under the BSD 3-Clause
License instead; see THIRD-PARTY-NOTICES.md.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,5 @@ BCrypt.Net-Next, the queue's cancellation, limits and pooling, and that a check

## License

MIT. `BlowfishConstants.cs` is BSD-3-Clause, from Go's `golang.org/x/crypto`; see
[THIRD-PARTY-NOTICES.md](https://github.com/codefusion-cc/bcrypt-dotnet/blob/main/THIRD-PARTY-NOTICES.md).
MIT. Blowfish's initial tables, the hexadecimal digits of pi, are computed at compile time by
[BlowfishTablesGenerator.cs](https://github.com/codefusion-cc/bcrypt-dotnet/blob/main/src/CodeFusion.Security.Bcrypt.Generators/BlowfishTablesGenerator.cs).
35 changes: 0 additions & 35 deletions THIRD-PARTY-NOTICES.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Globalization;
using System.Numerics;
using System.Text;
using Microsoft.CodeAnalysis;

namespace CodeFusion.Security.Generators;

/// <summary>
/// Writes BlowfishConstants.g.cs: Blowfish's initial state, which is the fractional part of pi in hexadecimal. The
/// P-array takes the first 18 32-bit words (0x243f6a88, ...) and the four S-boxes the next 1,024, as Schneier defined
/// it. The digits are computed here at compile time, so nothing is copied from another implementation.
/// </summary>
[Generator(LanguageNames.CSharp)]
public sealed class BlowfishTablesGenerator : IIncrementalGenerator
{
const int PWords = 18, SWords = 1024;

/// <inheritdoc/>
public void Initialize(IncrementalGeneratorInitializationContext context) =>
context.RegisterPostInitializationOutput(static c => c.AddSource("BlowfishConstants.g.cs", Source()));

internal static string Source()
{
uint[] words = PiWords(PWords + SWords);
var sb = new StringBuilder();
sb.Append(
"// <auto-generated>\n" +
"// Written by CodeFusion.Security.Bcrypt.Generators/BlowfishTablesGenerator.cs when the library compiles.\n" +
"// </auto-generated>\n" +
"namespace CodeFusion.Security;\n" +
"\n" +
"/// <summary>The Blowfish initial state: the fractional hex digits of pi, the P-array, then the four S-boxes.</summary>\n" +
"static class BlowfishConstants\n" +
"{\n");
Table(sb, "P", words, 0, PWords);
sb.Append('\n');
Table(sb, "S", words, PWords, SWords);
sb.Append("}\n");
return sb.ToString();
}

static void Table(StringBuilder sb, string name, uint[] words, int start, int count)
{
sb.Append(" public static readonly uint[] ").Append(name).Append(" =\n [\n");
for (int i = 0; i < count; i += 8)
{
sb.Append(" ");
for (int k = i; k < i + 8 && k < count; k++)
sb.Append(" 0x").Append(words[start + k].ToString("x8", CultureInfo.InvariantCulture)).Append(',');
sb.Append('\n');
}
sb.Append(" ];\n");
}

/// <summary>
/// The first <paramref name="count"/> 32-bit words of pi's fractional part, by Machin's formula
/// pi = 16 atan(1/5) - 4 atan(1/239) in binary fixed point. Each truncated term is off by less than one unit in
/// the last place, and the 64 guard bits absorb the sum of those errors many times over.
/// </summary>
internal static uint[] PiWords(int count)
{
int bits = 32 * count + 64;
BigInteger one = BigInteger.One << bits;
BigInteger pi = 16 * ArctanOfInverse(5, one) - 4 * ArctanOfInverse(239, one);
BigInteger fraction = pi - 3 * one; // pi = 3.243f6a88...
var words = new uint[count];
for (int i = 0; i < count; i++)
words[i] = (uint)((fraction >> (bits - 32 * (i + 1))) & uint.MaxValue);
return words;
}

/// <summary>atan(1/x) * one: the series 1/x - 1/(3x^3) + 1/(5x^5) - ...</summary>
static BigInteger ArctanOfInverse(int x, BigInteger one)
{
BigInteger power = one / x, sum = BigInteger.Zero, x2 = x * x;
for (int k = 0; !power.IsZero; k++)
{
BigInteger term = power / (2 * k + 1);
sum += k % 2 == 0 ? term : -term;
power /= x2;
}
return sum;
}
}
Loading
Loading