-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAesGcmContentCrypt.cs
More file actions
64 lines (56 loc) · 2.84 KB
/
AesGcmContentCrypt.cs
File metadata and controls
64 lines (56 loc) · 2.84 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
using SecureFolderFS.Core.Cryptography.Cipher;
using SecureFolderFS.Core.Cryptography.Helpers;
using System;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using static SecureFolderFS.Core.Cryptography.Constants.Crypto.Chunks.AesGcm;
using static SecureFolderFS.Core.Cryptography.Constants.Crypto.Headers.AesGcm;
using static SecureFolderFS.Core.Cryptography.Extensions.ContentCryptExtensions.AesGcmContentExtensions;
using static SecureFolderFS.Core.Cryptography.Extensions.HeaderCryptExtensions.AesGcmHeaderExtensions;
namespace SecureFolderFS.Core.Cryptography.ContentCrypt
{
/// <inheritdoc cref="IContentCrypt"/>
internal sealed class AesGcmContentCrypt : BaseContentCrypt
{
/// <inheritdoc/>
public override int ChunkPlaintextSize { get; } = CHUNK_PLAINTEXT_SIZE;
/// <inheritdoc/>
public override int ChunkCiphertextSize { get; } = CHUNK_CIPHERTEXT_SIZE;
/// <inheritdoc/>
public override int ChunkFirstReservedSize { get; } = CHUNK_NONCE_SIZE;
/// <inheritdoc/>
[SkipLocalsInit]
public override void EncryptChunk(ReadOnlySpan<byte> plaintextChunk, long chunkNumber, ReadOnlySpan<byte> header, Span<byte> ciphertextChunk)
{
// Chunk nonce
RandomNumberGenerator.Fill(ciphertextChunk.Slice(0, CHUNK_NONCE_SIZE));
// Big Endian chunk number and file header nonce
Span<byte> associatedData = stackalloc byte[sizeof(long) + HEADER_NONCE_SIZE];
CryptHelpers.FillAssociatedDataBigEndian(associatedData, header.GetHeaderNonce(), chunkNumber);
// Encrypt
AesGcm256.Encrypt(
plaintextChunk,
header.GetHeaderContentKey(),
ciphertextChunk.Slice(0, CHUNK_NONCE_SIZE),
ciphertextChunk.Slice(CHUNK_NONCE_SIZE + plaintextChunk.Length),
ciphertextChunk.Slice(CHUNK_NONCE_SIZE, plaintextChunk.Length),
associatedData);
}
/// <inheritdoc/>
[SkipLocalsInit]
public override bool DecryptChunk(ReadOnlySpan<byte> ciphertextChunk, long chunkNumber, ReadOnlySpan<byte> header, Span<byte> plaintextChunk)
{
// Big Endian chunk number and file header nonce
Span<byte> associatedData = stackalloc byte[sizeof(long) + HEADER_NONCE_SIZE];
CryptHelpers.FillAssociatedDataBigEndian(associatedData, header.GetHeaderNonce(), chunkNumber);
// Decrypt
return AesGcm256.TryDecrypt(
ciphertextChunk.GetChunkPayload(),
header.GetHeaderContentKey(),
ciphertextChunk.GetChunkNonce(),
ciphertextChunk.GetChunkTag(),
plaintextChunk.Slice(0, ciphertextChunk.Length - (CHUNK_NONCE_SIZE + CHUNK_TAG_SIZE)),
associatedData);
}
}
}