diff options
author | Simon Josefsson <simon@josefsson.org> | 2010-02-17 19:06:14 +0100 |
---|---|---|
committer | Simon Josefsson <simon@josefsson.org> | 2010-02-17 19:06:14 +0100 |
commit | 9985aac98be2b0121a681f1690832c6928fdbf00 (patch) | |
tree | 62d2ddcadbbe8835c66a248bc2903d571314cd4f | |
parent | 1d6766cda39a57f118afae222e2daa5d91b6e386 (diff) | |
download | gnutls-9985aac98be2b0121a681f1690832c6928fdbf00.tar.gz |
Indent. Don't include fcntl.h and sys/ioctl.h on (for example) Windows.
-rw-r--r-- | lib/cryptodev.c | 354 |
1 files changed, 191 insertions, 163 deletions
diff --git a/lib/cryptodev.c b/lib/cryptodev.c index 9a36b9d554..5402200342 100644 --- a/lib/cryptodev.c +++ b/lib/cryptodev.c @@ -24,13 +24,13 @@ #include <gnutls_errors.h> #include <gnutls_int.h> -#include <sys/ioctl.h> -#include <fcntl.h> #include <gnutls/crypto.h> #include <gnutls_cryptodev.h> #ifdef ENABLE_CRYPTODEV +#include <fcntl.h> +#include <sys/ioctl.h> #include <crypto/cryptodev.h> #ifndef CRYPTO_CIPHER_MAX_KEY_LEN @@ -44,217 +44,245 @@ static int cryptodev_fd = -1; -struct cryptodev_ctx { - struct session_op sess; - struct crypt_op cryp; - opaque iv[EALG_MAX_BLOCK_LEN]; - opaque key[CRYPTO_CIPHER_MAX_KEY_LEN]; - int cfd; +struct cryptodev_ctx +{ + struct session_op sess; + struct crypt_op cryp; + opaque iv[EALG_MAX_BLOCK_LEN]; + opaque key[CRYPTO_CIPHER_MAX_KEY_LEN]; + int cfd; }; static const int gnutls_cipher_map[] = { - [GNUTLS_CIPHER_AES_128_CBC] = CRYPTO_AES_CBC, - [GNUTLS_CIPHER_AES_192_CBC] = CRYPTO_AES_CBC, - [GNUTLS_CIPHER_AES_256_CBC] = CRYPTO_AES_CBC, - [GNUTLS_CIPHER_3DES_CBC] = CRYPTO_3DES_CBC, - [GNUTLS_CIPHER_CAMELLIA_128_CBC] = CRYPTO_CAMELLIA_CBC, - [GNUTLS_CIPHER_CAMELLIA_256_CBC] = CRYPTO_CAMELLIA_CBC, - [GNUTLS_CIPHER_DES_CBC] = CRYPTO_DES_CBC, + [GNUTLS_CIPHER_AES_128_CBC] = CRYPTO_AES_CBC, + [GNUTLS_CIPHER_AES_192_CBC] = CRYPTO_AES_CBC, + [GNUTLS_CIPHER_AES_256_CBC] = CRYPTO_AES_CBC, + [GNUTLS_CIPHER_3DES_CBC] = CRYPTO_3DES_CBC, + [GNUTLS_CIPHER_CAMELLIA_128_CBC] = CRYPTO_CAMELLIA_CBC, + [GNUTLS_CIPHER_CAMELLIA_256_CBC] = CRYPTO_CAMELLIA_CBC, + [GNUTLS_CIPHER_DES_CBC] = CRYPTO_DES_CBC, }; - -static int cryptodev_cipher_init(gnutls_cipher_algorithm_t algorithm, void** _ctx) -{ -struct cryptodev_ctx* ctx; -int cipher = gnutls_cipher_map[algorithm]; - *_ctx = gnutls_calloc(1, sizeof(struct cryptodev_ctx)); - if (*_ctx == NULL) { - gnutls_assert(); - return GNUTLS_E_MEMORY_ERROR; - } +static int +cryptodev_cipher_init (gnutls_cipher_algorithm_t algorithm, void **_ctx) +{ + struct cryptodev_ctx *ctx; + int cipher = gnutls_cipher_map[algorithm]; + + *_ctx = gnutls_calloc (1, sizeof (struct cryptodev_ctx)); + if (*_ctx == NULL) + { + gnutls_assert (); + return GNUTLS_E_MEMORY_ERROR; + } + + ctx = *_ctx; + + if (ioctl (cryptodev_fd, CRIOGET, &ctx->cfd)) + { + gnutls_assert (); + return GNUTLS_E_CRYPTODEV_IOCTL_ERROR; + } + + if (fcntl (ctx->cfd, F_SETFD, 1) == -1) + { + gnutls_assert (); + return GNUTLS_E_CRYPTODEV_IOCTL_ERROR; + } + + ctx->sess.cipher = cipher; + ctx->sess.key = ctx->key; + ctx->cryp.iv = ctx->iv; + + return 0; +} - ctx = *_ctx; +static int +cryptodev_setkey (void *_ctx, const void *key, size_t keysize) +{ + struct cryptodev_ctx *ctx = _ctx; - if (ioctl(cryptodev_fd, CRIOGET, &ctx->cfd)) { - gnutls_assert(); - return GNUTLS_E_CRYPTODEV_IOCTL_ERROR; - } + ctx->sess.keylen = keysize; + memcpy (ctx->key, key, keysize); - if (fcntl(ctx->cfd, F_SETFD, 1) == -1) { - gnutls_assert(); - return GNUTLS_E_CRYPTODEV_IOCTL_ERROR; - } + if (ioctl (ctx->cfd, CIOCGSESSION, &ctx->sess)) + { + gnutls_assert (); + return GNUTLS_E_CRYPTODEV_IOCTL_ERROR; + } + ctx->cryp.ses = ctx->sess.ses; - ctx->sess.cipher = cipher; - ctx->sess.key = ctx->key; - ctx->cryp.iv = ctx->iv; + return 0; - return 0; } -static int cryptodev_setkey( void* _ctx, const void *key, size_t keysize) +static int +cryptodev_setiv (void *_ctx, const void *iv, size_t iv_size) { -struct cryptodev_ctx* ctx = _ctx; - - ctx->sess.keylen = keysize; - memcpy(ctx->key, key, keysize); - - if (ioctl(ctx->cfd, CIOCGSESSION, &ctx->sess)) { - gnutls_assert(); - return GNUTLS_E_CRYPTODEV_IOCTL_ERROR; - } - ctx->cryp.ses = ctx->sess.ses; - - return 0; - -} + struct cryptodev_ctx *ctx = _ctx; -static int cryptodev_setiv( void* _ctx, const void* iv, size_t iv_size) -{ -struct cryptodev_ctx* ctx = _ctx; + memcpy (ctx->iv, iv, iv_size); - memcpy(ctx->iv, iv, iv_size); - - return 0; + return 0; } -static int cryptodev_encrypt(void* _ctx, const void* plain, size_t plainsize, - void* encr, size_t encrsize) +static int +cryptodev_encrypt (void *_ctx, const void *plain, size_t plainsize, + void *encr, size_t encrsize) { -struct cryptodev_ctx* ctx = _ctx; - ctx->cryp.len = plainsize; - ctx->cryp.src = (void*)plain; - ctx->cryp.dst = encr; - ctx->cryp.op = COP_ENCRYPT; - if (ioctl(ctx->cfd, CIOCCRYPT, &ctx->cryp)) { - gnutls_assert(); - return GNUTLS_E_CRYPTODEV_IOCTL_ERROR; - } - return 0; + struct cryptodev_ctx *ctx = _ctx; + ctx->cryp.len = plainsize; + ctx->cryp.src = (void *) plain; + ctx->cryp.dst = encr; + ctx->cryp.op = COP_ENCRYPT; + if (ioctl (ctx->cfd, CIOCCRYPT, &ctx->cryp)) + { + gnutls_assert (); + return GNUTLS_E_CRYPTODEV_IOCTL_ERROR; + } + return 0; } -static int cryptodev_decrypt(void* _ctx, const void* encr, size_t encrsize, - void* plain, size_t plainsize) +static int +cryptodev_decrypt (void *_ctx, const void *encr, size_t encrsize, + void *plain, size_t plainsize) { -struct cryptodev_ctx* ctx = _ctx; - - ctx->cryp.len = encrsize; - ctx->cryp.src = (void*)encr; - ctx->cryp.dst = plain; - ctx->cryp.op = COP_DECRYPT; - if (ioctl(ctx->cfd, CIOCCRYPT, &ctx->cryp)) { - gnutls_assert(); - return GNUTLS_E_CRYPTODEV_IOCTL_ERROR; - } - return 0; - + struct cryptodev_ctx *ctx = _ctx; + + ctx->cryp.len = encrsize; + ctx->cryp.src = (void *) encr; + ctx->cryp.dst = plain; + ctx->cryp.op = COP_DECRYPT; + if (ioctl (ctx->cfd, CIOCCRYPT, &ctx->cryp)) + { + gnutls_assert (); + return GNUTLS_E_CRYPTODEV_IOCTL_ERROR; + } + return 0; + } -static void cryptodev_deinit(void* _ctx) +static void +cryptodev_deinit (void *_ctx) { -struct cryptodev_ctx* ctx = _ctx; + struct cryptodev_ctx *ctx = _ctx; - close(ctx->cfd); - gnutls_free(ctx); + close (ctx->cfd); + gnutls_free (ctx); } static const gnutls_crypto_cipher_st cipher_struct = { - .init = cryptodev_cipher_init, - .setkey = cryptodev_setkey, - .setiv = cryptodev_setiv, - .encrypt = cryptodev_encrypt, - .decrypt = cryptodev_decrypt, - .deinit = cryptodev_deinit, + .init = cryptodev_cipher_init, + .setkey = cryptodev_setkey, + .setiv = cryptodev_setiv, + .encrypt = cryptodev_encrypt, + .decrypt = cryptodev_decrypt, + .deinit = cryptodev_deinit, }; -struct cipher_map { - gnutls_cipher_algorithm_t gnutls_cipher; - int cryptodev_cipher; - int keylen; +struct cipher_map +{ + gnutls_cipher_algorithm_t gnutls_cipher; + int cryptodev_cipher; + int keylen; }; static const struct cipher_map cipher_map[] = { - {GNUTLS_CIPHER_3DES_CBC, CRYPTO_3DES_CBC, 21}, - {GNUTLS_CIPHER_AES_128_CBC, CRYPTO_AES_CBC, 16}, - {GNUTLS_CIPHER_AES_192_CBC, CRYPTO_AES_CBC, 24}, - {GNUTLS_CIPHER_AES_256_CBC, CRYPTO_AES_CBC, 32}, - {GNUTLS_CIPHER_CAMELLIA_128_CBC, CRYPTO_CAMELLIA_CBC, 16}, - {GNUTLS_CIPHER_CAMELLIA_256_CBC, CRYPTO_CAMELLIA_CBC, 24}, - {GNUTLS_CIPHER_DES_CBC, CRYPTO_DES_CBC, 8}, - {GNUTLS_CIPHER_UNKNOWN, 0} + {GNUTLS_CIPHER_3DES_CBC, CRYPTO_3DES_CBC, 21}, + {GNUTLS_CIPHER_AES_128_CBC, CRYPTO_AES_CBC, 16}, + {GNUTLS_CIPHER_AES_192_CBC, CRYPTO_AES_CBC, 24}, + {GNUTLS_CIPHER_AES_256_CBC, CRYPTO_AES_CBC, 32}, + {GNUTLS_CIPHER_CAMELLIA_128_CBC, CRYPTO_CAMELLIA_CBC, 16}, + {GNUTLS_CIPHER_CAMELLIA_256_CBC, CRYPTO_CAMELLIA_CBC, 24}, + {GNUTLS_CIPHER_DES_CBC, CRYPTO_DES_CBC, 8}, + {GNUTLS_CIPHER_UNKNOWN, 0} }; -static int register_crypto(int cfd) -{ - struct session_op sess; - char fake_key[CRYPTO_CIPHER_MAX_KEY_LEN]; - int i=0, ret; - - memset(&sess, 0, sizeof(sess)); - do { - /* test if a cipher is support it and if yes register it */ - sess.cipher = cipher_map[i].cryptodev_cipher; - sess.keylen = cipher_map[i].keylen; - sess.key = fake_key; - - if (ioctl(cfd, CIOCGSESSION, &sess)) { - continue; - } - - ret = gnutls_crypto_single_cipher_register(cipher_map[i].gnutls_cipher, 90, &cipher_struct); - if (ret < 0) { - gnutls_assert(); - return ret; - } - - } while(cipher_map[i++].gnutls_cipher != GNUTLS_CIPHER_UNKNOWN); - - return 0; -} - -int _gnutls_cryptodev_init(void) +static int +register_crypto (int cfd) { - int cfd = -1, ret; - - /* Open the crypto device */ - cryptodev_fd = open("/dev/crypto", O_RDWR, 0); - if (cryptodev_fd < 0) { - gnutls_assert(); - return GNUTLS_E_CRYPTODEV_DEVICE_ERROR; + struct session_op sess; + char fake_key[CRYPTO_CIPHER_MAX_KEY_LEN]; + int i = 0, ret; + + memset (&sess, 0, sizeof (sess)); + do + { + /* test if a cipher is support it and if yes register it */ + sess.cipher = cipher_map[i].cryptodev_cipher; + sess.keylen = cipher_map[i].keylen; + sess.key = fake_key; + + if (ioctl (cfd, CIOCGSESSION, &sess)) + { + continue; } - /* Clone file descriptor */ - if (ioctl(cryptodev_fd, CRIOGET, &cfd)) { - gnutls_assert(); - return GNUTLS_E_CRYPTODEV_IOCTL_ERROR; + ret = + gnutls_crypto_single_cipher_register (cipher_map[i].gnutls_cipher, 90, + &cipher_struct); + if (ret < 0) + { + gnutls_assert (); + return ret; } - /* Set close-on-exec (not really neede here) */ - if (fcntl(cfd, F_SETFD, 1) == -1) { - gnutls_assert(); - return GNUTLS_E_CRYPTODEV_IOCTL_ERROR; - } + } + while (cipher_map[i++].gnutls_cipher != GNUTLS_CIPHER_UNKNOWN); - /* Run the test itself */ - ret = register_crypto(cfd); - - close(cfd); - return ret; + return 0; +} + +int +_gnutls_cryptodev_init (void) +{ + int cfd = -1, ret; + + /* Open the crypto device */ + cryptodev_fd = open ("/dev/crypto", O_RDWR, 0); + if (cryptodev_fd < 0) + { + gnutls_assert (); + return GNUTLS_E_CRYPTODEV_DEVICE_ERROR; + } + + /* Clone file descriptor */ + if (ioctl (cryptodev_fd, CRIOGET, &cfd)) + { + gnutls_assert (); + return GNUTLS_E_CRYPTODEV_IOCTL_ERROR; + } + + /* Set close-on-exec (not really neede here) */ + if (fcntl (cfd, F_SETFD, 1) == -1) + { + gnutls_assert (); + return GNUTLS_E_CRYPTODEV_IOCTL_ERROR; + } + + /* Run the test itself */ + ret = register_crypto (cfd); + + close (cfd); + return ret; } -void _gnutls_cryptodev_deinit() +void +_gnutls_cryptodev_deinit () { - close(cryptodev_fd); + close (cryptodev_fd); } #else -int _gnutls_cryptodev_init() +int +_gnutls_cryptodev_init () { - return 0; + return 0; } -void _gnutls_cryptodev_deinit() +void +_gnutls_cryptodev_deinit () { - return; + return; } #endif /* ENABLE_CRYPTODEV */ |