Commit bfc09a75 authored by Jaromil's avatar Jaromil
Browse files

implemented ECP.hashtopoint() for Coconut

generically useful function, takes any input, hashes with SHA512 and
maps it to a big number to be placed as point on curve. uses milagro's
internal mapit() function which increments big until fitting on curve,
to be enhanced in security by re-hashing.
parent 112e591c
......@@ -17,11 +17,11 @@ LAMBDA = require('functional')
L = LAMBDA -- alias
INSIDE = require('inspect')
I = INSIDE -- alias
ECP = require('ecp')
ECP2 = require('ecp2')
FP12 = require('fp12')
BIG = require('zenroom_big')
HASH = require('zenroom_hash')
ECP = require('zenroom_ecp')
H = HASH -- alias
-- override type to recognize zenroom's types
......
local ecdh = require'ecdh'
function prepare_session(keyring, pub) -- internal function
local kr
if (type(keyring) == "zenroom.ecdh") then
kr = keyring
else
error("encrypt error: arg #1 type not known ("..type(keyring)..") expected an ECDH keyring object")
end
local pk
if (type(pub) == "zenroom.ecdh") then
pk = pub:public()
elseif (type(pub) == "zenroom.octet") then
pk = pub
else
error("encrypt error: arg #2 type not known ("..type(pub)..") expected an ECDH keyring or OCTET object")
end
return(kr:session(pk))
end
-- encrypt with default AES-GCM technique, returns base58 encoded
-- values into a table containing: .text .iv .checksum .header
function ecdh.encrypt(alice, bob, msg, header)
local key = prepare_session(alice,bob)
local iv = RNG.new():octet(16)
-- convert strings to octets
local omsg, ohead
if(type(msg) == "string") then
omsg = str(msg) else omsg = msg end
if(type(header) == "string") then
ohead = str(header) else ohead = header end
local cypher = {header = ohead, iv = iv}
cypher.text, cypher.checksum = ecdh.aead_encrypt(key,omsg,iv,ohead)
return(cypher)
end
function ecdh.decrypt(alice, bob, cypher)
local key = prepare_session(alice,bob)
local decode = {header = cypher.header}
decode.text, decode.checksum =
ecdh.aead_decrypt(key,
cypher.text,
cypher.iv,
cypher.header)
if(cypher.checksum ~= decode.checksum) then
error("decrypt error: header checksum mismatch")
end
return(decode)
end
return ecdh
local ecp = require'ecp'
function ecp.hashtopoint(s) return ecp.mapit(BIG.new(sha512(s))) end
return ecp
This diff is collapsed.
......@@ -106,8 +106,12 @@ static int newbig(lua_State *L) {
// octet argument, import
void *ud = luaL_testudata(L, 1, "zenroom.octet");
if(ud) {
big *c = big_new(L); SAFE(L);
octet *o = (octet*)ud;
if(o->len < modbytes) {
error(L,"Octet too short: %u bytes of %u minimum required", o->len, modbytes);
lerror(L,"Cannot create a BIG number.");
return 0; }
big *c = big_new(L); SAFE(L);
BIG_fromBytesLen(c->val, o->val, o->len);
return 1; }
// number argument, import
......@@ -120,7 +124,7 @@ static int newbig(lua_State *L) {
BIG_norm(c->val);
return 1; }
error(L,"octet or number argument expected");
lerror(L,"octet or number argument expected");
return 0;
}
......
......@@ -434,6 +434,7 @@ int luaopen_ecp(lua_State *L) {
{"inf",ecp_get_infinity},
{"infinity",ecp_get_infinity},
{"order",ecp_order},
{"mapit",ecp_mapit},
{"generator",ecp_generator},
{"G",ecp_generator},
{NULL,NULL}};
......@@ -443,7 +444,6 @@ int luaopen_ecp(lua_State *L) {
{"double",ecp_double},
{"isinf",ecp_isinf},
{"isinfinity",ecp_isinf},
{"mapit",ecp_mapit},
{"octet",ecp_octet},
{"add",ecp_add},
{"x",ecp_get_x},
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment