crypto/ringlwe/ringlwe_key.c
/* Convert public key data structure into binary */
Convert public key data structure into binary
Convert public key data structure into binary
size_t i2o_RLWE_PUB(RLWE_PUB *pub, unsigned char **out) {
  size_t buf_len = 0;
  int new_buffer = 0, i;
  if (pub == NULL) {
    RINGLWEerr(RINGLWE_F_I2O_RLWE_PUB, ERR_R_PASSED_NULL_PARAMETER);
    return 0;
  }
  if (pub->param_data == NULL) {
    RINGLWEerr(RINGLWE_F_I2O_RLWE_PUB, ERR_R_PASSED_NULL_PARAMETER);
    return 0;
  }
  uint32_t m = pub->param_data->m;
  RINGELT  q = pub->param_data->q;
  uint32_t n_ringelt_bytes = 2;
  if (q >= 0x00010000)
    n_ringelt_bytes = 4;
  buf_len = _RLWE_DESCRIPTOR_LEN + m * n_ringelt_bytes; 
  if (out == NULL || buf_len == 0)
    return buf_len;
  if (*out == NULL) {
    if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
      RINGLWEerr(RINGLWE_F_I2O_RLWE_PUB, ERR_R_MALLOC_FAILURE);
      return 0;
    }
    new_buffer = 1;
  }
  unsigned char *ptr = *out;
  memcpy (ptr, pub->descriptor, _RLWE_DESCRIPTOR_LEN);
  ptr += _RLWE_DESCRIPTOR_LEN;
  for (i = 0; i < m; i++) {
    if (n_ringelt_bytes == 4) {
      *ptr++ = (unsigned char) ((pub->b[i] >> 24) & 0xff);
      *ptr++ = (unsigned char) ((pub->b[i] >> 16) & 0xff);
    }
    *ptr++ = (unsigned char) ((pub->b[i] >> 8) & 0xff);
    *ptr++ = (unsigned char) ((pub->b[i]) & 0xff);
  }
  if (!new_buffer)
    *out = ptr;
  return buf_len;
}
size_t i2o_RLWE_PUB ( RLWE_PUB * pub , unsigned char * * out ) { size_t buf_len = 0 ; int new_buffer = 0 , i ; if ( pub == NULL ) { RINGLWEerr ( RINGLWE_F_I2O_RLWE_PUB , ERR_R_PASSED_NULL_PARAMETER ) ; return 0 ; } if ( pub -> param_data == NULL ) { RINGLWEerr ( RINGLWE_F_I2O_RLWE_PUB , ERR_R_PASSED_NULL_PARAMETER ) ; return 0 ; } uint32_t m = pub -> param_data -> m ; RINGELT q = pub -> param_data -> q ; uint32_t n_ringelt_bytes = 2 ; if ( q >= 0x00010000 ) n_ringelt_bytes = 4 ; buf_len = _RLWE_DESCRIPTOR_LEN + m * n_ringelt_bytes ; if ( out == NULL || buf_len == 0 ) return buf_len ; if ( * out == NULL ) { if ( ( * out = OPENSSL_malloc ( buf_len ) ) == NULL ) { RINGLWEerr ( RINGLWE_F_I2O_RLWE_PUB , ERR_R_MALLOC_FAILURE ) ; return 0 ; } new_buffer = 1 ; } unsigned char * ptr = * out ; memcpy ( ptr , pub -> descriptor , _RLWE_DESCRIPTOR_LEN ) ; ptr += _RLWE_DESCRIPTOR_LEN ; for ( i = 0 ; i < m ; i ++ ) { if ( n_ringelt_bytes == 4 ) { * ptr ++ = ( unsigned char ) ( ( pub -> b [ i ] >> 24 ) & 0xff ) ; * ptr ++ = ( unsigned char ) ( ( pub -> b [ i ] >> 16 ) & 0xff ) ; } * ptr ++ = ( unsigned char ) ( ( pub -> b [ i ] >> 8 ) & 0xff ) ; * ptr ++ = ( unsigned char ) ( ( pub -> b [ i ] ) & 0xff ) ; } if ( ! new_buffer ) * out = ptr ; return buf_len ; }
Convert public key data structure into binary
Convert public key data structure into binary
/* out == NULL => just return the length of the octet string */ /* Copy 32-byte descriptor */ /* Copy public key data */