2010-10-06 23 views
9

Ho cercato un po 'di tempo ma non ho trovato da nessuna parte vicino documentazione/esempi sufficienti su come usare CryptoAPI che viene fornito con linux nella creazione di syscalls/nel kernel land.come utilizzare CryptoAPI nel kernel Linux 2.6

Se qualcuno sa di una buona fonte, per favore fatemi sapere, mi piacerebbe sapere come fare SHA1/MD5 e Blowfish/AES solo nello spazio del kernel.

risposta

6

Ci sono un paio di punti nel kernel che usano il modulo crypto: il file system eCryptfs (linux/fs/ecryptfs /) e lo stack wireless 802.11 (linux/drivers/staging/rtl8187se/ieee80211 /). Entrambi usano AES, ma potresti estrapolare ciò che trovi lì su MD5.

+1

ecryptfs era la strada da percorrere, grazie – Conor

1

Il punto migliore per iniziare è Documentation/crytpo nelle origini del kernel. dm-crypt è uno dei tanti componenti che probabilmente usa l'API crypto del kernel e puoi farvi riferimento per avere un'idea sull'utilizzo.

2

come fare SHA1/MD5 e Blowfish/AES solo nello spazio del kernel.

Esempio di dati di hashing utilizzando una di due elementi scatterlist:

struct crypto_hash *tfm = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC); 
if (tfm == NULL) 
    fail; 
char *output_buf = kmalloc(crypto_hash_digestsize(tfm), GFP_KERNEL); 
if (output_buf == NULL) 
    fail; 
struct scatterlist sg[2]; 
struct hash_desc desc = {.tfm = tfm}; 
ret = crypto_hash_init(&desc); 
if (ret != 0) 
    fail; 
sg_init_table(sg, ARRAY_SIZE(sg)); 
sg_set_buf(&sg[0], "Hello", 5); 
sg_set_buf(&sg[1], " World", 6); 
ret = crypto_hash_digest(&desc, sg, 11, output_buf); 
if (ret != 0) 
    fail;
6

altro buon esempio è dalla sorgente del kernel 2.6.18 in materia di sicurezza/seclvl.c

Nota: è possibile modificare CRYPTO_TFM_REQ_MAY_SLEEP se necessario

static int 
plaintext_to_sha1(unsigned char *hash, const char *plaintext, unsigned int len) 
{ 
    struct crypto_tfm *tfm; 
    struct scatterlist sg; 
    if (len > PAGE_SIZE) { 
    seclvl_printk(0, KERN_ERR, "Plaintext password too large (%d " 
      "characters). Largest possible is %lu " 
      "bytes.\n", len, PAGE_SIZE); 
    return -EINVAL; 
    } 
    tfm = crypto_alloc_tfm("sha1", CRYPTO_TFM_REQ_MAY_SLEEP); 
    if (tfm == NULL) { 
    seclvl_printk(0, KERN_ERR, 
      "Failed to load transform for SHA1\n"); 
    return -EINVAL; 
    } 
    sg_init_one(&sg, (u8 *)plaintext, len); 
    crypto_digest_init(tfm); 
    crypto_digest_update(tfm, &sg, 1); 
    crypto_digest_final(tfm, hash); 
    crypto_free_tfm(tfm); 
    return 0; 
} 
11
#include <linux/kernel.h> 
#include <linux/init.h> 
#include <linux/module.h> 
#include <linux/crypto.h> 
#include <linux/err.h> 
#include <linux/scatterlist.h> 

#define SHA1_LENGTH  20 

static int __init sha1_init(void) 
{ 
    struct scatterlist sg; 
    struct crypto_hash *tfm; 
    struct hash_desc desc; 
    unsigned char output[SHA1_LENGTH]; 
    unsigned char buf[10]; 
    int i; 

    printk(KERN_INFO "sha1: %s\n", __FUNCTION__); 

    memset(buf, 'A', 10); 
    memset(output, 0x00, SHA1_LENGTH); 

    tfm = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC); 

    desc.tfm = tfm; 
    desc.flags = 0; 

    sg_init_one(&sg, buf, 10); 
    crypto_hash_init(&desc); 

    crypto_hash_update(&desc, &sg, 10); 
    crypto_hash_final(&desc, output); 

    for (i = 0; i < 20; i++) { 
     printk(KERN_ERR "%d-%d\n", output[i], i); 
    } 

    crypto_free_hash(tfm); 

    return 0; 
} 

static void __exit sha1_exit(void) 
{ 
    printk(KERN_INFO "sha1: %s\n", __FUNCTION__); 
} 

module_init(sha1_init); 
module_exit(sha1_exit); 

MODULE_LICENSE("Dual MIT/GPL"); 
MODULE_AUTHOR("Me"); 
+0

come si fa u compilarlo ?? Intendo cosa sono le libs dipendenti? – user907810

+0

Questo è un esempio di modulo del kernel e, come tale, non dipende dalle librerie, ma piuttosto da altri moduli del kernel. [depmod] (http://linux.about.com/library/cmd/blcmdl8_depmod.htm) calcola queste dipendenze e modprobe carica tutto nell'ordine corretto. – Przemek

1

Una nota fondamentale:

Non confrontare mai il valore restituito della funzione crypto_alloc_hash su NULL per rilevare l'errore.

Passi:

Usare sempre IS_ERR funzione per questo scopo. Il confronto con NULL non rileva l'errore, quindi in seguito si verificano errori di segmentazione.

Se IS_ERR restituisce un errore, è possibile che un algoritmo di crittografia mancante sia stato compilato nell'immagine del kernel (o come modulo). Assicurati di aver selezionato il cripto algo appropriato. modulo make menuconfig.

2

Cryptodev-linux

https://github.com/cryptodev-linux/cryptodev-linux

È un modulo kernel che espone la crittografia API kernel nello spazio utente attraverso /dev/crypto.

calcolo SHA esempio: https://github.com/cryptodev-linux/cryptodev-linux/blob/da730106c2558c8e0c8e1b1b1812d32ef9574ab7/examples/sha.c

Come altri hanno detto, il kernel non sembra esporre l'API di crittografia a userspace stessa, che è un vero peccato dato che il kernel può già utilizzare le funzioni crittografiche hardware nativo accelerati internamente.

Crypto operazioni cryptodev supporti: https://github.com/nmav/cryptodev-linux/blob/383922cabeea7dca354415e8c590f8e932f4d7a8/crypto/cryptodev.h

operazioni crittografiche supporta Linux x86: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/arch/x86/crypto?id=refs/tags/v4.0

Problemi correlati