如果在文件范围中定义了签名缓冲区,为什么public_key_verify_signature()会返回错误? (linux内核加密)

时间:2017-03-22 15:24:52

标签: c linux linux-kernel cryptography rsa

我正在使用linux内核4.8.0.26,并编写一个验证数字签名的简单模块。

如果在文件范围内定义了struct public_key_signature作为签名字节的缓冲区 - public_key_verify_signature()返回-EINVAL.

如果在函数内部定义了缓冲区 - public_key_verify_signature()成功。

以下代码会返回错误。使它工作 - 只需在signature_bytes内移动rsa_verify()声明:

/*  
*  rsa-verify.c - simple kernel module using crypto api to verify digital signature.
*/
#include <linux/module.h>
#include <linux/kernel.h>   
#include <crypto/public_key.h>

MODULE_LICENSE("GPL");

//public key in der format
u8 public_key_bytes[] = { 0x30,0x82,0x01,0x0a,0x02,0x82,0x01,0x01,0x00,0xe4,0xde,0x7c,0x12,0x6d,0xaa,0x6b,0xe9,0xb6,0x17,0x58,0x81,0x4e,0xad,0xfb,0x0e,0x21,0x39,0x4b,0x09,0x57,0xdd,0xa0,0xe1,0xfb,0x53,0x4b,0xc3,0xc3,0x0c,0x33,0xa7,0x37,0xfb,0xa8,0x47,0x26,0x2f,0xea,0x70,0x7c,0xbf,0x91,0x52,0xd8,0x2d,0x3a,0xd7,0xd9,0xba,0xa0,0x87,0x6d,0x8d,0x0c,0x7a,0xfe,0x7b,0x6c,0x7c,0x3d,0x57,0x73,0xb8,0xd1,0x55,0x2c,0x1e,0xc5,0x81,0xad,0x1a,0x59,0x71,0x12,0xa3,0x0b,0xee,0xe2,0x7b,0xf8,0x0c,0x8f,0xbf,0x68,0xbf,0x50,0x11,0x8b,0xa7,0xd7,0x00,0xb6,0xdc,0x65,0x4f,0xe3,0x75,0xdd,0xcc,0xa4,0x55,0x0c,0x70,0x27,0x23,0x9b,0x8d,0x1e,0x7d,0x66,0x33,0x06,0xad,0x62,0x37,0x1b,0xd4,0x68,0x50,0xcf,0x26,0x9d,0x60,0x6e,0xc0,0x3c,0xc7,0x15,0xcb,0x76,0x07,0x8a,0xc4,0xd3,0x84,0xf6,0xad,0x51,0xe0,0x63,0x31,0x55,0x4b,0x4a,0x96,0x4d,0x43,0xb7,0xd6,0x57,0xd6,0x6b,0x66,0x5c,0x60,0x35,0xf9,0x32,0x8d,0xe9,0x49,0x5c,0x3d,0x0f,0x77,0xfb,0x87,0x95,0xc2,0x57,0x8a,0xa2,0x9d,0xbe,0x42,0xc2,0x95,0x74,0x80,0xb1,0x10,0xd4,0x6c,0x37,0xb6,0xc5,0xed,0x73,0x2b,0xdd,0x44,0x87,0xf9,0xed,0x14,0xec,0x58,0x46,0x86,0xa4,0x0d,0x38,0x0f,0xcb,0x61,0xf9,0x39,0xf5,0x06,0xe6,0x20,0x0a,0x02,0xed,0x01,0xc7,0x77,0x9e,0x4e,0xaa,0x45,0xcf,0xc9,0xb2,0xa6,0xd1,0xf0,0x2c,0x6e,0x33,0xbb,0x49,0x18,0x7a,0x0a,0x55,0x06,0x7b,0xd8,0x6f,0x08,0xd6,0xc7,0xfd,0x41,0x2b,0x37,0x77,0x40,0x1e,0x63,0xf8,0x27,0x58,0x23,0x02,0x03,0x01,0x00,0x01 };

//SHA-256 digest for the data "asaf"
u8 digest[] = { 0x6f,0x82,0xd3,0x9a,0x2c,0x53,0xaf,0xa5,0x00,0xd4,0x6e,0x0e,0xfb,0xe1,0xa8,0xd5,0x6c,0xc4,0x69,0x4d,0xde,0x94,0x66,0xde,0xb2,0xb7,0x76,0x30,0xd1,0xf7,0x53,0x57 };

//given signature of data "asaf" using a private key
u8 signature_bytes[] = { 0xb7,0xa8,0x48,0x1c,0x31,0x80,0xab,0xa3,0x16,0x23,0xc8,0xb8,0x2b,0xf6,0x99,0x5b,0x90,0x64,0xe1,0x4f,0xda,0xe4,0xfc,0x4c,0xa4,0xbe,0x58,0x9c,0xec,0xef,0x9c,0x92,0x56,0x05,0x3e,0xa3,0xcc,0x00,0xd3,0x7e,0xa3,0xab,0x09,0xd7,0xc9,0x3f,0x2b,0x28,0x4f,0x14,0x57,0x2a,0x24,0x56,0x07,0x55,0xc1,0xbe,0xf8,0xbe,0x41,0x8d,0xa3,0xa4,0x79,0xdd,0x27,0x3e,0xc6,0x32,0xe1,0x80,0xe3,0x40,0x27,0x48,0x9f,0xe8,0xb6,0x87,0xee,0x4e,0x8a,0x82,0x6c,0x6c,0x32,0x50,0x7a,0x28,0x63,0xbc,0xb2,0xd4,0x59,0x50,0x66,0x07,0xb9,0x93,0x27,0x1b,0x45,0x35,0x00,0x15,0xe2,0x6e,0x7d,0xf8,0xb6,0x75,0x9b,0x3a,0xb8,0xc5,0xe5,0x1d,0xda,0x4c,0xdc,0x01,0xb2,0x69,0x7f,0x87,0x08,0x6e,0x11,0xb7,0xcd,0x1f,0x9f,0xaa,0xb4,0xe4,0x13,0x4e,0x67,0x1f,0x60,0x9c,0x30,0x7d,0xec,0xf1,0xf4,0x0d,0x72,0x62,0x39,0x51,0x82,0x9f,0x5f,0x25,0x1f,0x4a,0xff,0x73,0x88,0xf1,0x8e,0x2d,0x6e,0xec,0xa9,0x2a,0xea,0xca,0x49,0x39,0x6d,0x00,0xeb,0xb8,0x25,0x2b,0x02,0x06,0xcf,0xf3,0xa7,0xe2,0x14,0xcd,0xd6,0x5c,0xfa,0xd0,0x87,0x6b,0x29,0xc5,0xc4,0xe0,0x97,0x76,0x6b,0xd8,0x7c,0x49,0x63,0x2e,0xc9,0x96,0xf6,0xb2,0x3b,0x2d,0x35,0x77,0x3c,0x33,0x09,0x39,0xa6,0xff,0xb2,0x98,0x27,0x61,0x32,0x5b,0x30,0x30,0xd5,0xee,0xa0,0xcb,0x0a,0xb1,0xc7,0x6d,0x7b,0xf5,0x1a,0x12,0x25,0xe1,0xf0,0x78,0x4f,0xd9,0x54,0x32,0xb7,0x3b,0x6f,0xf3,0x33,0x9e,0xa8,0x58,0x7c,0xa3 };    

int rsa_verify(void)
{       
    // u8 signature_bytes[]... if it was here - it would work

    struct public_key rsa_pub_key = {
        .key = public_key_bytes,
        .keylen = (u32)(sizeof(public_key_bytes)),
        .pkey_algo = "rsa",
        .id_type = "X509"
    };

    struct public_key_signature sig = {
        .s = signature_bytes,
        .s_size = (u32)(sizeof(signature_bytes)),
        .digest = digest,
        .digest_size = (u8)(sizeof(digest)),
        .pkey_algo = "rsa",
        .hash_algo = "sha256"
    };

    int error = public_key_verify_signature(&rsa_pub_key, &sig);
    if (error) {
        pr_info("error verifying. error %d\n", error);
        return -1;
    }

    pr_info("verified successfuly!!!\n");   
    return 0;
}

