Last active
December 3, 2019 18:07
-
-
Save btoews/942ec3f175448d68fed25018adbce5a7 to your computer and use it in GitHub Desktop.
Libsodium sealed box with non-random ephemeral key
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 <stdio.h> | |
#include <sodium.h> | |
const char *rand_five_implementation_name(void) { | |
return "five"; | |
} | |
uint32_t rand_five_random(void) { | |
return 5; | |
} | |
void rand_five_buf(void * const buf, const size_t size) { | |
for (int i = 0; i < size; i++) { | |
((unsigned char *)buf)[i] = 5; | |
} | |
} | |
struct randombytes_implementation rand_five = { | |
rand_five_implementation_name, | |
rand_five_random, | |
NULL, | |
NULL, | |
rand_five_buf | |
}; | |
int main(int argc, char **argv) { | |
if (sodium_init() < 0) { | |
return 1; | |
} | |
randombytes_set_implementation(&rand_five); | |
unsigned char privateKey[crypto_box_SECRETKEYBYTES]; | |
for (int i = 0; i < crypto_box_SECRETKEYBYTES; i++) { | |
privateKey[i] = 1; | |
} | |
unsigned char publicKey[crypto_box_PUBLICKEYBYTES]; | |
if (crypto_scalarmult_base(publicKey, privateKey) != 0) { | |
printf("Failed to calculate publicKey\n"); | |
return 1; | |
} | |
unsigned char message[64]; | |
for (int i = 0; i < 64; i++) { | |
message[i] = 3; | |
} | |
unsigned char ciphertext[crypto_box_SEALBYTES + 64]; | |
if (crypto_box_seal(ciphertext, message, 64, publicKey) != 0) { | |
printf("Failed calling crypto_box_seal\n"); | |
return 1; | |
} | |
for (int i = 0; i < crypto_box_SEALBYTES + 64; i++) { | |
printf("%02x", ciphertext[i]); | |
} | |
printf("\n"); | |
return 0; | |
} |
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 <stdio.h> | |
#include <sodium.h> | |
int main(int argc, char **argv) { | |
if (sodium_init() < 0) { | |
return 1; | |
} | |
unsigned char privateKey[crypto_box_SECRETKEYBYTES]; | |
for (int i = 0; i < crypto_box_SECRETKEYBYTES; i++) { | |
privateKey[i] = 1; | |
} | |
unsigned char publicKey[crypto_box_PUBLICKEYBYTES]; | |
if (crypto_scalarmult_base(publicKey, privateKey) != 0) { | |
printf("Failed to calculate publicKey\n"); | |
return 1; | |
} | |
unsigned char message[64]; | |
for (int i = 0; i < 64; i++) { | |
message[i] = 3; | |
} | |
unsigned char ciphertext[crypto_box_SEALBYTES + 64]; | |
if (crypto_box_seal(ciphertext, message, 64, publicKey) != 0) { | |
printf("Failed calling crypto_box_seal\n"); | |
return 1; | |
} | |
for (int i = 0; i < crypto_box_SEALBYTES + 64; i++) { | |
printf("%02x", ciphertext[i]); | |
} | |
printf("\n"); | |
return 0; | |
} |
Author
btoews
commented
Nov 4, 2019
$ gcc -lsodium -o seal ./seal.c
$ ./seal
3462e0640728247a6f581e3812850d6edc3dcad1ea5d8184c072f62fb65cb357e27ffa8b76f41656bc66a0882c4d359568410665746d27462a700f01e314f382edd7aae9064879b0f8ba7b88866f88f5e4fbd7649c850541877f9f33ebd25d46d9cbcce09b69a9ba07f0eb1d105d4264
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment