예제 #1
0
static bool HashFile( RageFileBasic &f, unsigned char buf_hash[20], int iHash )
{
	hash_state hash;
	int iRet = hash_descriptor[iHash].init( &hash );
	ASSERT_M( iRet == CRYPT_OK, error_to_string(iRet) );

	RString s;
	while( !f.AtEOF() )
	{
		s.erase();
		if( f.Read(s, 1024*4) == -1 )
		{
			LOG->Warn( "Error reading %s: %s", f.GetDisplayPath().c_str(), f.GetError().c_str() );
			hash_descriptor[iHash].done( &hash, buf_hash );
			return false;
		}

		iRet = hash_descriptor[iHash].process( &hash, (const unsigned char *) s.data(), s.size() );
		ASSERT_M( iRet == CRYPT_OK, error_to_string(iRet) );
	}

	iRet = hash_descriptor[iHash].done( &hash, buf_hash );
	ASSERT_M( iRet == CRYPT_OK, error_to_string(iRet) );

	return true;
}
예제 #2
0
static void sign_file(const char *fname, rsa_key *key, prng_state *prng, const int prng_index, const int hash_index)
{
    const size_t sigfnamelen = strlen(fname) + 5;
    char *sigfname = (char *) malloc(sigfnamelen);
    unsigned char hash[256];
    unsigned long hashlen = sizeof (hash);
    unsigned char sig[1024];
    unsigned long siglen = sizeof (sig);
    int rc = 0;
    int status = 0;

    if (!sigfname) {
        fail("out of memory");
    }

    if ((rc = hash_file(hash_index, fname, hash, &hashlen)) != CRYPT_OK) {
        fail("hash_file for '%s' failed: %s", fname, error_to_string(rc));
    }

    if ((rc = rsa_sign_hash(hash, hashlen, sig, &siglen, prng, prng_index, hash_index, SALT_LEN, key)) != CRYPT_OK) {
        fail("rsa_sign_hash for '%s' failed: %s", fname, error_to_string(rc));
    }

    if ((rc = rsa_verify_hash(sig, siglen, hash, hashlen, hash_index, SALT_LEN, &status, key)) != CRYPT_OK) {
        fail("rsa_verify_hash for '%s' failed: %s", fname, error_to_string(rc));
    }

    if (!status) {
        fail("Generated signature isn't valid! Bug in the program!");
    }

    snprintf(sigfname, sigfnamelen, "%s.sig", fname);
    write_file(sigfname, sig, siglen);
    free(sigfname);
}
예제 #3
0
void CryptManager::GenerateRSAKey( unsigned int keyLength, RString &sPrivKey, RString &sPubKey )
{
	int iRet;

	rsa_key key;
	iRet = rsa_make_key( &g_pPRNG->m_PRNG, g_pPRNG->m_iPRNG, keyLength / 8, 65537, &key );
	if( iRet != CRYPT_OK )
	{
		LOG->Warn( "GenerateRSAKey(%i) error: %s", keyLength, error_to_string(iRet) );
		return;
	}

	unsigned char buf[1024];
	unsigned long iSize = sizeof(buf);
	iRet = rsa_export( buf, &iSize, PK_PUBLIC, &key );
	if( iRet != CRYPT_OK )
	{
		LOG->Warn( "Export error: %s", error_to_string(iRet) );
		return;
	}

	sPubKey = RString( (const char *) buf, iSize );

	iSize = sizeof(buf);
	iRet = rsa_export( buf, &iSize, PK_PRIVATE, &key );
	if( iRet != CRYPT_OK )
	{
		LOG->Warn( "Export error: %s", error_to_string(iRet) );
		return;
	}

	sPrivKey = RString( (const char *) buf, iSize );
}
예제 #4
0
/* { aes_setup end } */
int aes_encrypt(unsigned char* in, unsigned long inLength, unsigned char* out, unsigned long* outLength){
	unsigned char pt[16];
	int i = 0;
	int err = 0;

	for(;i < inLength;i++){
		for(; i % 16 < 15; i++) {
			if(i >= inLength ){
				pt[i % 16] = 0;
			} else {
				pt[i % 16] = in[i];
			}
		}
		if(i >= inLength ){
			pt[i % 16] = 0;
		} else {
			pt[i % 16] = in[i];
		}


		if (i > *outLength) {
			printf("Error setting up AES ,%i, %s\n",err, error_to_string(CRYPT_BUFFER_OVERFLOW));
			exit(EXIT_FAILURE);
		}
		if ((err =rijndael_ecb_encrypt(pt,&out[i-15],&symKey)) != CRYPT_OK) {
			printf("Error setting up AES ,%i, %s\n",err, error_to_string(err));
			exit(EXIT_FAILURE);
		}
	}
	*outLength = i;
	return 0;
}
예제 #5
0
파일: test.c 프로젝트: FirebirdSQL/firebird
static void register_algs(void)
{
  int err;

  atexit(_unregister_all);

#ifndef LTC_YARROW
   #error This demo requires Yarrow.
#endif
   if ((err = register_all_ciphers()) != CRYPT_OK) {
      fprintf(stderr, "register_all_ciphers err=%s\n", error_to_string(err));
      exit(EXIT_FAILURE);
   }
   if ((err = register_all_hashes()) != CRYPT_OK) {
      fprintf(stderr, "register_all_hashes err=%s\n", error_to_string(err));
      exit(EXIT_FAILURE);
   }
   if ((err = register_all_prngs()) != CRYPT_OK) {
      fprintf(stderr, "register_all_prngs err=%s\n", error_to_string(err));
      exit(EXIT_FAILURE);
   }

   if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
      fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err));
      exit(EXIT_FAILURE);
   }

   if (strcmp("CRYPT_OK", error_to_string(err))) {
       exit(EXIT_FAILURE);
   }
}
예제 #6
0
//generate 6 different functions and store in memory
void generateRoundFunctions(unsigned char * seed, unsigned int * bufint, int blocks)
{
	int err;
	int i=0;
	int j=0;

	unsigned char buf[4];
	
	//bufint = malloc(blocks*sizeof(unsigned int));

	
	int index = 0;
	prng_state prng; 
	
	if ((err = fortuna_start(&prng)) != CRYPT_OK) {
		printf("start error: %s\n", error_to_string(err));
	}
	if ((err = fortuna_add_entropy(seed, strlen(seed), &prng))!= CRYPT_OK) {
		printf("Add entropy error: %s\n", error_to_string(err));
	}
	if ((err = fortuna_ready(&prng)) != CRYPT_OK) {
		printf("Ready error: %s\n", error_to_string(err));
	}
		

	for(i=0;i<blocks;i++){
			
		fortuna_read(buf,sizeof(buf),&prng);
		bufint[i] = *(unsigned int *)buf;	
	}

}
예제 #7
0
void packet_client_billinginfo(unsigned char* buff, int len)
{
    unsigned char rsa_out[1024];
    unsigned char depad_out[1024];
    unsigned char outbuff[4096];
    unsigned long x, y;
    int err;
    int chunk_size;
    int outpos = 0;

    //bytes_out(buff, len);
    /* first two bytes are unknown */
    buff += 2; len -= 2;

    /* key is made up of blocks which are padded then crypted.  They
       come on the wire as 2 bytes size (net order) then data */
    while (len > 0)  {
        chunk_size = (buff[0] << 8) | buff[1];
        buff += 2; len -= 2;

        x = sizeof(rsa_out);
        if ((err = rsa_exptmod(buff, chunk_size, rsa_out, &x, PK_PRIVATE, &key)) != CRYPT_OK) {
            printf("rsa_exptmod failed: %s\n", error_to_string(err));
            return;
        }
        y = sizeof(depad_out);
        if ((err = rsa_depad(rsa_out, x, depad_out, &y)) != CRYPT_OK) {
            printf("rsa_depad failed: %s\n", error_to_string(err));
            return;
        }

        memcpy(&outbuff[outpos], depad_out, y);
        outpos += y;

        //printf("packet_client_billinginfo has %lu bytes\n", y);

        buff += chunk_size; len -= chunk_size;
    }

    buff = outbuff;
    printf("Billing Info:\n");
    printf("  Account Name: %s\n", dump_str(&buff));
    printf("  Password: %s\n", dump_str(&buff));
    printf("  Cardholder's Name: %s\n", dump_str(&buff));
    printf("  CreditCard Number: %s\n", dump_str(&buff));
    printf("  Expiration Date: %s/", dump_str(&buff)); printf("%s\n", dump_str(&buff));
    printf("  Billing cycle: %s\n", dump_str(&buff));
}
예제 #8
0
bool CryptManager::Verify( RageFileBasic &file, RString sSignature, RString sPublicKey )
{
	RSAKeyWrapper key;
	RString sError;
	if( !key.Load(sPublicKey, sError) )
	{
		LOG->Warn( "Error loading RSA key: %s", sError.c_str() );
		return false;
	}

	int iHash = register_hash( &sha1_desc );
	ASSERT( iHash >= 0 );

	unsigned char buf_hash[20];
	HashFile( file, buf_hash, iHash );

	int iMatch;
	int iRet = rsa_verify_hash_ex( (const unsigned char *) sSignature.data(), sSignature.size(),
			buf_hash, sizeof(buf_hash),
			LTC_PKCS_1_EMSA, iHash, 0, &iMatch, &key.m_Key );

	if( iRet != CRYPT_OK )
	{
		LOG->Warn( "Verify(%s) failed: %s", file.GetDisplayPath().c_str(), error_to_string(iRet) );
		return false;
	}

	if( !iMatch )
	{
		LOG->Warn( "Verify(%s) failed: signature mismatch", file.GetDisplayPath().c_str() );
		return false;
	}

	return true;
}
예제 #9
0
static void bb10capture_open_camera(BB10Capture *d) {
    camera_error_t error;

    if (d->camera_openned) {
        ms_warning("[bb10_capture] camera already openned, skipping...");
        return;
    }

    ms_message("[bb10_capture] openning %s camera", d->camera == CAMERA_UNIT_FRONT ? "front" : (d->camera == CAMERA_UNIT_REAR ? "rear" : "unknown"));
    error = camera_open(d->camera, CAMERA_MODE_RW, &(d->cam_handle));
    if (error == CAMERA_EOK) {
        camera_set_vf_mode(d->cam_handle, CAMERA_VFMODE_VIDEO);
        camera_set_vf_property(d->cam_handle, CAMERA_IMGPROP_WIDTH, d->vsize.width, CAMERA_IMGPROP_HEIGHT, d->vsize.height);
        camera_set_vf_property(d->cam_handle, CAMERA_IMGPROP_FORMAT, CAMERA_FRAMETYPE_NV12);
        ms_debug("[bb10_capture] camera capture vsize: %i,%i", d->vsize.width, d->vsize.height);

        if (d->framerate > 0) {
            camera_set_vf_property(d->cam_handle, CAMERA_IMGPROP_VARIABLEFRAMERATE, 1);
            camera_set_vf_property(d->cam_handle, CAMERA_IMGPROP_MINFRAMERATE, (double)d->framerate, CAMERA_IMGPROP_FRAMERATE, (double)d->framerate);
        }

        int rotation = d->rotation;
        if (!d->is_front_cam) {
            rotation = 360 - d->rotation;
        }
        camera_set_vf_property(d->cam_handle, CAMERA_IMGPROP_ROTATION, rotation);
        ms_debug("[bb10_capture] camera capture rotation: %i", rotation);

        d->camera_openned = TRUE;
    } else {
        ms_error("[bb10_capture] openning %i camera failed: %s", d->camera, error_to_string(error));
    }
}
예제 #10
0
void run_cmd(int res, int line, char *file, char *cmd)
{
   if (res != CRYPT_OK) {
      fprintf(stderr, "%s (%d)\n%s:%d:%s\n", error_to_string(res), res, file, line, cmd);
      exit(EXIT_FAILURE);
   }
}
예제 #11
0
  		~CTomcryptInternals(void)
  		{
  			int ErrorCode;

  	    ErrorCode = unregister_hash(&sha256_desc);
  	    if (ErrorCode == -1)
  	    {
  	    	std::cerr << "Cannot unregister SHA2 hash." << std::endl;
  	    }

  	    ErrorCode = fortuna_done(&mRandomGenerator);
  	    if (ErrorCode != CRYPT_OK)
  	    {
  	    	std::cerr << "Cannot close random generator: " << error_to_string(ErrorCode) << std::endl;
  	    }

  	    ErrorCode = unregister_prng(&fortuna_desc);
  	    if (ErrorCode == -1)
  	    {
  	    	std::cerr << "Cannot unregister random generator fortuna cipher." << std::endl;
  	    }

  	    ErrorCode = unregister_cipher(&rijndael_desc);
  	    if (ErrorCode == -1)
  	    {
  	    	std::cerr << "Cannot unregister AES cipher." << std::endl;
  	    }

  	    return;
  		}
