GithubHelp home page GithubHelp logo

jedisct1 / charm Goto Github PK

View Code? Open in Web Editor NEW
154.0 6.0 13.0 25 KB

A really tiny crypto library.

License: MIT License

C 75.64% Shell 24.36%
xoodoo cryptography crypto tiny charm permutation-based

charm's Issues

Minimal code example for us noobs?

I struggled to grok how state I was supposed to managed state between lib calls. I realized that state needs to be fully managed between two peers which seems obvious, but I guess for those just looking to play with the library and see how it works it's not. Anyway I have this short working code example that I would like to find a home for here if at all possible, thanks.

#include "charm.h"
#include <stdio.h>
#include <string.h>

#define TAG_LEN 6 // not sure why this is here but it's used here https://github.com/fengjijiao/dsvpn/blob/19c9d6612ea29cb9796484a9a62b0f31ca8d42f3/include/vpn.h#L46
#define SHARED_CONTEXT "my code example"
#define SHARED_KEY "0my0secret0key0ayy0lmaooo0:)0000"

void printBytes(unsigned char *bb, size_t len) {
  for (void *end = &bb[len]; bb != end; bb++)
    printf("\\x%02x", *bb);
}

int main(int argc, char *argv[]) {
  // Show message.
  unsigned char msg[6] = "hello";
  printf("msg\n");
  printBytes(msg, sizeof msg);
  printf("\n");

  unsigned char tag_full[16];

  // Encrypt.
  {
    uint32_t st[12];
    unsigned char key[32] = SHARED_KEY;
    uint8_t iv[16] = SHARED_CONTEXT;

    uc_state_init(st, key, iv);
    uc_encrypt(st, (unsigned char *)msg, sizeof msg, tag_full);

    printf("encrypted msg\n");
    printBytes(msg, sizeof msg);
    printf("\n");
  }

  // Decrypt.
  {
    uint32_t st[12];
    unsigned char key[32] = SHARED_KEY;
    uint8_t iv[16] = SHARED_CONTEXT;
    unsigned char tag[TAG_LEN];

    uc_state_init(st, key, iv);
    memcpy(tag, tag_full, TAG_LEN);
    if (uc_decrypt(st, (unsigned char *)msg, sizeof msg, tag, sizeof(tag)) != 0) {
      fprintf(stderr, "failed to decrypt\n");
      exit(1);
    }
    printf("decrypted msg\n");
    printBytes(msg, 6);
    printf("\n");
  }

  return 0;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.