crypto/ringlwe/ringlwe_key.c
/* Convert private key data structure from binary */
Convert private key data structure from binary
Convert private key data structure from binary
RLWE_PAIR *o2i_RLWE_SEC(RLWE_PAIR **pair, const unsigned char *in, size_t len) {
  size_t i;
  if ((pair == NULL) || (in == NULL)) {
    RINGLWEerr(RINGLWE_F_O2I_RLWE_SEC, ERR_R_PASSED_NULL_PARAMETER);
    return 0;
  }
  if (len < _RLWE_DESCRIPTOR_LEN) {
    RINGLWEerr(RINGLWE_F_O2I_RLWE_SEC, 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_SEC, RINGLWE_R_PARAM_UNKNOWN);
    return 0;
  }
  RLWE_CTX *ctx;
  ctx = RLWE_CTX_new(nid);
  if (ctx == NULL) {
    RINGLWEerr(RINGLWE_F_O2I_RLWE_SEC, 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_SEC, RINGLWE_R_INVALID_FORMAT);
    return 0;
  }
  RLWE_PAIR *key_pair;
  if (*pair != NULL) {
    if (memcmp(ctx->param_data, (*pair)->param_data, sizeof(RINGLWE_PARAM_DATA))) {
      RLWE_CTX_free(ctx);
      RINGLWEerr(RINGLWE_F_O2I_RLWE_SEC, RINGLWE_R_PARAM_INVALID);
      return  0;
    }
    RLWE_CTX_free(ctx);
    key_pair = *pair;
  }
  else {
    key_pair = RLWE_PAIR_new(ctx);
    RLWE_CTX_free(ctx);
    if (key_pair == NULL) {
      RINGLWEerr(RINGLWE_F_O2I_RLWE_SEC, ERR_R_MALLOC_FAILURE);
      return 0;
    }
  }
  const unsigned char *ptr = (const unsigned char *)in + _RLWE_DESCRIPTOR_LEN;
  for (i = 0; i < m; i++) {
    key_pair->s[m+i] = 0;
    if (n_ringelt_bytes == 4) {
      key_pair->s[m+i] |= ((RINGELT)*ptr++) << 24;
      key_pair->s[m+i] |= ((RINGELT)*ptr++) << 16; }
    key_pair->s[m+i] |= ((RINGELT)*ptr++) << 8;
    key_pair->s[m+i] |= (RINGELT)*ptr++;
  }
  *pair = key_pair;
  return *pair;
}
RLWE_PAIR * o2i_RLWE_SEC ( RLWE_PAIR * * pair , const unsigned char * in , size_t len ) { size_t i ; if ( ( pair == NULL ) || ( in == NULL ) ) { RINGLWEerr ( RINGLWE_F_O2I_RLWE_SEC , ERR_R_PASSED_NULL_PARAMETER ) ; return 0 ; } if ( len < _RLWE_DESCRIPTOR_LEN ) { RINGLWEerr ( RINGLWE_F_O2I_RLWE_SEC , 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_SEC , RINGLWE_R_PARAM_UNKNOWN ) ; return 0 ; } RLWE_CTX * ctx ; ctx = RLWE_CTX_new ( nid ) ; if ( ctx == NULL ) { RINGLWEerr ( RINGLWE_F_O2I_RLWE_SEC , 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_SEC , RINGLWE_R_INVALID_FORMAT ) ; return 0 ; } RLWE_PAIR * key_pair ; if ( * pair != NULL ) { if ( memcmp ( ctx -> param_data , ( * pair ) -> param_data , sizeof ( RINGLWE_PARAM_DATA ) ) ) { RLWE_CTX_free ( ctx ) ; RINGLWEerr ( RINGLWE_F_O2I_RLWE_SEC , RINGLWE_R_PARAM_INVALID ) ; return 0 ; } RLWE_CTX_free ( ctx ) ; key_pair = * pair ; } else { key_pair = RLWE_PAIR_new ( ctx ) ; RLWE_CTX_free ( ctx ) ; if ( key_pair == NULL ) { RINGLWEerr ( RINGLWE_F_O2I_RLWE_SEC , ERR_R_MALLOC_FAILURE ) ; return 0 ; } } const unsigned char * ptr = ( const unsigned char * ) in + _RLWE_DESCRIPTOR_LEN ; for ( i = 0 ; i < m ; i ++ ) { key_pair -> s [ m + i ] = 0 ; if ( n_ringelt_bytes == 4 ) { key_pair -> s [ m + i ] |= ( ( RINGELT ) * ptr ++ ) << 24 ; key_pair -> s [ m + i ] |= ( ( RINGELT ) * ptr ++ ) << 16 ; } key_pair -> s [ m + i ] |= ( ( RINGELT ) * ptr ++ ) << 8 ; key_pair -> s [ m + i ] |= ( RINGELT ) * ptr ++ ; } * pair = key_pair ; return * pair ; }
Convert private key data structure from binary
Convert private key data structure from binary
/* Read 32-byte descriptor and get parameters */ /* Get public key data from binary into structure */