Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Split a secret into five shares and recover it with any three

Give five custodians one share each of a 32-byte recovery key. Any three can reconstruct the key, so two custodians can be unavailable at recovery time. Keep shares from different runs separate.

Choose the key size and field

uses
  System.SysUtils, StreamSec.DSI.ArithTypes, StreamSec.DSI.Arith,
  StreamSec.DSI.PKTypes, StreamSec.DSI.FFC, StreamSec.DSI.FFC.MODPGroups,
  StreamSec.DSI.SecretSharing;

const
  cShareCount = 5;
  cSharesNeeded = 3;

type
  tRecoveryKey = array[0..31] of Byte;
  tKeyShare = record
    Id: Byte;
    Value: array[0..255] of Byte;
  end;
  tFiveShares = array[0..cShareCount - 1] of tKeyShare;
function ShareModulus: iDSInteger;
var
  lParams: iFFSystemParams;
begin
  lParams := tFFSystemParams.Create(GetDSIFF);
  ModPGroup2048(lParams);
  Result := lParams.P;
end;

Shamir sharing treats the key as an unsigned integer in a prime field. The 2048-bit prime returned by ModPGroup2048 is larger than every 32-byte key. Each share has a one-byte identifier and a 256-byte field value in big-endian order. Use the same prime during recovery.

Split the key

function SplitKey(const aKey: tRecoveryKey): tFiveShares;
var
  lSecret: iDSInteger;
  lSharing: iDSSecretSharing;
  I: Integer;
begin
  if not GetDSIPool.UFromBase256(aKey[0], SizeOf(aKey), SizeOf(aKey) * 8, lSecret) then
    raise Exception.Create('Could not import the recovery key');
  lSharing := tDSSecretSharing.Create;
  if not lSharing.CreateSharing(cShareCount, cSharesNeeded - 1, lSecret, ShareModulus) then
    raise Exception.Create('Could not create the shares');
  for I := Low(Result) to High(Result) do begin
    Result[I].Id := lSharing.Items[I].Id;
    lSharing.Items[I].Data.AsIntegerConv.UToBase256(Result[I].Value[0], SizeOf(Result[I].Value));
  end;
end;

Pass the key to SplitKey. For a new key, see Generate random bytes and keys. CreateSharing takes the polynomial degree. For recovery with three shares, pass degree two. The library chooses the random coefficients and evaluates the polynomial at identifiers 1 through 5. Give each custodian their own identifier and value together over a confidential, authenticated channel. The identifier stays with that share during storage and recovery.

Recover with three custodians

function RecoverKey(const aShares: array of tKeyShare; out aKey: tRecoveryKey): Boolean;
var
  lSharing: iDSSecretSharing;
  lItem: iDSIntegerCollectionItem;
  lValue, lSecret: iDSInteger;
  I: Integer;
begin
  FillChar(aKey, SizeOf(aKey), 0);
  if Length(aShares) <> cSharesNeeded then
    Exit(False);
  lSharing := tDSSecretSharing.Create;
  lSharing.SetFieldModulus(ShareModulus);
  lSharing.Threshold := cSharesNeeded - 1;
  for I := Low(aShares) to High(aShares) do begin
    if (aShares[I].Id < 1) or (aShares[I].Id > cShareCount) or
       (lSharing.FindId(aShares[I].Id) >= 0) then
      Exit(False);
    if not GetDSIPool.UFromBase256(aShares[I].Value[0], SizeOf(aShares[I].Value),
      SizeOf(aShares[I].Value) * 8, lValue) then
      Exit(False);
    if lValue.CmpOffset(lSharing.FieldModulus) >= 0 then
      Exit(False);
    lItem := lSharing.Add;
    lItem.Id := aShares[I].Id;
    lItem.Data := lValue;
  end;
  if not lSharing.RecreateSecret(lSecret) then
    Exit(False);
  if lSecret.MSB > SizeOf(aKey) * 8 then
    Exit(False);
  lSecret.AsIntegerConv.UToBase256(aKey[0], SizeOf(aKey));
  Result := True;
end;

Pass exactly three shares in any order to RecoverKey. For example, with the array returned by SplitKey, use RecoverKey([Shares[4], Shares[0], Shares[2]], Key) to combine shares 5, 1 and 3. Use the 32-byte output only when the function returns True. The fixed-width conversion preserves any leading zero bytes in the key.

Use authenticated shares from the same split. Shamir interpolation computes a value from the points supplied. The identifier, field and length checks in this example cannot establish that a custodian supplied the original value.

Reference

StreamSec.DSI.SecretSharing · StreamSec.DSI.FFC.MODPGroups · StreamSec.DSI.ArithTypes

Related tasks