title: How to convert a string to a long with openssl bignum in C?
body: <p>I am trying to port this function from Python to C:</p>
  
  <pre><code>import binascii
  from Crypto.Util.number import bytes_to_long
  
  # converts from a string to a long
  def _strtoLong(s):
      s = bytes("".join(s.split()), "UTF-8")
      s = binascii.a2b_hex(s)
      return bytes_to_long(s)
  </code></pre>
  
  <p>The string is very long. The resulting number is  2048 bits I believe. I am trying to port this to C using openssl bn structure, but I can't seem to get the correct ouput. Here is the output of each step from the python function, printed as a string representation:</p>
  
  <pre><code>bytes: b'B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C69A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C013ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD7098488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708DF1FB2BC2E4A4371'
  
  a2b_hex: b'\xb1\x0b\x8f\x96\xa0\x80\xe0\x1d\xde\x92\xde^\xae]T\xecR\xc9\x9f\xbc\xfb\x06\xa3\xc6\x9aj\x9d\xcaR\xd2;a`s\xe2\x86u\xa2=\x18\x988\xef\x1e.\xe6R\xc0\x13\xec\xb4\xae\xa9\x06\x11#$\x97\\&lt;\xd4\x9b\x83\xbf\xac\xcb\xdd}\x90\xc4\xbdp\x98H\x8e\x9c!\x9asrN\xff\xd6\xfa\xe5dG8\xfa\xa3\x1aO\xf5[\xcc\xc0\xa1Q\xaf_\r\xc8\xb4\xbdE\xbf7\xdf6\\\x1ae\xe6\x8c\xfd\xa7mM\xa7\x08\xdf\x1f\xb2\xbc.JCq'
  
  bytes_to_long: 124325339146889384540494091085456630009856882741872806181731279018491820800119460022367403769795008250021191767583423221479185609066059226301250167164084041279837566626881119772675984258163062926954046545485368458404445166682380071370274810671501916789361956272226105723317679562001235501455748016154805420913
  </code></pre>
  
  <p>In C I am doing:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;openssl/bn.h&gt;
  
  int char2int(unsigned char input)
  {
      if(input &gt;= '0' &amp;&amp; input &lt;= '9')
          return input - '0';
      if(input &gt;= 'A' &amp;&amp; input &lt;= 'F')
          return input - 'A' + 10;
      if(input &gt;= 'a' &amp;&amp; input &lt;= 'f')
          return input - 'a' + 10;
      return -1;
  }
  
  // This function assumes src to be a zero terminated sanitized string with
  // an even number of [0-9a-f] characters, and target to be sufficiently large
  void hex2bin(const unsigned char* src, unsigned char* target)
  {
      while(*src &amp;&amp; src[1])
      {
          *(target++) = char2int(*src)*16 + char2int(src[1]);
          src += 2;
      }
  }
  
  
  int main()
  {
      unsigned char buf[512] = "B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C6"
                      "9A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C0"
                      "13ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD70"
                      "98488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0"
                      "A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708"
                      "DF1FB2BC2E4A4371";
      unsigned char P[2049], val[256];
      int i;
      int len = 256;
      for(i=0; i&lt;len; i++){
          P[i] = buf[i];
      }
  
      fprintf(stderr, "\nbytes=%s\n", P);
  
      hex2bin(P, val);
      fprintf(stderr, "\nhex2bin= ");
      for(i=0; i&lt;len/2; i++)
          fprintf(stderr, "%02x", val[i]);
      fprintf(stderr, "\n");
  
      BIGNUM *bn = BN_new();    
      BN_bin2bn(val, (int)len/2, bn);
  
  
      fprintf(stderr, "\nbn=");
      BN_print_fp(stdout, bn);
  
      char *str = BN_bn2hex(bn);
      printf("\n\nbn2hex: %s\n", str);
      free(str);
      BN_free(bn);
  
      return 0;
  }
  </code></pre>
  
  <p>This is what it prints out:</p>
  
  <pre><code>matthew@matthew-HP-15-Notebook-PC:~/Desktop/crypto/prog$ make
  clang -Wall -I../tomsfastmath/src/headers/ -I../openssl/include main.c ../tomsfastmath/libtfm.a ../openssl/libcrypto.a -o bignum
  matthew@matthew-HP-15-Notebook-PC:~/Desktop/crypto/prog$ ./bignum 
  
  bytes=B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C69A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C013ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD7098488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708DF1FB2BC2E4A4371
  
  hex2bin= b10b8f96a080e01dde92de5eae5d54ec52c99fbcfb06a3c69a6a9dca52d23b616073e28675a23d189838ef1e2ee652c013ecb4aea906112324975c3cd49b83bfaccbdd7d90c4bd7098488e9c219a73724effd6fae5644738faa31a4ff55bccc0a151af5f0dc8b4bd45bf37df365c1a65e68cfda76d4da708df1fb2bc2e4a4371
  
  bn=B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C69A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C013ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD7098488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708DF1FB2BC2E4A4371
  
  bn2hex: B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C69A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C013ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD7098488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708DF1FB2BC2E4A4371
  </code></pre>
  
  <p>Any ideas how to convert the bn to a long the same as the python?</p>