How to do an ECC signature?

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #4127
    Chris Torr
    Keymaster
    August 7, 2020

    Some example code for generating an EC keypair and using it to sign some data using the ECDSA algorithm.

    
    #include <multos.h>
    
    #define HASH_LEN 32
    #define PRIME_LEN 32
    
    // Structures for keys
    typedef struct
    {
    	BYTE x[PRIME_LEN];
    	BYTE y[PRIME_LEN];
    } ecc_public_s;
    
    typedef struct
    {
    	ecc_public_s publicKey;
    	BYTE privateKey[PRIME_LEN];
    } ecc_s;
    
    typedef struct
    {
    	BYTE r[PRIME_LEN];
    	BYTE s[PRIME_LEN];
    } ecc_sig_s;
    
    #pragma melstatic
    BYTE domainParams[] = {		// SECP256K1
     0x00 , // Format
     0x20 , // Prime length
     0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
     0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F, //P
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //A
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07, //B
     0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,0x0B,0x07,
     0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,0x17,0x98, // Gx
     0x48,0x3A,0xDA,0x77,0x26,0xA3,0xC4,0x65,0x5D,0xA4,0xFB,0xFC,0x0E,0x11,0x08,0xA8,
     0xFD,0x17,0xB4,0x48,0xA6,0x85,0x54,0x19,0x9C,0x47,0xD0,0x8F,0xFB,0x10,0xD4,0xB8, // Gy
     0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
     0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41, // N
     0x01  // H
    };
    ecc_s eccKeyPair;
    
    #pragma melsession	
    ecc_sig_s signature;
    BYTE hash[HASH_LEN];
    	
    	// .. in code
    	// Generate key pair
    	genOK = multosEccGenerateKeyPair((BYTE*)&eccKeyPair,domainParams);
    
    	if(genOK)
    	{
    		// Hash the data to be signed
    		multosSecureHash((BYTE*)&data,hash,sizeof(hash),sizeof(data));
    
    		// Sign the hash
    		genOK = multosECDSA((BYTE*)&signature,hash+(HASH_LEN-PRIME_LEN),eccKeyPair.privateKey,domainParams);
    		if(genOK)
    		{
    			// Verify the signature
    			if(multosECDSAVerify(hash,(BYTE*)&signature,(BYTE*)&eccKeyPair.publicKey,domainParams))
    				multosExitSW(0x9000);	// OK
    			else
    				multosExitSW(0x9001);
    		}
    		else
    			multosExitSW(0x9002);
    	}
    	else
    		multosExitSW(0x9001);
    
    #4605
    mdeniz
    Participant
    March 14, 2023

    Is this code tested and ready to run?

    #4921
    Chris Torr
    Keymaster
    August 7, 2020

    These are just fragments of code that you can add to your own application.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.