mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	crypto: rsa-pkcs1pad: Allow hash to be optional [ver #2]
The original pkcs1pad implementation allowed to pad/unpad raw RSA
output.  However, this has been taken out in commit:
commit c0d20d22e0 ("crypto: rsa-pkcs1pad - Require hash to be present")
This patch restored this ability as it is needed by the asymmetric key
implementation.
Signed-off-by: Denis Kenzior <denkenz@gmail.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Tested-by: Marcel Holtmann <marcel@holtmann.org>
Reviewed-by: Marcel Holtmann <marcel@holtmann.org>
Signed-off-by: James Morris <james.morris@microsoft.com>
			
			
This commit is contained in:
		
							parent
							
								
									3c58b2362b
								
							
						
					
					
						commit
						b3a8c8a5eb
					
				
					 1 changed files with 41 additions and 18 deletions
				
			
		| 
						 | 
					@ -392,6 +392,7 @@ static int pkcs1pad_sign(struct akcipher_request *req)
 | 
				
			||||||
	if (!ctx->key_size)
 | 
						if (!ctx->key_size)
 | 
				
			||||||
		return -EINVAL;
 | 
							return -EINVAL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (digest_info)
 | 
				
			||||||
		digest_size = digest_info->size;
 | 
							digest_size = digest_info->size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (req->src_len + digest_size > ctx->key_size - 11)
 | 
						if (req->src_len + digest_size > ctx->key_size - 11)
 | 
				
			||||||
| 
						 | 
					@ -412,6 +413,7 @@ static int pkcs1pad_sign(struct akcipher_request *req)
 | 
				
			||||||
	memset(req_ctx->in_buf + 1, 0xff, ps_end - 1);
 | 
						memset(req_ctx->in_buf + 1, 0xff, ps_end - 1);
 | 
				
			||||||
	req_ctx->in_buf[ps_end] = 0x00;
 | 
						req_ctx->in_buf[ps_end] = 0x00;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (digest_info)
 | 
				
			||||||
		memcpy(req_ctx->in_buf + ps_end + 1, digest_info->data,
 | 
							memcpy(req_ctx->in_buf + ps_end + 1, digest_info->data,
 | 
				
			||||||
		       digest_info->size);
 | 
							       digest_info->size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -475,10 +477,13 @@ static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
 | 
				
			||||||
		goto done;
 | 
							goto done;
 | 
				
			||||||
	pos++;
 | 
						pos++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (crypto_memneq(out_buf + pos, digest_info->data, digest_info->size))
 | 
						if (digest_info) {
 | 
				
			||||||
 | 
							if (crypto_memneq(out_buf + pos, digest_info->data,
 | 
				
			||||||
 | 
									  digest_info->size))
 | 
				
			||||||
			goto done;
 | 
								goto done;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		pos += digest_info->size;
 | 
							pos += digest_info->size;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = 0;
 | 
						err = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -608,11 +613,14 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	hash_name = crypto_attr_alg_name(tb[2]);
 | 
						hash_name = crypto_attr_alg_name(tb[2]);
 | 
				
			||||||
	if (IS_ERR(hash_name))
 | 
						if (IS_ERR(hash_name))
 | 
				
			||||||
		return PTR_ERR(hash_name);
 | 
							hash_name = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (hash_name) {
 | 
				
			||||||
		digest_info = rsa_lookup_asn1(hash_name);
 | 
							digest_info = rsa_lookup_asn1(hash_name);
 | 
				
			||||||
		if (!digest_info)
 | 
							if (!digest_info)
 | 
				
			||||||
			return -EINVAL;
 | 
								return -EINVAL;
 | 
				
			||||||
 | 
						} else
 | 
				
			||||||
 | 
							digest_info = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
 | 
						inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
 | 
				
			||||||
	if (!inst)
 | 
						if (!inst)
 | 
				
			||||||
| 
						 | 
					@ -632,14 +640,29 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = -ENAMETOOLONG;
 | 
						err = -ENAMETOOLONG;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
 | 
						if (!hash_name) {
 | 
				
			||||||
		     "pkcs1pad(%s,%s)", rsa_alg->base.cra_name, hash_name) >=
 | 
							if (snprintf(inst->alg.base.cra_name,
 | 
				
			||||||
	    CRYPTO_MAX_ALG_NAME ||
 | 
								     CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
 | 
				
			||||||
	    snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
 | 
								     rsa_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
 | 
				
			||||||
		     "pkcs1pad(%s,%s)",
 | 
								goto out_drop_alg;
 | 
				
			||||||
		     rsa_alg->base.cra_driver_name, hash_name) >=
 | 
					
 | 
				
			||||||
 | 
							if (snprintf(inst->alg.base.cra_driver_name,
 | 
				
			||||||
 | 
								     CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
 | 
				
			||||||
 | 
								     rsa_alg->base.cra_driver_name) >=
 | 
				
			||||||
			     CRYPTO_MAX_ALG_NAME)
 | 
								     CRYPTO_MAX_ALG_NAME)
 | 
				
			||||||
			goto out_drop_alg;
 | 
								goto out_drop_alg;
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
 | 
				
			||||||
 | 
								     "pkcs1pad(%s,%s)", rsa_alg->base.cra_name,
 | 
				
			||||||
 | 
								     hash_name) >= CRYPTO_MAX_ALG_NAME)
 | 
				
			||||||
 | 
								goto out_drop_alg;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (snprintf(inst->alg.base.cra_driver_name,
 | 
				
			||||||
 | 
								     CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s,%s)",
 | 
				
			||||||
 | 
								     rsa_alg->base.cra_driver_name,
 | 
				
			||||||
 | 
								     hash_name) >= CRYPTO_MAX_ALG_NAME)
 | 
				
			||||||
 | 
								goto out_drop_alg;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	inst->alg.base.cra_flags = rsa_alg->base.cra_flags & CRYPTO_ALG_ASYNC;
 | 
						inst->alg.base.cra_flags = rsa_alg->base.cra_flags & CRYPTO_ALG_ASYNC;
 | 
				
			||||||
	inst->alg.base.cra_priority = rsa_alg->base.cra_priority;
 | 
						inst->alg.base.cra_priority = rsa_alg->base.cra_priority;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue