Created
November 23, 2017 14:02
-
-
Save jessicacarneiro/82f8baae245f4795b7617b7b14c28eda to your computer and use it in GitHub Desktop.
Example of use of RELIC's ECDSA.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <string.h> | |
| #define SIZE_MSG 5 | |
| extern "C" { | |
| #include "../include/relic.h" | |
| } | |
| int relic_setup() | |
| { | |
| if (core_init() != STS_OK) { | |
| core_clean(); | |
| return 1; | |
| } | |
| if(ec_param_set_any()!=STS_OK){ | |
| THROW(ERR_NO_CURVE); | |
| core_clean(); | |
| return 3; | |
| } | |
| return 0; | |
| } | |
| void gen_keys_ecdsa(bn_t private_key, ec_t public_key) | |
| { | |
| bn_null(private_key); | |
| ec_null(public_key); | |
| bn_new(private_key); | |
| ec_new(public_key); | |
| int r = cp_ecdsa_gen(private_key, public_key); | |
| if(r == STS_OK){ | |
| Serial.println("ecdsa gen ok"); | |
| } | |
| else { | |
| Serial.println("ecdsa gen NOT ok"); | |
| } | |
| } | |
| void sig_msg_ecdsa(bn_t private_key, bn_t first, bn_t second, uint8_t *msg) | |
| { | |
| bn_null(first); | |
| bn_null(second); | |
| bn_new(first); | |
| bn_new(second); | |
| int r = cp_ecdsa_sig(first, second, msg, sizeof(msg), 0, private_key); | |
| if(r == STS_OK){ | |
| Serial.println("ecdsa sig ok"); | |
| } | |
| else { | |
| Serial.println("ecdsa sig NOT ok"); | |
| } | |
| } | |
| void ver_sig_ecdsa(ec_t public_key, bn_t first, bn_t second, uint8_t *msg) | |
| { | |
| int r = cp_ecdsa_ver(first, second, msg, sizeof(msg), 0, public_key); | |
| if(r == 1){ | |
| Serial.println("ecdsa ver ok"); | |
| } | |
| else { | |
| Serial.println("ecdsa ver NOT ok"); | |
| } | |
| } | |
| void setup() | |
| { | |
| Serial.begin(115200); | |
| while (!Serial) { | |
| ; // wait for serial port to connect | |
| } | |
| relic_setup(); | |
| } | |
| void loop(){ | |
| // private key ecdsa | |
| bn_t private_key; | |
| // first component of the signature | |
| bn_t first; | |
| // second component of the signature | |
| bn_t second; | |
| // public key ecdsa | |
| ec_t public_key; | |
| // message to sign | |
| uint8_t msg[SIZE_MSG] = {0,1,2,3,4}; | |
| // generate the keys | |
| gen_keys_ecdsa(private_key, public_key); | |
| // sign the message | |
| sig_msg_ecdsa(private_key, first, second, msg); | |
| // verifies the message | |
| ver_sig_ecdsa(public_key, first, second, msg); | |
| // free the variables | |
| bn_free(private_key); | |
| bn_free(first); | |
| bn_free(second); | |
| ec_free(public_key); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment