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
Dec 3, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment