Skip to content

Commit 622ffd5

Browse files
committedNov 7, 2018
add basic import support to compiler; make aead pass checker
1 parent cd66bde commit 622ffd5

File tree

7 files changed

+52
-20
lines changed

7 files changed

+52
-20
lines changed
 

‎Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
PYTHON?=python3.6
33

44
# hacspecs passing compiler checks
5-
FINISHED_SPECS=poly1305 chacha20 curve25519 curve448 aes
5+
FINISHED_SPECS=poly1305 chacha20 aead_chacha20poly1305 curve25519 curve448 \
6+
aes gf128 aead_aes128gcm
67

78
# hacspecs that pass all python checks and can be used in any make target.
8-
SPECS=$(FINISHED_SPECS) aead_chacha20poly1305 sha2 sha3 \
9-
ed25519 p256 gf128 aead_aes128gcm rsapss blake2
9+
SPECS=$(FINISHED_SPECS) sha2 sha3 ed25519 p256 rsapss blake2
1010

1111
# Don't use these in test. They take too long.
1212
SLOW_SPECS=wots kyber

‎compiler/fstar-compiler/specs/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ FSTAR_HOME?=../../../../FStar
22
HACL_HOME?=../../../../hacl-star
33
FSTAR=$(FSTAR_HOME)/bin/fstar.exe --include $(HACL_HOME)/lib --include $(HACL_HOME)/lib/fst --expose_interfaces
44

5-
SPECS=aes.fst gf128.fst chacha20.fst poly1305.fst curve25519.fst curve448.fst
5+
SPECS=aes.fst gf128.fst aead_chacha20poly1305.fst chacha20.fst poly1305.fst \
6+
curve25519.fst curve448.fst
67
BROKEN_SPECS=blake2.fst
78
LIBS = Lib.IntTypes.fst Lib.RawIntTypes.fst Lib.Sequence.fst Lib.ByteSequence.fst
89
LIBS_FILES= $(addprefix $(HACL_HOME)/lib/fst/, $(LIBS)) speclib.fst

‎compiler/fstar-compiler/to_fstar.ml

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ let main () =
1414
let modulename = Filename.remove_extension (Filename.basename filename) in
1515
try
1616
let env =
17-
(* T.tt_interface T.Env.empty *)
1817
let stream = P.from_file (C.Resource.getlib "speclib.pyi") in
1918
let speclib = P.parse_intf stream in
2019
T.tt_interface T.Env.empty speclib

‎compiler/libs/hacs/core.ml

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ module Resource : sig
5454
val root : string
5555
val get : string -> string
5656
val getlib : string -> string
57+
val getspec: string -> string
5758
end = struct
5859
let root : string =
5960
let mydir = Filename.dirname Sys.executable_name in
@@ -68,6 +69,9 @@ end = struct
6869

6970
let getlib (name : string) =
7071
get (Format.sprintf "../lib/%s" name)
72+
73+
let getspec (name : string) =
74+
get (Format.sprintf "../specs/%s" name)
7175
end
7276

7377
(* -------------------------------------------------------------------- *)

‎compiler/libs/hacs/typing.ml

+30-7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ let stdlib =
6565
((["bytes"],"from_uint128_be"), ([Some (`Exact (tword128))], (fs1 (fun _aty -> bytes_t)), Ident.make "bytes_from_uint128_be"));
6666
((["bytes"],"to_uint64_le"), ([Some (`Exact bytes_t)], (fs1 (fun _aty -> tword64)), Ident.make "bytes_to_uint64_le"));
6767
((["bytes"],"from_uint64_le"), ([Some (`Exact (tword64))], (fs1 (fun _aty -> bytes_t)), Ident.make "bytes_from_uint64_le"));
68+
((["bytes"],"from_uint64_be"), ([Some (`Exact (tword64))], (fs1 (fun _aty -> bytes_t)), Ident.make "bytes_from_uint64_be"));
6869

6970
(* (([],"natmod"), ([Some (`Approx PInt); Some (`Approx PInt)], (fun [aty;bty] -> TInt (`Natm (EUInt Big_int.zero))), Ident.make "natmod"));*)
7071
((["natmod"],"to_nat"), ([Some (`Approx PInt)], (fs1 (fun _aty -> nat_t)), Ident.make "natmod_to_nat"));
@@ -167,6 +168,7 @@ module Env : sig
167168
module Procs : sig
168169
val get : env -> symbol -> pdecl option
169170
val bind : env -> symbol -> hotype list * type_ -> env * ident
171+
val add : env -> pdecl -> env
170172
end
171173
end = struct
172174
type tdecl = { tname : ident; tdef : type_; }
@@ -372,6 +374,8 @@ end = struct
372374
let decl = { pname = Ident.make f; psig = sig_; pret = ret } in
373375
let env = { env with e_procs = Mstr.add f decl env.e_procs } in
374376
(env, decl.pname)
377+
let add (env : env) (p : pdecl) =
378+
{ env with e_procs = Mstr.add (Ast.Ident.to_string p.pname) p env.e_procs }
375379
end
376380

377381
let empty =
@@ -1009,8 +1013,27 @@ and tt_module (env : env) (nm : pident list) =
10091013
in resolve (env, []) nm
10101014

10111015
(* -------------------------------------------------------------------- *)
1012-
and tt_import (env : env) (_ : pimport) =
1013-
env
1016+
and tt_import (e : env) (imp : pimport) =
1017+
let impmod, impdec = imp in
1018+
let impmodpath, impmod2 = impmod in
1019+
if (unloc impmod2) = "speclib" then
1020+
e
1021+
else (
1022+
(* TODO: use path to read import *)
1023+
let _ = (String.concat " " (List.map unloc impmodpath)) in
1024+
let importeddeclarations = (List.map (fun x ->
1025+
match x with | None -> "" | Some y -> unloc y) (Option.get impdec)) in
1026+
let (pi : Env.env * Env.env Ast.program) =
1027+
let stream = Reader.from_file (Resource.getspec (unloc impmod2 ^ ".py")) in
1028+
let past = Reader.parse_spec stream in
1029+
tt_program e past
1030+
in
1031+
let new_env, _ = pi in
1032+
let newdecls = List.map (fun d ->
1033+
Env.Procs.get new_env d) importeddeclarations in
1034+
let newdecls = List.map Option.get newdecls in
1035+
List.fold_left Env.Procs.add e newdecls
1036+
)
10141037

10151038
(* -------------------------------------------------------------------- *)
10161039
and tt_tydecl (env : env) ((x, ty) : pident * pexpr) : env * tydecl =
@@ -1054,7 +1077,7 @@ and tt_annotation (env : env) (att : pexpr) : ident * hoexpr list =
10541077
| PEVar ([],x) when unloc x = "typechecked" ->
10551078
(Ident.make "typechecked", [])
10561079
| PECall (x, args) when qunloc x = ([],"contract3") || qunloc x = ([],"contract") ->
1057-
let es, tys = List.split (List.map (tt_expr env) args) in
1080+
let es, _ = List.split (List.map (tt_expr env) args) in
10581081
(Ident.make "contract3", List.map (fun x -> `Expr x) es)
10591082
| _ ->
10601083
error ~loc:(loc att) env UnsupportedAnnotation
@@ -1124,7 +1147,7 @@ and tt_procdef (env : env) (pf : pprocdef) : env * env procdef =
11241147
in (env, aout)
11251148

11261149
(* -------------------------------------------------------------------- *)
1127-
let tt_topdecl1 (env : env) = function
1150+
and tt_topdecl1 (env : env) = function
11281151
| PTImport info ->
11291152
(tt_import env info, [])
11301153

@@ -1138,7 +1161,7 @@ let tt_topdecl1 (env : env) = function
11381161
let env, x = tt_vardecl env ((x, ty), e) in (env, [TD_VarDecl x])
11391162

11401163
(* -------------------------------------------------------------------- *)
1141-
let tt_intf1 (env : env) = function
1164+
and tt_intf1 (env : env) = function
11421165
| IPTTypeAlias (x, ty) ->
11431166
let env, _ = tt_tydecl env (x, ty) in env
11441167

@@ -1158,10 +1181,10 @@ let tt_intf1 (env : env) = function
11581181
else Env.Mod.bindnm env (nmunloc nm) senv
11591182

11601183
(* -------------------------------------------------------------------- *)
1161-
let tt_interface (env : env) (i : pintf) : env =
1184+
and tt_interface (env : env) (i : pintf) : env =
11621185
List.fold_left tt_intf1 env i
11631186

11641187
(* -------------------------------------------------------------------- *)
1165-
let tt_program (env : env) (p : pspec) : env * env program =
1188+
and tt_program (env : env) (p : pspec) : env * env program =
11661189
let env, prgm = List.fold_left_map tt_topdecl1 env p in
11671190
(env, List.flatten prgm)

‎specs/aead_aes128gcm.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77
key_t = bytes_t(16)
88
nonce_t = bytes_t(12)
99
tag_t = bytes_t(16)
10+
block_t = bytes_t(16)
1011

1112
@typechecked
1213
def padded_aad_msg(aad:vlbytes_t,msg:vlbytes_t) -> tuple_t(int,vlbytes_t):
13-
laad : int = len(aad)
14-
lmsg : int = len(msg)
14+
laad : int = array.length(aad)
15+
lmsg : int = array.length(msg)
1516
pad_aad : int = laad
1617
if laad % 16 > 0:
1718
pad_aad = laad + (16 - (laad % 16))
1819
pad_msg : int = lmsg
1920
if lmsg % 16 > 0:
2021
pad_msg = lmsg + (16 - (lmsg % 16))
21-
to_mac = bytes(array.create(pad_aad + pad_msg + 16,uint8(0)))
22+
to_mac : vlbytes_t = bytes(array.create(pad_aad + pad_msg + 16,uint8(0)))
2223
to_mac[0:laad] = aad
2324
to_mac[pad_aad:pad_aad+lmsg] = msg
2425
to_mac[pad_aad+pad_msg:pad_aad+pad_msg+8] = bytes.from_uint64_be(uint64(laad * 8))
@@ -27,24 +28,26 @@ def padded_aad_msg(aad:vlbytes_t,msg:vlbytes_t) -> tuple_t(int,vlbytes_t):
2728

2829
@typechecked
2930
def aead_aes128gcm_encrypt(key:key_t,nonce:nonce_t,aad:vlbytes_t,msg:vlbytes_t) -> tuple_t(vlbytes_t,tag_t):
30-
nonce0 = bytes(array.create(12,uint8(0)))
31+
nonce0 : vlbytes_t = bytes(array.create(12,uint8(0)))
3132
mac_key : block_t = aes128_ctr_keyblock(key,nonce0,uint32(0))
3233
tag_mix : block_t = aes128_ctr_keyblock(key,nonce,uint32(1))
3334
ciphertext : vlbytes_t = aes128_encrypt(key,nonce,uint32(2),msg)
3435
to_mac : vlbytes_t
35-
_, to_mac = padded_aad_msg(aad,ciphertext)
36+
l: int # TODO: unused variable but _ is not supported by the compiler yet.
37+
l, to_mac = padded_aad_msg(aad,ciphertext)
3638
mac : tag_t = gmac(to_mac,mac_key)
3739
mac = xor_block(mac,tag_mix)
3840
return ciphertext, mac
3941

4042
@typechecked
4143
def aead_aes128gcm_decrypt(key:key_t,nonce:nonce_t,aad:vlbytes_t,
4244
ciphertext:vlbytes_t,tag:tag_t) -> vlbytes_t:
43-
nonce0 = bytes(array.create(12,uint8(0)))
45+
nonce0 : vlbytes_t = bytes(array.create(12,uint8(0)))
4446
mac_key : block_t = aes128_ctr_keyblock(key,nonce0,uint32(0))
4547
tag_mix : block_t = aes128_ctr_keyblock(key,nonce,uint32(1))
4648
to_mac : vlbytes_t
47-
_, to_mac = padded_aad_msg(aad,ciphertext)
49+
l: int # TODO: unused variable but _ is not supported by the compiler yet.
50+
l, to_mac = padded_aad_msg(aad,ciphertext)
4851
mac : tag_t = gmac(to_mac,mac_key)
4952
mac = xor_block(mac,tag_mix)
5053
if mac == tag:

‎specs/aead_chacha20poly1305.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
key_t = bytes_t(32)
88
nonce_t = bytes_t(12)
99
tag_t = bytes_t(16)
10+
block_t = bytes_t(64)
1011

1112
@typechecked
1213
def padded_aad_msg(aad:vlbytes_t,msg:vlbytes_t) -> tuple_t(int,vlbytes_t):
@@ -44,7 +45,8 @@ def aead_chacha20poly1305_decrypt(key:key_t,nonce:nonce_t,
4445
keyblock0: block_t = chacha20_block(key, uint32(0), nonce)
4546
mac_key: bytes_t = keyblock0[0:32]
4647
to_mac: vlbytes_t
47-
_, to_mac = padded_aad_msg(aad,ciphertext)
48+
l: int # TODO: unused variable but _ is not supported by the compiler yet.
49+
l, to_mac = padded_aad_msg(aad,ciphertext)
4850
mac: tag_t = poly1305_mac(to_mac,mac_key)
4951
if mac == tag:
5052
msg: vlbytes_t = chacha20_decrypt(key, uint32(1), nonce, ciphertext)

0 commit comments

Comments
 (0)
Please sign in to comment.