root/tags/omega-1.0.0-rc2/keys/gen-keys.c

Revision 154, 1.6 kB (checked in by malte, 2 years ago)

--

Line 
1
2 #include <stdlib.h>
3
4 #include <string.h>
5
6 #include <stdio.h>
7
8 typedef unsigned int word;
9
10 unsigned char *key;
11 word key_size;
12
13 unsigned char *iv;
14 word iv_size;
15
16 unsigned char alphabet_name[] = "twofish";
17
18 unsigned char alphabet_mode[] = "cfb";
19
20 int rand_seq(unsigned char *dest, int len);
21
22 int check_char(unsigned int ch);
23
24 int main(int argc, char **argv)
25 {
26     if (argc != 3) {
27
28         fprintf(stderr, "Wrong arguments count !\n");
29
30         return 1;
31
32     }
33
34     key_size = (word) atoi(argv[1]);
35
36     iv_size = (word) atoi(argv[2]);
37
38     if (key_size == 0 || iv_size == 0) {
39
40         fprintf(stderr, "Wrong argument syntax !\n");
41
42         return 1;
43     }
44
45     key = calloc((size_t) key_size + 1, sizeof(unsigned char));
46
47     iv = calloc((size_t) iv_size + 1, sizeof(unsigned char));
48
49     if (key == NULL || iv == NULL) {
50
51         fprintf(stderr, "Memory allocation error !\n");
52
53         return 1;
54
55     }
56
57     if (rand_seq(key, key_size) || rand_seq(iv, iv_size)) {
58
59         fprintf(stderr, "rand_seq() error !\n");
60
61         return 1;
62
63     }
64
65     fprintf(stdout, "%s\n%s\n", key, iv);
66
67     free(key); free(iv);
68
69     return 0;
70
71 }
72
73 int check_char(unsigned int ch) {
74
75     if (ch > 32 && ch < 127) return 1;
76
77     return 0;
78
79 }
80
81 int rand_seq(unsigned char *dest, int len) {
82
83     int i, ch, result;
84
85     FILE *fp;
86
87     fp = fopen("/dev/urandom", "rb");
88
89     if (fp == NULL)
90
91         return -1;
92
93     for (i = 0; i < len; i++) {
94
95         do {
96
97             ch = getc(fp);
98
99         } while (!check_char(ch));
100
101         *(dest)++ = (unsigned char) ch;
102     }
103
104     result = fclose(fp);
105
106     if (result)
107
108         return -1;
109
110     return 0;
111 }
112
Note: See TracBrowser for help on using the browser.