aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/include/otputil.h
blob: cb690d49a91cae2b813cbff898a2052d9aaa6997 (plain) (blame)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 *  OTP Utilities
 *  Copyright (C) 2020 David M. Syzdek <david@syzdek.net>.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are
 *  met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of David M. Syzdek nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 *  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M SYZDEK BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 *  SUCH DAMAGE.
 */
/*
 *  @file include/otputil.h
 */
#ifndef __OTPUTIL_H
#define __OTPUTIL_H 1


///////////////
//           //
//  Headers  //
//           //
///////////////
#pragma mark - Headers

#include <stddef.h>
#include <inttypes.h>
#include <sys/types.h>
#include <stdio.h>
#include <regex.h>


//////////////
//          //
//  Macros  //
//          //
//////////////
#pragma mark - Macros

// Exports function type
#undef OTPUTIL_C_DECLS
#undef OTPUTIL_BEGIN_C_DECLS
#undef OTPUTIL_END_C_DECLS
#undef _OTPUTIL_I
#undef _OTPUTIL_F
#undef _OTPUTIL_V
#if defined(__cplusplus) || defined(c_plusplus)
#   define _OTPUTIL_I             extern "C" inline
#   define OTPUTIL_C_DECLS        "C"             ///< exports as C functions
#   define OTPUTIL_BEGIN_C_DECLS  extern "C" {    ///< exports as C functions
#   define OTPUTIL_END_C_DECLS    }               ///< exports as C functions
#else
#   define _OTPUTIL_I             inline
#   define OTPUTIL_C_DECLS        /* empty */     ///< exports as C functions
#   define OTPUTIL_BEGIN_C_DECLS  /* empty */     ///< exports as C functions
#   define OTPUTIL_END_C_DECLS    /* empty */     ///< exports as C functions
#endif
#ifdef WIN32
#   ifdef _LIB_LIBOTPUTIL_H
#      define _OTPUTIL_F   extern OTPUTIL_C_DECLS __declspec(dllexport)   ///< used for library calls
#      define _OTPUTIL_V   extern OTPUTIL_C_DECLS __declspec(dllexport)   ///< used for library calls
#   else
#      define _OTPUTIL_F   extern OTPUTIL_C_DECLS __declspec(dllimport)   ///< used for library calls
#      define _OTPUTIL_V   extern OTPUTIL_C_DECLS __declspec(dllimport)   ///< used for library calls
#   endif
#else
#   ifdef _LIB_LIBOTPUTIL_H
#      define _OTPUTIL_F   /* empty */                                      ///< used for library calls
#      define _OTPUTIL_V   extern OTPUTIL_C_DECLS                         ///< used for library calls
#   else
#      define _OTPUTIL_F   extern OTPUTIL_C_DECLS                         ///< used for library calls
#      define _OTPUTIL_V   extern OTPUTIL_C_DECLS                         ///< used for library calls
#   endif
#endif


///////////////////
//               //
//  Definitions  //
//               //
///////////////////
#pragma mark - Definitions

#define OTPUTIL_SUCCESS             0x0000 ///< operation was successful
#define OTPUTIL_ENOTSUP             0x0001 ///< methd or feature not supported
#define OTPUTIL_EBADDATA            0x0002 ///< invalid data
#define OTPUTIL_ENOBUFS             0x0003 ///< no buffer space available
#define OTPUTIL_ENOMEM              0x0004 ///< out of virtual memory
#define OTPUTIL_EOPTION             0x0005 ///< invalid options
#define OTPUTIL_EOPTVAL             0x0006 ///< invalid option value


