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: wp512 - Use API partial block handling

Use the Crypto API partial block handling.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Tested-by: Milan Broz <gmazyland@gmail.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Herbert Xu 20d71750 df29f603

+47 -78
+47 -78
crypto/wp512.c
··· 21 21 */ 22 22 #include <crypto/internal/hash.h> 23 23 #include <linux/init.h> 24 + #include <linux/kernel.h> 24 25 #include <linux/module.h> 25 - #include <linux/mm.h> 26 - #include <asm/byteorder.h> 27 - #include <linux/types.h> 26 + #include <linux/string.h> 27 + #include <linux/unaligned.h> 28 28 29 29 #define WP512_DIGEST_SIZE 64 30 30 #define WP384_DIGEST_SIZE 48 ··· 37 37 38 38 struct wp512_ctx { 39 39 u8 bitLength[WP512_LENGTHBYTES]; 40 - u8 buffer[WP512_BLOCK_SIZE]; 41 - int bufferBits; 42 - int bufferPos; 43 40 u64 hash[WP512_DIGEST_SIZE/8]; 44 41 }; 45 42 ··· 776 779 * The core Whirlpool transform. 777 780 */ 778 781 779 - static __no_kmsan_checks void wp512_process_buffer(struct wp512_ctx *wctx) { 782 + static __no_kmsan_checks void wp512_process_buffer(struct wp512_ctx *wctx, 783 + const u8 *buffer) { 780 784 int i, r; 781 785 u64 K[8]; /* the round key */ 782 786 u64 block[8]; /* mu(buffer) */ 783 787 u64 state[8]; /* the cipher state */ 784 788 u64 L[8]; 785 - const __be64 *buffer = (const __be64 *)wctx->buffer; 786 789 787 790 for (i = 0; i < 8; i++) 788 - block[i] = be64_to_cpu(buffer[i]); 791 + block[i] = get_unaligned_be64(buffer + i * 8); 789 792 790 793 state[0] = block[0] ^ (K[0] = wctx->hash[0]); 791 794 state[1] = block[1] ^ (K[1] = wctx->hash[1]); ··· 988 991 int i; 989 992 990 993 memset(wctx->bitLength, 0, 32); 991 - wctx->bufferBits = wctx->bufferPos = 0; 992 - wctx->buffer[0] = 0; 993 994 for (i = 0; i < 8; i++) { 994 995 wctx->hash[i] = 0L; 995 996 } ··· 995 1000 return 0; 996 1001 } 997 1002 998 - static int wp512_update(struct shash_desc *desc, const u8 *source, 999 - unsigned int len) 1003 + static void wp512_add_length(u8 *bitLength, u64 value) 1000 1004 { 1001 - struct wp512_ctx *wctx = shash_desc_ctx(desc); 1002 - int sourcePos = 0; 1003 - unsigned int bits_len = len * 8; // convert to number of bits 1004 - int sourceGap = (8 - ((int)bits_len & 7)) & 7; 1005 - int bufferRem = wctx->bufferBits & 7; 1005 + u32 carry; 1006 1006 int i; 1007 - u32 b, carry; 1008 - u8 *buffer = wctx->buffer; 1009 - u8 *bitLength = wctx->bitLength; 1010 - int bufferBits = wctx->bufferBits; 1011 - int bufferPos = wctx->bufferPos; 1012 1007 1013 - u64 value = bits_len; 1014 1008 for (i = 31, carry = 0; i >= 0 && (carry != 0 || value != 0ULL); i--) { 1015 1009 carry += bitLength[i] + ((u32)value & 0xff); 1016 1010 bitLength[i] = (u8)carry; 1017 1011 carry >>= 8; 1018 1012 value >>= 8; 1019 1013 } 1020 - while (bits_len > 8) { 1021 - b = ((source[sourcePos] << sourceGap) & 0xff) | 1022 - ((source[sourcePos + 1] & 0xff) >> (8 - sourceGap)); 1023 - buffer[bufferPos++] |= (u8)(b >> bufferRem); 1024 - bufferBits += 8 - bufferRem; 1025 - if (bufferBits == WP512_BLOCK_SIZE * 8) { 1026 - wp512_process_buffer(wctx); 1027 - bufferBits = bufferPos = 0; 1028 - } 1029 - buffer[bufferPos] = b << (8 - bufferRem); 1030 - bufferBits += bufferRem; 1031 - bits_len -= 8; 1032 - sourcePos++; 1033 - } 1034 - if (bits_len > 0) { 1035 - b = (source[sourcePos] << sourceGap) & 0xff; 1036 - buffer[bufferPos] |= b >> bufferRem; 1037 - } else { 1038 - b = 0; 1039 - } 1040 - if (bufferRem + bits_len < 8) { 1041 - bufferBits += bits_len; 1042 - } else { 1043 - bufferPos++; 1044 - bufferBits += 8 - bufferRem; 1045 - bits_len -= 8 - bufferRem; 1046 - if (bufferBits == WP512_BLOCK_SIZE * 8) { 1047 - wp512_process_buffer(wctx); 1048 - bufferBits = bufferPos = 0; 1049 - } 1050 - buffer[bufferPos] = b << (8 - bufferRem); 1051 - bufferBits += (int)bits_len; 1052 - } 1053 - 1054 - wctx->bufferBits = bufferBits; 1055 - wctx->bufferPos = bufferPos; 1056 - 1057 - return 0; 1058 1014 } 1059 1015 1060 - static int wp512_final(struct shash_desc *desc, u8 *out) 1016 + static int wp512_update(struct shash_desc *desc, const u8 *source, 1017 + unsigned int len) 1018 + { 1019 + struct wp512_ctx *wctx = shash_desc_ctx(desc); 1020 + unsigned int remain = len % WP512_BLOCK_SIZE; 1021 + u64 bits_len = (len - remain) * 8ull; 1022 + u8 *bitLength = wctx->bitLength; 1023 + 1024 + wp512_add_length(bitLength, bits_len); 1025 + do { 1026 + wp512_process_buffer(wctx, source); 1027 + source += WP512_BLOCK_SIZE; 1028 + bits_len -= WP512_BLOCK_SIZE * 8; 1029 + } while (bits_len); 1030 + 1031 + return remain; 1032 + } 1033 + 1034 + static int wp512_finup(struct shash_desc *desc, const u8 *src, 1035 + unsigned int bufferPos, u8 *out) 1061 1036 { 1062 1037 struct wp512_ctx *wctx = shash_desc_ctx(desc); 1063 1038 int i; 1064 - u8 *buffer = wctx->buffer; 1065 1039 u8 *bitLength = wctx->bitLength; 1066 - int bufferBits = wctx->bufferBits; 1067 - int bufferPos = wctx->bufferPos; 1068 1040 __be64 *digest = (__be64 *)out; 1041 + u8 buffer[WP512_BLOCK_SIZE]; 1069 1042 1070 - buffer[bufferPos] |= 0x80U >> (bufferBits & 7); 1043 + wp512_add_length(bitLength, bufferPos * 8); 1044 + memcpy(buffer, src, bufferPos); 1045 + buffer[bufferPos] = 0x80U; 1071 1046 bufferPos++; 1072 1047 if (bufferPos > WP512_BLOCK_SIZE - WP512_LENGTHBYTES) { 1073 1048 if (bufferPos < WP512_BLOCK_SIZE) 1074 1049 memset(&buffer[bufferPos], 0, WP512_BLOCK_SIZE - bufferPos); 1075 - wp512_process_buffer(wctx); 1050 + wp512_process_buffer(wctx, buffer); 1076 1051 bufferPos = 0; 1077 1052 } 1078 1053 if (bufferPos < WP512_BLOCK_SIZE - WP512_LENGTHBYTES) ··· 1051 1086 bufferPos = WP512_BLOCK_SIZE - WP512_LENGTHBYTES; 1052 1087 memcpy(&buffer[WP512_BLOCK_SIZE - WP512_LENGTHBYTES], 1053 1088 bitLength, WP512_LENGTHBYTES); 1054 - wp512_process_buffer(wctx); 1089 + wp512_process_buffer(wctx, buffer); 1090 + memzero_explicit(buffer, sizeof(buffer)); 1055 1091 for (i = 0; i < WP512_DIGEST_SIZE/8; i++) 1056 1092 digest[i] = cpu_to_be64(wctx->hash[i]); 1057 - wctx->bufferBits = bufferBits; 1058 - wctx->bufferPos = bufferPos; 1059 1093 1060 1094 return 0; 1061 1095 } 1062 1096 1063 - static int wp384_final(struct shash_desc *desc, u8 *out) 1097 + static int wp384_finup(struct shash_desc *desc, const u8 *src, 1098 + unsigned int len, u8 *out) 1064 1099 { 1065 1100 u8 D[64]; 1066 1101 1067 - wp512_final(desc, D); 1102 + wp512_finup(desc, src, len, D); 1068 1103 memcpy(out, D, WP384_DIGEST_SIZE); 1069 1104 memzero_explicit(D, WP512_DIGEST_SIZE); 1070 1105 1071 1106 return 0; 1072 1107 } 1073 1108 1074 - static int wp256_final(struct shash_desc *desc, u8 *out) 1109 + static int wp256_finup(struct shash_desc *desc, const u8 *src, 1110 + unsigned int len, u8 *out) 1075 1111 { 1076 1112 u8 D[64]; 1077 1113 1078 - wp512_final(desc, D); 1114 + wp512_finup(desc, src, len, D); 1079 1115 memcpy(out, D, WP256_DIGEST_SIZE); 1080 1116 memzero_explicit(D, WP512_DIGEST_SIZE); 1081 1117 ··· 1087 1121 .digestsize = WP512_DIGEST_SIZE, 1088 1122 .init = wp512_init, 1089 1123 .update = wp512_update, 1090 - .final = wp512_final, 1124 + .finup = wp512_finup, 1091 1125 .descsize = sizeof(struct wp512_ctx), 1092 1126 .base = { 1093 1127 .cra_name = "wp512", 1094 1128 .cra_driver_name = "wp512-generic", 1129 + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 1095 1130 .cra_blocksize = WP512_BLOCK_SIZE, 1096 1131 .cra_module = THIS_MODULE, 1097 1132 } ··· 1100 1133 .digestsize = WP384_DIGEST_SIZE, 1101 1134 .init = wp512_init, 1102 1135 .update = wp512_update, 1103 - .final = wp384_final, 1136 + .finup = wp384_finup, 1104 1137 .descsize = sizeof(struct wp512_ctx), 1105 1138 .base = { 1106 1139 .cra_name = "wp384", 1107 1140 .cra_driver_name = "wp384-generic", 1141 + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 1108 1142 .cra_blocksize = WP512_BLOCK_SIZE, 1109 1143 .cra_module = THIS_MODULE, 1110 1144 } ··· 1113 1145 .digestsize = WP256_DIGEST_SIZE, 1114 1146 .init = wp512_init, 1115 1147 .update = wp512_update, 1116 - .final = wp256_final, 1148 + .finup = wp256_finup, 1117 1149 .descsize = sizeof(struct wp512_ctx), 1118 1150 .base = { 1119 1151 .cra_name = "wp256", 1120 1152 .cra_driver_name = "wp256-generic", 1153 + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 1121 1154 .cra_blocksize = WP512_BLOCK_SIZE, 1122 1155 .cra_module = THIS_MODULE, 1123 1156 }