| #include "ecc.c" |
| #include "syncrw.h" |
| #include <unistd.h> |
| #include <fcntl.h> |
| #include <stdio.h> |
| |
| static int write_key(int fd, const ECIES_privkey_t *privkey, const ECIES_pubkey_t *pubkey) { |
| int s1 = sync_write(fd, &privkey->k, ECIES_KEY_SIZE); |
| if(s1 < ECIES_KEY_SIZE) return s1; |
| int s2 = sync_write(fd, &pubkey->x, ECIES_KEY_SIZE); |
| if(s2 < ECIES_KEY_SIZE) { |
| if(s2 < 0) return -1; |
| return s1 + s2; |
| } |
| int s3 = sync_write(fd, &pubkey->y, ECIES_KEY_SIZE); |
| if(s3 < 0) return -1; |
| return s1 + s2 + s3; |
| } |
| |
| int main() { |
| int server_key_fd = open("serverkey", O_WRONLY | O_CREAT | O_EXCL, 0600); |
| if(server_key_fd == -1) { |
| perror("serverkey"); |
| return 1; |
| } |
| int client_key_fd = open("clientkey", O_WRONLY | O_CREAT | O_EXCL, 0600); |
| if(client_key_fd == -1) { |
| perror("clientkey"); |
| return 1; |
| } |
| |
| ECIES_privkey_t server_private_key; |
| ECIES_pubkey_t server_publib_key; |
| ECIES_privkey_t client_private_key; |
| ECIES_pubkey_t client_publib_key; |
| ECIES_generate_keys(&server_private_key, &client_publib_key); |
| ECIES_generate_keys(&client_private_key, &server_publib_key); |
| |
| int s = write_key(server_key_fd, &server_private_key, &server_publib_key); |
| if(s < ECIES_KEY_SIZE * 3) { |
| if(s < 0) perror("serverkey"); |
| else fprintf(stderr, "writting to file serverkey finished too early (%d < %d)\n", s, ECIES_KEY_SIZE * 3); |
| return 1; |
| } |
| s = write_key(client_key_fd, &client_private_key, &client_publib_key); |
| if(s < ECIES_KEY_SIZE * 3) { |
| if(s < 0) perror("clientkey"); |
| else fprintf(stderr, "writting to file clientkey finished too early (%d < %d)\n", s, ECIES_KEY_SIZE * 3); |
| return 1; |
| } |
| |
| if(close(server_key_fd) < 0) { |
| perror("serverkey"); |
| return 1; |
| } |
| if(close(client_key_fd) < 0) { |
| perror("clientkey"); |
| return 1; |
| } |
| |
| puts("serverkey and clientkey are saved into current directory"); |
| return 0; |
| } |