aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/include/otputil.h
blob: 4c558e897d5f8c8205787c7941de622d5ec4b6ca (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
/*
 *  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


#define OTPUTIL_OPT_SECRET          0x0000
#define OTPUTIL_OPT_K               0x0001
#define OTPUTIL_OPT_KSTR            0x0002
#define OTPUTIL_OPT_TOTP_T0         0x0003
#define OTPUTIL_OPT_TOTP_X          0x0004
#define OTPUTIL_OPT_TOTP_TIME       0x0005   // current UNIX time
#define OTPUTIL_OPT_HOTP_C          0x0006
#define OTPUTIL_OPT_DESC            0x0007
//                                  0x0008
#define OTPUTIL_OPT_METHOD          0x0009
#define OTPUTIL_OPT_HOTP_DIGITS     0x000a
#define OTPUTIL_OPT_TOTP_DIGITS     0x000b
#define OTPUTIL_OPT_DIGITS          0x000c


#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
#define OTPUTIL_DFLT_HOTP_C         1ULL
#define OTPUTIL_DFLT_HOTP_DIGITS    6
#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_MAX_CODE_SIZE       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 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

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


//---------------//
// OTP functions //
//---------------//
#pragma mark OTP functions

_OTPUTIL_F int
otputil_code(
         otputil_t *                   tud );


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


//---------------//
// HOTP functions //
//---------------//
#pragma mark HOTP functions

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


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


//---------------//
// TOTP functions //
//---------------//
#pragma mark TOTP functions

_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_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_digits,
         char *                        dst,
         size_t                        dstlen );


_OTPUTIL_F uint64_t
otputil_totp_timer(
         otputil_t *                   tud,
         uint64_t                      totp_time );


OTPUTIL_END_C_DECLS
#endif /* end of header file */