BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file OTPCalculator.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 <limits.h>
31  
32 #include <misc/balloc.h>
33  
34 #include <security/OTPCalculator.h>
35  
36 int OTPCalculator_Init (OTPCalculator *calc, int num_otps, int cipher)
37 {
38 ASSERT(num_otps >= 0)
39 ASSERT(BEncryption_cipher_valid(cipher))
40  
41 // init arguments
42 calc->num_otps = num_otps;
43 calc->cipher = cipher;
44  
45 // remember block size
46 calc->block_size = BEncryption_cipher_block_size(calc->cipher);
47  
48 // calculate number of blocks
49 if (calc->num_otps > SIZE_MAX / sizeof(otp_t)) {
50 goto fail0;
51 }
52 calc->num_blocks = bdivide_up(calc->num_otps * sizeof(otp_t), calc->block_size);
53  
54 // allocate buffer
55 if (!(calc->data = (otp_t *)BAllocArray(calc->num_blocks, calc->block_size))) {
56 goto fail0;
57 }
58  
59 // init debug object
60 DebugObject_Init(&calc->d_obj);
61  
62 return 1;
63  
64 fail0:
65 return 0;
66 }
67  
68 void OTPCalculator_Free (OTPCalculator *calc)
69 {
70 // free debug object
71 DebugObject_Free(&calc->d_obj);
72  
73 // free buffer
74 BFree(calc->data);
75 }
76  
77 otp_t * OTPCalculator_Generate (OTPCalculator *calc, uint8_t *key, uint8_t *iv, int shuffle)
78 {
79 ASSERT(shuffle == 0 || shuffle == 1)
80  
81 // copy IV so it can be updated
82 uint8_t iv_work[BENCRYPTION_MAX_BLOCK_SIZE];
83 memcpy(iv_work, iv, calc->block_size);
84  
85 // create zero block
86 uint8_t zero[BENCRYPTION_MAX_BLOCK_SIZE];
87 memset(zero, 0, calc->block_size);
88  
89 // init encryptor
90 BEncryption encryptor;
91 BEncryption_Init(&encryptor, BENCRYPTION_MODE_ENCRYPT, calc->cipher, key);
92  
93 // encrypt zero blocks
94 for (size_t i = 0; i < calc->num_blocks; i++) {
95 BEncryption_Encrypt(&encryptor, zero, (uint8_t *)calc->data + i * calc->block_size, calc->block_size, iv_work);
96 }
97  
98 // free encryptor
99 BEncryption_Free(&encryptor);
100  
101 // shuffle if requested
102 if (shuffle) {
103 int i = 0;
104 while (i < calc->num_otps) {
105 uint16_t ints[256];
106 BRandom_randomize((uint8_t *)ints, sizeof(ints));
107 for (int j = 0; j < 256 && i < calc->num_otps; j++) {
108 int newIndex = i + (ints[j] % (calc->num_otps - i));
109 otp_t temp = calc->data[i];
110 calc->data[i] = calc->data[newIndex];
111 calc->data[newIndex] = temp;
112 i++;
113 }
114 }
115 }
116  
117 return calc->data;
118 }