[PATCH] s390: aes crypto code fixes

Call KM[C] only with a multiple of block size. Check return value of KM[C]
instructions and complain about erros

Signed-off-by: Jan Glauber <jan.glauber@de.ibm.com>
Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

authored by Jan Glauber and committed by Linus Torvalds fda5e142 b8dc6038

+44 -16
+44 -16
arch/s390/crypto/aes_s390.c
··· 114 const u8 *in, unsigned int nbytes) 115 { 116 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm); 117 118 switch (sctx->key_len) { 119 case 16: 120 - crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in, nbytes); 121 break; 122 case 24: 123 - crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in, nbytes); 124 break; 125 case 32: 126 - crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in, nbytes); 127 break; 128 } 129 - return nbytes & ~(AES_BLOCK_SIZE - 1); 130 } 131 132 static unsigned int aes_decrypt_ecb(const struct cipher_desc *desc, u8 *out, 133 const u8 *in, unsigned int nbytes) 134 { 135 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm); 136 137 switch (sctx->key_len) { 138 case 16: 139 - crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in, nbytes); 140 break; 141 case 24: 142 - crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in, nbytes); 143 break; 144 case 32: 145 - crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in, nbytes); 146 break; 147 } 148 - return nbytes & ~(AES_BLOCK_SIZE - 1); 149 } 150 151 static unsigned int aes_encrypt_cbc(const struct cipher_desc *desc, u8 *out, 152 const u8 *in, unsigned int nbytes) 153 { 154 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm); 155 156 memcpy(&sctx->iv, desc->info, AES_BLOCK_SIZE); 157 switch (sctx->key_len) { 158 case 16: 159 - crypt_s390_kmc(KMC_AES_128_ENCRYPT, &sctx->iv, out, in, nbytes); 160 break; 161 case 24: 162 - crypt_s390_kmc(KMC_AES_192_ENCRYPT, &sctx->iv, out, in, nbytes); 163 break; 164 case 32: 165 - crypt_s390_kmc(KMC_AES_256_ENCRYPT, &sctx->iv, out, in, nbytes); 166 break; 167 } 168 memcpy(desc->info, &sctx->iv, AES_BLOCK_SIZE); 169 170 - return nbytes & ~(AES_BLOCK_SIZE - 1); 171 } 172 173 static unsigned int aes_decrypt_cbc(const struct cipher_desc *desc, u8 *out, 174 const u8 *in, unsigned int nbytes) 175 { 176 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm); 177 178 memcpy(&sctx->iv, desc->info, AES_BLOCK_SIZE); 179 switch (sctx->key_len) { 180 case 16: 181 - crypt_s390_kmc(KMC_AES_128_DECRYPT, &sctx->iv, out, in, nbytes); 182 break; 183 case 24: 184 - crypt_s390_kmc(KMC_AES_192_DECRYPT, &sctx->iv, out, in, nbytes); 185 break; 186 case 32: 187 - crypt_s390_kmc(KMC_AES_256_DECRYPT, &sctx->iv, out, in, nbytes); 188 break; 189 } 190 - return nbytes & ~(AES_BLOCK_SIZE - 1); 191 } 192 193
··· 114 const u8 *in, unsigned int nbytes) 115 { 116 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm); 117 + int ret; 118 + 119 + /* only use complete blocks */ 120 + nbytes &= ~(AES_BLOCK_SIZE - 1); 121 122 switch (sctx->key_len) { 123 case 16: 124 + ret = crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in, nbytes); 125 + BUG_ON((ret < 0) || (ret != nbytes)); 126 break; 127 case 24: 128 + ret = crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in, nbytes); 129 + BUG_ON((ret < 0) || (ret != nbytes)); 130 break; 131 case 32: 132 + ret = crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in, nbytes); 133 + BUG_ON((ret < 0) || (ret != nbytes)); 134 break; 135 } 136 + return nbytes; 137 } 138 139 static unsigned int aes_decrypt_ecb(const struct cipher_desc *desc, u8 *out, 140 const u8 *in, unsigned int nbytes) 141 { 142 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm); 143 + int ret; 144 + 145 + /* only use complete blocks */ 146 + nbytes &= ~(AES_BLOCK_SIZE - 1); 147 148 switch (sctx->key_len) { 149 case 16: 150 + ret = crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in, nbytes); 151 + BUG_ON((ret < 0) || (ret != nbytes)); 152 break; 153 case 24: 154 + ret = crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in, nbytes); 155 + BUG_ON((ret < 0) || (ret != nbytes)); 156 break; 157 case 32: 158 + ret = crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in, nbytes); 159 + BUG_ON((ret < 0) || (ret != nbytes)); 160 break; 161 } 162 + return nbytes; 163 } 164 165 static unsigned int aes_encrypt_cbc(const struct cipher_desc *desc, u8 *out, 166 const u8 *in, unsigned int nbytes) 167 { 168 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm); 169 + int ret; 170 + 171 + /* only use complete blocks */ 172 + nbytes &= ~(AES_BLOCK_SIZE - 1); 173 174 memcpy(&sctx->iv, desc->info, AES_BLOCK_SIZE); 175 switch (sctx->key_len) { 176 case 16: 177 + ret = crypt_s390_kmc(KMC_AES_128_ENCRYPT, &sctx->iv, out, in, nbytes); 178 + BUG_ON((ret < 0) || (ret != nbytes)); 179 break; 180 case 24: 181 + ret = crypt_s390_kmc(KMC_AES_192_ENCRYPT, &sctx->iv, out, in, nbytes); 182 + BUG_ON((ret < 0) || (ret != nbytes)); 183 break; 184 case 32: 185 + ret = crypt_s390_kmc(KMC_AES_256_ENCRYPT, &sctx->iv, out, in, nbytes); 186 + BUG_ON((ret < 0) || (ret != nbytes)); 187 break; 188 } 189 memcpy(desc->info, &sctx->iv, AES_BLOCK_SIZE); 190 191 + return nbytes; 192 } 193 194 static unsigned int aes_decrypt_cbc(const struct cipher_desc *desc, u8 *out, 195 const u8 *in, unsigned int nbytes) 196 { 197 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm); 198 + int ret; 199 + 200 + /* only use complete blocks */ 201 + nbytes &= ~(AES_BLOCK_SIZE - 1); 202 203 memcpy(&sctx->iv, desc->info, AES_BLOCK_SIZE); 204 switch (sctx->key_len) { 205 case 16: 206 + ret = crypt_s390_kmc(KMC_AES_128_DECRYPT, &sctx->iv, out, in, nbytes); 207 + BUG_ON((ret < 0) || (ret != nbytes)); 208 break; 209 case 24: 210 + ret = crypt_s390_kmc(KMC_AES_192_DECRYPT, &sctx->iv, out, in, nbytes); 211 + BUG_ON((ret < 0) || (ret != nbytes)); 212 break; 213 case 32: 214 + ret = crypt_s390_kmc(KMC_AES_256_DECRYPT, &sctx->iv, out, in, nbytes); 215 + BUG_ON((ret < 0) || (ret != nbytes)); 216 break; 217 } 218 + return nbytes; 219 } 220 221