// general options
#define OTPUTIL_OPT_DESC            0x0001   // (char *)
#define OTPUTIL_OPT_METHOD          0x0002   // (int *)
#define OTPUTIL_OPT_DIGITS          0x0003   // (int *) sets digits for both HOTP and TOTP
#define OTPUTIL_OPT_HMAC            0x0004   // (int *) sets HMAC for both HOTP and TOTP
// HOTP options (RFC4226)
#define OTPUTIL_OPT_HOTP_K          0x0100   // (otputil_bv_t *)
#define OTPUTIL_OPT_HOTP_KSTR       0x0101   // (char *) base32 encoded K
#define OTPUTIL_OPT_HOTP_C          0x0102   // (uint64_t *)
#define OTPUTIL_OPT_HOTP_DIGITS     0x0103   // (int *)
#define OTPUTIL_OPT_HOTP_HMAC       0x0104   // (int *)
// TOTP options (RFC6238)
#define OTPUTIL_OPT_TOTP_K          0x0200   // (otputil_bv_t *)
#define OTPUTIL_OPT_TOTP_KSTR       0x0201   // (char *) base32 encoded K
#define OTPUTIL_OPT_TOTP_T0         0x0202   // (uint64_t *)
#define OTPUTIL_OPT_TOTP_X          0x0203   // (uint64_t *)
#define OTPUTIL_OPT_TOTP_TIME       0x0204   // (uint64_t *) current UNIX time
#define OTPUTIL_OPT_TOTP_DIGITS     0x0205   // (int *)
#define OTPUTIL_OPT_TOTP_HMAC       0x0206   // (int *)


#define OTPUTIL_MD_NONE             0
#define OTPUTIL_MD_SHA1             1
#define OTPUTIL_MD_SHA256           2
#define OTPUTIL_MD_SHA512           3
#define OTPUTIL_MD_SHA3_256         4
#define OTPUTIL_MD_SHA3_512         5
#define OTPUTIL_MD_MD4              6
#define OTPUTIL_MD_MD5              7


#define OTPUTIL_ENC_HEX             1
#define OTPUTIL_ENC_SIXWORD         2
#define OTPUTIL_ENC_ALTDICT         3


#define OTPUTIL_METH_RFC4226        0x0001
#define OTPUTIL_METH_RFC6238        0x0002
#define OTPUTIL_METH_HOTP           OTPUTIL_METH_RFC4226
#define OTPUTIL_METH_TOTP           OTPUTIL_METH_RFC6238


#define OTPUTIL_DFLT_METH           OTPUTIL_METH_TOTP
// HOTP defaults
#define OTPUTIL_DFLT_HOTP_C         1ULL
#define OTPUTIL_DFLT_HOTP_DIGITS    6
#define OTPUTIL_DFLT_HOTP_HMAC      OTPUTIL_MD_SHA1
// TOTP defaults
#define OTPUTIL_DFLT_TOTP_T0        0ULL
#define OTPUTIL_DFLT_TOTP_X         30ULL
#define OTPUTIL_DFLT_TOTP_TIME      0ULL           // current UNIX time
#define OTPUTIL_DFLT_TOTP_DIGITS    6
#define OTPUTIL_DFLT_TOTP_HMAC      OTPUTIL_MD_SHA1


#define OTPUTIL_MAX_CODE_SIZE       16
#define OTPUTIL_MAX_ENCODE_SIZE     128
#define OTPUTIL_MAX_DECODE_SIZE     128
#define OTPUTIL_MAX_WORD_SIZE       8

#define OTPUTIL_OTP_PASS_MIN_LEN    10
#define OTPUTIL_OTP_PASS_MAX_LEN    127
#define OTPUTIL_OTP_SEED_MIN_LEN    1
#define OTPUTIL_OTP_SEED_MAX_LEN    16


/////////////////
//             //
//  Datatypes  //
//             //
/////////////////
#pragma mark - Datatypes

typedef struct _otputil_secret otputil_t;


typedef struct _otputil_berval otputil_bv_t;
struct _otputil_berval
{
   size_t         bv_len;
   void *         bv_val;
};


//////////////////
//              //
//  Prototypes  //
//              //
//////////////////
#pragma mark - Prototypes
OTPUTIL_BEGIN_C_DECLS

//----------------//
// BER prototypes //
//----------------//
#pragma mark BER prototypes

_OTPUTIL_F otputil_bv_t *
otputil_base32bv(
         const char *                  str );


_OTPUTIL_F otputil_bv_t *
otputil_bvalloc(
         const void *                  val,
         size_t                        len );


_OTPUTIL_F char *
otputil_bvbase32(
         const otputil_bv_t *          bv );


_OTPUTIL_F int
otputil_bvcmp(
         const void *                  a,
         const void *                  b );


_OTPUTIL_F otputil_bv_t *
otputil_bvdup(
         const otputil_bv_t *          bv );


