-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcryptorand.h
114 lines (100 loc) · 3.27 KB
/
cryptorand.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* Copyright (c) 2016, Kevin Lewi
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _CRYPTORAND_H_
#define _CRYPTORAND_H_
#define _GNU_SOURCE /* for fmemopen() */
#define AES_ALGORITHM EVP_aes_256_ctr()
#define DEFAULT_SEED "12345678901234567890123456789012"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <gmp.h>
#include <mpfr.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include "../flint/fmpz.h"
extern int errno;
/**
* Contains the state of a cryptorand_t type
*/
typedef struct _cryptorand_state_struct {
char cryptorand_init;
unsigned long ctr; // used to increment IV
EVP_CIPHER_CTX *ctx;
unsigned char key[SHA256_DIGEST_LENGTH];
unsigned char *iv;
} cryptorand_t[1];
/**
* Initializes a cryptorand_t type. The default seed is used.
*
* @param state The uninitialized state
* @return void
*/
void cryptorand_init(cryptorand_t state);
/**
* Initializes a cryptorand_t type with a seed string and a tweak string.
*
* @param state The uninitialized state
* @param seed A string used for the seed
* @param additional The "tweak", which can be determininistically chosen
* @return void
*/
void cryptorand_initseed(cryptorand_t state, char *seed, char *additional);
/**
* Clears the cryptorand_t type.
*
* @param state The state to clear
* @return void
*/
void cryptorand_clear(cryptorand_t state);
/**
* Selects a random integer in the range [0,m-1] and stores it into f, which is
* an fmpz_t.
*
* This function simply calls mpz_urandomb_crypto() and converts the resulting
* mpz_t into an fmpz_t.
*
* @param f The fmpz_t type to contain the random number
* @param state The cryptorand_t state to use
* @param m The modulus
* @return void
*/
void fmpz_randm_crypto(fmpz_t f, cryptorand_t state, const fmpz_t m);
/**
* Selects a random integer in the range [0,m-1] and stores it into rop, which
* is an mpz_t.
*
* When the modulus is not a power of 2, this function performs rejection
* sampling until the result is within the range [0,m-1].
*
* @param rop The mpz_t type to contain the random number
* @param state The cryptorand_t state to use
* @param m The modulus
* @return void
*/
void mpz_urandomb_crypto(mpz_t rop, cryptorand_t state, mp_bitcnt_t m);
/**
* Samples n random bits and stores them into rop as an mpz_t.
*
* @param rop The resutling mpz_t to contain the number [0,2^n-1].
* @param state The cryptorand_t state to use
* @param n The number of random bits to sample
* @return void
*/
void mpz_urandomm_crypto(mpz_t rop, cryptorand_t state, const mpz_t n);
#endif /* _CRYPTORAND_H_ */