28 asn1_node definitions = NULL;
29 asn1_node asn1_element = NULL;
30 char error_desc[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
38 if (rc != ASN1_SUCCESS) {
43 if (rc != ASN1_SUCCESS) {
52 #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA
57 if (rc != ASN1_SUCCESS) {
62 if (rc != ASN1_SUCCESS) {
66 rc = asn1_write_value(asn1_element,
"t", (
void *)&(ctx->
code_ctx->
t),
sizeof((ctx->
code_ctx->
t)));
67 if (rc != ASN1_SUCCESS) {
72 if (rc != ASN1_SUCCESS) {
77 if (rc != ASN1_SUCCESS) {
82 if (rc != ASN1_SUCCESS) {
109 rc = asn1_write_value(asn1_element,
"h_mat", (
void *)(h_mat), h_mat_bs);
112 if (rc != ASN1_SUCCESS) {
118 asn1_delete_structure(&definitions);
121 asn1_der_coding(asn1_element,
"", NULL, size, NULL);
122 *buffer = (
char *) malloc(*size);
124 rc = asn1_der_coding(asn1_element,
"", *buffer, size, NULL);
125 if (rc != ASN1_SUCCESS) {
130 asn1_delete_structure(&asn1_element);
136 asn1_node definitions = NULL;
137 asn1_node asn1_element = NULL;
139 char error_desc[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
149 if (rc != ASN1_SUCCESS) {
154 if (rc != ASN1_SUCCESS) {
159 asn1_delete_structure(&definitions);
162 rc = asn1_der_decoding(&asn1_element, buffer, size, error_desc);
163 if (rc != ASN1_SUCCESS) {
176 m = *(uint8_t *) tmp_buf;
182 t = *(uint8_t *) tmp_buf;
203 memcpy((*ctx)->code_ctx->code_spec->goppa->g->coef, tmp_buf, tmp_len);
204 (*ctx)->code_ctx->code_spec->goppa->g->deg = t;
214 if (
BPU_permMalloc(&((*ctx)->code_ctx->code_spec->goppa->permutation), (*ctx)->code_ctx->code_len)) {
217 memcpy((*ctx)->code_ctx->code_spec->goppa->permutation->elements, tmp_buf, tmp_len);
223 if (
BPU_gf2xMatMalloc(&((*ctx)->code_ctx->code_spec->goppa->h_mat), (*ctx)->code_ctx->t, (*ctx)->code_ctx->code_len)) {
227 for (i = 0; i < (*ctx)->code_ctx->code_spec->goppa->h_mat->k; i++) {
228 memcpy((*ctx)->code_ctx->code_spec->goppa->h_mat->elements[i], tmp_buf + j, (*ctx)->code_ctx->code_spec->goppa->h_mat->n *
sizeof(
BPU_T_GF2_16x));
229 j += (*ctx)->code_ctx->code_spec->goppa->h_mat->n *
sizeof(
BPU_T_GF2_16x);
233 asn1_delete_structure(&asn1_element);
239 asn1_node definitions = NULL;
240 asn1_node asn1_element = NULL;
241 char error_desc[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
249 if (rc != ASN1_SUCCESS) {
254 if (rc != ASN1_SUCCESS) {
263 #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA
268 if (rc != ASN1_SUCCESS) {
273 if (rc != ASN1_SUCCESS) {
277 rc = asn1_write_value(asn1_element,
"t", (
void *)&(ctx->
code_ctx->
t),
sizeof((ctx->
code_ctx->
t)));
278 if (rc != ASN1_SUCCESS) {
302 rc = asn1_write_value(asn1_element,
"g_mat", (
void *)(g_mat), g_mat_bs);
305 if (rc != ASN1_SUCCESS) {
310 asn1_delete_structure(&definitions);
313 asn1_der_coding(asn1_element,
"", NULL, size, NULL);
314 *buffer = (
char *) malloc(*size);
316 rc = asn1_der_coding(asn1_element,
"", *buffer, size, NULL);
317 if (rc != ASN1_SUCCESS) {
321 asn1_delete_structure(&asn1_element);
327 asn1_node definitions = NULL;
328 asn1_node asn1_element = NULL;
330 char error_desc[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
331 char *tmp_buf = NULL;
340 if (rc != ASN1_SUCCESS) {
345 if (rc != ASN1_SUCCESS) {
350 asn1_delete_structure(&definitions);
353 rc = asn1_der_decoding(&asn1_element, buffer, size, error_desc);
354 if (rc != ASN1_SUCCESS) {
367 m = *(uint8_t *) tmp_buf;
373 t = *(uint8_t *) tmp_buf;
393 if (
BPU_asn1ReadValue(&tmp_buf, &tmp_len, (*ctx)->code_ctx->code_len * (*ctx)->code_ctx->msg_len,
"g_mat", asn1_element)) {
396 if (
BPU_gf2MatMalloc(&((*ctx)->code_ctx->code_spec->goppa->g_mat), (*ctx)->code_ctx->code_len - (*ctx)->code_ctx->msg_len, (*ctx)->code_ctx->msg_len)) {
400 tmp = (*ctx)->code_ctx->code_spec->goppa->g_mat->elements_in_row * ((*ctx)->code_ctx->code_spec->goppa->g_mat->element_bit_size / 8);
401 for (i = 0; i < (*ctx)->code_ctx->code_spec->goppa->g_mat->k; i++) {
402 memcpy((*ctx)->code_ctx->code_spec->goppa->g_mat->elements[i], tmp_buf + j, tmp);
407 asn1_delete_structure(&asn1_element);
411 int BPU_asn1ReadValue(
char **buffer,
int *len,
const int max_len,
const char *name,
const asn1_node node) {
419 rc = asn1_read_value(node, name, *buffer, len);
421 if (rc != ASN1_SUCCESS) {
431 char *tmp_buf = NULL;
436 tmp_buf[tmp_len - 1] =
'\0';
442 #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA
458 rc = asn1_read_value(node, name, NULL, len);
460 if (*len > max_len) {
461 BPU_printError(
"requested length %d is bigger than max expected %d", *len, max_len);
466 *buffer = (
char *) malloc(*len);
481 FILE *fp = fopen(file_name,
"rb");
488 fseek(fp, 0L, SEEK_END);
490 fseek(fp, 0L, SEEK_SET);
498 *buffer = (
char *) malloc(*size);
506 tmp = fread(*buffer, 1, *size, fp);
509 BPU_printError(
"can not read data from file, read only %ld B, instead of %ld B", tmp, *size);
609 fp = fopen(file_name,
"wb");
616 tmp = fwrite(buffer, 1, size, fp);
619 BPU_printError(
"can not write buffer to file, written %ld B, instead of %ld B", tmp, size);
628 #endif // BPU_CONF_ASN1
BPU_T_Code_Ctx * code_ctx
int BPU_asn1EncodePubKey(char **buffer, int *size, const BPU_T_Mecs_Ctx *ctx)
#define BPU_STR_ASN1_OID_MECS_BASIC_GOPPA
src: http://www.hrz.tu-darmstadt.de/itsicherheit/object_identifier/oids_der_informatik__cdc/oids_des_...
BPU_T_GF2_16x mod
polynomial modulus
BPU_T_Math_Ctx * math_ctx
Math context.
BPU_T_Perm_Element * elements
permutation vector
int BPU_asn1WriteBufferToFile(const char *buffer, const long size, const char *file_name)
int BPU_asn1MallocBuffer(char **buffer, int *len, const int max_len, const asn1_node node, const char *name)
int BPU_asn1LoadPubKey(BPU_T_Mecs_Ctx **ctx, const char *pub_key_file)
int BPU_gf2xMatMalloc(BPU_T_GF2_16x_Matrix **m, int rows, int cols)
Allocate memory for matrix.
#define BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA
src: http://www.hrz.tu-darmstadt.de/itsicherheit/object_identifier/oids_der_informatik__cdc/oids_des_...
BPU_T_GF2_Matrix * g_mat
Generator matrix.
BPU_T_EN_Mecs_Types BPU_asn1GetMecsTypeFromOid(asn1_node node)
BPU_T_GF2_16x ** elements
all element of matrix
int BPU_gf2xPolyMalloc(BPU_T_GF2_16x_Poly **p, int16_t max_deg)
#define BPU_STR_ASN1_MECS_PRI_KEY
int BPU_asn1LoadPriKey(BPU_T_Mecs_Ctx **ctx, const char *pri_key_file)
uint16_t elements_in_row
number of elements in one row
BPU_T_Perm_Vector * permutation
permutation "matrix"
#define BPU_STR_ASN1_MECS_PUB_KEY_NAME
BPU_T_GF2_16x_Matrix * h_mat
Control matrix H.
int BPU_mecsInitParamsGoppa(BPU_T_UN_Mecs_Params *params, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod)
int BPU_asn1DecodePubKey(BPU_T_Mecs_Ctx **ctx, const char *buffer, const int size)
#define BPU_printError(fmt,...)
print error message with filename, line
int BPU_mecsInitCtx(BPU_T_Mecs_Ctx **ctx, const BPU_T_UN_Mecs_Params *params, const BPU_T_EN_Mecs_Types type)
Initialize (register) mecs functions for encryption, decryption and key gen based on type...
basic mecs, without any conversion
#define BPU_printDebug(fmt,...)
print debug message with filename, line
enum _BPU_T_EN_Mecs_Types BPU_T_EN_Mecs_Types
Possible types of MECS.
BPU_T_Perm_Element size
permutation size
int BPU_asn1ReadValue(char **buffer, int *len, const int max_len, const char *name, const asn1_node node)
int BPU_asn1SaveKeyPair(const BPU_T_Mecs_Ctx *ctx, const char *pri_key_file, const char *pub_key_file)
int BPU_gf2MatMalloc(BPU_T_GF2_Matrix **m, int rows, int cols)
void BPU_mecsFreeParamsGoppa(BPU_T_UN_Mecs_Params *params)
BPU_T_GF2_16x * coef
Polynomial over GF2m.
int BPU_asn1EncodePriKey(char **buffer, int *size, const BPU_T_Mecs_Ctx *ctx)
BPU_asn1EncodePriKey dsadsad.
int BPU_asn1DecodePriKey(BPU_T_Mecs_Ctx **ctx, const char *buffer, const int size)
#define BPU_STR_ASN1_MECS_PUB_KEY
int BPU_asn1LoadKeyPair(BPU_T_Mecs_Ctx **ctx, const char *pri_key_file, const char *pub_key_file)
adapted Pointcheval's cca2 conversion
int BPU_asn1LoadFileToBuffer(char **buffer, long *size, const char *file_name)
#define BPU_STR_ASN1_MECS_PRI_KEY_NAME
int BPU_asn1SavePriKey(const BPU_T_Mecs_Ctx *ctx, const char *pri_key_file)
int BPU_asn1SavePubKey(const BPU_T_Mecs_Ctx *ctx, const char *pub_key_file)
uint16_t BPU_T_Perm_Element
Definition of permutation element type.
uint8_t t
Error code correction capability.
int BPU_permMalloc(BPU_T_Perm_Vector **p, int size)
Allocate permutation vector elements of size size.
uint8_t mod_deg
modulo degree, galois finite field GF(2^m)
BPU_T_UN_Code_Spec * code_spec
Code specific structure, like generator matrix, control matrix, gen. poly ...
BPU_T_GF2_16x_Poly * g
gen. polynomial
BPU_T_GF2 ** elements
all element of matrix