Skip to content

Commit

Permalink
[CRYPTO] users: Fix up scatterlist conversion errors
Browse files Browse the repository at this point in the history
This patch fixes the errors made in the users of the crypto layer during
the sg_init_table conversion.  It also adds a few conversions that were
missing altogether.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
herbertx authored and davem330 committed Oct 27, 2007
1 parent a5a613a commit 68e3f5d
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 58 deletions.
4 changes: 2 additions & 2 deletions drivers/crypto/padlock-sha.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static void padlock_sha_bypass(struct crypto_tfm *tfm)
if (ctx(tfm)->data && ctx(tfm)->used) {
struct scatterlist sg;

sg_set_buf(&sg, ctx(tfm)->data, ctx(tfm)->used);
sg_init_one(&sg, ctx(tfm)->data, ctx(tfm)->used);
crypto_hash_update(&ctx(tfm)->fallback, &sg, sg.length);
}

Expand All @@ -79,7 +79,7 @@ static void padlock_sha_update(struct crypto_tfm *tfm,

if (unlikely(ctx(tfm)->bypass)) {
struct scatterlist sg;
sg_set_buf(&sg, (uint8_t *)data, length);
sg_init_one(&sg, (uint8_t *)data, length);
crypto_hash_update(&ctx(tfm)->fallback, &sg, length);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/md/dm-crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
return -ENOMEM;
}

sg_set_buf(&sg, cc->key, cc->key_size);
sg_init_one(&sg, cc->key, cc->key_size);
desc.tfm = hash_tfm;
desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
err = crypto_hash_digest(&desc, &sg, cc->key_size, salt);
Expand Down
10 changes: 9 additions & 1 deletion drivers/net/ppp_mppe.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ MODULE_VERSION("1.0.2");
static unsigned int
setup_sg(struct scatterlist *sg, const void *address, unsigned int length)
{
sg_init_one(sg, address, length);
sg_set_buf(sg, address, length);
return length;
}

Expand Down Expand Up @@ -140,6 +140,8 @@ static void get_new_key_from_sha(struct ppp_mppe_state * state)
struct scatterlist sg[4];
unsigned int nbytes;

sg_init_table(sg, 4);

nbytes = setup_sg(&sg[0], state->master_key, state->keylen);
nbytes += setup_sg(&sg[1], sha_pad->sha_pad1,
sizeof(sha_pad->sha_pad1));
Expand All @@ -166,6 +168,8 @@ static void mppe_rekey(struct ppp_mppe_state * state, int initial_key)
if (!initial_key) {
crypto_blkcipher_setkey(state->arc4, state->sha1_digest,
state->keylen);
sg_init_table(sg_in, 1);
sg_init_table(sg_out, 1);
setup_sg(sg_in, state->sha1_digest, state->keylen);
setup_sg(sg_out, state->session_key, state->keylen);
if (crypto_blkcipher_encrypt(&desc, sg_out, sg_in,
Expand Down Expand Up @@ -421,6 +425,8 @@ mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
isize -= 2;

/* Encrypt packet */
sg_init_table(sg_in, 1);
sg_init_table(sg_out, 1);
setup_sg(sg_in, ibuf, isize);
setup_sg(sg_out, obuf, osize);
if (crypto_blkcipher_encrypt(&desc, sg_out, sg_in, isize) != 0) {
Expand Down Expand Up @@ -608,6 +614,8 @@ mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
* Decrypt the first byte in order to check if it is
* a compressed or uncompressed protocol field.
*/
sg_init_table(sg_in, 1);
sg_init_table(sg_out, 1);
setup_sg(sg_in, ibuf, 1);
setup_sg(sg_out, obuf, 1);
if (crypto_blkcipher_decrypt(&desc, sg_out, sg_in, 1) != 0) {
Expand Down
5 changes: 2 additions & 3 deletions drivers/scsi/iscsi_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,9 +674,8 @@ partial_sg_digest_update(struct hash_desc *desc, struct scatterlist *sg,
{
struct scatterlist temp;

memcpy(&temp, sg, sizeof(struct scatterlist));
temp.offset = offset;
temp.length = length;
sg_init_table(&temp, 1);
sg_set_page(&temp, sg_page(sg), length, offset);
crypto_hash_update(desc, &temp, length);
}

Expand Down
2 changes: 2 additions & 0 deletions fs/ecryptfs/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
int offset;
int remainder_of_page;

sg_init_table(sg, sg_size);

while (size > 0 && i < sg_size) {
pg = virt_to_page(addr);
offset = offset_in_page(addr);
Expand Down
7 changes: 5 additions & 2 deletions net/ipv4/esp4.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
goto unlock;
}
sg_init_table(sg, nfrags);
skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen);
sg_mark_end(sg, skb_to_sgvec(skb, sg, esph->enc_data +
esp->conf.ivlen -
skb->data, clen));
err = crypto_blkcipher_encrypt(&desc, sg, sg, clen);
if (unlikely(sg != &esp->sgbuf[0]))
kfree(sg);
Expand Down Expand Up @@ -203,7 +205,8 @@ static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
goto out;
}
sg_init_table(sg, nfrags);
skb_to_sgvec(skb, sg, sizeof(*esph) + esp->conf.ivlen, elen);
sg_mark_end(sg, skb_to_sgvec(skb, sg, sizeof(*esph) + esp->conf.ivlen,
elen));
err = crypto_blkcipher_decrypt(&desc, sg, sg, elen);
if (unlikely(sg != &esp->sgbuf[0]))
kfree(sg);
Expand Down
8 changes: 6 additions & 2 deletions net/ipv6/esp6.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
goto unlock;
}
sg_init_table(sg, nfrags);
skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen);
sg_mark_end(sg, skb_to_sgvec(skb, sg, esph->enc_data +
esp->conf.ivlen -
skb->data, clen));
err = crypto_blkcipher_encrypt(&desc, sg, sg, clen);
if (unlikely(sg != &esp->sgbuf[0]))
kfree(sg);
Expand Down Expand Up @@ -207,7 +209,9 @@ static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
}
}
sg_init_table(sg, nfrags);
skb_to_sgvec(skb, sg, sizeof(*esph) + esp->conf.ivlen, elen);
sg_mark_end(sg, skb_to_sgvec(skb, sg,
sizeof(*esph) + esp->conf.ivlen,
elen));
ret = crypto_blkcipher_decrypt(&desc, sg, sg, elen);
if (unlikely(sg != &esp->sgbuf[0]))
kfree(sg);
Expand Down
66 changes: 33 additions & 33 deletions net/rxrpc/rxkad.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,8 @@ static void rxkad_prime_packet_security(struct rxrpc_connection *conn)
tmpbuf.x[2] = 0;
tmpbuf.x[3] = htonl(conn->security_ix);

memset(sg, 0, sizeof(sg));
sg_set_buf(&sg[0], &tmpbuf, sizeof(tmpbuf));
sg_set_buf(&sg[1], &tmpbuf, sizeof(tmpbuf));
sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));

memcpy(&conn->csum_iv, &tmpbuf.x[2], sizeof(conn->csum_iv));
Expand Down Expand Up @@ -180,9 +179,8 @@ static int rxkad_secure_packet_auth(const struct rxrpc_call *call,
desc.info = iv.x;
desc.flags = 0;

memset(sg, 0, sizeof(sg));
sg_set_buf(&sg[0], &tmpbuf, sizeof(tmpbuf));
sg_set_buf(&sg[1], &tmpbuf, sizeof(tmpbuf));
sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));

memcpy(sechdr, &tmpbuf, sizeof(tmpbuf));
Expand Down Expand Up @@ -227,9 +225,8 @@ static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call,
desc.info = iv.x;
desc.flags = 0;

memset(sg, 0, sizeof(sg[0]) * 2);
sg_set_buf(&sg[0], sechdr, sizeof(rxkhdr));
sg_set_buf(&sg[1], &rxkhdr, sizeof(rxkhdr));
sg_init_one(&sg[0], sechdr, sizeof(rxkhdr));
sg_init_one(&sg[1], &rxkhdr, sizeof(rxkhdr));
crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(rxkhdr));

/* we want to encrypt the skbuff in-place */
Expand All @@ -240,7 +237,7 @@ static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call,
len = data_size + call->conn->size_align - 1;
len &= ~(call->conn->size_align - 1);

skb_to_sgvec(skb, sg, 0, len);
sg_init_table(sg, skb_to_sgvec(skb, sg, 0, len));
crypto_blkcipher_encrypt_iv(&desc, sg, sg, len);

_leave(" = 0");
Expand Down Expand Up @@ -290,9 +287,8 @@ static int rxkad_secure_packet(const struct rxrpc_call *call,
tmpbuf.x[0] = sp->hdr.callNumber;
tmpbuf.x[1] = x;

memset(&sg, 0, sizeof(sg));
sg_set_buf(&sg[0], &tmpbuf, sizeof(tmpbuf));
sg_set_buf(&sg[1], &tmpbuf, sizeof(tmpbuf));
sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));

x = ntohl(tmpbuf.x[1]);
Expand Down Expand Up @@ -332,20 +328,23 @@ static int rxkad_verify_packet_auth(const struct rxrpc_call *call,
struct rxrpc_skb_priv *sp;
struct blkcipher_desc desc;
struct rxrpc_crypt iv;
struct scatterlist sg[2];
struct scatterlist sg[16];
struct sk_buff *trailer;
u32 data_size, buf;
u16 check;
int nsg;

_enter("");

sp = rxrpc_skb(skb);

/* we want to decrypt the skbuff in-place */
if (skb_cow_data(skb, 0, &trailer) < 0)
nsg = skb_cow_data(skb, 0, &trailer);
if (nsg < 0 || nsg > 16)
goto nomem;

skb_to_sgvec(skb, sg, 0, 8);
sg_init_table(sg, nsg);
sg_mark_end(sg, skb_to_sgvec(skb, sg, 0, 8));

/* start the decryption afresh */
memset(&iv, 0, sizeof(iv));
Expand Down Expand Up @@ -426,7 +425,8 @@ static int rxkad_verify_packet_encrypt(const struct rxrpc_call *call,
goto nomem;
}

skb_to_sgvec(skb, sg, 0, skb->len);
sg_init_table(sg, nsg);
sg_mark_end(sg, skb_to_sgvec(skb, sg, 0, skb->len));

/* decrypt from the session key */
payload = call->conn->key->payload.data;
Expand Down Expand Up @@ -521,9 +521,8 @@ static int rxkad_verify_packet(const struct rxrpc_call *call,
tmpbuf.x[0] = call->call_id;
tmpbuf.x[1] = x;

memset(&sg, 0, sizeof(sg));
sg_set_buf(&sg[0], &tmpbuf, sizeof(tmpbuf));
sg_set_buf(&sg[1], &tmpbuf, sizeof(tmpbuf));
sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));

x = ntohl(tmpbuf.x[1]);
Expand Down Expand Up @@ -690,16 +689,20 @@ static void rxkad_calc_response_checksum(struct rxkad_response *response)
static void rxkad_sg_set_buf2(struct scatterlist sg[2],
void *buf, size_t buflen)
{
int nsg = 1;

memset(sg, 0, sizeof(sg));
sg_init_table(sg, 2);

sg_set_buf(&sg[0], buf, buflen);
if (sg[0].offset + buflen > PAGE_SIZE) {
/* the buffer was split over two pages */
sg[0].length = PAGE_SIZE - sg[0].offset;
sg_set_buf(&sg[1], buf + sg[0].length, buflen - sg[0].length);
nsg++;
}

sg_mark_end(sg, nsg);

ASSERTCMP(sg[0].length + sg[1].length, ==, buflen);
}

Expand All @@ -712,17 +715,16 @@ static void rxkad_encrypt_response(struct rxrpc_connection *conn,
{
struct blkcipher_desc desc;
struct rxrpc_crypt iv;
struct scatterlist ssg[2], dsg[2];
struct scatterlist sg[2];

/* continue encrypting from where we left off */
memcpy(&iv, s2->session_key, sizeof(iv));
desc.tfm = conn->cipher;
desc.info = iv.x;
desc.flags = 0;

rxkad_sg_set_buf2(ssg, &resp->encrypted, sizeof(resp->encrypted));
memcpy(dsg, ssg, sizeof(dsg));
crypto_blkcipher_encrypt_iv(&desc, dsg, ssg, sizeof(resp->encrypted));
rxkad_sg_set_buf2(sg, &resp->encrypted, sizeof(resp->encrypted));
crypto_blkcipher_encrypt_iv(&desc, sg, sg, sizeof(resp->encrypted));
}

/*
Expand Down Expand Up @@ -817,7 +819,7 @@ static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
{
struct blkcipher_desc desc;
struct rxrpc_crypt iv, key;
struct scatterlist ssg[1], dsg[1];
struct scatterlist sg[1];
struct in_addr addr;
unsigned life;
time_t issue, now;
Expand Down Expand Up @@ -850,9 +852,8 @@ static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
desc.info = iv.x;
desc.flags = 0;

sg_init_one(&ssg[0], ticket, ticket_len);
memcpy(dsg, ssg, sizeof(dsg));
crypto_blkcipher_decrypt_iv(&desc, dsg, ssg, ticket_len);
sg_init_one(&sg[0], ticket, ticket_len);
crypto_blkcipher_decrypt_iv(&desc, sg, sg, ticket_len);

p = ticket;
end = p + ticket_len;
Expand Down Expand Up @@ -961,7 +962,7 @@ static void rxkad_decrypt_response(struct rxrpc_connection *conn,
const struct rxrpc_crypt *session_key)
{
struct blkcipher_desc desc;
struct scatterlist ssg[2], dsg[2];
struct scatterlist sg[2];
struct rxrpc_crypt iv;

_enter(",,%08x%08x",
Expand All @@ -979,9 +980,8 @@ static void rxkad_decrypt_response(struct rxrpc_connection *conn,
desc.info = iv.x;
desc.flags = 0;

rxkad_sg_set_buf2(ssg, &resp->encrypted, sizeof(resp->encrypted));
memcpy(dsg, ssg, sizeof(dsg));
crypto_blkcipher_decrypt_iv(&desc, dsg, ssg, sizeof(resp->encrypted));
rxkad_sg_set_buf2(sg, &resp->encrypted, sizeof(resp->encrypted));
crypto_blkcipher_decrypt_iv(&desc, sg, sg, sizeof(resp->encrypted));
mutex_unlock(&rxkad_ci_mutex);

_leave("");
Expand Down
3 changes: 1 addition & 2 deletions net/sctp/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,7 @@ void sctp_auth_calculate_hmac(const struct sctp_association *asoc,

/* set up scatter list */
end = skb_tail_pointer(skb);
sg_init_table(&sg, 1);
sg_set_buf(&sg, auth, end - (unsigned char *)auth);
sg_init_one(&sg, auth, end - (unsigned char *)auth);

desc.tfm = asoc->ep->auth_hmacs[hmac_id];
desc.flags = 0;
Expand Down
6 changes: 2 additions & 4 deletions net/sctp/sm_make_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1513,8 +1513,7 @@ static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
struct hash_desc desc;

/* Sign the message. */
sg_init_table(&sg, 1);
sg_set_buf(&sg, &cookie->c, bodysize);
sg_init_one(&sg, &cookie->c, bodysize);
keylen = SCTP_SECRET_SIZE;
key = (char *)ep->secret_key[ep->current_key];
desc.tfm = sctp_sk(ep->base.sk)->hmac;
Expand Down Expand Up @@ -1584,8 +1583,7 @@ struct sctp_association *sctp_unpack_cookie(

/* Check the signature. */
keylen = SCTP_SECRET_SIZE;
sg_init_table(&sg, 1);
sg_set_buf(&sg, bear_cookie, bodysize);
sg_init_one(&sg, bear_cookie, bodysize);
key = (char *)ep->secret_key[ep->current_key];
desc.tfm = sctp_sk(ep->base.sk)->hmac;
desc.flags = 0;
Expand Down
Loading

0 comments on commit 68e3f5d

Please sign in to comment.