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

6-freebsd-12
Last change on this file was 8e33f3b, checked in by Moyano, Gabriel <gabriel.moyano@…>, on 04/03/20 at 07:35:23

testsuite: A description for each test added

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