annotate src/keypair.c @ 12:e4b60d041491

Make sure that the session buffer is zero
author Ivo Smits <Ivo@UCIS.nl>
date Sun, 05 Dec 2010 23:12:04 +0100
parents 6d86596d8884
children dfac56805c77
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
1 /* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved.
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
2 Redistribution and use in source and binary forms, with or without modification, are
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
3 permitted provided that the following conditions are met:
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
4
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
5 1. Redistributions of source code must retain the above copyright notice, this list of
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
6 conditions and the following disclaimer.
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
7
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
8 2. Redistributions in binary form must reproduce the above copyright notice, this list
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
9 of conditions and the following disclaimer in the documentation and/or other materials
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
10 provided with the distribution.
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
11
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
12 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
13 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
15 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
16 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
17 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
18 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
19 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
20 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
21
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
22 The views and conclusions contained in the software and documentation are those of the
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
23 authors and should not be interpreted as representing official policies, either expressed
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
24 or implied, of Ivo Smits.*/
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
25
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
26 #include "common.c"
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
27 #include "crypto_box.h"
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
28 #include <time.h>
8
6d86596d8884 Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents: 5
diff changeset
29 #include <fcntl.h>
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
30
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
31 int main() {
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
32 print_header();
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
33
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
34 unsigned char cpublickey[crypto_box_PUBLICKEYBYTES];
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
35 unsigned char csecretkey[crypto_box_SECRETKEYBYTES];
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
36 int i;
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
37
8
6d86596d8884 Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents: 5
diff changeset
38 fprintf(stderr, "Please feed 32 bytes of random data to stdin.\n");
6d86596d8884 Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents: 5
diff changeset
39 fprintf(stderr, "Example (slow but secure): ./quicktun.keypair < /dev/random\n");
6d86596d8884 Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents: 5
diff changeset
40 fprintf(stderr, "Example (fast but insecure): ./quicktun.keypair < /dev/urandom\n");
6d86596d8884 Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents: 5
diff changeset
41
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
42 crypto_box_keypair(cpublickey, csecretkey);
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
43
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
44 printf("SECRET: ");
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
45 for (i = 0; i < crypto_box_SECRETKEYBYTES; i++) printf("%02x", csecretkey[i]);
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
46 printf("\n");
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
47
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
48 printf("PUBLIC: ");
5
9d449e899402 Fixed keypair code
ivo <ivo@UFO-Net.nl>
parents: 0
diff changeset
49 for (i = 0; i < crypto_box_PUBLICKEYBYTES; i++) printf("%02x", cpublickey[i]);
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
50 printf("\n");
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
51
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
52 return 0;
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
53 }
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
54
8
6d86596d8884 Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents: 5
diff changeset
55 int randombytes(char* bytes) {
6d86596d8884 Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents: 5
diff changeset
56 int len = fread(bytes, 1, crypto_box_SECRETKEYBYTES, stdin);
6d86596d8884 Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents: 5
diff changeset
57 if (len < crypto_box_SECRETKEYBYTES) return errorexitp("Error or end of file on STDIN");
6d86596d8884 Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents: 5
diff changeset
58 /* char* b;
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
59 srand(time(NULL));
8
6d86596d8884 Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents: 5
diff changeset
60 for (b = bytes; b < bytes + crypto_box_SECRETKEYBYTES; b++) *b = rand() % 255;*/
6d86596d8884 Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents: 5
diff changeset
61 return 0;
5
9d449e899402 Fixed keypair code
ivo <ivo@UFO-Net.nl>
parents: 0
diff changeset
62 }