diff options
-rw-r--r-- | crypto/chacha20_generic.c | 6 | ||||
-rw-r--r-- | drivers/char/random.c | 24 | ||||
-rw-r--r-- | include/crypto/chacha20.h | 3 | ||||
-rw-r--r-- | lib/chacha20.c | 2 |
4 files changed, 18 insertions, 17 deletions
diff --git a/crypto/chacha20_generic.c b/crypto/chacha20_generic.c index bb4affbd591c..e451c3cb6a56 100644 --- a/crypto/chacha20_generic.c +++ b/crypto/chacha20_generic.c | |||
@@ -18,20 +18,20 @@ | |||
18 | static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src, | 18 | static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src, |
19 | unsigned int bytes) | 19 | unsigned int bytes) |
20 | { | 20 | { |
21 | u8 stream[CHACHA20_BLOCK_SIZE]; | 21 | u32 stream[CHACHA20_BLOCK_WORDS]; |
22 | 22 | ||
23 | if (dst != src) | 23 | if (dst != src) |
24 | memcpy(dst, src, bytes); | 24 | memcpy(dst, src, bytes); |
25 | 25 | ||
26 | while (bytes >= CHACHA20_BLOCK_SIZE) { | 26 | while (bytes >= CHACHA20_BLOCK_SIZE) { |
27 | chacha20_block(state, stream); | 27 | chacha20_block(state, stream); |
28 | crypto_xor(dst, stream, CHACHA20_BLOCK_SIZE); | 28 | crypto_xor(dst, (const u8 *)stream, CHACHA20_BLOCK_SIZE); |
29 | bytes -= CHACHA20_BLOCK_SIZE; | 29 | bytes -= CHACHA20_BLOCK_SIZE; |
30 | dst += CHACHA20_BLOCK_SIZE; | 30 | dst += CHACHA20_BLOCK_SIZE; |
31 | } | 31 | } |
32 | if (bytes) { | 32 | if (bytes) { |
33 | chacha20_block(state, stream); | 33 | chacha20_block(state, stream); |
34 | crypto_xor(dst, stream, bytes); | 34 | crypto_xor(dst, (const u8 *)stream, bytes); |
35 | } | 35 | } |
36 | } | 36 | } |
37 | 37 | ||
diff --git a/drivers/char/random.c b/drivers/char/random.c index ec42c8bb9b0d..11304bbc78cc 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c | |||
@@ -431,9 +431,9 @@ static int crng_init = 0; | |||
431 | static int crng_init_cnt = 0; | 431 | static int crng_init_cnt = 0; |
432 | #define CRNG_INIT_CNT_THRESH (2*CHACHA20_KEY_SIZE) | 432 | #define CRNG_INIT_CNT_THRESH (2*CHACHA20_KEY_SIZE) |
433 | static void _extract_crng(struct crng_state *crng, | 433 | static void _extract_crng(struct crng_state *crng, |
434 | __u8 out[CHACHA20_BLOCK_SIZE]); | 434 | __u32 out[CHACHA20_BLOCK_WORDS]); |
435 | static void _crng_backtrack_protect(struct crng_state *crng, | 435 | static void _crng_backtrack_protect(struct crng_state *crng, |
436 | __u8 tmp[CHACHA20_BLOCK_SIZE], int used); | 436 | __u32 tmp[CHACHA20_BLOCK_WORDS], int used); |
437 | static void process_random_ready_list(void); | 437 | static void process_random_ready_list(void); |
438 | static void _get_random_bytes(void *buf, int nbytes); | 438 | static void _get_random_bytes(void *buf, int nbytes); |
439 | 439 | ||
@@ -817,7 +817,7 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r) | |||
817 | unsigned long flags; | 817 | unsigned long flags; |
818 | int i, num; | 818 | int i, num; |
819 | union { | 819 | union { |
820 | __u8 block[CHACHA20_BLOCK_SIZE]; | 820 | __u32 block[CHACHA20_BLOCK_WORDS]; |
821 | __u32 key[8]; | 821 | __u32 key[8]; |
822 | } buf; | 822 | } buf; |
823 | 823 | ||
@@ -851,7 +851,7 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r) | |||
851 | } | 851 | } |
852 | 852 | ||
853 | static void _extract_crng(struct crng_state *crng, | 853 | static void _extract_crng(struct crng_state *crng, |
854 | __u8 out[CHACHA20_BLOCK_SIZE]) | 854 | __u32 out[CHACHA20_BLOCK_WORDS]) |
855 | { | 855 | { |
856 | unsigned long v, flags; | 856 | unsigned long v, flags; |
857 | 857 | ||
@@ -867,7 +867,7 @@ static void _extract_crng(struct crng_state *crng, | |||
867 | spin_unlock_irqrestore(&crng->lock, flags); | 867 | spin_unlock_irqrestore(&crng->lock, flags); |
868 | } | 868 | } |
869 | 869 | ||
870 | static void extract_crng(__u8 out[CHACHA20_BLOCK_SIZE]) | 870 | static void extract_crng(__u32 out[CHACHA20_BLOCK_WORDS]) |
871 | { | 871 | { |
872 | struct crng_state *crng = NULL; | 872 | struct crng_state *crng = NULL; |
873 | 873 | ||
@@ -885,7 +885,7 @@ static void extract_crng(__u8 out[CHACHA20_BLOCK_SIZE]) | |||
885 | * enough) to mutate the CRNG key to provide backtracking protection. | 885 | * enough) to mutate the CRNG key to provide backtracking protection. |
886 | */ | 886 | */ |
887 | static void _crng_backtrack_protect(struct crng_state *crng, | 887 | static void _crng_backtrack_protect(struct crng_state *crng, |
888 | __u8 tmp[CHACHA20_BLOCK_SIZE], int used) | 888 | __u32 tmp[CHACHA20_BLOCK_WORDS], int used) |
889 | { | 889 | { |
890 | unsigned long flags; | 890 | unsigned long flags; |
891 | __u32 *s, *d; | 891 | __u32 *s, *d; |
@@ -897,14 +897,14 @@ static void _crng_backtrack_protect(struct crng_state *crng, | |||
897 | used = 0; | 897 | used = 0; |
898 | } | 898 | } |
899 | spin_lock_irqsave(&crng->lock, flags); | 899 | spin_lock_irqsave(&crng->lock, flags); |
900 | s = (__u32 *) &tmp[used]; | 900 | s = &tmp[used / sizeof(__u32)]; |
901 | d = &crng->state[4]; | 901 | d = &crng->state[4]; |
902 | for (i=0; i < 8; i++) | 902 | for (i=0; i < 8; i++) |
903 | *d++ ^= *s++; | 903 | *d++ ^= *s++; |
904 | spin_unlock_irqrestore(&crng->lock, flags); | 904 | spin_unlock_irqrestore(&crng->lock, flags); |
905 | } | 905 | } |
906 | 906 | ||
907 | static void crng_backtrack_protect(__u8 tmp[CHACHA20_BLOCK_SIZE], int used) | 907 | static void crng_backtrack_protect(__u32 tmp[CHACHA20_BLOCK_WORDS], int used) |
908 | { | 908 | { |
909 | struct crng_state *crng = NULL; | 909 | struct crng_state *crng = NULL; |
910 | 910 | ||
@@ -920,7 +920,7 @@ static void crng_backtrack_protect(__u8 tmp[CHACHA20_BLOCK_SIZE], int used) | |||
920 | static ssize_t extract_crng_user(void __user *buf, size_t nbytes) | 920 | static ssize_t extract_crng_user(void __user *buf, size_t nbytes) |
921 | { | 921 | { |
922 | ssize_t ret = 0, i = CHACHA20_BLOCK_SIZE; | 922 | ssize_t ret = 0, i = CHACHA20_BLOCK_SIZE; |
923 | __u8 tmp[CHACHA20_BLOCK_SIZE]; | 923 | __u32 tmp[CHACHA20_BLOCK_WORDS]; |
924 | int large_request = (nbytes > 256); | 924 | int large_request = (nbytes > 256); |
925 | 925 | ||
926 | while (nbytes) { | 926 | while (nbytes) { |
@@ -1507,7 +1507,7 @@ static void _warn_unseeded_randomness(const char *func_name, void *caller, | |||
1507 | */ | 1507 | */ |
1508 | static void _get_random_bytes(void *buf, int nbytes) | 1508 | static void _get_random_bytes(void *buf, int nbytes) |
1509 | { | 1509 | { |
1510 | __u8 tmp[CHACHA20_BLOCK_SIZE]; | 1510 | __u32 tmp[CHACHA20_BLOCK_WORDS]; |
1511 | 1511 | ||
1512 | trace_get_random_bytes(nbytes, _RET_IP_); | 1512 | trace_get_random_bytes(nbytes, _RET_IP_); |
1513 | 1513 | ||
@@ -2114,7 +2114,7 @@ u64 get_random_u64(void) | |||
2114 | if (use_lock) | 2114 | if (use_lock) |
2115 | read_lock_irqsave(&batched_entropy_reset_lock, flags); | 2115 | read_lock_irqsave(&batched_entropy_reset_lock, flags); |
2116 | if (batch->position % ARRAY_SIZE(batch->entropy_u64) == 0) { | 2116 | if (batch->position % ARRAY_SIZE(batch->entropy_u64) == 0) { |
2117 | extract_crng((u8 *)batch->entropy_u64); | 2117 | extract_crng((__u32 *)batch->entropy_u64); |
2118 | batch->position = 0; | 2118 | batch->position = 0; |
2119 | } | 2119 | } |
2120 | ret = batch->entropy_u64[batch->position++]; | 2120 | ret = batch->entropy_u64[batch->position++]; |
@@ -2144,7 +2144,7 @@ u32 get_random_u32(void) | |||
2144 | if (use_lock) | 2144 | if (use_lock) |
2145 | read_lock_irqsave(&batched_entropy_reset_lock, flags); | 2145 | read_lock_irqsave(&batched_entropy_reset_lock, flags); |
2146 | if (batch->position % ARRAY_SIZE(batch->entropy_u32) == 0) { | 2146 | if (batch->position % ARRAY_SIZE(batch->entropy_u32) == 0) { |
2147 | extract_crng((u8 *)batch->entropy_u32); | 2147 | extract_crng(batch->entropy_u32); |
2148 | batch->position = 0; | 2148 | batch->position = 0; |
2149 | } | 2149 | } |
2150 | ret = batch->entropy_u32[batch->position++]; | 2150 | ret = batch->entropy_u32[batch->position++]; |
diff --git a/include/crypto/chacha20.h b/include/crypto/chacha20.h index caaa470389e0..b83d66073db0 100644 --- a/include/crypto/chacha20.h +++ b/include/crypto/chacha20.h | |||
@@ -13,12 +13,13 @@ | |||
13 | #define CHACHA20_IV_SIZE 16 | 13 | #define CHACHA20_IV_SIZE 16 |
14 | #define CHACHA20_KEY_SIZE 32 | 14 | #define CHACHA20_KEY_SIZE 32 |
15 | #define CHACHA20_BLOCK_SIZE 64 | 15 | #define CHACHA20_BLOCK_SIZE 64 |
16 | #define CHACHA20_BLOCK_WORDS (CHACHA20_BLOCK_SIZE / sizeof(u32)) | ||
16 | 17 | ||
17 | struct chacha20_ctx { | 18 | struct chacha20_ctx { |
18 | u32 key[8]; | 19 | u32 key[8]; |
19 | }; | 20 | }; |
20 | 21 | ||
21 | void chacha20_block(u32 *state, void *stream); | 22 | void chacha20_block(u32 *state, u32 *stream); |
22 | void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv); | 23 | void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv); |
23 | int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key, | 24 | int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key, |
24 | unsigned int keysize); | 25 | unsigned int keysize); |
diff --git a/lib/chacha20.c b/lib/chacha20.c index 250ceed9ec9a..29d3801dee24 100644 --- a/lib/chacha20.c +++ b/lib/chacha20.c | |||
@@ -21,7 +21,7 @@ static inline u32 rotl32(u32 v, u8 n) | |||
21 | return (v << n) | (v >> (sizeof(v) * 8 - n)); | 21 | return (v << n) | (v >> (sizeof(v) * 8 - n)); |
22 | } | 22 | } |
23 | 23 | ||
24 | extern void chacha20_block(u32 *state, void *stream) | 24 | void chacha20_block(u32 *state, u32 *stream) |
25 | { | 25 | { |
26 | u32 x[16], *out = stream; | 26 | u32 x[16], *out = stream; |
27 | int i; | 27 | int i; |