예제 #12
0
SgObject Sg_MakePseudoRandom(SgString *name, SgObject seed)
{
  const char *cname = Sg_Utf32sToUtf8s(name);
  int wprng = find_prng(cname), err;
  SgBuiltinPrng *prng;
  
  if (wprng == -1) {
    Sg_Error(UC("%A is not supported"), name);
    return SG_UNDEF;
  }

  prng = make_prng(name);
  SG_BUILTIN_PRNG(prng)->wprng = wprng;
  err = prng_descriptor[wprng].start(&prng->prng);
  if (err != CRYPT_OK) goto err;

  if (!SG_FALSEP(seed)) {
    if (SG_BVECTORP(seed)) {
      err = prng_descriptor[wprng]
	.add_entropy(SG_BVECTOR_ELEMENTS(seed), 
		     SG_BVECTOR_SIZE(seed), &prng->prng);
      if (err != CRYPT_OK) goto err;
    } else { goto err; }
  }

  err = prng_descriptor[wprng].ready(&prng->prng);
  if (err != CRYPT_OK) goto err;

  Sg_RegisterFinalizer(prng, finalize_prng, NULL);
  return SG_OBJ(prng);
 err:
  Sg_Error(UC("Failed to initialize pseudo random: %A"),
	   Sg_MakeStringC(error_to_string(err)));
  return SG_UNDEF;
}
예제 #13
0
int main(int argc, char **argv)
{
    int rc = 0;
    prng_state prng;
    int prng_index, hash_index;
    rsa_key key;
    int i;

    ltc_mp = tfm_desc;

    prng_index = register_prng(&sprng_desc);  /* (fortuna_desc is a good choice if your platform's PRNG sucks.) */
    if (prng_index == -1) {
        fail("Failed to register a RNG");
    }

    hash_index = register_hash(&sha256_desc);
    if (hash_index == -1) {
        fail("Failed to register sha256 hasher");
    }

    if ((rc = rng_make_prng(128, prng_index, &prng, NULL)) != CRYPT_OK) {
        fail("rng_make_prng failed: %s", error_to_string(rc));
    }

    read_rsakey(&key, "privatekey.bin");

    for (i = 1; i < argc; i++) {
        sign_file(argv[i], &key, &prng, prng_index, hash_index);
    }

    rsa_free(&key);

    return 0;
}
void eax_gen(void)
{
   int err, kl, x, y1, z;
   FILE *out;
   unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], header[MAXBLOCKSIZE*2], 
                 plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
   unsigned long len;

   out = fopen("eax_tv.txt", "w");
   fprintf(out, "EAX Test Vectors.  Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key.  The outputs\n"
                "are of the form ciphertext,tag for a given NN.  The key for step N>1 is the tag of the previous\n"
                "step repeated sufficiently.\n\n");

   for (x = 0; cipher_descriptor[x].name != NULL; x++) {
      kl = cipher_descriptor[x].block_length;

      /* skip ciphers which do not have 64 or 128 bit block sizes */
      if (kl != 8 && kl != 16) continue;

      if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
         kl = cipher_descriptor[x].max_key_length;
      }
      fprintf(out, "EAX-%s (%d byte key)\n", cipher_descriptor[x].name, kl);

      /* the key */
      for (z = 0; z < kl; z++) {
          key[z] = (z & 255);
      }
      
      for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
         for (z = 0; z < y1; z++) {
            plaintext[z] = (unsigned char)(z & 255);
            nonce[z]     = (unsigned char)(z & 255);
            header[z]    = (unsigned char)(z & 255);
         }
         len = sizeof(tag);
         if ((err = eax_encrypt_authenticate_memory(x, key, kl, nonce, y1, header, y1, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
            printf("Error EAX'ing: %s\n", error_to_string(err));
            exit(EXIT_FAILURE);
         }
         fprintf(out, "%3d: ", y1);
         for (z = 0; z < y1; z++) {
            fprintf(out, "%02X", plaintext[z]);
         }
         fprintf(out, ", ");
         for (z = 0; z <(int)len; z++) {
            fprintf(out, "%02X", tag[z]);
         }
         fprintf(out, "\n");

         /* forward the key */
         for (z = 0; z < kl; z++) {
             key[z] = tag[z % len];
         }
      }
      fprintf(out, "\n");
   }
   fclose(out);
}
예제 #15
0
  void CAESModule::decrypt(Tools::CSecureMemory &rPlainText, Tools::CSecureMemory const &rCypherText) const
  {
    FASSERT(((rCypherText.getSize()-gIVSize) % gBlockSize) == 0);
    FASSERT(mKey.getSize() == gKeySize);

    Tools::CSecureMemory const IV(&rCypherText[0], gIVSize);

    int ErrorCode;
    int const Cipher = find_cipher("rijndael");
    FASSERT(Cipher != -1);

    symmetric_CBC CBCMode;
    ErrorCode = cbc_start(Cipher, &IV[0], &mKey[0], static_cast<unsigned long>(mKey.getSize()), 0, &CBCMode);
    if (ErrorCode != CRYPT_OK)
    {
    	throw ExInternalError(std::string("Cannot setup AES cipher: ") + std::string(error_to_string(ErrorCode)));
    }

    Tools::CSecureMemory PaddedPlainText;
    PaddedPlainText.allocate(rCypherText.getSize() - gIVSize);

    ErrorCode = cbc_decrypt(&rCypherText[gIVSize], &PaddedPlainText[0], static_cast<unsigned long>(PaddedPlainText.getSize()), &CBCMode);
    if (ErrorCode != CRYPT_OK)
    {
  	  throw ExInternalError(std::string("Error during decryption: ") + std::string(error_to_string(ErrorCode)));
    }

    ErrorCode = cbc_done(&CBCMode);
    if (ErrorCode != CRYPT_OK)
    {
    	throw ExInternalError(std::string("Error when closing decryption stream: ") + std::string(error_to_string(ErrorCode)));
    }

    try
    {
      Tools::getUnpaddedMemory(rPlainText, PaddedPlainText);
    }
    catch(Debug::ExAssert &rError)
    {
    	UNUSED_ARGUMENT(rError);

    	throw ExKeyError(std::string("{CAESModule} Memory structure of decrypted data is invalid. Cannot delete padding bytes."));
    }

  	return;
  }
