]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_apple_csp/lib/pbkdf2.c
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_apple_csp / lib / pbkdf2.c
1 /*
2 * Copyright (c) 2000-2001,2011,2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /*
20 File: pbkdf2.c
21 Contains: Apple Data Security Services PKCS #5 PBKDF2 function definition.
22 Copyright (c) 1999,2011,2013-2014 Apple Inc. All Rights Reserved.
23 */
24 #include "pbkdf2.h"
25 #include <ConditionalMacros.h>
26 #include <string.h>
27 /* Will write hLen bytes into dataPtr according to PKCS #5 2.0 spec.
28 See: http://www.rsa.com/rsalabs/pubs/PKCS/html/pkcs-5.html for details.
29 tempBuffer is a pointer to at least MAX (hLen, saltLen + 4) + hLen bytes. */
30 static void
31 F (PRF prf, uint32 hLen,
32 const void *passwordPtr, uint32 passwordLen,
33 const void *saltPtr, uint32 saltLen,
34 uint32 iterationCount,
35 uint32 blockNumber,
36 void *dataPtr,
37 void *tempBuffer)
38 {
39 uint8 *inBlock, *outBlock, *resultBlockPtr;
40 uint32 iteration;
41 outBlock = (uint8*)tempBuffer;
42 inBlock = outBlock + hLen;
43 /* Set up inBlock to contain Salt || INT (blockNumber). */
44 memcpy (inBlock, saltPtr, saltLen);
45
46 inBlock[saltLen + 0] = (uint8)(blockNumber >> 24);
47 inBlock[saltLen + 1] = (uint8)(blockNumber >> 16);
48 inBlock[saltLen + 2] = (uint8)(blockNumber >> 8);
49 inBlock[saltLen + 3] = (uint8)(blockNumber);
50
51 /* Caculate U1 (result goes to outBlock) and copy it to resultBlockPtr. */
52 resultBlockPtr = (uint8*)dataPtr;
53 prf (passwordPtr, passwordLen, inBlock, saltLen + 4, outBlock);
54 memcpy (resultBlockPtr, outBlock, hLen);
55 /* Calculate U2 though UiterationCount. */
56 for (iteration = 2; iteration <= iterationCount; iteration++)
57 {
58 uint8 *tempBlock;
59 uint32 byte;
60 /* Swap inBlock and outBlock pointers. */
61 tempBlock = inBlock;
62 inBlock = outBlock;
63 outBlock = tempBlock;
64 /* Now inBlock conatins Uiteration-1. Calclulate Uiteration into outBlock. */
65 prf (passwordPtr, passwordLen, inBlock, hLen, outBlock);
66 /* Xor data in dataPtr (U1 \xor U2 \xor ... \xor Uiteration-1) with
67 outBlock (Uiteration). */
68 for (byte = 0; byte < hLen; byte++)
69 resultBlockPtr[byte] ^= outBlock[byte];
70 }
71 }
72 void pbkdf2 (PRF prf, uint32 hLen,
73 const void *passwordPtr, uint32 passwordLen,
74 const void *saltPtr, uint32 saltLen,
75 uint32 iterationCount,
76 void *dkPtr, uint32 dkLen,
77 void *tempBuffer)
78 {
79 uint32 completeBlocks = dkLen / hLen;
80 uint32 partialBlockSize = dkLen % hLen;
81 uint32 blockNumber;
82 uint8 *dataPtr = (uint8*)dkPtr;
83 uint8 *blkBuffer = (uint8*)tempBuffer;
84 /* First calculate all the complete hLen sized blocks required. */
85 for (blockNumber = 1; blockNumber <= completeBlocks; blockNumber++)
86 {
87 F (prf, hLen, passwordPtr, passwordLen, saltPtr, saltLen,
88 iterationCount, blockNumber, dataPtr, blkBuffer + hLen);
89 dataPtr += hLen;
90 }
91 /* Finally if the requested output size was not an even multiple of hLen, calculate
92 the final block and copy the first partialBlockSize bytes of it to the output. */
93 if (partialBlockSize > 0)
94 {
95 F (prf, hLen, passwordPtr, passwordLen, saltPtr, saltLen,
96 iterationCount, blockNumber, blkBuffer, blkBuffer + hLen);
97 memcpy (dataPtr, blkBuffer, partialBlockSize);
98 }
99 }