universal.h (6485B)
1 /* 2 * universal.h 3 * This file is part of lib-gpu-verify. 4 * 5 * lib-gpu-verify 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 3 of the License, or 8 * (at your option) any later version. 9 * 10 * lib-gpu-verify 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 * Created by Cedric Zwahlen 16 * 17 */ 18 19 #ifndef universal_h 20 #define universal_h 21 22 #include <stdio.h> 23 #include <time.h> 24 #include <stdint.h> 25 26 27 28 29 struct gpuv_public_key; 30 31 struct gpuv_signature_message; 32 33 struct gpuv_batch; 34 35 struct gpuv_info; 36 37 struct gpuv_state; 38 39 enum GPUV_VARIANT { 40 41 GPUV_GPU_REGULAR = 0, 42 GPUV_GPU_MONTGOMERY = 1, 43 GPUV_CPU = 2 44 45 }; 46 47 enum GPUV_BIT_LENGTH { 48 49 GPUV_BIT_LENGTH_2048 = 2048, 50 51 }; 52 53 /** 54 * @brief Prepare gcry library. 55 * 56 * This function must be called at the beginning of the program, if at any point the CPU mode should be used. 57 * If your program already initialises gcry, calling this function is not necessary. 58 * 59 */ 60 void gpuv_prepare_gcry(void); 61 62 /** 63 * 64 * @param arg1 pointer to arbitrary user data 65 * @param arg2 1 if all signatures are valid, 0 if at least one signature is false 66 * @param arg3 the time taken to verify the signatures 67 * @param arg4 the length of the res array 68 * @param arg5 a pointer to the result array. A bitmask containing the verification result for each signature. If a bit at position x is 1, signature x is valid, otherwise signature x is not valid. 69 * 70 * @return 0 on success. 1 on error. 71 */ 72 typedef void (*gpuv_cls)(void *cls, int valid, struct timespec time, unsigned long len, uint32_t *res); 73 74 /** 75 * @brief Initialise a info struct. 76 * 77 * Provides an struct that contains information about the GPUs and capabilities of the system. If no GPU is available, NULL is returned. 78 * 79 * @param arg1 which kernel to use 80 * @param arg2 what length of RSA key 81 * 82 * @return An initialised info struct pointer 83 */ 84 struct gpuv_info * gpuv_init(enum GPUV_VARIANT variant, enum GPUV_BIT_LENGTH bit_length); 85 86 /** 87 * @brief Initialise a batch struct. 88 * 89 * Prepares and allocates a batch struct, used to store references to public keys, signatures their corresponding messages. 90 * 91 * @return An initialised batch struct pointer 92 */ 93 struct gpuv_batch * gpuv_prepare_batch(void); 94 95 /** 96 * @brief Add a single signature message pair to a batch. 97 * 98 * Add a signature message pair to a batch. The struct is copied, so it may be freed after adding it to the batch. 99 * 100 * @param arg1 the batch to which to add the pair 101 * @param arg2 the pair to add 102 * 103 * @return 0 on success. 1 on failure to allocate more storage. 104 */ 105 int gpuv_add_to_batch(struct gpuv_batch * batch, struct gpuv_signature_message * sigmem); 106 107 /** 108 * @brief Initialise a signature message struct. 109 * 110 * Prepare and initialise a single signature message pair. 111 * 112 * @param arg1 the public key with which the signature should be associated. 113 * 114 * @return An initialised signature message struct pointer 115 */ 116 struct gpuv_signature_message * gpuv_prepare_sig_msg(struct gpuv_public_key *pubkey); 117 118 /** 119 * @brief Add a signature to a signature message. 120 * 121 * Add a signature to a signature message structure. 122 * 123 * @param arg1 the signature message structure pointer to add the signature to 124 * @param arg2 the length of the signature in words. For the Montgomery kernel and CPU, the word length is expected to be 64 bits, for the Regular kernel the word length is expected to be 32 bits. 125 * @param arg3 a pointer to the signature. The least significant word should the first word pointed to. Do not free it until gpuv_prepare is called. 126 * 127 */ 128 void gpuv_add_signature(struct gpuv_signature_message * sig_msg, unsigned long len, void *s); 129 130 /** 131 * @brief Add a message to a signature message. 132 * 133 * Add a message to a signature message structure. 134 * 135 * @param arg1 the signature message structure pointer to add the message to 136 * @param arg2 the length of the message in words. For the Montgomery kernel and CPU, the word length is expected to be 64 bits, for the Regular kernel the word length is expected to be 32 bits. 137 * @param arg3 a pointer to the message. The least significant word should the first word pointed to. Do not free it until gpuv_prepare is called. 138 * 139 */ 140 void gpuv_add_message(struct gpuv_signature_message * sig_msg, unsigned long len, void *m); 141 /** 142 * @brief Prepare a public key struct. 143 * 144 * Prepare and initialise a single public key struct. 145 * 146 * @param arg1 the public exponent 147 * @param arg2 the length of the modulus in words. For the Montgomery kernel and CPU, the word length is expected to be 64 bits, for the Regular kernel the word length is expected to be 32 bits. 148 * @param arg3 a pointer to the modulus. The least significant word should the first word pointed to. Do not free it until gpuv_prepare is called. 149 * 150 */ 151 struct gpuv_public_key * gpuv_prepare_pubkey(unsigned long e, unsigned long len_n, void *n); 152 153 /** 154 * @brief Free a batch struct. 155 * 156 * Freeing a batch also releases all associated public key structures. 157 * 158 */ 159 void gpuv_free_batch(struct gpuv_batch * batch); 160 161 /** 162 * @brief Free a state struct. 163 * 164 */ 165 void gpuv_free_state(struct gpuv_state * state); 166 167 /** 168 * @brief Prepare a state struct. 169 * 170 * Prepare and initialise a state object. When using the Montgomery Kernel, this function performs precalculations. 171 * 172 * @param arg1 a vaild state struct pointer 173 * 174 * @return An initialised state struct pointer 175 */ 176 struct gpuv_state * gpuv_prepare(struct gpuv_info *info, struct gpuv_batch * batch); 177 178 /** 179 * @brief Verify the batch of signatures. 180 * 181 * Begin verifying the signatures in the batch. If calculations are to be performed on the GPU, this function returns immediately. 182 * 183 * @param arg1 a valid state containing the signatures to verify and a reference to an info structure 184 * @param arg2 a closure that is called once the kernel has processed the signatures in the batch 185 * @param arg3 a pointer to custom user data that is passed to cls. 186 * @param arg4 the batch to process. 187 * 188 * @return 0 on success. 1 on error. 189 */ 190 int gpuv_start(struct gpuv_state *state, gpuv_cls cls, void * arg, struct gpuv_batch *batch); 191 192 /** 193 * @brief Free an info struct. 194 * 195 */ 196 void gpuv_finish(struct gpuv_info * info); 197 198 199 200 #endif /* universal_h */