Saturday, September 29, 2012

Unicast example in Contiki

Simple unicast example, Click button in each node to unicast message.

Please consider that this code is written for cooja simulator.

UniA.c for node 1
#include "contiki.h"
#include "net/rime.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#include <stdio.h>

PROCESS(example_unicast_process, "Example unicast");
AUTOSTART_PROCESSES(&example_unicast_process);

static void
recv_uc(struct unicast_conn *c, const rimeaddr_t *from)
{
  printf("broadcast message received from %d.%d: '%s'\n",from->u8[0], from->u8[1], (char *)packetbuf_dataptr());
}

static const struct unicast_callbacks unicast_callbacks = {recv_uc};

static struct unicast_conn uc;

static void unicast_message()
{
    unicast_open(&uc, 146, &unicast_callbacks);

    rimeaddr_t addr;
    
    packetbuf_copyfrom("AAAAA", 5);
    addr.u8[0] = 2;
    addr.u8[1] = 0;
    if(!rimeaddr_cmp(&addr, &rimeaddr_node_addr))
    {
      unicast_send(&uc, &addr);
    }
}

PROCESS_THREAD(example_unicast_process, ev, data)
{
  PROCESS_EXITHANDLER(unicast_close(&uc);)
    
  PROCESS_BEGIN();
  
  while(1) {
    PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor);
    unicast_message();
    printf("message sent.\n");
  }  
    
  PROCESS_END();
}

UniB.c for node2
#include "contiki.h"
#include "net/rime.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#include <stdio.h>

PROCESS(example_unicast_process, "Example unicast");
AUTOSTART_PROCESSES(&example_unicast_process);

static void
recv_uc(struct unicast_conn *c, const rimeaddr_t *from)
{
  printf("broadcast message received from %d.%d: '%s'\n",from->u8[0], from->u8[1], (char *)packetbuf_dataptr());
}

static const struct unicast_callbacks unicast_callbacks = {recv_uc};

static struct unicast_conn uc;

static void unicast_message()
{
    unicast_open(&uc, 146, &unicast_callbacks);

    rimeaddr_t addr;
    
    packetbuf_copyfrom("BBBBB", 5);
    addr.u8[0] = 1;
    addr.u8[1] = 0;
    if(!rimeaddr_cmp(&addr, &rimeaddr_node_addr))
    {
      unicast_send(&uc, &addr);
    }
}

PROCESS_THREAD(example_unicast_process, ev, data)
{
  PROCESS_EXITHANDLER(unicast_close(&uc);)
    
  PROCESS_BEGIN();
  
  while(1) {
    PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor);
    unicast_message();
    printf("message sent.\n");
  }  
    
  PROCESS_END();
}

Friday, September 14, 2012

Prime Fields Example

In cryptography, prime fields play major role in its mathematical problems. Below you can see a simple example of a prime field 29 which denoted by F29.

The elements of F29 are {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27 28}

For any integer a, a mod p shall denote the unique integer remainder r , 0 ≤ r ≤ p − 1, obtained upon dividing a by p; this operation is called reduction modulo p.

(i) Addition: 17 + 20 = 8 since 37 mod 29 = 8

(ii) Subtraction: 17 − 20 = 26 since −3 mod 29 = 26

(iii) Multiplication: 17 · 20 = 21 since 340 mod 29 = 21

(iv) Inversion: 17−1 = 12 since 17 · 12 mod 29 = 1

Monday, September 10, 2012

RSA Encryption Scheme

RSA, named after its inventors Rivest, Shamir and Adleman, was proposed in 1977 shortly after the discovery of public-key cryptography. 


RSA key pair generation

INPUT: Security parameter l.
OUTPUT: RSA public key (n, e) and private key d.
   1. Randomly select two primes p and q of the same bitlength l/2.
   2. Compute n = pq and φ = ( p − 1)(q − 1).
   3. Select an arbitrary integer e with 1 < e < φ and gcd(e, φ) = 1.
   4. Compute the integer d satisfying 1 < d < φ and ed ≡ 1 (mod φ).
   5. Return(n, e, d)

Basic RSA encryption

INPUT: RSA public key (n, e), plaintext m ∈ [0, n − 1].
OUTPUT: Ciphertext c.
   1. Compute c = me mod n.
   2. Return(c)

Basic RSA decryption

INPUT: RSA public key (n, e), RSA private key d, ciphertext c.
OUTPUT: Plaintext m.
   1. Compute m = cd mod n.
   2. Return(m)


Implementations in Java
Implementations in .NET

Sunday, September 9, 2012

RSA Digital Signature Scheme

RSA, named after its inventors Rivest, Shamir and Adleman, was proposed in 1977 shortly after the discovery of public-key cryptography. 

RSA key pair generation

INPUT: Security parameter l.
OUTPUT: RSA public key (n, e) and private key d.
   1. Randomly select two primes p and q of the same bitlength l/2.
   2. Compute n = pq and φ = ( p − 1)(q − 1).
   3. Select an arbitrary integer e with 1 < e < φ and gcd(e, φ) = 1.
   4. Compute the integer d satisfying 1 < d < φ and ed ≡ 1 (mod φ).
   5. Return(n, e, d).

Basic RSA signature generation

INPUT: RSA public key (n, e), RSA private key d, message m.
OUTPUT: Signature s.
   1. Compute h = H (m) where H is a hash function.
   2. Compute s = hd mod n.
   3. Return(s).

Basic RSA signature verification

INPUT: RSA public key (n, e), message m, signature s.
OUTPUT: Acceptance or rejection of the signature.
   1. Compute h = H (m).
   2. Compute h` = se mod n.
   3. If h = h` then return(“Accept the signature”);
       Else return(“Reject the signature”).

Implementations in Java
Implementations in .NET