Ejemplo n.º 1
0
err_status_t
ctr_prng_init(rand_source_func_t random_source) {
  uint8_t tmp_key[32];
  err_status_t status;

  /* initialize output count to zero */
  ctr_prng.octet_count = 0;

  /* set random source */
  ctr_prng.rand = random_source;
  
  /* initialize secret key from random source */
  status = random_source(tmp_key, 32);
  if (status) 
    return status;

  /* initialize aes ctr context with random key */
#ifdef OPENSSL
  status = aes_icm_openssl_context_init(&ctr_prng.state, tmp_key, 30);
#else
  status = aes_icm_context_init(&ctr_prng.state, tmp_key, 30);
#endif
  if (status) 
    return status;

  return err_status_ok;
}
Ejemplo n.º 2
0
static ismacryp_rc_t initSessionData (ismacryp_session_id_t session, 
                               ismacryp_session_t *sp,
                               ismacryp_keytype_t keytype) 
{

#ifdef HAVE_SRTP
  FILE *fp;
  int i;
  char kms_data_file[KMS_DATA_FILE_FILENAME_MAX_LEN];
  char kms_data[KMS_DATA_FILE_MAX_LINE_LEN+1];
  char temp[25];
  err_status_t rc = err_status_ok;
  size_t pathlen;
  size_t filenamelen;
  int foundKey = FALSE;
  int len;
#endif

  sp->sessid = session;
  sp->next = NULL;
  sp->prev = NULL;

#ifdef HAVE_SRTP
  // get the key material
  // NULL case, key and salt have been memset to 0, nothing breaks.

  strncpy(kms_data_file,getenv("HOME"),KMS_DATA_FILE_FILENAME_MAX_LEN);
  pathlen = strlen(kms_data_file);
  filenamelen = strlen(KMS_DATA_FILE);
  if ( (pathlen + filenamelen + 1) > KMS_DATA_FILE_FILENAME_MAX_LEN ) { // +1 for '/'
     fprintf(stdout,"key file name too long\n");
     return ismacryp_rc_keyfilename_error;
  }
  kms_data_file[pathlen+1] = kms_data_file[pathlen];
  kms_data_file[pathlen] = '/';
  strncpy(&kms_data_file[pathlen+1], KMS_DATA_FILE, filenamelen );

  switch( keytype) {
  case KeyTypeVideo:
    strcpy(temp,KMS_DATA_VIDEOKEY_STR);
    break;
  case KeyTypeAudio:
    strcpy(temp,KMS_DATA_AUDIOKEY_STR);
    break;
  case KeyTypeOther:
  default:
    fprintf(stdout,"Unsupported key type: %d\n",keytype);
    return ismacryp_rc_keytype_error;
  }

  if ( !(fp=fopen(kms_data_file,"r")) ) {
     fprintf(stdout,"Can't open kms file: %s\n",kms_data_file);
     return(ismacryp_rc_keyfile_error);
  }

  i = 0;
  while ( fgets(kms_data,KMS_DATA_FILE_MAX_LINE_LEN,fp))  {
     len = strlen(kms_data);
     kms_data[len-1] = kms_data[len]; // get rid of newline
     i++;

     if ( !strncmp(kms_data, temp, strlen(KMS_DATA_AUDIOKEY_STR)) ) {
       for (i=0;i<AES_KEY_SALT_LEN;i++) {
	 uint temp;
	 fscanf(fp, "%x", &temp);
	 sp->kk.aes_overlay[i] = temp;
       }
       foundKey = TRUE;
       break;
     }
  }
  if ( !foundKey ) {
     fprintf(stdout, "Can't find %s\n", temp);
     fclose(fp);
     return ismacryp_rc_key_error;

  }

  fclose(fp);
#endif

   
  sp->keycount      = 1;
  sp->sample_count  = 0;
  sp->BSO           = 0;
  sp->IV_len        = ISMACRYP_DEFAULT_IV_LENGTH;
  sp->deltaIV_len   = ISMACRYP_DEFAULT_DELTA_IV_LENGTH;
  sp->key_type      = keytype;
  sp->selective_enc = FALSE;

#ifdef HAVE_SRTP
  // Allocate cipher.
  //fprintf(stdout," - allocate cipher for session %d\n", session);
  rc=aes_icm_alloc_ismacryp(&(sp->cp), AES_KEY_SALT_LEN, 1);
  if ( rc != err_status_ok ) {
      fprintf(stdout," - allocate cipher for session %d FAILED  rc = %d\n", session,
                                     rc );
      return ismacryp_rc_cipheralloc_error;
  }

  // Init cipher.
  rc=aes_icm_context_init(sp->cp->state, sp->kk.aes_overlay);
  if ( rc != err_status_ok ) {
      fprintf(stdout," - init cipher for session %d FAILED  rc = %d\n", session,
                                     rc );
      return ismacryp_rc_cipherinit_error;
  }
#endif

  return ismacryp_rc_ok;
}