testing:support openbsd crypto test for nuttx
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
parent
ccd7d12bcb
commit
ac4b42fee7
84
LICENSE
84
LICENSE
@ -1723,3 +1723,87 @@ apps/netutils/chat/chat.h
|
||||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
apps/testing/crypto/3descbc.c
|
||||
=========================
|
||||
$OpenBSD: des3.c,v 1.8 2010/10/15 10:39:12 jsg Exp $
|
||||
Copyright (c) 2002 Markus Friedl. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
apps/testing/crypto/aesctr.c
|
||||
=========================
|
||||
$OpenBSD: aesctr.c,v 1.1 2005/05/25 05:47:53 markus Exp $
|
||||
Copyright (c) 2005 Markus Friedl <markus@openbsd.org>
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
apps/testing/crypto/aesxts.c
|
||||
=========================
|
||||
$OpenBSD: aes_xts.c,v 1.2 2013/10/06 16:59:34 jsing Exp $
|
||||
Copyright (c) 2002 Markus Friedl. All rights reserved.
|
||||
Copyright (c) 2008 Damien Miller. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
apps/testing/crypto/hmac.c
|
||||
=========================
|
||||
Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
@ -1,6 +1,7 @@
|
||||
/* $OpenBSD: des3.c,v 1.10 2021/12/13 16:56:49 deraadt Exp $ */
|
||||
|
||||
/*
|
||||
/****************************************************************************
|
||||
* apps/testing/crypto/3descbc.c
|
||||
* $OpenBSD: des3.c,v 1.8 2010/10/15 10:39:12 jsg Exp $
|
||||
*
|
||||
* Copyright (c) 2002 Markus Friedl. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@ -22,9 +23,16 @@
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
****************************************************************************/
|
||||
|
||||
#include <openssl/des.h>
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <crypto/cryptodev.h>
|
||||
#include <err.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
@ -32,141 +40,154 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* Stubs */
|
||||
|
||||
u_int32_t deflate_global(u_int8_t *, u_int32_t, int, u_int8_t **);
|
||||
|
||||
u_int32_t
|
||||
deflate_global(u_int8_t *data, u_int32_t size, int comp, u_int8_t **out)
|
||||
static int syscrypt(FAR const unsigned char *key, size_t klen,
|
||||
FAR const unsigned char *iv,
|
||||
FAR const unsigned char *in, FAR unsigned char *out,
|
||||
size_t len, int encrypt)
|
||||
{
|
||||
return 0;
|
||||
struct session_op session;
|
||||
struct crypt_op cryp;
|
||||
int cryptodev_fd = -1;
|
||||
|
||||
if ((cryptodev_fd = open("/dev/crypto", O_RDWR, 0)) < 0)
|
||||
{
|
||||
warn("/dev/crypto");
|
||||
goto err;
|
||||
}
|
||||
|
||||
memset(&session, 0, sizeof(session));
|
||||
session.cipher = CRYPTO_3DES_CBC;
|
||||
session.key = (caddr_t) key;
|
||||
session.keylen = klen;
|
||||
if (ioctl(cryptodev_fd, CIOCGSESSION, &session) == -1)
|
||||
{
|
||||
warn("CIOCGSESSION");
|
||||
goto err;
|
||||
}
|
||||
|
||||
memset(&cryp, 0, sizeof(cryp));
|
||||
cryp.ses = session.ses;
|
||||
cryp.op = encrypt ? COP_ENCRYPT : COP_DECRYPT;
|
||||
cryp.flags = 0;
|
||||
cryp.len = len;
|
||||
cryp.src = (caddr_t) in;
|
||||
cryp.dst = (caddr_t) out;
|
||||
cryp.iv = (caddr_t) iv;
|
||||
cryp.mac = 0;
|
||||
|
||||
if (ioctl(cryptodev_fd, CIOCCRYPT, &cryp) == -1)
|
||||
{
|
||||
warn("CIOCCRYPT");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (ioctl(cryptodev_fd, CIOCFSESSION, &session.ses) == -1)
|
||||
{
|
||||
warn("CIOCFSESSION");
|
||||
goto err;
|
||||
}
|
||||
|
||||
close(cryptodev_fd);
|
||||
return (0);
|
||||
|
||||
err:
|
||||
if (cryptodev_fd != -1)
|
||||
{
|
||||
close(cryptodev_fd);
|
||||
}
|
||||
|
||||
return (-1);
|
||||
}
|
||||
|
||||
void explicit_bzero(void *, size_t);
|
||||
|
||||
void
|
||||
explicit_bzero(void *b, size_t len)
|
||||
static int match(FAR unsigned char *a, FAR unsigned char *b, size_t len)
|
||||
{
|
||||
bzero(b, len);
|
||||
int i;
|
||||
|
||||
if (memcmp(a, b, len) == 0)
|
||||
return (1);
|
||||
|
||||
warnx("decrypt/plaintext mismatch");
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
printf("%2.2x", a[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
printf("%2.2x", b[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
/* Simulate CBC mode */
|
||||
|
||||
static int
|
||||
docrypt(const unsigned char *key, size_t klen, const unsigned char *iv0,
|
||||
const unsigned char *in, unsigned char *out, size_t len, int encrypt)
|
||||
{
|
||||
u_int8_t block[8], iv[8], iv2[8], *ivp = iv, *nivp;
|
||||
u_int8_t ctx[384];
|
||||
int i, j, error = 0;
|
||||
|
||||
memcpy(iv, iv0, 8);
|
||||
memset(ctx, 0, sizeof(ctx));
|
||||
error = des3_setkey(ctx, key, klen);
|
||||
if (error)
|
||||
return -1;
|
||||
for (i = 0; i < len / 8; i ++) {
|
||||
bcopy(in, block, 8);
|
||||
in += 8;
|
||||
if (encrypt) {
|
||||
for (j = 0; j < 8; j++)
|
||||
block[j] ^= ivp[j];
|
||||
des3_encrypt(ctx, block);
|
||||
memcpy(ivp, block, 8);
|
||||
} else {
|
||||
nivp = ivp == iv ? iv2 : iv;
|
||||
memcpy(nivp, block, 8);
|
||||
des3_decrypt(ctx, block);
|
||||
for (j = 0; j < 8; j++)
|
||||
block[j] ^= ivp[j];
|
||||
ivp = nivp;
|
||||
}
|
||||
bcopy(block, out, 8);
|
||||
out += 8;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
match(unsigned char *a, unsigned char *b, size_t len)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (memcmp(a, b, len) == 0)
|
||||
return (1);
|
||||
|
||||
warnx("decrypt/plaintext mismatch");
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
printf("%2.2x", a[i]);
|
||||
printf("\n");
|
||||
for (i = 0; i < len; i++)
|
||||
printf("%2.2x", b[i]);
|
||||
printf("\n");
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
#define SZ 16
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
int main(int argc, FAR char **argv)
|
||||
{
|
||||
DES_key_schedule ks1, ks2, ks3;
|
||||
unsigned char iv0[8], iv[8], key[24] = "012345670123456701234567";
|
||||
unsigned char b1[SZ], b2[SZ];
|
||||
int i, fail = 0;
|
||||
u_int32_t rand = 0;
|
||||
unsigned char iv0[8];
|
||||
unsigned char iv[8];
|
||||
unsigned char key[24] = "012345670123456701234567";
|
||||
unsigned char b1[SZ];
|
||||
unsigned char b2[SZ];
|
||||
int i;
|
||||
int fail = 0;
|
||||
u_int32_t rand = 0;
|
||||
|
||||
/* setup data and iv */
|
||||
for (i = 0; i < sizeof(b1); i++ ) {
|
||||
if (i % 4 == 0)
|
||||
rand = arc4random();
|
||||
b1[i] = rand;
|
||||
rand >>= 8;
|
||||
}
|
||||
for (i = 0; i < sizeof(iv0); i++ ) {
|
||||
if (i % 4 == 0)
|
||||
rand = arc4random();
|
||||
iv0[i] = rand;
|
||||
rand >>= 8;
|
||||
}
|
||||
memset(b2, 0, sizeof(b2));
|
||||
/* setup data and iv */
|
||||
|
||||
/* keysetup for software */
|
||||
DES_set_key((void *) key, &ks1);
|
||||
DES_set_key((void *) (key+8), &ks2);
|
||||
DES_set_key((void *) (key+16), &ks3);
|
||||
for (i = 0; i < sizeof(b1); i++ )
|
||||
{
|
||||
if (i % 4 == 0)
|
||||
{
|
||||
rand = random();
|
||||
}
|
||||
|
||||
/* encrypt with software, decrypt with /dev/crypto */
|
||||
memcpy(iv, iv0, sizeof(iv0));
|
||||
DES_ede3_cbc_encrypt((void *)b1, (void*)b2, sizeof(b1), &ks1, &ks2,
|
||||
&ks3, (void*)iv, DES_ENCRYPT);
|
||||
memcpy(iv, iv0, sizeof(iv0));
|
||||
if (docrypt(key, sizeof(key), iv, b2, b2, sizeof(b1), 0) < 0) {
|
||||
warnx("decryption failed");
|
||||
fail++;
|
||||
}
|
||||
if (!match(b1, b2, sizeof(b1)))
|
||||
fail++;
|
||||
else
|
||||
printf("ok, decrypted\n");
|
||||
b1[i] = rand;
|
||||
rand >>= 8;
|
||||
}
|
||||
|
||||
/* encrypt with kernel functions, decrypt with openssl */
|
||||
memset(b2, 0, sizeof(b2));
|
||||
memcpy(iv, iv0, sizeof(iv0));
|
||||
if (docrypt(key, sizeof(key), iv, b1, b2, sizeof(b1), 1) < 0) {
|
||||
warnx("encryption failed");
|
||||
fail++;
|
||||
}
|
||||
memcpy(iv, iv0, sizeof(iv0));
|
||||
DES_ede3_cbc_encrypt((void *)b2, (void*)b2, sizeof(b1), &ks1, &ks2,
|
||||
&ks3, (void*)iv, DES_DECRYPT);
|
||||
if (!match(b1, b2, sizeof(b1)))
|
||||
fail++;
|
||||
else
|
||||
printf("ok, encrypted\n");
|
||||
for (i = 0; i < sizeof(iv0); i++ )
|
||||
{
|
||||
if (i % 4 == 0)
|
||||
{
|
||||
rand = random();
|
||||
}
|
||||
|
||||
exit((fail > 0) ? 1 : 0);
|
||||
iv0[i] = rand;
|
||||
rand >>= 8;
|
||||
}
|
||||
|
||||
memset(b2, 0, sizeof(b2));
|
||||
memcpy(iv, iv0, sizeof(iv0));
|
||||
|
||||
if (syscrypt(key, sizeof(key), iv, b1, b2, sizeof(b1), 1) < 0)
|
||||
{
|
||||
warnx("encrypt with /dev/crypto failed");
|
||||
fail++;
|
||||
}
|
||||
|
||||
memcpy(iv, iv0, sizeof(iv0));
|
||||
if (syscrypt(key, sizeof(key), iv, b2, b2, sizeof(b1), 0) < 0)
|
||||
{
|
||||
warnx("decrypt with /dev/crypto failed");
|
||||
fail++;
|
||||
}
|
||||
|
||||
if (!match(b1, b2, sizeof(b1)))
|
||||
{
|
||||
fail++;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("ok, encrypt with /dev/crypto, decrypt with /dev/crypto\n");
|
||||
}
|
||||
|
||||
exit((fail > 0) ? 1 : 0);
|
||||
}
|
||||
|
17
testing/crypto/Kconfig
Normal file
17
testing/crypto/Kconfig
Normal file
@ -0,0 +1,17 @@
|
||||
config TESTING_CRYPTO
|
||||
tristate "crypto test"
|
||||
default n
|
||||
---help---
|
||||
Enable crypto test
|
||||
|
||||
if TESTING_CRYPTO
|
||||
|
||||
config TESTING_CRYPTO_PRIORITY
|
||||
int "crypto test task priority"
|
||||
default 100
|
||||
|
||||
config TESTING_CRYPTO_STACKSIZE
|
||||
int "crypto test stack size"
|
||||
default DEFAULT_TASK_STACKSIZE
|
||||
|
||||
endif
|
23
testing/crypto/Make.defs
Normal file
23
testing/crypto/Make.defs
Normal file
@ -0,0 +1,23 @@
|
||||
############################################################################
|
||||
# apps/testing/fstest/Make.defs
|
||||
#
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership. The
|
||||
# ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance with the
|
||||
# License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
ifneq ($(CONFIG_TESTING_CRYPTO),)
|
||||
CONFIGURED_APPS += $(APPDIR)/testing/crypto
|
||||
endif
|
32
testing/crypto/Makefile
Normal file
32
testing/crypto/Makefile
Normal file
@ -0,0 +1,32 @@
|
||||
############################################################################
|
||||
# apps/testing/fstest/Makefile
|
||||
#
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership. The
|
||||
# ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance with the
|
||||
# License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
include $(APPDIR)/Make.defs
|
||||
|
||||
# Generic file system stress test application info
|
||||
PROGNAME = des3cbc aesxts aesctr hmac
|
||||
MAINSRC = 3descbc.c aesxts.c aesctr.c hmac.c
|
||||
PRIORITY = $(CONFIG_TESTING_CRYPTO_PRIORITY)
|
||||
STACKSIZE = $(CONFIG_TESTING_CRYPTO_STACKSIZE)
|
||||
MODULE = $(CONFIG_TESTING_CRYPTO)
|
||||
|
||||
# Generic file system stress test
|
||||
|
||||
include $(APPDIR)/Application.mk
|
@ -1,6 +1,7 @@
|
||||
/* $OpenBSD: aesctr.c,v 1.4 2021/12/13 16:56:49 deraadt Exp $ */
|
||||
|
||||
/*
|
||||
/****************************************************************************
|
||||
* apps/testing/crypto/aesctr.c
|
||||
* $OpenBSD: aesctr.c,v 1.1 2005/05/25 05:47:53 markus Exp $
|
||||
*
|
||||
* Copyright (c) 2005 Markus Friedl <markus@openbsd.org>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
@ -14,11 +15,18 @@
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <crypto/aes.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <crypto/cryptodev.h>
|
||||
#include <err.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -26,237 +34,313 @@
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
|
||||
int debug = 0;
|
||||
static int debug = 0;
|
||||
|
||||
enum { TST_KEY, TST_IV, TST_PLAIN, TST_CIPHER, TST_NUM };
|
||||
enum
|
||||
{
|
||||
TST_KEY,
|
||||
TST_IV,
|
||||
TST_PLAIN,
|
||||
TST_CIPHER,
|
||||
TST_NUM
|
||||
};
|
||||
|
||||
/* Test vectors from RFC 3686 */
|
||||
struct {
|
||||
char *data[TST_NUM];
|
||||
} tests[] = {
|
||||
/* 128 bit key */
|
||||
{
|
||||
"AE 68 52 F8 12 10 67 CC 4B F7 A5 76 55 77 F3 9E "
|
||||
"00 00 00 30",
|
||||
"00 00 00 00 00 00 00 00",
|
||||
"53 69 6E 67 6C 65 20 62 6C 6F 63 6B 20 6D 73 67",
|
||||
"E4 09 5D 4F B7 A7 B3 79 2D 61 75 A3 26 13 11 B8"
|
||||
},
|
||||
{
|
||||
"7E 24 06 78 17 FA E0 D7 43 D6 CE 1F 32 53 91 63 "
|
||||
"00 6C B6 DB",
|
||||
"C0 54 3B 59 DA 48 D9 0B",
|
||||
"00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F "
|
||||
"10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F",
|
||||
"51 04 A1 06 16 8A 72 D9 79 0D 41 EE 8E DA D3 88 "
|
||||
"EB 2E 1E FC 46 DA 57 C8 FC E6 30 DF 91 41 BE 28"
|
||||
},
|
||||
{
|
||||
"76 91 BE 03 5E 50 20 A8 AC 6E 61 85 29 F9 A0 DC "
|
||||
"00 E0 01 7B",
|
||||
"27 77 7F 3F 4A 17 86 F0",
|
||||
"00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F "
|
||||
"10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F"
|
||||
/*"20 21 22 23"*/,
|
||||
"C1 CF 48 A8 9F 2F FD D9 CF 46 52 E9 EF DB 72 D7 "
|
||||
"45 40 A4 2B DE 6D 78 36 D5 9A 5C EA AE F3 10 53"
|
||||
/*"25 B2 07 2F"*/
|
||||
},
|
||||
/* 192 bit key */
|
||||
{
|
||||
"16 AF 5B 14 5F C9 F5 79 C1 75 F9 3E 3B FB 0E ED "
|
||||
"86 3D 06 CC FD B7 85 15 "
|
||||
"00 00 00 48",
|
||||
"36 73 3C 14 7D 6D 93 CB",
|
||||
"53 69 6E 67 6C 65 20 62 6C 6F 63 6B 20 6D 73 67",
|
||||
"4B 55 38 4F E2 59 C9 C8 4E 79 35 A0 03 CB E9 28",
|
||||
},
|
||||
{
|
||||
"7C 5C B2 40 1B 3D C3 3C 19 E7 34 08 19 E0 F6 9C "
|
||||
"67 8C 3D B8 E6 F6 A9 1A "
|
||||
"00 96 B0 3B",
|
||||
"02 0C 6E AD C2 CB 50 0D",
|
||||
"00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F "
|
||||
"10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F",
|
||||
"45 32 43 FC 60 9B 23 32 7E DF AA FA 71 31 CD 9F "
|
||||
"84 90 70 1C 5A D4 A7 9C FC 1F E0 FF 42 F4 FB 00",
|
||||
},
|
||||
{
|
||||
"02 BF 39 1E E8 EC B1 59 B9 59 61 7B 09 65 27 9B "
|
||||
"F5 9B 60 A7 86 D3 E0 FE "
|
||||
"00 07 BD FD",
|
||||
"5C BD 60 27 8D CC 09 12",
|
||||
"00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F "
|
||||
"10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F"
|
||||
/*"20 21 22 23"*/,
|
||||
"96 89 3F C5 5E 5C 72 2F 54 0B 7D D1 DD F7 E7 58 "
|
||||
"D2 88 BC 95 C6 91 65 88 45 36 C8 11 66 2F 21 88"
|
||||
/*"AB EE 09 35"*/,
|
||||
},
|
||||
/* 256 bit key */
|
||||
{
|
||||
"77 6B EF F2 85 1D B0 6F 4C 8A 05 42 C8 69 6F 6C "
|
||||
"6A 81 AF 1E EC 96 B4 D3 7F C1 D6 89 E6 C1 C1 04 "
|
||||
"00 00 00 60",
|
||||
"DB 56 72 C9 7A A8 F0 B2",
|
||||
"53 69 6E 67 6C 65 20 62 6C 6F 63 6B 20 6D 73 67",
|
||||
"14 5A D0 1D BF 82 4E C7 56 08 63 DC 71 E3 E0 C0"
|
||||
},
|
||||
{
|
||||
"F6 D6 6D 6B D5 2D 59 BB 07 96 36 58 79 EF F8 86 "
|
||||
"C6 6D D5 1A 5B 6A 99 74 4B 50 59 0C 87 A2 38 84 "
|
||||
"00 FA AC 24",
|
||||
"C1 58 5E F1 5A 43 D8 75",
|
||||
"00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F "
|
||||
"10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F",
|
||||
"F0 5E 23 1B 38 94 61 2C 49 EE 00 0B 80 4E B2 A9 "
|
||||
"B8 30 6B 50 8F 83 9D 6A 55 30 83 1D 93 44 AF 1C",
|
||||
},
|
||||
{
|
||||
"FF 7A 61 7C E6 91 48 E4 F1 72 6E 2F 43 58 1D E2 "
|
||||
"AA 62 D9 F8 05 53 2E DF F1 EE D6 87 FB 54 15 3D "
|
||||
"00 1C C5 B7",
|
||||
"51 A5 1D 70 A1 C1 11 48",
|
||||
"00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F "
|
||||
"10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F"
|
||||
/*"20 21 22 23"*/,
|
||||
"EB 6C 52 82 1D 0B BB F7 CE 75 94 46 2A CA 4F AA "
|
||||
"B4 07 DF 86 65 69 FD 07 F4 8C C0 B5 83 D6 07 1F"
|
||||
/*"1E C0 E6 B8"*/,
|
||||
},
|
||||
|
||||
struct
|
||||
{
|
||||
FAR char *data[TST_NUM];
|
||||
}
|
||||
static tests[] =
|
||||
{
|
||||
/* 128 bit key */
|
||||
|
||||
{
|
||||
{
|
||||
"ae 68 52 f8 12 10 67 cc 4b f7 a5 76 55 77 f3 9e "
|
||||
"00 00 00 30",
|
||||
"00 00 00 00 00 00 00 00",
|
||||
"53 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 6d 73 67",
|
||||
"e4 09 5d 4f b7 a7 b3 79 2d 61 75 a3 26 13 11 b8"
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
"7e 24 06 78 17 fa e0 d7 43 d6 ce 1f 32 53 91 63 "
|
||||
"00 6c b6 db",
|
||||
"c0 54 3b 59 da 48 d9 0b",
|
||||
"00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f "
|
||||
"10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f",
|
||||
"51 04 a1 06 16 8a 72 d9 79 0d 41 ee 8e da d3 88 "
|
||||
"eb 2e 1e fc 46 da 57 c8 fc e6 30 df 91 41 be 28"
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
"76 91 be 03 5e 50 20 a8 ac 6e 61 85 29 f9 a0 dc "
|
||||
"00 e0 01 7b",
|
||||
"27 77 7f 3f 4a 17 86 f0",
|
||||
"00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f "
|
||||
"10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f"
|
||||
/* "20 21 22 23" */,
|
||||
|
||||
"c1 cf 48 a8 9f 2f fd d9 cf 46 52 e9 ef db 72 d7 "
|
||||
"45 40 a4 2b de 6d 78 36 d5 9a 5c ea ae f3 10 53"
|
||||
|
||||
/* "25 b2 07 2f" */
|
||||
}
|
||||
},
|
||||
|
||||
/* 192 bit key */
|
||||
|
||||
{
|
||||
{
|
||||
"16 af 5b 14 5f c9 f5 79 c1 75 f9 3e 3b fb 0e ed "
|
||||
"86 3d 06 cc fd b7 85 15 "
|
||||
"00 00 00 48",
|
||||
"36 73 3c 14 7d 6d 93 cb",
|
||||
"53 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 6d 73 67",
|
||||
"4b 55 38 4f e2 59 c9 c8 4e 79 35 a0 03 cb e9 28",
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
"7c 5c b2 40 1b 3d c3 3c 19 e7 34 08 19 e0 f6 9c "
|
||||
"67 8c 3d b8 e6 f6 a9 1a "
|
||||
"00 96 b0 3b",
|
||||
"02 0c 6e ad c2 cb 50 0d",
|
||||
"00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f "
|
||||
"10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f",
|
||||
"45 32 43 fc 60 9b 23 32 7e df aa fa 71 31 cd 9f "
|
||||
"84 90 70 1c 5a d4 a7 9c fc 1f e0 ff 42 f4 fb 00",
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
"02 bf 39 1e e8 ec b1 59 b9 59 61 7b 09 65 27 9b "
|
||||
"f5 9b 60 a7 86 d3 e0 fe "
|
||||
"00 07 bd fd",
|
||||
"5c bd 60 27 8d cc 09 12",
|
||||
"00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f "
|
||||
"10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f"
|
||||
/* "20 21 22 23" */,
|
||||
|
||||
"96 89 3f c5 5e 5c 72 2f 54 0b 7d d1 dd f7 e7 58 "
|
||||
"d2 88 bc 95 c6 91 65 88 45 36 c8 11 66 2f 21 88"
|
||||
/* "ab ee 09 35" */,
|
||||
}
|
||||
},
|
||||
|
||||
/* 256 bit key */
|
||||
|
||||
{
|
||||
{
|
||||
"77 6b ef f2 85 1d b0 6f 4c 8a 05 42 c8 69 6f 6c "
|
||||
"6a 81 af 1e ec 96 b4 d3 7f c1 d6 89 e6 c1 c1 04 "
|
||||
"00 00 00 60",
|
||||
"db 56 72 c9 7a a8 f0 b2",
|
||||
"53 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 6d 73 67",
|
||||
"14 5a d0 1d bf 82 4e c7 56 08 63 dc 71 e3 e0 c0"
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
"f6 d6 6d 6b d5 2d 59 bb 07 96 36 58 79 ef f8 86 "
|
||||
"c6 6d d5 1a 5b 6a 99 74 4b 50 59 0c 87 a2 38 84 "
|
||||
"00 fa ac 24",
|
||||
"c1 58 5e f1 5a 43 d8 75",
|
||||
"00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f "
|
||||
"10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f",
|
||||
"f0 5e 23 1b 38 94 61 2c 49 ee 00 0b 80 4e b2 a9 "
|
||||
"b8 30 6b 50 8f 83 9d 6a 55 30 83 1d 93 44 af 1c",
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
"ff 7a 61 7c e6 91 48 e4 f1 72 6e 2f 43 58 1d e2 "
|
||||
"aa 62 d9 f8 05 53 2e df f1 ee d6 87 fb 54 15 3d "
|
||||
"00 1c c5 b7",
|
||||
"51 a5 1d 70 a1 c1 11 48",
|
||||
"00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f "
|
||||
"10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f"
|
||||
/* "20 21 22 23" */,
|
||||
|
||||
"eb 6c 52 82 1d 0b bb f7 ce 75 94 46 2a ca 4f aa "
|
||||
"b4 07 df 86 65 69 fd 07 f4 8c c0 b5 83 d6 07 1f"
|
||||
/* "1e c0 e6 b8" */,
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
/* Stubs */
|
||||
|
||||
u_int32_t deflate_global(u_int8_t *, u_int32_t, int, u_int8_t **);
|
||||
|
||||
u_int32_t
|
||||
deflate_global(u_int8_t *data, u_int32_t size, int comp, u_int8_t **out)
|
||||
static int syscrypt(FAR const unsigned char *key, size_t klen,
|
||||
FAR const unsigned char *iv, FAR const unsigned char *in,
|
||||
FAR unsigned char *out, size_t len, int encrypt)
|
||||
{
|
||||
return 0;
|
||||
struct session_op session;
|
||||
struct crypt_op cryp;
|
||||
int cryptodev_fd = -1;
|
||||
|
||||
if ((cryptodev_fd = open("/dev/crypto", O_RDWR, 0)) < 0)
|
||||
{
|
||||
warn("/dev/crypto");
|
||||
goto err;
|
||||
}
|
||||
|
||||
memset(&session, 0, sizeof(session));
|
||||
session.cipher = CRYPTO_AES_CTR;
|
||||
session.key = (caddr_t) key;
|
||||
session.keylen = klen;
|
||||
if (ioctl(cryptodev_fd, CIOCGSESSION, &session) == -1)
|
||||
{
|
||||
warn("CIOCGSESSION");
|
||||
goto err;
|
||||
}
|
||||
|
||||
memset(&cryp, 0, sizeof(cryp));
|
||||
cryp.ses = session.ses;
|
||||
cryp.op = encrypt ? COP_ENCRYPT : COP_DECRYPT;
|
||||
cryp.flags = 0;
|
||||
cryp.len = len;
|
||||
cryp.src = (caddr_t) in;
|
||||
cryp.dst = (caddr_t) out;
|
||||
cryp.iv = (caddr_t) iv;
|
||||
cryp.mac = 0;
|
||||
if (ioctl(cryptodev_fd, CIOCCRYPT, &cryp) == -1)
|
||||
{
|
||||
warn("CIOCCRYPT");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (ioctl(cryptodev_fd, CIOCFSESSION, &session.ses) == -1)
|
||||
{
|
||||
warn("CIOCFSESSION");
|
||||
goto err;
|
||||
}
|
||||
|
||||
close(cryptodev_fd);
|
||||
return (0);
|
||||
|
||||
err:
|
||||
if (cryptodev_fd != -1)
|
||||
{
|
||||
close(cryptodev_fd);
|
||||
}
|
||||
|
||||
return (-1);
|
||||
}
|
||||
|
||||
void explicit_bzero(void *, size_t);
|
||||
|
||||
void
|
||||
explicit_bzero(void *b, size_t len)
|
||||
static int match(FAR unsigned char *a, FAR unsigned char *b, size_t len)
|
||||
{
|
||||
bzero(b, len);
|
||||
int i;
|
||||
|
||||
if (memcmp(a, b, len) == 0)
|
||||
{
|
||||
return (1);
|
||||
}
|
||||
|
||||
warnx("ciphertext mismatch");
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
printf("%2.2x", a[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
printf("%2.2x", b[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Definitions from /sys/crypto/xform.c */
|
||||
|
||||
#define AESCTR_NONCESIZE 4
|
||||
#define AESCTR_IVSIZE 8
|
||||
#define AESCTR_BLOCKSIZE 16
|
||||
|
||||
struct aes_ctr_ctx {
|
||||
AES_CTX ac_key;
|
||||
u_int8_t ac_block[AESCTR_BLOCKSIZE];
|
||||
};
|
||||
|
||||
int aes_ctr_setkey(void *, u_int8_t *, int);
|
||||
void aes_ctr_encrypt(caddr_t, u_int8_t *);
|
||||
void aes_ctr_decrypt(caddr_t, u_int8_t *);
|
||||
void aes_ctr_reinit(caddr_t, u_int8_t *);
|
||||
|
||||
static int
|
||||
docrypt(const unsigned char *key, size_t klen, const unsigned char *iv,
|
||||
const unsigned char *in, unsigned char *out, size_t len, int encrypt)
|
||||
static int run(int num)
|
||||
{
|
||||
u_int8_t block[AESCTR_BLOCKSIZE];
|
||||
struct aes_ctr_ctx ctx;
|
||||
int error = 0;
|
||||
size_t i;
|
||||
int i;
|
||||
int fail = 1;
|
||||
int len;
|
||||
int j;
|
||||
int length[TST_NUM];
|
||||
u_long val;
|
||||
FAR char *ep;
|
||||
FAR char *from;
|
||||
FAR u_char *p;
|
||||
FAR u_char *data[TST_NUM];
|
||||
|
||||
error = aes_ctr_setkey(&ctx, (u_int8_t *)key, klen);
|
||||
if (error)
|
||||
return -1;
|
||||
aes_ctr_reinit((caddr_t)&ctx, (u_int8_t *)iv);
|
||||
for (i = 0; i < len / AESCTR_BLOCKSIZE; i++) {
|
||||
bcopy(in, block, AESCTR_BLOCKSIZE);
|
||||
in += AESCTR_BLOCKSIZE;
|
||||
aes_ctr_crypt(&ctx, block);
|
||||
bcopy(block, out, AESCTR_BLOCKSIZE);
|
||||
out += AESCTR_BLOCKSIZE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
for (i = 0; i < TST_NUM; i++)
|
||||
{
|
||||
data[i] = NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
match(unsigned char *a, unsigned char *b, size_t len)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < TST_NUM; i++)
|
||||
{
|
||||
from = tests[num].data[i];
|
||||
if (debug)
|
||||
{
|
||||
printf("%s\n", from);
|
||||
}
|
||||
|
||||
if (memcmp(a, b, len) == 0)
|
||||
return (1);
|
||||
len = strlen(from);
|
||||
if ((p = malloc(len)) == 0)
|
||||
{
|
||||
warn("malloc");
|
||||
goto done;
|
||||
}
|
||||
|
||||
warnx("ciphertext mismatch");
|
||||
errno = 0;
|
||||
for (j = 0; j < len; j++)
|
||||
{
|
||||
val = strtoul(&from[j * 3], &ep, 16);
|
||||
p[j] = (u_char)val;
|
||||
if (*ep == '\0' || errno)
|
||||
break;
|
||||
}
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
printf("%2.2x", a[i]);
|
||||
printf("\n");
|
||||
for (i = 0; i < len; i++)
|
||||
printf("%2.2x", b[i]);
|
||||
printf("\n");
|
||||
length[i] = j + 1;
|
||||
data[i] = p;
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
len = length[TST_PLAIN];
|
||||
if ((p = malloc(len)) == 0)
|
||||
{
|
||||
warn("malloc");
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int
|
||||
run(int num)
|
||||
{
|
||||
int i, fail = 1, len, j, length[TST_NUM];
|
||||
u_long val;
|
||||
char *ep, *from;
|
||||
u_char *p, *data[TST_NUM];
|
||||
if (syscrypt(data[TST_KEY], length[TST_KEY],
|
||||
data[TST_IV], data[TST_PLAIN], p,
|
||||
length[TST_PLAIN], 0) < 0)
|
||||
{
|
||||
warnx("crypt with /dev/crypto failed");
|
||||
goto done;
|
||||
}
|
||||
|
||||
for (i = 0; i < TST_NUM; i++)
|
||||
data[i] = NULL;
|
||||
for (i = 0; i < TST_NUM; i++) {
|
||||
from = tests[num].data[i];
|
||||
if (debug)
|
||||
printf("%s\n", from);
|
||||
len = strlen(from);
|
||||
if ((p = malloc(len)) == 0) {
|
||||
warn("malloc");
|
||||
goto done;
|
||||
}
|
||||
errno = 0;
|
||||
for (j = 0; j < len; j++) {
|
||||
val = strtoul(&from[j*3], &ep, 16);
|
||||
p[j] = (u_char)val;
|
||||
if (*ep == '\0' || errno)
|
||||
break;
|
||||
}
|
||||
length[i] = j+1;
|
||||
data[i] = p;
|
||||
}
|
||||
len = length[TST_PLAIN];
|
||||
if ((p = malloc(len)) == 0) {
|
||||
warn("malloc");
|
||||
return (1);
|
||||
}
|
||||
if (docrypt(data[TST_KEY], length[TST_KEY],
|
||||
data[TST_IV], data[TST_PLAIN], p,
|
||||
length[TST_PLAIN], 0) < 0) {
|
||||
warnx("encryption failed");
|
||||
goto done;
|
||||
}
|
||||
fail = !match(data[TST_CIPHER], p, len);
|
||||
printf("%s test vector %d\n", fail ? "FAILED" : "OK", num);
|
||||
fail = !match(data[TST_CIPHER], p, len);
|
||||
printf("%s test vector %d\n", fail ? "FAILED" : "OK", num);
|
||||
done:
|
||||
for (i = 0; i < TST_NUM; i++)
|
||||
free(data[i]);
|
||||
return (fail);
|
||||
for (i = 0; i < TST_NUM; i++)
|
||||
{
|
||||
free(data[i]);
|
||||
}
|
||||
|
||||
return (fail);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
int main(int argc, FAR char **argv)
|
||||
{
|
||||
int fail = 0, i;
|
||||
int fail = 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++)
|
||||
fail += run(i);
|
||||
exit((fail > 0) ? 1 : 0);
|
||||
for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++)
|
||||
{
|
||||
fail += run(i);
|
||||
}
|
||||
|
||||
exit((fail > 0) ? 1 : 0);
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,9 +0,0 @@
|
||||
digest = 0x9294727a3638bb1c13f48ef8158bfc9d
|
||||
digest = 0x750c783e6ab0b503eaa86e310a5db738
|
||||
digest = 0x56be34521d144c88dbb8c733f0e8b3f6
|
||||
digest = 0x675b0b3a1b4ddf4e124872da6c2f632bfed957e9
|
||||
digest = 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
|
||||
digest = 0xd730594d167e35d5956fd8003d0db3d3f46dc7bb
|
||||
digest = 0x492ce020fe2534a5789dc3848806c78f4f6711397f08e7e7a12ca5a4483c8aa6
|
||||
digest = 0x5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843
|
||||
digest = 0x7dda3cc169743a6484649f94f0eda0f9f2ff496a9733fb796ed5adb40a44c3c1
|
@ -1,4 +1,5 @@
|
||||
/*-
|
||||
/****************************************************************************
|
||||
* apps/testing/crypto/hmac.c
|
||||
* Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
@ -12,87 +13,239 @@
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <err.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <crypto/cryptodev.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <crypto/md5.h>
|
||||
#include <crypto/sha1.h>
|
||||
#include <crypto/sha2.h>
|
||||
#include <crypto/hmac.h>
|
||||
#include <string.h>
|
||||
|
||||
static void
|
||||
print_hex(unsigned char *buf, int len)
|
||||
struct tb
|
||||
{
|
||||
int i;
|
||||
FAR char *key;
|
||||
int keylen;
|
||||
FAR char *data;
|
||||
int datalen;
|
||||
}
|
||||
testcase[] =
|
||||
{
|
||||
{
|
||||
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
|
||||
16,
|
||||
"Hi There",
|
||||
8,
|
||||
},
|
||||
{
|
||||
"Jefe",
|
||||
4,
|
||||
"what do ya want for nothing?",
|
||||
28,
|
||||
},
|
||||
{
|
||||
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
|
||||
16,
|
||||
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
|
||||
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
|
||||
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
|
||||
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
|
||||
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
|
||||
50
|
||||
},
|
||||
};
|
||||
|
||||
printf("digest = 0x");
|
||||
for (i = 0; i < len; i++)
|
||||
printf("%02x", buf[i]);
|
||||
printf("\n");
|
||||
FAR char *md5_result[] =
|
||||
{
|
||||
"\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d",
|
||||
"\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38",
|
||||
"\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6"
|
||||
};
|
||||
|
||||
FAR char *sha1_result[] =
|
||||
{
|
||||
"\x67\x5b\x0b\x3a\x1b\x4d\xdf\x4e\x12\x48\x72\xda\x6c\x2f\x63\x2b"
|
||||
"\xfe\xd9\x57\xe9",
|
||||
"\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c"
|
||||
"\x25\x9a\x7c\x79",
|
||||
"\xd7\x30\x59\x4d\x16\x7e\x35\xd5\x95\x6f\xd8\x00\x3d\x0d\xb3\xd3"
|
||||
"\xf4\x6d\xc7\xbb"
|
||||
};
|
||||
|
||||
FAR char *sha256_result[] =
|
||||
{
|
||||
"\x49\x2c\xe0\x20\xfe\x25\x34\xa5\x78\x9d\xc3\x84\x88\x06\xc7\x8f"
|
||||
"\x4f\x67\x11\x39\x7f\x08\xe7\xe7\xa1\x2c\xa5\xa4\x48\x3c\x8a\xa6",
|
||||
"\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75\xc7"
|
||||
"\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec\x38\x43",
|
||||
"\x7d\xda\x3c\xc1\x69\x74\x3a\x64\x84\x64\x9f\x94\xf0\xed\xa0\xf9"
|
||||
"\xf2\xff\x49\x6a\x97\x33\xfb\x79\x6e\xd5\xad\xb4\x0a\x44\xc3\xc1"
|
||||
};
|
||||
|
||||
int syshmac(int mac, FAR const char *key, size_t keylen,
|
||||
FAR const char *s, size_t len, FAR char *out)
|
||||
{
|
||||
struct session_op session;
|
||||
struct crypt_op cryp;
|
||||
int cryptodev_fd = -1;
|
||||
|
||||
if ((cryptodev_fd = open("/dev/crypto", O_RDWR, 0)) < 0)
|
||||
{
|
||||
warn("/dev/crypto");
|
||||
goto err;
|
||||
}
|
||||
|
||||
memset(&session, 0, sizeof(session));
|
||||
session.cipher = 0;
|
||||
session.mac = mac;
|
||||
session.mackey = (caddr_t)key;
|
||||
session.mackeylen = keylen;
|
||||
if (ioctl(cryptodev_fd, CIOCGSESSION, &session) == -1)
|
||||
{
|
||||
warn("CIOCGSESSION");
|
||||
goto err;
|
||||
}
|
||||
|
||||
memset(&cryp, 0, sizeof(cryp));
|
||||
cryp.ses = session.ses;
|
||||
cryp.op = COP_ENCRYPT;
|
||||
cryp.flags = 0;
|
||||
cryp.src = (caddr_t) s;
|
||||
cryp.len = len;
|
||||
cryp.dst = 0;
|
||||
cryp.mac = (caddr_t) out;
|
||||
cryp.iv = 0;
|
||||
if (ioctl(cryptodev_fd, CIOCCRYPT, &cryp) == -1)
|
||||
{
|
||||
warn("CIOCCRYPT");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (ioctl(cryptodev_fd, CIOCFSESSION, &session.ses) == -1)
|
||||
{
|
||||
warn("CIOCFSESSION");
|
||||
goto err;
|
||||
};
|
||||
|
||||
close(cryptodev_fd);
|
||||
return 0;
|
||||
err:
|
||||
if (cryptodev_fd != -1)
|
||||
{
|
||||
close(cryptodev_fd);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
static int match(unsigned char *a, unsigned char *b, size_t len)
|
||||
{
|
||||
HMAC_MD5_CTX md5;
|
||||
HMAC_SHA1_CTX sha1;
|
||||
HMAC_SHA256_CTX sha256;
|
||||
u_int8_t data[50], output[32];
|
||||
int i;
|
||||
|
||||
HMAC_MD5_Init(&md5, "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16);
|
||||
HMAC_MD5_Update(&md5, "Hi There", 8);
|
||||
HMAC_MD5_Final(output, &md5);
|
||||
print_hex(output, MD5_DIGEST_LENGTH);
|
||||
if (memcmp(a, b, len) == 0)
|
||||
return (0);
|
||||
|
||||
HMAC_MD5_Init(&md5, "Jefe", 4);
|
||||
HMAC_MD5_Update(&md5, "what do ya want for nothing?", 28);
|
||||
HMAC_MD5_Final(output, &md5);
|
||||
print_hex(output, MD5_DIGEST_LENGTH);
|
||||
warnx("hmac mismatch");
|
||||
|
||||
HMAC_MD5_Init(&md5, "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 16);
|
||||
memset(data, 0xDD, sizeof data);
|
||||
HMAC_MD5_Update(&md5, data, sizeof data);
|
||||
HMAC_MD5_Final(output, &md5);
|
||||
print_hex(output, MD5_DIGEST_LENGTH);
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
printf("%02x", a[i]);
|
||||
}
|
||||
|
||||
HMAC_SHA1_Init(&sha1, "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16);
|
||||
HMAC_SHA1_Update(&sha1, "Hi There", 8);
|
||||
HMAC_SHA1_Final(output, &sha1);
|
||||
print_hex(output, SHA1_DIGEST_LENGTH);
|
||||
printf("\n");
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
printf("%02x", b[i]);
|
||||
}
|
||||
|
||||
HMAC_SHA1_Init(&sha1, "Jefe", 4);
|
||||
HMAC_SHA1_Update(&sha1, "what do ya want for nothing?", 28);
|
||||
HMAC_SHA1_Final(output, &sha1);
|
||||
print_hex(output, SHA1_DIGEST_LENGTH);
|
||||
printf("\n");
|
||||
|
||||
HMAC_SHA1_Init(&sha1, "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 16);
|
||||
memset(data, 0xDD, sizeof data);
|
||||
HMAC_SHA1_Update(&sha1, data, sizeof data);
|
||||
HMAC_SHA1_Final(output, &sha1);
|
||||
print_hex(output, SHA1_DIGEST_LENGTH);
|
||||
|
||||
HMAC_SHA256_Init(&sha256, "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16);
|
||||
HMAC_SHA256_Update(&sha256, "Hi There", 8);
|
||||
HMAC_SHA256_Final(output, &sha256);
|
||||
print_hex(output, SHA256_DIGEST_LENGTH);
|
||||
|
||||
HMAC_SHA256_Init(&sha256, "Jefe", 4);
|
||||
HMAC_SHA256_Update(&sha256, "what do ya want for nothing?", 28);
|
||||
HMAC_SHA256_Final(output, &sha256);
|
||||
print_hex(output, SHA256_DIGEST_LENGTH);
|
||||
|
||||
HMAC_SHA256_Init(&sha256, "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 16);
|
||||
memset(data, 0xDD, sizeof data);
|
||||
HMAC_SHA256_Update(&sha256, data, sizeof data);
|
||||
HMAC_SHA256_Final(output, &sha256);
|
||||
print_hex(output, SHA256_DIGEST_LENGTH);
|
||||
|
||||
return 0;
|
||||
return (1);
|
||||
}
|
||||
|
||||
void
|
||||
explicit_bzero(void *b, size_t len)
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
bzero(b, len);
|
||||
char output[32];
|
||||
int ret = 0;
|
||||
for (int i = 0; i < sizeof(testcase) / sizeof(struct tb); i++)
|
||||
{
|
||||
ret += syshmac(CRYPTO_MD5_HMAC, testcase[i].key,
|
||||
testcase[i].keylen,
|
||||
testcase[i].data, testcase[i].datalen, output);
|
||||
if (ret)
|
||||
{
|
||||
printf("syshamc md5 failed\n");
|
||||
}
|
||||
|
||||
ret += match((unsigned char *)md5_result[i],
|
||||
(unsigned char *)output,
|
||||
MD5_DIGEST_LENGTH);
|
||||
if (ret)
|
||||
{
|
||||
printf("match md5 failed\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("hmac md5 success\n");
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < sizeof(testcase) / sizeof(struct tb); i++)
|
||||
{
|
||||
ret = syshmac(CRYPTO_SHA1_HMAC, testcase[i].key,
|
||||
testcase[i].keylen,
|
||||
testcase[i].data, testcase[i].datalen, output);
|
||||
if (ret)
|
||||
{
|
||||
printf("syshamc sha1 failed\n");
|
||||
}
|
||||
|
||||
ret = match((unsigned char *)sha1_result[i],
|
||||
(unsigned char *)output,
|
||||
SHA1_DIGEST_LENGTH);
|
||||
if (ret)
|
||||
{
|
||||
printf("match sha1 failed\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("hmac sha1 success\n");
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < sizeof(testcase) / sizeof(struct tb); i++)
|
||||
{
|
||||
ret = syshmac(CRYPTO_SHA2_256_HMAC, testcase[i].key,
|
||||
testcase[i].keylen,
|
||||
testcase[i].data, testcase[i].datalen, output);
|
||||
if (ret)
|
||||
{
|
||||
printf("syshamc sha256 failed\n");
|
||||
}
|
||||
|
||||
ret = match((unsigned char *)sha256_result[i],
|
||||
(unsigned char *)output,
|
||||
SHA256_DIGEST_LENGTH);
|
||||
if (ret)
|
||||
{
|
||||
printf("match sha256 failed\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("hmac sha256 success\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user