1*12720SWyllys.Ingersoll@Sun.COM /* 2*12720SWyllys.Ingersoll@Sun.COM * CDDL HEADER START 3*12720SWyllys.Ingersoll@Sun.COM * 4*12720SWyllys.Ingersoll@Sun.COM * The contents of this file are subject to the terms of the 5*12720SWyllys.Ingersoll@Sun.COM * Common Development and Distribution License (the "License"). 6*12720SWyllys.Ingersoll@Sun.COM * You may not use this file except in compliance with the License. 7*12720SWyllys.Ingersoll@Sun.COM * 8*12720SWyllys.Ingersoll@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*12720SWyllys.Ingersoll@Sun.COM * or http://www.opensolaris.org/os/licensing. 10*12720SWyllys.Ingersoll@Sun.COM * See the License for the specific language governing permissions 11*12720SWyllys.Ingersoll@Sun.COM * and limitations under the License. 12*12720SWyllys.Ingersoll@Sun.COM * 13*12720SWyllys.Ingersoll@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each 14*12720SWyllys.Ingersoll@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*12720SWyllys.Ingersoll@Sun.COM * If applicable, add the following below this CDDL HEADER, with the 16*12720SWyllys.Ingersoll@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying 17*12720SWyllys.Ingersoll@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner] 18*12720SWyllys.Ingersoll@Sun.COM * 19*12720SWyllys.Ingersoll@Sun.COM * CDDL HEADER END 20*12720SWyllys.Ingersoll@Sun.COM */ 21*12720SWyllys.Ingersoll@Sun.COM 22*12720SWyllys.Ingersoll@Sun.COM /* 23*12720SWyllys.Ingersoll@Sun.COM * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 24*12720SWyllys.Ingersoll@Sun.COM */ 25*12720SWyllys.Ingersoll@Sun.COM 26*12720SWyllys.Ingersoll@Sun.COM /** 27*12720SWyllys.Ingersoll@Sun.COM * \file KMSAgentCryptoUtilities.cpp 28*12720SWyllys.Ingersoll@Sun.COM */ 29*12720SWyllys.Ingersoll@Sun.COM 30*12720SWyllys.Ingersoll@Sun.COM #include <openssl/rand.h> 31*12720SWyllys.Ingersoll@Sun.COM #include <openssl/hmac.h> 32*12720SWyllys.Ingersoll@Sun.COM #include <openssl/err.h> 33*12720SWyllys.Ingersoll@Sun.COM #include <openssl/sha.h> 34*12720SWyllys.Ingersoll@Sun.COM 35*12720SWyllys.Ingersoll@Sun.COM #include "KMSAgentCryptoUtilities.h" 36*12720SWyllys.Ingersoll@Sun.COM #include "SYSCommon.h" 37*12720SWyllys.Ingersoll@Sun.COM #include "KMSAgentStringUtilities.h" 38*12720SWyllys.Ingersoll@Sun.COM //#include "ApplianceParameters.h" 39*12720SWyllys.Ingersoll@Sun.COM 40*12720SWyllys.Ingersoll@Sun.COM // Find header in CryptoUtilities.h 41*12720SWyllys.Ingersoll@Sun.COM bool GetPseudorandomBytes( 42*12720SWyllys.Ingersoll@Sun.COM int i_iNumBytes, 43*12720SWyllys.Ingersoll@Sun.COM unsigned char* o_pBytes ) 44*12720SWyllys.Ingersoll@Sun.COM { 45*12720SWyllys.Ingersoll@Sun.COM if ( 1 != RAND_bytes( o_pBytes, i_iNumBytes) ) 46*12720SWyllys.Ingersoll@Sun.COM { 47*12720SWyllys.Ingersoll@Sun.COM return false; 48*12720SWyllys.Ingersoll@Sun.COM } 49*12720SWyllys.Ingersoll@Sun.COM 50*12720SWyllys.Ingersoll@Sun.COM return true; 51*12720SWyllys.Ingersoll@Sun.COM } 52*12720SWyllys.Ingersoll@Sun.COM 53*12720SWyllys.Ingersoll@Sun.COM // assumes o_pHashedBuffer points to HASH_LENGTH bytes 54*12720SWyllys.Ingersoll@Sun.COM bool HashBuffer( 55*12720SWyllys.Ingersoll@Sun.COM const unsigned char* i_pBufferToHash, 56*12720SWyllys.Ingersoll@Sun.COM int i_iBufferToHashSize, 57*12720SWyllys.Ingersoll@Sun.COM unsigned char* o_pHashedBuffer ) 58*12720SWyllys.Ingersoll@Sun.COM { 59*12720SWyllys.Ingersoll@Sun.COM 60*12720SWyllys.Ingersoll@Sun.COM FATAL_ASSERT( HASH_LENGTH == SHA_DIGEST_LENGTH ); 61*12720SWyllys.Ingersoll@Sun.COM FATAL_ASSERT( i_pBufferToHash && (i_iBufferToHashSize > 0) && o_pHashedBuffer ); 62*12720SWyllys.Ingersoll@Sun.COM 63*12720SWyllys.Ingersoll@Sun.COM unsigned char aDigest[HASH_LENGTH]; 64*12720SWyllys.Ingersoll@Sun.COM 65*12720SWyllys.Ingersoll@Sun.COM if ( NULL == SHA1( i_pBufferToHash, i_iBufferToHashSize, aDigest ) ) 66*12720SWyllys.Ingersoll@Sun.COM { 67*12720SWyllys.Ingersoll@Sun.COM return false; 68*12720SWyllys.Ingersoll@Sun.COM } 69*12720SWyllys.Ingersoll@Sun.COM 70*12720SWyllys.Ingersoll@Sun.COM memcpy( o_pHashedBuffer, aDigest, HASH_LENGTH ); 71*12720SWyllys.Ingersoll@Sun.COM 72*12720SWyllys.Ingersoll@Sun.COM return true; 73*12720SWyllys.Ingersoll@Sun.COM } 74*12720SWyllys.Ingersoll@Sun.COM 75*12720SWyllys.Ingersoll@Sun.COM // assumes o_pHMACBuffer points to HMAC_LENGTH bytes 76*12720SWyllys.Ingersoll@Sun.COM bool HMACBuffers( 77*12720SWyllys.Ingersoll@Sun.COM int i_iBufferCount, 78*12720SWyllys.Ingersoll@Sun.COM const unsigned char** i_pBufferToHMAC, 79*12720SWyllys.Ingersoll@Sun.COM int* i_pBufferToHMACSize, 80*12720SWyllys.Ingersoll@Sun.COM const unsigned char* i_pHMACKey, 81*12720SWyllys.Ingersoll@Sun.COM int i_iHMACKeySize, 82*12720SWyllys.Ingersoll@Sun.COM unsigned char* o_pHMACBuffer ) 83*12720SWyllys.Ingersoll@Sun.COM { 84*12720SWyllys.Ingersoll@Sun.COM // assumes o_pHMACBuffer points to HMAC_LENGTH bytes 85*12720SWyllys.Ingersoll@Sun.COM 86*12720SWyllys.Ingersoll@Sun.COM FATAL_ASSERT( HMAC_LENGTH == SHA_DIGEST_LENGTH ); 87*12720SWyllys.Ingersoll@Sun.COM FATAL_ASSERT( (i_iBufferCount > 0) && 88*12720SWyllys.Ingersoll@Sun.COM i_pBufferToHMAC && 89*12720SWyllys.Ingersoll@Sun.COM i_pBufferToHMACSize && 90*12720SWyllys.Ingersoll@Sun.COM i_pHMACKey && 91*12720SWyllys.Ingersoll@Sun.COM (i_iHMACKeySize > 0) && o_pHMACBuffer ); 92*12720SWyllys.Ingersoll@Sun.COM 93*12720SWyllys.Ingersoll@Sun.COM HMAC_CTX stContext; 94*12720SWyllys.Ingersoll@Sun.COM 95*12720SWyllys.Ingersoll@Sun.COM HMAC_CTX_init( &stContext ); 96*12720SWyllys.Ingersoll@Sun.COM 97*12720SWyllys.Ingersoll@Sun.COM HMAC_Init_ex( &stContext, i_pHMACKey, i_iHMACKeySize, EVP_sha1(), NULL ); 98*12720SWyllys.Ingersoll@Sun.COM 99*12720SWyllys.Ingersoll@Sun.COM int i; 100*12720SWyllys.Ingersoll@Sun.COM for ( i = 0; i < i_iBufferCount; i++ ) 101*12720SWyllys.Ingersoll@Sun.COM { 102*12720SWyllys.Ingersoll@Sun.COM HMAC_Update( &stContext, i_pBufferToHMAC[i], i_pBufferToHMACSize[i] ); 103*12720SWyllys.Ingersoll@Sun.COM } 104*12720SWyllys.Ingersoll@Sun.COM 105*12720SWyllys.Ingersoll@Sun.COM unsigned int iHMACSize = HMAC_LENGTH; 106*12720SWyllys.Ingersoll@Sun.COM 107*12720SWyllys.Ingersoll@Sun.COM HMAC_Final( &stContext, o_pHMACBuffer, &iHMACSize ); 108*12720SWyllys.Ingersoll@Sun.COM 109*12720SWyllys.Ingersoll@Sun.COM FATAL_ASSERT( iHMACSize == HMAC_LENGTH ); 110*12720SWyllys.Ingersoll@Sun.COM 111*12720SWyllys.Ingersoll@Sun.COM HMAC_CTX_cleanup( &stContext ); 112*12720SWyllys.Ingersoll@Sun.COM 113*12720SWyllys.Ingersoll@Sun.COM return true; 114*12720SWyllys.Ingersoll@Sun.COM } 115*12720SWyllys.Ingersoll@Sun.COM 116