-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAesCtrHmacHeaderCrypt.cs
More file actions
138 lines (114 loc) · 5.45 KB
/
AesCtrHmacHeaderCrypt.cs
File metadata and controls
138 lines (114 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using SecureFolderFS.Core.Cryptography.Cipher;
using SecureFolderFS.Core.Cryptography.SecureStore;
using System;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using static SecureFolderFS.Core.Cryptography.Constants.Crypto.Headers.AesCtrHmac;
using static SecureFolderFS.Core.Cryptography.Extensions.HeaderCryptExtensions.AesCtrHmacHeaderExtensions;
namespace SecureFolderFS.Core.Cryptography.HeaderCrypt
{
/// <inheritdoc cref="IHeaderCrypt"/>
internal sealed class AesCtrHmacHeaderCrypt : BaseHeaderCrypt
{
/// <inheritdoc/>
public override int HeaderCiphertextSize { get; } = HEADER_SIZE;
/// <inheritdoc/>
public override int HeaderPlaintextSize { get; } = HEADER_NONCE_SIZE + HEADER_CONTENTKEY_SIZE;
public AesCtrHmacHeaderCrypt(KeyPair keyPair)
: base(keyPair)
{
}
/// <inheritdoc/>
public override void CreateHeader(Span<byte> plaintextHeader)
{
// Nonce
RandomNumberGenerator.Fill(plaintextHeader.Slice(0, HEADER_NONCE_SIZE));
// Content key
RandomNumberGenerator.Fill(plaintextHeader.Slice(HEADER_NONCE_SIZE, HEADER_CONTENTKEY_SIZE));
}
/// <inheritdoc/>
public override unsafe void EncryptHeader(ReadOnlySpan<byte> plaintextHeader, Span<byte> ciphertextHeader)
{
// Nonce
plaintextHeader.Slice(0, HEADER_NONCE_SIZE).CopyTo(ciphertextHeader);
// Use unsafe pointers to pass span data through the UseKey callback
fixed (byte* plaintextPtr = plaintextHeader)
fixed (byte* ciphertextPtr = ciphertextHeader)
{
var state = (ptPtr: (nint)plaintextPtr, ptLen: plaintextHeader.Length, ctPtr: (nint)ciphertextPtr, ctLen: ciphertextHeader.Length);
// Encrypt with DekKey
DekKey.UseKey(state, static (dekKey, s) =>
{
var pt = new ReadOnlySpan<byte>((byte*)s.ptPtr, s.ptLen);
var ct = new Span<byte>((byte*)s.ctPtr, s.ctLen);
AesCtr256.Encrypt(
pt.GetHeaderContentKey(),
dekKey,
pt.GetHeaderNonce(),
ct.Slice(HEADER_NONCE_SIZE, HEADER_CONTENTKEY_SIZE));
});
// Calculate MAC with MacKey
MacKey.UseKey(state, static (macKey, s) =>
{
var pt = new ReadOnlySpan<byte>((byte*)s.ptPtr, s.ptLen);
var ct = new Span<byte>((byte*)s.ctPtr, s.ctLen);
CalculateHeaderMacInternal(
macKey,
pt.GetHeaderNonce(),
ct.Slice(HEADER_NONCE_SIZE, HEADER_CONTENTKEY_SIZE),
ct.Slice(pt.Length)); // plaintextHeader.Length already includes HEADER_NONCE_SIZE
});
}
}
/// <inheritdoc/>
[SkipLocalsInit]
public override unsafe bool DecryptHeader(ReadOnlySpan<byte> ciphertextHeader, Span<byte> plaintextHeader)
{
// Use unsafe pointers to pass span data through the UseKey callback
fixed (byte* ciphertextPtr = ciphertextHeader)
fixed (byte* plaintextPtr = plaintextHeader)
{
var state = (ctPtr: (nint)ciphertextPtr, ctLen: ciphertextHeader.Length, ptPtr: (nint)plaintextPtr, ptLen: plaintextHeader.Length);
// Verify MAC with MacKey
var macValid = MacKey.UseKey(state, static (macKey, s) =>
{
var ct = new ReadOnlySpan<byte>((byte*)s.ctPtr, s.ctLen);
// Allocate byte* for MAC
Span<byte> mac = stackalloc byte[HEADER_MAC_SIZE];
// Calculate MAC
CalculateHeaderMacInternal(
macKey,
ct.GetHeaderNonce(),
ct.GetHeaderContentKey(),
mac);
// Check MAC using constant-time comparison to prevent timing attacks
return CryptographicOperations.FixedTimeEquals(mac, ct.GetHeaderMac());
});
if (!macValid)
return false;
// Nonce
ciphertextHeader.GetHeaderNonce().CopyTo(plaintextHeader);
// Decrypt with DekKey
DekKey.UseKey(state, static (dekKey, s) =>
{
var ct = new ReadOnlySpan<byte>((byte*)s.ctPtr, s.ctLen);
var pt = new Span<byte>((byte*)s.ptPtr, s.ptLen);
AesCtr256.Decrypt(
ct.GetHeaderContentKey(),
dekKey,
ct.GetHeaderNonce(),
pt.Slice(HEADER_NONCE_SIZE));
});
return true;
}
}
private static void CalculateHeaderMacInternal(ReadOnlySpan<byte> macKey, ReadOnlySpan<byte> headerNonce, ReadOnlySpan<byte> ciphertextPayload, Span<byte> headerMac)
{
// Initialize HMAC
using var hmacSha256 = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA256, macKey);
hmacSha256.AppendData(headerNonce); // headerNonce
hmacSha256.AppendData(ciphertextPayload); // ciphertextPayload
hmacSha256.GetCurrentHash(headerMac);
}
}
}