-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAesSiv256.cs
More file actions
56 lines (48 loc) · 1.71 KB
/
Copy pathAesSiv256.cs
File metadata and controls
56 lines (48 loc) · 1.71 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
using System;
using System.Runtime.CompilerServices;
using Miscreant;
namespace SecureFolderFS.Core.Cryptography.Cipher
{
public sealed class AesSiv256 : IDisposable
{
private readonly Aead _aesCmacSiv;
private AesSiv256(Aead aesCmacSiv)
{
_aesCmacSiv = aesCmacSiv;
}
public static AesSiv256 CreateInstance(ReadOnlySpan<byte> dekKey, ReadOnlySpan<byte> macKey)
{
// The longKey will be split into two keys - one for S2V and the other one for CTR
var longKey = new byte[dekKey.Length + macKey.Length];
var longKeySpan = longKey.AsSpan();
// Copy keys
dekKey.CopyTo(longKeySpan);
macKey.CopyTo(longKeySpan.Slice(dekKey.Length));
var aesCmacSiv = Aead.CreateAesCmacSiv(longKey);
return new AesSiv256(aesCmacSiv);
}
[MethodImpl(MethodImplOptions.Synchronized)]
public byte[] Encrypt(ReadOnlySpan<byte> bytes, ReadOnlySpan<byte> associatedData)
{
return _aesCmacSiv.Seal(bytes.ToArray(), data: associatedData.ToArray());
}
[MethodImpl(MethodImplOptions.Synchronized)]
public byte[] Decrypt(ReadOnlySpan<byte> bytes, ReadOnlySpan<byte> associatedData)
{
return _aesCmacSiv.Open(bytes.ToArray(), data: associatedData.ToArray());
}
/// <inheritdoc/>
public void Dispose()
{
try
{
_aesCmacSiv.Dispose();
}
catch (Exception ex)
{
// TODO: Investigate. Sometimes an exception is thrown when disposing the Aead instance
_ = ex;
}
}
}
}