Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages | Examples

test_prot_mem.c

Example of protected memory usage.
/*
 * Copyright (C) 2003 BLADOX, s.r.o.  All rights reserved.
 * 
 * This file is part of an example program for Turbo. This example
 * program may be used, distributed and modified without limitation.
 *
 */

#include <config.h>
#include <turbo/turbo.h>

#include <stdlib.h>
#include <string.h>

/* *INDENT-OFF* */

lc_char PROGMEM lc_Prot[]={
        LC_EN("Protected Memory")
        LC_END
};

lc_char PROGMEM lc_Encrypt[]={
        LC_EN("Encrypt")
        LC_END
};

lc_char PROGMEM lc_Decrypt[]={
        LC_EN("Decrypt")
        LC_END
};

lc_char PROGMEM lc_Id[]={
        LC_EN("Protected block id: ")
        LC_END
};

lc_char PROGMEM lc_Text[]={
        LC_EN("Block content: ")
        LC_END
};


/* *INDENT-ON* */

u8 PROGMEM err_no_prot_mem[] = "ERROR: NO PROT MEM";
u8 PROGMEM err_wrong_block[] = "ERROR: WRONG BLOCK";
u8 PROGMEM err_forbidden[] = "ERROR: FORBIDDEN";

u8 PROGMEM op_read[] = "READ allowed?";
u8 PROGMEM op_encrypt[] = "ENCRYPT allowed?";
u8 PROGMEM op_decrypt[] = "DECRYPT allowed?";

u8 PROGMEM decrypt_ok[] = "DECRYPT OK";
u8 PROGMEM decrypt_err[] = "DECRYPT ERROR";

u8 PROGMEM encrypt_ok[] = "ENCRYPT OK";
u8 PROGMEM encrypt_err[] = "ENCRYPT ERROR";

u8 display_error (u8 err)
{
  u8 ret;

// ERR_PROT_MEM_WRONG_BLOCK
// ERR_PROT_MEM_FORBIDDEN
// ERR_NO_PROT_MEM
  if (err == ERR_PROT_MEM_FORBIDDEN)
    ret = display_text (err_forbidden, NULL);
  else if (err == ERR_PROT_MEM_WRONG_BLOCK)
    ret = display_text (err_wrong_block, NULL);
  else if (err == ERR_NO_PROT_MEM)
    ret = display_text (err_no_prot_mem, NULL);
  else
    return perror (err);
  if (ret == APP_OK)
    ret = APP_BACK;
  return ret;
}

u8 display_buf (u8 * buf)
{
  u8 *r = buf_B ();
  u8 i;

  for (i = 0; i < 16; i++)
  {
    r = sprintch (r, buf[i]);
    r = sprintc (r, ' ');
  }
  r = sprintc (r, '\n');
  for (i = 0; i < 16; i++)
    r = sprintc (r, buf[i]);
  r = sprintc (r, '\0');
  if (display_text (buf_B (), NULL) == APP_END)
    return APP_END;
  return APP_BACK;
}

u8 prot_new (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    b8 nr;
    u8 op = 0;
    u8 ret;
    u8 *r, *res;

    ret = yesno (op_read, 0, 0);
    if (ret == APP_END)
      return APP_END;
    if (ret == APP_BACK)
      return APP_BACK;
    if (ret == APP_YES)
      op |= PROT_MEM_READ;

    ret = yesno (op_encrypt, 0, 0);
    if (ret == APP_END)
      return APP_END;
    if (ret == APP_BACK)
      return APP_BACK;
    if (ret == APP_YES)
      op |= PROT_MEM_ENCRYPT;

    ret = yesno (op_decrypt, 0, 0);
    if (ret == APP_END)
      return APP_END;
    if (ret == APP_BACK)
      return APP_BACK;
    if (ret == APP_YES)
      op |= PROT_MEM_DECRYPT;

    res = get_input (locale (lc_Text), 0, 16, NULL, Q_GET_INPUT_ALPHABET);
    if (res == ENULL)
      return APP_END;
    if (res == NULL)
      return APP_BACK;

    bzero (buf_B (), 16);
    res[res[0] + 1] = '\0';
    memcpy (buf_B (), &res[2], res[0]);

    nr = prot_mem_make_block (op, buf_B ());
    if (nr < 0)
      display_error (-nr);
    r = buf_B ();
    r = sprints (r, locale (lc_Id));
    r = sprinti (r, nr);
    r = sprintc (r, '\0');
    if (display_text (buf_B (), NULL) == APP_END)
      return APP_END;
    return APP_BACK;
  }
  return APP_OK;
}

u8 prot_delete (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    u8 *res;
    u8 nr;

    res = get_input (locale (lc_Id), 1, 3, NULL, Q_GET_INPUT_DIGITS);
    if (res == ENULL)
      return APP_END;
    if (res == NULL)
      return APP_BACK;

    res[res[0] + 1] = '\0';
    nr = atoi (&res[2]);

    nr = prot_mem_delete (nr);
    if (nr != NO_ERROR)
      return display_error (nr);
    return APP_BACK;
  }
  return APP_OK;
}

