| 1 |
/* |
| 2 |
* Copyright (c) 2010 SURFnet bv |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
RFC4880.cpp |
| 29 |
|
| 30 |
Implements a secure password-based key derivation scheme. It is not a generic |
| 31 |
implementation of the RFC but only generates 256-bit AES keys according to |
| 32 |
the "iterated and salted" scheme. |
| 33 |
*****************************************************************************/ |
| 34 |
|
| 35 |
#include "config.h" |
| 36 |
#include "RFC4880.h" |
| 37 |
#include "CryptoFactory.h" |
| 38 |
#include "HashAlgorithm.h" |
| 39 |
|
| 40 |
// This function derives a 256-bit AES key from the supplied password data |
| 41 |
bool RFC4880::PBEDeriveKey(const ByteString& password, ByteString& salt, AESKey** ppKey) |
| 42 |
{ |
| 43 |
// Check that a proper salt value was supplied; it should be at least 8 bytes long |
| 44 |
if (salt.size() < 8) |
| 45 |
{ |
| 46 |
ERROR_MSG("Insufficient salt data supplied for password-based encryption"); |
| 47 |
|
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
// Check other parameters |
| 52 |
if ((password.size() == 0) || (ppKey == NULL)) |
| 53 |
{ |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
// Determine the iteration count based on the last byte of the salt |
| 58 |
unsigned int iter = PBE_ITERATION_BASE_COUNT + salt[salt.size() - 1]; |
| 59 |
|
| 60 |
// Get a hash instance |
| 61 |
HashAlgorithm* hash = CryptoFactory::i()->getHashAlgorithm(HashAlgo::SHA256); |
| 62 |
|
| 63 |
if (hash == NULL) |
| 64 |
{ |
| 65 |
ERROR_MSG("Could not get a SHA-256 instance"); |
| 66 |
|
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
// Perform the first iteration which takes as input the salt value and |
| 71 |
// the password |
| 72 |
ByteString intermediate; |
| 73 |
|
| 74 |
if (!hash->hashInit() || |
| 75 |
!hash->hashUpdate(salt) || |
| 76 |
!hash->hashUpdate(password) || |
| 77 |
!hash->hashFinal(intermediate)) |
| 78 |
{ |
| 79 |
ERROR_MSG("Hashing failed"); |
| 80 |
|
| 81 |
CryptoFactory::i()->recycleHashAlgorithm(hash); |
| 82 |
|
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
// Perform the remaining iteration |
| 87 |
while (--iter > 0) |
| 88 |
{ |
| 89 |
if (!hash->hashInit() || |
| 90 |
!hash->hashUpdate(intermediate) || |
| 91 |
!hash->hashFinal(intermediate)) |
| 92 |
{ |
| 93 |
ERROR_MSG("Hashing failed"); |
| 94 |
|
| 95 |
CryptoFactory::i()->recycleHashAlgorithm(hash); |
| 96 |
|
| 97 |
return false; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// Create the AES key instance |
| 102 |
*ppKey = new AESKey(256); |
| 103 |
(*ppKey)->setKeyBits(intermediate); |
| 104 |
|
| 105 |
// Release the hash instance |
| 106 |
CryptoFactory::i()->recycleHashAlgorithm(hash); |
| 107 |
|
| 108 |
return true; |
| 109 |
} |
| 110 |
|