From 238a34b5c30fa703b2c610aec74a0fcd76c307ab Mon Sep 17 00:00:00 2001 From: toddm Date: Fri, 18 Jan 2019 17:15:49 +1300 Subject: [PATCH] Code tidy up --- source/LbBigInt.pas | 11 +++++++---- source/LbRSA.pas | 26 ++++++++------------------ 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/source/LbBigInt.pas b/source/LbBigInt.pas index bd7a7d1..679178c 100644 --- a/source/LbBigInt.pas +++ b/source/LbBigInt.pas @@ -72,7 +72,7 @@ TLbBigInt = class function GetBase64Str : string; procedure SetBase64Str(const Value: string); procedure SetHexStr(const Value: string); - function GetASN1Text: String; + function GetASNTriplet: String; protected {private} FI : LbInteger; procedure setSign(value : Boolean); @@ -148,7 +148,7 @@ TLbBigInt = class property IntStr : string read GetHexStr write SetHexStr; property Size : integer read GetSize; property Base64Str : string read GetBase64Str write SetBase64Str; - property ASN1Text : String read GetASN1Text; + property ASNTriplet : String read GetASNTriplet; end; @@ -2628,10 +2628,13 @@ procedure TLbBigInt.GCD(I2: TLbBigInt); LbGreatestCommonDivisor(self, I2); end; { ------------------------------------------------------------------- } -function TLbBigInt.GetASN1Text: String; +function TLbBigInt.GetASNTriplet: String; +const + INTEGER_TAG = '02'; var ReversedBigInt : TLbBigInt; begin + //the byte order in openSSL is reversed compared with lockbox ReversedBigInt := TlbBigInt.Create(Size); try ReversedBigInt.Copy(self); @@ -2642,7 +2645,7 @@ function TLbBigInt.GetASN1Text: String; end; ReversedBigInt.ReverseBytes(False); //don't trim the null byte - Result := ASN1HexSize(ReversedBigInt.Size) + ReversedBigInt.IntStr; + Result := INTEGER_TAG + ASN1HexSize(ReversedBigInt.Size) + ReversedBigInt.IntStr; finally ReversedBigInt.Free; end; diff --git a/source/LbRSA.pas b/source/LbRSA.pas index d9a6b50..5ca932d 100644 --- a/source/LbRSA.pas +++ b/source/LbRSA.pas @@ -1123,15 +1123,14 @@ function TLbRSA.GetCryptoServiceProviderXML(AIsForPrivateKey : Boolean): String; Text : String; ReversedBigInt, P1, Q1 : TLbBigInt; begin + //the byte order in MS cryptography is reversed compared with lockbox ReversedBigInt := TlbBigInt.Create(cLbAsymKeyBytes[FKeySize]); try ReversedBigInt.Copy(FPublicKey.Modulus); - ReversedBigInt.Trim; ReversedBigInt.ReverseBytes; Text := Format(XML_TAG, [RSA_MODULUS, ReversedBigInt.Base64Str]); ReversedBigInt.Copy(FPublicKey.Exponent); - ReversedBigInt.Trim; ReversedBigInt.ReverseBytes; Text := Text + Format(XML_TAG, [RSA_PUBLIC_EXPONENT, ReversedBigInt.Base64Str]); @@ -1143,12 +1142,10 @@ function TLbRSA.GetCryptoServiceProviderXML(AIsForPrivateKey : Boolean): String; end; ReversedBigInt.Copy(FFirstPrime); - ReversedBigInt.Trim; ReversedBigInt.ReverseBytes; Text := Text + Format(XML_TAG, [RSA_PRIME_ONE, ReversedBigInt.Base64Str]); ReversedBigInt.Copy(FSecondPrime); - ReversedBigInt.Trim; ReversedBigInt.ReverseBytes; Text := Text + Format(XML_TAG, [RSA_PRIME_TWO, ReversedBigInt.Base64Str]); @@ -1159,7 +1156,6 @@ function TLbRSA.GetCryptoServiceProviderXML(AIsForPrivateKey : Boolean): String; ReversedBigInt.Copy(FPrivateKey.Exponent); ReversedBigInt.Modulus(P1); - ReversedBigInt.Trim; ReversedBigInt.ReverseBytes; Text := Text + Format(XML_TAG, [RSA_D_MOD_PRIME_ONE, ReversedBigInt.Base64Str]); finally @@ -1173,7 +1169,6 @@ function TLbRSA.GetCryptoServiceProviderXML(AIsForPrivateKey : Boolean): String; ReversedBigInt.Copy(FPrivateKey.Exponent); ReversedBigInt.Modulus(Q1); - ReversedBigInt.Trim; ReversedBigInt.ReverseBytes; Text := Text + Format(XML_TAG, [RSA_D_MOD_PRIME_TWO, ReversedBigInt.Base64Str]); finally @@ -1182,12 +1177,10 @@ function TLbRSA.GetCryptoServiceProviderXML(AIsForPrivateKey : Boolean): String; ReversedBigInt.Copy(FSecondPrime); ReversedBigInt.ModInv(FFirstPrime); - ReversedBigInt.Trim; ReversedBigInt.ReverseBytes; Text := Text + Format(XML_TAG, [RSA_PRIME_TWO_INVERSE, ReversedBigInt.Base64Str]); ReversedBigInt.Copy(FPrivateKey.Exponent); - ReversedBigInt.Trim; ReversedBigInt.ReverseBytes; Text := Text + Format(XML_TAG, [RSA_PRIVATE_EXPONENT, ReversedBigInt.Base64Str]); end; @@ -1202,7 +1195,6 @@ function TLbRSA.GetOpenSSLText(AIsForPrivateKey: Boolean): String; const PRIVACY_TO_TEXT : array[Boolean] of string = ('PUBLIC','PRIVATE'); SEQUENCE_TAG = '30'; - INTEGER_TAG = '02'; BLOCK_FORMAT = '-----%s RSA %s KEY-----' + sLineBreak; MAX_CHARACTERS_PER_LINE = 64; var @@ -1210,19 +1202,17 @@ function TLbRSA.GetOpenSSLText(AIsForPrivateKey: Boolean): String; P1, Q1, DP, DQ, QInv, TempBigInt : TLbBigInt; Index, LastIndex : Integer; begin - Text := INTEGER_TAG + FPublicKey.Modulus.ASN1Text; - Text := Text + INTEGER_TAG + FPublicKey.Exponent.ASN1Text; + Text := FPublicKey.Modulus.ASNTriplet + FPublicKey.Exponent.ASNTriplet; if AIsForPrivateKey then begin - Text := Text + INTEGER_TAG + FPrivateKey.Exponent.ASN1Text; + Text := Text + FPrivateKey.Exponent.ASNTriplet; if not CalculatePQ then begin raise Exception.Create('Cannot calculate prime numbers'); end; - Text := Text + INTEGER_TAG + FFirstPrime.ASN1Text; - Text := Text + INTEGER_TAG + FSecondPrime.ASN1Text; + Text := Text + FFirstPrime.ASNTriplet + FSecondPrime.ASNTriplet; P1 := TLbBigInt.Create(FFirstPrime.Size); try @@ -1233,7 +1223,7 @@ function TLbRSA.GetOpenSSLText(AIsForPrivateKey: Boolean): String; try DP.Copy(FPrivateKey.Exponent); DP.Modulus(P1); - Text := Text + INTEGER_TAG + DP.ASN1Text; + Text := Text + DP.ASNTriplet; finally DP.Free; end; @@ -1250,7 +1240,7 @@ function TLbRSA.GetOpenSSLText(AIsForPrivateKey: Boolean): String; try DQ.Copy(FPrivateKey.Exponent); DQ.Modulus(Q1); - Text := Text + INTEGER_TAG + DQ.ASN1Text; + Text := Text + DQ.ASNTriplet; finally DQ.Free; end; @@ -1262,7 +1252,7 @@ function TLbRSA.GetOpenSSLText(AIsForPrivateKey: Boolean): String; try QInv.Copy(FSecondPrime); QInv.ModInv(FFirstPrime); - Text := Text + INTEGER_TAG + QInv.ASN1Text; + Text := Text + QInv.ASNTriplet; finally QInv.Free; end; @@ -1274,7 +1264,7 @@ function TLbRSA.GetOpenSSLText(AIsForPrivateKey: Boolean): String; try //prepend version 0 TempBigInt.AppendByte(0); - Text := INTEGER_TAG + TempBigInt.ASN1Text + Text; + Text := TempBigInt.ASNTriplet + Text; finally TempBigInt.Free; end;