BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file OTPGenerator.c
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  
30 #include <string.h>
31  
32 #include <security/OTPGenerator.h>
33  
34 static void work_func (OTPGenerator *g)
35 {
36 g->otps[!g->cur_calc] = OTPCalculator_Generate(&g->calc[!g->cur_calc], g->tw_key, g->tw_iv, 1);
37 }
38  
39 static void work_done_handler (OTPGenerator *g)
40 {
41 ASSERT(g->tw_have)
42 DebugObject_Access(&g->d_obj);
43  
44 // free work
45 BThreadWork_Free(&g->tw);
46 g->tw_have = 0;
47  
48 // use new OTPs
49 g->cur_calc = !g->cur_calc;
50 g->position = 0;
51  
52 // call handler
53 g->handler(g->user);
54 return;
55 }
56  
57 int OTPGenerator_Init (OTPGenerator *g, int num_otps, int cipher, BThreadWorkDispatcher *twd, OTPGenerator_handler handler, void *user)
58 {
59 ASSERT(num_otps >= 0)
60 ASSERT(BEncryption_cipher_valid(cipher))
61  
62 // init arguments
63 g->num_otps = num_otps;
64 g->cipher = cipher;
65 g->twd = twd;
66 g->handler = handler;
67 g->user = user;
68  
69 // init position
70 g->position = g->num_otps;
71  
72 // init calculator
73 if (!OTPCalculator_Init(&g->calc[0], g->num_otps, g->cipher)) {
74 goto fail0;
75 }
76  
77 // init calculator
78 if (!OTPCalculator_Init(&g->calc[1], g->num_otps, g->cipher)) {
79 goto fail1;
80 }
81  
82 // set current calculator
83 g->cur_calc = 0;
84  
85 // have no work
86 g->tw_have = 0;
87  
88 DebugObject_Init(&g->d_obj);
89 return 1;
90  
91 fail1:
92 OTPCalculator_Free(&g->calc[0]);
93 fail0:
94 return 0;
95 }
96  
97 void OTPGenerator_Free (OTPGenerator *g)
98 {
99 DebugObject_Free(&g->d_obj);
100  
101 // free work
102 if (g->tw_have) {
103 BThreadWork_Free(&g->tw);
104 }
105  
106 // free calculator
107 OTPCalculator_Free(&g->calc[1]);
108  
109 // free calculator
110 OTPCalculator_Free(&g->calc[0]);
111 }
112  
113 void OTPGenerator_SetSeed (OTPGenerator *g, uint8_t *key, uint8_t *iv)
114 {
115 DebugObject_Access(&g->d_obj);
116  
117 // free existing work
118 if (g->tw_have) {
119 BThreadWork_Free(&g->tw);
120 }
121  
122 // copy key and IV
123 memcpy(g->tw_key, key, BEncryption_cipher_key_size(g->cipher));
124 memcpy(g->tw_iv, iv, BEncryption_cipher_block_size(g->cipher));
125  
126 // start work
127 BThreadWork_Init(&g->tw, g->twd, (BThreadWork_handler_done)work_done_handler, g, (BThreadWork_work_func)work_func, g);
128  
129 // set have work
130 g->tw_have = 1;
131 }
132  
133 int OTPGenerator_GetPosition (OTPGenerator *g)
134 {
135 DebugObject_Access(&g->d_obj);
136  
137 return g->position;
138 }
139  
140 void OTPGenerator_Reset (OTPGenerator *g)
141 {
142 DebugObject_Access(&g->d_obj);
143  
144 // free existing work
145 if (g->tw_have) {
146 BThreadWork_Free(&g->tw);
147 g->tw_have = 0;
148 }
149  
150 g->position = g->num_otps;
151 }
152  
153 otp_t OTPGenerator_GetOTP (OTPGenerator *g)
154 {
155 ASSERT(g->position < g->num_otps)
156 DebugObject_Access(&g->d_obj);
157  
158 return g->otps[g->cur_calc][g->position++];
159 }