1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <errno.h> 30 #include <fcntl.h> 31 #include <sys/stat.h> 32 #include <sys/types.h> 33 #include <security/cryptoki.h> 34 #include "softGlobal.h" 35 #include "softRandom.h" 36 #include "softSession.h" 37 38 39 CK_RV 40 C_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen) 41 { 42 43 CK_RV rv; 44 soft_session_t *session_p; 45 boolean_t lock_held = B_FALSE; 46 int fd; 47 long nwrite; 48 49 if (!softtoken_initialized) 50 return (CKR_CRYPTOKI_NOT_INITIALIZED); 51 52 /* Obtain the session pointer just for validity check. */ 53 rv = handle2session(hSession, &session_p); 54 if (rv != CKR_OK) 55 return (rv); 56 57 SES_REFRELE(session_p, lock_held); 58 59 if ((pSeed == NULL) || (ulSeedLen == 0)) { 60 return (CKR_ARGUMENTS_BAD); 61 } 62 63 while ((fd = open(DEV_URANDOM, O_WRONLY)) < 0) { 64 if (errno != EINTR) 65 break; 66 } 67 if (fd == -1) { 68 if (errno == EACCES) 69 return (CKR_RANDOM_SEED_NOT_SUPPORTED); 70 return (CKR_DEVICE_ERROR); 71 } 72 (void) fcntl(fd, F_SETFD, FD_CLOEXEC); 73 74 nwrite = looping_write(fd, pSeed, ulSeedLen); 75 if (nwrite <= 0) { 76 (void) close(fd); 77 return (CKR_DEVICE_ERROR); 78 } 79 80 (void) close(fd); 81 82 return (CKR_OK); 83 84 } 85 86 CK_RV 87 C_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData, 88 CK_ULONG ulRandomLen) 89 { 90 91 CK_RV rv; 92 soft_session_t *session_p; 93 boolean_t lock_held = B_FALSE; 94 95 if (!softtoken_initialized) 96 return (CKR_CRYPTOKI_NOT_INITIALIZED); 97 98 /* Obtain the session pointer just for validity check. */ 99 rv = handle2session(hSession, &session_p); 100 if (rv != CKR_OK) 101 return (rv); 102 103 SES_REFRELE(session_p, lock_held); 104 105 if ((pRandomData == NULL) || (ulRandomLen == 0)) { 106 return (CKR_ARGUMENTS_BAD); 107 } 108 109 return (soft_random_generator(pRandomData, ulRandomLen, B_FALSE)); 110 111 } 112