Skip to content

Commit

Permalink
Merge pull request #28 from atsign-foundation/jeremy-repl
Browse files Browse the repository at this point in the history
feat: repl
  • Loading branch information
JeremyTubongbanua authored Aug 7, 2023
2 parents 0214902 + f07c9dc commit 3bb2c5d
Show file tree
Hide file tree
Showing 11 changed files with 501 additions and 274 deletions.
10 changes: 2 additions & 8 deletions include/at_chops/aes_ctr.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@ typedef enum {
AES_256 = 256,
} AESKeySize;

typedef struct {
int status; // status code of the operation
size_t reslen; // length of the result written
unsigned char *res; // result of the encryption/decryption
} AESResult;

/**
* @brief AES CTR encrypt plaintext
*
* @param key_base64 the base64 encoded AES 256 key (e.g. "1DPU9OP3CYvamnVBMwGgL7fm8yB1klAap0Uc5Z9R79g=")
* @param plaintext the plain text to encryt, must be null terminated `\0`
* @return AESResult* the result of the encryption
*/
AESResult *atchops_aes_ctr_encrypt(const char *key_base64, const AESKeySize key_size, const unsigned char *plaintext);
int atchops_aes_ctr_encrypt(const char *key_base64, const AESKeySize key_size, const unsigned char *plaintext, const size_t plaintextlen, size_t *ciphertextolen, unsigned char *ciphertext, const size_t ciphertextlen);

/**
* @brief AES CTR decrypt cipher text
Expand All @@ -38,7 +32,7 @@ AESResult *atchops_aes_ctr_encrypt(const char *key_base64, const AESKeySize key_
* @param ciphertext the base64 encoded cipher text, must be null terminated `\0`
* @return AESResult* the result of the decrytion
*/
AESResult *atchops_aes_ctr_decrypt(const char *key_base64, const AESKeySize key_size, const unsigned char *ciphertext);
int atchops_aes_ctr_decrypt(const char *key_base64, const AESKeySize key_size, const unsigned char *ciphertext, const size_t ciphertextlen, size_t *plaintextolen, unsigned char *plaintext, const size_t plaintextlen);

#ifdef __cplusplus
}
Expand Down
3 changes: 3 additions & 0 deletions include/at_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ extern "C"
#define ROOT_PORT 64

#include "at_chops.h"
#include "at_client/atkeys_filereader.h"
#include "at_client/connection.h"
#include "at_client/at_logger.h"

#ifdef __cplusplus
}
Expand Down
65 changes: 52 additions & 13 deletions include/at_client/atkeys_filereader.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,59 @@
#pragma once

#define TOKEN_AES_PKAM_PUBLIC_KEY "aesPkamPublicKey"
#define TOKEN_AES_PKAM_PUBLIC_KEY_LEN 16

#define TOKEN_AES_PKAM_PRIVATE_KEY "aesPkamPrivateKey"
#define TOKEN_AES_PKAM_PRIVATE_KEY_LEN 17

#define TOKEN_AES_ENCRYPT_PUBLIC_KEY "aesEncryptPublicKey"
#define TOKEN_AES_ENCRYPT_PUBLIC_KEY_LEN 19

#define TOKEN_AES_ENCRYPT_PRIVATE_KEY "aesEncryptPrivateKey"
#define TOKEN_AES_ENCRYPT_PRIVATE_KEY_LEN 20

#define TOKEN_SELF_ENCRYPTION_KEY "selfEncryptionKey"
#define TOKEN_SELF_ENCRYPTION_KEY_LEN 17

typedef struct atclient_atkeysfile_entry{
size_t len;
char* key;
char *key;
} atclient_atkeysfile_entry;

