[SOLVED] CryptoLib4Pascal practical examples lazarus / 21-General ---------------------------------------------------------------------- torbente · 2021-02-08 09:07 Im developing a criptocurrency engine using freepascal/lazarus. I used OpenSSL to create key pairs using TProcess calls; same to sign and verify messages using secp256k1. Now i found this library: https://github.com/Xor-el/CryptoLib4Pascal Which seems to be able to do that, so i could eliminate dependency from OpenSSL, reduces the time of processing in nearly 98% and make it automatically cross-plattform. Even when the lybrary includes some examples, im unable to find the way to get a working functions for this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Keyspair= Packed Record     public: String[255];     private: String[255];     end;   VarMyKeysPair: Keyspair;MyMessage : string = 'Hello world';BeginKeyspair := GenerateKeys('secp256k1');End; Function GenerateKeys(algo:string):Keyspair;Begin// Stuff from CryptoLib4Pascal...result.public := ???;result.private := ???;End;Then, with the keys pair sign and verify the message: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i varSignedMessage: string;BeginSignedMessage := GetSignedMessage(Keyspair.private,MyMessage);if VerifySignature(MyMessage,SignedMessage,Keyspair.public) then writeln('Perfect');End; function GetSignedMessage(PrivKey,Message:String):String;Begin// Stuff from CryptoLib4Pascal...result := ???;End; function VerifySignature(Message,Signature,PubKey:string):boolean;BeginResult:= false;if {stuff from CryptoLib4Pascal} then result := true;End; We consider this as the most important improvement we could do rigth now, but as we are unable to understand the examples from github, we can not do it. Thanks. ---------------------------------------------------------------------- AlexTP · 2021-02-08 09:54 https://github.com/Xor-el/CryptoLib4Pascal/issues Maybe try to ask there. ---------------------------------------------------------------------- devEric69 · 2021-02-08 10:50 Basically, the public key (always the same) should be provided via a web API (a listening URL, with POST\GET\COOKIE possible "channels", dealing with variables being simple\json\SOAPed...) published by a public service \ server, and another published web API should give individually - to each client - their own private key (necessarily using an SSL layer). ---------------------------------------------------------------------- PierceNg · 2021-02-08 11:34 Quote from: torbente on February 08, 2021, 10:07:11 amIm developing a criptocurrency engine using freepascal/lazarus. I used OpenSSL to create key pairs using TProcess calls; same to sign and verify messages using secp256k1. Now i found this library: https://github.com/Xor-el/CryptoLib4Pascal I cloned the repo and its dependencies. The FPC console test program built and ran with all tests passed. Check out the test unit PascalCoinECIESTests which has tests involving secp256k1. Also UsageExamples.pas has PascalCoin-related tests that appear to use secp256k1. ---------------------------------------------------------------------- torbente · 2021-02-08 12:29 QuoteI cloned the repo and its dependencies. The FPC console test program built and ran with all tests passed. Check out the test unit PascalCoinECIESTests which has tests involving secp256k1. Also UsageExamples.pas has PascalCoin-related tests that appear to use secp256k1. Yes, the examples uses the same algo and hash we use in the engine via OpenSSL/TProcess, but we do not catch de whole process in this library. Quote from: devEric69 on February 08, 2021, 11:50:43 amBasically, the public key (always the same) should be provided via a web API (a listening URL, with POST\GET\COOKIE possible "channels", dealing with variables being simple\json\SOAPed...) published by a public service \ server, and another published web API should give individually - to each client - their own private key (necessarily using an SSL layer). Are you saying that the library do not generates the keys itself? If so, i believe you are wrong. ---------------------------------------------------------------------- Xor-el · 2021-02-08 15:51 Hi Torbente, attached below is a sample that generates a keypair using secp256k1 while signing and verifying with the SHA-1/ECDSA algorithm as specified in the GitHub issue you created. Can you please verify that it works as expected for you and close the issue on GitHub. Thanks. ---------------------------------------------------------------------- devEric69 · 2021-02-08 16:30 Quote from: torbente on February 08, 2021, 01:29:09 pmAre you saying that the library do not generates the keys itself? No. I thought you were wondering how to organise the roles (who could do what), in your final program. ---------------------------------------------------------------------- torbente · 2021-02-08 23:04 Quote from: Xor-el on February 08, 2021, 04:51:35 pmHi Torbente, attached below is a sample that generates a keypair using secp256k1 while signing and verifying with the SHA-1/ECDSA algorithm as specified in the GitHub issue you created. Can you please verify that it works as expected for you and close the issue on GitHub. Thanks. Hi. I just closed the issue on github. I downloaded and tested your program and it runs perfect. I could reply it on my project if i can figure out how to convert public and private keys to string to store it, and reverse the process (from that string to public/private key) To do this with  TBytes data, i can use StringOf and BytesOf. This is how public and private keys looks when created with OpenSSL Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Public:MFYw EAYHKoZIzj0CAQYFK4EEAAoDQgAEBzUVoHGnJtBeu7raipNv5J7C4jozij2ki3uP1CAZfR eQrdB1/xiAip8fZMga09tugEz2lPENWFkR048Axnvypg==Private:MHQCAQEEIMXz00xt Fwmvg2OtTYGz2i5iQsHLdr2aNajlr6c+xEOmoAcGBSuBBAAKoUQDQgAEBzUVoHGnJtBeu7 raipNv5J7C4jozij2ki3uP1CAZfReQrdB1/xiAip8fZMga09tugEz2lPENWFkR048Axnvy pg== If i remember correctly (i wrote that code +2 years ago) OpenSSL generates a Base64 string of the keys. This is the code i use to create the keypair actually: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure CreateKeysPair();var  MyProcess, MyProcess2 : TProcess;Begin//Generates the privateMyProcess:= TProcess.Create(nil);MyProcess.Executable := UserOptions.SSLPath;MyPro cess.Parameters.Add('ecparam');MyProcess.Parameters.Add('- name');MyProcess.Parameters.Add('secp256k1');MyProcess.Parameters.Add( '-genkey');MyProcess.Parameters.Add('- noout');MyProcess.Parameters.Add('- out');MyProcess.Parameters.Add('private.pem');MyProcess.Options := MyProcess.Options + [poWaitOnExit, poUsePipes, poNoConsole];MyProcess.Execute;// Extract public keyMyProcess2:= TProcess.Create(nil);MyProcess2.Executable := UserOptions.SSLPath;MyPr ocess2.Parameters.Add('ec');MyProcess2.Parameters.Add('- in');MyProcess2.Parameters.Add('private.pem');MyProcess2.Parameters.Ad d('-pubout');MyProcess2.Parameters.Add('- out');MyProcess2.Parameters.Add('public.pem');MyProcess2.Options := MyProcess2.Options + [poWaitOnExit, poUsePipes, poNoConsole];MyProcess2.Execute;MyProcess.Free;MyProcess2.Free;End; That code generates 2 txt files (private.pem and public.pem) from where i can extract the keys. If keys could be returned as a Base64 string then your code will be OpenSSL compatible. If keys can be stored as sometype of string (and revert the process) is enough for me. EDIT: using this in your code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Signature := TSignerUtils.SignMessage(MessageAsBytes, KeyPair.PrivateKey);SignatureSTR := StringOf( Signature );Signature := BytesOf(SignatureSTR); Makes the verifications fail so it is not working. I will try with TEnconding. I just check my code: OpenSSL generates a base64 string with the signature: Example Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i MEUCIQCbxTG JabHhVgAz7HvRFCeRYxNYERiEO5QKO643RwbhYAIgWxFoAQk0OpU+xKmu/aCMH5CkzM1bT IffRn3ruFuq4yg=Then i will only look how i can convert a TBytes to a base64 string with a reversible process ---------------------------------------------------------------------- torbente · 2021-02-09 00:42 I solved the TBytes <=> B64 string using unit base64 and this functions: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function ByteToString(const Value: TBytes): String;var  I: integer;  S : String;  Letra: char;beginS := '';for I := Length(Value)-1 Downto 0 do   begin   letra := Chr(Value[I]);   S := letra + S;   end;Result := S;end; function StrToByte(const Value: String): TBytes;var  I: integer;beginSetLength(Result, Length(Value));   for I := 0 to Length(Value) - 1 do      Result[I] := ord(Value[I + 1]);end; I added to your code Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SignatureB64 : String;SignatureB64 := EncodeStringBase64(ByteToString(Signature));Signature := StrToByte(DecodeStringBase64(SignatureB64)); The verification works always, and SignatureB64 is always 96 chars length (same as OpenSSL) so i guess it is compatible. Now i need convert keys to B64 strings to check the whole process, but i do not have idea how to extract the byte info from IECPublicKeyParameters (i can not even  found the type declaration) ---------------------------------------------------------------------- Xor-el · 2021-02-09 08:05 @torbente, attached below is a modified demo that saves the key as a Byte Arrays in the TKeyPair record. the public and private keys are saved using the standard raw DER format without the whole noise of the PEM intricacies that OpenSSL adds. I also attached a sample that prints the signature, public and private keys as Base64 strings. If you need to export the keys as PEM format, I suggest you read up the PEM specification that OpenSSL uses. ---------------------------------------------------------------------- torbente · 2021-02-09 08:21 Quote from: Xor-el on February 09, 2021, 09:05:47 am@torbente, attached below is a modified demo that saves the key as a Byte Arrays in the TKeyPair record. the public and private keys are saved using the standard raw DER format without the whole noise of the PEM intricacies that OpenSSL adds. I also attached a sample that prints the signature, public and private keys as Base64 strings. If you need to export the keys as PEM format, I suggest you read up the PEM specification that OpenSSL uses. We do not really need manage PEM format if we finally do not use OpenSSL. Your new code fits perfect for our project. I already implemented it for key generation, sign and verify messages and it works perfect. Please, see image. ---------------------------------------------------------------------- Xor-el · 2021-02-09 12:14 Quote from: torbente on February 09, 2021, 09:21:06 amQuote from: Xor- el on February 09, 2021, 09:05:47 am@torbente, attached below is a modified demo that saves the key as a Byte Arrays in the TKeyPair record. the public and private keys are saved using the standard raw DER format without the whole noise of the PEM intricacies that OpenSSL adds. I also attached a sample that prints the signature, public and private keys as Base64 strings. If you need to export the keys as PEM format, I suggest you read up the PEM specification that OpenSSL uses. We do not really need manage PEM format if we finally do not use OpenSSL. Your new code fits perfect for our project. I already implemented it for key generation, sign and verify messages and it works perfect. Please, see image. Glad it works Ok for you. Also thanks for the credits.