_OTPUTIL_F void
otputil_bvfree(
         otputil_bv_t *                bv );


//------------------//
// error prototypes //
//------------------//
#pragma mark error prototypes

_OTPUTIL_F const char *
otputil_err2string(
         int                           err );


//-------------------//
// memory prototypes //
//-------------------//
#pragma mark memory prototypes

_OTPUTIL_F void
otputil_free(
         otputil_t *                   tud );


_OTPUTIL_F int
otputil_get_param(
         otputil_t *                   tud,
         int                           option,
         void *                        outvalue );


_OTPUTIL_F int
otputil_initialize(
         otputil_t **                  tudp );


_OTPUTIL_F int
otputil_set_param(
         otputil_t *                   tud,
         int                           option,
         const void *                  invalue );


//----------------//
// misc functions //
//----------------//
#pragma mark misc functions

_OTPUTIL_F char *
otputil_getpass(
         const char *                  prompt,
         char *                        pass,
         size_t                        passlen );


_OTPUTIL_F const char *
otputil_md2str(
         int                           hmac );


_OTPUTIL_F int
otputil_str2md(
         const char *                  str );


//---------------------//
// Front-end functions //
//---------------------//
#pragma mark front-end functions

_OTPUTIL_F int
otputil_code(
         otputil_t *                   tud );


_OTPUTIL_F char *
otputil_str(
         otputil_t *                   tud,
         char *                        dst,
         size_t                        dstlen );


_OTPUTIL_F uint64_t
otputil_timer(
         otputil_t *                   tud );


//---------------//
// HOTP functions //
//---------------//
#pragma mark HOTP functions (RFC 4226)

_OTPUTIL_F int
otputil_hotp_code(
         const otputil_bv_t *          hotp_k,
         uint64_t                      hotp_c,
         int                           hotp_hmac,
         int                           hotp_digits );


_OTPUTIL_F char *
otputil_hotp_str(
         const otputil_bv_t *          hotp_k,
         uint64_t                      hotp_c,
         int                           hotp_hmac,
         int                           hotp_digits,
         char *                        dst,
         size_t                        dstlen );


//---------------//
// OTP functions //
//---------------//
#pragma mark OTP functions (RFC 2289)

_OTPUTIL_F uint64_t
otputil_otp_code(
         const char *                  otp_pass,
         const char *                  otp_seed,
         int                           otp_seq,
         int                           otp_hash );


_OTPUTIL_F otputil_bv_t *
otputil_otp_decode(
         const char *                  src,
         otputil_bv_t *                dst,
         int                           altdict_hash );


_OTPUTIL_F size_t
otputil_otp_decode_len(
         const char *                  str );


_OTPUTIL_F const char *
otputil_otp_encode(
         const otputil_bv_t *          bv,
         char *                        dst,
         size_t                        dstlen,
         int                           methd );


_OTPUTIL_F size_t
otputil_otp_encode_len(
         const otputil_bv_t *          bv );


//----------------//
// S/KEY functions //
//----------------//
#pragma mark S/KEY functions (RFC 1760)

_OTPUTIL_F int
otputil_skey_dict_value(
         const char *                  word );


_OTPUTIL_F const char *
otputil_skey_dict_word(
         int                           value );


//---------------//
// TOTP functions //
//---------------//
#pragma mark TOTP functions (RFC 6238)

_OTPUTIL_F int
otputil_totp_code(
         const otputil_bv_t *          totp_k,
         uint64_t                      totp_t0,
         uint64_t                      totp_tx,
         uint64_t                      totp_time,
         int                           totp_hmac,
         int                           totp_digits );


_OTPUTIL_F char *
otputil_totp_str(
         const otputil_bv_t *          totp_k,
         uint64_t                      totp_t0,
         uint64_t                      totp_tx,
         uint64_t                      totp_time,
         int                           totp_hmac,
         int                           totp_digits,
         char *                        dst,
         size_t                        dstlen );


_OTPUTIL_F uint64_t
otputil_totp_timer(
         uint64_t                      totp_t0,
         uint64_t                      totp_tx,
         uint64_t                      totp_time );


OTPUTIL_END_C_DECLS
#endif /* end of header file */