mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-18 02:09:14 +02:00
* Rename types with ECC prefix
This commit is contained in:
parent
93f579ecd8
commit
34f0612932
@ -28,37 +28,51 @@ type
|
|||||||
Int32 = LongInt;
|
Int32 = LongInt;
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
UInt64 = QWord;
|
UInt64 = QWord;
|
||||||
|
|
||||||
TByteBuffer = array[0..MaxInt-1] of Byte;
|
TByteBuffer = array[0..MaxInt-1] of Byte;
|
||||||
PByteBuffer = ^TByteBuffer;
|
PByteBuffer = ^TByteBuffer;
|
||||||
TPublicKey = array[0..ECC_BYTES] of Byte;
|
|
||||||
TPrivateKey = array[0..ECC_BYTES-1] of Byte;
|
TEccPublicKey = array[0..ECC_BYTES] of Byte;
|
||||||
PPrivateKey = ^TPrivateKey;
|
TEccPrivateKey = array[0..ECC_BYTES-1] of Byte;
|
||||||
|
PEccPrivateKey = ^TEccPrivateKey;
|
||||||
TEccHash = array[0..ECC_BYTES-1] of Byte;
|
TEccHash = array[0..ECC_BYTES-1] of Byte;
|
||||||
PEccHash = ^TEccHash;
|
PEccHash = ^TEccHash;
|
||||||
TEccSecretKey = array[0..ECC_BYTES-1] of Byte;
|
TEccSecretKey = array[0..ECC_BYTES-1] of Byte;
|
||||||
TSignature = array[0..2*ECC_BYTES-1] of Byte;
|
TEccSignature = array[0..2*ECC_BYTES-1] of Byte;
|
||||||
|
|
||||||
TVLI = array[0..NUM_ECC_DIGITS-1] of UInt64;
|
TVLI = array[0..NUM_ECC_DIGITS-1] of UInt64;
|
||||||
PVLI = ^TVLI;
|
PVLI = ^TVLI;
|
||||||
TVLI2 = array[0..(2*NUM_ECC_DIGITS)-1] of UInt64;
|
TVLI2 = array[0..(2*NUM_ECC_DIGITS)-1] of UInt64;
|
||||||
|
|
||||||
TEccGetRandomNumber = function(out VLI: TVLI): Boolean;
|
TEccGetRandomNumber = function(out VLI: TVLI): Boolean;
|
||||||
|
|
||||||
|
TPublicKey = TECCPublicKey;
|
||||||
|
TPrivateKey = TECCPrivateKey;
|
||||||
|
TSignature = TEccSignature;
|
||||||
|
PPrivateKey = PEccPrivateKey;
|
||||||
|
|
||||||
|
|
||||||
// Util function to work with ASN.1
|
// Util function to work with ASN.1
|
||||||
procedure EccPublicKeyFromPrivateKey(out PublicKey: TPublicKey; const PrivateKey: TPrivateKey);
|
procedure EccPublicKeyFromPrivateKey(out PublicKey: TEccPublicKey; const PrivateKey: TEccPrivateKey);
|
||||||
function EccImportX963(Input: PByteBuffer; InputLen: Integer; out PublicKey: TPublicKey): Boolean;
|
function EccImportX963(Input: PByteBuffer; InputLen: Integer; out PublicKey: TEccPublicKey): Boolean;
|
||||||
function EccImportX963FromXY(InputX: PByteBuffer; InputXLen: Integer; InputY: PByteBuffer; InputYLen: Integer; out PublicKey: TPublicKey): Boolean;
|
function EccImportX963FromXY(InputX: PByteBuffer; InputXLen: Integer; InputY: PByteBuffer; InputYLen: Integer; out PublicKey: TEccPublicKey): Boolean;
|
||||||
function EccExportX963(const PublicKey: TPublicKey; Output: PByteBuffer; OutputLen: Integer): Boolean;
|
function EccExportX963(const PublicKey: TEccPublicKey; Output: PByteBuffer; OutputLen: Integer): Boolean;
|
||||||
|
procedure EccPublicKeyFromHexa(out PublicKey: TEccPublicKey; const Hexa: String); overload;
|
||||||
|
Function EccPublicKeyFromHexa(const Hexa: String) : TEccPublicKey; overload;
|
||||||
|
|
||||||
// Major functions
|
// Major functions
|
||||||
function EccMakeKey(out PublicKey: TPublicKey; out PrivateKey: TPrivateKey): Boolean;
|
function EccMakeKey(out PublicKey: TEccPublicKey; out PrivateKey: TEccPrivateKey): Boolean;
|
||||||
function EcdhSharedSecret(const PublicKey: TPublicKey; const PrivateKey: TPrivateKey; out Secret: TEccSecretKey): Boolean;
|
function EcdhSharedSecret(const PublicKey: TEccPublicKey; const PrivateKey: TEccPrivateKey; out Secret: TEccSecretKey): Boolean;
|
||||||
function EcdsaSign(const PrivateKey: TPrivateKey; const Hash: TEccHash; out Signature: TSignature): Boolean;
|
function EcdsaSign(const PrivateKey: TEccPrivateKey; const Hash: TEccHash; out Signature: TSignature): Boolean;
|
||||||
function EcdsaVerify(const PublicKey: TPublicKey; const Hash: TEccHash; const Signature: TSignature): Boolean;
|
function EcdsaVerify(const PublicKey: TEccPublicKey; const Hash: TEccHash; const Signature: TSignature): Boolean;
|
||||||
|
|
||||||
var
|
var
|
||||||
EccGetRandomNumber: TEccGetRandomNumber;
|
EccGetRandomNumber: TEccGetRandomNumber;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
|
uses sysutils, hashutils;
|
||||||
|
|
||||||
type
|
type
|
||||||
UInt128 = record
|
UInt128 = record
|
||||||
m_low: UInt64;
|
m_low: UInt64;
|
||||||
@ -259,7 +273,7 @@ begin
|
|||||||
Result := Borrow;
|
Result := Borrow;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure mul_64_64(const Left, Right: UInt64; var Result: UInt128);
|
procedure mul_64_64(const Left, Right: UInt64; out Result: UInt128);
|
||||||
var
|
var
|
||||||
a0, a1, b0, b1, m0, m1, m2, m3: UInt64;
|
a0, a1, b0, b1, m0, m1, m2, m3: UInt64;
|
||||||
begin
|
begin
|
||||||
@ -820,7 +834,7 @@ begin
|
|||||||
vli_sub(Point.y, Curve_P_32, Point.y);
|
vli_sub(Point.y, Curve_P_32, Point.y);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function EccImportX963(Input: PByteBuffer; InputLen: Integer; out PublicKey: TPublicKey): Boolean; //TLS_1_2
|
function EccImportX963(Input: PByteBuffer; InputLen: Integer; out PublicKey: TEccPublicKey): Boolean; //TLS_1_2
|
||||||
begin
|
begin
|
||||||
Result := False;
|
Result := False;
|
||||||
if Input = nil then
|
if Input = nil then
|
||||||
@ -835,7 +849,7 @@ begin
|
|||||||
Result := True;
|
Result := True;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function EccImportX963FromXY(InputX: PByteBuffer; InputXLen: Integer; InputY: PByteBuffer; InputYLen: Integer; out PublicKey: TPublicKey): Boolean;
|
function EccImportX963FromXY(InputX: PByteBuffer; InputXLen: Integer; InputY: PByteBuffer; InputYLen: Integer; out PublicKey: TEccPublicKey): Boolean;
|
||||||
var
|
var
|
||||||
Compressed: PByteBuffer;
|
Compressed: PByteBuffer;
|
||||||
I: Integer;
|
I: Integer;
|
||||||
@ -848,7 +862,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
// @Output[65] in case secp256r1
|
// @Output[65] in case secp256r1
|
||||||
function EccExportX963(const PublicKey: TPublicKey; Output: PByteBuffer; OutputLen: Integer): Boolean; //TLS_1_2
|
function EccExportX963(const PublicKey: TEccPublicKey; Output: PByteBuffer; OutputLen: Integer): Boolean; //TLS_1_2
|
||||||
var
|
var
|
||||||
PublicPoint: TEccPoint;
|
PublicPoint: TEccPoint;
|
||||||
begin
|
begin
|
||||||
@ -864,7 +878,7 @@ begin
|
|||||||
Result := True;
|
Result := True;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure EccPublicKeyFromPrivateKey(out PublicKey: TPublicKey; const PrivateKey: TPrivateKey);
|
procedure EccPublicKeyFromPrivateKey(out PublicKey: TEccPublicKey; const PrivateKey: TEccPrivateKey);
|
||||||
|
|
||||||
var
|
var
|
||||||
PrivateK: TVLI;
|
PrivateK: TVLI;
|
||||||
@ -877,7 +891,7 @@ begin
|
|||||||
PublicKey[0] := 2 + (PublicPoint.y[0] and $01);
|
PublicKey[0] := 2 + (PublicPoint.y[0] and $01);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function EccMakeKey(out PublicKey: TPublicKey; out PrivateKey: TPrivateKey): Boolean;
|
function EccMakeKey(out PublicKey: TEccPublicKey; out PrivateKey: TEccPrivateKey): Boolean;
|
||||||
|
|
||||||
var
|
var
|
||||||
PrivateK: TVLI;
|
PrivateK: TVLI;
|
||||||
@ -905,7 +919,7 @@ begin
|
|||||||
Result := True;
|
Result := True;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function EcdhSharedSecret(const PublicKey: TPublicKey; const PrivateKey: TPrivateKey; out Secret: TEccSecretKey): Boolean;
|
function EcdhSharedSecret(const PublicKey: TEccPublicKey; const PrivateKey: TEccPrivateKey; out Secret: TEccSecretKey): Boolean;
|
||||||
|
|
||||||
var
|
var
|
||||||
PublicPoint: TEccPoint;
|
PublicPoint: TEccPoint;
|
||||||
@ -999,7 +1013,7 @@ begin
|
|||||||
Result := B;
|
Result := B;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function EcdsaSign(const PrivateKey: TPrivateKey; const Hash: TEccHash; out Signature: TSignature): Boolean;
|
function EcdsaSign(const PrivateKey: TEccPrivateKey; const Hash: TEccHash; out Signature: TSignature): Boolean;
|
||||||
|
|
||||||
var
|
var
|
||||||
K, Temp, S: TVLI;
|
K, Temp, S: TVLI;
|
||||||
@ -1037,7 +1051,7 @@ end;
|
|||||||
type
|
type
|
||||||
TEccPoints4 = array[0..3] of PEccPoint;
|
TEccPoints4 = array[0..3] of PEccPoint;
|
||||||
|
|
||||||
function EcdsaVerify(const PublicKey: TPublicKey; const Hash: TEccHash; const Signature: TSignature): Boolean;
|
function EcdsaVerify(const PublicKey: TEccPublicKey; const Hash: TEccHash; const Signature: TSignature): Boolean;
|
||||||
|
|
||||||
var
|
var
|
||||||
I, Index, NumBits: Integer;
|
I, Index, NumBits: Integer;
|
||||||
@ -1130,6 +1144,45 @@ begin
|
|||||||
Result:=True;
|
Result:=True;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
Function EccPublicKeyFromHexa(const Hexa: String) : TEccPublicKey;
|
||||||
|
|
||||||
|
begin
|
||||||
|
EccPublicKeyFromHexa(Result,Hexa);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure EccPublicKeyFromHexa(out PublicKey: TEccPublicKey; const Hexa: String);
|
||||||
|
|
||||||
|
var
|
||||||
|
B: Byte;
|
||||||
|
HLength: Integer;
|
||||||
|
SHexa: String;
|
||||||
|
Buf : TBytes;
|
||||||
|
|
||||||
|
begin
|
||||||
|
hLength:=Length(Hexa);
|
||||||
|
if hLength= 66 then
|
||||||
|
begin
|
||||||
|
// _XString(S, @PublicKey, SizeOf(PublicKey));
|
||||||
|
Buf:=HexStrToBytes(Hexa);
|
||||||
|
if Length(Buf)<SizeOf(PublicKey) then
|
||||||
|
raise Exception.CreateFmt('EccPublicKeyFromHexa - Invalid length: %d', [Length(buf)]);
|
||||||
|
Move(Buf[0],PublicKey,Sizeof(PublicKey)) // Compressed public key
|
||||||
|
end else
|
||||||
|
begin // uncompressed public key as stored in PEM ASN.1
|
||||||
|
if hLength = 2+2*(2*ECC_BYTES) then
|
||||||
|
SHexa:=Copy(Hexa, 3, ECC_BYTES*2) // skip the leading $04 for uncompressed publickey
|
||||||
|
else if hLength <> 2*(2*ECC_BYTES) then
|
||||||
|
raise Exception.CreateFmt('EccPublicKeyFromHexa - Invalid length: %d', [hLength])
|
||||||
|
else
|
||||||
|
SHexa:=Hexa;
|
||||||
|
Buf:=HexStrToBytes(SHexa);
|
||||||
|
B:=StrToInt('$'+Copy(Hexa,hLength-1,2));
|
||||||
|
PublicKey[0] := 2 + (B and $01);
|
||||||
|
Move(Buf[0],PublicKey[1],SizeOf(TEccPublicKey)-1);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
initialization
|
initialization
|
||||||
EccGetRandomNumber:=@IntEccGetRandomNumber;
|
EccGetRandomNumber:=@IntEccGetRandomNumber;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user