コード例 #1
0
ファイル: cc2538-ccm-star.c プロジェクト: 13416795/contiki
/*---------------------------------------------------------------------------*/
static void
aead(const uint8_t *nonce, uint8_t *m, uint8_t m_len, const uint8_t *a,
     uint8_t a_len, uint8_t *result, uint8_t mic_len, int forward)
{
  uint16_t cdata_len;
  uint8_t crypto_enabled, ret;

  crypto_enabled = enable_crypto();

  if(forward) {
    ret = ccm_auth_encrypt_start(CCM_STAR_LEN_LEN, CC2538_AES_128_KEY_AREA,
                                 nonce, a, a_len, m, m_len, m, mic_len, NULL);
    if(ret != CRYPTO_SUCCESS) {
      PRINTF("%s: ccm_auth_encrypt_start() error %u\n", MODULE_NAME, ret);
      restore_crypto(crypto_enabled);
      return;
    }

    while(!ccm_auth_encrypt_check_status());
    ret = ccm_auth_encrypt_get_result(result, mic_len);
    if(ret != CRYPTO_SUCCESS) {
      PRINTF("%s: ccm_auth_encrypt_get_result() error %u\n", MODULE_NAME, ret);
    }
  } else {
    cdata_len = m_len + mic_len;
    ret = ccm_auth_decrypt_start(CCM_STAR_LEN_LEN, CC2538_AES_128_KEY_AREA,
                                 nonce, a, a_len, m, cdata_len, m, mic_len,
                                 NULL);
    if(ret != CRYPTO_SUCCESS) {
      PRINTF("%s: ccm_auth_decrypt_start() error %u\n", MODULE_NAME, ret);
      restore_crypto(crypto_enabled);
      return;
    }

    while(!ccm_auth_decrypt_check_status());
    ret = ccm_auth_decrypt_get_result(m, cdata_len, result, mic_len);
    if(ret != CRYPTO_SUCCESS) {
      PRINTF("%s: ccm_auth_decrypt_get_result() error %u\n", MODULE_NAME, ret);
    }
  }

  restore_crypto(crypto_enabled);
}
コード例 #2
0
ファイル: ccm-test.c プロジェクト: 13416795/contiki
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(ccm_test_process, ev, data)
{
  static const char *const str_res[] = {
    "success",
    "invalid param",
    "NULL error",
    "resource in use",
    "DMA bus error",
    "keystore read error",
    "keystore write error",
    "authentication failed"
  };
  static const uint8_t keys128[][128 / 8] = {
    { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
    { 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
      0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf }
  };
  static const uint8_t keys192[][192 / 8] = {
    { 0x26, 0x51, 0x1f, 0xb5, 0x1f, 0xcf, 0xa7, 0x5c,
      0xb4, 0xb4, 0x4d, 0xa7, 0x5a, 0x6e, 0x5a, 0x0e,
      0xb8, 0xd9, 0xc8, 0xf3, 0xb9, 0x06, 0xf8, 0x86 },
    { 0x08, 0x6e, 0x29, 0x67, 0xcd, 0xe9, 0x9e, 0x90,
      0xfa, 0xae, 0xa8, 0xa9, 0x4e, 0x16, 0x8b, 0xf0,
      0xe0, 0x66, 0xc5, 0x03, 0xa8, 0x49, 0xa9, 0xf3 },
    { 0xce, 0xb0, 0x09, 0xae, 0xa4, 0x45, 0x44, 0x51,
      0xfe, 0xad, 0xf0, 0xe6, 0xb3, 0x6f, 0x45, 0x55,
      0x5d, 0xd0, 0x47, 0x23, 0xba, 0xa4, 0x48, 0xe8 },
    { 0x11, 0xfd, 0x45, 0x74, 0x3d, 0x94, 0x6e, 0x6d,
      0x37, 0x34, 0x1f, 0xec, 0x49, 0x94, 0x7e, 0x8c,
      0x70, 0x48, 0x24, 0x94, 0xa8, 0xf0, 0x7f, 0xcc }
  };
  static const uint8_t keys256[][256 / 8] = {
    { 0x26, 0x51, 0x1f, 0xb5, 0x1f, 0xcf, 0xa7, 0x5c,
      0xb4, 0xb4, 0x4d, 0xa7, 0x5a, 0x6e, 0x5a, 0x0e,
      0xb8, 0xd9, 0xc8, 0xf3, 0xb9, 0x06, 0xf8, 0x86,
      0xdf, 0x3b, 0xa3, 0xe6, 0xda, 0x3a, 0x13, 0x89 },
    { 0xc6, 0xc1, 0x4c, 0x65, 0x5e, 0x52, 0xc8, 0xa4,
      0xc7, 0xe8, 0xd5, 0x4e, 0x97, 0x4d, 0x69, 0x8e,
      0x1f, 0x21, 0xee, 0x3b, 0xa7, 0x17, 0xa0, 0xad,
      0xfa, 0x61, 0x36, 0xd0, 0x26, 0x68, 0xc4, 0x76 },
    { 0x55, 0x35, 0x21, 0xa7, 0x65, 0xab, 0x0c, 0x3f,
      0xd2, 0x03, 0x65, 0x4e, 0x99, 0x16, 0x33, 0x0e,
      0x18, 0x9b, 0xdf, 0x95, 0x1f, 0xee, 0xe9, 0xb4,
      0x4b, 0x10, 0xda, 0x20, 0x8f, 0xee, 0x7a, 0xcf },
    { 0x90, 0x74, 0xb1, 0xae, 0x4c, 0xa3, 0x34, 0x2f,
      0xe5, 0xbf, 0x6f, 0x14, 0xbc, 0xf2, 0xf2, 0x79,
      0x04, 0xf0, 0xb1, 0x51, 0x79, 0xd9, 0x5a, 0x65,
      0x4f, 0x61, 0xe6, 0x99, 0x69, 0x2e, 0x6f, 0x71 }
  };
  static const struct {
    const void *keys;
    uint8_t key_size;
    uint8_t count;
  } keys[] = {
    { keys128, AES_KEY_STORE_SIZE_KEY_SIZE_128,
      sizeof(keys128) / sizeof(keys128[0]) },
    { keys192, AES_KEY_STORE_SIZE_KEY_SIZE_192,
      sizeof(keys192) / sizeof(keys192[0]) },
    { keys256, AES_KEY_STORE_SIZE_KEY_SIZE_256,
      sizeof(keys256) / sizeof(keys256[0]) }
  };
  static const struct {
    bool encrypt;
    uint8_t len_len;
    uint8_t key_size_index;
    uint8_t key_area;
    uint8_t nonce[NONCE_MAX_LEN];
    uint8_t adata[ADATA_MAX_LEN];
    uint16_t adata_len;
    uint8_t mdata[MDATA_MAX_LEN];
    uint16_t mdata_len;
    uint8_t mic_len;
    uint8_t expected[MDATA_MAX_LEN];
  } vectors[] = {
    {
      true, /* encrypt */
      2, /* len_len */
      0, /* key_size_index */
      0, /* key_area */
      { 0x00, 0x00, 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0,
        0x00, 0x00, 0x00, 0x00, 0x05 }, /* nonce */
      {}, /* adata */
      0, /* adata_len */
      { 0x14, 0xaa, 0xbb, 0x00, 0x00, 0x01, 0x02, 0x03,
        0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
        0x0c, 0x0d, 0x0e, 0x0f }, /* mdata */
      20, /* mdata_len */
      0, /* mic_len */
      { 0x92, 0xe8, 0xad, 0xca, 0x53, 0x81, 0xbf, 0xd0,
        0x5b, 0xdd, 0xf3, 0x61, 0x09, 0x09, 0x82, 0xe6,
        0x2c, 0x61, 0x01, 0x4e } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      0, /* key_size_index */
      1, /* key_area */
      { 0xac, 0xde, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01,
        0x00, 0x00, 0x00, 0x05, 0x02 }, /* nonce */
      { 0x08, 0xd0, 0x84, 0x21, 0x43, 0x01, 0x00, 0x00,
        0x00, 0x00, 0x48, 0xde, 0xac, 0x02, 0x05, 0x00,
        0x00, 0x00, 0x55, 0xcf, 0x00, 0x00, 0x51, 0x52,
        0x53, 0x54 }, /* adata */
      26, /* adata_len */
      {}, /* mdata */
      0, /* mdata_len */
      8, /* mic_len */
      { 0x22, 0x3b, 0xc1, 0xec, 0x84, 0x1a, 0xb5, 0x53 } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      0, /* key_size_index */
      0, /* key_area */
      { 0x00, 0x00, 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0,
        0x00, 0x00, 0x00, 0x00, 0x05 }, /* nonce */
      { 0x69, 0x98, 0x03, 0x33, 0x63, 0xbb, 0xaa, 0x01,
        0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x03 }, /* adata */
      15, /* adata_len */
      { 0x14, 0xaa, 0xbb, 0x00, 0x00, 0x01, 0x02, 0x03,
        0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
        0x0c, 0x0d, 0x0e, 0x0f }, /* mdata */
      20, /* mdata_len */
      4, /* mic_len */
      { 0x92, 0xe8, 0xad, 0xca, 0x53, 0x81, 0xbf, 0xd0,
        0x5b, 0xdd, 0xf3, 0x61, 0x09, 0x09, 0x82, 0xe6,
        0x2c, 0x61, 0x01, 0x4e, 0x7b, 0x34, 0x4f, 0x09 } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      0, /* key_size_index */
      0, /* key_area */
      { 0x00, 0x00, 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0,
        0x00, 0x00, 0x00, 0x00, 0x05 }, /* nonce */
      {}, /* adata */
      0, /* adata_len */
      { 0x92, 0xe8, 0xad, 0xca, 0x53, 0x81, 0xbf, 0xd0,
        0x5b, 0xdd, 0xf3, 0x61, 0x09, 0x09, 0x82, 0xe6,
        0x2c, 0x61, 0x01, 0x4e }, /* mdata */
      20, /* mdata_len */
      0, /* mic_len */
      { 0x14, 0xaa, 0xbb, 0x00, 0x00, 0x01, 0x02, 0x03,
        0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
        0x0c, 0x0d, 0x0e, 0x0f } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      0, /* key_size_index */
      1, /* key_area */
      { 0xac, 0xde, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01,
        0x00, 0x00, 0x00, 0x05, 0x02 }, /* nonce */
      { 0x08, 0xd0, 0x84, 0x21, 0x43, 0x01, 0x00, 0x00,
        0x00, 0x00, 0x48, 0xde, 0xac, 0x02, 0x05, 0x00,
        0x00, 0x00, 0x55, 0xcf, 0x00, 0x00, 0x51, 0x52,
        0x53, 0x54 }, /* adata */
      26, /* adata_len */
      { 0x22, 0x3b, 0xc1, 0xec, 0x84, 0x1a, 0xb5, 0x53 }, /* mdata */
      8, /* mdata_len */
      8, /* mic_len */
      {} /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      0, /* key_size_index */
      0, /* key_area */
      { 0x00, 0x00, 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0,
        0x00, 0x00, 0x00, 0x00, 0x05 }, /* nonce */
      { 0x69, 0x98, 0x03, 0x33, 0x63, 0xbb, 0xaa, 0x01,
        0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x03 }, /* adata */
      15, /* adata_len */
      { 0x92, 0xe8, 0xad, 0xca, 0x53, 0x81, 0xbf, 0xd0,
        0x5b, 0xdd, 0xf3, 0x61, 0x09, 0x09, 0x82, 0xe6,
        0x2c, 0x61, 0x01, 0x4e, 0x7b, 0x34, 0x4f, 0x09 }, /* mdata */
      24, /* mdata_len */
      4, /* mic_len */
      { 0x14, 0xaa, 0xbb, 0x00, 0x00, 0x01, 0x02, 0x03,
        0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
        0x0c, 0x0d, 0x0e, 0x0f } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      1, /* key_size_index */
      0, /* key_area */
      { 0x15, 0xb3, 0x69, 0x88, 0x96, 0x99, 0xb6, 0xde,
        0x1f, 0xa3, 0xee, 0x73, 0xe5 }, /* nonce */
      {}, /* adata */
      0, /* adata_len */
      { 0x39, 0xf0, 0x8a, 0x2a, 0xf1, 0xd8, 0xda, 0x62,
        0x12, 0x55, 0x06, 0x39, 0xb9, 0x1f, 0xb2, 0x57,
        0x3e, 0x39, 0xa8, 0xeb, 0x5d, 0x80, 0x1d, 0xe8 }, /* mdata */
      24, /* mdata_len */
      16, /* mic_len */
      { 0x63, 0x42, 0xb8, 0x70, 0x0e, 0xde, 0xc9, 0x7a,
        0x96, 0x0e, 0xb1, 0x6e, 0x7c, 0xb1, 0xeb, 0x44,
        0x12, 0xfb, 0x4e, 0x26, 0x3d, 0xdd, 0x22, 0x06,
        0xb0, 0x90, 0x15, 0x5d, 0x34, 0xa7, 0x6c, 0x83,
        0x24, 0xe5, 0x55, 0x0c, 0x3e, 0xf4, 0x26, 0xed } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      1, /* key_size_index */
      2, /* key_area */
      { 0x92, 0x95, 0x42, 0xcd, 0x69, 0x0f, 0x1b, 0xab,
        0xcf, 0x16, 0x96, 0xcb, 0x03 }, /* nonce */
      { 0x58, 0xf7, 0x0b, 0xab, 0x24, 0xe0, 0xa6, 0x13,
        0x7e, 0x5c, 0xd3, 0xeb, 0x18, 0x65, 0x6f, 0x2b,
        0x5c, 0xcd, 0xdc, 0x3f, 0x53, 0x8a, 0x00, 0x00,
        0xc6, 0x51, 0x90, 0xe4, 0xa3, 0x66, 0x8e, 0x71 }, /* adata */
      32, /* adata_len */
      {}, /* mdata */
      0, /* mdata_len */
      16, /* mic_len */
      { 0x3b, 0xf9, 0xd9, 0x3a, 0xf6, 0xff, 0xac, 0x9a,
        0xc8, 0x4c, 0xd3, 0x20, 0x2d, 0x4e, 0x0c, 0xc8 } /* expected */
    }, {
      true, /* encrypt */
      8, /* len_len */
      1, /* key_size_index */
      4, /* key_area */
      { 0x76, 0x40, 0x43, 0xc4, 0x94, 0x60, 0xb7 }, /* nonce */
      { 0x6e, 0x80, 0xdd, 0x7f, 0x1b, 0xad, 0xf3, 0xa1,
        0xc9, 0xab, 0x25, 0xc7, 0x5f, 0x10, 0xbd, 0xe7,
        0x8c, 0x23, 0xfa, 0x0e, 0xb8, 0xf9, 0xaa, 0xa5,
        0x3a, 0xde, 0xfb, 0xf4, 0xcb, 0xf7, 0x8f, 0xe4 }, /* adata */
      32, /* adata_len */
      { 0xc8, 0xd2, 0x75, 0xf9, 0x19, 0xe1, 0x7d, 0x7f,
        0xe6, 0x9c, 0x2a, 0x1f, 0x58, 0x93, 0x9d, 0xfe,
        0x4d, 0x40, 0x37, 0x91, 0xb5, 0xdf, 0x13, 0x10 }, /* mdata */
      24, /* mdata_len */
      16, /* mic_len */
      { 0x8a, 0x0f, 0x3d, 0x82, 0x29, 0xe4, 0x8e, 0x74,
        0x87, 0xfd, 0x95, 0xa2, 0x8a, 0xd3, 0x92, 0xc8,
        0x0b, 0x36, 0x81, 0xd4, 0xfb, 0xc7, 0xbb, 0xfd,
        0x2d, 0xd6, 0xef, 0x1c, 0x45, 0xd4, 0xcc, 0xb7,
        0x23, 0xdc, 0x07, 0x44, 0x14, 0xdb, 0x50, 0x6d } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      1, /* key_size_index */
      6, /* key_area */
      { 0xc6, 0xae, 0xeb, 0xcb, 0x14, 0x6c, 0xfa, 0xfa,
        0xae, 0x66, 0xf7, 0x8a, 0xab }, /* nonce */
      { 0x7d, 0xc8, 0xc5, 0x21, 0x44, 0xa7, 0xcb, 0x65,
        0xb3, 0xe5, 0xa8, 0x46, 0xe8, 0xfd, 0x7e, 0xae,
        0x37, 0xbf, 0x69, 0x96, 0xc2, 0x99, 0xb5, 0x6e,
        0x49, 0x14, 0x4e, 0xbf, 0x43, 0xa1, 0x77, 0x0f }, /* adata */
      32, /* adata_len */
      { 0xee, 0x7e, 0x60, 0x75, 0xba, 0x52, 0x84, 0x6d,
        0xe5, 0xd6, 0x25, 0x49, 0x59, 0xa1, 0x8a, 0xff,
        0xc4, 0xfa, 0xf5, 0x9c, 0x8e, 0xf6, 0x34, 0x89 }, /* mdata */
      24, /* mdata_len */
      4, /* mic_len */
      { 0x13, 0x7d, 0x9d, 0xa5, 0x9b, 0xaf, 0x5c, 0xbf,
        0xd4, 0x66, 0x20, 0xc5, 0xf2, 0x98, 0xfc, 0x76,
        0x6d, 0xe1, 0x0a, 0xc6, 0x8e, 0x77, 0x4e, 0xdf,
        0x1f, 0x2c, 0x5b, 0xad } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      1, /* key_size_index */
      0, /* key_area */
      { 0x15, 0xb3, 0x69, 0x88, 0x96, 0x99, 0xb6, 0xde,
        0x1f, 0xa3, 0xee, 0x73, 0xe5 }, /* nonce */
      {}, /* adata */
      0, /* adata_len */
      { 0x63, 0x42, 0xb8, 0x70, 0x0e, 0xde, 0xc9, 0x7a,
        0x96, 0x0e, 0xb1, 0x6e, 0x7c, 0xb1, 0xeb, 0x44,
        0x12, 0xfb, 0x4e, 0x26, 0x3d, 0xdd, 0x22, 0x06,
        0xb0, 0x90, 0x15, 0x5d, 0x34, 0xa7, 0x6c, 0x83,
        0x24, 0xe5, 0x55, 0x0c, 0x3e, 0xf4, 0x26, 0xed }, /* mdata */
      40, /* mdata_len */
      16, /* mic_len */
      { 0x39, 0xf0, 0x8a, 0x2a, 0xf1, 0xd8, 0xda, 0x62,
        0x12, 0x55, 0x06, 0x39, 0xb9, 0x1f, 0xb2, 0x57,
        0x3e, 0x39, 0xa8, 0xeb, 0x5d, 0x80, 0x1d, 0xe8 } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      1, /* key_size_index */
      2, /* key_area */
      { 0x92, 0x95, 0x42, 0xcd, 0x69, 0x0f, 0x1b, 0xab,
        0xcf, 0x16, 0x96, 0xcb, 0x03 }, /* nonce */
      { 0x58, 0xf7, 0x0b, 0xab, 0x24, 0xe0, 0xa6, 0x13,
        0x7e, 0x5c, 0xd3, 0xeb, 0x18, 0x65, 0x6f, 0x2b,
        0x5c, 0xcd, 0xdc, 0x3f, 0x53, 0x8a, 0x00, 0x00,
        0xc6, 0x51, 0x90, 0xe4, 0xa3, 0x66, 0x8e, 0x71 }, /* adata */
      32, /* adata_len */
      { 0x3b, 0xf9, 0xd9, 0x3a, 0xf6, 0xff, 0xac, 0x9a,
        0xc8, 0x4c, 0xd3, 0x20, 0x2d, 0x4e, 0x0c, 0xc8 }, /* mdata */
      16, /* mdata_len */
      16, /* mic_len */
      {} /* expected */
    }, {
      false, /* decrypt */
      8, /* len_len */
      1, /* key_size_index */
      4, /* key_area */
      { 0x76, 0x40, 0x43, 0xc4, 0x94, 0x60, 0xb7 }, /* nonce */
      { 0x6e, 0x80, 0xdd, 0x7f, 0x1b, 0xad, 0xf3, 0xa1,
        0xc9, 0xab, 0x25, 0xc7, 0x5f, 0x10, 0xbd, 0xe7,
        0x8c, 0x23, 0xfa, 0x0e, 0xb8, 0xf9, 0xaa, 0xa5,
        0x3a, 0xde, 0xfb, 0xf4, 0xcb, 0xf7, 0x8f, 0xe4 }, /* adata */
      32, /* adata_len */
      { 0x8a, 0x0f, 0x3d, 0x82, 0x29, 0xe4, 0x8e, 0x74,
        0x87, 0xfd, 0x95, 0xa2, 0x8a, 0xd3, 0x92, 0xc8,
        0x0b, 0x36, 0x81, 0xd4, 0xfb, 0xc7, 0xbb, 0xfd,
        0x2d, 0xd6, 0xef, 0x1c, 0x45, 0xd4, 0xcc, 0xb7,
        0x23, 0xdc, 0x07, 0x44, 0x14, 0xdb, 0x50, 0x6d }, /* mdata */
      40, /* mdata_len */
      16, /* mic_len */
      { 0xc8, 0xd2, 0x75, 0xf9, 0x19, 0xe1, 0x7d, 0x7f,
        0xe6, 0x9c, 0x2a, 0x1f, 0x58, 0x93, 0x9d, 0xfe,
        0x4d, 0x40, 0x37, 0x91, 0xb5, 0xdf, 0x13, 0x10 } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      1, /* key_size_index */
      6, /* key_area */
      { 0xc6, 0xae, 0xeb, 0xcb, 0x14, 0x6c, 0xfa, 0xfa,
        0xae, 0x66, 0xf7, 0x8a, 0xab }, /* nonce */
      { 0x7d, 0xc8, 0xc5, 0x21, 0x44, 0xa7, 0xcb, 0x65,
        0xb3, 0xe5, 0xa8, 0x46, 0xe8, 0xfd, 0x7e, 0xae,
        0x37, 0xbf, 0x69, 0x96, 0xc2, 0x99, 0xb5, 0x6e,
        0x49, 0x14, 0x4e, 0xbf, 0x43, 0xa1, 0x77, 0x0f }, /* adata */
      32, /* adata_len */
      { 0x13, 0x7d, 0x9d, 0xa5, 0x9b, 0xaf, 0x5c, 0xbf,
        0xd4, 0x66, 0x20, 0xc5, 0xf2, 0x98, 0xfc, 0x76,
        0x6d, 0xe1, 0x0a, 0xc6, 0x8e, 0x77, 0x4e, 0xdf,
        0x1f, 0x2c, 0x5b, 0xad }, /* mdata */
      28, /* mdata_len */
      4, /* mic_len */
      { 0xee, 0x7e, 0x60, 0x75, 0xba, 0x52, 0x84, 0x6d,
        0xe5, 0xd6, 0x25, 0x49, 0x59, 0xa1, 0x8a, 0xff,
        0xc4, 0xfa, 0xf5, 0x9c, 0x8e, 0xf6, 0x34, 0x89 } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      2, /* key_size_index */
      0, /* key_area */
      { 0x72, 0xa6, 0x0f, 0x34, 0x5a, 0x19, 0x78, 0xfb,
        0x40, 0xf2, 0x8a, 0x2f, 0xa4 }, /* nonce */
      {}, /* adata */
      0, /* adata_len */
      { 0x30, 0xd5, 0x6f, 0xf2, 0xa2, 0x5b, 0x83, 0xfe,
        0xe7, 0x91, 0x11, 0x0f, 0xca, 0xea, 0x48, 0xe4,
        0x1d, 0xb7, 0xc7, 0xf0, 0x98, 0xa8, 0x10, 0x00 }, /* mdata */
      24, /* mdata_len */
      16, /* mic_len */
      { 0x55, 0xf0, 0x68, 0xc0, 0xbb, 0xba, 0x8b, 0x59,
        0x80, 0x13, 0xdd, 0x18, 0x41, 0xfd, 0x74, 0x0f,
        0xda, 0x29, 0x02, 0x32, 0x21, 0x48, 0xab, 0x5e,
        0x93, 0x57, 0x53, 0xe6, 0x01, 0xb7, 0x9d, 0xb4,
        0xae, 0x73, 0x0b, 0x6a, 0xe3, 0x50, 0x07, 0x31 } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      2, /* key_size_index */
      2, /* key_area */
      { 0x29, 0x1e, 0x91, 0xb1, 0x9d, 0xe5, 0x18, 0xcd,
        0x78, 0x06, 0xde, 0x44, 0xf6 }, /* nonce */
      { 0xb4, 0xf8, 0x32, 0x69, 0x44, 0xa4, 0x5d, 0x95,
        0xf9, 0x18, 0x87, 0xc2, 0xa6, 0xac, 0x36, 0xb6,
        0x0e, 0xea, 0x5e, 0xde, 0xf8, 0x4c, 0x1c, 0x35,
        0x81, 0x46, 0xa6, 0x66, 0xb6, 0x87, 0x83, 0x35 }, /* adata */
      32, /* adata_len */
      {}, /* mdata */
      0, /* mdata_len */
      16, /* mic_len */
      { 0xca, 0x48, 0x2c, 0x67, 0x4b, 0x59, 0x90, 0x46,
        0xcc, 0x7d, 0x7e, 0xe0, 0xd0, 0x0e, 0xec, 0x1e } /* expected */
    }, {
      true, /* encrypt */
      8, /* len_len */
      2, /* key_size_index */
      4, /* key_area */
      { 0xaa, 0xa2, 0x3f, 0x10, 0x16, 0x47, 0xd8 }, /* nonce */
      { 0xa3, 0x55, 0xd4, 0xc6, 0x11, 0x81, 0x2e, 0x5f,
        0x92, 0x58, 0xd7, 0x18, 0x8b, 0x3d, 0xf8, 0x85,
        0x14, 0x77, 0x09, 0x4f, 0xfc, 0x2a, 0xf2, 0xcf,
        0x0c, 0x86, 0x70, 0xdb, 0x90, 0x3f, 0xbb, 0xe0 }, /* adata */
      32, /* adata_len */
      { 0x64, 0x4e, 0xb3, 0x4b, 0x9a, 0x12, 0x6e, 0x43,
        0x7b, 0x5e, 0x01, 0x5e, 0xea, 0x14, 0x1c, 0xa1,
        0xa8, 0x80, 0x20, 0xf2, 0xd5, 0xd6, 0xcc, 0x2c }, /* mdata */
      24, /* mdata_len */
      16, /* mic_len */
      { 0x27, 0xed, 0x90, 0x66, 0x81, 0x74, 0xeb, 0xf8,
        0x24, 0x1a, 0x3c, 0x74, 0xb3, 0x5e, 0x12, 0x46,
        0xb6, 0x61, 0x7e, 0x41, 0x23, 0x57, 0x8f, 0x15,
        0x3b, 0xdb, 0x67, 0x06, 0x2a, 0x13, 0xef, 0x4e,
        0x98, 0x6f, 0x5b, 0xb3, 0xd0, 0xbb, 0x43, 0x07 } /* expected */
    }, {
      true, /* encrypt */
      2, /* len_len */
      2, /* key_size_index */
      6, /* key_area */
      { 0x2e, 0x1e, 0x01, 0x32, 0x46, 0x85, 0x00, 0xd4,
        0xbd, 0x47, 0x86, 0x25, 0x63 }, /* nonce */
      { 0x3c, 0x5f, 0x54, 0x04, 0x37, 0x0a, 0xbd, 0xcb,
        0x1e, 0xdd, 0xe9, 0x9d, 0xe6, 0x0d, 0x06, 0x82,
        0xc6, 0x00, 0xb0, 0x34, 0xe0, 0x63, 0xb7, 0xd3,
        0x23, 0x77, 0x23, 0xda, 0x70, 0xab, 0x75, 0x52 }, /* adata */
      32, /* adata_len */
      { 0x23, 0x90, 0x29, 0xf1, 0x50, 0xbc, 0xcb, 0xd6,
        0x7e, 0xdb, 0xb6, 0x7f, 0x8a, 0xe4, 0x56, 0xb4,
        0xea, 0x06, 0x6a, 0x4b, 0xee, 0xe0, 0x65, 0xf9 }, /* mdata */
      24, /* mdata_len */
      4, /* mic_len */
      { 0x9c, 0x8d, 0x5d, 0xd2, 0x27, 0xfd, 0x9f, 0x81,
        0x23, 0x76, 0x01, 0x83, 0x0a, 0xfe, 0xe4, 0xf0,
        0x11, 0x56, 0x36, 0xc8, 0xe5, 0xd5, 0xfd, 0x74,
        0x3c, 0xb9, 0xaf, 0xed } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      2, /* key_size_index */
      0, /* key_area */
      { 0x72, 0xa6, 0x0f, 0x34, 0x5a, 0x19, 0x78, 0xfb,
        0x40, 0xf2, 0x8a, 0x2f, 0xa4 }, /* nonce */
      {}, /* adata */
      0, /* adata_len */
      { 0x55, 0xf0, 0x68, 0xc0, 0xbb, 0xba, 0x8b, 0x59,
        0x80, 0x13, 0xdd, 0x18, 0x41, 0xfd, 0x74, 0x0f,
        0xda, 0x29, 0x02, 0x32, 0x21, 0x48, 0xab, 0x5e,
        0x93, 0x57, 0x53, 0xe6, 0x01, 0xb7, 0x9d, 0xb4,
        0xae, 0x73, 0x0b, 0x6a, 0xe3, 0x50, 0x07, 0x31 }, /* mdata */
      40, /* mdata_len */
      16, /* mic_len */
      { 0x30, 0xd5, 0x6f, 0xf2, 0xa2, 0x5b, 0x83, 0xfe,
        0xe7, 0x91, 0x11, 0x0f, 0xca, 0xea, 0x48, 0xe4,
        0x1d, 0xb7, 0xc7, 0xf0, 0x98, 0xa8, 0x10, 0x00 } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      2, /* key_size_index */
      2, /* key_area */
      { 0x29, 0x1e, 0x91, 0xb1, 0x9d, 0xe5, 0x18, 0xcd,
        0x78, 0x06, 0xde, 0x44, 0xf6 }, /* nonce */
      { 0xb4, 0xf8, 0x32, 0x69, 0x44, 0xa4, 0x5d, 0x95,
        0xf9, 0x18, 0x87, 0xc2, 0xa6, 0xac, 0x36, 0xb6,
        0x0e, 0xea, 0x5e, 0xde, 0xf8, 0x4c, 0x1c, 0x35,
        0x81, 0x46, 0xa6, 0x66, 0xb6, 0x87, 0x83, 0x35 }, /* adata */
      32, /* adata_len */
      { 0xca, 0x48, 0x2c, 0x67, 0x4b, 0x59, 0x90, 0x46,
        0xcc, 0x7d, 0x7e, 0xe0, 0xd0, 0x0e, 0xec, 0x1e }, /* mdata */
      16, /* mdata_len */
      16, /* mic_len */
      {} /* expected */
    }, {
      false, /* decrypt */
      8, /* len_len */
      2, /* key_size_index */
      4, /* key_area */
      { 0xaa, 0xa2, 0x3f, 0x10, 0x16, 0x47, 0xd8 }, /* nonce */
      { 0xa3, 0x55, 0xd4, 0xc6, 0x11, 0x81, 0x2e, 0x5f,
        0x92, 0x58, 0xd7, 0x18, 0x8b, 0x3d, 0xf8, 0x85,
        0x14, 0x77, 0x09, 0x4f, 0xfc, 0x2a, 0xf2, 0xcf,
        0x0c, 0x86, 0x70, 0xdb, 0x90, 0x3f, 0xbb, 0xe0 }, /* adata */
      32, /* adata_len */
      { 0x27, 0xed, 0x90, 0x66, 0x81, 0x74, 0xeb, 0xf8,
        0x24, 0x1a, 0x3c, 0x74, 0xb3, 0x5e, 0x12, 0x46,
        0xb6, 0x61, 0x7e, 0x41, 0x23, 0x57, 0x8f, 0x15,
        0x3b, 0xdb, 0x67, 0x06, 0x2a, 0x13, 0xef, 0x4e,
        0x98, 0x6f, 0x5b, 0xb3, 0xd0, 0xbb, 0x43, 0x07 }, /* mdata */
      40, /* mdata_len */
      16, /* mic_len */
      { 0x64, 0x4e, 0xb3, 0x4b, 0x9a, 0x12, 0x6e, 0x43,
        0x7b, 0x5e, 0x01, 0x5e, 0xea, 0x14, 0x1c, 0xa1,
        0xa8, 0x80, 0x20, 0xf2, 0xd5, 0xd6, 0xcc, 0x2c } /* expected */
    }, {
      false, /* decrypt */
      2, /* len_len */
      2, /* key_size_index */
      6, /* key_area */
      { 0x2e, 0x1e, 0x01, 0x32, 0x46, 0x85, 0x00, 0xd4,
        0xbd, 0x47, 0x86, 0x25, 0x63 }, /* nonce */
      { 0x3c, 0x5f, 0x54, 0x04, 0x37, 0x0a, 0xbd, 0xcb,
        0x1e, 0xdd, 0xe9, 0x9d, 0xe6, 0x0d, 0x06, 0x82,
        0xc6, 0x00, 0xb0, 0x34, 0xe0, 0x63, 0xb7, 0xd3,
        0x23, 0x77, 0x23, 0xda, 0x70, 0xab, 0x75, 0x52 }, /* adata */
      32, /* adata_len */
      { 0x9c, 0x8d, 0x5d, 0xd2, 0x27, 0xfd, 0x9f, 0x81,
        0x23, 0x76, 0x01, 0x83, 0x0a, 0xfe, 0xe4, 0xf0,
        0x11, 0x56, 0x36, 0xc8, 0xe5, 0xd5, 0xfd, 0x74,
        0x3c, 0xb9, 0xaf, 0xed }, /* mdata */
      28, /* mdata_len */
      4, /* mic_len */
      { 0x23, 0x90, 0x29, 0xf1, 0x50, 0xbc, 0xcb, 0xd6,
        0x7e, 0xdb, 0xb6, 0x7f, 0x8a, 0xe4, 0x56, 0xb4,
        0xea, 0x06, 0x6a, 0x4b, 0xee, 0xe0, 0x65, 0xf9 } /* expected */
    }
  };
  static uint8_t adata[ADATA_MAX_LEN];
  static uint8_t mdata[MDATA_MAX_LEN];
  static uint8_t mic[MIC_MAX_LEN];
  static int i;
  static uint8_t key_size_index = -1, ret;
  static rtimer_clock_t time, time2, total_time;

  PROCESS_BEGIN();

  puts("-----------------------------------------\n"
       "Initializing cryptoprocessor...");
  crypto_init();

  for(i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) {
    if(key_size_index != vectors[i].key_size_index) {
      key_size_index = vectors[i].key_size_index;
      printf("-----------------------------------------\n"
             "Filling %d-bit key store...\n", 128 + (key_size_index << 6));
      time = RTIMER_NOW();
      ret = aes_load_keys(keys[key_size_index].keys,
              keys[key_size_index].key_size, keys[key_size_index].count, 0);
      time = RTIMER_NOW() - time;
      printf("aes_load_keys(): %s, %lu us\n", str_res[ret],
             (uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
      PROCESS_PAUSE();
      if(ret != CRYPTO_SUCCESS) {
        break;
      }
    }

    printf("-----------------------------------------\n"
           "Test vector #%d: %s\n"
           "len_len=%d key_area=%d\n"
           "adata_len=%d mdata_len=%d mic_len=%d\n",
           i, vectors[i].encrypt ? "encrypt" : "decrypt",
           vectors[i].len_len, vectors[i].key_area,
           vectors[i].adata_len, vectors[i].mdata_len, vectors[i].mic_len);

    /* adata and mdata have to be in SRAM. */
    rom_util_memcpy(adata, vectors[i].adata, vectors[i].adata_len);
    rom_util_memcpy(mdata, vectors[i].mdata, vectors[i].mdata_len);

    time = RTIMER_NOW();
    if(vectors[i].encrypt) {
      ret = ccm_auth_encrypt_start(vectors[i].len_len, vectors[i].key_area,
                                   vectors[i].nonce, adata,
                                   vectors[i].adata_len, mdata,
                                   vectors[i].mdata_len, mdata,
                                   vectors[i].mic_len, &ccm_test_process);
      time2 = RTIMER_NOW();
      time = time2 - time;
      total_time = time;
      if(ret == CRYPTO_SUCCESS) {
        PROCESS_WAIT_EVENT_UNTIL(ccm_auth_encrypt_check_status());
        time2 = RTIMER_NOW() - time2;
        total_time += time2;
      }
      printf("ccm_auth_encrypt_start(): %s, %lu us\n", str_res[ret],
             (uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
      if(ret != CRYPTO_SUCCESS) {
        PROCESS_PAUSE();
        continue;
      }
      printf("ccm_auth_encrypt_check_status() wait: %lu us\n",
             (uint32_t)((uint64_t)time2 * 1000000 / RTIMER_SECOND));

      time = RTIMER_NOW();
      ret = ccm_auth_encrypt_get_result(mic, vectors[i].mic_len);
      time = RTIMER_NOW() - time;
      total_time += time;
      printf("ccm_auth_encrypt_get_result(): %s, %lu us\n", str_res[ret],
             (uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
      PROCESS_PAUSE();
      if(ret != CRYPTO_SUCCESS) {
        continue;
      }

      if(rom_util_memcmp(mdata, vectors[i].expected, vectors[i].mdata_len)) {
        puts("Encrypted message does not match expected one");
      } else {
        puts("Encrypted message OK");
      }

      if(rom_util_memcmp(mic, vectors[i].expected + vectors[i].mdata_len,
                         vectors[i].mic_len)) {
        puts("MIC does not match expected one");
      } else {
        puts("MIC OK");
      }
    } else {
      ret = ccm_auth_decrypt_start(vectors[i].len_len, vectors[i].key_area,
                                   vectors[i].nonce, adata,
                                   vectors[i].adata_len, mdata,
                                   vectors[i].mdata_len, mdata,
                                   vectors[i].mic_len, &ccm_test_process);
      time2 = RTIMER_NOW();
      time = time2 - time;
      total_time = time;
      if(ret == CRYPTO_SUCCESS) {
        PROCESS_WAIT_EVENT_UNTIL(ccm_auth_decrypt_check_status());
        time2 = RTIMER_NOW() - time2;
        total_time += time2;
      }
      printf("ccm_auth_decrypt_start(): %s, %lu us\n", str_res[ret],
             (uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
      if(ret != CRYPTO_SUCCESS) {
        PROCESS_PAUSE();
        continue;
      }
      printf("ccm_auth_decrypt_check_status() wait: %lu us\n",
             (uint32_t)((uint64_t)time2 * 1000000 / RTIMER_SECOND));

      time = RTIMER_NOW();
      ret = ccm_auth_decrypt_get_result(mdata, vectors[i].mdata_len,
                                        mic, vectors[i].mic_len);
      time = RTIMER_NOW() - time;
      total_time += time;
      printf("ccm_auth_decrypt_get_result(): %s, %lu us\n", str_res[ret],
             (uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
      PROCESS_PAUSE();
      if(ret != CRYPTO_SUCCESS) {
        continue;
      }

      if(rom_util_memcmp(mdata, vectors[i].expected,
                         vectors[i].mdata_len - vectors[i].mic_len)) {
        puts("Decrypted message does not match expected one");
      } else {
        puts("Decrypted message OK");
      }
    }

    printf("Total duration: %lu us\n",
           (uint32_t)((uint64_t)total_time * 1000000 / RTIMER_SECOND));
  }

  puts("-----------------------------------------\n"
       "Disabling cryptoprocessor...");
  crypto_disable();

  puts("Done!");

  PROCESS_END();
}
コード例 #3
0
ファイル: triumviMain.c プロジェクト: lab11/G2
PROCESS_THREAD(decryptProcess, ev, data) {
    PROCESS_BEGIN();
    // AES
    static uint8_t myMic[8] = {0x0};
    static uint8_t myNonce[13] = {0};
    static uint8_t aes_key[] = AES_KEY;
    aes_load_keys(aes_key, AES_KEY_STORE_SIZE_KEY_SIZE_128, 1, 0);
    
    static uint8_t srcExtAddr[8];
    static uint8_t packetPayload[128];
    static uint8_t packetDuplicated[123];
    static uint8_t* cData = packetDuplicated;
    static uint8_t* aData = srcExtAddr;

    uint8_t *packet_hdr;
    uint8_t *packet_ptr;
    uint16_t packet_length;
    uint8_t src_addr_len;

    uint8_t auth_res;
    uint8_t myPDATA_LEN;

    uint16_t i, j, k;
    uint8_t tmp;

    packet_header_t rx_pkt_header;

    while(1){
        PROCESS_YIELD();
        #ifndef LED_DEBUG
        leds_on(LEDS_RED);
        #else
        leds_on(LEDS_RED);
        leds_on(LEDS_GREEN);
        leds_on(LEDS_BLUE);
        #endif
        // Get data from radio buffer and parse it
        packet_hdr = packetbuf_hdrptr();                                   
        packet_ptr = packetbuf_dataptr();                                  
        packet_length = packetbuf_datalen();                               
        process_packet_header(&rx_pkt_header, packet_hdr);
        memcpy(packetPayload, packet_ptr, packet_length);
        src_addr_len = rx_pkt_header.pkt_src_addr_len;

        // data format: 
        // 1 bytes ID, 
        // 8 bytes source addr, 
        // 4 bytes power, 
        // 1 bytes status reg
        // 2 bytes panel/circuit ID (optional)
        // 4 bytes pf (optional)
        // 4 bytes VRMS (optional)
        // 4 bytes IRMS (optional)

        // RX buffer is not full
        if (triumviRXBufFull==0){
            triumviRXPackets[triumviAvailIDX].length = 1;
            if (src_addr_len>0){
                for (i=0; i<src_addr_len; i++){
                    srcExtAddr[i] = rx_pkt_header.pkt_src_addr[src_addr_len - 1 - i];
                    triumviRXPackets[triumviAvailIDX].payload[1+i] = srcExtAddr[i];
                }
                triumviRXPackets[triumviAvailIDX].length += src_addr_len;
            }
            // Decrypt packet
            if (packet_ptr[0]==TRIUMVI_PKT_IDENTIFIER){
                memcpy(myNonce, srcExtAddr, 8);
                memcpy(&myNonce[9], &packetPayload[1], 4); // Nonce[8] should be 0
                for (i=0; i<POSSIBLE_PKT_LEN_COMBINATION; i++){ 
                    myPDATA_LEN = possible_packet_length[i];
                    memcpy(packetDuplicated, &packetPayload[5], packet_length-5);
                    ccm_auth_decrypt_start(LEN_LEN, 0, myNonce, aData, ADATA_LEN, 
                        cData, (myPDATA_LEN+MIC_LEN), MIC_LEN, NULL);
                    while (ccm_auth_decrypt_check_status()!=AES_CTRL_INT_STAT_RESULT_AV){}
                    auth_res = ccm_auth_decrypt_get_result(cData, myPDATA_LEN+MIC_LEN, myMic, MIC_LEN);
                    if (auth_res==CRYPTO_SUCCESS)
                        break;
                }
                // succefully decoded the packet, pack the data into buffer
                if (auth_res==CRYPTO_SUCCESS){
                    triumviRXPackets[triumviAvailIDX].payload[0] = packet_ptr[0];
                    tmp = triumviRXPackets[triumviAvailIDX].length;
                    for (i=0; i<4; i++){
                        triumviRXPackets[triumviAvailIDX].payload[tmp+i] = cData[i];
                    }
                    triumviRXPackets[triumviAvailIDX].payload[tmp+4] = cData[4]; // Status register
                    j = 5;
                    k = 5;
                    if (cData[4]&BATTERYPACK_STATUSREG){
                        triumviRXPackets[triumviAvailIDX].payload[tmp+j] = cData[k]; // Panel ID
                        triumviRXPackets[triumviAvailIDX].payload[tmp+j+1] = cData[k+1]; // Circuit ID
                        triumviRXPackets[triumviAvailIDX].length += 2;
                        j += 2;
                        k += 2;
                    }
                    // PF, VRMS, IRMS
                    if (cData[4]&POWERFACTOR_STATUSREG){
                        for (i=0; i<6; i++){
                            triumviRXPackets[triumviAvailIDX].payload[tmp+j+i] = cData[k+i];
                        }
                        triumviRXPackets[triumviAvailIDX].length += 6;
                    }
                    // base length
                    triumviRXPackets[triumviAvailIDX].length += 5;
                    
                    // check if fifo is full
                    if (((triumviAvailIDX == TRIUMVI_PACKET_BUF_LEN-1) && (triumviFullIDX == 0)) || 
                        ((triumviAvailIDX != TRIUMVI_PACKET_BUF_LEN-1) && (triumviFullIDX == triumviAvailIDX+1))){
                        triumviRXBufFull = 1;
                        #ifndef LED_DEBUG
                        leds_on(LEDS_GREEN);
                        #endif
                    }
                    // advance pointer
                    if (triumviAvailIDX == TRIUMVI_PACKET_BUF_LEN-1)
                        triumviAvailIDX = 0;
                    else
                        triumviAvailIDX += 1;
                }
            }
        }
        #ifndef LED_DEBUG
        leds_off(LEDS_RED);
        #endif
        process_poll(&mainProcess);
    }
    PROCESS_END();
}