static BOOL CALLBACK beeLogoDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { // начало if (uMsg == WM_INITDIALOG) { blob_t str = 0; // печать информации о версии beePrintBuildInfo(&str); if (str != 0) { SetDlgItemTextA(hDlg, 102, (const char*)str); blobClose(str); } return TRUE; } // выход if (uMsg == WM_COMMAND && HIWORD(wParam) == BN_CLICKED && (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) { EndDialog(hDlg, 0); return TRUE; } return FALSE; }
/* * Transfer the contents of the text into a blob * The caller must free the returned blob if b is NULL */ blob * textToBlob(text *t, blob *b, int destroy) { size_t s; blob *bin; if(t == NULL) return NULL; s = 0; (void)textIterate(t, getLength, &s, 0); if(s == 0) return b; /* * copy b. If b is NULL and an error occurs we know we need to free * before returning */ bin = b; if(b == NULL) { b = blobCreate(); if(b == NULL) return NULL; } if(blobGrow(b, s) != CL_SUCCESS) { cli_warnmsg("Couldn't grow the blob: we may be low on memory\n"); #if 0 if(!destroy) { if(bin == NULL) blobDestroy(b); return NULL; } /* * We may be able to recover enough memory as we destroy to * create the blob */ #else if(bin == NULL) blobDestroy(b); return NULL; #endif } (void)textIterate(t, addToBlob, b, destroy); if(destroy && t->t_next) { textDestroy(t->t_next); t->t_next = NULL; } blobClose(b); return b; }
/* * No longer needed to be growable, so turn into a normal memory area which * the caller must free. The passed blob is destroyed */ void * blobToMem(blob *b) { void *ret; assert(b != NULL); assert(b->magic == BLOBCLASS); if(!b->isClosed) blobClose(b); if(b->name) free(b->name); #ifdef CL_DEBUG b->magic = INVALIDCLASS; #endif ret = (void *)b->data; free(b); return ret; }
static err_t beeReadAndCalcStamp(octet stampRead[STAMP_SIZE], octet stampCalc[STAMP_SIZE]) { err_t code = ERR_OK; char name[MAX_PATH]; HANDLE hFile, hMapping; DWORD size, offset; octet* image; void* hash_state; // имя модуля if (!GetModuleFileNameA(GetModuleHandleA("bee2.dll"), name, sizeof(name))) return ERR_SYS; // открыть файл hFile = CreateFileA(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) return ERR_FILE_OPEN; // длина файла size = SetFilePointer(hFile, 0, NULL, FILE_END); if (size == INVALID_SET_FILE_POINTER) { CloseHandle(hFile); return ERR_SYS; } // проецировать файл в память hMapping = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL); if (hMapping == NULL) { CloseHandle(hFile); return ERR_SYS; } // отобразить файл в память image = (octet*)MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0); if (image == NULL) { CloseHandle(hMapping), CloseHandle(hFile); return ERR_SYS; } // найти смещение контрольной характеристики offset = stampFindOffset(image, size); if (offset == (DWORD)-1) { UnmapViewOfFile(image), CloseHandle(hMapping), CloseHandle(hFile); return ERR_BAD_FORMAT; } // сохранить характеристику memCopy(stampRead, image + offset, STAMP_SIZE); // вычислить характеристику CASSERT(STAMP_SIZE >= 32); memSetZero(stampCalc, STAMP_SIZE); hash_state = blobCreate(beltHash_keep()); if (hash_state) { // хэшировать beltHashStart(hash_state); beltHashStepH(image, offset, hash_state); beltHashStepH(image + offset + STAMP_SIZE, size - offset - STAMP_SIZE, hash_state); beltHashStepG(stampCalc, hash_state); blobClose(hash_state); } else code = ERR_OUTOFMEMORY; // очистка и выход UnmapViewOfFile(image); CloseHandle(hMapping); CloseHandle(hFile); return code; }
void g12sCloseEc(ec_o* ec) { blobClose(ec); }
static err_t g12sCreateEc( ec_o** pec, /* [out] описание эллиптической кривой */ const g12s_params* params, /* [in] долговременные параметры */ g12s_deep_i deep /* [in] потребности в стековой памяти */ ) { // размерности size_t n, no, nb; size_t f_keep; size_t f_deep; size_t ec_d; size_t ec_keep; size_t ec_deep; // состояние void* state; qr_o* f; /* базовое поле */ ec_o* ec; /* кривая */ void* stack; // pre ASSERT(memIsValid(pec, sizeof(*pec))); // минимальная проверка входных данных if (!memIsValid(params, sizeof(g12s_params)) || params->l != 256 && params->l != 512) return ERR_BAD_PARAMS; // определить размерности no = memNonZeroSize(params->p, sizeof(params->p) * params->l / 512); n = W_OF_O(no); f_keep = gfpCreate_keep(no); f_deep = gfpCreate_deep(no); ec_d = 3; ec_keep = ecpCreateJ_keep(no); ec_deep = ecpCreateJ_deep(no, f_deep); // создать состояние state = blobCreate( f_keep + ec_keep + utilMax(3, ec_deep, ecCreateGroup_deep(f_deep), deep(n, f_deep, ec_d, ec_deep))); if (state == 0) return ERR_NOT_ENOUGH_MEMORY; // создать поле f = (qr_o*)((octet*)state + ec_keep); stack = (octet*)f + f_keep; if (!gfpCreate(f, params->p, no, stack)) { blobClose(state); return ERR_BAD_PARAMS; } // проверить длину p nb = wwBitSize(f->mod, n); if (params->l == 256 && nb <= 253 || params->l == 512 && nb <= 507) { blobClose(state); return ERR_BAD_PARAMS; } // создать кривую и группу ec = (ec_o*)state; if (!ecpCreateJ(ec, f, params->a, params->b, stack) || !ecCreateGroup(ec, params->xP, params->yP, params->q, params->l / 8, params->n, stack)) { blobClose(state); return ERR_BAD_PARAMS; } // проверить q n = W_OF_B(params->l); nb = wwBitSize(ec->order, n); if (params->l == 256 && nb <= 254 || params->l == 512 && nb <= 508 || zzIsEven(ec->order, n)) { blobClose(state); return ERR_BAD_PARAMS; } // присоединить f к ec objAppend(ec, f, 0); // все нормально *pec = ec; return ERR_OK; }
void _dstuCloseEc(ec_o* ec) { blobClose(ec); }
static err_t _dstuCreateEc( ec_o** pec, /* [out] описание эллиптической кривой */ const dstu_params* params, /* [in] долговременные параметры */ _dstu_deep_i deep /* [in] потребности в стековой памяти */ ) { // размерности size_t m; size_t n; size_t f_keep; size_t f_deep; size_t ec_d; size_t ec_keep; size_t ec_deep; // состояние void* state; size_t* p; /* описание многочлена */ qr_o* f; /* поле */ octet* A; /* коэффициент A */ ec_o* ec; /* кривая */ void* stack; // pre ASSERT(memIsValid(pec, sizeof(*pec))); // минимальная проверка входных данных if (!memIsValid(params, sizeof(dstu_params)) || (m = params->p[0]) < 160 || m > 509 || params->A > 1) return ERR_BAD_PARAMS; // определить размерности n = W_OF_B(m); f_keep = gf2Create_keep(m); f_deep = gf2Create_deep(m); ec_d = 3; ec_keep = ec2CreateLD_keep(n); ec_deep = ec2CreateLD_deep(n, f_deep); // создать состояние state = blobCreate( f_keep + ec_keep + utilMax(4, 4 * sizeof(size_t) + f_deep, O_OF_B(m) + ec_deep, ecCreateGroup_deep(f_deep), deep(n, f_deep, ec_d, ec_deep))); if (state == 0) return ERR_NOT_ENOUGH_MEMORY; // создать поле f = (qr_o*)((octet*)state + ec_keep); p = (size_t*)((octet*)f + f_keep); p[0] = params->p[0]; p[1] = params->p[1]; p[2] = params->p[2]; p[3] = params->p[3]; stack = p + 4; if (!gf2Create(f, p, stack)) { blobClose(state); return ERR_BAD_PARAMS; } // создать кривую и группу ec = (ec_o*)state; A = (octet*)p; A[0] = params->A; memSetZero(A + 1, f->no - 1); stack = A + f->no; if (!ec2CreateLD(ec, f, A, params->B, stack) || !ecCreateGroup(ec, params->P, params->P + ec->f->no, params->n, ec->f->no, params->c, stack)) { blobClose(state); return ERR_BAD_PARAMS; } // присоединить f к ec objAppend(ec, f, 0); // все нормально *pec = ec; return ERR_OK; }
// проверить характеристику void stampCheck(const char* name) { HANDLE hFile; DWORD size; HANDLE hMapping; octet* image; DWORD offset; octet stamp[STAMP_SIZE]; void* hash_state; bool_t success; // открыть файл hFile = CreateFileA(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("File \"%s\" was not found or could not be open.\n", name); return; } // длина файла size = SetFilePointer(hFile, 0, NULL, FILE_END); if (size == INVALID_SET_FILE_POINTER) { CloseHandle(hFile); printf("Error processing the file \"%s\".\n", name); return; } // проецировать файл в память hMapping = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL); if (hMapping == NULL) { CloseHandle(hFile); printf("Error processing the file \"%s\".\n", name); return; } // отобразить файл в память image = (octet*)MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0); if (image == NULL) { CloseHandle(hMapping); CloseHandle(hFile); printf("Error processing the file \"%s\".\n", name); return; } // найти смещение контрольной характеристики offset = stampFindOffset(image, size); if (offset == (DWORD)-1) { UnmapViewOfFile(image); CloseHandle(hMapping); CloseHandle(hFile); printf("Control stamp of \"%s\" was not found or corrupted.\n", name); return; } // подготовить место для контрольной характеристики CASSERT(STAMP_SIZE >= 32); memSet(stamp, 0, STAMP_SIZE); // состояние хэширования hash_state = blobCreate(beltHash_keep()); if (hash_state) { // хэшировать beltHashStart(hash_state); beltHashStepH(image, offset, hash_state); beltHashStepH(image + offset + STAMP_SIZE, size - offset - STAMP_SIZE, hash_state); beltHashStepG(stamp, hash_state); blobClose(hash_state); // сравнить success = memEq(image + offset, stamp, STAMP_SIZE); printf("Integrity of \"%s\"... %s\n", name, success ? "OK" : "Failed"); if (success) stampPrint(image + offset, "stamp"); else stampPrint(image + offset, "read_stamp"), stampPrint(stamp, "calc_stamp"); } else printf("Insufficient memory.\n"); // очистка UnmapViewOfFile(image); CloseHandle(hMapping); CloseHandle(hFile); }
// установить характеристику void stampSet(const char* name) { HANDLE hFile; DWORD size; HANDLE hMapping; octet* image; DWORD offset; void* hash_state; // открыть файл hFile = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("File \"%s\" was not found or could not be open.\n", name); return; } // длина файла size = SetFilePointer(hFile, 0, NULL, FILE_END); if (size == INVALID_SET_FILE_POINTER) { CloseHandle(hFile); printf("Error processing the file \"%s\".\n", name); return; } // проецировать файл в память hMapping = CreateFileMappingA(hFile, NULL, PAGE_READWRITE, 0, 0, NULL); if (hMapping == NULL) { CloseHandle(hFile); printf("Error processing the file \"%s\".\n", name); return; } // отобразить файл в память image = (octet*)MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, 0); if (image == NULL) { CloseHandle(hMapping); CloseHandle(hFile); printf("Error processing the file \"%s\".\n", name); return; } // найти смещение контрольной характеристики offset = stampFindOffset(image, size); if (offset == (DWORD)-1) { UnmapViewOfFile(image); CloseHandle(hMapping); CloseHandle(hFile); printf("Control stamp of \"%s\" was not found or corrupted.\n", name); return; } // подготовить место для контрольной характеристики CASSERT(STAMP_SIZE >= 32); memSetZero(image + offset, STAMP_SIZE); // стек хэширования hash_state = blobCreate(beltHash_keep()); if (hash_state) { // хэшировать beltHashStart(hash_state); beltHashStepH(image, offset, hash_state); beltHashStepH(image + offset + STAMP_SIZE, size - offset - STAMP_SIZE, hash_state); beltHashStepG(image + offset, hash_state); blobClose(hash_state); // печать printf("Control stamp successfully added to \"%s\"\n", name); stampPrint(image + offset, "stamp"); } else printf("Insufficient memory.\n"); // очистка UnmapViewOfFile(image); CloseHandle(hMapping); CloseHandle(hFile); }