Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

crypto: mips/octeon-sha512 - Use API partial block handling

Use the Crypto API partial block handling.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

+42 -113
+42 -113
arch/mips/cavium-octeon/crypto/octeon-sha512.c
··· 13 13 * Copyright (c) 2003 Kyle McMartin <kyle@debian.org> 14 14 */ 15 15 16 - #include <linux/mm.h> 17 - #include <crypto/sha2.h> 18 - #include <crypto/sha512_base.h> 19 - #include <linux/init.h> 20 - #include <linux/types.h> 21 - #include <linux/module.h> 22 - #include <asm/byteorder.h> 23 16 #include <asm/octeon/octeon.h> 24 17 #include <crypto/internal/hash.h> 18 + #include <crypto/sha2.h> 19 + #include <crypto/sha512_base.h> 20 + #include <linux/kernel.h> 21 + #include <linux/module.h> 25 22 26 23 #include "octeon-crypto.h" 27 24 ··· 50 53 sctx->state[7] = read_octeon_64bit_hash_sha512(7); 51 54 } 52 55 53 - static void octeon_sha512_transform(const void *_block) 56 + static void octeon_sha512_transform(struct sha512_state *sctx, 57 + const u8 *src, int blocks) 54 58 { 55 - const u64 *block = _block; 59 + do { 60 + const u64 *block = (const u64 *)src; 56 61 57 - write_octeon_64bit_block_sha512(block[0], 0); 58 - write_octeon_64bit_block_sha512(block[1], 1); 59 - write_octeon_64bit_block_sha512(block[2], 2); 60 - write_octeon_64bit_block_sha512(block[3], 3); 61 - write_octeon_64bit_block_sha512(block[4], 4); 62 - write_octeon_64bit_block_sha512(block[5], 5); 63 - write_octeon_64bit_block_sha512(block[6], 6); 64 - write_octeon_64bit_block_sha512(block[7], 7); 65 - write_octeon_64bit_block_sha512(block[8], 8); 66 - write_octeon_64bit_block_sha512(block[9], 9); 67 - write_octeon_64bit_block_sha512(block[10], 10); 68 - write_octeon_64bit_block_sha512(block[11], 11); 69 - write_octeon_64bit_block_sha512(block[12], 12); 70 - write_octeon_64bit_block_sha512(block[13], 13); 71 - write_octeon_64bit_block_sha512(block[14], 14); 72 - octeon_sha512_start(block[15]); 73 - } 62 + write_octeon_64bit_block_sha512(block[0], 0); 63 + write_octeon_64bit_block_sha512(block[1], 1); 64 + write_octeon_64bit_block_sha512(block[2], 2); 65 + write_octeon_64bit_block_sha512(block[3], 3); 66 + write_octeon_64bit_block_sha512(block[4], 4); 67 + write_octeon_64bit_block_sha512(block[5], 5); 68 + write_octeon_64bit_block_sha512(block[6], 6); 69 + write_octeon_64bit_block_sha512(block[7], 7); 70 + write_octeon_64bit_block_sha512(block[8], 8); 71 + write_octeon_64bit_block_sha512(block[9], 9); 72 + write_octeon_64bit_block_sha512(block[10], 10); 73 + write_octeon_64bit_block_sha512(block[11], 11); 74 + write_octeon_64bit_block_sha512(block[12], 12); 75 + write_octeon_64bit_block_sha512(block[13], 13); 76 + write_octeon_64bit_block_sha512(block[14], 14); 77 + octeon_sha512_start(block[15]); 74 78 75 - static void __octeon_sha512_update(struct sha512_state *sctx, const u8 *data, 76 - unsigned int len) 77 - { 78 - unsigned int part_len; 79 - unsigned int index; 80 - unsigned int i; 81 - 82 - /* Compute number of bytes mod 128. */ 83 - index = sctx->count[0] % SHA512_BLOCK_SIZE; 84 - 85 - /* Update number of bytes. */ 86 - if ((sctx->count[0] += len) < len) 87 - sctx->count[1]++; 88 - 89 - part_len = SHA512_BLOCK_SIZE - index; 90 - 91 - /* Transform as many times as possible. */ 92 - if (len >= part_len) { 93 - memcpy(&sctx->buf[index], data, part_len); 94 - octeon_sha512_transform(sctx->buf); 95 - 96 - for (i = part_len; i + SHA512_BLOCK_SIZE <= len; 97 - i += SHA512_BLOCK_SIZE) 98 - octeon_sha512_transform(&data[i]); 99 - 100 - index = 0; 101 - } else { 102 - i = 0; 103 - } 104 - 105 - /* Buffer remaining input. */ 106 - memcpy(&sctx->buf[index], &data[i], len - i); 79 + src += SHA512_BLOCK_SIZE; 80 + } while (--blocks); 107 81 } 108 82 109 83 static int octeon_sha512_update(struct shash_desc *desc, const u8 *data, ··· 83 115 struct sha512_state *sctx = shash_desc_ctx(desc); 84 116 struct octeon_cop2_state state; 85 117 unsigned long flags; 86 - 87 - /* 88 - * Small updates never reach the crypto engine, so the generic sha512 is 89 - * faster because of the heavyweight octeon_crypto_enable() / 90 - * octeon_crypto_disable(). 91 - */ 92 - if ((sctx->count[0] % SHA512_BLOCK_SIZE) + len < SHA512_BLOCK_SIZE) 93 - return crypto_sha512_update(desc, data, len); 118 + int remain; 94 119 95 120 flags = octeon_crypto_enable(&state); 96 121 octeon_sha512_store_hash(sctx); 97 122 98 - __octeon_sha512_update(sctx, data, len); 123 + remain = sha512_base_do_update_blocks(desc, data, len, 124 + octeon_sha512_transform); 99 125 100 126 octeon_sha512_read_hash(sctx); 101 127 octeon_crypto_disable(&state, flags); 102 - 103 - return 0; 128 + return remain; 104 129 } 105 130 106 - static int octeon_sha512_final(struct shash_desc *desc, u8 *hash) 131 + static int octeon_sha512_finup(struct shash_desc *desc, const u8 *src, 132 + unsigned int len, u8 *hash) 107 133 { 108 134 struct sha512_state *sctx = shash_desc_ctx(desc); 109 - static u8 padding[128] = { 0x80, }; 110 135 struct octeon_cop2_state state; 111 - __be64 *dst = (__be64 *)hash; 112 - unsigned int pad_len; 113 136 unsigned long flags; 114 - unsigned int index; 115 - __be64 bits[2]; 116 - int i; 117 - 118 - /* Save number of bits. */ 119 - bits[1] = cpu_to_be64(sctx->count[0] << 3); 120 - bits[0] = cpu_to_be64(sctx->count[1] << 3 | sctx->count[0] >> 61); 121 - 122 - /* Pad out to 112 mod 128. */ 123 - index = sctx->count[0] & 0x7f; 124 - pad_len = (index < 112) ? (112 - index) : ((128+112) - index); 125 137 126 138 flags = octeon_crypto_enable(&state); 127 139 octeon_sha512_store_hash(sctx); 128 140 129 - __octeon_sha512_update(sctx, padding, pad_len); 130 - 131 - /* Append length (before padding). */ 132 - __octeon_sha512_update(sctx, (const u8 *)bits, sizeof(bits)); 141 + sha512_base_do_finup(desc, src, len, octeon_sha512_transform); 133 142 134 143 octeon_sha512_read_hash(sctx); 135 144 octeon_crypto_disable(&state, flags); 136 - 137 - /* Store state in digest. */ 138 - for (i = 0; i < 8; i++) 139 - dst[i] = cpu_to_be64(sctx->state[i]); 140 - 141 - /* Zeroize sensitive information. */ 142 - memset(sctx, 0, sizeof(struct sha512_state)); 143 - 144 - return 0; 145 - } 146 - 147 - static int octeon_sha384_final(struct shash_desc *desc, u8 *hash) 148 - { 149 - u8 D[64]; 150 - 151 - octeon_sha512_final(desc, D); 152 - 153 - memcpy(hash, D, 48); 154 - memzero_explicit(D, 64); 155 - 156 - return 0; 145 + return sha512_base_finish(desc, hash); 157 146 } 158 147 159 148 static struct shash_alg octeon_sha512_algs[2] = { { 160 149 .digestsize = SHA512_DIGEST_SIZE, 161 150 .init = sha512_base_init, 162 151 .update = octeon_sha512_update, 163 - .final = octeon_sha512_final, 164 - .descsize = sizeof(struct sha512_state), 152 + .finup = octeon_sha512_finup, 153 + .descsize = SHA512_STATE_SIZE, 165 154 .base = { 166 155 .cra_name = "sha512", 167 156 .cra_driver_name= "octeon-sha512", 168 157 .cra_priority = OCTEON_CR_OPCODE_PRIORITY, 158 + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY | 159 + CRYPTO_AHASH_ALG_FINUP_MAX, 169 160 .cra_blocksize = SHA512_BLOCK_SIZE, 170 161 .cra_module = THIS_MODULE, 171 162 } ··· 132 205 .digestsize = SHA384_DIGEST_SIZE, 133 206 .init = sha384_base_init, 134 207 .update = octeon_sha512_update, 135 - .final = octeon_sha384_final, 136 - .descsize = sizeof(struct sha512_state), 208 + .finup = octeon_sha512_finup, 209 + .descsize = SHA512_STATE_SIZE, 137 210 .base = { 138 211 .cra_name = "sha384", 139 212 .cra_driver_name= "octeon-sha384", 140 213 .cra_priority = OCTEON_CR_OPCODE_PRIORITY, 214 + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY | 215 + CRYPTO_AHASH_ALG_FINUP_MAX, 141 216 .cra_blocksize = SHA384_BLOCK_SIZE, 142 217 .cra_module = THIS_MODULE, 143 218 }