Artifact
d39a5c3fa58e12e257de51e843ca9647c01635c1 :
File
threefishlib.fs
— part of check-in
[211b96e714]
at
2019-06-05 22:15:40
on branch trunk
— Add new method for vault key exchange
(user:
bernd
size: 2282)
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 63 20 76 6f 69 64 }.\c }.\c void
0520: 20 74 66 5f 74 77 65 61 6b 32 35 36 5f 70 70 28 tf_tweak256_pp(
0530: 73 74 72 75 63 74 20 74 66 5f 63 74 78 5f 32 35 struct tf_ctx_25
0540: 36 20 2a 63 74 78 29 0a 5c 63 20 7b 0a 5c 63 20 6 *ctx).\c {.\c
0550: 20 20 63 74 78 2d 3e 74 77 65 61 6b 5b 31 5d 20 ctx->tweak[1]
0560: 2b 3d 20 21 2b 2b 28 63 74 78 2d 3e 74 77 65 61 += !++(ctx->twea
0570: 6b 5b 30 5d 29 3b 0a 5c 63 20 7d 0a 5c 63 20 76 k[0]);.\c }.\c v
0580: 6f 69 64 20 74 66 5f 74 77 65 61 6b 35 31 32 5f oid tf_tweak512_
0590: 70 70 28 73 74 72 75 63 74 20 74 66 5f 63 74 78 pp(struct tf_ctx
05a0: 5f 35 31 32 20 2a 63 74 78 29 0a 5c 63 20 7b 0a _512 *ctx).\c {.
05b0: 5c 63 20 20 20 63 74 78 2d 3e 74 77 65 61 6b 5b \c ctx->tweak[
05c0: 31 5d 20 2b 3d 20 21 2b 2b 28 63 74 78 2d 3e 74 1] += !++(ctx->t
05d0: 77 65 61 6b 5b 30 5d 29 3b 0a 5c 63 20 7d 0a 5c weak[0]);.\c }.\
05e0: 20 2d 2d 2d 2d 2d 2d 2d 3d 3d 3d 3c 20 73 74 72 -------===< str
05f0: 75 63 74 73 20 3e 3d 3d 3d 2d 2d 2d 2d 2d 2d 2d ucts >===-------
0600: 2d 0a 5c 20 74 66 5f 63 74 78 5f 32 35 36 0a 62 -.\ tf_ctx_256.b
0610: 65 67 69 6e 2d 73 74 72 75 63 74 75 72 65 20 74 egin-structure t
0620: 66 5f 63 74 78 5f 32 35 36 0a 20 20 20 20 64 72 f_ctx_256. dr
0630: 6f 70 20 30 20 34 30 20 2b 66 69 65 6c 64 20 74 op 0 40 +field t
0640: 66 5f 63 74 78 5f 32 35 36 2d 6b 65 79 0a 20 20 f_ctx_256-key.
0650: 20 20 64 72 6f 70 20 34 30 20 32 34 20 2b 66 69 drop 40 24 +fi
0660: 65 6c 64 20 74 66 5f 63 74 78 5f 32 35 36 2d 74 eld tf_ctx_256-t
0670: 77 65 61 6b 0a 20 20 20 20 64 72 6f 70 20 36 34 weak. drop 64
0680: 20 65 6e 64 2d 73 74 72 75 63 74 75 72 65 0a 5c end-structure.\
0690: 20 74 66 5f 63 74 78 0a 62 65 67 69 6e 2d 73 74 tf_ctx.begin-st
06a0: 72 75 63 74 75 72 65 20 74 66 5f 63 74 78 0a 20 ructure tf_ctx.
06b0: 20 20 20 64 72 6f 70 20 30 20 37 32 20 2b 66 69 drop 0 72 +fi
06c0: 65 6c 64 20 74 66 5f 63 74 78 2d 6b 65 79 0a 20 eld tf_ctx-key.
06d0: 20 20 20 64 72 6f 70 20 37 32 20 32 34 20 2b 66 drop 72 24 +f
06e0: 69 65 6c 64 20 74 66 5f 63 74 78 2d 74 77 65 61 ield tf_ctx-twea
06f0: 6b 0a 20 20 20 20 64 72 6f 70 20 39 36 20 65 6e k. drop 96 en
0700: 64 2d 73 74 72 75 63 74 75 72 65 0a 0a 5c 20 2d d-structure..\ -
0710: 2d 2d 2d 2d 2d 3d 3d 3d 3c 20 66 75 6e 63 74 69 -----===< functi
0720: 6f 6e 73 20 3e 3d 3d 3d 2d 2d 2d 2d 2d 2d 2d 0a ons >===-------.
0730: 63 2d 66 75 6e 63 74 69 6f 6e 20 74 66 5f 65 6e c-function tf_en
0740: 63 72 79 70 74 20 74 66 5f 65 6e 63 72 79 70 74 crypt tf_encrypt
0750: 5f 35 31 32 20 61 20 61 20 61 20 6e 20 2d 2d 20 _512 a a a n --
0760: 76 6f 69 64 0a 63 2d 66 75 6e 63 74 69 6f 6e 20 void.c-function
0770: 74 66 5f 64 65 63 72 79 70 74 20 74 66 5f 64 65 tf_decrypt tf_de
0780: 63 72 79 70 74 5f 35 31 32 20 61 20 61 20 61 20 crypt_512 a a a
0790: 6e 20 2d 2d 20 76 6f 69 64 0a 63 2d 66 75 6e 63 n -- void.c-func
07a0: 74 69 6f 6e 20 74 66 5f 65 6e 63 72 79 70 74 5f tion tf_encrypt_
07b0: 32 35 36 20 74 66 5f 65 6e 63 72 79 70 74 5f 32 256 tf_encrypt_2
07c0: 35 36 20 61 20 61 20 61 20 6e 20 2d 2d 20 76 6f 56 a a a n -- vo
07d0: 69 64 0a 63 2d 66 75 6e 63 74 69 6f 6e 20 74 66 id.c-function tf
07e0: 5f 64 65 63 72 79 70 74 5f 32 35 36 20 74 66 5f _decrypt_256 tf_
07f0: 64 65 63 72 79 70 74 5f 32 35 36 20 61 20 61 20 decrypt_256 a a
0800: 61 20 6e 20 2d 2d 20 76 6f 69 64 0a 63 2d 66 75 a n -- void.c-fu
0810: 6e 63 74 69 6f 6e 20 74 66 5f 65 6e 63 72 79 70 nction tf_encryp
0820: 74 5f 6c 6f 6f 70 20 74 66 5f 65 6e 63 72 79 70 t_loop tf_encryp
0830: 74 5f 6c 6f 6f 70 20 61 20 61 20 6e 20 6e 20 6e t_loop a a n n n
0840: 20 2d 2d 20 76 6f 69 64 0a 63 2d 66 75 6e 63 74 -- void.c-funct
0850: 69 6f 6e 20 74 66 5f 64 65 63 72 79 70 74 5f 6c ion tf_decrypt_l
0860: 6f 6f 70 20 74 66 5f 64 65 63 72 79 70 74 5f 6c oop tf_decrypt_l
0870: 6f 6f 70 20 61 20 61 20 6e 20 6e 20 6e 20 2d 2d oop a a n n n --
0880: 20 76 6f 69 64 0a 63 2d 66 75 6e 63 74 69 6f 6e void.c-function
0890: 20 74 66 5f 74 77 65 61 6b 32 35 36 2b 2b 20 74 tf_tweak256++ t
08a0: 66 5f 74 77 65 61 6b 32 35 36 5f 70 70 20 61 20 f_tweak256_pp a
08b0: 2d 2d 20 76 6f 69 64 0a 63 2d 66 75 6e 63 74 69 -- void.c-functi
08c0: 6f 6e 20 74 66 5f 74 77 65 61 6b 35 31 32 2b 2b on tf_tweak512++
08d0: 20 74 66 5f 74 77 65 61 6b 35 31 32 5f 70 70 20 tf_tweak512_pp
08e0: 61 20 2d 2d 20 76 6f 69 64 0a a -- void.