All of lore.kernel.org
 help / color / mirror / Atom feed
From: James Prestwood <james.prestwood@linux.intel.com>
To: ofono@ofono.org
Subject: [PATCH 2/5] aes: small module for AES128
Date: Tue, 03 Oct 2017 09:32:35 -0700	[thread overview]
Message-ID: <1507048360-15441-2-git-send-email-james.prestwood@linux.intel.com> (raw)
In-Reply-To: <1507048360-15441-1-git-send-email-james.prestwood@linux.intel.com>

[-- Attachment #1: Type: text/plain, Size: 4688 bytes --]

A new module with AES128 functionality using kernel crypto
APIs. SimAuth module requires AES128 for UMTS authenticate
command.
---
 src/aes.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/aes.h |  32 +++++++++++++++++
 2 files changed, 153 insertions(+)
 create mode 100644 src/aes.c
 create mode 100644 src/aes.h

diff --git a/src/aes.c b/src/aes.c
new file mode 100644
index 0000000..921ec79
--- /dev/null
+++ b/src/aes.c
@@ -0,0 +1,121 @@
+/****************************************************************************
+**
+** This file is part of the Qt Extended Opensource Package.
+**
+** Copyright (C) 2017  Intel Corporation. All rights reserved.
+**
+** This file may be used under the terms of the GNU General Public License
+** version 2.0 as published by the Free Software Foundation and appearing
+** in the file LICENSE.GPL included in the packaging of this file.
+**
+** Please review the following information to ensure GNU General Public
+** Licensing requirements will be met:
+**     http://www.fsf.org/licensing/licenses/info/GPLv2.html.
+**
+**
+****************************************************************************/
+
+#define _GNU_SOURCE
+#include <unistd.h>
+#include <sys/socket.h>
+#include <alloca.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "aes.h"
+
+#ifndef AF_ALG
+#define AF_ALG	38
+#define PF_ALG	AF_ALG
+#endif
+
+#ifndef HAVE_LINUX_IF_ALG_H
+struct sockaddr_alg {
+    uint16_t    salg_family;
+    uint8_t     salg_type[14];
+    uint32_t    salg_feat;
+    uint32_t    salg_mask;
+    uint8_t     salg_name[64];
+};
+
+#define ALG_SET_KEY     1
+#define ALG_SET_OP      3
+#define ALG_OP_ENCRYPT  1
+#else
+#include <linux/if_alg.h>
+#endif
+
+#ifndef SOL_ALG
+#define SOL_ALG 279
+#endif
+
+bool aes_encrypt(const uint8_t *key, size_t key_len, const uint8_t *in,
+        uint8_t *out, size_t len)
+{
+    uint32_t op = ALG_OP_ENCRYPT;
+    char *msg_buf;
+    size_t msg_size;
+    struct msghdr msg;
+    struct cmsghdr *c_msg;
+    ssize_t result;
+
+    struct sockaddr_alg sa = {
+            .salg_family = AF_ALG,
+            .salg_type = "skcipher",
+            .salg_name = "ecb(aes)"
+    };
+
+    struct iovec iov = {
+            .iov_base = (void *) in,
+            .iov_len = len
+    };
+
+    int sock = socket(AF_ALG, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
+    if (bind(sock, (struct sockaddr *)&sa, sizeof(sa))) {
+        close(sock);
+        return 0;
+    }
+    if (setsockopt(sock, SOL_ALG, ALG_SET_KEY, key, key_len)) {
+        close(sock);
+        return 0;
+    }
+
+    int cipher = accept4(sock, NULL, 0, SOCK_CLOEXEC);
+    if (cipher == -1) {
+        close(sock);
+        return 0;
+    }
+    close(sock);
+
+    msg_size = CMSG_SPACE(sizeof(op));
+
+    msg_buf = alloca(msg_size);
+
+    memset(msg_buf, 0, msg_size);
+    memset(&msg, 0, sizeof(msg));
+
+    msg.msg_iov = &iov;
+    msg.msg_control = msg_buf;
+    msg.msg_controllen = msg_size;
+    msg.msg_iovlen = 1;
+
+    c_msg = CMSG_FIRSTHDR(&msg);
+    c_msg->cmsg_level = SOL_ALG;
+    c_msg->cmsg_type = ALG_SET_OP;
+    c_msg->cmsg_len = CMSG_LEN(sizeof(op));
+    memcpy(CMSG_DATA(c_msg), &op, sizeof(op));
+
+    result = sendmsg(cipher, &msg, 0);
+    if (result < 0) {
+        return 0;
+    }
+
+    result = read(cipher, out, len);
+    if (result < 0) {
+        return 0;
+    }
+
+    return 1;
+}
+
diff --git a/src/aes.h b/src/aes.h
new file mode 100644
index 0000000..278e0cc
--- /dev/null
+++ b/src/aes.h
@@ -0,0 +1,32 @@
+/****************************************************************************
+**
+** This file is part of the Qt Extended Opensource Package.
+**
+** Copyright (C) 2017  Intel Corporation. All rights reserved.
+**
+** This file may be used under the terms of the GNU General Public License
+** version 2.0 as published by the Free Software Foundation and appearing
+** in the file LICENSE.GPL included in the packaging of this file.
+**
+** Please review the following information to ensure GNU General Public
+** Licensing requirements will be met:
+**     http://www.fsf.org/licensing/licenses/info/GPLv2.html.
+**
+**
+****************************************************************************/
+
+#ifndef CIPHER_H
+#define CIPHER_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+bool aes_encrypt(const uint8_t *key, size_t key_len, const uint8_t *in,
+        uint8_t *out, size_t len);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CIPHER_H */
-- 
2.7.4


  reply	other threads:[~2017-10-03 16:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-03 16:32 [PATCH 1/5] comp128: GSM algorithm (comp128) implementation James Prestwood
2017-10-03 16:32 ` James Prestwood [this message]
2017-10-03 16:32 ` [PATCH 3/5] simauth: Added SimAuth class for GSM/UMTS authentication James Prestwood
2017-10-03 16:32 ` [PATCH 3/5] simauth: Added SimAuth class for GSM/UMTS auth James Prestwood
2017-10-03 16:32 ` [PATCH 4/5] make: add simauth/aes/comp128 to build James Prestwood
2017-10-03 16:34   ` James Prestwood
  -- strict thread matches above, loose matches on Subject: below --
2017-10-03 16:35 [PATCH 1/5] comp128: GSM algorithm (comp128) implementation James Prestwood
2017-10-03 16:35 ` [PATCH 2/5] aes: small module for AES128 James Prestwood

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1507048360-15441-2-git-send-email-james.prestwood@linux.intel.com \
    --to=james.prestwood@linux.intel.com \
    --cc=ofono@ofono.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.