Tool/software: Code Composer Studio
Dear Sir,
I have an question about the ECC private key and public key. here I have to sign some payload and I have to send to cloud with public key (for the signature) so cloud side they will verification with the public key.
here I am using the SL_NETUTIL_CRYPTO_CMD_SIGN_MSG method for the signature and verification from the device side . please have a look my attachment that is function implementation for the signature and verification in device side.
Here I have following question.
1. As I see in NWP guidelines I can sign the payload using the private key at index zero (as per the document it is unique key). so SL_NETUTIL_CRYPTO_CMD_SIGN_MSG method does sign the payload using private key only? (I haven't install any other key pair to device so I am referring the index 0 only )
2. After sign the payload (in device), I do verification (in device) using the SL_NETUTIL_CRYPTO_CMD_VERIFY_MSG method with index0, so this verification will be done internally by the device Stack so by what key there are verifying the signature result.?
// Getting Public key from the input index static int generate_public_key_from_index(uint32_t index, uint32_t configOpt, uint8_t *publicKey, uint16_t *keyLen) { int rc = -1; rc = sl_NetUtilGet(configOpt, index, publicKey, keyLen); if (rc < 0) { ASSERT_ON_ERROR(rc, "sl_NetUtilCmd() failed"); } return rc; } // Verify the signature using the given index static int verify_sign_buffer_using_index(uint32_t index, uint8_t* buff , uint16_t buffLen , uint8_t* signBuff , uint16_t signBuffLen) { int rc = -1; uint16_t resultLen = 0; if (1500 < (buffLen + signBuffLen)) { SHOW_ERROR(rc, "buffer length is too large"); return rc; } /* now verify the buffer */ memcpy(base64encode, buff, buffLen); memcpy(base64encode + buffLen, signBuff, signBuffLen); SlNetUtilCryptoCmdVerifyAttrib_t verAttrib; verAttrib.Flags = 0; verAttrib.ObjId = 0; verAttrib.SigType = SL_NETUTIL_CRYPTO_SIG_SHAwECDSA; /* this is the only type supported */ verAttrib.MsgLen = buffLen; verAttrib.SigLen = signBuffLen; resultLen = 4; int verifyResult = 0; rc = sl_NetUtilCmd(SL_NETUTIL_CRYPTO_CMD_VERIFY_MSG, (_u8 *) &verAttrib, sizeof(SlNetUtilCryptoCmdVerifyAttrib_t), (const _u8 *) base64encode, buffLen + signBuffLen, (_u8 *) &verifyResult, &resultLen); if (rc < 0) { ASSERT_ON_ERROR(rc, "sl_NetUtilCmd() failed"); } else { WMDebugInfo(DEBUG_LOG, "sing buffer verification result %d ", verifyResult); } return verifyResult; } // Signing the buffer using given index static int sing_buffer_using_index(uint32_t index, uint8_t* buff , uint16_t buffLen , uint8_t* signBuff , uint16_t *signBuffLen) { int rc = -1; if(buff == NULL) { SHOW_ERROR(rc, "buffer is null"); return rc; } if (buffLen >= 1500) { SHOW_ERROR(rc, "buffer length is too large"); return rc; } SlNetUtilCryptoCmdSignAttrib_t signAttrib; signAttrib.Flags = 0; signAttrib.ObjId = index; signAttrib.SigType = SL_NETUTIL_CRYPTO_SIG_SHAwECDSA; /* this is the only type supported */ rc = sl_NetUtilCmd(SL_NETUTIL_CRYPTO_CMD_SIGN_MSG, (uint8_t *) &signAttrib, sizeof(SlNetUtilCryptoCmdSignAttrib_t), buff, buffLen, signBuff, signBuffLen); if (rc < 0) { ASSERT_ON_ERROR(rc, "sl_NetUtilCmd() failed"); } return rc; } // Process for Creating Public key, signing message and verifying int decription_method(WM_https_register_get_ts *WM_https_register_get) { uint8_t buf[SL_NETUTIL_CMD_BUFFER_SIZE]; int i = 0; uint16_t configLen = 255; if(generate_public_key_from_index(0, SL_NETUTIL_CRYPTO_PUBLIC_KEY, (uint8_t*)buf, (uint16_t*)&configLen) == 0) { WMDebugInfo(NO_CRLF, "\r\n\r\n"); WMDebugInfo(NO_CRLF, "key result : "); for (i = 0; i < configLen; i++) { sprintf(&rawValueBuffer[i], "%02x", buf[i]); WMDebugInfo(NO_CRLF, "%02x", buf[i]); } WMDebugInfo(NO_CRLF, "\r\n\r\n"); } configLen = 255; MQTTPayload_ts signaturePayload; uint16_t len = 0; len = Make_payload_for_signature(&signaturePayload); if (len == 0) { ASSERT_ON_ERROR(-1, "JSON packed creation failed"); } WMDebugInfo(DEBUG_LOG, "signature payload %s", signaturePayload.jsonPayload); if(sing_buffer_using_index(0, (uint8_t*)signaturePayload.jsonPayload, signaturePayload.len, (uint8_t*)signatureBuffer, (uint16_t*)&configLen) == 0) { WMDebugInfo(NO_CRLF, "\r\n\r\n"); WMDebugInfo(NO_CRLF, "signature result : "); for (i = 0; i < configLen; i++) { sprintf(&rawValueBuffer[i], "%x", signatureBuffer[i]); WMDebugInfo(NO_CRLF, "%x", signatureBuffer[i]); } WMDebugInfo(NO_CRLF, "\r\n\r\n"); WMDebugInfo(NO_CRLF, "signature result : %s", rawValueBuffer); WMDebugInfo(NO_CRLF, "\r\n\r\n"); } if(verify_sign_buffer_using_index(0, (uint8_t*)signaturePayload.jsonPayload, signaturePayload.len, (uint8_t*)signatureBuffer, configLen) == 0) { WMDebugInfo(DEBUG_LOG, "sing buffer verification passed"); } else { WMDebugInfo(DEBUG_LOG, "sing buffer verification failed "); } return 0; }