softRandUtil.c revision 19193bb63b10fe65b6e01f1ce7232407a18a917a
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <fcntl.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <security/cryptoki.h>
#include <bignum.h>
#include <cryptoutil.h>
#include "softGlobal.h"
#include "softRandom.h"
#include "softCrypt.h"
{
long nread;
/*
* When random-number generator is called by asymmetric token
*/
if (token) {
if (soft_random_fd < 0) {
(void) pthread_mutex_lock(&soft_giant_mutex);
/* Check again holding the mutex */
if (soft_random_fd < 0) {
O_RDONLY);
if (soft_random_fd < 0) {
(void) pthread_mutex_unlock(
return (CKR_DEVICE_ERROR);
}
}
(void) pthread_mutex_unlock(&soft_giant_mutex);
}
} else {
if (soft_urandom_fd < 0) {
(void) pthread_mutex_lock(&soft_giant_mutex);
/* Check again holding the mutex */
if (soft_urandom_fd < 0) {
O_RDONLY);
if (soft_urandom_fd < 0) {
(void) pthread_mutex_unlock(
return (CKR_DEVICE_ERROR);
}
}
(void) pthread_mutex_unlock(&soft_giant_mutex);
}
}
if (token)
else
if (nread <= 0) {
return (CKR_DEVICE_ERROR);
}
return (CKR_OK);
}
/*
* This function guarantees to return non-zero random numbers.
*/
{
size_t i = 0;
char extrarand[32];
/*
* soft_random_generator() may return zeros.
*/
return (rv);
}
/*
* Walk through the returned random numbers pointed by ran_out,
* and look for any random number which is zero.
* If we find zero, call soft_random_generator() to generate
* another 32 random numbers pool. Replace any zeros in ran_out[]
* from the random number in pool.
*/
while (i < ran_len) {
if (((char *)ran_out)[i] != 0) {
i++;
continue;
}
if (ebc == 0) {
/* refresh extrarand */
extrarand_len = sizeof (extrarand);
return (rv);
}
ebc = extrarand_len;
}
-- ebc;
/*
* the next pass through the loop.
*/
}
return (CKR_OK);
}
/*
* Generate random number in BIGNUM format. length is in bits
*/
{
/* Convert length of bits to length of word to hold valid data. */
/* len1 is the byte count. */
/* Generate len1 bytes of data and store in memory pointed by value. */
return (convert_brv(rv));
}
/*
* If the bit length is not on word boundary, shift the existing
* bits in last word to right adjusted.
*/
if ((length % BIG_CHUNK_SIZE) != 0)
r->sign = 1;
return (BIG_OK);
}