4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
27 * \file KMSAgentCryptoUtilities.cpp
30 #include <openssl/rand.h>
31 #include <openssl/hmac.h>
32 #include <openssl/err.h>
33 #include <openssl/sha.h>
35 #include "KMSAgentCryptoUtilities.h"
36 #include "SYSCommon.h"
37 #include "KMSAgentStringUtilities.h"
38 //#include "ApplianceParameters.h"
40 // Find header in CryptoUtilities.h
41 bool GetPseudorandomBytes(
43 unsigned char* o_pBytes
)
45 if ( 1 != RAND_bytes( o_pBytes
, i_iNumBytes
) )
53 // assumes o_pHashedBuffer points to HASH_LENGTH bytes
55 const unsigned char* i_pBufferToHash
,
56 int i_iBufferToHashSize
,
57 unsigned char* o_pHashedBuffer
)
60 FATAL_ASSERT( HASH_LENGTH
== SHA_DIGEST_LENGTH
);
61 FATAL_ASSERT( i_pBufferToHash
&& (i_iBufferToHashSize
> 0) && o_pHashedBuffer
);
63 unsigned char aDigest
[HASH_LENGTH
];
65 if ( NULL
== SHA1( i_pBufferToHash
, i_iBufferToHashSize
, aDigest
) )
70 memcpy( o_pHashedBuffer
, aDigest
, HASH_LENGTH
);
75 // assumes o_pHMACBuffer points to HMAC_LENGTH bytes
78 const unsigned char** i_pBufferToHMAC
,
79 int* i_pBufferToHMACSize
,
80 const unsigned char* i_pHMACKey
,
82 unsigned char* o_pHMACBuffer
)
84 // assumes o_pHMACBuffer points to HMAC_LENGTH bytes
86 FATAL_ASSERT( HMAC_LENGTH
== SHA_DIGEST_LENGTH
);
87 FATAL_ASSERT( (i_iBufferCount
> 0) &&
89 i_pBufferToHMACSize
&&
91 (i_iHMACKeySize
> 0) && o_pHMACBuffer
);
95 HMAC_CTX_init( &stContext
);
97 HMAC_Init_ex( &stContext
, i_pHMACKey
, i_iHMACKeySize
, EVP_sha1(), NULL
);
100 for ( i
= 0; i
< i_iBufferCount
; i
++ )
102 HMAC_Update( &stContext
, i_pBufferToHMAC
[i
], i_pBufferToHMACSize
[i
] );
105 unsigned int iHMACSize
= HMAC_LENGTH
;
107 HMAC_Final( &stContext
, o_pHMACBuffer
, &iHMACSize
);
109 FATAL_ASSERT( iHMACSize
== HMAC_LENGTH
);
111 HMAC_CTX_cleanup( &stContext
);