u8 prot_read (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    u8 *res;
    u8 nr;

    res = get_input (locale (lc_Id), 1, 3, NULL, Q_GET_INPUT_DIGITS);
    if (res == ENULL)
      return APP_END;
    if (res == NULL)
      return APP_BACK;
    res[res[0] + 1] = '\0';
    nr = atoi (&res[2]);

    nr = prot_mem_read (nr, buf_B () + 200);    //somewhere at the end
    if (nr != NO_ERROR)
      return display_error (nr);
    return display_buf (buf_B () + 200);
  }
  return APP_OK;
}

u8 prot_encrypt (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    u8 *res;
    u8 nr;
    u8 i;
    u8 *buf = buf_B ();
    u8 out[16];
    Twofish_key *key;

    res = get_input (locale (lc_Id), 1, 3, NULL, Q_GET_INPUT_DIGITS);
    if (res == ENULL)
      return APP_END;
    if (res == NULL)
      return APP_BACK;
    res[res[0] + 1] = '\0';
    nr = atoi (&res[2]);

    for (i = 0; i < 16; i++)
      out[i] = i;               // test vector
    nr = prot_mem_encrypt (nr, out, out);       // encrypt test vector
    if (nr != NO_ERROR)
      return display_error (nr);

    res = get_input (locale (lc_Text), 0, 16, NULL, Q_GET_INPUT_ALPHABET);
    if (res == ENULL)
      return APP_END;
    if (res == NULL)
      return APP_BACK;

    bzero (buf_B (), 16);
    res[res[0] + 1] = '\0';
    memcpy (buf_B (), &res[2], res[0]);

    key = malloc (sizeof (Twofish_key));
    twofish_setup (buf_B (), 16, key);
    twofish_ecb_decrypt (out, out, key);
    free (key);

    for (i = 0; i < 16; i++)
      if (out[i] != i)          // check text vector
      {
        if (display_text (encrypt_err, NULL) == APP_END)
          return APP_END;
        return APP_BACK;
      }

    if (display_text (encrypt_ok, NULL) == APP_END)
      return APP_END;
    return APP_BACK;
  }
  return APP_OK;
}

u8 prot_decrypt (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    u8 *res;
    u8 nr;
    u8 i;
    u8 *buf = buf_B ();
    u8 out[16];
    Twofish_key *key;

    res = get_input (locale (lc_Id), 1, 3, NULL, Q_GET_INPUT_DIGITS);
    if (res == ENULL)
      return APP_END;
    if (res == NULL)
      return APP_BACK;
    res[res[0] + 1] = '\0';
    nr = atoi (&res[2]);

    for (i = 0; i < 16; i++)
      out[i] = i;
    nr = prot_mem_decrypt (nr, out, out);
    if (nr != NO_ERROR)
      return display_error (nr);

    res = get_input (locale (lc_Text), 0, 16, NULL, Q_GET_INPUT_ALPHABET);
    if (res == ENULL)
      return APP_END;
    if (res == NULL)
      return APP_BACK;

    bzero (buf_B (), 16);
    res[res[0] + 1] = '\0';
    memcpy (buf_B (), &res[2], res[0]);

    key = malloc (sizeof (Twofish_key));
    twofish_setup (buf_B (), 16, key);
    twofish_ecb_encrypt (out, out, key);
    free (key);
    for (i = 0; i < 16; i++)
      if (out[i] != i)
      {
        if (display_text (decrypt_err, NULL) == APP_END)
          return APP_END;
        return APP_BACK;
      }

    if (display_text (decrypt_ok, NULL) == APP_END)
      return APP_END;
    return APP_BACK;
  }
  return APP_OK;
}

SNodeP prot_n = { lc_Prot, NULL };
SNodeP prot_n_new = { LC_NEW, prot_new };
SNodeP prot_n_delete = { LC_DELETE, prot_delete };
SNodeP prot_n_read = { LC_VIEW, prot_read };
SNodeP prot_n_encrypt = { lc_Encrypt, prot_encrypt };
SNodeP prot_n_decrypt = { lc_Decrypt, prot_decrypt };

SEdgeP prot_edges_p[] = {
  {&prot_n, &prot_n_new}
  ,
  {&prot_n, &prot_n_delete}
  ,
  {&prot_n, &prot_n_read}
  ,
  {&prot_n, &prot_n_encrypt}
  ,
  {&prot_n, &prot_n_decrypt}
  ,
  NULL
};

void action_menu (void *data)
{
  SCtx *c = spider_init ();

  c->n = &prot_n;
  c->eP = &prot_edges_p;
  spider (c);
}

void turbo_handler (u8 action, void *data)
{
  switch (action)
  {
    case ACTION_INSERT_MENU:
      insert_menu (locale (lc_Prot));
      break;
    case ACTION_MENU_SELECTION:
      stk_thread (action_menu, data);
      break;
    default:
      break;
  }
}


Copyright © 2004-2006 BLADOX
Turbo version 1.2