Example #1
0
/*
void response_client_open_ex(response_client_t *uclient, char *conn_str, char *(*id_f)(), char *(*pph_f)()) {
    uclient->id_f = id_f;
    uclient->passphrase_f = pph_f;
    response_client_open_int(uclient, conn_str);
}
*/
int response_client_send(response_client_t *uclient, response_request_t *request) {
    int ret;
    long nbytes;
    char buff[UCLIENT_BUFSIZE];
#if defined(USERVER_ENCRYPTED)
    char cipher[UCLIENT_BUFSIZE];
    char message[UCLIENT_BUFSIZE];
    uint32_t timestamp;
    char sts[32];
    char *passphrase = uclient->passphrase_f();
    char otp[100];
    char *id = uclient->id_f();
    char challenge[32];
    int len = 32;
    int len1 = 32;
    
    lvc_t lvc;
    
    timestamp = get_ts();
    len1 = ts2str(timestamp, sts);
    
    generate_otp(otp, passphrase, sts);

    do_encrypt(challenge, &len, sts, len1, otp);

    lvc_init(&lvc, message, UCLIENT_BUFSIZE);

    fprintf(stdout, "lvc_pack id:%s\n", id);
    lvc_pack( &lvc, strlen(id), id );
    fprintf(stdout, "lvc_pack ts:%u\n", timestamp);
    lvc_pack( &lvc, sizeof(uint32_t), (char *)&timestamp );
    fprintf(stdout, "lvc_pack challenge:%d\n", len);
    lvc_pack( &lvc, len, challenge );
#endif
    response_build_request(buff, sizeof(buff), request);
    nbytes = strlen(buff);

#if defined(USERVER_ENCRYPTED)
    fprintf(stdout, "Message to send:%.*s\n", nbytes, buff);
    
    len = sizeof(cipher);
    do_encrypt(cipher, &len, buff, nbytes, otp);

    lvc_pack( &lvc, len, cipher );
    lvc_pack_finish(&lvc);
    
    nbytes = lvc.len;

    ret = pj_sock_sendto(uclient->fd, lvc.data, &nbytes, 0, (const pj_sockaddr_t *)uclient->connect_data, sizeof(pj_sockaddr_in));
#else
    ret = pj_sock_sendto(uclient->fd, buff, &nbytes, 0, (const pj_sockaddr_t *)uclient->connect_data, sizeof(pj_sockaddr_in));
#endif

    if(ret != 0) {
        PERROR_IF_TRUE(1, "Error in sending data\n");
        return -1;
    }

    return nbytes;
}
Example #2
0
static void
test_keys( ELG_secret_key *sk, unsigned int nbits )
{
    ELG_public_key pk;
    MPI test = mpi_alloc( 0 );
    MPI out1_a = mpi_alloc ( mpi_nlimb_hint_from_nbits (nbits) );
    MPI out1_b = mpi_alloc ( mpi_nlimb_hint_from_nbits (nbits) );
    MPI out2   = mpi_alloc ( mpi_nlimb_hint_from_nbits (nbits) );

    pk.p = sk->p;
    pk.g = sk->g;
    pk.y = sk->y;

    /*mpi_set_bytes( test, nbits, get_random_byte, 0 );*/
    {	char *p = get_random_bits( nbits, 0, 0 );
	mpi_set_buffer( test, p, (nbits+7)/8, 0 );
	xfree(p);
    }

    do_encrypt( out1_a, out1_b, test, &pk );
    decrypt( out2, out1_a, out1_b, sk );
    if( mpi_cmp( test, out2 ) )
	log_fatal("Elgamal operation: encrypt, decrypt failed\n");

    mpi_free( test );
    mpi_free( out1_a );
    mpi_free( out1_b );
    mpi_free( out2 );
}
Example #3
0
static
apr_status_t ap_spread_log_writer(request_rec *r, void *handle,
		const char **strs, int *strl, int nelts, apr_size_t len)
{
	spreaded_log *sl = (spreaded_log *)handle;
	log_spread2_options *opt = sl ? &sl->opt : NULL;

	if (sl == NULL || opt == NULL) {
		ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
				"ap_spread_log_writer called without handle");
		return -1;
	}

	int i;
	char *s, *str = apr_pcalloc(r->pool, len);

	for (i = 0, s = str; i < nelts; ++i) {
		memcpy(s, strs[i], strl[i]);
		s += strl[i];
	}


	if (opt->key) {
		do_encrypt(r->pool, opt->key, str, len, 1);
	}

	apr_status_t rv = SP_multicast(opt->mbox, RELIABLE_MESS, sl->group, 1, len, str);

	if (rv < 0) {
		ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
				MNAME ": could not multicast log message, not connected?");
		return rv;
	}
}
Example #4
0
pubnub_bymebl_t pbaes256_encrypt_alloc(pubnub_bymebl_t msg, uint8_t const* key, uint8_t const* iv)
{
    int encrypt_result;
    pubnub_bymebl_t result = { NULL, 0 };
    EVP_CIPHER_CTX* aes256 = EVP_CIPHER_CTX_new();

    if (NULL == aes256) {
        PUBNUB_LOG_ERROR("Failed to allocate AES-256 encryption context\n");
        return result;
    }

    result.ptr = (uint8_t*)malloc(msg.size + EVP_CIPHER_block_size(EVP_aes_256_cbc()));
    if (NULL == result.ptr) {
        EVP_CIPHER_CTX_free(aes256);
        PUBNUB_LOG_ERROR("Failed to allocate memory for AES-256 encryption\n");
        return result;
    }
    
    encrypt_result = do_encrypt(aes256, msg, key, iv, &result);
    if (-1 == encrypt_result) {
        free(result.ptr);
        result.ptr = NULL;
    }
    EVP_CIPHER_CTX_free(aes256);

    return result;
}
Example #5
0
static void
do_encrypt_block ( BLOWFISH_context *bc, byte *outbuf, const byte *inbuf )
{
  u32 d1, d2;

  d1 = buf_get_be32(inbuf);
  d2 = buf_get_be32(inbuf + 4);
  do_encrypt( bc, &d1, &d2 );
  buf_put_be32(outbuf, d1);
  buf_put_be32(outbuf + 4, d2);
}
Example #6
0
void measure_ciphermul(int size_input, mpz_t *plain, mpz_t *cipher,
                       int prime_size, mpz_t prime) {
    vector<double> measurements(NUM_SAMPLES, 0);
    char scratch_str[BUFLEN];

    for (int i=0; i<NUM_SAMPLES; i++) {
        do_encrypt(size_input, plain, cipher, prime_size, prime);
        measurements[i] = do_simel_ciphermul(size_input, plain, cipher, prime_size, prime)/size_input;
    }
    snprintf(scratch_str, BUFLEN-1, "h_simel_%d", prime_size);
    print_stats(scratch_str, measurements);

    for (int i=0; i<NUM_SAMPLES; i++) {
        do_encrypt(size_input, plain, cipher, prime_size, prime);
        measurements[i] = do_regular_ciphermul(size_input, plain, cipher, prime_size, prime)/size_input;
    }

    snprintf(scratch_str, BUFLEN-1, "h_regular_%d", prime_size);
    print_stats(scratch_str, measurements);
}
Example #7
0
int SendToDestination(int netfd, int tapfd, unsigned char *buffer, int len, ClientConf *Client)
{
	int cr_size = 0, dcr_size = 0;
	unsigned char dcr_buffer[BUFSIZ], cr_buffer[BUFSIZ];
	register int i = 0;
	struct iphdr *header;
	struct in_addr dst;

	/* If Client == NULL, then the packet is a raw packet from the tun/tap device, so it must not be decrypted */
	if (Client != NULL) {

		if (len == 0)
			return 0;

		/* Decrypt the message to get the destination */
		if (do_decrypt(&(Client->ctx), Client->PrivKey, Client->IV, buffer, len, dcr_buffer, &dcr_size) < 0) {
			fprintf(stderr, "SendToDestination : do_decrypt\n");
			return -1;
		}

		/* Empty packet. Drop it */
		if (!*dcr_buffer)
			return 0;
	}

	/* Get the header and the destination address */
	header = (Client == NULL ? (struct iphdr *)buffer : (struct iphdr *)dcr_buffer);
	dst.s_addr = header->daddr;

	/* Get the destination client's information if we have it */
	for (i = 0; i < CurrentClients; i++)
		if (dst.s_addr == Clients[i].ns.data.sin_addr.s_addr)
			Client = Clients + i;

	if (Client != NULL) {
		printf("SENT!\n");
		/* Encrypt the message with the destination key */
		if (do_encrypt(&(Client->ctx), Client->PrivKey, Client->IV, dcr_buffer, dcr_size, cr_buffer, &cr_size) < 0) {
			fprintf(stderr, "SendToDestination : do_encrypt\n");
			return -1;
		}
	}
	else
		return 0;

	if (WriteH(netfd, (struct sockaddr *)&Client->ns.data, (Client != NULL) ? cr_buffer : dcr_buffer, (Client != NULL) ? cr_size : dcr_size) < 0) {
	    fprintf(stderr, "SendToDestination : WriteH\n");
	    return -1;
	}

	return 0;
}
Example #8
0
int pbaes256_encrypt(pubnub_bymebl_t msg, uint8_t const* key, uint8_t const* iv, pubnub_bymebl_t *encrypted)
{
    int result;
    EVP_CIPHER_CTX* aes256 = EVP_CIPHER_CTX_new();

    if (NULL == aes256) {
        PUBNUB_LOG_ERROR("Failed to allocate AES-256 encryption context\n");
        return -1;
    }

    result = do_encrypt(aes256, msg, key, iv, encrypted);

    EVP_CIPHER_CTX_free(aes256);

    return result;
}
Example #9
0
int
elg_encrypt( int algo, MPI *resarr, MPI data, MPI *pkey )
{
    ELG_public_key pk;

    if( !is_ELGAMAL(algo) )
	return G10ERR_PUBKEY_ALGO;
    if( !data || !pkey[0] || !pkey[1] || !pkey[2] )
	return G10ERR_BAD_MPI;

    pk.p = pkey[0];
    pk.g = pkey[1];
    pk.y = pkey[2];
    resarr[0] = mpi_alloc( mpi_get_nlimbs( pk.p ) );
    resarr[1] = mpi_alloc( mpi_get_nlimbs( pk.p ) );
    do_encrypt( resarr[0], resarr[1], data, &pk );
    return 0;
}
Example #10
0
static int
test_keys ( ELG_secret_key *sk, unsigned int nbits, int nodie )
{
  ELG_public_key pk;
  gcry_mpi_t test = gcry_mpi_new ( 0 );
  gcry_mpi_t out1_a = gcry_mpi_new ( nbits );
  gcry_mpi_t out1_b = gcry_mpi_new ( nbits );
  gcry_mpi_t out2 = gcry_mpi_new ( nbits );
  int failed = 0;

  pk.p = sk->p;
  pk.g = sk->g;
  pk.y = sk->y;

  gcry_mpi_randomize ( test, nbits, GCRY_WEAK_RANDOM );

  do_encrypt ( out1_a, out1_b, test, &pk );
  decrypt ( out2, out1_a, out1_b, sk );
  if ( mpi_cmp( test, out2 ) )
    failed |= 1;

  sign ( out1_a, out1_b, test, sk );
  if ( !verify( out1_a, out1_b, test, &pk ) )
    failed |= 2;

  gcry_mpi_release ( test );
  gcry_mpi_release ( out1_a );
  gcry_mpi_release ( out1_b );
  gcry_mpi_release ( out2 );

  if (failed && !nodie)
    log_fatal ("Elgamal test key for %s %s failed\n",
               (failed & 1)? "encrypt+decrypt":"",
               (failed & 2)? "sign+verify":"");
  if (failed && DBG_CIPHER) 
    log_debug ("Elgamal test key for %s %s failed\n",
               (failed & 1)? "encrypt+decrypt":"",
               (failed & 2)? "sign+verify":"");

  return failed;
}
Example #11
0
gcry_err_code_t
_gcry_elg_encrypt (int algo, gcry_mpi_t *resarr,
                   gcry_mpi_t data, gcry_mpi_t *pkey, int flags)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  ELG_public_key pk;

  (void)algo;
  (void)flags;

  if ((! data) || (! pkey[0]) || (! pkey[1]) || (! pkey[2]))
    err = GPG_ERR_BAD_MPI;
  else
    {
      pk.p = pkey[0];
      pk.g = pkey[1];
      pk.y = pkey[2];
      resarr[0] = mpi_alloc (mpi_get_nlimbs (pk.p));
      resarr[1] = mpi_alloc (mpi_get_nlimbs (pk.p));
      do_encrypt (resarr[0], resarr[1], data, &pk);
    }
  return err;
}
Example #12
0
int main(int argc, char **argv)
{
	int encrypt_opt = 0;
	int decrypt_opt = 0;
	int input_opt = 0;
	int output_opt = 0;
	char *input_filename = NULL;
	char *output_filename = NULL;
 
	int input_fd;
	int output_fd;
	off_t file_len;
	char *p;
	char buf[sizeof(image_header_t) + 3];
	image_header_t *header;
 
	while (1) {
		static struct option long_options[] = {
			{"encrypt", no_argument,       0, 'e'},
			{"decrypt", no_argument,       0, 'd'},
			{"input",   required_argument, 0, 'i'},
			{"output",  required_argument, 0, 'o'},
			{0,         0,                 0, 0  }
		};
		int option_index = 0;
		int c = getopt_long(argc, argv, "dei:o:",
		                long_options, &option_index);
		if (c == -1)
			break;
 
		switch (c) {
		case 'd':
			decrypt_opt++;
			if (decrypt_opt > 1) {
				fprintf(stderr, "%s: decrypt may only be specified once\n",
				        argv[0]);
				show_usage(argv[0]);
			}
			break;
 
		case 'e':
			encrypt_opt++;
			if (encrypt_opt > 1) {
				fprintf(stderr, "%s: encrypt may only be specified once\n",
				        argv[0]);
				show_usage(argv[0]);
			}
			break;
 
		case 'i':
			input_opt++;
			if (input_opt > 1) {
				fprintf(stderr, "%s: only one input file may be specified\n",
				        argv[0]);
				show_usage(argv[0]);
			}
			if (strcmp("-", optarg) != 0) {
				input_filename = optarg;
			}
			break;
 
		case 'o':
			output_opt++;
			if (output_opt > 1) {
				fprintf(stderr, "%s: only one output file may be specified\n",
				        argv[0]);
				show_usage(argv[0]);
			}
			if (strcmp("-", optarg) != 0) {
				output_filename = optarg;
			}
			break;
 
		case '?':
			exit(-1);
 
		default:
			abort();
		}
	}
 
	if (decrypt_opt && encrypt_opt) {
		fprintf(stderr, "%s: decrypt and encrypt may not be used together\n",
		        argv[0]);
		show_usage(argv[0]);
	}
 
	if (!decrypt_opt && !encrypt_opt) {
		fprintf(stderr, "%s: neither decrypt or encrypt were specified\n",
		        argv[0]);
		show_usage(argv[0]);
	}
 
	temp_fd = fileno(tmpfile());
	if (temp_fd < 0) {
		fprintf(stderr, "Can't create temporary file\n");
		exit(EXIT_FAILURE);
	}
 
	atexit(exit_cleanup);
	DES_set_key_unchecked((const_DES_cblock *)DES_KEY, &schedule);
 
	if (input_filename) {
		input_fd = open(input_filename, O_RDONLY);
		if (input_fd < 0) {
			fprintf(stderr, "Can't open %s for reading: %s\n", input_filename,
			        strerror(errno));
			exit(EXIT_FAILURE);
		}
		copy_file(input_fd, temp_fd);
		close(input_fd);
	}
	else {
		copy_file(STDIN_FILENO, temp_fd);
	}
 
	file_len = lseek(temp_fd, 0, SEEK_CUR);
	if (file_len < 64) {
		fprintf(stderr, "Not enough data\n");
		exit(EXIT_FAILURE);
	}
 
	p = mmap(0, file_len, PROT_READ|PROT_WRITE, MAP_SHARED, temp_fd, 0);
	if (p == MAP_FAILED) {
		fprintf(stderr, "mmap failed: %s\n", strerror(errno));
		exit(EXIT_FAILURE);
	}	
 
	if (encrypt_opt) {
		header = (image_header_t *)p;
		off_t len = min(file_len,
		                ntohl(header->ih_size) + sizeof(image_header_t));
		if (ntohl(header->ih_magic) != IH_MAGIC) {
			fprintf(stderr, "Header magic incorrect: "
			        "expected 0x%08X, got 0x%08X\n",
			        IH_MAGIC, ntohl(header->ih_magic));
			munmap(p, file_len);
			exit(EXIT_FAILURE);
		}
		do_encrypt(p, len);
		munmap(p, file_len);
		if (len != file_len) {
			if (ftruncate(temp_fd, len) < 0) {
				fprintf(stderr, "ftruncate failed: %s\n", strerror(errno));
				exit(EXIT_FAILURE);
			}
		}		
	}
 
	if (decrypt_opt) {
		off_t header_len = min(file_len, sizeof(image_header_t) + 3);
		memcpy(buf, p, header_len);
		do_decrypt(buf, header_len);
		header = (image_header_t *)buf;
		if (ntohl(header->ih_magic) != IH_MAGIC) {
			fprintf(stderr, "Header magic incorrect: "
			        "expected 0x%08X, got 0x%08X\n",
			        IH_MAGIC, ntohl(header->ih_magic));
			exit(EXIT_FAILURE);
		}
		do_decrypt(p, file_len);
		munmap(p, file_len);
	}
 
	lseek(temp_fd, 0, SEEK_SET);
	if (output_filename) {
		output_fd = creat(output_filename, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
		if (output_fd < 0) {
			fprintf(stderr, "Can't open %s for writing: %s\n",
			        output_filename, strerror(errno));
			exit(EXIT_FAILURE);
		}
		copy_file(temp_fd, output_fd);
		close(output_fd);
	}
	else {
		copy_file(temp_fd, STDOUT_FILENO);
	}
 
	exit(EXIT_SUCCESS);
	return 0;
}
Example #13
0
/**************************************************************************
 *
 *  Function:  ubsec_ioctl
 *
 *************************************************************************/
static int 
ubsec_ioctl(struct inode *inode,struct file *filp,unsigned int cmd, unsigned long arg)
{

  long			Retval=0;

  int                   status = 0;
  int                   deadlockctr = 0;
  
  unsigned short        value;

  
  
  /* Simple round robin scheduling of device. We need to increment
     first since the keysetup command may block. */
  
 TheBeginning:

  deadlockctr = 0;
  do {

    /* For diagnostic related stuff do not try any available devices */
    /* Try the intended device or all devices as directed by the command */

    if (cmd >= UBSEC_DEVICEDUMP || cmd == UBSEC_SELFTEST || cmd == UBSEC_FAILDEVICE) {
      break;
    }

    if(++deadlockctr == (NumDevices * 2)) {  /* to be conservative... */
#ifdef DEBUG_FAILOVER
      PRINTK("dispatch found no more devices.\n");
#endif
      return 1; /* error: no more devices */
    }

    if ((++SelectedDevice) == NumDevices)
      SelectedDevice=0;

  } while(GetDeviceStatus(DeviceInfoList[SelectedDevice]));

#ifdef DEBUG_FAILOVER
  printk("\n");
  PRINTK("dsptch-pre: SltdDev=%d,DevStati=%d %d\n", 
	 SelectedDevice, DeviceInfoList[0].DeviceStatus, DeviceInfoList[1].DeviceStatus);
#endif


  switch(cmd) {
#ifdef BCM_OEM_1
  case BCM_OEM_1_IOCTL1:
	BCM_OEM1_IOCTL1_HANDLER();
	break;
  case BCM_OEM_1_IOCTL2:
	BCM_OEM1_IOCTL2_HANDLER();
	break;
#endif /* BCM_OEM_1 */

  case UBSEC_ENCRYPT_DECRYPT_FUNC:
    status = do_encrypt(DeviceInfoList[SelectedDevice].Context, 
			(void *)arg, DeviceInfoList[SelectedDevice].Features);
    break;

  case UBSEC_KEY_SETUP_FUNC:
    status = ubsec_keysetup(DeviceInfoList[SelectedDevice].Context, (void *)arg);
    break;

  case UBSEC_MATH_FUNC:
    status = ubsec_math(DeviceInfoList[SelectedDevice].Context, (void *)arg);
    break;

  case UBSEC_RNG_FUNC:
    if (DeviceInfoList[SelectedDevice].Features & UBSEC_EXTCHIPINFO_RNG)
      status = ubsec_rng(DeviceInfoList[SelectedDevice].Context, (void *)arg);
    else
      status = UBSEC_STATUS_NO_DEVICE;
    break;
    
  case UBSEC_TLS_HMAC_FUNC:
    status = ubsec_tlsmac(DeviceInfoList[SelectedDevice].Context, (void *)arg);
    break;
    
  case UBSEC_SSL_MAC_FUNC:
    status = ubsec_sslmac(DeviceInfoList[SelectedDevice].Context, (void *)arg);
    break;
    
  case UBSEC_SSL_HASH_FUNC:
    status = ubsec_hash(DeviceInfoList[SelectedDevice].Context, (void *)arg);
    break;

  case UBSEC_SSL_DES_FUNC:
    status = ubsec_sslcipher(DeviceInfoList[SelectedDevice].Context, (void *)arg,
			     DeviceInfoList[SelectedDevice].Features);
    break;

  case UBSEC_SSL_ARC4_FUNC:
    if (DeviceInfoList[SelectedDevice].Features & UBSEC_EXTCHIPINFO_ARC4)
      status = ubsec_sslarc4(DeviceInfoList[SelectedDevice].Context, (void *)arg);
    else
      status = UBSEC_STATUS_NO_DEVICE;
    break;

  case UBSEC_CHIPINFO_FUNC:
    status = obsolete_chipinfo(DeviceInfoList[SelectedDevice].Context, (void *)arg);
    break;

  case UBSEC_STATS_FUNC:
    {
      ubsec_stats_io_t IOInfo;
      int device_num;
      if (copy_from_user((void *) &IOInfo,(void *) arg, sizeof(ubsec_stats_io_t)))
        return -EFAULT; 
      device_num  = IOInfo.device_num;


	if ( (device_num >= NumDevices) || (device_num < 0) )
		return -1;

      ubsec_GetStatistics(DeviceInfoList[device_num].Context, &IOInfo.dev_stats);
      if (copy_to_user((void *) arg, (void *) &IOInfo, sizeof(ubsec_stats_io_t)))
        return -EFAULT;
    }
    break;
    
  case UBSEC_EXTCHIPINFO_FUNC:
    if (copy_from_user((void *)&ExtChipInfo, (void *)arg, sizeof(ubsec_chipinfo_io_t)))
      return -EFAULT; 
    if (ExtChipInfo.Status !=sizeof(ubsec_chipinfo_io_t)) {
      UserCopySize = sizeof(ubsec_chipinfo_io_t);
      if (UserCopySize > ExtChipInfo.Status)
	UserCopySize = ExtChipInfo.Status;
      ExtChipInfo.Status = UBSEC_STATUS_NO_DEVICE;
      if (copy_to_user((void *)arg, (void *)&ExtChipInfo, UserCopySize))
        return -EFAULT;
      return(-1);
    }
    else if ((ExtChipInfo.CardNum >= NumDevices) || (ExtChipInfo.CardNum < 0)) {
      ExtChipInfo.CardNum = NumDevices; 
      ExtChipInfo.Status = UBSEC_STATUS_INVALID_PARAMETER; 
    }
    else {
      status = ubsec_chipinfo(DeviceInfoList[ExtChipInfo.CardNum].Context, &ExtChipInfo); 
      ExtChipInfo.NumDevices = NumDevices; 
      ExtChipInfo.Features &= DeviceInfoList[ExtChipInfo.CardNum].Features;
      ExtChipInfo.Status = UBSEC_STATUS_SUCCESS; 
    }
    if (copy_to_user((void *)arg, (void *)&ExtChipInfo, sizeof(ubsec_chipinfo_io_t)))
      return -EFAULT; 
    if (ExtChipInfo.Status != UBSEC_STATUS_SUCCESS)
      return(-1);
    else
      return(0);
    break;
    
  case UBSEC_DEVICEDUMP:
    if (copy_from_user((void *)&PInt_Contents, (void *)arg, sizeof(int)))
      return -EFAULT;
    Retval=DumpDeviceInfo((PInt)&PInt_Contents);
    if (Retval)
      return(-1); /* Error */
    break;

  case UBSEC_FAILDEVICE:
    if (copy_from_user((void *)&PInt_Contents, (void *)arg, sizeof(int)))
      return -EFAULT;
    Retval=FailDevices((PInt)&PInt_Contents);
    if (Retval)
      return(-1); /* Error */
    break;

  case UBSEC_SELFTEST:
#ifdef BCM_OEM_1
	DISABLE_BCM_OEM1();
#endif /*BCM_OEM_1 */
    if (copy_from_user((void *)&PInt_Contents, (void *)arg, sizeof(int)))
      return -EFAULT;
    Retval=TestDevices((PInt)&PInt_Contents);
#ifdef BCM_OEM_1
	ENABLE_BCM_OEM1();
#endif /*BCM_OEM_1 */
    if (Retval)
      return (Retval); /* Error */

  case UBSEC_GETVERSION:
    if (copy_from_user((void *)&PInt_Contents, (void *)arg, sizeof(int)))
      return -EFAULT;
    Retval=GetHardwareVersion((PInt)&PInt_Contents); /* For the moment one card */
    Retval=Retval<<16;
    Retval+=Version;
    #ifdef LINUX_IA64
    return Retval;
    #else
    return(-Retval);
    #endif
    break;

  case UBSEC_GETNUMCARDS:
	if (copy_to_user((void *)arg,&NumDevices,sizeof(int)))
          return -EFAULT;
    return NumDevices;


  case  UBSEC_GET_FUNCTION_PTRS:
	{
	ubsec_Function_Ptrs_t fptrs;
	get_ubsec_Function_Ptrs(&fptrs);
      	if (copy_to_user((void *) arg, (void *) &fptrs, sizeof(ubsec_Function_Ptrs_t)))
          return -EFAULT;
	}
    break;

#ifdef DVT 
  case UBSEC_RESERVED:
    if (copy_from_user((void *)&DVTparams, (void *)arg, sizeof(DVT_Params_t)))
      return -EFAULT;
    if ((DVTparams.CardNum >= NumDevices) || (DVTparams.CardNum < 0)) {
      {PRINTK("Invalid CardNum (%d), must be 0",DVTparams.CardNum);}
      if (NumDevices == 1)
	printk("\n");
      else
	printk("-%d\n",NumDevices-1);
      return -1; 
    }
    switch (DVTparams.Command) {
    case UBSEC_DVT_PAGESIZE: /* Wrapper command */
      DVTparams.OutParameter = Page_Size;
      if (!DVTparams.InParameter) {
	DVTparams.OutParameter = Page_Size = PAGE_SIZE;
	DVTparams.Status = UBSEC_STATUS_SUCCESS;
	Retval = UBSEC_STATUS_SUCCESS;
      }      
      else if ((DVTparams.InParameter > PAGE_SIZE) ||
	       (DVTparams.InParameter < 2)) {
	DVTparams.Status = UBSEC_STATUS_INVALID_PARAMETER;
      }
      else {
	if (!power_of_2(DVTparams.InParameter))
	  DVTparams.InParameter = next_smaller_power_of_2(DVTparams.InParameter);
	DVTparams.OutParameter = Page_Size; 
	Page_Size = DVTparams.InParameter; 
	DVTparams.Status = UBSEC_STATUS_SUCCESS;
	Retval = UBSEC_STATUS_SUCCESS;
      }
      break;
    default:
      /* Pass all other commands down to the SRL */
      Retval=ubsec_dvt_handler((void *)DeviceInfoList[DVTparams.CardNum].Context,(void *)&DVTparams); 
    };
    if (copy_to_user((void *)arg, (void *)&DVTparams, sizeof(DVT_Params_t)))
      return -EFAULT;
    return(Retval);
    break;
#endif /* DVT */

  default:
    return -EINVAL;
  }
  
#ifdef DEBUG_FAILOVER
  PRINTK("dsptch-pst: SltdDev=%d,DevStati=%d %d\n", 
	 SelectedDevice, DeviceInfoList[0].DeviceStatus, DeviceInfoList[1].DeviceStatus);
  if((status == ETIMEDOUT) || (status == -ETIMEDOUT)) {
    PRINTK("dispatch.c: TIMED OUT SelectedDevice=%d, DeviceStatus=%d\n", 
	   SelectedDevice, DeviceInfoList[SelectedDevice].DeviceStatus);
  }
#endif

  switch(status) {
  case 0:
    break;

  case (ETIMEDOUT):
    status = -ETIMEDOUT;
  case (-ETIMEDOUT):
    DeviceInfoList[SelectedDevice].DeviceStatus = TestDevice(SelectedDevice);
    /*  goto TheBeginning; */
    return(status);
    break;
    
  default:
    /*  goto TheBeginning; */
    return(status);
    break;
  }
  
  return 0;
}