Artifact
9704d75a01f6bcba40a0efd051cb4f4facc4bfe2 :
File
kregion.fs
— part of check-in
[a7d674ebd4]
at
2016-03-04 21:22:24
on branch trunk
— use smove for string moves to known buffer sizes
(user:
bernd
size: 2506)
0000: 5c 20 63 72 79 70 74 6f 20 72 65 67 69 6f 6e 20 \ crypto region
0010: 62 61 73 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e based allocation
0020: 0a 0a 5c 20 43 6f 70 79 72 69 67 68 74 20 28 43 ..\ Copyright (C
0030: 29 20 32 30 31 34 20 20 20 42 65 72 6e 64 20 50 ) 2014 Bernd P
0040: 61 79 73 61 6e 0a 0a 5c 20 54 68 69 73 20 70 72 aysan..\ This pr
0050: 6f 67 72 61 6d 20 69 73 20 66 72 65 65 20 73 6f ogram is free so
0060: 66 74 77 61 72 65 3a 20 79 6f 75 20 63 61 6e 20 ftware: you can
0070: 72 65 64 69 73 74 72 69 62 75 74 65 20 69 74 20 redistribute it
0080: 61 6e 64 2f 6f 72 20 6d 6f 64 69 66 79 0a 5c 20 and/or modify.\
0090: 69 74 20 75 6e 64 65 72 20 74 68 65 20 74 65 72 it under the ter
00a0: 6d 73 20 6f 66 20 74 68 65 20 47 4e 55 20 41 66 ms of the GNU Af
00b0: 66 65 72 6f 20 47 65 6e 65 72 61 6c 20 50 75 62 fero General Pub
00c0: 6c 69 63 20 4c 69 63 65 6e 73 65 20 61 73 20 70 lic License as p
00d0: 75 62 6c 69 73 68 65 64 20 62 79 0a 5c 20 74 68 ublished by.\ th
00e0: 65 20 46 72 65 65 20 53 6f 66 74 77 61 72 65 20 e Free Software
00f0: 46 6f 75 6e 64 61 74 69 6f 6e 2c 20 65 69 74 68 Foundation, eith
0100: 65 72 20 76 65 72 73 69 6f 6e 20 33 20 6f 66 20 er version 3 of
0110: 74 68 65 20 4c 69 63 65 6e 73 65 2c 20 6f 72 0a the License, or.
0120: 5c 20 28 61 74 20 79 6f 75 72 20 6f 70 74 69 6f \ (at your optio
0130: 6e 29 20 61 6e 79 20 6c 61 74 65 72 20 76 65 72 n) any later ver
0140: 73 69 6f 6e 2e 0a 0a 5c 20 54 68 69 73 20 70 72 sion...\ This pr
0150: 6f 67 72 61 6d 20 69 73 20 64 69 73 74 72 69 62 ogram is distrib
0160: 75 74 65 64 20 69 6e 20 74 68 65 20 68 6f 70 65 uted in the hope
0170: 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 that it will be
0180: 20 75 73 65 66 75 6c 2c 0a 5c 20 62 75 74 20 57 useful,.\ but W
0190: 49 54 48 4f 55 54 20 41 4e 59 20 57 41 52 52 41 ITHOUT ANY WARRA
01a0: 4e 54 59 3b 20 77 69 74 68 6f 75 74 20 65 76 65 NTY; without eve
01b0: 6e 20 74 68 65 20 69 6d 70 6c 69 65 64 20 77 61 n the implied wa
01c0: 72 72 61 6e 74 79 20 6f 66 0a 5c 20 4d 45 52 43 rranty of.\ MERC
01d0: 48 41 4e 54 41 42 49 4c 49 54 59 20 6f 72 20 46 HANTABILITY or F
01e0: 49 54 4e 45 53 53 20 46 4f 52 20 41 20 50 41 52 ITNESS FOR A PAR
01f0: 54 49 43 55 4c 41 52 20 50 55 52 50 4f 53 45 2e TICULAR PURPOSE.
0200: 20 20 53 65 65 20 74 68 65 0a 5c 20 47 4e 55 20 See the.\ GNU
0210: 41 66 66 65 72 6f 20 47 65 6e 65 72 61 6c 20 50 Affero General P
0220: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f ublic License fo
0230: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a r more details..
0240: 0a 5c 20 59 6f 75 20 73 68 6f 75 6c 64 20 68 61 .\ You should ha
0250: 76 65 20 72 65 63 65 69 76 65 64 20 61 20 63 6f ve received a co
0260: 70 79 20 6f 66 20 74 68 65 20 47 4e 55 20 41 66 py of the GNU Af
0270: 66 65 72 6f 20 47 65 6e 65 72 61 6c 20 50 75 62 fero General Pub
0280: 6c 69 63 20 4c 69 63 65 6e 73 65 0a 5c 20 61 6c lic License.\ al
0290: 6f 6e 67 20 77 69 74 68 20 74 68 69 73 20 70 72 ong with this pr
02a0: 6f 67 72 61 6d 2e 20 20 49 66 20 6e 6f 74 2c 20 ogram. If not,
02b0: 73 65 65 20 3c 68 74 74 70 3a 2f 2f 77 77 77 2e see <http://www.
02c0: 67 6e 75 2e 6f 72 67 2f 6c 69 63 65 6e 73 65 73 gnu.org/licenses
02d0: 2f 3e 2e 0a 0a 32 56 61 72 69 61 62 6c 65 20 6b />...2Variable k
02e0: 72 65 67 69 6f 6e 20 5c 20 63 75 72 72 65 6e 74 region \ current
02f0: 20 72 65 67 69 6f 6e 20 70 6f 69 6e 74 65 72 20 region pointer
0300: 2b 20 72 65 6d 61 69 6e 64 65 72 0a 56 61 72 69 + remainder.Vari
0310: 61 62 6c 65 20 6b 66 72 65 65 36 34 27 20 5c 20 able kfree64' \
0320: 66 72 65 65 20 6c 69 73 74 20 66 6f 72 20 36 34 free list for 64
0330: 20 62 79 74 65 73 20 6b 65 79 73 0a 0a 24 34 30 bytes keys..$40
0340: 30 30 20 43 6f 6e 73 74 61 6e 74 20 2f 6b 72 65 00 Constant /kre
0350: 67 69 6f 6e 0a 0a 24 31 30 20 43 6f 6e 73 74 61 gion..$10 Consta
0360: 6e 74 20 63 72 79 70 74 2d 61 6c 69 67 6e 0a 0a nt crypt-align..
0370: 3a 20 6b 61 6c 69 67 6e 20 28 20 61 64 64 72 20 : kalign ( addr
0380: 2d 2d 20 61 64 64 72 27 20 29 0a 20 20 20 20 5b -- addr' ). [
0390: 20 63 72 79 70 74 2d 61 6c 69 67 6e 20 31 2d 20 crypt-align 1-
03a0: 5d 4c 20 2b 20 5b 20 63 72 79 70 74 2d 61 6c 69 ]L + [ crypt-ali
03b0: 67 6e 20 6e 65 67 61 74 65 20 5d 4c 20 61 6e 64 gn negate ]L and
03c0: 20 3b 0a 0a 3a 20 6b 61 6c 6c 6f 63 20 28 20 6c ;..: kalloc ( l
03d0: 65 6e 20 2d 2d 20 61 64 64 72 20 29 0a 20 20 20 en -- addr ).
03e0: 20 5c 47 20 61 6c 6c 6f 63 61 74 65 20 61 20 6c \G allocate a l
03f0: 65 6e 20 62 79 74 65 20 62 6c 6f 63 6b 20 6f 66 en byte block of
0400: 20 6e 6f 6e 2d 73 77 61 70 70 61 62 6c 65 20 73 non-swappable s
0410: 74 75 66 66 0a 20 20 20 20 6b 61 6c 69 67 6e 20 tuff. kalign
0420: 3e 72 0a 20 20 20 20 72 40 20 2f 6b 72 65 67 69 >r. r@ /kregi
0430: 6f 6e 20 75 3e 20 21 21 6b 72 2d 73 69 7a 65 21 on u> !!kr-size!
0440: 21 0a 20 20 20 20 6b 72 65 67 69 6f 6e 20 32 40 !. kregion 2@
0450: 20 64 75 70 20 72 40 20 75 3c 20 49 46 0a 09 32 dup r@ u< IF..2
0460: 64 72 6f 70 20 2f 6b 72 65 67 69 6f 6e 20 61 6c drop /kregion al
0470: 6c 6f 63 2b 6c 6f 63 6b 20 2f 6b 72 65 67 69 6f loc+lock /kregio
0480: 6e 20 32 64 75 70 20 6b 72 65 67 69 6f 6e 20 32 n 2dup kregion 2
0490: 21 20 20 54 48 45 4e 0a 20 20 20 20 6f 76 65 72 ! THEN. over
04a0: 20 73 77 61 70 20 72 3e 20 73 61 66 65 2f 73 74 swap r> safe/st
04b0: 72 69 6e 67 20 6b 72 65 67 69 6f 6e 20 32 21 20 ring kregion 2!
04c0: 28 20 6b 61 6c 6c 6f 63 28 20 2e 22 20 6b 61 6c ( kalloc( ." kal
04d0: 6c 6f 63 3a 20 22 20 64 75 70 20 68 65 78 2e 20 loc: " dup hex.
04e0: 63 72 20 29 20 3b 0a 0a 5c 20 66 69 78 65 64 20 cr ) ;..\ fixed
04f0: 73 69 7a 65 20 73 65 63 72 65 74 73 20 61 72 65 size secrets are
0500: 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 20 61 assumed to be a
0510: 6c 6c 20 36 34 20 62 79 74 65 73 20 6c 6f 6e 67 ll 64 bytes long
0520: 0a 5c 20 69 66 20 74 68 65 79 20 61 72 65 20 6a .\ if they are j
0530: 75 73 74 20 33 32 20 62 79 74 65 73 2c 20 74 68 ust 32 bytes, th
0540: 65 20 73 65 63 6f 6e 64 20 68 61 6c 66 20 69 73 e second half is
0550: 20 61 6c 6c 20 7a 65 72 6f 0a 0a 3a 20 6b 61 6c all zero..: kal
0560: 6c 6f 63 36 34 20 28 20 2d 2d 20 61 64 64 72 20 loc64 ( -- addr
0570: 29 0a 20 20 20 20 6b 66 72 65 65 36 34 27 20 40 ). kfree64' @
0580: 20 3f 64 75 70 2d 69 66 20 20 64 75 70 20 40 20 ?dup-if dup @
0590: 6b 66 72 65 65 36 34 27 20 21 20 64 75 70 20 6f kfree64' ! dup o
05a0: 66 66 20 20 65 78 69 74 20 20 74 68 65 6e 0a 20 ff exit then.
05b0: 20 20 20 36 34 20 6b 61 6c 6c 6f 63 20 3b 0a 3a 64 kalloc ;.:
05c0: 20 6b 66 72 65 65 36 34 20 28 20 61 64 64 72 20 kfree64 ( addr
05d0: 2d 2d 20 29 0a 20 20 20 20 64 75 70 20 36 34 20 -- ). dup 64
05e0: 65 72 61 73 65 20 6b 66 72 65 65 36 34 27 20 40 erase kfree64' @
05f0: 20 6f 76 65 72 20 21 20 6b 66 72 65 65 36 34 27 over ! kfree64'
0600: 20 21 20 3b 0a 3a 20 6b 61 6c 6c 6f 63 36 34 3f ! ;.: kalloc64?
0610: 20 28 20 61 64 64 72 20 2d 2d 20 61 64 64 72 27 ( addr -- addr'
0620: 20 29 0a 20 20 20 20 64 75 70 20 40 20 49 46 20 ). dup @ IF
0630: 20 40 20 20 45 58 49 54 20 20 54 48 45 4e 20 20 @ EXIT THEN
0640: 64 72 6f 70 20 6b 61 6c 6c 6f 63 36 34 20 3b 0a drop kalloc64 ;.
0650: 0a 33 32 20 62 75 66 66 65 72 3a 20 7a 65 72 6f .32 buffer: zero
0660: 33 32 0a 0a 3a 20 73 65 63 2d 6f 66 66 20 28 20 32..: sec-off (
0670: 61 64 64 72 20 2d 2d 20 29 20 64 75 70 20 40 20 addr -- ) dup @
0680: 64 75 70 20 20 49 46 20 20 6b 66 72 65 65 36 34 dup IF kfree64
0690: 20 6f 66 66 20 45 58 49 54 20 20 54 48 45 4e 20 off EXIT THEN
06a0: 20 32 64 72 6f 70 20 3b 0a 3a 20 73 65 63 21 20 2drop ;.: sec!
06b0: 28 20 61 64 64 72 31 20 75 31 20 61 64 64 72 32 ( addr1 u1 addr2
06c0: 20 2d 2d 20 29 0a 20 20 20 20 6f 76 65 72 20 30 -- ). over 0
06d0: 3d 20 49 46 20 20 73 65 63 2d 6f 66 66 20 32 64 = IF sec-off 2d
06e0: 72 6f 70 20 20 45 58 49 54 20 20 54 48 45 4e 0a rop EXIT THEN.
06f0: 20 20 20 20 3e 72 20 72 40 20 6b 61 6c 6c 6f 63 >r r@ kalloc
0700: 36 34 3f 20 64 75 70 20 72 3e 20 21 20 24 34 30 64? dup r> ! $40
0710: 20 73 6d 6f 76 65 20 3b 0a 3a 20 73 65 63 40 20 smove ;.: sec@
0720: 28 20 61 64 64 72 20 2d 2d 20 61 64 64 72 31 20 ( addr -- addr1
0730: 75 31 20 29 0a 20 20 20 20 40 20 64 75 70 20 49 u1 ). @ dup I
0740: 46 20 20 24 34 30 20 6f 76 65 72 20 24 32 30 20 F $40 over $20
0750: 2b 20 24 32 30 20 7a 65 72 6f 33 32 20 6f 76 65 + $20 zero32 ove
0760: 72 20 73 74 72 3d 20 49 46 20 20 32 2f 20 20 54 r str= IF 2/ T
0770: 48 45 4e 0a 20 20 20 20 45 4c 53 45 20 30 20 54 HEN. ELSE 0 T
0780: 48 45 4e 20 3b 0a 3a 20 73 65 63 2b 21 20 28 20 HEN ;.: sec+! (
0790: 61 64 64 72 31 20 75 31 20 61 64 64 72 32 20 2d addr1 u1 addr2 -
07a0: 2d 20 29 0a 20 20 20 20 64 75 70 20 40 20 30 3d - ). dup @ 0=
07b0: 20 49 46 20 20 73 65 63 21 20 20 45 4c 53 45 20 IF sec! ELSE
07c0: 20 73 65 63 40 20 64 75 70 20 3e 72 20 2b 20 24 sec@ dup >r + $
07d0: 34 30 20 72 3e 20 2d 20 73 6d 6f 76 65 20 20 54 40 r> - smove T
07e0: 48 45 4e 20 3b 0a 0a 3a 20 73 65 63 2b 5b 5d 21 HEN ;..: sec+[]!
07f0: 20 28 20 61 64 64 72 31 20 75 31 20 61 64 64 72 ( addr1 u1 addr
0800: 32 20 2d 2d 20 29 20 3e 72 0a 20 20 20 20 30 20 2 -- ) >r. 0
0810: 7b 20 77 5e 20 73 65 63 20 7d 20 73 65 63 20 73 { w^ sec } sec s
0820: 65 63 21 20 73 65 63 20 63 65 6c 6c 20 72 3e 20 ec! sec cell r>
0830: 24 2b 21 20 3b 0a 0a 3a 20 73 65 63 5b 5d 40 20 $+! ;..: sec[]@
0840: 28 20 69 20 61 64 64 72 20 2d 2d 20 61 64 64 72 ( i addr -- addr
0850: 20 75 20 29 20 20 24 5b 5d 20 73 65 63 40 20 3b u ) $[] sec@ ;
0860: 0a 0a 73 74 6f 72 61 67 65 20 63 6c 61 73 73 20 ..storage class
0870: 65 6e 64 2d 63 6c 61 73 73 20 63 72 79 70 74 6f end-class crypto
0880: 2d 61 6c 6c 6f 63 0a 0a 3a 6e 6f 6e 61 6d 65 20 -alloc..:noname
0890: 20 28 20 6c 65 6e 20 2d 2d 20 61 64 64 72 20 29 ( len -- addr )
08a0: 0a 20 20 20 20 5b 20 63 72 79 70 74 2d 61 6c 69 . [ crypt-ali
08b0: 67 6e 20 63 65 6c 6c 2d 20 63 72 79 70 74 2d 61 gn cell- crypt-a
08c0: 6c 69 67 6e 20 31 2d 20 2b 20 5d 4c 20 2b 0a 20 lign 1- + ]L +.
08d0: 20 20 20 5b 20 63 72 79 70 74 2d 61 6c 69 67 6e [ crypt-align
08e0: 20 6e 65 67 61 74 65 20 5d 4c 20 61 6e 64 20 6b negate ]L and k
08f0: 61 6c 6c 6f 63 20 5b 20 63 72 79 70 74 2d 61 6c alloc [ crypt-al
0900: 69 67 6e 20 63 65 6c 6c 2d 20 5d 4c 20 2b 0a 3b ign cell- ]L +.;
0910: 20 63 72 79 70 74 6f 2d 61 6c 6c 6f 63 20 74 6f crypto-alloc to
0920: 20 3a 61 6c 6c 6f 63 61 74 65 0a 27 20 64 72 6f :allocate.' dro
0930: 70 20 63 72 79 70 74 6f 2d 61 6c 6c 6f 63 20 74 p crypto-alloc t
0940: 6f 20 3a 66 72 65 65 0a 5c 20 77 65 20 6e 65 76 o :free.\ we nev
0950: 65 72 20 66 72 65 65 20 74 68 65 73 65 20 63 6c er free these cl
0960: 61 73 73 65 73 2c 20 74 68 65 79 20 61 72 65 20 asses, they are
0970: 70 65 72 2d 74 61 73 6b 20 74 65 6d 70 6f 72 61 per-task tempora
0980: 72 79 20 73 74 6f 72 61 67 65 73 0a 0a 63 72 79 ry storages..cry
0990: 70 74 6f 2d 61 6c 6c 6f 63 20 27 20 6e 65 77 20 pto-alloc ' new
09a0: 73 74 61 74 69 63 2d 61 20 77 69 74 68 2d 61 6c static-a with-al
09b0: 6c 6f 63 61 74 65 72 20 43 6f 6e 73 74 61 6e 74 locater Constant
09c0: 20 63 72 79 70 74 6f 2d 61 0a crypto-a.