annotate src/sha256.c @ 2180:f60a0c9cbe6c vim73

Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
author Bram Moolenaar <bram@vim.org>
date Sun, 16 May 2010 22:32:54 +0200
parents
children 3cb515c62e9c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2180
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4:
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
2 *
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
3 * FIPS-180-2 compliant SHA-256 implementation
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
4 * GPL by Christophe Devine.
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
5 * Modified for md5deep, in public domain.
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
6 * Modified For Vim, GPL(C) Mohsin Ahmed, http://www.cs.albany.edu/~mosh
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
7 *
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
8 * Vim specific notes:
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
9 * Functions exported by this file:
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
10 * 1. sha256_key() hashes the password to 64 bytes char string.
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
11 * 2. sha2_seed() generates a random header.
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
12 * sha256_self_test() is implicitly called once.
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
13 */
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
14
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
15 #include "vim.h"
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
16
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
17 #ifdef FEAT_CRYPT
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
18
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
19 typedef unsigned long uint32_t;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
20
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
21 typedef struct {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
22 uint32_t total[2];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
23 uint32_t state[8];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
24 char_u buffer[64];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
25 } context_sha256_T;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
26
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
27 static void sha256_starts __ARGS((context_sha256_T *ctx));
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
28 static void sha256_process __ARGS((context_sha256_T *ctx, char_u data[64]));
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
29 static void sha256_update __ARGS((context_sha256_T *ctx, char_u *input, uint32_t length));
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
30 static void sha256_finish __ARGS((context_sha256_T *ctx, char_u digest[32]));
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
31 static char *sha256_bytes __ARGS((char *buf, int buflen));
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
32 static unsigned int get_some_time __ARGS((void));
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
33
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
34
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
35 #define GET_UINT32(n, b, i) \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
36 { \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
37 (n) = ( (uint32_t)(b)[(i) ] << 24) \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
38 | ( (uint32_t)(b)[(i) + 1] << 16) \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
39 | ( (uint32_t)(b)[(i) + 2] << 8) \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
40 | ( (uint32_t)(b)[(i) + 3] ); \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
41 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
42
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
43 #define PUT_UINT32(n,b,i) \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
44 { \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
45 (b)[(i) ] = (char_u)((n) >> 24); \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
46 (b)[(i) + 1] = (char_u)((n) >> 16); \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
47 (b)[(i) + 2] = (char_u)((n) >> 8); \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
48 (b)[(i) + 3] = (char_u)((n) ); \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
49 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
50
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
51 static void
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
52 sha256_starts(ctx)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
53 context_sha256_T *ctx;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
54 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
55 ctx->total[0] = 0;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
56 ctx->total[1] = 0;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
57
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
58 ctx->state[0] = 0x6A09E667;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
59 ctx->state[1] = 0xBB67AE85;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
60 ctx->state[2] = 0x3C6EF372;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
61 ctx->state[3] = 0xA54FF53A;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
62 ctx->state[4] = 0x510E527F;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
63 ctx->state[5] = 0x9B05688C;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
64 ctx->state[6] = 0x1F83D9AB;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
65 ctx->state[7] = 0x5BE0CD19;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
66 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
67
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
68 static void
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
69 sha256_process(ctx, data)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
70 context_sha256_T *ctx;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
71 char_u data[64];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
72 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
73 uint32_t temp1, temp2, W[64];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
74 uint32_t A, B, C, D, E, F, G, H;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
75
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
76 GET_UINT32(W[0], data, 0);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
77 GET_UINT32(W[1], data, 4);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
78 GET_UINT32(W[2], data, 8);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
79 GET_UINT32(W[3], data, 12);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
80 GET_UINT32(W[4], data, 16);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
81 GET_UINT32(W[5], data, 20);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
82 GET_UINT32(W[6], data, 24);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
83 GET_UINT32(W[7], data, 28);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
84 GET_UINT32(W[8], data, 32);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
85 GET_UINT32(W[9], data, 36);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
86 GET_UINT32(W[10], data, 40);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
87 GET_UINT32(W[11], data, 44);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
88 GET_UINT32(W[12], data, 48);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
89 GET_UINT32(W[13], data, 52);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
90 GET_UINT32(W[14], data, 56);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
91 GET_UINT32(W[15], data, 60);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
92
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
93 #define SHR(x, n) ((x & 0xFFFFFFFF) >> n)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
94 #define ROTR(x, n) (SHR(x, n) | (x << (32 - n)))
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
95
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
96 #define S0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
97 #define S1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
98
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
99 #define S2(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
100 #define S3(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
101
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
102 #define F0(x, y, z) ((x & y) | (z & (x | y)))
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
103 #define F1(x, y, z) (z ^ (x & (y ^ z)))
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
104
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
105 #define R(t) \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
106 ( \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
107 W[t] = S1(W[t - 2]) + W[t - 7] + \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
108 S0(W[t - 15]) + W[t - 16] \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
109 )
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
110
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
111 #define P(a,b,c,d,e,f,g,h,x,K) \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
112 { \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
113 temp1 = h + S3(e) + F1(e, f, g) + K + x; \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
114 temp2 = S2(a) + F0(a, b, c); \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
115 d += temp1; h = temp1 + temp2; \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
116 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
117
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
118 A = ctx->state[0];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
119 B = ctx->state[1];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
120 C = ctx->state[2];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
121 D = ctx->state[3];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
122 E = ctx->state[4];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
123 F = ctx->state[5];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
124 G = ctx->state[6];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
125 H = ctx->state[7];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
126
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
127 P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
128 P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
129 P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
130 P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
131 P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
132 P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
133 P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
134 P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
135 P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
136 P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
137 P( G, H, A, B, C, D, E, F, W[10], 0x243185BE);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
138 P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
139 P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
140 P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
141 P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
142 P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
143 P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
144 P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
145 P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
146 P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
147 P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
148 P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
149 P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
150 P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
151 P( A, B, C, D, E, F, G, H, R(24), 0x983E5152);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
152 P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
153 P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
154 P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
155 P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
156 P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
157 P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
158 P( B, C, D, E, F, G, H, A, R(31), 0x14292967);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
159 P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
160 P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
161 P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
162 P( F, G, H, A, B, C, D, E, R(35), 0x53380D13);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
163 P( E, F, G, H, A, B, C, D, R(36), 0x650A7354);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
164 P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
165 P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
166 P( B, C, D, E, F, G, H, A, R(39), 0x92722C85);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
167 P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
168 P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
169 P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
170 P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
171 P( E, F, G, H, A, B, C, D, R(44), 0xD192E819);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
172 P( D, E, F, G, H, A, B, C, R(45), 0xD6990624);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
173 P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
174 P( B, C, D, E, F, G, H, A, R(47), 0x106AA070);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
175 P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
176 P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
177 P( G, H, A, B, C, D, E, F, R(50), 0x2748774C);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
178 P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
179 P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
180 P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
181 P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
182 P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
183 P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
184 P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
185 P( G, H, A, B, C, D, E, F, R(58), 0x84C87814);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
186 P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
187 P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
188 P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
189 P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
190 P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
191
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
192 ctx->state[0] += A;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
193 ctx->state[1] += B;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
194 ctx->state[2] += C;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
195 ctx->state[3] += D;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
196 ctx->state[4] += E;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
197 ctx->state[5] += F;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
198 ctx->state[6] += G;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
199 ctx->state[7] += H;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
200 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
201
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
202 static void
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
203 sha256_update(ctx, input, length)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
204 context_sha256_T *ctx;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
205 char_u *input;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
206 uint32_t length;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
207 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
208 uint32_t left, fill;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
209
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
210 if (length == 0)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
211 return;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
212
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
213 left = ctx->total[0] & 0x3F;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
214 fill = 64 - left;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
215
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
216 ctx->total[0] += length;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
217 ctx->total[0] &= 0xFFFFFFFF;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
218
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
219 if (ctx->total[0] < length)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
220 ctx->total[1]++;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
221
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
222 if (left && length >= fill)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
223 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
224 memcpy((void *)(ctx->buffer + left), (void *)input, fill);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
225 sha256_process(ctx, ctx->buffer);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
226 length -= fill;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
227 input += fill;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
228 left = 0;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
229 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
230
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
231 while (length >= 64)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
232 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
233 sha256_process(ctx, input);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
234 length -= 64;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
235 input += 64;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
236 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
237
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
238 if (length)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
239 memcpy((void *)(ctx->buffer + left), (void *)input, length);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
240 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
241
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
242 static char_u sha256_padding[64] = {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
243 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
244 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
245 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
246 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
247 };
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
248
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
249 static void
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
250 sha256_finish(ctx, digest)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
251 context_sha256_T *ctx;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
252 char_u digest[32];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
253 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
254 uint32_t last, padn;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
255 uint32_t high, low;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
256 char_u msglen[8];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
257
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
258 high = (ctx->total[0] >> 29) | (ctx->total[1] << 3);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
259 low = (ctx->total[0] << 3);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
260
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
261 PUT_UINT32(high, msglen, 0);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
262 PUT_UINT32(low, msglen, 4);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
263
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
264 last = ctx->total[0] & 0x3F;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
265 padn = (last < 56) ? (56 - last) : (120 - last);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
266
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
267 sha256_update(ctx, sha256_padding, padn);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
268 sha256_update(ctx, msglen, 8);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
269
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
270 PUT_UINT32(ctx->state[0], digest, 0);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
271 PUT_UINT32(ctx->state[1], digest, 4);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
272 PUT_UINT32(ctx->state[2], digest, 8);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
273 PUT_UINT32(ctx->state[3], digest, 12);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
274 PUT_UINT32(ctx->state[4], digest, 16);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
275 PUT_UINT32(ctx->state[5], digest, 20);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
276 PUT_UINT32(ctx->state[6], digest, 24);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
277 PUT_UINT32(ctx->state[7], digest, 28);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
278 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
279
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
280 static char *
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
281 sha256_bytes(buf, buflen)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
282 char *buf;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
283 int buflen;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
284 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
285 char_u sha256sum[32];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
286 static char hexit[65];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
287 int j;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
288 context_sha256_T ctx;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
289
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
290 sha256_self_test();
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
291
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
292 sha256_starts(&ctx);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
293 sha256_update(&ctx, (char_u *)buf, buflen);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
294 sha256_finish(&ctx, sha256sum);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
295 for (j = 0; j < 32; j++)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
296 sprintf(hexit + j * 2, "%02x", sha256sum[j]);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
297 hexit[sizeof(hexit) - 1] = '\0';
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
298 return hexit;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
299 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
300
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
301 /*
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
302 * Returns sha256(buf) as 64 hex chars.
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
303 */
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
304 char *
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
305 sha256_key(buf)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
306 char *buf;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
307 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
308 static char *hexit = 0;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
309 int buflen;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
310
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
311 /* No passwd means don't encrypt */
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
312 if (buf == NULL || *buf == NUL)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
313 return "";
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
314
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
315 /* if password is "0", reuse previous hash, for user convienience. */
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
316 if (!strcmp(buf, "0") && hexit)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
317 return hexit;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
318
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
319 buflen = strlen(buf);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
320 hexit = sha256_bytes(buf, buflen);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
321 return hexit;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
322 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
323
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
324 /*
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
325 * These are the standard FIPS-180-2 test vectors
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
326 */
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
327
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
328 static char *sha_self_test_msg[] = {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
329 "abc",
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
330 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
331 NULL
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
332 };
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
333
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
334 static char *sha_self_test_vector[] = {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
335 "ba7816bf8f01cfea414140de5dae2223" \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
336 "b00361a396177a9cb410ff61f20015ad",
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
337 "248d6a61d20638b8e5c026930c3e6039" \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
338 "a33ce45964ff2167f6ecedd419db06c1",
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
339 "cdc76e5c9914fb9281a1c7e284d73e67" \
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
340 "f1809a48a497200e046d39ccc7112cd0"
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
341 };
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
342
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
343 /*
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
344 * Perform a test on the SHA256 algorithm.
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
345 * Return FAIL or OK.
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
346 */
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
347 int
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
348 sha256_self_test()
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
349 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
350 int i, j;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
351 char output[65];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
352 context_sha256_T ctx;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
353 char_u buf[1000];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
354 char_u sha256sum[32];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
355 static int failures = 0;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
356 char *hexit;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
357 static int sha256_self_tested = 0;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
358
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
359 if (sha256_self_tested > 0)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
360 return failures > 0 ? FAIL : OK;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
361 sha256_self_tested = 1;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
362
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
363 for (i = 0; i < 3; i++)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
364 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
365 if (i < 2)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
366 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
367 hexit = sha256_bytes(sha_self_test_msg[i],
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
368 strlen(sha_self_test_msg[i]));
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
369 strcpy(output, hexit);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
370 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
371 else
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
372 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
373 sha256_starts(&ctx);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
374 memset(buf, 'a', 1000);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
375 for (j = 0; j < 1000; j++)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
376 sha256_update(&ctx, (char_u *)buf, 1000);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
377 sha256_finish(&ctx, sha256sum);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
378 for (j = 0; j < 32; j++)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
379 sprintf(output + j * 2, "%02x", sha256sum[j]);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
380 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
381 if (memcmp(output, sha_self_test_vector[i], 64))
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
382 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
383 failures++;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
384 output[sizeof(output) - 1] = '\0';
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
385 /* printf("sha256_self_test %d failed %s\n", i, output); */
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
386 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
387 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
388 return failures > 0 ? FAIL : OK;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
389 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
390
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
391 static unsigned int
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
392 get_some_time()
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
393 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
394 #ifdef HAVE_GETTIMEOFDAY
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
395 struct timeval tv;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
396
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
397 /* Using usec makes it less predictable. */
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
398 gettimeofday(&tv, NULL);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
399 return (unsigned int)(tv.tv_sec + tv.tv_usec);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
400 #else
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
401 return (unsigned int)time(NULL);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
402 #endif
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
403 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
404
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
405 /*
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
406 * set header = sha2_seed(random_data);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
407 */
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
408 void
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
409 sha2_seed(header, header_len)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
410 char_u header[];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
411 int header_len;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
412 {
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
413 int i;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
414 static char_u random_data[1000];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
415 char_u sha256sum[32];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
416 context_sha256_T ctx;
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
417 srand(get_some_time());
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
418
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
419 for (i = 0; i < (int)sizeof(random_data) - 1; i++)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
420 random_data[i] = (char_u)((get_some_time() ^ rand()) & 0xff);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
421 sha256_starts(&ctx);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
422 sha256_update(&ctx, (char_u *)random_data, sizeof(random_data));
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
423 sha256_finish(&ctx, sha256sum);
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
424
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
425 for (i = 0; i < header_len; i++)
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
426 header[i] = sha256sum[i % sizeof(sha256sum)];
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
427 }
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
428
f60a0c9cbe6c Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
429 #endif /* FEAT_CRYPT */