crypto/ringlwe/ringlwe_key.c
/* Convert public key data structure from binary */
Convert public key data structure from binary
Convert public key data structure from binary
RLWE_PUB *o2i_RLWE_PUB(RLWE_PUB **pub, const unsigned char *in, size_t len) {
  size_t i;
  if ((pub == NULL) || (in == NULL)) {
    RINGLWEerr(RINGLWE_F_O2I_RLWE_PUB, ERR_R_PASSED_NULL_PARAMETER);
    return 0;
  }
  if (len < _RLWE_DESCRIPTOR_LEN) {
    RINGLWEerr(RINGLWE_F_O2I_RLWE_PUB, RINGLWE_R_INVALID_FORMAT);
    return 0;
  }
  const unsigned char *descriptor = in;
  int nid;
  nid  = RLWE_get_nid_from_descriptor(descriptor);
  if (nid == 0) {
    RINGLWEerr(RINGLWE_F_O2I_RLWE_PUB, RINGLWE_R_PARAM_UNKNOWN);
    return 0;
  }
  RLWE_CTX *ctx;
  ctx = RLWE_CTX_new(nid);
  if (ctx == NULL) {
    RINGLWEerr(RINGLWE_F_O2I_RLWE_PUB, ERR_R_MALLOC_FAILURE);
    return 0;
  }
  uint32_t m = ctx->param_data->m;
  RINGELT  q = ctx->param_data->q;
  uint32_t n_ringelt_bytes = 2;
  if (q >= 0x00010000)
    n_ringelt_bytes = 4;
  if (len != _RLWE_DESCRIPTOR_LEN + m * n_ringelt_bytes) {
    RLWE_CTX_free(ctx);
    RINGLWEerr(RINGLWE_F_O2I_RLWE_PUB, RINGLWE_R_INVALID_FORMAT);
    return 0;
  }
  RLWE_PUB *pub_key;
  if (*pub != NULL) {
    if (memcmp(ctx->param_data, (*pub)->param_data, sizeof(RINGLWE_PARAM_DATA))) {
      RLWE_CTX_free(ctx);
      RINGLWEerr(RINGLWE_F_O2I_RLWE_PUB, RINGLWE_R_PARAM_INVALID);
      return  0;
    }
    RLWE_CTX_free(ctx);
    pub_key = *pub;
  }
  else {
    pub_key = RLWE_PUB_new(ctx);
    RLWE_CTX_free(ctx);
    if (pub_key == NULL) {
      RINGLWEerr(RINGLWE_F_O2I_RLWE_PUB, ERR_R_MALLOC_FAILURE);
      return 0;
    }
  }
  const unsigned char *ptr = (const unsigned char *)in + _RLWE_DESCRIPTOR_LEN;
  for (i = 0; i < m; i++) {
    pub_key->b[i] = 0;
    if (n_ringelt_bytes == 4) {
      pub_key->b[i] |= ((RINGELT)*ptr++) << 24;
      pub_key->b[i] |= ((RINGELT)*ptr++) << 16; }
    pub_key->b[i] |= ((RINGELT)*ptr++) << 8;
    pub_key->b[i] |= (RINGELT)*ptr++;
  }
  *pub = pub_key;
  return *pub;
}
RLWE_PUB * o2i_RLWE_PUB ( RLWE_PUB * * pub , const unsigned char * in , size_t len ) { size_t i ; if ( ( pub == NULL ) || ( in == NULL ) ) { RINGLWEerr ( RINGLWE_F_O2I_RLWE_PUB , ERR_R_PASSED_NULL_PARAMETER ) ; return 0 ; } if ( len < _RLWE_DESCRIPTOR_LEN ) { RINGLWEerr ( RINGLWE_F_O2I_RLWE_PUB , RINGLWE_R_INVALID_FORMAT ) ; return 0 ; } const unsigned char * descriptor = in ; int nid ; nid = RLWE_get_nid_from_descriptor ( descriptor ) ; if ( nid == 0 ) { RINGLWEerr ( RINGLWE_F_O2I_RLWE_PUB , RINGLWE_R_PARAM_UNKNOWN ) ; return 0 ; } RLWE_CTX * ctx ; ctx = RLWE_CTX_new ( nid ) ; if ( ctx == NULL ) { RINGLWEerr ( RINGLWE_F_O2I_RLWE_PUB , ERR_R_MALLOC_FAILURE ) ; return 0 ; } uint32_t m = ctx -> param_data -> m ; RINGELT q = ctx -> param_data -> q ; uint32_t n_ringelt_bytes = 2 ; if ( q >= 0x00010000 ) n_ringelt_bytes = 4 ; if ( len != _RLWE_DESCRIPTOR_LEN + m * n_ringelt_bytes ) { RLWE_CTX_free ( ctx ) ; RINGLWEerr ( RINGLWE_F_O2I_RLWE_PUB , RINGLWE_R_INVALID_FORMAT ) ; return 0 ; } RLWE_PUB * pub_key ; if ( * pub != NULL ) { if ( memcmp ( ctx -> param_data , ( * pub ) -> param_data , sizeof ( RINGLWE_PARAM_DATA ) ) ) { RLWE_CTX_free ( ctx ) ; RINGLWEerr ( RINGLWE_F_O2I_RLWE_PUB , RINGLWE_R_PARAM_INVALID ) ; return 0 ; } RLWE_CTX_free ( ctx ) ; pub_key = * pub ; } else { pub_key = RLWE_PUB_new ( ctx ) ; RLWE_CTX_free ( ctx ) ; if ( pub_key == NULL ) { RINGLWEerr ( RINGLWE_F_O2I_RLWE_PUB , ERR_R_MALLOC_FAILURE ) ; return 0 ; } } const unsigned char * ptr = ( const unsigned char * ) in + _RLWE_DESCRIPTOR_LEN ; for ( i = 0 ; i < m ; i ++ ) { pub_key -> b [ i ] = 0 ; if ( n_ringelt_bytes == 4 ) { pub_key -> b [ i ] |= ( ( RINGELT ) * ptr ++ ) << 24 ; pub_key -> b [ i ] |= ( ( RINGELT ) * ptr ++ ) << 16 ; } pub_key -> b [ i ] |= ( ( RINGELT ) * ptr ++ ) << 8 ; pub_key -> b [ i ] |= ( RINGELT ) * ptr ++ ; } * pub = pub_key ; return * pub ; }
Convert public key data structure from binary
Convert public key data structure from binary
/* Read 32-byte descriptor and get parameters */ /* Get public key data from binary into structure */