ed25519/src/seed.c

41 lines
655 B
C
Raw Permalink Normal View History

#include "../include/ed25519.h"
2013-01-22 10:58:57 +01:00
2013-01-12 00:54:40 +01:00
#ifndef ED25519_NO_SEED
#ifdef _WIN32
#include <windows.h>
#include <wincrypt.h>
2013-01-12 00:54:40 +01:00
#else
#include <stdio.h>
#endif
int ed25519_create_seed(unsigned char *seed) {
2013-01-12 02:38:34 +01:00
#ifdef _WIN32
2013-04-26 15:32:01 +02:00
HCRYPTPROV prov;
2013-01-12 00:54:40 +01:00
2013-04-26 15:32:01 +02:00
if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
return 1;
}
2013-01-12 00:54:40 +01:00
2013-04-26 15:32:01 +02:00
if (!CryptGenRandom(prov, 32, seed)) {
CryptReleaseContext(prov, 0);
return 1;
}
2013-04-26 15:32:01 +02:00
CryptReleaseContext(prov, 0);
2013-01-12 02:38:34 +01:00
#else
2013-04-26 15:32:01 +02:00
FILE *f = fopen("/dev/urandom", "rb");
2013-04-26 15:32:01 +02:00
if (f == NULL) {
return 1;
}
2013-04-26 15:32:01 +02:00
fread(seed, 1, 32, f);
fclose(f);
2013-01-12 02:38:34 +01:00
#endif
2013-01-22 10:58:57 +01:00
2013-01-12 02:38:34 +01:00
return 0;
2013-01-12 00:54:40 +01:00
}
#endif