| .. | .. |
|---|
| 11 | 11 | #include <crypto/internal/hash.h> |
|---|
| 12 | 12 | #include <linux/string.h> |
|---|
| 13 | 13 | |
|---|
| 14 | | -void blake2s_compress_generic(struct blake2s_state *state,const u8 *block, |
|---|
| 14 | +void blake2s_compress_generic(struct blake2s_state *state, const u8 *block, |
|---|
| 15 | 15 | size_t nblocks, const u32 inc); |
|---|
| 16 | 16 | |
|---|
| 17 | | -void blake2s_compress_arch(struct blake2s_state *state,const u8 *block, |
|---|
| 18 | | - size_t nblocks, const u32 inc); |
|---|
| 17 | +void blake2s_compress(struct blake2s_state *state, const u8 *block, |
|---|
| 18 | + size_t nblocks, const u32 inc); |
|---|
| 19 | 19 | |
|---|
| 20 | 20 | bool blake2s_selftest(void); |
|---|
| 21 | 21 | |
|---|
| .. | .. |
|---|
| 24 | 24 | state->f[0] = -1; |
|---|
| 25 | 25 | } |
|---|
| 26 | 26 | |
|---|
| 27 | | -typedef void (*blake2s_compress_t)(struct blake2s_state *state, |
|---|
| 28 | | - const u8 *block, size_t nblocks, u32 inc); |
|---|
| 29 | | - |
|---|
| 30 | 27 | /* Helper functions for BLAKE2s shared by the library and shash APIs */ |
|---|
| 31 | 28 | |
|---|
| 32 | | -static inline void __blake2s_update(struct blake2s_state *state, |
|---|
| 33 | | - const u8 *in, size_t inlen, |
|---|
| 34 | | - blake2s_compress_t compress) |
|---|
| 29 | +static __always_inline void |
|---|
| 30 | +__blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen, |
|---|
| 31 | + bool force_generic) |
|---|
| 35 | 32 | { |
|---|
| 36 | 33 | const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen; |
|---|
| 37 | 34 | |
|---|
| .. | .. |
|---|
| 39 | 36 | return; |
|---|
| 40 | 37 | if (inlen > fill) { |
|---|
| 41 | 38 | memcpy(state->buf + state->buflen, in, fill); |
|---|
| 42 | | - (*compress)(state, state->buf, 1, BLAKE2S_BLOCK_SIZE); |
|---|
| 39 | + if (force_generic) |
|---|
| 40 | + blake2s_compress_generic(state, state->buf, 1, |
|---|
| 41 | + BLAKE2S_BLOCK_SIZE); |
|---|
| 42 | + else |
|---|
| 43 | + blake2s_compress(state, state->buf, 1, |
|---|
| 44 | + BLAKE2S_BLOCK_SIZE); |
|---|
| 43 | 45 | state->buflen = 0; |
|---|
| 44 | 46 | in += fill; |
|---|
| 45 | 47 | inlen -= fill; |
|---|
| .. | .. |
|---|
| 47 | 49 | if (inlen > BLAKE2S_BLOCK_SIZE) { |
|---|
| 48 | 50 | const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2S_BLOCK_SIZE); |
|---|
| 49 | 51 | /* Hash one less (full) block than strictly possible */ |
|---|
| 50 | | - (*compress)(state, in, nblocks - 1, BLAKE2S_BLOCK_SIZE); |
|---|
| 52 | + if (force_generic) |
|---|
| 53 | + blake2s_compress_generic(state, in, nblocks - 1, |
|---|
| 54 | + BLAKE2S_BLOCK_SIZE); |
|---|
| 55 | + else |
|---|
| 56 | + blake2s_compress(state, in, nblocks - 1, |
|---|
| 57 | + BLAKE2S_BLOCK_SIZE); |
|---|
| 51 | 58 | in += BLAKE2S_BLOCK_SIZE * (nblocks - 1); |
|---|
| 52 | 59 | inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1); |
|---|
| 53 | 60 | } |
|---|
| .. | .. |
|---|
| 55 | 62 | state->buflen += inlen; |
|---|
| 56 | 63 | } |
|---|
| 57 | 64 | |
|---|
| 58 | | -static inline void __blake2s_final(struct blake2s_state *state, u8 *out, |
|---|
| 59 | | - blake2s_compress_t compress) |
|---|
| 65 | +static __always_inline void |
|---|
| 66 | +__blake2s_final(struct blake2s_state *state, u8 *out, bool force_generic) |
|---|
| 60 | 67 | { |
|---|
| 61 | 68 | blake2s_set_lastblock(state); |
|---|
| 62 | 69 | memset(state->buf + state->buflen, 0, |
|---|
| 63 | 70 | BLAKE2S_BLOCK_SIZE - state->buflen); /* Padding */ |
|---|
| 64 | | - (*compress)(state, state->buf, 1, state->buflen); |
|---|
| 71 | + if (force_generic) |
|---|
| 72 | + blake2s_compress_generic(state, state->buf, 1, state->buflen); |
|---|
| 73 | + else |
|---|
| 74 | + blake2s_compress(state, state->buf, 1, state->buflen); |
|---|
| 65 | 75 | cpu_to_le32_array(state->h, ARRAY_SIZE(state->h)); |
|---|
| 66 | 76 | memcpy(out, state->h, state->outlen); |
|---|
| 67 | 77 | } |
|---|
| .. | .. |
|---|
| 99 | 109 | |
|---|
| 100 | 110 | static inline int crypto_blake2s_update(struct shash_desc *desc, |
|---|
| 101 | 111 | const u8 *in, unsigned int inlen, |
|---|
| 102 | | - blake2s_compress_t compress) |
|---|
| 112 | + bool force_generic) |
|---|
| 103 | 113 | { |
|---|
| 104 | 114 | struct blake2s_state *state = shash_desc_ctx(desc); |
|---|
| 105 | 115 | |
|---|
| 106 | | - __blake2s_update(state, in, inlen, compress); |
|---|
| 116 | + __blake2s_update(state, in, inlen, force_generic); |
|---|
| 107 | 117 | return 0; |
|---|
| 108 | 118 | } |
|---|
| 109 | 119 | |
|---|
| 110 | 120 | static inline int crypto_blake2s_final(struct shash_desc *desc, u8 *out, |
|---|
| 111 | | - blake2s_compress_t compress) |
|---|
| 121 | + bool force_generic) |
|---|
| 112 | 122 | { |
|---|
| 113 | 123 | struct blake2s_state *state = shash_desc_ctx(desc); |
|---|
| 114 | 124 | |
|---|
| 115 | | - __blake2s_final(state, out, compress); |
|---|
| 125 | + __blake2s_final(state, out, force_generic); |
|---|
| 116 | 126 | return 0; |
|---|
| 117 | 127 | } |
|---|
| 118 | 128 | |
|---|