typedef struct atclient_atkeysfile{
atclient_atkeysfile_entry* aesPkamPublicKey;
atclient_atkeysfile_entry* aesPkamPrivateKey;
atclient_atkeysfile_entry* aesEncryptPublicKey;
atclient_atkeysfile_entry* aesEncryptPrivateKey;
atclient_atkeysfile_entry* selfEncryptionKey;
atclient_atkeysfile_entry* atSign;
typedef struct atclient_atkeysfile {
atclient_atkeysfile_entry *aes_pkam_public_key;
atclient_atkeysfile_entry *aes_pkam_private_key;
atclient_atkeysfile_entry *aes_encrypt_public_key;
atclient_atkeysfile_entry *aes_encrypt_private_key;
atclient_atkeysfile_entry *self_encryption_key;
} atclient_atkeysfile;

void atclient_atkeysfile_init(atclient_atkeysfile** atkeysfile);
char* save(char* token, atclient_atkeysfile_entry* attribute);
void updateFileLine(char** line, char* type, atclient_atkeysfile_entry *attribute, int comma);
int atclient_atkeysfile_read(const char* path, const size_t pathlen, atclient_atkeysfile* atsign);
int atclient_atkeysfile_write(const char *path, const size_t len, atclient_atkeysfile *atsign);
void atclient_atkeysfile_init(atclient_atkeysfile *atkeysfile);
int atclient_atkeysfile_read(const char *path, atclient_atkeysfile *atkeysfile);
int atclient_atkeysfile_write(const char *path, const char *atsign, atclient_atkeysfile *atkeysfile);
void atclient_atkeysfile_free(atclient_atkeysfile *atkeysfile);

/**
* Usage example
* atclient_atkeysfile atkeysfile;
* atclient_atkeysfile_init(&atkeysfile);
* printf("done init...\n")
*
ret = atclient_atkeysfile_read(path, &atkeysfile);
if (ret != 0)
{
goto exit;
}
printf("done read...\n");
printf("aes_pkam_public_key: %s\n", atkeysfile.aes_pkam_public_key->key);
printf("aes_pkam_private_key: %s\n", atkeysfile.aes_pkam_private_key->key);
printf("aes_encrypt_public_key: %s\n", atkeysfile.aes_encrypt_public_key->key);
printf("aes_encrypt_private_key: %s\n", atkeysfile.aes_encrypt_private_key->key);
printf("self_encryption_key: %s\n", atkeysfile.self_encryption_key->key);
printf("writing...\n");
ret = atclient_atkeysfile_write("/Users/jeremytubongbanua/.atsign/temp/@smoothalligator_key.atKeys", ATSIGN, &atkeysfile);
*
*/
4 changes: 2 additions & 2 deletions include/at_client/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ typedef struct atclient_connection_ctx {
void *saved_session;
} atclient_connection_ctx;

void atclient_connection_init(atclient_connection_ctx *ctx, const char *host, const int port);
int atclient_connection_connect(atclient_connection_ctx *ctx);
void atclient_connection_init(atclient_connection_ctx *ctx);
int atclient_connection_connect(atclient_connection_ctx *ctx, const char *host, const int port);
int atclient_connection_send(atclient_connection_ctx *ctx, unsigned char *recv, const size_t recvlen, size_t *olen, const unsigned char *src, const size_t srclen);
void atclient_connection_free(atclient_connection_ctx *ctx);
46 changes: 26 additions & 20 deletions src/at_chops/aes_ctr.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ extern "C"

#define MAX_BYTES_ALLOCATED_FOR_ENCRYPTION_OPERATION 5000

AESResult *atchops_aes_ctr_encrypt(const char *key_base64, const AESKeySize key_size, const unsigned char *plaintext )
int atchops_aes_ctr_encrypt(const char *key_base64, const AESKeySize key_size, const unsigned char *plaintext, const size_t plaintextlen, size_t *ciphertextolen, unsigned char *ciphertext, const size_t ciphertextlen)
{
AESResult *result = malloc(sizeof(AESResult));
int ret = 1;
size_t plaintext_len = strlen(plaintext);

// pad the plain text to be a multiple of 16 bytes
Expand Down Expand Up @@ -52,12 +52,12 @@ AESResult *atchops_aes_ctr_encrypt(const char *key_base64, const AESKeySize key_
size_t keylen = sizeof(key);
size_t *writtenlen = malloc(sizeof(size_t));

result->status = atchops_base64_decode(key, keylen, writtenlen, key_base64, strlen(key_base64));
ret = atchops_base64_decode(key, keylen, writtenlen, key_base64, strlen(key_base64));

// initialize AES context
mbedtls_aes_context *ctx = malloc(sizeof(mbedtls_aes_context));
mbedtls_aes_init(ctx);
result->status = mbedtls_aes_setkey_enc(ctx, key, key_size);
ret = mbedtls_aes_setkey_enc(ctx, key, key_size);

size_t *iv_ctr = malloc(sizeof(unsigned int));
unsigned char *iv = malloc(sizeof(unsigned char) * IV_AMOUNT_BYTES);
Expand All @@ -67,7 +67,7 @@ AESResult *atchops_aes_ctr_encrypt(const char *key_base64, const AESKeySize key_
// maybe base 64 encode it before feeding to cipher

// run encrypt
result->status = mbedtls_aes_crypt_ctr(ctx, plaintext_paddedlen, iv_ctr, iv, stream_block, plaintext_padded, aes_encrypted);
ret = mbedtls_aes_crypt_ctr(ctx, plaintext_paddedlen, iv_ctr, iv, stream_block, plaintext_padded, aes_encrypted);

// find how much of the encrypted data is actually used
int aes_encryptedlen = 0;
Expand All @@ -82,52 +82,55 @@ AESResult *atchops_aes_ctr_encrypt(const char *key_base64, const AESKeySize key_
// encode the encrypted data in base64
size_t dstlen = MAX_TEXT_LENGTH_FORBASE64_ENCODING_OPERATION;
unsigned char *dst = malloc(sizeof(unsigned char) * dstlen);
result->status = atchops_base64_encode(dst, dstlen, writtenlen, aes_encrypted, aes_encryptedlen);
ret = atchops_base64_encode(dst, dstlen, writtenlen, aes_encrypted, aes_encryptedlen);

// printf("%s\n", dst);

// done
result->res = dst;
result->reslen = *writtenlen;
unsigned char *p = ciphertext;
for (int i = 0; i < ciphertextlen; i++)
{
*p++ = *(dst + i);
}
*ciphertextolen = *writtenlen;

mbedtls_aes_free(ctx);
free(iv_ctr);
free(iv);
free(stream_block);
free(aes_encrypted);

return result;
return ret;
}

AESResult *atchops_aes_ctr_decrypt(const char *key_base64, const AESKeySize key_size, const unsigned char *ciphertext)
int atchops_aes_ctr_decrypt(const char *key_base64, const AESKeySize key_size, const unsigned char *ciphertext, const size_t ciphertextlen, size_t *plaintextolen, unsigned char *plaintext, const size_t plaintextlen)
{
AESResult *result = malloc(sizeof(AESResult));

int ret = 1;
// initialize AES key

unsigned char key[key_size/8];
size_t keylen = sizeof(key);

size_t *writtenlen = malloc(sizeof(size_t));
result->status = atchops_base64_decode(key, keylen, writtenlen, key_base64, strlen(key_base64));
ret = atchops_base64_decode(key, keylen, writtenlen, key_base64, strlen(key_base64));

// initialize AES context
mbedtls_aes_context *ctx = malloc(sizeof(mbedtls_aes_context));
mbedtls_aes_init(ctx);
result->status = mbedtls_aes_setkey_enc(ctx, key, key_size);
ret = mbedtls_aes_setkey_enc(ctx, key, key_size);

// decode the base64 ciphertext
size_t dstlen = MAX_TEXT_LENGTH_FORBASE64_ENCODING_OPERATION;
unsigned char *dst = malloc(sizeof(unsigned char) * dstlen);
result->status = atchops_base64_decode(dst, dstlen, writtenlen, ciphertext, strlen(ciphertext));
ret = atchops_base64_decode(dst, dstlen, writtenlen, ciphertext, strlen(ciphertext));

// run decrypt
size_t *iv_ctr = malloc(sizeof(unsigned int));
unsigned char *iv = malloc(sizeof(unsigned char) * IV_AMOUNT_BYTES);
unsigned char *stream_block = malloc(sizeof(unsigned char) * IV_AMOUNT_BYTES);
unsigned char *aes_decrypted = malloc(sizeof(unsigned char) * MAX_BYTES_ALLOCATED_FOR_ENCRYPTION_OPERATION);

result->status = mbedtls_aes_crypt_ctr(ctx, *writtenlen, iv_ctr, iv, stream_block, dst, aes_decrypted);
ret = mbedtls_aes_crypt_ctr(ctx, *writtenlen, iv_ctr, iv, stream_block, dst, aes_decrypted);

// find how much of the decrypted data is actually used
int aes_decryptedlen = 0;
Expand All @@ -150,7 +153,6 @@ AESResult *atchops_aes_ctr_decrypt(const char *key_base64, const AESKeySize key_
*(aes_decrypted+aes_decryptedlen) = '\0';
}

// printf("aa");
// printf("aa\n");
// for(int i = 0; i < aes_decryptedlen + 10; i++)
// {
Expand All @@ -165,16 +167,20 @@ AESResult *atchops_aes_ctr_decrypt(const char *key_base64, const AESKeySize key_
}

// done
result->res = aes_decrypted_unpadded;
result->reslen = aes_decryptedlen;
unsigned char *p = plaintext;
for (int i = 0; i < plaintextlen; i++)
{
*p++ = *(aes_decrypted_unpadded + i);
}
*plaintextolen = aes_decryptedlen;

mbedtls_aes_free(ctx);
free(iv_ctr);
free(iv);
free(stream_block);
free(aes_decrypted);

return result;
return ret;
}

#ifdef __cplusplus
Expand Down
1 change: 0 additions & 1 deletion src/at_chops/sha.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <stdlib.h>
#include <mbedtls/md.h>
#include <mbedtls/md5.h>
#include "at_chops/sha.h"

int atchops_sha_hash(const char *input, size_t inputlen, unsigned char **output, atchops_md_type mdtype)
Expand Down
Loading

0 comments on commit 3bb2c5d

Please sign in to comment.