예제 #16
0
/* { ecc_decrypt start } */
int ecc_decrypt(unsigned char* in, unsigned long inLength, unsigned char* out, unsigned long* outLength, ecc_key* key){
	int err;
	if ((err = ecc_decrypt_key(in,inLength,out,outLength,key)) != CRYPT_OK) {
		printf("Error decrypting ,%i, %s\n",err, error_to_string(err));
		exit(EXIT_FAILURE);
	}
	return 0;
}
예제 #17
0
 database_handle::~database_handle() {
   if (dbh_) {
     int code = close();
     if (code != SQLITE_OK) {
       DROMOZOA_UNEXPECTED(error_to_string(code));
     }
   }
 }
예제 #18
0
int prng_test(void)
{
   int           err = CRYPT_NOP;
   int           x;
   unsigned char buf[4096] = { 0 };
   unsigned long n, one;
   prng_state    nprng;

#ifdef LTC_PRNG_ENABLE_LTC_RNG
   unsigned long before;

   unsigned long (*previous)(unsigned char *, unsigned long , void (*)(void)) = ltc_rng;
   ltc_rng = my_test_rng;

   before = my_test_rng_read;

   if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
      fprintf(stderr, "rng_make_prng with 'my_test_rng' failed: %s\n", error_to_string(err));
      exit(EXIT_FAILURE);
   }

   if (before == my_test_rng_read) {
      fprintf(stderr, "somehow there was no read from the ltc_rng! %lu == %lu\n", before, my_test_rng_read);
      exit(EXIT_FAILURE);
   }

   ltc_rng = previous;
#endif

   /* test prngs (test, import/export) */
   for (x = 0; prng_descriptor[x].name != NULL; x++) {
      if(strstr(prng_descriptor[x].name, "no_prng") == prng_descriptor[x].name) continue;
      err = CRYPT_OK;
      DOX(prng_descriptor[x].test(), prng_descriptor[x].name);
      DOX(prng_descriptor[x].start(&nprng), prng_descriptor[x].name);
      DOX(prng_descriptor[x].add_entropy((unsigned char *)"helloworld12", 12, &nprng), prng_descriptor[x].name);
      DOX(prng_descriptor[x].ready(&nprng), prng_descriptor[x].name);
      n = sizeof(buf);
      if (strcmp(prng_descriptor[x].name, "sprng")) {
         one = 1;
         if (prng_descriptor[x].pexport(buf, &one, &nprng) != CRYPT_BUFFER_OVERFLOW) {
            fprintf(stderr, "Error testing pexport with a short buffer (%s)\n", prng_descriptor[x].name);
            return CRYPT_ERROR;
         }
      }
      DOX(prng_descriptor[x].pexport(buf, &n, &nprng), prng_descriptor[x].name);
      prng_descriptor[x].done(&nprng);
      DOX(prng_descriptor[x].pimport(buf, n, &nprng), prng_descriptor[x].name);
      DOX(prng_descriptor[x].pimport(buf, 4096, &nprng), prng_descriptor[x].name); /* try to import larger data */
      DOX(prng_descriptor[x].ready(&nprng), prng_descriptor[x].name);
      if (prng_descriptor[x].read(buf, 100, &nprng) != 100) {
         fprintf(stderr, "Error reading from imported PRNG (%s)!\n", prng_descriptor[x].name);
         return CRYPT_ERROR;
      }
      prng_descriptor[x].done(&nprng);
   }
   return err;
}
예제 #19
0
void packet_client_setenckey(unsigned char* buff, int len)
{
    unsigned char rsa_out[4096];
    unsigned char depad_out[4096];
    unsigned char tmp_symkey[SYMKEY_SIZE+4];
    unsigned long x, y;
    int err;
    int chunk_size;
    int symkeysize;
    int outpos = 0;

    /* first two bytes are unknown */
    buff += 2; len -= 2;

    /* key is made up of blocks which are padded then crypted.  They
       come on the wire as 2 bytes size (net order) then data */
    while (len > 0)  {
        chunk_size = (buff[0] << 8) | buff[1];
        buff += 2; len -= 2;

        x = sizeof(rsa_out);
        if ((err = rsa_exptmod(buff, chunk_size, rsa_out, &x, PK_PRIVATE, &key)) != CRYPT_OK) {
            printf("rsa_exptmod failed: %s\n", error_to_string(err));
            return;
        }
        y = sizeof(depad_out);
        if ((err = rsa_depad(rsa_out, x, depad_out, &y)) != CRYPT_OK) {
            printf("rsa_depad failed: %s\n", error_to_string(err));
            return;
        }

        memcpy(&tmp_symkey[outpos], depad_out, y);
        outpos += y;

        // printf("packet_client_setenckey has %lu bytes\n", y);

        buff += chunk_size; len -= chunk_size;
    }

    /* first 4 bytes are WORD keysize twice (net order) */
    symkeysize = my_ntohs(tmp_symkey); //(tmp_symkey[0] << 8) | tmp_symkey[1];
    setup_sbox_from_key(&tmp_symkey[4], symkeysize);

    printf("Client sent symmetric key (%d bytes)...\n", symkeysize);
}
예제 #20
0
파일: lrng.c 프로젝트: WeiY/mihini-repo
/** done(userdata) */
static int Ldone(lua_State* L) {
    prng_state* prng = luaL_checkudata(L, 1, MYTYPE);
    int status = fortuna_done(prng);
    if (status != CRYPT_OK) {
        lua_pushnil(L);
        lua_pushstring(L, error_to_string(status));
    }
    return 0;
}
예제 #21
0
파일: tv_gen.c 프로젝트: dyemanov/firebird
void pmac_gen(void)
{
#ifdef LTC_PMAC
   unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];
   int err, x, y, z, kl;
   FILE *out;
   unsigned long len;

   out = fopen("pmac_tv.txt", "w");

   fprintf(out,
"PMAC Tests.  In these tests messages of N bytes long (00,01,02,...,NN-1) are PMAC'ed.  The initial key is\n"
"of the same format (length specified per cipher).  The PMAC key in step N+1 is the PMAC output of\n"
"step N (repeated as required to fill the array).\n\n");

   for (x = 0; cipher_descriptor[x].name != NULL; x++) {
      kl = cipher_descriptor[x].block_length;

      /* skip ciphers which do not have 64 or 128 bit block sizes */
      if (kl != 8 && kl != 16) continue;

      if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
         kl = cipher_descriptor[x].max_key_length;
      }
      fprintf(out, "PMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl);

      /* initial key/block */
      for (y = 0; y < kl; y++) {
          key[y] = (y & 255);
      }

      for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) {
         for (z = 0; z < y; z++) {
            input[z] = (unsigned char)(z & 255);
         }
         len = sizeof(output);
         if ((err = pmac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) {
            printf("Error omacing: %s\n", error_to_string(err));
            exit(EXIT_FAILURE);
         }
         fprintf(out, "%3d: ", y);
         for (z = 0; z <(int)len; z++) {
            fprintf(out, "%02X", output[z]);
         }
         fprintf(out, "\n");

         /* forward the key */
         for (z = 0; z < kl; z++) {
             key[z] = output[z % len];
         }
      }
      fprintf(out, "\n");
   }
   fclose(out);
#endif
}
예제 #22
0
int setup_crypt(void)
{
    int err;

    if(register_prng(&yarrow_desc) != CRYPT_OK) {
        printf("Could not register prng.\n");
        return -1;
    }
    printf("prng registered...\n");

    if ((err = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK)  {
        printf("Could not make prng: %s\n", error_to_string(err));
        return -1;
    }

    /* generate a 1536 bit RSA key.  This duplicates the exported key size
       of Mythic's algorithm, but other sizes would work as well */
    if ((err = rsa_make_key(&prng, find_prng("yarrow"), 192, 65537, &key)) != CRYPT_OK) {
        printf("Could not generate RSA key: %s\n", error_to_string(err));
        return -1;
    }
    printf("RSA key generated...\n");

    /* export the key starting at keybuff[10] so we can prepend the
       fixed header the client expects */
    exported_key_len = sizeof(exported_key_buffer);
    if ((err = rsa_export(&exported_key_buffer[10], &exported_key_len, PK_PUBLIC, &key)) != CRYPT_OK) {
        printf("Could not export RSA public key: %s\n", error_to_string(err));
        return -1;
    }
    printf("RSA public key exported (%lu bytes)...\n", exported_key_len);

    /* some sort of protocol version information proceeds the key when
       we send it. If not correct, login.dll generates version mismatch 
	       error message. */
    *((unsigned long *)&exported_key_buffer[0]) = htonl(LOGIN_PROTOCOL_VERSION);
    *((unsigned short *)&exported_key_buffer[4]) = htons(1);
    /* add the size */
    *((unsigned short *)&exported_key_buffer[6]) = htons(exported_key_len);
    *((unsigned short *)&exported_key_buffer[8]) = htons(exported_key_len);

    return 0;
}
예제 #23
0
void Win32Socket::throw_if_socket_failed(int result) const
{
	if (result == SOCKET_ERROR)
	{
		int error = 0;
		int length = sizeof(int);
		getsockopt(handle, SOL_SOCKET, SO_ERROR, (char *) &error, &length);
		throw Exception(error_to_string(error));
	}
}
예제 #24
0
static void bb10camera_detect(MSWebCamManager *obj) {
    camera_error_t error;
    camera_handle_t handle;

    error = camera_open(CAMERA_UNIT_FRONT, CAMERA_MODE_RW, &handle);
    if (error == CAMERA_EOK) {
        if (camera_has_feature(handle, CAMERA_FEATURE_VIDEO)) {
            if (camera_can_feature(handle, CAMERA_FEATURE_VIDEO)) {
                MSWebCam *cam = ms_web_cam_new(&ms_bb10_camera_desc);
                cam->name = ms_strdup("BB10 Front Camera");
                ms_message("[bb10_capture] camera added: %s", cam->name);
                ms_web_cam_manager_add_cam(obj, cam);
                camera_close(handle);
            } else {
                ms_warning("[bb10_capture] front camera has video feature but can't do it...");
            }
        } else {
            ms_warning("[bb10_capture] front camera doesn't have video feature");
        }
    } else {
        ms_warning("[bb10_capture] Can't open front camera: %s", error_to_string(error));
    }

    error = camera_open(CAMERA_UNIT_REAR, CAMERA_MODE_RW, &handle);
    if (error == CAMERA_EOK) {
        if (camera_has_feature(handle, CAMERA_FEATURE_VIDEO)) {
            if (camera_can_feature(handle, CAMERA_FEATURE_VIDEO)) {
                MSWebCam *cam = ms_web_cam_new(&ms_bb10_camera_desc);
                cam->name = ms_strdup("BB10 Rear Camera");
                ms_message("[bb10_capture] camera added: %s", cam->name);
                ms_web_cam_manager_add_cam(obj, cam);
                camera_close(handle);
            } else {
                ms_warning("[bb10_capture] rear camera has video feature but can't do it...");
            }
        } else {
            ms_warning("[bb10_capture] rear camera doesn't have video feature");
        }
    } else {
        ms_warning("[bb10_capture] Can't open rear camera: %s", error_to_string(error));
    }
}
예제 #25
0
파일: rpmltc.c 프로젝트: avokhmin/RPM5
static
int rpmltcErr(rpmltc ltc, const char * msg, int rc)
        /*@*/
{
    /* XXX FIXME: Don't spew on expected failures ... */
    if (rc != CRYPT_OK) {
        fprintf (stderr, "rpmltc: %s rc(%d) %s\n",
                msg, rc, error_to_string(rc));
    }
    return rc;
}
예제 #26
0
/* { generateKeys start } */
int generateKeys(prng_state* prng){
	ecc_key key;
	int err;
	if ((err = ecc_make_key(prng,find_prng("fortuna"),24,&key))!= CRYPT_OK) {
		printf("Error setting up , %s\n", error_to_string(err));
		exit(EXIT_FAILURE);
	}
	saveKeyToFile(&key, "private.key", PK_PRIVATE);
	saveKeyToFile(&key, "public.key", PK_PUBLIC);
	return 0;
}
예제 #27
0
int initEncrypt(){
	prng_index = register_prng(&fortuna_desc);
	hash_index = register_hash(&sha256_desc);

	int err;
	if ((err = rng_make_prng(128, find_prng("fortuna"), &prng, NULL)) != CRYPT_OK) {
		printf("Error setting up PRNG, %s\n", error_to_string(err));
		exit(EXIT_FAILURE);
	}
	return 0;
}
예제 #28
0
void read_rsakey(rsa_key *key, const char *fname)
{
    unsigned char buf[4096];
    unsigned long len = sizeof (buf);
    int rc;

    read_file(fname, buf, &len);

    if ((rc = rsa_import(buf, len, key)) != CRYPT_OK) {
        fail("rsa_import for '%s' failed: %s", fname, error_to_string(rc));
    }
}
예제 #29
0
void run_cmd(int res, int line, const char *file, const char *cmd, const char *algorithm)
{
   if (res != CRYPT_OK) {
      fprintf(stderr, "%s (%d)%s%s\n%s:%d:%s\n",
              error_to_string(res), res,
              (algorithm ? " - " : ""), (algorithm ? algorithm : ""),
              file, line, cmd);
      if (res != CRYPT_NOP) {
         exit(EXIT_FAILURE);
      }
   }
}
예제 #30
0
void Win32Socket::connect(const SocketName &socketname)
{
	sockaddr_in addr;
	socketname.to_sockaddr(AF_INET, (sockaddr *) &addr, sizeof(sockaddr_in));
	int result = ::connect(handle, (const sockaddr *) &addr, sizeof(sockaddr_in));
	if (result == SOCKET_ERROR)
	{
		int errorcode = WSAGetLastError();
		if ( ! ( (errorcode ==  WSAEWOULDBLOCK) || (errorcode ==  WSAEINPROGRESS) ) )
			throw Exception(error_to_string(errorcode));
	}
}