Skip to content

Instantly share code, notes, and snippets.

@btoews
Last active December 3, 2019 18:07
Show Gist options
  • Save btoews/942ec3f175448d68fed25018adbce5a7 to your computer and use it in GitHub Desktop.
Save btoews/942ec3f175448d68fed25018adbce5a7 to your computer and use it in GitHub Desktop.
Libsodium sealed box with non-random ephemeral key
#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;
}
#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;
}
@btoews
Copy link
Author

btoews commented Nov 4, 2019

$ gcc -lsodium -o rand_five ./rand_five.c
$ ./rand_five
50a61409b1ddd0325e9b16b700e719e9772c07000b1bd7786e907c653d20495d2af1697137a53b1b1dfc9befc49b6eeb38f86be720e155eb2be61976d2efb34d67ecd44a6ad634625eb9c288bfc883431a84ab0f5557dfe673aa6f74c19f033e648a947358cfcc606397fa1747d5219a

@btoews
Copy link
Author

btoews commented Dec 3, 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