📄 pgpshare.c
字号:
/*____________________________________________________________________________
Copyright (C) 1997 Network Associates Inc. and affiliated companies.
All rights reserved.
$Id: pgpShare.c,v 1.10 1999/03/10 02:55:48 heller Exp $
____________________________________________________________________________*/
#include "pgpSharePriv.h"
#include "pgpUtilities.h"
#include "pgpHash.h"
#include "pgpMemoryMgr.h"
#include "pgpMem.h"
#include "pgpDebug.h"
typedef struct PGPShare PGPShare;
struct PGPShare
{
PGPContextRef context;
PGPMemoryMgrRef memoryMgr;
PGPKeyID keyID;
PGPShareID shareID;
PGPUInt32 threshold;
PGPUInt32 numShares;
PGPUInt32 totalShares;
PGPSize shareDataSize;
PGPByte * shareData;
PGPSize shareHashSize;
PGPByte * shareHash;
};
#define PGPValidateShare(ref) PGPValidateParam(ref != NULL)
static PGPError sCalculateShareHash(PGPShareRef share);
PGPError PGPCreateShares(PGPContextRef context, PGPKeyRef key,
PGPUInt32 threshold, PGPUInt32 numShares,
PGPShareRef *share)
{
PGPMemoryMgrRef memoryMgr = NULL;
PGPShareRef newShare = NULL;
PGPInt32 lockBits = 0;
PGPSize passkeySize = 0;
PGPSize shareSize = 0;
PGPSize totalSharesSize = 0;
PGPByte * passkey = NULL;
PGPError err = kPGPError_NoErr;
if (IsntNull(share))
*share = NULL;
PGPValidateParam(PGPContextRefIsValid(context));
PGPValidateParam(PGPKeyRefIsValid(key));
PGPValidateParam(threshold > 0);
PGPValidateParam(numShares > 0);
PGPValidateParam(threshold <= numShares);
PGPValidatePtr(share);
memoryMgr = PGPGetContextMemoryMgr(context);
pgpAssert(PGPMemoryMgrIsValid(memoryMgr));
if (!PGPMemoryMgrIsValid(memoryMgr))
return kPGPError_BadParams;
newShare = (PGPShareRef) PGPNewData(memoryMgr, sizeof(PGPShare),
kPGPMemoryMgrFlags_Clear);
pgpAssert(IsntNull(newShare));
if (IsNull(newShare))
return kPGPError_OutOfMemory;
newShare->context = context;
newShare->memoryMgr = memoryMgr;
newShare->threshold = threshold;
newShare->numShares = numShares;
newShare->totalShares = numShares;
newShare->shareDataSize = 0;
newShare->shareData = NULL;
newShare->shareHashSize = 0;
newShare->shareHash = NULL;
err = PGPGetKeyIDFromKey(key, &(newShare->keyID));
if (IsntPGPError(err))
err = PGPGetKeyNumber(key, kPGPKeyPropLockingBits, &lockBits);
if (IsntPGPError(err))
{
passkeySize = lockBits / 8;
passkey = (PGPByte *)PGPNewSecureData(memoryMgr, passkeySize,
kPGPMemoryMgrFlags_Clear);
}
if (IsNull(passkey))
err = kPGPError_OutOfMemory;
if (IsntPGPError(err))
err = PGPContextGetRandomBytes(context, passkey, passkeySize);
if (IsntPGPError(err))
{
shareSize = kPGPShareHeaderSize + passkeySize;
totalSharesSize = shareSize * numShares;
newShare->shareData = (PGPByte *)PGPNewSecureData(memoryMgr,
totalSharesSize,
kPGPMemoryMgrFlags_Clear);
}
if (IsNull(newShare->shareData))
err = kPGPError_OutOfMemory;
if (IsntPGPError(err))
{
newShare->shareDataSize = totalSharesSize;
err = PGPSecretShareData(context, passkey, passkeySize, threshold,
numShares, newShare->shareData);
}
if (IsntPGPError(err))
err = sCalculateShareHash(newShare);
if (IsntPGPError(err))
err = PGPContextGetRandomBytes(context, newShare->shareID.data,
sizeof(newShare->shareID.data));
if (IsntNull(passkey))
PGPFreeData(passkey);
if (IsPGPError(err))
{
PGPFreeShares(newShare);
newShare = NULL;
}
*share = newShare;
return err;
}
PGPError pgpCreateShares(PGPContextRef context, PGPKeyID keyID,
PGPShareID shareID, PGPUInt32 threshold, PGPUInt32 numShares,
PGPUInt32 totalShares, PGPSize shareSize,
const PGPByte *shareData, PGPShareRef *shares)
{
PGPMemoryMgrRef memoryMgr = NULL;
PGPShareRef newShare = NULL;
PGPError err = kPGPError_NoErr;
if (IsntNull(shares))
*shares = NULL;
PGPValidateParam(PGPContextRefIsValid(context));
PGPValidateParam(threshold > 0);
PGPValidateParam(numShares > 0);
PGPValidateParam(totalShares > 0);
PGPValidateParam(threshold <= totalShares);
PGPValidateParam(numShares <= totalShares);
PGPValidateParam(shareSize > 0);
PGPValidatePtr(shareData);
PGPValidatePtr(shares);
memoryMgr = PGPGetContextMemoryMgr(context);
pgpAssert(PGPMemoryMgrIsValid(memoryMgr));
if (!PGPMemoryMgrIsValid(memoryMgr))
return kPGPError_BadParams;
newShare = (PGPShareRef) PGPNewData(memoryMgr, sizeof(PGPShare),
kPGPMemoryMgrFlags_Clear);
pgpAssert(IsntNull(newShare));
if (IsNull(newShare))
return kPGPError_OutOfMemory;
newShare->context = context;
newShare->memoryMgr = memoryMgr;
newShare->threshold = threshold;
newShare->numShares = numShares;
newShare->totalShares = totalShares;
newShare->shareDataSize = shareSize;
newShare->shareData = NULL;
newShare->shareHashSize = 0;
newShare->shareHash = NULL;
newShare->shareData = (PGPByte *) PGPNewSecureData(memoryMgr, shareSize,
kPGPMemoryMgrFlags_Clear);
if (IsNull(newShare->shareData))
err = kPGPError_OutOfMemory;
if (IsntPGPError(err))
{
pgpCopyMemory(&keyID, &(newShare->keyID), sizeof(PGPKeyID));
pgpCopyMemory(&shareID, &(newShare->shareID), sizeof(PGPShareID));
pgpCopyMemory(shareData, newShare->shareData, shareSize);
}
if (IsntPGPError(err))
err = sCalculateShareHash(newShare);
if (IsPGPError(err))
{
PGPFreeShares(newShare);
newShare = NULL;
}
*shares = newShare;
return err;
}
/* The passkey needs to be freed with PGPFreeData(passkey) */
PGPError PGPGetPasskeyFromShares(PGPShareRef share, PGPByte **passkey,
PGPSize *passkeySize)
{
PGPSize shareSize = 0;
PGPSize newPasskeySize = 0;
PGPByte * newPasskey = NULL;
PGPError err = kPGPError_NoErr;
if (passkey != NULL)
*passkey = NULL;
if (passkeySize != NULL)
*passkeySize = 0;
PGPValidateParam(PGPShareRefIsValid(share));
PGPValidatePtr(passkey);
PGPValidatePtr(passkeySize);
shareSize = share->shareDataSize / share->numShares;
newPasskeySize = shareSize - kPGPShareHeaderSize;
newPasskey = (PGPByte *) PGPNewSecureData(share->memoryMgr, newPasskeySize,
kPGPMemoryMgrFlags_Clear);
if (IsNull(newPasskey))
err = kPGPError_OutOfMemory;
if (IsntPGPError(err))
err = PGPSecretReconstructData(share->context, share->shareData,
newPasskeySize, share->numShares, newPasskey);
if (IsPGPError(err))
{
PGPFreeData(newPasskey);
newPasskey = NULL;
newPasskeySize = 0;
}
*passkey = newPasskey;
*passkeySize = newPasskeySize;
return err;
}
PGPError PGPSplitShares(PGPShareRef share, PGPUInt32 numShares,
PGPShareRef *splitShares)
{
PGPShareRef newShare = NULL;
PGPUInt32 sharesLeft = 0;
PGPSize shareSize = 0;
PGPSize dataSize = 0;
PGPError err = kPGPError_NoErr;
if (IsntNull(share))
*splitShares = NULL;
PGPValidateParam(PGPShareRefIsValid(share));
PGPValidateParam(numShares > 0);
PGPValidatePtr(splitShares);
if (numShares > share->numShares)
return kPGPClientError_NotEnoughSharesInObject;
newShare = (PGPShareRef) PGPNewData(share->memoryMgr, sizeof(PGPShare),
kPGPMemoryMgrFlags_Clear);
pgpAssert(IsntNull(newShare));
if (IsNull(newShare))
return kPGPError_OutOfMemory;
newShare->context = share->context;
newShare->memoryMgr = share->memoryMgr;
newShare->threshold = share->threshold;
newShare->totalShares = share->totalShares;
pgpCopyMemory(&(share->keyID), &(newShare->keyID), sizeof(PGPKeyID));
pgpCopyMemory(&(share->shareID), &(newShare->shareID),
sizeof(PGPShareID));
shareSize = share->shareDataSize / share->numShares;
dataSize = numShares * shareSize;
sharesLeft = share->numShares - numShares;
newShare->shareData = (PGPByte *) PGPNewSecureData(share->memoryMgr,
dataSize,
kPGPMemoryMgrFlags_Clear);
if (IsNull(newShare->shareData))
err = kPGPError_OutOfMemory;
if (IsntPGPError(err))
{
pgpCopyMemory(&(share->shareData[sharesLeft * shareSize]),
newShare->shareData, dataSize);
newShare->numShares = numShares;
newShare->shareDataSize = dataSize;
share->numShares = sharesLeft;
share->shareDataSize = sharesLeft * shareSize;
if (share->shareDataSize > 0)
err = PGPReallocData(share->memoryMgr, &(share->shareData),
share->shareDataSize, 0);
else
{
PGPFreeData(share->shareData);
share->shareData = NULL;
}
}
PGPFreeData(share->shareHash);
if (IsntPGPError(err))
err = sCalculateShareHash(share);
if (IsntPGPError(err))
err = sCalculateShareHash(newShare);
if (IsPGPError(err))
{
PGPFreeShares(newShare);
newShare = NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -