source: rtems-libbsd/testsuite/crypto01/test_main.c @ f7a09b5

55-freebsd-126-freebsd-12
Last change on this file since f7a09b5 was 8189ea8, checked in by Sebastian Huber <sebastian.huber@…>, on 03/27/18 at 12:42:31

CRYPTO(4): Port to RTEMS

  • Property mode set to 100644
File size: 5.4 KB
Line 
1/*
2 * Copyright (c) 2018 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#include <sys/ioctl.h>
34#include <sys/time.h>
35#include <sys/stat.h>
36#include <sys/sysctl.h>
37#include <crypto/cryptodev.h>
38
39#include <assert.h>
40#include <fcntl.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45
46#define TEST_NAME "LIBBSD CRYPTO 1"
47
48#define KEY_LENGTH 16
49
50typedef struct {
51        int dev_fd;
52        int session_fd;
53        struct session2_op session;
54} test_context;
55
56static test_context test_instance;
57
58/* Test data obtained from http://cryptodev-linux.org/ */
59
60static const char iv[AES_BLOCK_LEN];
61
62static const char key_0[KEY_LENGTH] = { 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00,
63    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
64
65static const char plaintext_0[AES_BLOCK_LEN];
66
67static const char ciphertext_0[AES_BLOCK_LEN] = { 0xdf, 0x55, 0x6a, 0x33, 0x43,
68    0x8d, 0xb8, 0x7b, 0xc4, 0x1b, 0x17, 0x52, 0xc5, 0x5e, 0x5e, 0x49 };
69
70static const char key_1[KEY_LENGTH];
71
72static const char plaintext_1[AES_BLOCK_LEN] = { 0xff, 0xff, 0xff, 0xff, 0xff,
73    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00 };
74
75static const char ciphertext_1[AES_BLOCK_LEN] = { 0xb7, 0x97, 0x2b, 0x39, 0x41,
76    0xc4, 0x4b, 0x90, 0xaf, 0xa7, 0xb2, 0x64, 0xbf, 0xba, 0x73, 0x87 };
77
78static void
79aes_session_create(test_context *ctx, const void *key, size_t keylen)
80{
81        int rv;
82
83        rv = ioctl(ctx->dev_fd, CRIOGET, &ctx->session_fd);
84        assert(rv == 0);
85
86        memset(&ctx->session, 0, sizeof(ctx->session));
87        ctx->session.cipher = CRYPTO_AES_CBC;
88        ctx->session.key = (caddr_t)key;
89        ctx->session.keylen = (u_int32_t)keylen;
90        ctx->session.crid = CRYPTO_FLAG_HARDWARE | CRYPTO_FLAG_SOFTWARE;
91
92        rv = ioctl(ctx->session_fd, CIOCGSESSION2, &ctx->session);
93        assert(rv == 0);
94}
95
96static void
97aes_session_destroy(test_context *ctx)
98{
99        int rv;
100
101        rv = ioctl(ctx->session_fd, CIOCFSESSION, &ctx->session.ses);
102        assert(rv == 0);
103
104        rv = close(ctx->session_fd);
105        assert(rv == 0);
106}
107
108static void
109aes_encrypt(const test_context *ctx, const void *iv, const void *plaintext,
110    void *ciphertext, size_t len)
111{
112        struct crypt_op op;
113        int rv;
114
115        memset(&op, 0, sizeof(op));
116        op.op = COP_ENCRYPT;
117        op.ses = ctx->session.ses;
118        op.len = (u_int)len;
119        op.src = __DECONST(void *, plaintext);
120        op.dst = ciphertext;
121        op.iv = __DECONST(void *, iv);
122        rv = ioctl(ctx->session_fd, CIOCCRYPT, &op);
123        assert(rv == 0);
124}
125
126static void
127aes_decrypt(const test_context *ctx, const void *iv, const void *ciphertext,
128    void *plaintext, size_t len)
129{
130        struct crypt_op op;
131        int rv;
132
133        memset(&op, 0, sizeof(op));
134        op.op = COP_DECRYPT;
135        op.ses = ctx->session.ses;
136        op.len = (u_int)len;
137        op.src = __DECONST(void *, ciphertext);
138        op.dst = plaintext;
139        op.iv = __DECONST(void *, iv);
140        rv = ioctl(ctx->session_fd, CIOCCRYPT, &op);
141        assert(rv == 0);
142}
143
144static void
145aes_test(test_context *ctx, const char *key, const char *plaintext,
146    const char *expected_ciphertext)
147{
148        char ciphertext[AES_BLOCK_LEN];
149        char decrypted_ciphertext[AES_BLOCK_LEN];
150
151        aes_session_create(ctx, key, KEY_LENGTH);
152
153        memset(ciphertext, 0xff, AES_BLOCK_LEN);
154        aes_encrypt(ctx, iv, plaintext, ciphertext, AES_BLOCK_LEN);
155        assert(memcmp(ciphertext, expected_ciphertext, AES_BLOCK_LEN) == 0);
156
157        memset(decrypted_ciphertext, 0xff, AES_BLOCK_LEN);
158        aes_decrypt(ctx, iv, ciphertext, decrypted_ciphertext, AES_BLOCK_LEN);
159        assert(memcmp(decrypted_ciphertext, plaintext, AES_BLOCK_LEN) == 0);
160
161        aes_session_destroy(ctx);
162}
163
164static void
165test_main(void)
166{
167        test_context *ctx;
168        int allow;
169        int rv;
170
171        ctx = &test_instance;
172
173        allow = 1;
174        rv = sysctlbyname("kern.cryptodevallowsoft", NULL, NULL, &allow,
175            sizeof(allow));
176        assert(rv == 0);
177
178        ctx->dev_fd = open("/dev/crypto", O_RDWR);
179        assert(ctx->dev_fd >= 0);
180
181        aes_test(ctx, key_0, plaintext_0, ciphertext_0);
182        aes_test(ctx, key_1, plaintext_1, ciphertext_1);
183
184        rv = close(ctx->dev_fd);
185        assert(rv == 0);
186
187        exit(0);
188}
189
190#include <rtems/bsd/bsd.h>
191
192#include <machine/rtems-bsd-nexus-bus.h>
193
194SYSINIT_MODULE_REFERENCE(cryptodev);
195
196RTEMS_BSD_DEFINE_NEXUS_DEVICE(cryptosoft, 0, 0, NULL);
197
198#include <rtems/bsd/test/default-init.h>
Note: See TracBrowser for help on using the repository browser.