Skip to content

Commit 0f44dcb

Browse files
2 parents a85112e + 34564f9 commit 0f44dcb

File tree

1 file changed

+120
-1
lines changed

1 file changed

+120
-1
lines changed

README.md

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,120 @@
1-
# AeadChaCha20Poly1305.NetCore
1+
# AeadChaCha20Poly1305.NetCore
2+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![nuget](https://img.shields.io/nuget/v/AeadChaCha20Poly1305.NetCore.svg)](https://www.nuget.org/packages/AeadChaCha20Poly1305.NetCore/)
3+
4+
Implementation of AEAD_CHACHA20_POLY1305 an authenticated encryption with additional data algorithm using ChaCha20, and Poly1305 designed by D. J. Bernstein. Optimized for PinnedMemory, and .NET core.
5+
6+
You can read more about AEAD, ChaCha20, and Poly1305 using the resources below:
7+
- https://tools.ietf.org/html/rfc7539#section-2.8
8+
- https://blog.cloudflare.com/it-takes-two-to-chacha-poly/
9+
10+
# Install
11+
12+
From a command prompt
13+
```bash
14+
dotnet add package AeadChaCha20Poly1305.NetCore
15+
```
16+
17+
```bash
18+
Install-Package AeadChaCha20Poly1305.NetCore
19+
```
20+
21+
You can also search for package via your nuget ui / website:
22+
23+
https://www.nuget.org/packages/AeadChaCha20Poly1305.NetCore/
24+
25+
# Examples
26+
27+
You can find more examples in the github examples project.
28+
29+
```csharp
30+
var nonce = new byte[16];
31+
var key = new byte[32];
32+
var data = new byte[1024];
33+
34+
using var provider = new RNGCryptoServiceProvider();
35+
provider.GetBytes(nonce);
36+
provider.GetBytes(key);
37+
provider.GetBytes(data);
38+
39+
using var keyPin = new PinnedMemory<byte>(key, false);
40+
var aeadChaCha20Poly1305 = new AeadChaCha20Poly1305(keyPin, nonce, new byte[] { 32 });
41+
42+
// Encryption / Authentication
43+
using var dataPin = new PinnedMemory<byte>(data, false);
44+
eadChaCha20Poly1305.UpdateBlock(dataPin,0, dataPin.Length);
45+
46+
using var output = new PinnedMemory<byte>(new byte[aeadChaCha20Poly1305.GetLength()]);
47+
aeadChaCha20Poly1305.DoFinal(output, 0);
48+
var tag = aeadChaCha20Poly1305.GetTag(); // Poly1305 tag used to authenticate cipher
49+
50+
// Decryption / Authentication
51+
aeadChaCha20Poly1305.Reset();
52+
aeadChaCha20Poly1305.SetTag(tag);
53+
aeadChaCha20Poly1305.UpdateBlock(output,0, output.Length);
54+
55+
using var plain = new PinnedMemory<byte>(new byte[aeadChaCha20Poly1305.GetLength()]);
56+
aeadChaCha20Poly1305.DoFinal(plain, 0);
57+
```
58+
59+
# Constructor
60+
61+
```csharp
62+
AeadChaCha20Poly1305(PinnedMemory<byte> key, byte[] nonce, byte[] ad = null, int rounds = 20)
63+
```
64+
65+
# Methods
66+
67+
Get the cipher output length.
68+
```csharp
69+
int GetLength()
70+
```
71+
72+
Get the cipher authentication tag length.
73+
```csharp
74+
int GetTagLength()
75+
```
76+
77+
Get the contents of the internal buffer, this can be used to compare data before encryption, or decryption.
78+
```csharp
79+
byte[] GetBuffer()
80+
```
81+
82+
Update the cipher with a single byte.
83+
```csharp
84+
void Update(byte input)
85+
```
86+
87+
Update the cipher with a pinned memory byte array.
88+
```csharp
89+
void UpdateBlock(PinnedMemory<byte> input, int inOff, int len)
90+
```
91+
92+
Update the cipher with a byte array.
93+
```csharp
94+
void UpdateBlock(byte[] input, int inOff, int len)
95+
```
96+
97+
Produce the final cipher outputting to pinned memory. Key & nonce remain.
98+
```csharp
99+
void DoFinal(PinnedMemory<byte> output, int outOff)
100+
```
101+
102+
Get the final cipher tag, this should be called after DoFinal.
103+
```csharp
104+
PinnedMemory<byte> GetTag()
105+
```
106+
107+
Set the final cipher tag, this should be called before DoFinal, and is required for decryption.
108+
```csharp
109+
void SetTag(PinnedMemory<byte> value)
110+
```
111+
112+
Reset the cipher back to it's initial state for further processing. Key remains until dispose is called.
113+
```csharp
114+
void Reset()
115+
```
116+
117+
Clear key & nonce, reset cipher back to it's initial state.
118+
```csharp
119+
void Dispose()
120+
```

0 commit comments

Comments
 (0)