Differences From
Artifact [bf3957f4f4]:
34 34 sha384 = `hashalg {id = lib.md.MBEDTLS_MD_SHA384; bytes = m.algsz.sha384};
35 35 sha224 = `hashalg {id = lib.md.MBEDTLS_MD_SHA224; bytes = m.algsz.sha224};
36 36 -- md5 = {id = lib.md.MBEDTLS_MD_MD5};-- !!!
37 37 };
38 38 local callbacks = {}
39 39 if config.feat.randomizer == 'kern' then
40 40 local rnd = terralib.externfunction('getrandom', {&opaque, intptr, uint} -> ptrdiff);
41 - terra callbacks.randomize(ctx: &opaque, dest: &uint8, sz: intptr): int
41 + terra m.spray(dest: &uint8, sz: intptr): int
42 42 return rnd(dest, sz, 0)
43 43 end
44 44 elseif config.feat.randomizer == 'devfs' then
45 - terra callbacks.randomize(ctx: &opaque, dest: &uint8, sz: intptr): int
45 + terra m.spray(dest: &uint8, sz: intptr): int
46 46 var gen = lib.io.open("/dev/urandom",0)
47 47 lib.io.read(gen, dest, sz)
48 48 lib.io.close(gen)
49 49 return sz
50 50 end
51 51 elseif config.feat.randomizer == 'libc' then
52 52 local rnd = terralib.externfunction('rand', {} -> int);
53 53 local srnd = terralib.externfunction('srand', uint -> int);
54 54 local time = terralib.includec 'time.h'
55 55 lib.init[#lib.init + 1] = quote srnd(time.time(nil)) end
56 56 print '(warn) using libc soft-rand function for cryptographic purposes, this is very bad!'
57 - terra callbacks.randomize(ctx: &opaque, dest: &uint8, sz: intptr): int
57 + terra m.spray(dest: &uint8, sz: intptr): int
58 58 for i=0,sz do dest[i] = [uint8](rnd()) end
59 59 return sz
60 60 end
61 61 end
62 +
63 +m.random = macro(function(typ, from, to)
64 + local ty = typ:astype()
65 + return quote
66 + var v: ty
67 + m.spray([&uint8](&v), sizeof(ty))
68 + v = v % (to - from) + from -- only works with unsigned!!
69 + in v end
70 +end)
71 +
72 +terra callbacks.randomize(ctx: &opaque, dest: &uint8, sz: intptr)
73 + return m.spray(dest,sz) end
62 74
63 75 terra m.pem(pub: bool, key: &ctx, buf: &uint8): bool
64 76 if pub then
65 77 return lib.pk.mbedtls_pk_write_pubkey_pem(key, buf, const.maxpemsz) == 0
66 78 else
67 79 return lib.pk.mbedtls_pk_write_key_pem(key, buf, const.maxpemsz) == 0
68 80 end