Add length parameter to AES CTR modes

This commit is contained in:
Raymond Pasco 2016-08-12 03:00:37 -07:00
parent b17d033e84
commit 38ff23eb6b

View File

@ -430,7 +430,7 @@
::
++ ctra :: AES-128 CTR
~% %ctra +> ~
|_ {key/@H mod/bloq ctr/@H}
|_ {key/@H mod/bloq len/@ ctr/@H}
++ en
~/ %en
|= txt/@
@ -438,10 +438,9 @@
=/ encrypt ~(en ecba key)
=/ blocks (met 7 txt)
=. blocks ?:(=(0 blocks) 1 blocks)
=/ bytes (met 3 txt)
=. bytes ?:(=(0 bytes) 1 bytes)
?> (gte len (met 3 txt))
%+ mix txt
%^ rsh 3 (sub (mul 16 blocks) bytes)
%^ rsh 3 (sub (mul 16 blocks) len)
%+ rep 7
%- flop :: stupid backwards AES
|- ^- (list @ux)
@ -453,7 +452,7 @@
::
++ ctrb :: AES-192 CTR
~% %ctrb +> ~
|_ {key/@I mod/bloq ctr/@H}
|_ {key/@I mod/bloq len/@ ctr/@H}
++ en
~/ %en
|= txt/@
@ -461,10 +460,9 @@
=/ encrypt ~(en ecbb key)
=/ blocks (met 7 txt)
=. blocks ?:(=(0 blocks) 1 blocks)
=/ bytes (met 3 txt)
=. bytes ?:(=(0 bytes) 1 bytes)
?> (gte len (met 3 txt))
%+ mix txt
%^ rsh 3 (sub (mul 16 blocks) bytes)
%^ rsh 3 (sub (mul 16 blocks) len)
%+ rep 7
%- flop :: stupid backwards AES
|- ^- (list @ux)
@ -476,7 +474,7 @@
::
++ ctrc :: AES-256 CTR
~% %ctrc +> ~
|_ {key/@I mod/bloq ctr/@H}
|_ {key/@I mod/bloq len/@ ctr/@H}
++ en
~/ %en
|= txt/@
@ -484,10 +482,9 @@
=/ encrypt ~(en ecbc key)
=/ blocks (met 7 txt)
=. blocks ?:(=(0 blocks) 1 blocks)
=/ bytes (met 3 txt)
=. bytes ?:(=(0 bytes) 1 bytes)
?> (gte len (met 3 txt))
%+ mix txt
%^ rsh 3 (sub (mul 16 blocks) bytes)
%^ rsh 3 (sub (mul 16 blocks) len)
%+ rep 7
%- flop :: stupid backwards AES
|- ^- (list @ux)
@ -656,19 +653,21 @@
++ en
~/ %en
|= txt/@
^- (pair @uxH @ux)
^- (trel @uxH @ud @ux)
=+ [k1=(rsh 7 1 key) k2=(end 7 1 key)]
=+ iv=(s2va k1 (weld vec (limo ~[txt])))
:-
=+ len=(met 3 txt)
:+
iv
(~(en ctra k2 7 (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)) txt)
len
(~(en ctra k2 7 len (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)) txt)
++ de
~/ %de
|= {iv/@H txt/@}
|= {iv/@H len/@ txt/@}
^- (unit @ux)
=+ [k1=(rsh 7 1 key) k2=(end 7 1 key)]
=+ ^= pln
(~(de ctra k2 7 (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)) txt)
(~(de ctra k2 7 len (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)) txt)
?. =((s2va k1 (weld vec (limo ~[pln]))) iv)
~
`pln
@ -680,19 +679,21 @@
++ en
~/ %en
|= txt/@
^- (pair @uxH @ux)
^- (trel @uxH @ud @ux)
=+ [k1=(rsh 5 3 key) k2=(end 5 3 key)]
=+ iv=(s2vb k1 (weld vec (limo ~[txt])))
:-
=+ len=(met 3 txt)
:+
iv
(~(en ctrb k2 7 (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)) txt)
len
(~(en ctrb k2 7 len (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)) txt)
++ de
~/ %de
|= {iv/@H txt/@}
|= {iv/@H len/@ txt/@}
^- (unit @ux)
=+ [k1=(rsh 5 3 key) k2=(end 5 3 key)]
=+ ^= pln
(~(de ctrb k2 7 (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)) txt)
(~(de ctrb k2 7 len (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)) txt)
?. =((s2vb k1 (weld vec (limo ~[pln]))) iv)
~
`pln
@ -704,19 +705,21 @@
++ en
~/ %en
|= txt/@
^- (pair @uxH @ux)
^- (trel @uxH @ud @ux)
=+ [k1=(rsh 8 1 key) k2=(end 8 1 key)]
=+ iv=(s2vc k1 (weld vec (limo ~[txt])))
:-
=+ len=(met 3 txt)
:+
iv
(~(en ctrc k2 7 (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)) txt)
len
(~(en ctrc k2 7 len (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)) txt)
++ de
~/ %de
|= {iv/@H txt/@}
|= {iv/@H len/@ txt/@}
^- (unit @ux)
=+ [k1=(rsh 8 1 key) k2=(end 8 1 key)]
=+ ^= pln
(~(de ctrc k2 7 (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)) txt)
(~(de ctrc k2 7 len (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)) txt)
?. =((s2vc k1 (weld vec (limo ~[pln]))) iv)
~
`pln
@ -948,24 +951,26 @@
?> =('b' (end 3 1 bpk))
=+ pk=(rsh 8 1 (rsh 3 1 bpk))
=+ shar=(shax (shar:ed pk cry.u.sek))
=+ ((hard {iv/@ cph/@}) (cue txt))
=+ try=(~(de siva:aes shar ~) iv cph)
=+ ((hard {iv/@ len/@ cph/@}) (cue txt))
=+ try=(~(de siva:aes shar ~) iv len cph)
?~ try ~
=+ veri=(sure:as:(com:nu:crub bpk) ~ u.try)
?~ veri ~
(some ((hard (pair @ux @ux)) (cue u.veri)))
--
++ de
|= {key/@J cph/@}
|= {key/@J txt/@}
^- (unit @ux)
%+ ~(de sivc:aes (shaz key) ~)
(end 7 1 cph)
(rsh 7 1 cph)
=+ ((hard {iv/@ len/@ cph/@}) (cue txt))
%^ ~(de sivc:aes (shaz key) ~)
iv
len
cph
++ dy |=({key/@I cph/@} (need (de key cph)))
++ en
|= {key/@J msg/@}
^- @ux
(cat 7 (~(en sivc:aes (shaz key) ~) msg))
(jam (~(en sivc:aes (shaz key) ~) msg))
++ ex
|%
++ fig ^- @uvH (shaf %bfig sgn.^pub)