Artifact
7aaad00511e0bac6ae4d2a23ff302a655d9a8632:
- File
threefishlib.fs
— part of check-in
[b94045e2a9]
at
2018-03-13 23:36:54
on branch trunk
— Tie secret key to routes
(user:
bernd
size: 1984)
0000: 5c 20 74 68 72 65 65 66 69 73 68 20 77 72 61 70 \ threefish wrap
0010: 70 65 72 0a 0a 5c 20 43 6f 70 79 72 69 67 68 74 per..\ Copyright
0020: 20 28 43 29 20 32 30 31 35 2c 32 30 31 38 20 20 (C) 2015,2018
0030: 20 42 65 72 6e 64 20 50 61 79 73 61 6e 0a 0a 5c Bernd Paysan..\
0040: 20 54 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 This program is
0050: 20 66 72 65 65 20 73 6f 66 74 77 61 72 65 3a 20 free software:
0060: 79 6f 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 you can redistri
0070: 62 75 74 65 20 69 74 20 61 6e 64 2f 6f 72 20 6d bute it and/or m
0080: 6f 64 69 66 79 0a 5c 20 69 74 20 75 6e 64 65 72 odify.\ it under
0090: 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 the terms of th
00a0: 65 20 47 4e 55 20 41 66 66 65 72 6f 20 47 65 6e e GNU Affero Gen
00b0: 65 72 61 6c 20 50 75 62 6c 69 63 20 4c 69 63 65 eral Public Lice
00c0: 6e 73 65 20 61 73 20 70 75 62 6c 69 73 68 65 64 nse as published
00d0: 20 62 79 0a 5c 20 74 68 65 20 46 72 65 65 20 53 by.\ the Free S
00e0: 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 61 74 69 oftware Foundati
00f0: 6f 6e 2c 20 65 69 74 68 65 72 20 76 65 72 73 69 on, either versi
0100: 6f 6e 20 33 20 6f 66 20 74 68 65 20 4c 69 63 65 on 3 of the Lice
0110: 6e 73 65 2c 20 6f 72 0a 5c 20 28 61 74 20 79 6f nse, or.\ (at yo
0120: 75 72 20 6f 70 74 69 6f 6e 29 20 61 6e 79 20 6c ur option) any l
0130: 61 74 65 72 20 76 65 72 73 69 6f 6e 2e 0a 0a 5c ater version...\
0140: 20 54 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 This program is
0150: 20 64 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 distributed in
0160: 74 68 65 20 68 6f 70 65 20 74 68 61 74 20 69 74 the hope that it
0170: 20 77 69 6c 6c 20 62 65 20 75 73 65 66 75 6c 2c will be useful,
0180: 0a 5c 20 62 75 74 20 57 49 54 48 4f 55 54 20 41 .\ but WITHOUT A
0190: 4e 59 20 57 41 52 52 41 4e 54 59 3b 20 77 69 74 NY WARRANTY; wit
01a0: 68 6f 75 74 20 65 76 65 6e 20 74 68 65 20 69 6d hout even the im
01b0: 70 6c 69 65 64 20 77 61 72 72 61 6e 74 79 20 6f plied warranty o
01c0: 66 0a 5c 20 4d 45 52 43 48 41 4e 54 41 42 49 4c f.\ MERCHANTABIL
01d0: 49 54 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 ITY or FITNESS F
01e0: 4f 52 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 OR A PARTICULAR
01f0: 50 55 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 PURPOSE. See th
0200: 65 0a 5c 20 47 4e 55 20 41 66 66 65 72 6f 20 47 e.\ GNU Affero G
0210: 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 20 4c 69 eneral Public Li
0220: 63 65 6e 73 65 20 66 6f 72 20 6d 6f 72 65 20 64 cense for more d
0230: 65 74 61 69 6c 73 2e 0a 0a 5c 20 59 6f 75 20 73 etails...\ You s
0240: 68 6f 75 6c 64 20 68 61 76 65 20 72 65 63 65 69 hould have recei
0250: 76 65 64 20 61 20 63 6f 70 79 20 6f 66 20 74 68 ved a copy of th
0260: 65 20 47 4e 55 20 41 66 66 65 72 6f 20 47 65 6e e GNU Affero Gen
0270: 65 72 61 6c 20 50 75 62 6c 69 63 20 4c 69 63 65 eral Public Lice
0280: 6e 73 65 0a 5c 20 61 6c 6f 6e 67 20 77 69 74 68 nse.\ along with
0290: 20 74 68 69 73 20 70 72 6f 67 72 61 6d 2e 20 20 this program.
02a0: 49 66 20 6e 6f 74 2c 20 73 65 65 20 3c 68 74 74 If not, see <htt
02b0: 70 3a 2f 2f 77 77 77 2e 67 6e 75 2e 6f 72 67 2f p://www.gnu.org/
02c0: 6c 69 63 65 6e 73 65 73 2f 3e 2e 0a 0a 5c 63 20 licenses/>...\c
02d0: 23 69 6e 63 6c 75 64 65 20 3c 74 68 72 65 65 66 #include <threef
02e0: 69 73 68 2e 68 3e 0a 5c 63 20 76 6f 69 64 20 74 ish.h>.\c void t
02f0: 66 5f 65 6e 63 72 79 70 74 5f 6c 6f 6f 70 28 73 f_encrypt_loop(s
0300: 74 72 75 63 74 20 74 66 5f 63 74 78 5f 35 31 32 truct tf_ctx_512
0310: 20 2a 63 74 78 2c 20 75 69 6e 74 36 34 5f 74 20 *ctx, uint64_t
0320: 2a 70 2c 20 73 69 7a 65 5f 74 20 6e 2c 0a 5c 63 *p, size_t n,.\c
0330: 20 09 09 09 20 20 20 20 69 6e 74 20 66 6c 61 67 ... int flag
0340: 73 31 2c 20 69 6e 74 20 66 6c 61 67 73 32 29 20 s1, int flags2)
0350: 7b 0a 5c 63 20 20 20 69 6e 74 20 66 6c 61 67 73 {.\c int flags
0360: 3d 66 6c 61 67 73 31 3b 0a 5c 63 20 20 20 77 68 =flags1;.\c wh
0370: 69 6c 65 28 6e 3e 3d 36 34 29 20 7b 0a 5c 63 20 ile(n>=64) {.\c
0380: 20 20 20 20 74 66 5f 65 6e 63 72 79 70 74 5f 35 tf_encrypt_5
0390: 31 32 28 63 74 78 2c 20 70 2c 20 70 2c 20 66 6c 12(ctx, p, p, fl
03a0: 61 67 73 29 3b 0a 5c 63 20 20 20 20 20 66 6c 61 ags);.\c fla
03b0: 67 73 3d 66 6c 61 67 73 32 3b 20 70 2b 3d 38 3b gs=flags2; p+=8;
03c0: 20 6e 2d 3d 36 34 3b 0a 5c 63 20 20 20 20 20 63 n-=64;.\c c
03d0: 74 78 2d 3e 74 77 65 61 6b 5b 31 5d 20 2b 3d 20 tx->tweak[1] +=
03e0: 21 2b 2b 28 63 74 78 2d 3e 74 77 65 61 6b 5b 30 !++(ctx->tweak[0
03f0: 5d 29 3b 0a 5c 63 20 20 20 7d 0a 5c 63 20 7d 0a ]);.\c }.\c }.
0400: 5c 63 20 76 6f 69 64 20 74 66 5f 64 65 63 72 79 \c void tf_decry
0410: 70 74 5f 6c 6f 6f 70 28 73 74 72 75 63 74 20 74 pt_loop(struct t
0420: 66 5f 63 74 78 5f 35 31 32 20 2a 63 74 78 2c 20 f_ctx_512 *ctx,
0430: 75 69 6e 74 36 34 5f 74 20 2a 63 2c 20 73 69 7a uint64_t *c, siz
0440: 65 5f 74 20 6e 2c 0a 5c 63 20 09 09 09 20 20 20 e_t n,.\c ...
0450: 20 69 6e 74 20 66 6c 61 67 73 31 2c 20 69 6e 74 int flags1, int
0460: 20 66 6c 61 67 73 32 29 20 7b 0a 5c 63 20 20 20 flags2) {.\c
0470: 69 6e 74 20 66 6c 61 67 73 3d 66 6c 61 67 73 31 int flags=flags1
0480: 3b 0a 5c 63 20 20 20 77 68 69 6c 65 28 6e 3e 3d ;.\c while(n>=
0490: 36 34 29 20 7b 0a 5c 63 20 20 20 20 20 74 66 5f 64) {.\c tf_
04a0: 64 65 63 72 79 70 74 5f 35 31 32 28 63 74 78 2c decrypt_512(ctx,
04b0: 20 63 2c 20 63 2c 20 66 6c 61 67 73 29 3b 0a 5c c, c, flags);.\
04c0: 63 20 20 20 20 20 66 6c 61 67 73 3d 66 6c 61 67 c flags=flag
04d0: 73 32 3b 20 63 2b 3d 38 3b 20 6e 2d 3d 36 34 3b s2; c+=8; n-=64;
04e0: 0a 5c 63 20 20 20 20 20 63 74 78 2d 3e 74 77 65 .\c ctx->twe
04f0: 61 6b 5b 31 5d 20 2b 3d 20 21 2b 2b 28 63 74 78 ak[1] += !++(ctx
0500: 2d 3e 74 77 65 61 6b 5b 30 5d 29 3b 0a 5c 63 20 ->tweak[0]);.\c
0510: 20 20 7d 0a 5c 63 20 7d 0a 5c 20 2d 2d 2d 2d 2d }.\c }.\ -----
0520: 2d 2d 3d 3d 3d 3c 20 73 74 72 75 63 74 73 20 3e --===< structs >
0530: 3d 3d 3d 2d 2d 2d 2d 2d 2d 2d 2d 0a 5c 20 74 66 ===--------.\ tf
0540: 5f 63 74 78 5f 32 35 36 0a 62 65 67 69 6e 2d 73 _ctx_256.begin-s
0550: 74 72 75 63 74 75 72 65 20 74 66 5f 63 74 78 5f tructure tf_ctx_
0560: 32 35 36 0a 20 20 20 20 64 72 6f 70 20 30 20 34 256. drop 0 4
0570: 30 20 2b 66 69 65 6c 64 20 74 66 5f 63 74 78 5f 0 +field tf_ctx_
0580: 32 35 36 2d 6b 65 79 0a 20 20 20 20 64 72 6f 70 256-key. drop
0590: 20 34 30 20 32 34 20 2b 66 69 65 6c 64 20 74 66 40 24 +field tf
05a0: 5f 63 74 78 5f 32 35 36 2d 74 77 65 61 6b 0a 20 _ctx_256-tweak.
05b0: 20 20 20 64 72 6f 70 20 36 34 20 65 6e 64 2d 73 drop 64 end-s
05c0: 74 72 75 63 74 75 72 65 0a 5c 20 74 66 5f 63 74 tructure.\ tf_ct
05d0: 78 0a 62 65 67 69 6e 2d 73 74 72 75 63 74 75 72 x.begin-structur
05e0: 65 20 74 66 5f 63 74 78 0a 20 20 20 20 64 72 6f e tf_ctx. dro
05f0: 70 20 30 20 37 32 20 2b 66 69 65 6c 64 20 74 66 p 0 72 +field tf
0600: 5f 63 74 78 2d 6b 65 79 0a 20 20 20 20 64 72 6f _ctx-key. dro
0610: 70 20 37 32 20 32 34 20 2b 66 69 65 6c 64 20 74 p 72 24 +field t
0620: 66 5f 63 74 78 2d 74 77 65 61 6b 0a 20 20 20 20 f_ctx-tweak.
0630: 64 72 6f 70 20 39 36 20 65 6e 64 2d 73 74 72 75 drop 96 end-stru
0640: 63 74 75 72 65 0a 0a 5c 20 2d 2d 2d 2d 2d 2d 3d cture..\ ------=
0650: 3d 3d 3c 20 66 75 6e 63 74 69 6f 6e 73 20 3e 3d ==< functions >=
0660: 3d 3d 2d 2d 2d 2d 2d 2d 2d 0a 63 2d 66 75 6e 63 ==-------.c-func
0670: 74 69 6f 6e 20 74 66 5f 65 6e 63 72 79 70 74 20 tion tf_encrypt
0680: 74 66 5f 65 6e 63 72 79 70 74 5f 35 31 32 20 61 tf_encrypt_512 a
0690: 20 61 20 61 20 6e 20 2d 2d 20 76 6f 69 64 0a 63 a a n -- void.c
06a0: 2d 66 75 6e 63 74 69 6f 6e 20 74 66 5f 64 65 63 -function tf_dec
06b0: 72 79 70 74 20 74 66 5f 64 65 63 72 79 70 74 5f rypt tf_decrypt_
06c0: 35 31 32 20 61 20 61 20 61 20 6e 20 2d 2d 20 76 512 a a a n -- v
06d0: 6f 69 64 0a 63 2d 66 75 6e 63 74 69 6f 6e 20 74 oid.c-function t
06e0: 66 5f 65 6e 63 72 79 70 74 5f 32 35 36 20 74 66 f_encrypt_256 tf
06f0: 5f 65 6e 63 72 79 70 74 5f 32 35 36 20 61 20 61 _encrypt_256 a a
0700: 20 61 20 6e 20 2d 2d 20 76 6f 69 64 0a 63 2d 66 a n -- void.c-f
0710: 75 6e 63 74 69 6f 6e 20 74 66 5f 64 65 63 72 79 unction tf_decry
0720: 70 74 5f 32 35 36 20 74 66 5f 64 65 63 72 79 70 pt_256 tf_decryp
0730: 74 5f 32 35 36 20 61 20 61 20 61 20 6e 20 2d 2d t_256 a a a n --
0740: 20 76 6f 69 64 0a 63 2d 66 75 6e 63 74 69 6f 6e void.c-function
0750: 20 74 66 5f 65 6e 63 72 79 70 74 5f 6c 6f 6f 70 tf_encrypt_loop
0760: 20 74 66 5f 65 6e 63 72 79 70 74 5f 6c 6f 6f 70 tf_encrypt_loop
0770: 20 61 20 61 20 6e 20 6e 20 6e 20 2d 2d 20 76 6f a a n n n -- vo
0780: 69 64 0a 63 2d 66 75 6e 63 74 69 6f 6e 20 74 66 id.c-function tf
0790: 5f 64 65 63 72 79 70 74 5f 6c 6f 6f 70 20 74 66 _decrypt_loop tf
07a0: 5f 64 65 63 72 79 70 74 5f 6c 6f 6f 70 20 61 20 _decrypt_loop a
07b0: 61 20 6e 20 6e 20 6e 20 2d 2d 20 76 6f 69 64 0a a n n n -- void.