int init_module(void)
{
    printk(KERN_INFO "Hello rsa-verify\n");
    return rsa_verify();
}

void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye rsa-verify\n");
}

生成文件:

obj-m += rsa-verify.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules EXTRA_CFLAGS="$(EXTRA_CFLAGS)"

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

(gcc版本6.1.1)

2 个答案:

答案 0 :(得分:0)

原因是当您按照代码操作时,您可以看到以下调用:

public_key_verify_signature();               // will call:
sg_init_one(&sig_sg, sig->s, sig->s_size);`  //will call:
sg_set_buf(sg, buf, buflen);

sg_set_buf代码是:

#ifdef CONFIG_DEBUG_SG
    BUG_ON(!virt_addr_valid(buf));
#endif
    sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));

virt_to_page无法使用,因为精灵部分已加载到使用vmalloc分配的网页。

答案 1 :(得分:0)

哈哈它的工作... 链接器,我是说修修补补...(dota 2)

int public_key_verify_signature1(const struct public_key *pkey,
                const struct public_key_signature *sig)
{
    struct crypto_wait cwait;
    struct crypto_akcipher *tfm;
    struct akcipher_request *req;
    struct scatterlist src_tab[3];
    const char *alg_name;
    char alg_name_buf[CRYPTO_MAX_ALG_NAME];
    void *output;
    unsigned int outlen;
    int ret;

    pr_devel("==>%s()\n", __func__);

    BUG_ON(!pkey);
    BUG_ON(!sig);
    BUG_ON(!sig->s);

    if (!sig->digest)
        return -ENOPKG;

    alg_name = sig->pkey_algo;
    if (strcmp(sig->pkey_algo, "rsa") == 0) {
        /* The data wangled by the RSA algorithm is typically padded
         * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
         * sec 8.2].
         */
        if (snprintf(alg_name_buf, CRYPTO_MAX_ALG_NAME,
                 "pkcs1pad(rsa,%s)", sig->hash_algo
                 ) >= CRYPTO_MAX_ALG_NAME)
            return -EINVAL;
        alg_name = alg_name_buf;
    }

    tfm = crypto_alloc_akcipher(alg_name, 0, 0);
    if (IS_ERR(tfm))
        return PTR_ERR(tfm);

    ret = -ENOMEM;
    req = akcipher_request_alloc(tfm, GFP_KERNEL);
    if (!req)
        goto error_free_tfm;

    ret = crypto_akcipher_set_pub_key(tfm, pkey->key, pkey->keylen);
    if (ret)
        goto error_free_req;

    ret = -ENOMEM;
    outlen = crypto_akcipher_maxsize(tfm);
    output = kmalloc(outlen, GFP_KERNEL);
    if (!output)
        goto error_free_req;


    sg_init_table(src_tab, 3);

    sg_set_buf(&src_tab[1], sig->digest, sig->digest_size);
    sg_set_buf(&src_tab[0], sig->s, sig->s_size);

    akcipher_request_set_crypt(req, src_tab, NULL, sig->s_size, sig->digest_size);

    crypto_init_wait(&cwait);
    akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
                      CRYPTO_TFM_REQ_MAY_SLEEP,
                      crypto_req_done, &cwait);


    ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
    if (ret)
        goto out_free_output;
    pr_info("verified successfuly!!!\n");

out_free_output:
    kfree(output);
error_free_req:
    akcipher_request_free(req);
error_free_tfm:
    crypto_free_akcipher(tfm);
    pr_devel("<==%s() = %d\n", __func__, ret);
    if (WARN_ON_ONCE(ret > 0))
        ret = -EINVAL;
    return ret;
}
int kk(void *data){
 // u8 signature_bytes[]... if it was here - it would work
//public key in der format
    unsigned char public_key_bytes[] = { 0x30,0x82,0x01,0x0a,0x02,0x82,0x01,0x01,0x00,0xe4,0xde,0x7c,0x12,0x6d,0xaa,0x6b,0xe9,0xb6,0x17,0x58,0x81,0x4e,0xad,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0x7b,0xd8,0x6f,0x08,0xd6,0xc7,0xfd,0x41,0x2b,0x37,0x77,0x40,0x1e,0x63,0xf8,0x27,0x58,0x23,0x02,0x03,0x01,0x00,0x01 };

    //SHA-256 digest for the data "asaf"
    unsigned char digest[] = { 0x6f,0x82,0xd3,0x9a,0x2c,0x53,0xaf,0xa5,0x00,0xd4,0x6e,0x0e,0xfb,0xe1,0xa8,0xd5,0x6c,0xc4,0x69,0x4d,0xde,0x94,0x66,0xde,0xb2,0xb7,0x76,0x30,0xd1,0xf7,0x53,0x57 };

    //given signature of data "asaf" using a private key
    unsigned char signature_bytes[] = { 0xb7,0xa8,0x48,0x1c,0x31,0x80,0xab,0xa3,0x16,0x23,0xc8,0xb8,0x2b,0xf6,0x99,0x5b,0x90,0x64,0xe1,0x4f,0xda,0xe4,0xfc,0x4c,0xa4,0xbe,0x58,0x9c,0xec,0xef,0x9c,0x92,0x56,0x05,0x3e,0xa3,0xcc,0x00,0xd3,0x7e,0xa3,0xab,0x09,0xd7,0xc9,0x3f,0x2b,0x28,0x4f,0x14,0x57,0x2a,0x24,0x56,0x07,0x55,0xc1,0xbe,0xf8,0xbe,0x41,0x8d,0xa3,0xa4,0x79,0xdd,0x27,0x3e,0xc6,0x32,0xe1,0x80,0xe3,0x40,0x27,0x48,0x9f,0xe8,0xb6,0x87,0xee,0x4e,0x8a,0x82,0x6c,0x6c,0x32,0x50,0x7a,0x28,0x63,0xbc,0xb2,0xd4,0x59,0x50,0x66,0x07,0xb9,0x93,0x27,0x1b,0x45,0x35,0x00,0x15,0xe2,0x6e,0x7d,0xf8,0xb6,0x75,0x9b,0x3a,0xb8,0xc5,0xe5,0x1d,0xda,0x4c,0xdc,0x01,0xb2,0x69,0x7f,0x87,0x08,0x6e,0x11,0xb7,0xcd,0x1f,0x9f,0xaa,0xb4,0xe4,0x13,0x4e,0x67,0x1f,0x60,0x9c,0x30,0x7d,0xec,0xf1,0xf4,0x0d,0x72,0x62,0x39,0x51,0x82,0x9f,0x5f,0x25,0x1f,0x4a,0xff,0x73,0x88,0xf1,0x8e,0x2d,0x6e,0xec,0xa9,0x2a,0xea,0xca,0x49,0x39,0x6d,0x00,0xeb,0xb8,0x25,0x2b,0x02,0x06,0xcf,0xf3,0xa7,0xe2,0x14,0xcd,0xd6,0x5c,0xfa,0xd0,0x87,0x6b,0x29,0xc5,0xc4,0xe0,0x97,0x76,0x6b,0xd8,0x7c,0x49,0x63,0x2e,0xc9,0x96,0xf6,0xb2,0x3b,0x2d,0x35,0x77,0x3c,0x33,0x09,0x39,0xa6,0xff,0xb2,0x98,0x27,0x61,0x32,0x5b,0x30,0x30,0xd5,0xee,0xa0,0xcb,0x0a,0xb1,0xc7,0x6d,0x7b,0xf5,0x1a,0x12,0x25,0xe1,0xf0,0x78,0x4f,0xd9,0x54,0x32,0xb7,0x3b,0x6f,0xf3,0x33,0x9e,0xa8,0x58,0x7c,0xa3 };

    struct public_key rsa_pub_key = {
        .key = public_key_bytes,
        .keylen = 270,
        .pkey_algo = "rsa",
        .id_type = "X509"
    };

    struct public_key_signature sig = {
        .s = signature_bytes,
        .s_size = 256,
        .digest = digest,
        .digest_size = 32,
        .pkey_algo = "rsa",
        .hash_algo = "sha256"
    };

    int error = public_key_verify_signature1(&rsa_pub_key, &sig);
    if (error) {
        pr_info("error verifying. error %d '%s'\n", error, get_error(error));
        return -1;
    }


    return 0;
}