1*1c9bd843Sdinak /* 2*1c9bd843Sdinak * CDDL HEADER START 3*1c9bd843Sdinak * 4*1c9bd843Sdinak * The contents of this file are subject to the terms of the 5*1c9bd843Sdinak * Common Development and Distribution License (the "License"). 6*1c9bd843Sdinak * You may not use this file except in compliance with the License. 7*1c9bd843Sdinak * 8*1c9bd843Sdinak * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*1c9bd843Sdinak * or http://www.opensolaris.org/os/licensing. 10*1c9bd843Sdinak * See the License for the specific language governing permissions 11*1c9bd843Sdinak * and limitations under the License. 12*1c9bd843Sdinak * 13*1c9bd843Sdinak * When distributing Covered Code, include this CDDL HEADER in each 14*1c9bd843Sdinak * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*1c9bd843Sdinak * If applicable, add the following below this CDDL HEADER, with the 16*1c9bd843Sdinak * fields enclosed by brackets "[]" replaced with your own identifying 17*1c9bd843Sdinak * information: Portions Copyright [yyyy] [name of copyright owner] 18*1c9bd843Sdinak * 19*1c9bd843Sdinak * CDDL HEADER END 20*1c9bd843Sdinak */ 21*1c9bd843Sdinak /* 22*1c9bd843Sdinak * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23*1c9bd843Sdinak * Use is subject to license terms. 24*1c9bd843Sdinak */ 25*1c9bd843Sdinak 26*1c9bd843Sdinak #pragma ident "%Z%%M% %I% %E% SMI" 27*1c9bd843Sdinak 28*1c9bd843Sdinak #include <stdio.h> 29*1c9bd843Sdinak #include <string.h> 30*1c9bd843Sdinak #include <fcntl.h> 31*1c9bd843Sdinak #include <locale.h> 32*1c9bd843Sdinak #include <cryptoutil.h> 33*1c9bd843Sdinak 34*1c9bd843Sdinak #define RANDOM_DEVICE "/dev/urandom" /* random device name */ 35*1c9bd843Sdinak 36*1c9bd843Sdinak /* 37*1c9bd843Sdinak * Put the requested amount of random data into a preallocated buffer. 38*1c9bd843Sdinak * Good for passphrase salts, initialization vectors. 39*1c9bd843Sdinak */ 40*1c9bd843Sdinak int 41*1c9bd843Sdinak pkcs11_random_data(void *dbuf, size_t dlen) 42*1c9bd843Sdinak { 43*1c9bd843Sdinak int fd; 44*1c9bd843Sdinak 45*1c9bd843Sdinak if (dbuf == NULL || dlen == 0) 46*1c9bd843Sdinak return (0); 47*1c9bd843Sdinak 48*1c9bd843Sdinak /* Read random data directly from /dev/urandom */ 49*1c9bd843Sdinak if ((fd = open(RANDOM_DEVICE, O_RDONLY)) != -1) { 50*1c9bd843Sdinak if (read(fd, dbuf, dlen) == dlen) { 51*1c9bd843Sdinak (void) close(fd); 52*1c9bd843Sdinak return (0); 53*1c9bd843Sdinak } 54*1c9bd843Sdinak (void) close(fd); 55*1c9bd843Sdinak } 56*1c9bd843Sdinak return (-1); 57*1c9bd843Sdinak } 58