2 crypto.c -- Cryptographic miscellaneous functions and initialisation
3 Copyright (C) 2007-2013 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
20 #include "../system.h"
22 #include <openssl/rand.h>
23 #include <openssl/evp.h>
24 #include <openssl/engine.h>
26 #include "../crypto.h"
28 #include "brainpool.h"
30 EC_GROUP *brainpoolp512r1;
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";
54 BN_CTX *ctx = BN_CTX_new();
56 if(!bn_p || !bn_A || !bn_B || !bn_x || !bn_y || !bn_q || !ctx)
59 brainpoolp512r1 = EC_GROUP_new_curve_GFp(bn_p, bn_A, bn_B, ctx);
64 EC_POINT *generator = EC_POINT_new(brainpoolp512r1);
69 if(EC_POINT_set_affine_coordinates_GFp(brainpoolp512r1, generator, bn_x, bn_y, ctx) != 1)
72 if(EC_GROUP_set_generator(brainpoolp512r1, generator, bn_q, NULL) != 1)
75 EC_POINT_free(generator);
85 void crypto_init(void) {
86 RAND_load_file("/dev/urandom", 1024);
88 ENGINE_load_builtin_engines();
89 ENGINE_register_all_complete();
91 ERR_load_crypto_strings();
92 OpenSSL_add_all_algorithms();
95 fprintf(stderr, "Not enough entropy for the PRNG!\n");
99 generate_brainpool_curve();
102 void crypto_exit(void) {
106 void randomize(void *out, size_t outlen) {
107 RAND_pseudo_bytes(out, outlen);