BOOL PSPrintDC::OutputCoord(DocCoord& Coord, EPSAccuracy Accuracy) { // We need a view to scale the coords. ERROR2IF(pView == NULL, FALSE, "No view attached to PostScript DC!"); WinCoord PSCoord = TransformCoord(Coord); // Output to PostScript stream. BOOL Ok = (OutputValue(PSCoord.x) && OutputValue(PSCoord.y)); return Ok; }
STDMETHODIMP AitEncoder::Initialize(IStream *pIStream, WICBitmapEncoderCacheOption cacheOption) { HRESULT result = E_INVALIDARG; if (pIStream) { result = S_OK; stream = pIStream; if (SUCCEEDED(result)) { result = VerifyFactory(); } // Remember the position of the header for later reference if (SUCCEEDED(result)) { LARGE_INTEGER zero = { 0 }; result = stream->Seek(zero, STREAM_SEEK_CUR, &headerPos); } // Write the header with a placeholder for the number of frames if (SUCCEEDED(result)) { BeginBlock(stream, "AIT", 0xFFFFFFFF); OutputValue(stream, (UINT)0); result = EndBlock(stream); } } return result; }
BOOL PSPrintDC::OutputUserSpaceValue(MILLIPOINT n, EPSAccuracy Accuracy) { // We need a view to scale the value. ERROR2IF(pView == NULL, FALSE, "No view attached to PostScript DC!"); // Use pixel size to scale to device units. n /= PixelSize; // Output as device units return OutputValue(n); }
static HRESULT BeginBlock(IStream *stream, LPCSTR name, UINT frameNum) { HRESULT result = S_OK; if (NULL == stream) { result = E_INVALIDARG; } // Remember where we are if (SUCCEEDED(result)) { LARGE_INTEGER zero = { 0 }; result = stream->Seek(zero, STREAM_SEEK_CUR, &lastBlockPos); } // The number of bytes is stored as zero for now. The function EndBlock will write the // correct value later. UINT numBytes = 0; // Output the name if (SUCCEEDED(result)) { result = OutputValues(stream, name, 4); } // Output the frame number if (SUCCEEDED(result)) { result = OutputValue(stream, frameNum); } // Output the size if (SUCCEEDED(result)) { result = OutputValue(stream, numBytes); } return result; }
static HRESULT OutputBitmapPalette(IStream *stream, UINT frameNum, IWICPalette *palette) { HRESULT result = E_INVALIDARG; if (stream && palette) { UINT colorCount = 0; WICColor *colors = NULL; result = S_OK; if (SUCCEEDED(result)) { result = palette->GetColorCount(&colorCount); } if (colorCount > 0) { if (SUCCEEDED(result)) { colors = new WICColor[colorCount]; if (NULL == colors) { result = E_OUTOFMEMORY; } } if (SUCCEEDED(result)) { UINT cActualColors = 0; result = palette->GetColors(colorCount, colors, &cActualColors); } if (SUCCEEDED(result)) { BeginBlock(stream, "PAL", frameNum); OutputValue(stream, colorCount); OutputValues(stream, colors, colorCount); result = EndBlock(stream); } if (colors) { delete[] colors; } } } return result; }
static HRESULT OutputColorContext(IStream *stream, UINT frameNum, UINT colorContextCount, IWICColorContext **colorContext) { HRESULT result = E_INVALIDARG; if (stream && colorContext) { BeginBlock(stream, "COL", frameNum); for(int i = 0; i < colorContextCount; i++) { UINT numBytes = 0; BYTE *bytes = NULL; result = S_OK; if (SUCCEEDED(result)) { result = colorContext[i]->GetProfileBytes(0, NULL, &numBytes); } if (numBytes > 0) { if (SUCCEEDED(result)) { bytes = new BYTE[numBytes]; if (NULL == bytes) { result = E_OUTOFMEMORY; } } if (SUCCEEDED(result)) { result = colorContext[i]->GetProfileBytes(numBytes, bytes, &numBytes); } if (SUCCEEDED(result)) { OutputValue(stream, numBytes); OutputValues(stream, bytes, numBytes); } if (bytes) { delete[] bytes; } } } result = EndBlock(stream); } return result; }
static HRESULT EndBlock(IStream *stream) { HRESULT result = S_OK; if (NULL == stream) { result = E_INVALIDARG; } // Remember where we are ULARGE_INTEGER curPos = { 0 }; if (SUCCEEDED(result)) { LARGE_INTEGER zero = { 0 }; result = stream->Seek(zero, STREAM_SEEK_CUR, &curPos); } // Move to where the size of the last block is stored if (SUCCEEDED(result)) { LARGE_INTEGER newPos; newPos.QuadPart = static_cast<LONGLONG>(lastBlockPos.QuadPart) + 2*sizeof(UINT); result = stream->Seek(newPos, STREAM_SEEK_SET, NULL); } // Store the size if (SUCCEEDED(result)) { UINT size = static_cast<UINT>(curPos.QuadPart - lastBlockPos.QuadPart) - 3*sizeof(UINT); result = OutputValue(stream, size); } // Move back to where we should be if (SUCCEEDED(result)) { LARGE_INTEGER cp; cp.QuadPart = static_cast<LONGLONG>(curPos.QuadPart); result = stream->Seek(cp, STREAM_SEEK_SET, NULL); } return result; }
STDMETHODIMP AitEncoder::Commit() { HRESULT result = E_UNEXPECTED; if (stream) { ULARGE_INTEGER curPos = { 0 }; result = S_OK; // Go back and insert the number of frames // Remember this position if (SUCCEEDED(result)) { LARGE_INTEGER zero = { 0 }; result = stream->Seek(zero, STREAM_SEEK_CUR, NULL); } // Seek to the numFrames member of the header if (SUCCEEDED(result)) { LARGE_INTEGER pos; pos.QuadPart = static_cast<LONGLONG>(headerPos.QuadPart) + 3*sizeof(UINT); result = stream->Seek(pos, STREAM_SEEK_SET, NULL); } // Write the proper value if (SUCCEEDED(result)) { UINT numFrames = frames.size(); result = OutputValue(stream, numFrames); } // Seek back to where we were if (SUCCEEDED(result)) { LARGE_INTEGER pos; pos.QuadPart = static_cast<LONGLONG>(curPos.QuadPart); result = stream->Seek(pos, STREAM_SEEK_SET, NULL); } if (SUCCEEDED(result) && (NULL != destPalette)) { result = OutputBitmapPalette(stream, 0xFFFFFFFF, destPalette); } if (SUCCEEDED(result) && (UINT)colorContexts.size() != 0) { IWICColorContext** ppIColorContext = new IWICColorContext*[colorContexts.size()]; for (int i = 0; i < (UINT)colorContexts.size(); i++) { ppIColorContext[i] = colorContexts[i]; } result = OutputColorContext(stream, 0xFFFFFFFF, colorContexts.size(), (IWICColorContext**)ppIColorContext); delete[] ppIColorContext; } if (SUCCEEDED(result) && (NULL != destThumbnail)) { result = OutputBitmapSource(stream, "THU", 0xFFFFFFFF, 0, 0, destThumbnail); } if (SUCCEEDED(result) && (NULL != destPreview)) { result = OutputBitmapSource(stream, "PRE", 0xFFFFFFFF, 0, 0, destPreview); } } return result; }
int main(int argc, const char* argv[]){ if(argc < 3){ fprintf(stderr, "Usage: %s buffer_size stack_depth\n", argv[0]); return 1; } /* Read arguments. */ char* endptr; unsigned buffsize = strtol(argv[1], &endptr, 10); unsigned depth = strtol(argv[2], &endptr, 10); /* Read json from std input. */ FD_Reader reader(0); uint32_t *pstack = new uint32_t[depth]; BNJ::PullParser parser(depth, pstack); /* Initialize parsing session. buffer is I/O scratch space. */ uint8_t *buffer = new uint8_t[buffsize]; parser.Begin(buffer, buffsize, &reader); int ret = 0; try{ /* Begin a tree walk. */ bool running = true; while(running){ switch(parser.Pull()){ /* Stop when there is no more data. */ case BNJ::PullParser::ST_NO_DATA: running = false; break; /* Parser pulled a datum. */ case BNJ::PullParser::ST_DATUM: OutputValue(parser); break; case BNJ::PullParser::ST_MAP: fprintf(stdout, "map open '{'\n"); break; case BNJ::PullParser::ST_LIST: fprintf(stdout, "array open '['\n"); break; case BNJ::PullParser::ST_ASCEND_MAP: fprintf(stdout, "map close '}'\n"); break; case BNJ::PullParser::ST_ASCEND_LIST: fprintf(stdout, "array close ']'\n"); break; default: break; } } } catch(const std::exception& e){ fprintf(stderr, "Runtime Error: %s\n", e.what()); ret = 1; } delete [] buffer; delete [] pstack; return ret; }
static HRESULT OutputBitmapSource(IStream *stream, LPCSTR name, UINT frameNum, double dpiX, double dpiY, IWICBitmapSource *source) { HRESULT result = E_INVALIDARG; if (stream && source) { UINT width = 0, height = 0; UINT stride = 0; WICPixelFormatGUID pixelFormat; BYTE *pixels = NULL; result = S_OK; // Retrieve the dimensions of the source if (SUCCEEDED(result)) { result = source->GetSize(&width, &height); } // Optionally retrieve the resolution if (SUCCEEDED(result) && (dpiX == 0 || dpiY == 0)) { result = source->GetResolution(&dpiX, &dpiY); } // Get the pixel format if (SUCCEEDED(result)) { result = source->GetPixelFormat(&pixelFormat); } // Get the stride if (SUCCEEDED(result)) { stride = GetScanlineStride(width, pixelFormat); if (stride < 1) { result = E_UNEXPECTED; } } // Allocate the pixels if (SUCCEEDED(result)) { pixels = new BYTE[stride * height]; if (NULL == pixels) { result = E_OUTOFMEMORY; } } // Get the pixels if (SUCCEEDED(result)) { WICRect rct; rct.X = 0; rct.Y = 0; rct.Width = width; rct.Height = height; result = source->CopyPixels(&rct, stride, stride * height, pixels); } // Output everything if (SUCCEEDED(result)) { BeginBlock(stream, name, frameNum); OutputValue(stream, width); OutputValue(stream, height); OutputValue(stream, dpiX); OutputValue(stream, dpiY); OutputValue(stream, stride); OutputValue(stream, pixelFormat); OutputValues(stream, pixels, stride*height); result = EndBlock(stream); } if (pixels) { delete[] pixels; } } return result; }
int proc_file(char *rqfile) { char afn[256], rfn[256]; FILE *afp = NULL, *rfp = NULL; char ibuf[2048]; int ilen, len, ret = 0; char amode[8] = ""; char atest[100] = ""; int akeysz=0; unsigned char iVec[20], aKey[40]; int dir = -1, err = 0, step = 0; unsigned char plaintext[2048]; unsigned char ciphertext[2048]; char *rp; EVP_CIPHER_CTX ctx; int numkeys=1; if (!rqfile || !(*rqfile)) { printf("No req file\n"); return -1; } strcpy(afn, rqfile); if ((afp = fopen(afn, "r")) == NULL) { printf("Cannot open file: %s, %s\n", afn, strerror(errno)); return -1; } strcpy(rfn,afn); rp=strstr(rfn,"req/"); assert(rp); memcpy(rp,"rsp",3); rp = strstr(rfn, ".req"); memcpy(rp, ".rsp", 4); if ((rfp = fopen(rfn, "w")) == NULL) { printf("Cannot open file: %s, %s\n", rfn, strerror(errno)); fclose(afp); afp = NULL; return -1; } while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL) { ilen = strlen(ibuf); /* printf("step=%d ibuf=%s",step,ibuf);*/ if(step == 3 && !strcmp(amode,"ECB")) { memset(iVec, 0, sizeof(iVec)); step = (dir)? 4: 5; /* no ivec for ECB */ } switch (step) { case 0: /* read preamble */ if (ibuf[0] == '\n') { /* end of preamble */ if (*amode == '\0') { printf("Missing Mode\n"); err = 1; } else { fputs(ibuf, rfp); ++ step; } } else if (ibuf[0] != '#') { printf("Invalid preamble item: %s\n", ibuf); err = 1; } else { /* process preamble */ char *xp, *pp = ibuf+2; int n; if(*amode) { /* insert current time & date */ time_t rtim = time(0); fprintf(rfp, "# %s", ctime(&rtim)); } else { fputs(ibuf, rfp); if(!strncmp(pp,"INVERSE ",8) || !strncmp(pp,"DES ",4) || !strncmp(pp,"TDES ",5) || !strncmp(pp,"PERMUTATION ",12) || !strncmp(pp,"SUBSTITUTION ",13) || !strncmp(pp,"VARIABLE ",9)) { /* get test type */ if(!strncmp(pp,"DES ",4)) pp+=4; else if(!strncmp(pp,"TDES ",5)) pp+=5; xp = strchr(pp, ' '); n = xp-pp; strncpy(atest, pp, n); atest[n] = '\0'; /* get mode */ xp = strrchr(pp, ' '); /* get mode" */ n = strlen(xp+1)-1; strncpy(amode, xp+1, n); amode[n] = '\0'; /* amode[3] = '\0'; */ printf("Test=%s, Mode=%s\n",atest,amode); } } } break; case 1: /* [ENCRYPT] | [DECRYPT] */ if(ibuf[0] == '\n') break; if (ibuf[0] == '[') { fputs(ibuf, rfp); ++step; if (strncasecmp(ibuf, "[ENCRYPT]", 9) == 0) dir = 1; else if (strncasecmp(ibuf, "[DECRYPT]", 9) == 0) dir = 0; else { printf("Invalid keyword: %s\n", ibuf); err = 1; } break; } else if (dir == -1) { err = 1; printf("Missing ENCRYPT/DECRYPT keyword\n"); break; } else step = 2; case 2: /* KEY = xxxx */ if(*ibuf == '\n') { fputs(ibuf, rfp); break; } if(!strncasecmp(ibuf,"COUNT = ",8)) { fputs(ibuf, rfp); break; } if(!strncasecmp(ibuf,"COUNT=",6)) { fputs(ibuf, rfp); break; } if(!strncasecmp(ibuf,"NumKeys = ",10)) { numkeys=atoi(ibuf+10); break; } fputs(ibuf, rfp); if(!strncasecmp(ibuf,"KEY = ",6)) { akeysz=64; len = hex2bin((char*)ibuf+6, strlen(ibuf+6)-1, aKey); if (len < 0) { printf("Invalid KEY\n"); err=1; break; } PrintValue("KEY", aKey, len); ++step; } else if(!strncasecmp(ibuf,"KEYs = ",7)) { akeysz=64*3; len=hex2bin(ibuf+7,strlen(ibuf+7)-1,aKey); if(len != 8) { printf("Invalid KEY\n"); err=1; break; } memcpy(aKey+8,aKey,8); memcpy(aKey+16,aKey,8); ibuf[4]='\0'; PrintValue("KEYs",aKey,len); ++step; } else if(!strncasecmp(ibuf,"KEY",3)) { int n=ibuf[3]-'1'; akeysz=64*3; len=hex2bin(ibuf+7,strlen(ibuf+7)-1,aKey+n*8); if(len != 8) { printf("Invalid KEY\n"); err=1; break; } ibuf[4]='\0'; PrintValue(ibuf,aKey,len); if(n == 2) ++step; } else { printf("Missing KEY\n"); err = 1; } break; case 3: /* IV = xxxx */ fputs(ibuf, rfp); if (strncasecmp(ibuf, "IV = ", 5) != 0) { printf("Missing IV\n"); err = 1; } else { len = hex2bin((char*)ibuf+5, strlen(ibuf+5)-1, iVec); if (len < 0) { printf("Invalid IV\n"); err =1; break; } PrintValue("IV", iVec, len); step = (dir)? 4: 5; } break; case 4: /* PLAINTEXT = xxxx */ fputs(ibuf, rfp); if (strncasecmp(ibuf, "PLAINTEXT = ", 12) != 0) { printf("Missing PLAINTEXT\n"); err = 1; } else { int nn = strlen(ibuf+12); if(!strcmp(amode,"CFB1")) len=bint2bin(ibuf+12,nn-1,plaintext); else len=hex2bin(ibuf+12, nn-1,plaintext); if (len < 0) { printf("Invalid PLAINTEXT: %s", ibuf+12); err =1; break; } if (len >= sizeof(plaintext)) { printf("Buffer overflow\n"); } PrintValue("PLAINTEXT", (unsigned char*)plaintext, len); if (strcmp(atest, "Monte") == 0) /* Monte Carlo Test */ { do_mct(amode,akeysz,numkeys,aKey,iVec,dir,plaintext,len,rfp); } else { assert(dir == 1); ret = DESTest(&ctx, amode, akeysz, aKey, iVec, dir, /* 0 = decrypt, 1 = encrypt */ ciphertext, plaintext, len); OutputValue("CIPHERTEXT",ciphertext,len,rfp, !strcmp(amode,"CFB1")); } step = 6; } break; case 5: /* CIPHERTEXT = xxxx */ fputs(ibuf, rfp); if (strncasecmp(ibuf, "CIPHERTEXT = ", 13) != 0) { printf("Missing KEY\n"); err = 1; } else { if(!strcmp(amode,"CFB1")) len=bint2bin(ibuf+13,strlen(ibuf+13)-1,ciphertext); else len = hex2bin(ibuf+13,strlen(ibuf+13)-1,ciphertext); if (len < 0) { printf("Invalid CIPHERTEXT\n"); err =1; break; } PrintValue("CIPHERTEXT", ciphertext, len); if (strcmp(atest, "Monte") == 0) /* Monte Carlo Test */ { do_mct(amode, akeysz, numkeys, aKey, iVec, dir, ciphertext, len, rfp); } else { assert(dir == 0); ret = DESTest(&ctx, amode, akeysz, aKey, iVec, dir, /* 0 = decrypt, 1 = encrypt */ plaintext, ciphertext, len); OutputValue("PLAINTEXT",(unsigned char *)plaintext,len,rfp, !strcmp(amode,"CFB1")); } step = 6; } break; case 6: if (ibuf[0] != '\n') { err = 1; printf("Missing terminator\n"); } else if (strcmp(atest, "MCT") != 0) { /* MCT already added terminating nl */ fputs(ibuf, rfp); } step = 1; break; } } if (rfp) fclose(rfp); if (afp) fclose(afp); return err; }
BOOL PrintComponent::WriteEPSComments(EPSFilter *pFilter) { // Graeme (23-5-00) - Neither AI nor ArtWorks files need the print control entries, so // just return TRUE to avoid problems. if ( !pFilter->NeedsPrintComponents () ) { return TRUE; } ERROR2IF ( pPrCtrl == NULL, FALSE, "No print control to output in PrintComponent::WriteEPSComments()" ); // Set up the export DC ptr pExportDC = pFilter->GetExportDC(); ERROR2IF(pExportDC == NULL,FALSE,"Export DC is NULL in PrintComponent::WriteEPSComments()"); BOOL ok = TRUE; if (ok) ok = OutputValue(PCTOKEN_SECTIONNAME, PC_SECTION_VERSION); if (ok) ok = OutputValue(PCTOKEN_WHOLESPREAD, pPrCtrl->IsWholeSpread()); if (ok) ok = OutputValue(PCTOKEN_SCALE, pPrCtrl->GetScale()); if (ok) ok = OutputValue(PCTOKEN_ORIENTATION, pPrCtrl->GetPrintOrient()); if (ok) ok = OutputValue(PCTOKEN_FITTYPE, pPrCtrl->GetFitType()); if (ok) ok = OutputValue(PCTOKEN_TOPMARGIN, pPrCtrl->GetTopMargin()); if (ok) ok = OutputValue(PCTOKEN_LEFTMARGIN, pPrCtrl->GetLeftMargin()); if (ok) ok = OutputValue(PCTOKEN_WIDTH, pPrCtrl->GetWidth()); if (ok) ok = OutputValue(PCTOKEN_HEIGHT, pPrCtrl->GetHeight()); if (ok) ok = OutputValue(PCTOKEN_ROWS, pPrCtrl->GetRows()); if (ok) ok = OutputValue(PCTOKEN_COLUMNS, pPrCtrl->GetColumns()); if (ok) ok = OutputValue(PCTOKEN_GUTTER, pPrCtrl->GetGutter()); if (ok) ok = OutputValue(PCTOKEN_LAYERS, pPrCtrl->GetPrintLayers()); if (ok) ok = OutputValue(PCTOKEN_PSLEVEL, pPrCtrl->GetPSLevel()); if (ok) ok = OutputValue(PCTOKEN_BITMAPRESMETHOD, pPrCtrl->GetBitmapResMethod()); // We only need to output the bitmap DPI if the user has selected 'manual' bitmap DPI // Auto DPI will calculated the DPI automatically ( surprise, surprise - Cilla 'ere!) // This also means that the printer's DPI is not needed before saving, hence avoiding any unnecessary // delays. if (ok) { if (pPrCtrl->GetBitmapResMethod() == BITMAPRES_MANUAL) ok = OutputValue(PCTOKEN_DOTSPERINCH, pPrCtrl->GetDotsPerInch()); else ok = OutputValue(PCTOKEN_DOTSPERINCH, 150); } if (ok) ok = OutputValue(PCTOKEN_COLLATED, pPrCtrl->IsCollated()); if (ok) ok = OutputValue(PCTOKEN_NUMCOPIES, pPrCtrl->GetNumCopies()); if (ok) ok = OutputValue(PCTOKEN_PRINTTOFILE, pPrCtrl->GetPrintToFile()); if (ok) ok = OutputValue(PCTOKEN_OBJPRINTRANGE, pPrCtrl->GetObjPrintRange()); if (ok) ok = OutputValue(PCTOKEN_DPSPRINTRANGE, pPrCtrl->GetDPSPrintRange()); if (ok) ok = OutputValue(PCTOKEN_ALLTEXTASSHAPES, pPrCtrl->GetTextOptions()); // if (ok) ok = OutputValue(PCTOKEN_PRINTMETHOD, pPrCtrl->GetPrintMethod()); // reset the DC ptr to NULL (ready for next time) pExportDC = NULL; return ok; }
static int proc_file(char *rqfile, char *rspfile) { char afn[256], rfn[256]; FILE *afp = NULL, *rfp = NULL; char ibuf[2048]; char tbuf[2048]; int ilen, len, ret = 0; char algo[8] = ""; char amode[8] = ""; char atest[8] = ""; int akeysz = 0; unsigned char iVec[20], aKey[40]; int dir = -1, err = 0, step = 0; unsigned char plaintext[2048]; unsigned char ciphertext[2048]; char *rp; EVP_CIPHER_CTX ctx; FIPS_cipher_ctx_init(&ctx); if (!rqfile || !(*rqfile)) { printf("No req file\n"); return -1; } strcpy(afn, rqfile); if ((afp = fopen(afn, "r")) == NULL) { printf("Cannot open file: %s, %s\n", afn, strerror(errno)); return -1; } if (!rspfile) { strcpy(rfn,afn); rp=strstr(rfn,"req/"); #ifdef OPENSSL_SYS_WIN32 if (!rp) rp=strstr(rfn,"req\\"); #endif assert(rp); memcpy(rp,"rsp",3); rp = strstr(rfn, ".req"); memcpy(rp, ".rsp", 4); rspfile = rfn; } if ((rfp = fopen(rspfile, "w")) == NULL) { printf("Cannot open file: %s, %s\n", rfn, strerror(errno)); fclose(afp); afp = NULL; return -1; } while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL) { tidy_line(tbuf, ibuf); ilen = strlen(ibuf); /* printf("step=%d ibuf=%s",step,ibuf); */ switch (step) { case 0: /* read preamble */ if (ibuf[0] == '\n') { /* end of preamble */ if ((*algo == '\0') || (*amode == '\0') || (akeysz == 0)) { printf("Missing Algorithm, Mode or KeySize (%s/%s/%d)\n", algo,amode,akeysz); err = 1; } else { fputs(ibuf, rfp); ++ step; } } else if (ibuf[0] != '#') { printf("Invalid preamble item: %s\n", ibuf); err = 1; } else { /* process preamble */ char *xp, *pp = ibuf+2; int n; if (akeysz) { /* insert current time & date */ time_t rtim = time(0); fprintf(rfp, "# %s", ctime(&rtim)); } else { fputs(ibuf, rfp); if (strncmp(pp, "AESVS ", 6) == 0) { strcpy(algo, "AES"); /* get test type */ pp += 6; xp = strchr(pp, ' '); n = xp-pp; strncpy(atest, pp, n); atest[n] = '\0'; /* get mode */ xp = strrchr(pp, ' '); /* get mode" */ n = strlen(xp+1)-1; strncpy(amode, xp+1, n); amode[n] = '\0'; /* amode[3] = '\0'; */ if (VERBOSE) printf("Test = %s, Mode = %s\n", atest, amode); } else if (strncasecmp(pp, "Key Length : ", 13) == 0) { akeysz = atoi(pp+13); if (VERBOSE) printf("Key size = %d\n", akeysz); } } } break; case 1: /* [ENCRYPT] | [DECRYPT] */ if (ibuf[0] == '[') { fputs(ibuf, rfp); ++step; if (strncasecmp(ibuf, "[ENCRYPT]", 9) == 0) dir = 1; else if (strncasecmp(ibuf, "[DECRYPT]", 9) == 0) dir = 0; else { printf("Invalid keyword: %s\n", ibuf); err = 1; } break; } else if (dir == -1) { err = 1; printf("Missing ENCRYPT/DECRYPT keyword\n"); break; } else step = 2; case 2: /* KEY = xxxx */ fputs(ibuf, rfp); if(*ibuf == '\n') break; if(!strncasecmp(ibuf,"COUNT = ",8)) break; if (strncasecmp(ibuf, "KEY = ", 6) != 0) { printf("Missing KEY\n"); err = 1; } else { len = hex2bin((char*)ibuf+6, aKey); if (len < 0) { printf("Invalid KEY\n"); err =1; break; } PrintValue("KEY", aKey, len); if (strcmp(amode, "ECB") == 0) { memset(iVec, 0, sizeof(iVec)); step = (dir)? 4: 5; /* no ivec for ECB */ } else ++step; } break; case 3: /* IV = xxxx */ fputs(ibuf, rfp); if (strncasecmp(ibuf, "IV = ", 5) != 0) { printf("Missing IV\n"); err = 1; } else { len = hex2bin((char*)ibuf+5, iVec); if (len < 0) { printf("Invalid IV\n"); err =1; break; } PrintValue("IV", iVec, len); step = (dir)? 4: 5; } break; case 4: /* PLAINTEXT = xxxx */ fputs(ibuf, rfp); if (strncasecmp(ibuf, "PLAINTEXT = ", 12) != 0) { printf("Missing PLAINTEXT\n"); err = 1; } else { int nn = strlen(ibuf+12); if(!strcmp(amode,"CFB1")) len=bint2bin(ibuf+12,nn-1,plaintext); else len=hex2bin(ibuf+12, plaintext); if (len < 0) { printf("Invalid PLAINTEXT: %s", ibuf+12); err =1; break; } if (len >= (int)sizeof(plaintext)) { printf("Buffer overflow\n"); } PrintValue("PLAINTEXT", (unsigned char*)plaintext, len); if (strcmp(atest, "MCT") == 0) /* Monte Carlo Test */ { if(do_mct(amode, akeysz, aKey, iVec, dir, (unsigned char*)plaintext, len, rfp) < 0) EXIT(1); } else { ret = AESTest(&ctx, amode, akeysz, aKey, iVec, dir, /* 0 = decrypt, 1 = encrypt */ plaintext, ciphertext, len); OutputValue("CIPHERTEXT",ciphertext,len,rfp, !strcmp(amode,"CFB1")); } step = 6; } break; case 5: /* CIPHERTEXT = xxxx */ fputs(ibuf, rfp); if (strncasecmp(ibuf, "CIPHERTEXT = ", 13) != 0) { printf("Missing KEY\n"); err = 1; } else { if(!strcmp(amode,"CFB1")) len=bint2bin(ibuf+13,strlen(ibuf+13)-1,ciphertext); else len = hex2bin(ibuf+13,ciphertext); if (len < 0) { printf("Invalid CIPHERTEXT\n"); err =1; break; } PrintValue("CIPHERTEXT", ciphertext, len); if (strcmp(atest, "MCT") == 0) /* Monte Carlo Test */ { do_mct(amode, akeysz, aKey, iVec, dir, ciphertext, len, rfp); } else { ret = AESTest(&ctx, amode, akeysz, aKey, iVec, dir, /* 0 = decrypt, 1 = encrypt */ plaintext, ciphertext, len); OutputValue("PLAINTEXT",(unsigned char *)plaintext,len,rfp, !strcmp(amode,"CFB1")); } step = 6; } break; case 6: if (ibuf[0] != '\n') { err = 1; printf("Missing terminator\n"); } else if (strcmp(atest, "MCT") != 0) { /* MCT already added terminating nl */ fputs(ibuf, rfp); } step = 1; break; } } if (rfp) fclose(rfp); if (afp) fclose(afp); return err; }
static void pqg(FILE *in, FILE *out) { char buf[1024]; char lbuf[1024]; char *keyword, *value; int dsa2, L, N; const EVP_MD *md = NULL; while(fgets(buf,sizeof buf,in) != NULL) { if (!parse_line(&keyword, &value, lbuf, buf)) { fputs(buf,out); continue; } if(!strcmp(keyword,"[mod")) { fputs(buf,out); if (!parse_mod(value, &dsa2, &L, &N, &md)) { fprintf(stderr, "Mod Parse Error\n"); exit (1); } } else if(!strcmp(keyword,"N")) { int n=atoi(value); while(n--) { unsigned char seed[EVP_MAX_MD_SIZE]; DSA *dsa; int counter; unsigned long h; dsa = FIPS_dsa_new(); if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md, NULL, 0, seed, &counter, &h, NULL)) { fprintf(stderr, "Parameter Generation error\n"); exit(1); } if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, md, NULL, 0, seed, &counter, &h, NULL) <= 0) { fprintf(stderr, "Parameter Generation error\n"); exit(1); } do_bn_print_name(out, "P",dsa->p); do_bn_print_name(out, "Q",dsa->q); do_bn_print_name(out, "G",dsa->g); OutputValue("Seed",seed, M_EVP_MD_size(md), out, 0); fprintf(out, "c = %d\n",counter); fprintf(out, "H = %lx\n\n",h); } } else fputs(buf,out); } }
static int do_mct(char *amode, int akeysz, unsigned char *aKey,unsigned char *iVec, int dir, unsigned char *text, int len, FILE *rfp) { int ret = 0; unsigned char key[101][32]; unsigned char iv[101][AES_BLOCK_SIZE]; unsigned char ptext[1001][32]; unsigned char ctext[1001][32]; unsigned char ciphertext[64+4]; int i, j, n, n1, n2; int imode = 0, nkeysz = akeysz/8; EVP_CIPHER_CTX ctx; FIPS_cipher_ctx_init(&ctx); if (len > 32) { printf("\n>>>> Length exceeds 32 for %s %d <<<<\n\n", amode, akeysz); return -1; } for (imode = 0; imode < 6; ++imode) if (strcmp(amode, t_mode[imode]) == 0) break; if (imode == 6) { printf("Unrecognized mode: %s\n", amode); return -1; } memcpy(key[0], aKey, nkeysz); if (iVec) memcpy(iv[0], iVec, AES_BLOCK_SIZE); if (dir == XENCRYPT) memcpy(ptext[0], text, len); else memcpy(ctext[0], text, len); for (i = 0; i < 100; ++i) { /* printf("Iteration %d\n", i); */ if (i > 0) { fprintf(rfp,"COUNT = %d\n",i); OutputValue("KEY",key[i],nkeysz,rfp,0); if (imode != ECB) /* ECB */ OutputValue("IV",iv[i],AES_BLOCK_SIZE,rfp,0); /* Output Ciphertext | Plaintext */ OutputValue(t_tag[dir^1],dir ? ptext[0] : ctext[0],len,rfp, imode == CFB1); } for (j = 0; j < 1000; ++j) { switch (imode) { case ECB: if (j == 0) { /* set up encryption */ ret = AESTest(&ctx, amode, akeysz, key[i], NULL, dir, /* 0 = decrypt, 1 = encrypt */ ptext[j], ctext[j], len); if (dir == XENCRYPT) memcpy(ptext[j+1], ctext[j], len); else memcpy(ctext[j+1], ptext[j], len); } else { if (dir == XENCRYPT) { FIPS_cipher(&ctx, ctext[j], ptext[j], len); memcpy(ptext[j+1], ctext[j], len); } else { FIPS_cipher(&ctx, ptext[j], ctext[j], len); memcpy(ctext[j+1], ptext[j], len); } } break; case CBC: case OFB: case CFB128: if (j == 0) { ret = AESTest(&ctx, amode, akeysz, key[i], iv[i], dir, /* 0 = decrypt, 1 = encrypt */ ptext[j], ctext[j], len); if (dir == XENCRYPT) memcpy(ptext[j+1], iv[i], len); else memcpy(ctext[j+1], iv[i], len); } else { if (dir == XENCRYPT) { FIPS_cipher(&ctx, ctext[j], ptext[j], len); memcpy(ptext[j+1], ctext[j-1], len); } else { FIPS_cipher(&ctx, ptext[j], ctext[j], len); memcpy(ctext[j+1], ptext[j-1], len); } } break; case CFB8: if (j == 0) { ret = AESTest(&ctx, amode, akeysz, key[i], iv[i], dir, /* 0 = decrypt, 1 = encrypt */ ptext[j], ctext[j], len); } else { if (dir == XENCRYPT) FIPS_cipher(&ctx, ctext[j], ptext[j], len); else FIPS_cipher(&ctx, ptext[j], ctext[j], len); } if (dir == XENCRYPT) { if (j < 16) memcpy(ptext[j+1], &iv[i][j], len); else memcpy(ptext[j+1], ctext[j-16], len); } else { if (j < 16) memcpy(ctext[j+1], &iv[i][j], len); else memcpy(ctext[j+1], ptext[j-16], len); } break; case CFB1: if(j == 0) { #if 0 /* compensate for wrong endianness of input file */ if(i == 0) ptext[0][0]<<=7; #endif ret = AESTest(&ctx,amode,akeysz,key[i],iv[i],dir, ptext[j], ctext[j], len); } else { if (dir == XENCRYPT) FIPS_cipher(&ctx, ctext[j], ptext[j], len); else FIPS_cipher(&ctx, ptext[j], ctext[j], len); } if(dir == XENCRYPT) { if(j < 128) sb(ptext[j+1],0,gb(iv[i],j)); else sb(ptext[j+1],0,gb(ctext[j-128],0)); } else { if(j < 128) sb(ctext[j+1],0,gb(iv[i],j)); else sb(ctext[j+1],0,gb(ptext[j-128],0)); } break; } } --j; /* reset to last of range */ /* Output Ciphertext | Plaintext */ OutputValue(t_tag[dir],dir ? ctext[j] : ptext[j],len,rfp, imode == CFB1); fprintf(rfp, "\n"); /* add separator */ /* Compute next KEY */ if (dir == XENCRYPT) { if (imode == CFB8) { /* ct = CT[j-15] || CT[j-14] || ... || CT[j] */ for (n1 = 0, n2 = nkeysz-1; n1 < nkeysz; ++n1, --n2) ciphertext[n1] = ctext[j-n2][0]; } else if(imode == CFB1) { for(n1=0,n2=akeysz-1 ; n1 < akeysz ; ++n1,--n2) sb(ciphertext,n1,gb(ctext[j-n2],0)); } else switch (akeysz) { case 128: memcpy(ciphertext, ctext[j], 16); break; case 192: memcpy(ciphertext, ctext[j-1]+8, 8); memcpy(ciphertext+8, ctext[j], 16); break; case 256: memcpy(ciphertext, ctext[j-1], 16); memcpy(ciphertext+16, ctext[j], 16); break; } } else { if (imode == CFB8) { /* ct = CT[j-15] || CT[j-14] || ... || CT[j] */ for (n1 = 0, n2 = nkeysz-1; n1 < nkeysz; ++n1, --n2) ciphertext[n1] = ptext[j-n2][0]; } else if(imode == CFB1) { for(n1=0,n2=akeysz-1 ; n1 < akeysz ; ++n1,--n2) sb(ciphertext,n1,gb(ptext[j-n2],0)); } else switch (akeysz) { case 128: memcpy(ciphertext, ptext[j], 16); break; case 192: memcpy(ciphertext, ptext[j-1]+8, 8); memcpy(ciphertext+8, ptext[j], 16); break; case 256: memcpy(ciphertext, ptext[j-1], 16); memcpy(ciphertext+16, ptext[j], 16); break; } } /* Compute next key: Key[i+1] = Key[i] xor ct */ for (n = 0; n < nkeysz; ++n) key[i+1][n] = key[i][n] ^ ciphertext[n]; /* Compute next IV and text */ if (dir == XENCRYPT) { switch (imode) { case ECB: memcpy(ptext[0], ctext[j], AES_BLOCK_SIZE); break; case CBC: case OFB: case CFB128: memcpy(iv[i+1], ctext[j], AES_BLOCK_SIZE); memcpy(ptext[0], ctext[j-1], AES_BLOCK_SIZE); break; case CFB8: /* IV[i+1] = ct */ for (n1 = 0, n2 = 15; n1 < 16; ++n1, --n2) iv[i+1][n1] = ctext[j-n2][0]; ptext[0][0] = ctext[j-16][0]; break; case CFB1: for(n1=0,n2=127 ; n1 < 128 ; ++n1,--n2) sb(iv[i+1],n1,gb(ctext[j-n2],0)); ptext[0][0]=ctext[j-128][0]&0x80; break; } } else { switch (imode) { case ECB: memcpy(ctext[0], ptext[j], AES_BLOCK_SIZE); break; case CBC: case OFB: case CFB128: memcpy(iv[i+1], ptext[j], AES_BLOCK_SIZE); memcpy(ctext[0], ptext[j-1], AES_BLOCK_SIZE); break; case CFB8: for (n1 = 0, n2 = 15; n1 < 16; ++n1, --n2) iv[i+1][n1] = ptext[j-n2][0]; ctext[0][0] = ptext[j-16][0]; break; case CFB1: for(n1=0,n2=127 ; n1 < 128 ; ++n1,--n2) sb(iv[i+1],n1,gb(ptext[j-n2],0)); ctext[0][0]=ptext[j-128][0]&0x80; break; } } } return ret; }
int main(int argc,char **argv) { FILE *in, *out; DRBG_CTX *dctx = NULL; TEST_ENT t; int r, nid = 0; int pr = 0; char buf[2048], lbuf[2048]; unsigned char randout[2048]; char *keyword = NULL, *value = NULL; unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL; long entlen, noncelen, perslen, adinlen; int df = 0; int randoutlen = 0; int gen = 0; fips_algtest_init(); if (argc == 3) { in = fopen(argv[1], "r"); if (!in) { fprintf(stderr, "Error opening input file\n"); exit(1); } out = fopen(argv[2], "w"); if (!out) { fprintf(stderr, "Error opening output file\n"); exit(1); } } else if (argc == 1) { in = stdin; out = stdout; } else { fprintf(stderr,"%s (infile outfile)\n",argv[0]); exit(1); } while (fgets(buf, sizeof(buf), in) != NULL) { fputs(buf, out); if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5)) { nid = parse_md(buf); if (nid == NID_undef) exit(1); } if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5)) { nid = parse_aes(buf, &df); if (nid == NID_undef) exit(1); } if (!parse_line(&keyword, &value, lbuf, buf)) continue; if (!strcmp(keyword, "[PredictionResistance")) { if (!strcmp(value, "True]")) pr = 1; else if (!strcmp(value, "False]")) pr = 0; else exit(1); } if (!strcmp(keyword, "EntropyInput")) { ent = hex2bin_m(value, &entlen); t.ent = ent; t.entlen = entlen; } if (!strcmp(keyword, "Nonce")) { nonce = hex2bin_m(value, &noncelen); t.nonce = nonce; t.noncelen = noncelen; } if (!strcmp(keyword, "PersonalizationString")) { pers = hex2bin_m(value, &perslen); dctx = FIPS_drbg_new(nid, df | DRBG_FLAG_TEST); if (!dctx) exit (1); FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0); FIPS_drbg_set_app_data(dctx, &t); randoutlen = (int)FIPS_drbg_get_blocklength(dctx); r = FIPS_drbg_instantiate(dctx, pers, perslen); if (!r) { fprintf(stderr, "Error instantiating DRBG\n"); exit(1); } OPENSSL_free(pers); OPENSSL_free(ent); OPENSSL_free(nonce); ent = nonce = pers = NULL; gen = 0; } if (!strcmp(keyword, "AdditionalInput")) { adin = hex2bin_m(value, &adinlen); if (pr) continue; r = FIPS_drbg_generate(dctx, randout, randoutlen, 0, 0, adin, adinlen); if (!r) { fprintf(stderr, "Error generating DRBG bits\n"); exit(1); } if (!r) exit(1); OPENSSL_free(adin); adin = NULL; gen++; } if (pr) { if (!strcmp(keyword, "EntropyInputPR")) { ent = hex2bin_m(value, &entlen); t.ent = ent; t.entlen = entlen; r = FIPS_drbg_generate(dctx, randout, randoutlen, 0, 1, adin, adinlen); if (!r) { fprintf(stderr, "Error generating DRBG bits\n"); exit(1); } OPENSSL_free(adin); OPENSSL_free(ent); adin = ent = NULL; gen++; } } if (!strcmp(keyword, "EntropyInputReseed")) { ent = hex2bin_m(value, &entlen); t.ent = ent; t.entlen = entlen; } if (!strcmp(keyword, "AdditionalInputReseed")) { adin = hex2bin_m(value, &adinlen); FIPS_drbg_reseed(dctx, adin, adinlen); OPENSSL_free(ent); OPENSSL_free(adin); ent = adin = NULL; } if (gen == 2) { OutputValue("ReturnedBits", randout, randoutlen, out, 0); FIPS_drbg_free(dctx); dctx = NULL; gen = 0; } } return 0; }
static void pqg(FILE *in, FILE *out) { char buf[1024]; char lbuf[1024]; char *keyword, *value; int dsa2, L, N; const EVP_MD *md = NULL; BIGNUM *p = NULL, *q = NULL; enum pqtype { PQG_NONE, PQG_PQ, PQG_G, PQG_GCANON} pqg_type = PQG_NONE; int seedlen=-1, idxlen, idx = -1; unsigned char seed[1024], idtmp[1024]; while(fgets(buf,sizeof buf,in) != NULL) { if (buf[0] == '[') { if (strstr(buf, "Probable")) pqg_type = PQG_PQ; else if (strstr(buf, "Unverifiable")) pqg_type = PQG_G; else if (strstr(buf, "Canonical")) pqg_type = PQG_GCANON; } if (!parse_line(&keyword, &value, lbuf, buf)) { fputs(buf,out); continue; } if (strcmp(keyword, "Num")) fputs(buf,out); if(!strcmp(keyword,"[mod")) { if (!parse_mod(value, &dsa2, &L, &N, &md)) { fprintf(stderr, "Mod Parse Error\n"); exit (1); } } else if(!strcmp(keyword,"N") || (!strcmp(keyword, "Num") && pqg_type == PQG_PQ)) { int n=atoi(value); while(n--) { DSA *dsa; int counter; unsigned long h; dsa = FIPS_dsa_new(); if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md, NULL, 0, seed, &counter, &h, NULL)) { fprintf(stderr, "Parameter Generation error\n"); exit(1); } if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, md, NULL, 0, -1, seed, &counter, &h, NULL) <= 0) { fprintf(stderr, "Parameter Generation error\n"); exit(1); } do_bn_print_name(out, "P",dsa->p); do_bn_print_name(out, "Q",dsa->q); if (!dsa2) do_bn_print_name(out, "G",dsa->g); OutputValue(dsa2 ? "domain_parameter_seed" : "Seed", seed, M_EVP_MD_size(md), out, 0); if (!dsa2) { fprintf(out, "c = %d" RESP_EOL, counter); fprintf(out, "H = %lx" RESP_EOL RESP_EOL,h); } else { fprintf(out, "counter = %d" RESP_EOL RESP_EOL, counter); } FIPS_dsa_free(dsa); } } else if(!strcmp(keyword,"P")) p=hex2bn(value); else if(!strcmp(keyword,"Q")) q=hex2bn(value); else if(!strcmp(keyword,"domain_parameter_seed")) seedlen = hex2bin(value, seed); else if(!strcmp(keyword,"firstseed")) seedlen = hex2bin(value, seed); else if(!strcmp(keyword,"pseed")) seedlen += hex2bin(value, seed + seedlen); else if(!strcmp(keyword,"qseed")) seedlen += hex2bin(value, seed + seedlen); else if(!strcmp(keyword,"index")) { idxlen = hex2bin(value, idtmp); if (idxlen != 1) { fprintf(stderr, "Index value error\n"); exit (1); } idx = idtmp[0]; } if ((idx >= 0 && pqg_type == PQG_GCANON) || (q && pqg_type == PQG_G)) { DSA *dsa; dsa = FIPS_dsa_new(); dsa->p = p; dsa->q = q; p = q = NULL; if (dsa_builtin_paramgen2(dsa, L, N, md, seed, seedlen, idx, NULL, NULL, NULL, NULL) <= 0) { fprintf(stderr, "Parameter Generation error\n"); exit(1); } do_bn_print_name(out, "G",dsa->g); FIPS_dsa_free(dsa); idx = -1; } } }
int main(int argc,char **argv) #endif { FILE *in, *out; DRBG_CTX *dctx = NULL; TEST_ENT t; int r, nid = 0; int pr = 0; char buf[2048], lbuf[2048]; unsigned char randout[2048]; char *keyword = NULL, *value = NULL; unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL; long entlen, noncelen, perslen, adinlen; int df = 0; enum dtype { DRBG_NONE, DRBG_CTR, DRBG_HASH, DRBG_HMAC, DRBG_DUAL_EC } drbg_type = DRBG_NONE; int randoutlen = 0; int gen = 0; fips_algtest_init(); if (argc == 3) { in = fopen(argv[1], "r"); if (!in) { fprintf(stderr, "Error opening input file\n"); exit(1); } out = fopen(argv[2], "w"); if (!out) { fprintf(stderr, "Error opening output file\n"); exit(1); } } else if (argc == 1) { in = stdin; out = stdout; } else { fprintf(stderr,"%s (infile outfile)\n",argv[0]); exit(1); } while (fgets(buf, sizeof(buf), in) != NULL) { fputs(buf, out); if (drbg_type == DRBG_NONE) { if (strstr(buf, "CTR_DRBG")) drbg_type = DRBG_CTR; else if (strstr(buf, "Hash_DRBG")) drbg_type = DRBG_HASH; else if (strstr(buf, "HMAC_DRBG")) drbg_type = DRBG_HMAC; else if (strstr(buf, "Dual_EC_DRBG")) drbg_type = DRBG_DUAL_EC; else continue; } if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5)) { nid = dparse_md(buf); if (nid == NID_undef) exit(1); if (drbg_type == DRBG_HMAC) { switch (nid) { case NID_sha1: nid = NID_hmacWithSHA1; break; case NID_sha224: nid = NID_hmacWithSHA224; break; case NID_sha256: nid = NID_hmacWithSHA256; break; case NID_sha384: nid = NID_hmacWithSHA384; break; case NID_sha512: nid = NID_hmacWithSHA512; break; default: exit(1); } } } if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5)) { nid = parse_aes(buf, &df); if (nid == NID_undef) exit(1); } if (strlen(buf) > 12 && !strncmp(buf, "[P-", 3)) { nid = parse_ec(buf); if (nid == NID_undef) exit(1); } if (!parse_line(&keyword, &value, lbuf, buf)) continue; if (!strcmp(keyword, "[PredictionResistance")) { if (!strcmp(value, "True]")) pr = 1; else if (!strcmp(value, "False]")) pr = 0; else exit(1); } if (!strcmp(keyword, "EntropyInput")) { ent = hex2bin_m(value, &entlen); t.ent = ent; t.entlen = entlen; } if (!strcmp(keyword, "Nonce")) { nonce = hex2bin_m(value, &noncelen); t.nonce = nonce; t.noncelen = noncelen; } if (!strcmp(keyword, "PersonalizationString")) { pers = hex2bin_m(value, &perslen); if (nid == 0) { fprintf(stderr, "DRBG type not recognised!\n"); exit (1); } dctx = FIPS_drbg_new(nid, df | DRBG_FLAG_TEST); if (!dctx) exit (1); FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0); FIPS_drbg_set_app_data(dctx, &t); randoutlen = (int)FIPS_drbg_get_blocklength(dctx); r = FIPS_drbg_instantiate(dctx, pers, perslen); if (!r) { fprintf(stderr, "Error instantiating DRBG\n"); exit(1); } OPENSSL_free(pers); OPENSSL_free(ent); OPENSSL_free(nonce); ent = nonce = pers = NULL; gen = 0; } if (!strcmp(keyword, "AdditionalInput")) { adin = hex2bin_m(value, &adinlen); if (pr) continue; r = FIPS_drbg_generate(dctx, randout, randoutlen, 0, adin, adinlen); if (!r) { fprintf(stderr, "Error generating DRBG bits\n"); exit(1); } if (!r) exit(1); OPENSSL_free(adin); adin = NULL; gen++; } if (pr) { if (!strcmp(keyword, "EntropyInputPR")) { ent = hex2bin_m(value, &entlen); t.ent = ent; t.entlen = entlen; r = FIPS_drbg_generate(dctx, randout, randoutlen, 1, adin, adinlen); if (!r) { fprintf(stderr, "Error generating DRBG bits\n"); exit(1); } OPENSSL_free(adin); OPENSSL_free(ent); adin = ent = NULL; gen++; } } if (!strcmp(keyword, "EntropyInputReseed")) { ent = hex2bin_m(value, &entlen); t.ent = ent; t.entlen = entlen; } if (!strcmp(keyword, "AdditionalInputReseed")) { adin = hex2bin_m(value, &adinlen); FIPS_drbg_reseed(dctx, adin, adinlen); OPENSSL_free(ent); OPENSSL_free(adin); ent = adin = NULL; } if (gen == 2) { OutputValue("ReturnedBits", randout, randoutlen, out, 0); FIPS_drbg_free(dctx); dctx = NULL; gen = 0; } } return 0; }
static void vst(FILE *in, FILE *out) { unsigned char *key = NULL; unsigned char *v = NULL; unsigned char *dt = NULL; unsigned char ret[16]; char buf[1024]; char lbuf[1024]; uint8_t seed[1024]; char *keyword, *value; long i, keylen; struct session_op session = { .rng = CRYPTO_ANSI_CPRNG, .key = seed }; struct crypt_op op = { .dst = ret, .len = 16 }; keylen = 0; while(fgets(buf,sizeof buf,in) != NULL) { fputs(buf,out); if(!strncmp(buf,"[AES 128-Key]", 13)) keylen = 16; else if(!strncmp(buf,"[AES 192-Key]", 13)) keylen = 24; else if(!strncmp(buf,"[AES 256-Key]", 13)) keylen = 32; if (!parse_line(&keyword, &value, lbuf, buf)) continue; if(!strcmp(keyword,"Key")) { key=hex2bin_m(value,&i); if (i != keylen) { fprintf(stderr, "Invalid key length, expecting %ld\n", keylen); return; } } else if(!strcmp(keyword,"DT")) { dt=hex2bin_m(value,&i); if (i != 16) { fprintf(stderr, "Invalid DT length\n"); return; } } else if(!strcmp(keyword,"V")) { v=hex2bin_m(value,&i); if (i != 16) { fprintf(stderr, "Invalid V length\n"); return; } if (!key || !dt) { fprintf(stderr, "Missing key or DT\n"); return; } session.keylen = build_seed(seed, v, key, dt, keylen); if (session.keylen > sizeof(seed)/sizeof(*seed)) { fprintf(stderr, "Seed buffer overrun\n"); return; } if (!cryptodev_op(session,op)) { fprintf(stderr, "Error getting PRNG value\n"); return; } OutputValue("R", ret, 16, out, 0); OPENSSL_free(key); key = NULL; OPENSSL_free(dt); dt = NULL; OPENSSL_free(v); v = NULL; } } } static void mct(FILE *in, FILE *out) { unsigned char *key = NULL; unsigned char *v = NULL; unsigned char *dt = NULL; unsigned char ret[16]; char buf[1024]; char lbuf[1024]; uint8_t seed[1024]; char *keyword, *value; long i, keylen; int cryptofd; struct session_op session = { .rng = CRYPTO_ANSI_CPRNG, .key = seed }; struct crypt_op op = { .dst = ret, .len = 16 }; keylen = 0; while(fgets(buf,sizeof buf,in) != NULL) { fputs(buf,out); if(!strncmp(buf,"[AES 128-Key]", 13)) keylen = 16; else if(!strncmp(buf,"[AES 192-Key]", 13)) keylen = 24; else if(!strncmp(buf,"[AES 256-Key]", 13)) keylen = 32; if (!parse_line(&keyword, &value, lbuf, buf)) continue; if(!strcmp(keyword,"Key")) { key=hex2bin_m(value,&i); if (i != keylen) { fprintf(stderr, "Invalid key length, expecting %ld\n", keylen); return; } } else if(!strcmp(keyword,"DT")) { dt=hex2bin_m(value,&i); if (i != 16) { fprintf(stderr, "Invalid DT length\n"); return; } } else if(!strcmp(keyword,"V")) { v=hex2bin_m(value,&i); if (i != 16) { fprintf(stderr, "Invalid V length\n"); return; } if (!key || !dt) { fprintf(stderr, "Missing key or DT\n"); return; } session.keylen = build_seed(seed, v, key, dt, keylen); if (session.keylen > sizeof(seed)/sizeof(*seed)) { fprintf(stderr, "Seed buffer overrun\n"); return; } cryptodev_start_session(&session, &cryptofd); for (i = 0; i < 10000; i++) { if (cryptodev_session_op(session, cryptofd, op) <= 0) { fprintf(stderr, "Error getting PRNG value\n"); return; } } cryptodev_end_session(session, cryptofd); OutputValue("R", ret, 16, out, 0); OPENSSL_free(key); key = NULL; OPENSSL_free(dt); dt = NULL; OPENSSL_free(v); v = NULL; } } } #ifdef FIPS_ALGVS int fips_rngvs_main(int argc, char **argv) #else int main(int argc, char **argv) #endif { FILE *in, *out; if (argc == 4) { in = fopen(argv[2], "r"); if (!in) { fprintf(stderr, "Error opening input file\n"); exit(1); } out = fopen(argv[3], "w"); if (!out) { fprintf(stderr, "Error opening output file\n"); exit(1); } } else if (argc == 2) { in = stdin; out = stdout; } else { fprintf(stderr,"%s [mct|vst]\n",argv[0]); exit(1); } if(!strcmp(argv[1],"mct")) mct(in, out); else if(!strcmp(argv[1],"vst")) vst(in, out); else { fprintf(stderr,"Don't know how to %s.\n",argv[1]); exit(1); } if (argc == 4) { fclose(in); fclose(out); } return 0; }
HeapTuple _rserv_log_() #endif { Trigger *trigger; /* to get trigger name */ int nargs; /* # of args specified in CREATE TRIGGER */ char **args; /* argument: argnum */ Relation rel; /* triggered relation */ HeapTuple tuple; /* tuple to return */ HeapTuple newtuple = NULL; /* tuple to return */ TupleDesc tupdesc; /* tuple description */ int keynum; char *key; char *okey; char *newkey = NULL; int deleted; char sql[8192]; char outbuf[8192]; char oidbuf[64]; int ret; /* Called by trigger manager ? */ if (!CurrentTriggerData) /* internal error */ elog(ERROR, "_rserv_log_: triggers are not initialized"); /* Should be called for ROW trigger */ if (TRIGGER_FIRED_FOR_STATEMENT(CurrentTriggerData->tg_event)) /* internal error */ elog(ERROR, "_rserv_log_: can't process STATEMENT events"); tuple = CurrentTriggerData->tg_trigtuple; trigger = CurrentTriggerData->tg_trigger; nargs = trigger->tgnargs; args = trigger->tgargs; if (nargs != 1) /* odd number of arguments! */ /* internal error */ elog(ERROR, "_rserv_log_: need in *one* argument"); keynum = atoi(args[0]); if (keynum < 0 && keynum != ObjectIdAttributeNumber) /* internal error */ elog(ERROR, "_rserv_log_: invalid keynum %d", keynum); rel = CurrentTriggerData->tg_relation; tupdesc = rel->rd_att; deleted = (TRIGGER_FIRED_BY_DELETE(CurrentTriggerData->tg_event)) ? 1 : 0; if (TRIGGER_FIRED_BY_UPDATE(CurrentTriggerData->tg_event)) newtuple = CurrentTriggerData->tg_newtuple; #ifndef PG_FUNCTION_INFO_V1 /* * Setting CurrentTriggerData to NULL prevents direct calls to trigger * functions in queries. Normally, trigger functions have to be called * by trigger manager code only. */ CurrentTriggerData = NULL; #endif /* Connect to SPI manager */ if ((ret = SPI_connect()) < 0) /* internal error */ elog(ERROR, "_rserv_log_: SPI_connect returned %d", ret); if (keynum == ObjectIdAttributeNumber) { snprintf(oidbuf, sizeof(oidbuf), "%u", HeapTupleGetOid(tuple)); key = oidbuf; } else key = SPI_getvalue(tuple, tupdesc, keynum); if (key == NULL) ereport(ERROR, (errcode(ERRCODE_NOT_NULL_VIOLATION), errmsg("key must be not null"))); if (newtuple && keynum != ObjectIdAttributeNumber) { newkey = SPI_getvalue(newtuple, tupdesc, keynum); if (newkey == NULL) ereport(ERROR, (errcode(ERRCODE_NOT_NULL_VIOLATION), errmsg("key must be not null"))); if (strcmp(newkey, key) == 0) newkey = NULL; else deleted = 1; /* old key was deleted */ } if (strpbrk(key, "\\ \n'")) okey = OutputValue(key, outbuf, sizeof(outbuf)); else okey = key; snprintf(sql, 8192, "update _RSERV_LOG_ set logid = %d, logtime = now(), " "deleted = %d where reloid = %u and key = '%s'", GetCurrentTransactionId(), deleted, rel->rd_id, okey); if (debug) elog(DEBUG4, "sql: %s", sql); ret = SPI_exec(sql, 0); if (ret < 0) ereport(ERROR, (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION), errmsg("SPI_exec(update) returned %d", ret))); /* * If no tuple was UPDATEd then do INSERT... */ if (SPI_processed > 1) ereport(ERROR, (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION), errmsg("duplicate tuples"))); else if (SPI_processed == 0) { snprintf(sql, 8192, "insert into _RSERV_LOG_ " "(reloid, logid, logtime, deleted, key) " "values (%u, %d, now(), %d, '%s')", rel->rd_id, GetCurrentTransactionId(), deleted, okey); if (debug) elog(DEBUG4, "sql: %s", sql); ret = SPI_exec(sql, 0); if (ret < 0) ereport(ERROR, (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION), errmsg("SPI_exec(insert) returned %d", ret))); } if (okey != key && okey != outbuf) pfree(okey); if (newkey) { if (strpbrk(newkey, "\\ \n'")) okey = OutputValue(newkey, outbuf, sizeof(outbuf)); else okey = newkey; snprintf(sql, 8192, "insert into _RSERV_LOG_ " "(reloid, logid, logtime, deleted, key) " "values (%u, %d, now(), 0, '%s')", rel->rd_id, GetCurrentTransactionId(), okey); if (debug) elog(DEBUG4, "sql: %s", sql); ret = SPI_exec(sql, 0); if (ret < 0) ereport(ERROR, (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION), errmsg("SPI_exec returned %d", ret))); if (okey != newkey && okey != outbuf) pfree(okey); } SPI_finish(); #ifdef PG_FUNCTION_INFO_V1 return (PointerGetDatum(tuple)); #else return (tuple); #endif }
void do_mct(char *amode, int akeysz, int numkeys, unsigned char *akey,unsigned char *ivec, int dir, unsigned char *text, int len, FILE *rfp) { int i,imode; unsigned char nk[4*8]; /* longest key+8 */ unsigned char text0[8]; for (imode=0 ; imode < 6 ; ++imode) if(!strcmp(amode,t_mode[imode])) break; if (imode == 6) { printf("Unrecognized mode: %s\n", amode); EXIT(1); } for(i=0 ; i < 400 ; ++i) { int j; int n; EVP_CIPHER_CTX ctx; int kp=akeysz/64; unsigned char old_iv[8]; fprintf(rfp,"\nCOUNT = %d\n",i); if(kp == 1) OutputValue("KEY",akey,8,rfp,0); else for(n=0 ; n < kp ; ++n) { fprintf(rfp,"KEY%d",n+1); OutputValue("",akey+n*8,8,rfp,0); } if(imode != ECB) OutputValue("IV",ivec,8,rfp,0); OutputValue(t_tag[dir^1],text,len,rfp,imode == CFB1); /* compensate for endianness */ if(imode == CFB1) text[0]<<=7; memcpy(text0,text,8); for(j=0 ; j < 10000 ; ++j) { unsigned char old_text[8]; memcpy(old_text,text,8); if(j == 0) { memcpy(old_iv,ivec,8); DESTest(&ctx,amode,akeysz,akey,ivec,dir,text,text,len); } else { memcpy(old_iv,ctx.iv,8); EVP_Cipher(&ctx,text,text,len); } if(j == 9999) { OutputValue(t_tag[dir],text,len,rfp,imode == CFB1); /* memcpy(ivec,text,8); */ } /* DebugValue("iv",ctx.iv,8); */ /* accumulate material for the next key */ shiftin(nk,text,Sizes[imode]); /* DebugValue("nk",nk,24);*/ if((dir && (imode == CFB1 || imode == CFB8 || imode == CFB64 || imode == CBC)) || imode == OFB) memcpy(text,old_iv,8); if(!dir && (imode == CFB1 || imode == CFB8 || imode == CFB64)) { /* the test specifies using the output of the raw DES operation which we don't have, so reconstruct it... */ for(n=0 ; n < 8 ; ++n) text[n]^=old_text[n]; } } for(n=0 ; n < 8 ; ++n) akey[n]^=nk[16+n]; for(n=0 ; n < 8 ; ++n) akey[8+n]^=nk[8+n]; for(n=0 ; n < 8 ; ++n) akey[16+n]^=nk[n]; if(numkeys < 3) memcpy(&akey[2*8],akey,8); if(numkeys < 2) memcpy(&akey[8],akey,8); DES_set_odd_parity((DES_cblock *)akey); DES_set_odd_parity((DES_cblock *)(akey+8)); DES_set_odd_parity((DES_cblock *)(akey+16)); memcpy(ivec,ctx.iv,8); /* pointless exercise - the final text doesn't depend on the initial text in OFB mode, so who cares what it is? (Who designed these tests?) */ if(imode == OFB) for(n=0 ; n < 8 ; ++n) text[n]=text0[n]^old_iv[n]; } }