Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add password wrap for all versions #91

Open
wants to merge 59 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
a6f80ad
Added Test for ensuring operations only occur on the current key
TimothyMakkison Sep 14, 2022
4d80128
Added/Updated test vectors
TimothyMakkison Sep 14, 2022
4cd6886
Added ExpectFail property
TimothyMakkison Sep 14, 2022
216f25d
Add ExpectFail Test
TimothyMakkison Sep 14, 2022
1ed5017
Added properties to TestItem
TimothyMakkison Sep 14, 2022
29aa9f9
Updated header to serialize to the correct type, updated tests to acc…
TimothyMakkison Sep 14, 2022
3b78684
Remove purpose parameter from Paserk Encode
TimothyMakkison Sep 15, 2022
eed1d43
Added Encode function
TimothyMakkison Sep 15, 2022
7d995d8
Merge branch 'daviddesmet:master' into paserk_additions
TimothyMakkison Sep 17, 2022
39a663e
Merge branch 'paserk_additions' of https://github.com/TimothyMakkison…
TimothyMakkison Sep 17, 2022
221cc24
Added PaserkHelpers
TimothyMakkison Sep 17, 2022
3881c93
Added ExpectFail test case
TimothyMakkison Sep 17, 2022
1d34385
Temp PaserkHelper update
TimothyMakkison Sep 17, 2022
134d219
Edit PaserkHelper to use switch statements cheking key length
TimothyMakkison Sep 17, 2022
c16b6ab
Refactor SimpleDecode and SimpleEncode
TimothyMakkison Sep 18, 2022
f121818
Added Id encoding for V2 and V4
TimothyMakkison Sep 19, 2022
23bbb71
Added TestItem Generator
TimothyMakkison Sep 19, 2022
036ced6
Added invalid key formats handler for TypesTestVectors
TimothyMakkison Sep 19, 2022
4f5fa77
Added tests for Pid, Sid and Lid. Added IdEncode to Paserk Encode
TimothyMakkison Sep 19, 2022
6c840f4
Added decode error for Pid, Sid and Lid
TimothyMakkison Sep 19, 2022
a01295a
Initial commit
TimothyMakkison Sep 19, 2022
468f556
Added Encode Decode tests for Local-Pw
TimothyMakkison Sep 22, 2022
fd0e072
Added Pbkdf2 Sha384
TimothyMakkison Sep 22, 2022
6132604
Added PBKW decode for V1 and V3
TimothyMakkison Sep 22, 2022
cfb3630
Updated PBKW Encode for V1 and V3
TimothyMakkison Sep 22, 2022
7bd4c64
Edit cleaned up testing code.
TimothyMakkison Sep 22, 2022
14a8629
Add V1 identifier support and updated README
TimothyMakkison Sep 24, 2022
58b1205
Added int to big endian bytes and PEM functions
TimothyMakkison Sep 24, 2022
82784ad
Added Pbkw Pbkdf2Encryption and Pbkdf2Decryption
TimothyMakkison Sep 24, 2022
e38c7bb
Fix tests , add SecretPw and support V1 SecretPw
TimothyMakkison Sep 24, 2022
ba0d09e
Initial commit
TimothyMakkison Sep 19, 2022
7acda8e
Added Encode Decode tests for Local-Pw
TimothyMakkison Sep 22, 2022
6919f9b
Added Pbkdf2 Sha384
TimothyMakkison Sep 22, 2022
3cd16c8
Added PBKW decode for V1 and V3
TimothyMakkison Sep 22, 2022
816c689
Updated PBKW Encode for V1 and V3
TimothyMakkison Sep 22, 2022
6cc574f
Edit cleaned up testing code.
TimothyMakkison Sep 22, 2022
b29f79b
Added int to big endian bytes and PEM functions
TimothyMakkison Sep 24, 2022
14fa541
Added Pbkw Pbkdf2Encryption and Pbkdf2Decryption
TimothyMakkison Sep 24, 2022
aaab530
Fix tests , add SecretPw and support V1 SecretPw
TimothyMakkison Sep 24, 2022
afaca83
Merge branch 'Add_PasswordWrap' of https://github.com/TimothyMakkison…
TimothyMakkison Sep 24, 2022
094e42d
Add updated test vectors and update password to bytes.
TimothyMakkison Sep 24, 2022
12f96fc
Added Argon2id from Konscious.Security.Cryptography.Argon2
TimothyMakkison Sep 27, 2022
1012efd
Added Pbkw.Argon2Id Encrypt/Decrypt and implement Local/Secret Pw for…
TimothyMakkison Sep 27, 2022
fbdc70e
Update readme
TimothyMakkison Sep 27, 2022
f7c4766
Update readme
TimothyMakkison Sep 27, 2022
e36d4f6
Merge branch 'Implement_pw' of https://github.com/TimothyMakkison/pas…
TimothyMakkison Sep 27, 2022
6acd654
Removed concat usage
TimothyMakkison Sep 27, 2022
542c95a
Resolve conflicts
TimothyMakkison Sep 28, 2022
bc82841
Refactor IdEncode to call Paserk.Encode
TimothyMakkison Sep 28, 2022
8bd0022
Refactor Password Wrap
TimothyMakkison Sep 28, 2022
2d274a6
Update documentation
TimothyMakkison Sep 28, 2022
c35bae5
Changed Argon2id encode to take bytes instead of KiB.
TimothyMakkison Sep 30, 2022
68417f8
Lower test encode memory cost and iterations to reduce run time
TimothyMakkison Sep 30, 2022
809ab92
Deleted key type specific Encode overloads.
TimothyMakkison Oct 7, 2022
1933775
Add comments, minor changes
TimothyMakkison Oct 7, 2022
8d67776
Change PBKW to accept KiB
TimothyMakkison Oct 7, 2022
df6c9f6
Change PBKW to accept KiB
TimothyMakkison Oct 7, 2022
cef64cb
Resolve conflict
TimothyMakkison Oct 7, 2022
7e7c252
Update documentation.
TimothyMakkison Oct 13, 2022
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
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ PASERK extension
| local | ✅ |
| seal | ❌ |
| local-wrap | ❌ |
| local-pw | |
| local-pw | |
| sid | ✅ |
| public | ✅ |
| pid | ✅ |
| secret | ✅ |
| secret-wrap | ❌ |
| secret-pw | |
| secret-pw | |

## Installation

Expand Down Expand Up @@ -125,13 +125,24 @@ k[version].[type].[data]
#### Encoding a Key

```csharp
var paserk = Paserk.Encode(pasetoKey, type);
// Paserk encode local, public and secret keys.
var paserk = Paserk.Encode(pasetoKey, paserkType);

// Password wrap a local or secret key for versions 1 and 3.
// var pwPaserk = Paserk.Encode(pasetoKey, type, password, iterations);

// Password wrap a local or secret key for versions 2 and 4.
// var pwPaserk = Paserk.Encode(pasetoKey, type, password, memoryCost, iterations, parallelism);
```

#### Decoding a Key

```csharp
// Decode a paserk encoded paseto key.
var key = Paserk.Decode(paserk);

// Decode a password wrapped paseto key.
// var key = Paserk.Decode(paserk, password);
```

## Roadmap
Expand All @@ -151,7 +162,7 @@ var key = Paserk.Decode(paserk);
## Cryptography

* Uses Ed25519 (EdDSA over Curve25519) algorithm from CodesInChaos [Chaos.NaCl](https://github.com/CodesInChaos/Chaos.NaCl) cryptography library.
* Uses Blake2b cryptographic hash function from [Konscious.Security.Cryptography](https://github.com/kmaragon/Konscious.Security.Cryptography) repository.
* Uses Blake2b cryptographic hash and Argon2 key derivation functions from [Konscious.Security.Cryptography](https://github.com/kmaragon/Konscious.Security.Cryptography) repository.
* Uses AES-256-CTR, ECDSA over P-384 algorithms from [Bouncy Castle](https://github.com/novotnyllc/bc-csharp) cryptography library.
* Uses XChaCha20-Poly1305 AEAD from [NaCl.Core](https://github.com/daviddesmet/NaCl.Core) repository.

Expand Down
115 changes: 115 additions & 0 deletions src/Paseto/Cryptography/Internal/Argon2/Argon2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System.Diagnostics.CodeAnalysis;

namespace Paseto.Cryptography.Internal.Argon2;

using System;
using System.Threading.Tasks;
using System.Security.Cryptography;

/// <summary>
/// An implementation of Argon2 https://github.com/P-H-C/phc-winner-argon2
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819")]
internal abstract class Argon2 : DeriveBytes
{
/// <summary>
/// Create an Argon2 for encrypting the given password
/// </summary>
/// <param name="password"></param>
public Argon2(byte[] password)
{
if (password == null || password.Length == 0)
throw new ArgumentException("Argon2 needs a password set", nameof(password));

_password = password;
}

/// <summary>
/// Implementation of Reset
/// </summary>
public override void Reset()
{
}

/// <summary>
/// Implementation of GetBytes
/// </summary>
public override byte[] GetBytes(int bc)
{
ValidateParameters(bc);
var task = Task.Run(async () => await GetBytesAsyncImpl(bc).ConfigureAwait(false));
return task.Result;
}


/// <summary>
/// Implementation of GetBytes
/// </summary>
public Task<byte[]> GetBytesAsync(int bc)
{
ValidateParameters(bc);
return GetBytesAsyncImpl(bc);
}

/// <summary>
/// The password hashing salt
/// </summary>
public byte[] Salt { get; set; }

/// <summary>
/// An optional secret to use while hashing the Password
/// </summary>
public byte[] KnownSecret { get; set; }

/// <summary>
/// Any extra associated data to use while hashing the password
/// </summary>
public byte[] AssociatedData { get; set; }

/// <summary>
/// The number of iterations to apply to the password hash
/// </summary>
public int Iterations { get; set; }

/// <summary>
/// The number of 1kB memory blocks to use while processing the hash
/// </summary>
public int MemorySize { get; set; }

/// <summary>
/// The number of lanes to use while processing the hash
/// </summary>
public int DegreeOfParallelism { get; set; }

internal abstract Argon2Core BuildCore(int bc);

private void ValidateParameters(int bc)
{
if (bc > 1024)
throw new NotSupportedException("Current implementation of Argon2 only supports generating up to 1024 bytes");

if (Iterations < 1)
throw new InvalidOperationException("Cannot perform an Argon2 Hash with out at least 1 iteration");

if (MemorySize < 4)
throw new InvalidOperationException("Argon2 requires a minimum of 4kB of memory (MemorySize >= 4)");

if (DegreeOfParallelism < 1)
throw new InvalidOperationException("Argon2 requires at least 1 thread (DegreeOfParallelism)");
}

private Task<byte[]> GetBytesAsyncImpl(int bc)
{
var n = BuildCore(bc);
n.Salt = Salt;
n.Secret = KnownSecret;
n.AssociatedData = AssociatedData;
n.Iterations = Iterations;
n.MemorySize = MemorySize;
n.DegreeOfParallelism = DegreeOfParallelism;

return n.Hash(_password);
}

private byte[] _password;
}
Loading