-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRsaParameterTraits.cs
More file actions
82 lines (76 loc) · 2.8 KB
/
RsaParameterTraits.cs
File metadata and controls
82 lines (76 loc) · 2.8 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
#region
using System;
using System.Diagnostics;
#endregion
namespace Ultz.Extensions.PrivacyEnhancedMail
{
public class RsaParameterTraits
{
public readonly int SizeD = -1;
public readonly int SizeDp = -1;
public readonly int SizeDq = -1;
public readonly int SizeExp = -1;
public readonly int SizeInvQ = -1;
public readonly int SizeMod = -1;
public readonly int SizeP = -1;
public readonly int SizeQ = -1;
public RsaParameterTraits(int modulusLengthInBits)
{
// The modulus length is supposed to be one of the common lengths, which is the commonly referred to strength of the key,
// like 1024 bit, 2048 bit, etc. It might be a few bits off though, since if the modulus has leading zeros it could show
// up as 1016 bits or something like that.
int assumedLength;
var logbase = Math.Log(modulusLengthInBits, 2);
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (logbase == (int) logbase)
{
// It's already an even power of 2
assumedLength = modulusLengthInBits;
}
else
{
// It's not an even power of 2, so round it up to the nearest power of 2.
assumedLength = (int) (logbase + 1.0);
assumedLength = (int) Math.Pow(2, assumedLength);
Debug.Assert(false); // Can this really happen in the field? I've never seen it, so if it happens
// you should verify that this really does the 'right' thing!
}
switch (assumedLength)
{
case 1024:
SizeMod = 0x80;
SizeExp = -1;
SizeD = 0x80;
SizeP = 0x40;
SizeQ = 0x40;
SizeDp = 0x40;
SizeDq = 0x40;
SizeInvQ = 0x40;
break;
case 2048:
SizeMod = 0x100;
SizeExp = -1;
SizeD = 0x100;
SizeP = 0x80;
SizeQ = 0x80;
SizeDp = 0x80;
SizeDq = 0x80;
SizeInvQ = 0x80;
break;
case 4096:
SizeMod = 0x200;
SizeExp = -1;
SizeD = 0x200;
SizeP = 0x100;
SizeQ = 0x100;
SizeDp = 0x100;
SizeDq = 0x100;
SizeInvQ = 0x100;
break;
default:
Debug.Assert(false); // Unknown key size?
break;
}
}
}
}