Add the brainpoolp512r1 curve and use it.
[tinc] / src / openssl / crypto.c
1 /*
2     crypto.c -- Cryptographic miscellaneous functions and initialisation
3     Copyright (C) 2007-2013 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "../system.h"
21
22 #include <openssl/rand.h>
23 #include <openssl/evp.h>
24 #include <openssl/engine.h>
25
26 #include "../crypto.h"
27
28 #include "brainpool.h"
29
30 EC_GROUP *brainpoolp512r1;
31
32 static void generate_brainpool_curve() {
33         static const char *p = "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3";
34         static const char *A = "7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA";
35         static const char *B = "3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723";
36         static const char *x = "81AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D0098EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F822";
37         static const char *y = "7DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F8111B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892";
38         static const char *q = "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA70330870553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069";
39
40         BIGNUM *bn_p = NULL;
41         BIGNUM *bn_A = NULL;
42         BIGNUM *bn_B = NULL;
43         BIGNUM *bn_x = NULL;
44         BIGNUM *bn_y = NULL;
45         BIGNUM *bn_q = NULL;
46
47         BN_hex2bn(&bn_p, p);
48         BN_hex2bn(&bn_A, A);
49         BN_hex2bn(&bn_B, B);
50         BN_hex2bn(&bn_x, x);
51         BN_hex2bn(&bn_y, y);
52         BN_hex2bn(&bn_q, q);
53
54         BN_CTX *ctx = BN_CTX_new();
55
56         if(!bn_p || !bn_A || !bn_B || !bn_x || !bn_y || !bn_q || !ctx)
57                 abort();
58
59         brainpoolp512r1 = EC_GROUP_new_curve_GFp(bn_p, bn_A, bn_B, ctx);
60
61         if(!brainpoolp512r1)
62                 abort();
63
64         EC_POINT *generator = EC_POINT_new(brainpoolp512r1);
65
66         if(!generator)
67                 abort();
68
69         if(EC_POINT_set_affine_coordinates_GFp(brainpoolp512r1, generator, bn_x, bn_y, ctx) != 1)
70                 abort();
71
72         if(EC_GROUP_set_generator(brainpoolp512r1, generator, bn_q, NULL) != 1)
73                 abort();
74
75         EC_POINT_free(generator);
76         BN_CTX_free(ctx);
77         BN_free(bn_p);
78         BN_free(bn_A);
79         BN_free(bn_B);
80         BN_free(bn_x);
81         BN_free(bn_y);
82         BN_free(bn_q);
83 }
84
85 void crypto_init(void) {
86         RAND_load_file("/dev/urandom", 1024);
87
88         ENGINE_load_builtin_engines();
89         ENGINE_register_all_complete();
90
91         ERR_load_crypto_strings();
92         OpenSSL_add_all_algorithms();
93
94         if(!RAND_status()) {
95                 fprintf(stderr, "Not enough entropy for the PRNG!\n");
96                 abort();
97         }
98
99         generate_brainpool_curve();
100 }
101
102 void crypto_exit(void) {
103         EVP_cleanup();
104 }
105
106 void randomize(void *out, size_t outlen) {
107         RAND_pseudo_bytes(out, outlen);
108 }