📄 csptests.c
字号:
}/** \brief Get user key exchange Key. * * Get the user signature key. */int testGetUserKxKey(){ printf("Getting key exchange key.\n"); return !RCRYPT_FAILED(CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hUserKxKey));}/** \brief Get user Keys. * * Get the user keys. */int testGetUserKeys(){ int failure; failure = 0; if(!testGetUserSigKey()) { updateMessages("Failed to load signature keys :-("); failure++; } else { updateMessages("Signature keys loaded :-)"); } if(!testGetUserKxKey()) { updateMessages("Failed to load key exchange keys :-("); failure++; } else { updateMessages("Key exchange keys loaded :-)"); } if(failure>1) { return FALSE; } else { return TRUE; }}/** \brief Release an acquired context. * */int testReleaseContext(){ return !RCRYPT_FAILED(CryptReleaseContext(hProv, 0));}int testRandom(){ BYTE buffer[128]; char byteAscii[4]; int i; /* Iterator.*/ if(RCRYPT_FAILED(CryptGenRandom(hProv, 128, buffer))) { return FALSE; } sprintf(line,"Random bytes: "); updateMessages(NULL); strcpy(line,""); for(i=0; i<128; i++) { sprintf(byteAscii,"%x", buffer[i]); strcat(line, byteAscii); } updateMessages(NULL); return TRUE;} /** \brief Test functions launcher.*/int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hInstPrevious, LPSTR c, int d){ WNDCLASS wc; /* The window strucutre.*/ MSG msg; /* The recieved windows message.*/ /** - Initiate test heap.*/ heapHandle = HeapCreate(0, 10000*sizeof(DWORD), 0); returnValue = 0; wc.style = 0; wc.lpfnWndProc = MainWndProc; wc.hInstance = hInstance; wc.lpszClassName = "csp11test"; wc.lpszMenuName = "MMenu"; wc.hbrBackground = GetStockObject(GRAY_BRUSH); wc.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE( 100 ) ); wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.cbClsExtra = wc.cbWndExtra = 0; /** - Register the main window structure.*/ if(!RegisterClass(&wc)) { return FALSE; } hMainWnd = CreateWindowEx(0, "csp11test", "CSP #11 Test program", WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); if( ! hMainWnd ) { return FALSE; } hWndHScroll = CreateWindowEx( 0, "SCROLLBAR", NULL, WS_CHILD | SBS_HORZ, 0, 0, 200, CW_USEDEFAULT, hMainWnd, NULL, hInstance, NULL); hWndVScroll = CreateWindowEx( 0, "SCROLLBAR", NULL, WS_CHILD | SBS_VERT, 0, 0, CW_USEDEFAULT, 200, hMainWnd, NULL, hInstance, NULL); ShowWindow( hMainWnd, d ); UpdateWindow( hMainWnd ); /** - Launche the Windows main loop.*/ while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } return returnValue; } LRESULT WINAPI MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){ HDC hdc; /* Handle to the window device context.*/ TEXTMETRIC tm; /* Structure for text metrics.*/ PAINTSTRUCT ps; /* Required by BeginPaint.*/ SCROLLINFO si; /* Used to manage scroll bar.*/ static DWORD dwClientWidth; /* With of the client area.*/ static DWORD dwClientHeight; /* Height of the client area.*/ static DWORD dwClientMaxWidth; /* Max width of the client area.*/ static DWORD dwXChar; /* Horizontql scrolling unit.*/ static DWORD dwYChar; /* Vertical scrolling unit.*/ static DWORD dwAvgUpperX; /* Average width of upper characters.*/ static DWORD xPos; /* Horizontal scrolling position.*/ static DWORD yPos; /* Vertical scrolling position.*/ int i; /* Loop iterator.*/ DWORD x,y; /* Coordinates.*/ DWORD FirstLine; /* First invalidated data line.*/ DWORD LastLine; /* Last invalidated data line.*/ HRESULT hr; /* Function result handler.*/ size_t *abcLength; /* Length of an ABC[] item.*/ switch(msg) { case WM_CREATE: /** - Get the metrics of the current font.*/ hdc = GetDC(hWnd); /** - Extract fonts information from device context.*/ GetTextMetrics(hdc, &tm); /** - Save average character width and height.*/ /** - Average size are the scrolling unit.*/ dwXChar = tm.tmAveCharWidth; dwYChar = tm.tmHeight + tm.tmExternalLeading; /** - Average upper width:*/ dwAvgUpperX = (tm.tmPitchAndFamily & 1 ? 3 : 2) * dwXChar/2; ReleaseDC(hWnd, hdc); updateMessages("Welcome to the CSP #11 test utility."); currentLineNumber = 1; return 0; /** - (Re)Sizing window.*/ case WM_SIZE: /** - Get the client area rectangle.*/ //GetClientRect(hWnd, &clientRect); /** - Save the new width and Height.*/ dwClientWidth = LOWORD(lParam); dwClientHeight = HIWORD(lParam); /** - Set vertical scrolling information.*/ si.cbSize = sizeof(si); si.fMask = SIF_RANGE | SIF_PAGE; si.nMin = 0; si.nMax = MAXLINES - 1; si.nPage = dwClientHeight / dwYChar; SetScrollInfo(hWnd, SB_VERT, &si, TRUE); /** - Set horizontal scrolling information.*/ si.cbSize = sizeof(si); si.fMask = SIF_RANGE | SIF_PAGE; si.nMin = 0; si.nMax = 255; si.nPage = dwClientWidth / dwXChar; SetScrollInfo(hWnd, SB_HORZ, &si, TRUE); return 0; /** - Horizontal scrolling.*/ case WM_HSCROLL: /** - Get horizontal scrolling information.*/ si.cbSize = sizeof(si); si.fMask = SIF_ALL; /** - Save the current position.*/ GetScrollInfo(hWnd, SB_HORZ, &si); xPos = si.nPos; /** - Switch on word param low part.*/ switch (LOWORD (wParam)) { /** - Left arrow.*/ case SB_LINELEFT: /** - One step backward.*/ si.nPos -=1; break; /** - Right arrow.*/ case SB_LINERIGHT: /** - One step forward.*/ si.nPos +=1; break; /** - Left side of the scroll box.*/ case SB_PAGELEFT: /** - One page backward.*/ si.nPos -= si.nPage; break; /** - Right side of the scroll box.*/ case SB_PAGERIGHT: /** - One page forward.*/ si.nPos += si.nPage; break; /** - Dragging the scroll box.*/ case SB_THUMBTRACK: /** - Move to box position.*/ si.nPos = si.nTrackPos; break; default: break; } /** - Set the new scroll position.*/ si.fMask = SIF_POS; SetScrollInfo(hWnd, SB_HORZ, &si, TRUE); /** - Get the new position (may be different from set).*/ GetScrollInfo(hWnd, SB_HORZ, &si); /** - If position is not the same, apply the new one.*/ if (si.nPos != xPos) { ScrollWindow(hWnd, dwXChar * (xPos - si.nPos), 0, NULL, NULL); } return 0; /** - Vertical scrolling.*/ case WM_VSCROLL: /** - Get vertical scrolling information.*/ si.cbSize = sizeof(si); si.fMask = SIF_ALL; /** - Save the current position.*/ GetScrollInfo(hWnd, SB_VERT, &si); yPos = si.nPos; /** - Switch on word param low part.*/ switch (LOWORD (wParam)) { /** - Home.*/ case SB_TOP: /** - Top of the scroll bar.*/ si.nPos = si.nMin; break; /** - End.*/ case SB_BOTTOM: /** - Bottom of the scroll bar.*/ si.nPos = si.nMax; break; /** - Up arrow.*/ case SB_LINEUP: /** - One step upward.*/ si.nPos -=1; break; /** - Down arrow.*/ case SB_LINEDOWN: /** - One step downward.*/ si.nPos +=1; break; /** - Page UP.*/ case SB_PAGEUP: /** - One page upward.*/ si.nPos -= si.nPage; break; /** - Page down.*/ case SB_PAGEDOWN: /** - One page downward.*/ si.nPos += si.nPage; break; /** - Dragging the scroll box.*/ case SB_THUMBTRACK: /** - Move to box position.*/ si.nPos = si.nTrackPos; break; default: break; } /** - Set the new scroll position.*/ si.fMask = SIF_POS; SetScrollInfo(hWnd, SB_VERT, &si, TRUE); /** - Get the new position (may be different from set).*/ GetScrollInfo(hWnd, SB_VERT, &si); /** - If position is not the same, apply the new one.*/ if (si.nPos != yPos) { ScrollWindow(hWnd, 0, dwYChar * (yPos - si.nPos), NULL, NULL); InvalidateRect(hWnd,NULL,TRUE); } return 0; /** - Window drawing.*/ case WM_PAINT: /** - Begin window painting (update text).*/ BeginPaint(hWnd, &ps); /** - Set text color.*/ SetTextColor(ps.hdc, 0x00FFFFFF); /** - Set text background to transparent.*/ SetBkMode(ps.hdc,TRANSPARENT); /** - Get the vertical scroll bar information.*/ si.cbSize = sizeof(si); si.fMask = SIF_POS; GetScrollInfo(hWnd, SB_VERT, &si); yPos = si.nPos; /** - Get the horizontal scroll bar information.*/ GetScrollInfo(hWnd, SB_HORZ, &si); xPos = si.nPos; /** - Find painting limits.*/ FirstLine = max(0, yPos + ps.rcPaint.top / dwYChar); LastLine = min(currentLineNumber -1 , yPos + ps.rcPaint.bottom / dwYChar); /*printf("Painting....(cln: %d)\n", currentLineNumber); printf("============\n");*/ x = dwXChar * (-xPos); y= 0; for (i = FirstLine; i <= LastLine; i++) { TextOut(ps.hdc, x, y,testsMessages[i] ,strlen(testsMessages[i])); y+=dwYChar; } EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(returnValue); break; case WM_COMMAND: /** - Clic on the Test menu item.*/ if(wParam == 100) { //OffsetRect(&lineRect,0,dwCharY); /** - Initial tests, acquiring context.*/ /** - Set the cryptographic ui.*/ if(!runTest(hWnd,"Set the parent window handle", &setWindowHandle,0)) { break; } if(!runTest(hWnd,"Acquire a cryptographic context", &testAcquireContext, 0)) { break; } if(!runTest(hWnd,"Get user keys", &testGetUserKeys, 0)) { updateMessages("Cannot get User key, releasing context..."); if(!runTest(hWnd,"ReleaseContext", &testReleaseContext, 0)) { break; } updateMessages("Now acquire a new context with a new key set."); if(!runTest(hWnd,"Acquire a cryptographic context with a new key set", &testAcquireCtxNewKeySet,0)) { break; } runTest(hWnd,"Get freshly generated user keys", &testGetUserKeys,0); } else { runTest(hWnd,"Get container name", &testGetContainerName,0); runTest(hWnd, "Export Public signature key", &testExportPublicSigKey,0); /** - Release user keys.*/ runTest(hWnd, "Release user keys", &testDestroyUserKeys, 0); updateMessages("Release context"); runTest(hWnd,"Release context", &testReleaseContext,0); updateMessages("Existing key, but..."); runTest(hWnd,"Acquire a cryptographic context with a new key set", &testAcquireCtxNewKeySet, NTE_EXISTS); } if(!runTest(hWnd,"Acquire a cryptographic context", &testAcquireContext,0)) { break; } runTest(hWnd,"Get container name", &testGetContainerName,0); /** - Begin of "true" crypto API tests.*/ if(runTest(hWnd,"Create a new MD5 hash", &testCreateMD5Hash, 0)) { runTest(hWnd,"Feed some data into it.", &testFeedHash, 0); runTest(hWnd,"Get the hash result", &testGetHashValue, 0); runTest(hWnd,"Sign the hash with AT_SIGNATURE", &testSigSignHash, 0); runTest(hWnd,"Destroy Hash.", &testDestroyHash, 0); } if(runTest(hWnd,"Create a new SHA(-1) hash", &testCreateSHA1Hash, 0)) { runTest(hWnd,"Feed some data into it.", &testFeedHash, 0); runTest(hWnd,"Get the hash result", &testGetHashValue, 0); runTest(hWnd,"Sign the hash with AT_SIGNATURE", &testSigSignHash, 0); runTest(hWnd,"Destroy Hash.", &testDestroyHash, 0); } if(runTest(hWnd,"Create a new SSL3_SHAMD5 hash", &testCreateSHAMD5Hash, 0)) { runTest(hWnd,"Feed some data into it.", &testFeedHash, 0); runTest(hWnd,"Get the hash result", &testGetHashValue, 0); runTest(hWnd,"Sign the hash with AT_SIGNATURE", &testSigSignHash, 0); runTest(hWnd,"Destroy Hash.", &testDestroyHash, 0); } if(runTest(hWnd,"Create a new MD5 hash", &testCreateMD5Hash, 0)) { runTest(hWnd,"Feed some data into it.", &testFeedHash, 0); runTest(hWnd,"Get the hash result", &testGetHashValue, 0); runTest(hWnd,"Sign the hash with AT_KEYEXCHANGE", &testKxSignHash, 0); runTest(hWnd,"Destroy Hash.", &testDestroyHash, 0); } if(runTest(hWnd,"Create a new SHA(-1) hash", &testCreateSHA1Hash, 0)) { runTest(hWnd,"Feed some data into it.", &testFeedHash, 0); runTest(hWnd,"Get the hash result", &testGetHashValue, 0); runTest(hWnd,"Sign the hash with AT_KEYEXCHANGE", &testKxSignHash, 0); runTest(hWnd,"Destroy Hash.", &testDestroyHash, 0); } if(runTest(hWnd,"Create a new SSL3_SHAMD5 hash", &testCreateSHAMD5Hash, 0)) { runTest(hWnd,"Feed some data into it.", &testFeedHash, 0); runTest(hWnd,"Get the hash result", &testGetHashValue, 0); runTest(hWnd,"Sign the hash with AT_KEYEXCHANGE", &testKxSignHash, 0); runTest(hWnd,"Destroy Hash.", &testDestroyHash, 0); } runTest(hWnd, "Randomize once :", &testRandom, 0); runTest(hWnd, "Randomize twice:", &testRandom, 0); /* - Release user keys.*/ /* No in fact no key getted in this C context.*/ /*runTest(hWnd, "Release user keys", &testDestroyUserKeys, 0);*/ /** - End of tests, releasing context.*/ runTest(hWnd,"Release context", &testReleaseContext,0); break; } if(wParam == 101) { DestroyWindow(hWnd); break; } break; default: return DefWindowProc(hWnd, msg, wParam, lParam); } return FALSE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -