BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file OTPGenerator.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 which generates OTPs for use in sending packets.
32 */
33  
34 #ifndef BADVPN_SECURITY_OTPGENERATOR_H
35 #define BADVPN_SECURITY_OTPGENERATOR_H
36  
37 #include <misc/debug.h>
38 #include <security/OTPCalculator.h>
39 #include <base/DebugObject.h>
40 #include <threadwork/BThreadWork.h>
41  
42 /**
43 * Handler called when OTP generation for a seed is finished.
44 * The OTP position is reset to zero before the handler is called.
45 *
46 * @param user as in {@link OTPGenerator_Init}
47 */
48 typedef void (*OTPGenerator_handler) (void *user);
49  
50 /**
51 * Object which generates OTPs for use in sending packets.
52 */
53 typedef struct {
54 int num_otps;
55 int cipher;
56 BThreadWorkDispatcher *twd;
57 OTPGenerator_handler handler;
58 void *user;
59 int position;
60 int cur_calc;
61 OTPCalculator calc[2];
62 otp_t *otps[2];
63 int tw_have;
64 BThreadWork tw;
65 uint8_t tw_key[BENCRYPTION_MAX_KEY_SIZE];
66 uint8_t tw_iv[BENCRYPTION_MAX_BLOCK_SIZE];
67 DebugObject d_obj;
68 } OTPGenerator;
69  
70 /**
71 * Initializes the generator.
72 * The object is initialized with number of used OTPs = num_otps.
73 * {@link BSecurity_GlobalInitThreadSafe} must have been done if
74 * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
75 *
76 * @param g the object
77 * @param num_otps number of OTPs to generate from a seed. Must be >=0.
78 * @param cipher encryption cipher for calculating the OTPs. Must be valid
79 * according to {@link BEncryption_cipher_valid}.
80 * @param twd thread work dispatcher
81 * @param handler handler to call when generation of new OTPs is complete,
82 * after {@link OTPGenerator_SetSeed} was called.
83 * @param user argument to handler
84 * @return 1 on success, 0 on failure
85 */
86 int OTPGenerator_Init (OTPGenerator *g, int num_otps, int cipher, BThreadWorkDispatcher *twd, OTPGenerator_handler handler, void *user) WARN_UNUSED;
87  
88 /**
89 * Frees the generator.
90 *
91 * @param g the object
92 */
93 void OTPGenerator_Free (OTPGenerator *g);
94  
95 /**
96 * Starts generating OTPs for a seed.
97 * When generation is complete and the new OTPs may be used, the {@link OTPGenerator_handler}
98 * handler will be called.
99 * If OTPs are still being generated for a previous seed, it will be forgotten.
100 * This call by itself does not affect the OTP position; rather the position is set to zero
101 * before the handler is called.
102 *
103 * @param g the object
104 * @param key encryption key
105 * @param iv initialization vector
106 */
107 void OTPGenerator_SetSeed (OTPGenerator *g, uint8_t *key, uint8_t *iv);
108  
109 /**
110 * Returns the number of OTPs used up from the current seed so far.
111 * If there is no seed yet, returns num_otps.
112 *
113 * @param g the object
114 * @return number of used OTPs
115 */
116 int OTPGenerator_GetPosition (OTPGenerator *g);
117  
118 /**
119 * Sets the number of used OTPs to num_otps.
120 *
121 * @param g the object
122 */
123 void OTPGenerator_Reset (OTPGenerator *g);
124  
125 /**
126 * Generates a single OTP.
127 * The number of used OTPs must be < num_otps.
128 * The number of used OTPs is incremented.
129 *
130 * @param g the object
131 */
132 otp_t OTPGenerator_GetOTP (OTPGenerator *g);
133  
134 #endif