[erlang-questions] crypto:dh_compute_key/3 requires 128 bytes keys?

Pablo Platt pablo.platt@REDACTED
Sun Jul 10 15:46:16 CEST 2011


I'm trying to use crypto:dh_compute_key/3 to create a shared secret.
When the OthersPublicKey length is 128 bytes (without the int32 length) it's ok
but when its length is 132 I'm getting a computation_failed exception.

I don't see anything in the docs that require 128 bytes length
and the c++ implementation I'm trying to duplicate doesn't seem to require that.

Is it correct that the equivalent to the c BN_bn2bin and BN_bin2bn functions in erlang is just to append the int32 length to the binary?


Am I missing something?


Please see the following example:


OthersPublicKey = <<0,0,0,132,129,2,29,2,39,75,73,146,222,72,20,118,110,64,2,253,161,64,62,246,
  27,140,79,50,38,237,13,178,255,71,226,246,68,92,250,118,182,81,224,96,149,58,
  170,221,15,122,155,157,247,231,49,66,201,249,157,152,106,108,113,155,235,53,
  246,83,175,57,116,223,28,97,91,83,226,139,50,27,69,219,73,138,125,86,159,208,
  229,236,77,130,187,40,0,234,78,120,195,79,170,225,219,7,241,7,87,208,126,93,
  4,74,1,64,242,179,222,135,241,100,96,179,135,28,195,186,147,248,20,184,33,
  204,27,122,178,116>>,

MyPrivateKey = <<0,0,0,128,91,203,79,5,193,226,103,129,180,9,151,55,246,246,58,4,27,65,61,92,
  153,216,254,29,58,116,181,18,181,241,241,192,120,72,135,87,95,255,67,206,38,
  68,226,146,84,103,130,15,189,36,180,184,99,245,96,81,124,80,53,230,241,18,
  209,65,113,126,91,131,90,166,53,207,152,63,154,97,7,161,57,149,226,171,86,
  156,203,54,4,93,245,192,153,85,81,107,24,183,42,89,81,14,188,117,109,9,170,
  56,139,181,138,8,84,34,239,14,242,52,113,192,117,224,20,116,1,151,15,186,182,
  140>>,

DH_P = <<0,0,0,128,255,255,255,255,255,255,255,255,201,15,218,162,33,104,194,52,196,
  198,98,139,128,220,28,209,41,2,78,8,138,103,204,116,2,11,190,166,59,19,155,
  34,81,74,8,121,142,52,4,221,239,149,25,179,205,58,67,27,48,43,10,109,242,95,
  20,55,79,225,53,109,109,81,194,69,228,133,181,118,98,94,126,198,244,76,66,
  233,166,55,237,107,11,255,92,182,244,6,183,237,238,56,107,251,90,137,159,165,
  174,159,36,17,124,75,31,230,73,40,102,81,236,230,83,129,255,255,255,255,255,
  255,255,255>>,

DH_G = <<0,0,0,1,2>>,

DHParams = [DH_P, DH_G],

% getting computation_failed exception
crypto:dh_compute_key(OthersPublicKey, MyPrivateKey, DHParams).


% remove last 4 bytes and update the length from 132 to 128
OthersPublicKey2 = <<0,0,0,128,129,2,29,2,39,75,73,146,222,72,20,118,110,64,2,253,161,64,62,246,
  27,140,79,50,38,237,13,178,255,71,226,246,68,92,250,118,182,81,224,96,149,58,
  170,221,15,122,155,157,247,231,49,66,201,249,157,152,106,108,113,155,235,53,
  246,83,175,57,116,223,28,97,91,83,226,139,50,27,69,219,73,138,125,86,159,208,
  229,236,77,130,187,40,0,234,78,120,195,79,170,225,219,7,241,7,87,208,126,93,
  4,74,1,64,242,179,222,135,241,100,96,179,135,28,195,186,147,248,20,184,33,
  204>>,

% no exception
crypto:dh_compute_key(OthersPublicKey2, MyPrivateKey, DHParam




More information about the erlang-questions mailing list