Exemple #1
0
static inline void
encrypt(rijndael_ctx *ctx, size_t L, unsigned long counter,
	unsigned char *msg, size_t len,
	unsigned char A[DTLS_CCM_BLOCKSIZE],
	unsigned char S[DTLS_CCM_BLOCKSIZE]) {

  static unsigned long C;

  SET_COUNTER(A, L, counter, C);    
  rijndael_encrypt(ctx, A, S);
  memxor(msg, S, len);
}
Exemple #2
0
static inline void
encrypt(rijndael_ctx *ctx, size_t L, unsigned long counter,
        unsigned char *msg, size_t len,
        unsigned char A[DTLS_CCM_BLOCKSIZE],
        unsigned char S[DTLS_CCM_BLOCKSIZE]) {

    static unsigned long counter_tmp;

    SET_COUNTER(A, L, counter, counter_tmp);

#ifdef NOUVELLE_IMPLE_AES
    // TODO vérifier les tailles des copies
    memcpy(S, A, DTLS_CCM_BLOCKSIZE);
    aes128_enc(S, ctx);
#endif

#ifdef ANCIENNE_IMPLE_AES
    rijndael_encrypt(ctx, A, S);
#endif
    memxor(msg, S, len);
}
Exemple #3
0
long int
dtls_ccm_decrypt_message(rijndael_ctx *ctx, size_t M, size_t L,
			 unsigned char N[DTLS_CCM_BLOCKSIZE], 
			 unsigned char *msg, size_t lm, 
			 const unsigned char *aad, size_t la) {
  
  size_t len;
  unsigned long C;
  unsigned long counter = 1; /* \bug does not work correctly on ia32 when
			             lm >= 2^16 */
  unsigned char A[DTLS_CCM_BLOCKSIZE]; /* A_i blocks for encryption input */
  unsigned char B[DTLS_CCM_BLOCKSIZE]; /* B_i blocks for CBC-MAC input */
  unsigned char S[DTLS_CCM_BLOCKSIZE]; /* S_i = encrypted A_i blocks */
  unsigned char X[DTLS_CCM_BLOCKSIZE]; /* X_i = encrypted B_i blocks */

  if (lm < M)
    goto error;

  len = lm;	      /* save original length */
  lm -= M;	      /* detract MAC size*/

  /* create the initial authentication block B0 */
  block0(M, L, la, lm, N, B);
  add_auth_data(ctx, aad, la, B, X);

  /* initialize block template */
  A[0] = L-1;

  /* copy the nonce */
  memcpy(A + 1, N, DTLS_CCM_BLOCKSIZE - L);
  
  while (lm >= DTLS_CCM_BLOCKSIZE) {
    /* decrypt */
    encrypt(ctx, L, counter, msg, DTLS_CCM_BLOCKSIZE, A, S);
    
    /* calculate MAC */
    mac(ctx, msg, DTLS_CCM_BLOCKSIZE, B, X);

    /* update local pointers */
    lm -= DTLS_CCM_BLOCKSIZE;
    msg += DTLS_CCM_BLOCKSIZE;
    counter++;
  }

  if (lm) {
    /* decrypt */
    encrypt(ctx, L, counter, msg, lm, A, S);

    /* Calculate MAC. Note that msg ends in the MAC so we must
     * construct B to contain X ^ msg for the first lm bytes (done in
     * mac() and X ^ 0 for the remaining DTLS_CCM_BLOCKSIZE - lm bytes
     * (i.e., we can use memcpy() here).
     */
    memcpy(B + lm, X + lm, DTLS_CCM_BLOCKSIZE - lm);
    mac(ctx, msg, lm, B, X); 

    /* update local pointers */
    msg += lm;
  }
  
  /* calculate S_0 */  
  SET_COUNTER(A, L, 0, C);
  rijndael_encrypt(ctx, A, S);

  memxor(msg, S, M);

  /* return length if MAC is valid, otherwise continue with error handling */
  if (memcmp(X, msg, M) == 0) 
    return len - M;
  
 error:
  return -1;
}
Exemple #4
0
long int
dtls_ccm_encrypt_message(rijndael_ctx *ctx, size_t M, size_t L, 
			 unsigned char N[DTLS_CCM_BLOCKSIZE], 
			 unsigned char *msg, size_t lm, 
			 const unsigned char *aad, size_t la) {
  size_t i, len;
  unsigned long C;
  unsigned long counter = 1; /* \bug does not work correctly on ia32 when
			             lm >= 2^16 */
  unsigned char A[DTLS_CCM_BLOCKSIZE]; /* A_i blocks for encryption input */
  unsigned char B[DTLS_CCM_BLOCKSIZE]; /* B_i blocks for CBC-MAC input */
  unsigned char S[DTLS_CCM_BLOCKSIZE]; /* S_i = encrypted A_i blocks */
  unsigned char X[DTLS_CCM_BLOCKSIZE]; /* X_i = encrypted B_i blocks */

  len = lm;			/* save original length */
  /* create the initial authentication block B0 */
  block0(M, L, la, lm, N, B);
  add_auth_data(ctx, aad, la, B, X);

  /* initialize block template */
  A[0] = L-1;

  /* copy the nonce */
  memcpy(A + 1, N, DTLS_CCM_BLOCKSIZE - L);
  
  while (lm >= DTLS_CCM_BLOCKSIZE) {
    /* calculate MAC */
    mac(ctx, msg, DTLS_CCM_BLOCKSIZE, B, X);

    /* encrypt */
    encrypt(ctx, L, counter, msg, DTLS_CCM_BLOCKSIZE, A, S);

    /* update local pointers */
    lm -= DTLS_CCM_BLOCKSIZE;
    msg += DTLS_CCM_BLOCKSIZE;
    counter++;
  }

  if (lm) {
    /* Calculate MAC. The remainder of B must be padded with zeroes, so
     * B is constructed to contain X ^ msg for the first lm bytes (done in
     * mac() and X ^ 0 for the remaining DTLS_CCM_BLOCKSIZE - lm bytes
     * (i.e., we can use memcpy() here).
     */
    memcpy(B + lm, X + lm, DTLS_CCM_BLOCKSIZE - lm);
    mac(ctx, msg, lm, B, X);

    /* encrypt */
    encrypt(ctx, L, counter, msg, lm, A, S);

    /* update local pointers */
    msg += lm;
  }
  
  /* calculate S_0 */  
  SET_COUNTER(A, L, 0, C);
  rijndael_encrypt(ctx, A, S);

  for (i = 0; i < M; ++i)
    *msg++ = X[i] ^ S[i];

  return len + M;
}
Exemple #5
0
void armci_send_req(int proc, request_header_t* msginfo, int len)
{
    int msglen = sizeof(request_header_t);
    lapi_cntr_t *pcmpl_cntr, *pcntr = &(BUF_TO_EVBUF(msginfo)->cntr);
    int rc;

    msginfo->tag.cntr= pcntr;
#if ARMCI_ENABLE_GPC_CALLS
    if(msginfo->operation==GET && msginfo->format==VECTOR && msginfo->ehlen){ 
        msginfo->tag.buf = (char *)(msginfo+1)+msginfo->dscrlen;
    }
    else 
#endif
        msginfo->tag.buf = msginfo+1;

    if(msginfo->operation==GET || msginfo->operation==LOCK){

        SET_COUNTER(*(lapi_cmpl_t*)pcntr,1);/*dataarrive in same buf*/
        /*The GPC case. Note that we don't use the parameter len*/
        if(msginfo->format==VECTOR && msginfo->ehlen > 0) 
            msglen += msginfo->datalen;
        if(lapi_max_uhdr_data_sz < msginfo->dscrlen){

            msginfo->dscrlen = -msginfo->dscrlen; /* no room for descriptor */
            pcntr = NULL; /* GET(descr) from CH will increment buf cntr */

        }else msglen += msginfo->dscrlen;

        /*
           we should send the mutex, too. When op==LOCK, Value of len parameter
           is already sizeof(reqest_header_t)+sizeof(int), since we dont use 
           len but construct our own msglen, we need to add sizeof(int).
           */
        if(msginfo->operation==LOCK) msglen += sizeof(int);

        pcmpl_cntr=NULL; /* don't trace completion status for load ops */

    }else if (msginfo->operation==UNLOCK){

        msglen += msginfo->dscrlen;
        pcmpl_cntr=NULL; /* don't trace completion status for unlock */

    }else{

        if(lapi_max_uhdr_data_sz < (msginfo->datalen + msginfo->dscrlen)){

            msginfo->datalen = -msginfo->datalen;
            msginfo->dscrlen = -msginfo->dscrlen;
            pcntr = NULL; /* GET/LOCK from CH will increment buf cntr */

        }else msglen += msginfo->dscrlen+msginfo->datalen;

        /* trace completion of store ops */
        pcmpl_cntr = &cmpl_arr[msginfo->to].cntr; 

    }

    if(msginfo->operation==PUT || ARMCI_ACC(msginfo->operation)) 
        UPDATE_FENCE_STATE(msginfo->to, msginfo->operation, 1);

    if((rc=LAPI_Amsend(lapi_handle,(uint)msginfo->to,
                    (void*)armci_header_handler, msginfo, msglen, NULL, 0,
                    NULL, pcntr, pcmpl_cntr))) armci_die("AM failed",rc);

    if(DEBUG_) fprintf(stderr,"%d sending req=%d to %d\n",
            armci_me, msginfo->operation, proc);
}