diff options
author | Jussi Kivilinna <jussi.kivilinna@iki.fi> | 2013-04-07 09:43:41 -0400 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2013-04-25 09:01:46 -0400 |
commit | 9489667d3e3d39ba452037585e48a89ce44ccbfe (patch) | |
tree | 130eaf3e4184eea42dc5f5ff409cf0b4f76fa76e /crypto | |
parent | b149a30d87d165e1079163fdab6f14e48b2f57b2 (diff) |
crypto: gcm - make GMAC work when dst and src are different
The GMAC code assumes that dst==src, which causes problems when trying to add
rfc4543(gcm(aes)) test vectors.
So fix this code to work when source and destination buffer are different.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/Kconfig | 1 | ||||
-rw-r--r-- | crypto/gcm.c | 97 |
2 files changed, 81 insertions, 17 deletions
diff --git a/crypto/Kconfig b/crypto/Kconfig index a654b13ae004..6cc27f111551 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig | |||
@@ -198,6 +198,7 @@ config CRYPTO_GCM | |||
198 | select CRYPTO_CTR | 198 | select CRYPTO_CTR |
199 | select CRYPTO_AEAD | 199 | select CRYPTO_AEAD |
200 | select CRYPTO_GHASH | 200 | select CRYPTO_GHASH |
201 | select CRYPTO_NULL | ||
201 | help | 202 | help |
202 | Support for Galois/Counter Mode (GCM) and Galois Message | 203 | Support for Galois/Counter Mode (GCM) and Galois Message |
203 | Authentication Code (GMAC). Required for IPSec. | 204 | Authentication Code (GMAC). Required for IPSec. |
diff --git a/crypto/gcm.c b/crypto/gcm.c index 137ad1ec5438..4ff213997fbd 100644 --- a/crypto/gcm.c +++ b/crypto/gcm.c | |||
@@ -37,8 +37,14 @@ struct crypto_rfc4106_ctx { | |||
37 | u8 nonce[4]; | 37 | u8 nonce[4]; |
38 | }; | 38 | }; |
39 | 39 | ||
40 | struct crypto_rfc4543_instance_ctx { | ||
41 | struct crypto_aead_spawn aead; | ||
42 | struct crypto_skcipher_spawn null; | ||
43 | }; | ||
44 | |||
40 | struct crypto_rfc4543_ctx { | 45 | struct crypto_rfc4543_ctx { |
41 | struct crypto_aead *child; | 46 | struct crypto_aead *child; |
47 | struct crypto_blkcipher *null; | ||
42 | u8 nonce[4]; | 48 | u8 nonce[4]; |
43 | }; | 49 | }; |
44 | 50 | ||
@@ -1094,20 +1100,20 @@ static int crypto_rfc4543_setauthsize(struct crypto_aead *parent, | |||
1094 | } | 1100 | } |
1095 | 1101 | ||
1096 | static struct aead_request *crypto_rfc4543_crypt(struct aead_request *req, | 1102 | static struct aead_request *crypto_rfc4543_crypt(struct aead_request *req, |
1097 | int enc) | 1103 | bool enc) |
1098 | { | 1104 | { |
1099 | struct crypto_aead *aead = crypto_aead_reqtfm(req); | 1105 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
1100 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead); | 1106 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead); |
1101 | struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req); | 1107 | struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req); |
1102 | struct aead_request *subreq = &rctx->subreq; | 1108 | struct aead_request *subreq = &rctx->subreq; |
1103 | struct scatterlist *dst = req->dst; | 1109 | struct scatterlist *src = req->src; |
1104 | struct scatterlist *cipher = rctx->cipher; | 1110 | struct scatterlist *cipher = rctx->cipher; |
1105 | struct scatterlist *payload = rctx->payload; | 1111 | struct scatterlist *payload = rctx->payload; |
1106 | struct scatterlist *assoc = rctx->assoc; | 1112 | struct scatterlist *assoc = rctx->assoc; |
1107 | unsigned int authsize = crypto_aead_authsize(aead); | 1113 | unsigned int authsize = crypto_aead_authsize(aead); |
1108 | unsigned int assoclen = req->assoclen; | 1114 | unsigned int assoclen = req->assoclen; |
1109 | struct page *dstp; | 1115 | struct page *srcp; |
1110 | u8 *vdst; | 1116 | u8 *vsrc; |
1111 | u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child), | 1117 | u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child), |
1112 | crypto_aead_alignmask(ctx->child) + 1); | 1118 | crypto_aead_alignmask(ctx->child) + 1); |
1113 | 1119 | ||
@@ -1118,19 +1124,19 @@ static struct aead_request *crypto_rfc4543_crypt(struct aead_request *req, | |||
1118 | if (enc) | 1124 | if (enc) |
1119 | memset(rctx->auth_tag, 0, authsize); | 1125 | memset(rctx->auth_tag, 0, authsize); |
1120 | else | 1126 | else |
1121 | scatterwalk_map_and_copy(rctx->auth_tag, dst, | 1127 | scatterwalk_map_and_copy(rctx->auth_tag, src, |
1122 | req->cryptlen - authsize, | 1128 | req->cryptlen - authsize, |
1123 | authsize, 0); | 1129 | authsize, 0); |
1124 | 1130 | ||
1125 | sg_init_one(cipher, rctx->auth_tag, authsize); | 1131 | sg_init_one(cipher, rctx->auth_tag, authsize); |
1126 | 1132 | ||
1127 | /* construct the aad */ | 1133 | /* construct the aad */ |
1128 | dstp = sg_page(dst); | 1134 | srcp = sg_page(src); |
1129 | vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + dst->offset; | 1135 | vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset; |
1130 | 1136 | ||
1131 | sg_init_table(payload, 2); | 1137 | sg_init_table(payload, 2); |
1132 | sg_set_buf(payload, req->iv, 8); | 1138 | sg_set_buf(payload, req->iv, 8); |
1133 | scatterwalk_crypto_chain(payload, dst, vdst == req->iv + 8, 2); | 1139 | scatterwalk_crypto_chain(payload, src, vsrc == req->iv + 8, 2); |
1134 | assoclen += 8 + req->cryptlen - (enc ? 0 : authsize); | 1140 | assoclen += 8 + req->cryptlen - (enc ? 0 : authsize); |
1135 | 1141 | ||
1136 | sg_init_table(assoc, 2); | 1142 | sg_init_table(assoc, 2); |
@@ -1147,6 +1153,19 @@ static struct aead_request *crypto_rfc4543_crypt(struct aead_request *req, | |||
1147 | return subreq; | 1153 | return subreq; |
1148 | } | 1154 | } |
1149 | 1155 | ||
1156 | static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc) | ||
1157 | { | ||
1158 | struct crypto_aead *aead = crypto_aead_reqtfm(req); | ||
1159 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead); | ||
1160 | unsigned int authsize = crypto_aead_authsize(aead); | ||
1161 | unsigned int nbytes = req->cryptlen - (enc ? 0 : authsize); | ||
1162 | struct blkcipher_desc desc = { | ||
1163 | .tfm = ctx->null, | ||
1164 | }; | ||
1165 | |||
1166 | return crypto_blkcipher_encrypt(&desc, req->dst, req->src, nbytes); | ||
1167 | } | ||
1168 | |||
1150 | static int crypto_rfc4543_encrypt(struct aead_request *req) | 1169 | static int crypto_rfc4543_encrypt(struct aead_request *req) |
1151 | { | 1170 | { |
1152 | struct crypto_aead *aead = crypto_aead_reqtfm(req); | 1171 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
@@ -1154,7 +1173,13 @@ static int crypto_rfc4543_encrypt(struct aead_request *req) | |||
1154 | struct aead_request *subreq; | 1173 | struct aead_request *subreq; |
1155 | int err; | 1174 | int err; |
1156 | 1175 | ||
1157 | subreq = crypto_rfc4543_crypt(req, 1); | 1176 | if (req->src != req->dst) { |
1177 | err = crypto_rfc4543_copy_src_to_dst(req, true); | ||
1178 | if (err) | ||
1179 | return err; | ||
1180 | } | ||
1181 | |||
1182 | subreq = crypto_rfc4543_crypt(req, true); | ||
1158 | err = crypto_aead_encrypt(subreq); | 1183 | err = crypto_aead_encrypt(subreq); |
1159 | if (err) | 1184 | if (err) |
1160 | return err; | 1185 | return err; |
@@ -1167,7 +1192,15 @@ static int crypto_rfc4543_encrypt(struct aead_request *req) | |||
1167 | 1192 | ||
1168 | static int crypto_rfc4543_decrypt(struct aead_request *req) | 1193 | static int crypto_rfc4543_decrypt(struct aead_request *req) |
1169 | { | 1194 | { |
1170 | req = crypto_rfc4543_crypt(req, 0); | 1195 | int err; |
1196 | |||
1197 | if (req->src != req->dst) { | ||
1198 | err = crypto_rfc4543_copy_src_to_dst(req, false); | ||
1199 | if (err) | ||
1200 | return err; | ||
1201 | } | ||
1202 | |||
1203 | req = crypto_rfc4543_crypt(req, false); | ||
1171 | 1204 | ||
1172 | return crypto_aead_decrypt(req); | 1205 | return crypto_aead_decrypt(req); |
1173 | } | 1206 | } |
@@ -1175,16 +1208,25 @@ static int crypto_rfc4543_decrypt(struct aead_request *req) | |||
1175 | static int crypto_rfc4543_init_tfm(struct crypto_tfm *tfm) | 1208 | static int crypto_rfc4543_init_tfm(struct crypto_tfm *tfm) |
1176 | { | 1209 | { |
1177 | struct crypto_instance *inst = (void *)tfm->__crt_alg; | 1210 | struct crypto_instance *inst = (void *)tfm->__crt_alg; |
1178 | struct crypto_aead_spawn *spawn = crypto_instance_ctx(inst); | 1211 | struct crypto_rfc4543_instance_ctx *ictx = crypto_instance_ctx(inst); |
1212 | struct crypto_aead_spawn *spawn = &ictx->aead; | ||
1179 | struct crypto_rfc4543_ctx *ctx = crypto_tfm_ctx(tfm); | 1213 | struct crypto_rfc4543_ctx *ctx = crypto_tfm_ctx(tfm); |
1180 | struct crypto_aead *aead; | 1214 | struct crypto_aead *aead; |
1215 | struct crypto_blkcipher *null; | ||
1181 | unsigned long align; | 1216 | unsigned long align; |
1217 | int err = 0; | ||
1182 | 1218 | ||
1183 | aead = crypto_spawn_aead(spawn); | 1219 | aead = crypto_spawn_aead(spawn); |
1184 | if (IS_ERR(aead)) | 1220 | if (IS_ERR(aead)) |
1185 | return PTR_ERR(aead); | 1221 | return PTR_ERR(aead); |
1186 | 1222 | ||
1223 | null = crypto_spawn_blkcipher(&ictx->null.base); | ||
1224 | err = PTR_ERR(null); | ||
1225 | if (IS_ERR(null)) | ||
1226 | goto err_free_aead; | ||
1227 | |||
1187 | ctx->child = aead; | 1228 | ctx->child = aead; |
1229 | ctx->null = null; | ||
1188 | 1230 | ||
1189 | align = crypto_aead_alignmask(aead); | 1231 | align = crypto_aead_alignmask(aead); |
1190 | align &= ~(crypto_tfm_ctx_alignment() - 1); | 1232 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
@@ -1194,6 +1236,10 @@ static int crypto_rfc4543_init_tfm(struct crypto_tfm *tfm) | |||
1194 | align + 16; | 1236 | align + 16; |
1195 | 1237 | ||
1196 | return 0; | 1238 | return 0; |
1239 | |||
1240 | err_free_aead: | ||
1241 | crypto_free_aead(aead); | ||
1242 | return err; | ||
1197 | } | 1243 | } |
1198 | 1244 | ||
1199 | static void crypto_rfc4543_exit_tfm(struct crypto_tfm *tfm) | 1245 | static void crypto_rfc4543_exit_tfm(struct crypto_tfm *tfm) |
@@ -1201,6 +1247,7 @@ static void crypto_rfc4543_exit_tfm(struct crypto_tfm *tfm) | |||
1201 | struct crypto_rfc4543_ctx *ctx = crypto_tfm_ctx(tfm); | 1247 | struct crypto_rfc4543_ctx *ctx = crypto_tfm_ctx(tfm); |
1202 | 1248 | ||
1203 | crypto_free_aead(ctx->child); | 1249 | crypto_free_aead(ctx->child); |
1250 | crypto_free_blkcipher(ctx->null); | ||
1204 | } | 1251 | } |
1205 | 1252 | ||
1206 | static struct crypto_instance *crypto_rfc4543_alloc(struct rtattr **tb) | 1253 | static struct crypto_instance *crypto_rfc4543_alloc(struct rtattr **tb) |
@@ -1209,6 +1256,7 @@ static struct crypto_instance *crypto_rfc4543_alloc(struct rtattr **tb) | |||
1209 | struct crypto_instance *inst; | 1256 | struct crypto_instance *inst; |
1210 | struct crypto_aead_spawn *spawn; | 1257 | struct crypto_aead_spawn *spawn; |
1211 | struct crypto_alg *alg; | 1258 | struct crypto_alg *alg; |
1259 | struct crypto_rfc4543_instance_ctx *ctx; | ||
1212 | const char *ccm_name; | 1260 | const char *ccm_name; |
1213 | int err; | 1261 | int err; |
1214 | 1262 | ||
@@ -1223,11 +1271,12 @@ static struct crypto_instance *crypto_rfc4543_alloc(struct rtattr **tb) | |||
1223 | if (IS_ERR(ccm_name)) | 1271 | if (IS_ERR(ccm_name)) |
1224 | return ERR_CAST(ccm_name); | 1272 | return ERR_CAST(ccm_name); |
1225 | 1273 | ||
1226 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); | 1274 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
1227 | if (!inst) | 1275 | if (!inst) |
1228 | return ERR_PTR(-ENOMEM); | 1276 | return ERR_PTR(-ENOMEM); |
1229 | 1277 | ||
1230 | spawn = crypto_instance_ctx(inst); | 1278 | ctx = crypto_instance_ctx(inst); |
1279 | spawn = &ctx->aead; | ||
1231 | crypto_set_aead_spawn(spawn, inst); | 1280 | crypto_set_aead_spawn(spawn, inst); |
1232 | err = crypto_grab_aead(spawn, ccm_name, 0, | 1281 | err = crypto_grab_aead(spawn, ccm_name, 0, |
1233 | crypto_requires_sync(algt->type, algt->mask)); | 1282 | crypto_requires_sync(algt->type, algt->mask)); |
@@ -1236,15 +1285,23 @@ static struct crypto_instance *crypto_rfc4543_alloc(struct rtattr **tb) | |||
1236 | 1285 | ||
1237 | alg = crypto_aead_spawn_alg(spawn); | 1286 | alg = crypto_aead_spawn_alg(spawn); |
1238 | 1287 | ||
1288 | crypto_set_skcipher_spawn(&ctx->null, inst); | ||
1289 | err = crypto_grab_skcipher(&ctx->null, "ecb(cipher_null)", 0, | ||
1290 | CRYPTO_ALG_ASYNC); | ||
1291 | if (err) | ||
1292 | goto out_drop_alg; | ||
1293 | |||
1294 | crypto_skcipher_spawn_alg(&ctx->null); | ||
1295 | |||
1239 | err = -EINVAL; | 1296 | err = -EINVAL; |
1240 | 1297 | ||
1241 | /* We only support 16-byte blocks. */ | 1298 | /* We only support 16-byte blocks. */ |
1242 | if (alg->cra_aead.ivsize != 16) | 1299 | if (alg->cra_aead.ivsize != 16) |
1243 | goto out_drop_alg; | 1300 | goto out_drop_ecbnull; |
1244 | 1301 | ||
1245 | /* Not a stream cipher? */ | 1302 | /* Not a stream cipher? */ |
1246 | if (alg->cra_blocksize != 1) | 1303 | if (alg->cra_blocksize != 1) |
1247 | goto out_drop_alg; | 1304 | goto out_drop_ecbnull; |
1248 | 1305 | ||
1249 | err = -ENAMETOOLONG; | 1306 | err = -ENAMETOOLONG; |
1250 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, | 1307 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, |
@@ -1252,7 +1309,7 @@ static struct crypto_instance *crypto_rfc4543_alloc(struct rtattr **tb) | |||
1252 | snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, | 1309 | snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
1253 | "rfc4543(%s)", alg->cra_driver_name) >= | 1310 | "rfc4543(%s)", alg->cra_driver_name) >= |
1254 | CRYPTO_MAX_ALG_NAME) | 1311 | CRYPTO_MAX_ALG_NAME) |
1255 | goto out_drop_alg; | 1312 | goto out_drop_ecbnull; |
1256 | 1313 | ||
1257 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD; | 1314 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD; |
1258 | inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC; | 1315 | inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC; |
@@ -1279,6 +1336,8 @@ static struct crypto_instance *crypto_rfc4543_alloc(struct rtattr **tb) | |||
1279 | out: | 1336 | out: |
1280 | return inst; | 1337 | return inst; |
1281 | 1338 | ||
1339 | out_drop_ecbnull: | ||
1340 | crypto_drop_skcipher(&ctx->null); | ||
1282 | out_drop_alg: | 1341 | out_drop_alg: |
1283 | crypto_drop_aead(spawn); | 1342 | crypto_drop_aead(spawn); |
1284 | out_free_inst: | 1343 | out_free_inst: |
@@ -1289,7 +1348,11 @@ out_free_inst: | |||
1289 | 1348 | ||
1290 | static void crypto_rfc4543_free(struct crypto_instance *inst) | 1349 | static void crypto_rfc4543_free(struct crypto_instance *inst) |
1291 | { | 1350 | { |
1292 | crypto_drop_spawn(crypto_instance_ctx(inst)); | 1351 | struct crypto_rfc4543_instance_ctx *ctx = crypto_instance_ctx(inst); |
1352 | |||
1353 | crypto_drop_aead(&ctx->aead); | ||
1354 | crypto_drop_skcipher(&ctx->null); | ||
1355 | |||
1293 | kfree(inst); | 1356 | kfree(inst); |
1294 | } | 1357 | } |
1295 | 1358 | ||