BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file OTPChecker.h
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Object that checks OTPs agains known seeds.
32 */
33  
34 #ifndef BADVPN_SECURITY_OTPCHECKER_H
35 #define BADVPN_SECURITY_OTPCHECKER_H
36  
37 #include <stdint.h>
38  
39 #include <misc/balign.h>
40 #include <misc/debug.h>
41 #include <misc/modadd.h>
42 #include <security/OTPCalculator.h>
43 #include <base/DebugObject.h>
44 #include <threadwork/BThreadWork.h>
45  
46 struct OTPChecker_entry {
47 otp_t otp;
48 int avail;
49 };
50  
51 struct OTPChecker_table {
52 uint16_t id;
53 struct OTPChecker_entry *entries;
54 };
55  
56 /**
57 * Handler called when OTP generation for a seed is finished and new OTPs
58 * can be recognized.
59 *
60 * @param user as in {@link OTPChecker_Init}
61 */
62 typedef void (*OTPChecker_handler) (void *user);
63  
64 /**
65 * Object that checks OTPs agains known seeds.
66 */
67 typedef struct {
68 BThreadWorkDispatcher *twd;
69 OTPChecker_handler handler;
70 void *user;
71 int num_otps;
72 int cipher;
73 int num_entries;
74 int num_tables;
75 int tables_used;
76 int next_table;
77 OTPCalculator calc;
78 struct OTPChecker_table *tables;
79 struct OTPChecker_entry *entries;
80 int tw_have;
81 BThreadWork tw;
82 uint8_t tw_key[BENCRYPTION_MAX_KEY_SIZE];
83 uint8_t tw_iv[BENCRYPTION_MAX_BLOCK_SIZE];
84 DebugObject d_obj;
85 } OTPChecker;
86  
87 /**
88 * Initializes the checker.
89 * {@link BSecurity_GlobalInitThreadSafe} must have been done if
90 * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
91 *
92 * @param mc the object
93 * @param num_otps number of OTPs to generate from a seed. Must be >0.
94 * @param cipher encryption cipher for calculating the OTPs. Must be valid
95 * according to {@link BEncryption_cipher_valid}.
96 * @param num_tables number of tables to keep, each for one seed. Must be >0.
97 * @param twd thread work dispatcher
98 * @return 1 on success, 0 on failure
99 */
100 int OTPChecker_Init (OTPChecker *mc, int num_otps, int cipher, int num_tables, BThreadWorkDispatcher *twd) WARN_UNUSED;
101  
102 /**
103 * Frees the checker.
104 *
105 * @param mc the object
106 */
107 void OTPChecker_Free (OTPChecker *mc);
108  
109 /**
110 * Starts generating OTPs to recognize for a seed.
111 * OTPs for this seed will not be recognized until the {@link OTPChecker_handler} handler is called.
112 * If OTPs are still being generated for a previous seed, it will be forgotten.
113 *
114 * @param mc the object
115 * @param seed_id seed identifier
116 * @param key encryption key
117 * @param iv initialization vector
118 */
119 void OTPChecker_AddSeed (OTPChecker *mc, uint16_t seed_id, uint8_t *key, uint8_t *iv);
120  
121 /**
122 * Removes all active seeds.
123 *
124 * @param mc the object
125 */
126 void OTPChecker_RemoveSeeds (OTPChecker *mc);
127  
128 /**
129 * Checks an OTP.
130 *
131 * @param mc the object
132 * @param seed_id identifer of seed whom the OTP is claimed to belong to
133 * @param otp OTP to check
134 * @return 1 if the OTP is valid, 0 if not
135 */
136 int OTPChecker_CheckOTP (OTPChecker *mc, uint16_t seed_id, otp_t otp);
137  
138 /**
139 * Sets handlers.
140 *
141 * @param mc the object
142 * @param handler handler to call when generation of new OTPs is complete,
143 * after {@link OTPChecker_AddSeed} was called.
144 * @param user argument to handler
145 */
146 void OTPChecker_SetHandlers (OTPChecker *mc, OTPChecker_handler handler, void *user);
147  
148 #endif