-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCSOTP.cs
More file actions
333 lines (278 loc) · 8.8 KB
/
CSOTP.cs
File metadata and controls
333 lines (278 loc) · 8.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Created by SharpDevelop.
* User: CBUD
* Date: 12/6/2017
* Time: 12:49 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Text;
using System.Runtime.Serialization;
namespace CSOTP
{
[Serializable()]
public class HMACGenerationError : System.Exception
{
public HMACGenerationError() : base() { }
public HMACGenerationError(string message) : base(message) { }
public HMACGenerationError(string message, System.Exception inner) : base(message, inner) { }
protected HMACGenerationError(SerializationInfo info, StreamingContext context) { }
}
[Serializable()]
public class BASE32FormatError : System.Exception
{
public BASE32FormatError() : base() { }
public BASE32FormatError(string message) : base(message) { }
public BASE32FormatError(string message, System.Exception inner) : base(message, inner) { }
protected BASE32FormatError(SerializationInfo info, StreamingContext context) { }
}
public enum OTPType {
OTP, TOTP, HOTP
}
/// <summary>
/// Description of Class1.
/// </summary>
public class OTP
{
public static readonly char[] DEFAULT_BASE32_CHARS = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5',
'6', '7'
};
public int digits { get; set; }
public int bits { get; set; }
public OTPType method { get; set; }
public Func<byte[], byte[], byte[]> algo { get; set; }
public byte[] digest { get; set; }
public byte[] base32_secret { get; set; }
protected Random random;
public OTP(byte[] base32_secret, int bits, Func<byte[], byte[], byte[]> algo, byte[] digest, int digits)
{
random = new Random();
random.Next(1,2);
this.base32_secret = base32_secret;
this.bits = bits;
this.algo = algo;
this.digest = digest;
this.digits = digits;
this.method = OTPType.OTP;
}
public int generate(long input, byte[] output)
{
int secret_len = this.base32_secret.Length;
int desired_secret_len = (secret_len / 8) * 5;
if (this.bits % 8 != 0)
throw new HMACGenerationError("generate `this.bits` must be divisble by 8 (got " + this.bits + ")");
int bit_size = this.bits / 8;
byte[] byte_string = this.int_to_bytestring(input);
byte[] _byte_secret = this.byte_secret(secret_len, desired_secret_len + 1);
byte[] hmac = this.algo(_byte_secret, byte_string);
if (hmac == null)
throw new HMACGenerationError("generate `hmac` returned null from supplied decrypt function");
int offset = (hmac[bit_size - 1] & 0xF);
int code =
(
(hmac[offset] & 0x7F) << 24 |
(hmac[offset+1] & 0xFF) << 16 |
(hmac[offset+2] & 0xFF) << 8 |
(hmac[offset+3] & 0xFF)
) % (int) Math.Pow(10, this.digits);
if (output != null) {
byte[] temp = Encoding.ASCII.GetBytes(code.ToString("G").PadLeft(this.digits));
Array.Copy(temp, output, this.digits);
}
return code;
}
public byte[] byte_secret(int size, int len)
{
if (size % 8 != 0)
throw new BASE32FormatError("byte_secret `size` must be divisble by 8 (got " + size + ")");
byte[] out_str = new byte[len];
int n = 5;
for (int i=0; ; i++) {
n = -1;
out_str[i*5] = 0;
for (int block=0; block<8; block++) {
int offset = (3 - (5*block) % 8);
int octet = (block*5) / 8;
int c = 0;
if (i*8+block < this.base32_secret.Length)
c = this.base32_secret[i*8+block] & 0xFF;
if (c >= 'A' && c <= 'Z')
n = c - 'A';
if (c >= '2' && c <= '7')
n = 26 + c - '2';
if (n < 0) {
n = octet;
break;
}
out_str[i*5+octet] |= BitConverter.GetBytes(-offset > 0 ? n >> -offset : n << offset)[0];
if (offset < 0)
out_str[i*5+octet+1] = BitConverter.GetBytes(-(8 + offset) > 0 ? n >> -(8 + offset) : n << (8 + offset))[0];
}
if (n < 5)
break;
}
return out_str;
}
public byte[] int_to_bytestring(long integer)
{
return new byte[] {
0, 0, 0, 0,
BitConverter.GetBytes(integer >> 24)[0],
BitConverter.GetBytes(integer >> 16)[0],
BitConverter.GetBytes(integer >> 8 )[0],
BitConverter.GetBytes(integer)[0]
};
}
public byte[] random_base32(int len, char[] chars) {
len = len > 0 ? len : 16;
if (len % 8 != 0)
throw new BASE32FormatError("random_base32 `len` must be divisble by 8 (got " + len + ")");
byte[] bytes = new byte[len];
for (int i=0; i<len; i++)
bytes[i] = (byte)chars[random.Next() % 32];
return bytes;
}
}
public class TOTP : OTP
{
public int interval { get; set; }
public TOTP(byte[] base32_secret, int bits, Func<byte[], byte[], byte[]> algo, byte[] digest, int digits, int interval)
: base(base32_secret, bits, algo, digest, digits)
{
this.interval = interval;
this.method = OTPType.TOTP;
}
public bool compare(int key, int increment, long for_time)
{
return this.compare(
Encoding.ASCII.GetBytes(key.ToString("G").PadLeft(base.digits)),
increment,
for_time);
}
public bool compare(byte[] key, int increment, long for_time)
{
byte[] time_str = new byte[base.digits];
this.at(for_time, increment, time_str);
for (int i=0; i<key.Length; i++)
if (i > time_str.Length || key[i] != time_str[i])
return false;
return true;
}
public int at(long for_time, int counter_offset)
{
return this.at(for_time, counter_offset, null);
}
public int at(long for_time, int counter_offset, byte[] output)
{
return base.generate(this.timecode(for_time) + (long)counter_offset, output);
}
public int now()
{
return this.now(null);
}
public int now(byte[] output)
{
return base.generate(this.timecode(DateTimeOffset.Now.ToUnixTimeSeconds()), output);
}
public bool verify(int key, long for_time, int valid_window)
{
return this.verify(
Encoding.ASCII.GetBytes(key.ToString("G").PadLeft(base.digits, '0')),
for_time,
valid_window);
}
public bool verify(byte[] key, long for_time, int valid_window)
{
if (valid_window < 0)
return false;
if (valid_window > 0) {
for (int i=-valid_window; i<valid_window; i++)
if (this.compare(key, i, for_time) == true)
return true;
}
return this.compare(key, 0, for_time);
}
public long valid_until(long for_time, int valid_window)
{
return for_time + (this.interval * valid_window);
}
public long timecode(long for_time)
{
if (for_time <= 0)
return 0;
return (long)((double)for_time/(double)this.interval);
}
}
public class HOTP : OTP
{
public HOTP(byte[] base32_secret, int bits, Func<byte[], byte[], byte[]> algo, byte[] digest, int digits)
: base(base32_secret, bits, algo, digest, digits)
{
this.method = OTPType.HOTP;
}
public bool compare(int key, int counter)
{
return this.compare(
Encoding.ASCII.GetBytes(key.ToString("G").PadLeft(base.digits, '0')),
counter);
}
public bool compare(byte[] key, int counter)
{
byte[] cnt_str = new byte[base.digits];
this.at(counter, cnt_str);
for (int i=0; i<key.Length; i++)
if (i > cnt_str.Length || key[i] != cnt_str[i])
return false;
return true;
}
public int at(int counter)
{
return this.at(counter, null);
}
public int at(int counter, byte[] output)
{
return base.generate(counter, output);
}
public bool verify(int key, int counter)
{
return this.verify(
Encoding.ASCII.GetBytes(key.ToString("G").PadLeft(base.digits, '0')),
counter);
}
public bool verify(byte[] key, int counter)
{
return this.compare(key, counter);
}
}
public static class OTPUri {
public static String build_uri(OTP data, String issuer, String name, int counter) {
String cissuer = Uri.EscapeDataString(issuer);
String postarg = "";
String otp_type = "";
switch(data.method) {
case OTPType.TOTP:
otp_type = "totp";
postarg += "&period=" + ((TOTP)data).interval;
break;
case OTPType.HOTP:
otp_type = "hotp";
postarg += "&counter=" + counter;
break;
default:
otp_type = "otp";
break;
}
String pre = "otpauth://" + otp_type + "/" + cissuer + ":" + Uri.EscapeDataString(name);
String args =
"?secret=" + Uri.EscapeDataString(Encoding.ASCII.GetString(data.base32_secret)) +
"&issuer=" + cissuer +
"&algorithm=" + Uri.EscapeDataString(Encoding.ASCII.GetString(data.digest)) +
"&digits=" + data.digits.ToString("G");
return pre + args + postarg;
}
}
}