testing:support openbsd crypto test for nuttx

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao 2022-08-25 12:02:11 +08:00 committed by Xiang Xiao
parent ccd7d12bcb
commit ac4b42fee7
9 changed files with 2636 additions and 2176 deletions

84
LICENSE
View File

@ -1723,3 +1723,87 @@ apps/netutils/chat/chat.h
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. 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.

View File

@ -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. * Copyright (c) 2002 Markus Friedl. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * 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 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * 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 <err.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
@ -32,64 +40,66 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
/* Stubs */ static int syscrypt(FAR const unsigned char *key, size_t klen,
FAR const unsigned char *iv,
u_int32_t deflate_global(u_int8_t *, u_int32_t, int, u_int8_t **); FAR const unsigned char *in, FAR unsigned char *out,
size_t len, int encrypt)
u_int32_t
deflate_global(u_int8_t *data, u_int32_t size, int comp, u_int8_t **out)
{ {
return 0; struct session_op session;
} struct crypt_op cryp;
int cryptodev_fd = -1;
void explicit_bzero(void *, size_t); if ((cryptodev_fd = open("/dev/crypto", O_RDWR, 0)) < 0)
{
void warn("/dev/crypto");
explicit_bzero(void *b, size_t len) goto err;
{
bzero(b, len);
}
/* 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; 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;
} }
return 0;
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);
} }
static int static int match(FAR unsigned char *a, FAR unsigned char *b, size_t len)
match(unsigned char *a, unsigned char *b, size_t len)
{ {
int i; int i;
@ -99,74 +109,85 @@ match(unsigned char *a, unsigned char *b, size_t len)
warnx("decrypt/plaintext mismatch"); warnx("decrypt/plaintext mismatch");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{
printf("%2.2x", a[i]); printf("%2.2x", a[i]);
}
printf("\n"); printf("\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{
printf("%2.2x", b[i]); printf("%2.2x", b[i]);
}
printf("\n"); printf("\n");
return (0); return (0);
} }
#define SZ 16 #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];
unsigned char iv0[8], iv[8], key[24] = "012345670123456701234567"; unsigned char iv[8];
unsigned char b1[SZ], b2[SZ]; unsigned char key[24] = "012345670123456701234567";
int i, fail = 0; unsigned char b1[SZ];
unsigned char b2[SZ];
int i;
int fail = 0;
u_int32_t rand = 0; u_int32_t rand = 0;
/* setup data and iv */ /* setup data and iv */
for (i = 0; i < sizeof(b1); i++ ) {
for (i = 0; i < sizeof(b1); i++ )
{
if (i % 4 == 0) if (i % 4 == 0)
rand = arc4random(); {
rand = random();
}
b1[i] = rand; b1[i] = rand;
rand >>= 8; rand >>= 8;
} }
for (i = 0; i < sizeof(iv0); i++ ) {
for (i = 0; i < sizeof(iv0); i++ )
{
if (i % 4 == 0) if (i % 4 == 0)
rand = arc4random(); {
rand = random();
}
iv0[i] = rand; iv0[i] = rand;
rand >>= 8; rand >>= 8;
} }
memset(b2, 0, sizeof(b2));
/* keysetup for software */
DES_set_key((void *) key, &ks1);
DES_set_key((void *) (key+8), &ks2);
DES_set_key((void *) (key+16), &ks3);
/* 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");
/* encrypt with kernel functions, decrypt with openssl */
memset(b2, 0, sizeof(b2)); memset(b2, 0, sizeof(b2));
memcpy(iv, iv0, sizeof(iv0)); memcpy(iv, iv0, sizeof(iv0));
if (docrypt(key, sizeof(key), iv, b1, b2, sizeof(b1), 1) < 0) {
warnx("encryption failed"); if (syscrypt(key, sizeof(key), iv, b1, b2, sizeof(b1), 1) < 0)
{
warnx("encrypt with /dev/crypto failed");
fail++; fail++;
} }
memcpy(iv, iv0, sizeof(iv0)); memcpy(iv, iv0, sizeof(iv0));
DES_ede3_cbc_encrypt((void *)b2, (void*)b2, sizeof(b1), &ks1, &ks2, if (syscrypt(key, sizeof(key), iv, b2, b2, sizeof(b1), 0) < 0)
&ks3, (void*)iv, DES_DECRYPT); {
if (!match(b1, b2, sizeof(b1))) warnx("decrypt with /dev/crypto failed");
fail++; fail++;
}
if (!match(b1, b2, sizeof(b1)))
{
fail++;
}
else else
printf("ok, encrypted\n"); {
printf("ok, encrypt with /dev/crypto, decrypt with /dev/crypto\n");
}
exit((fail > 0) ? 1 : 0); exit((fail > 0) ? 1 : 0);
} }

17
testing/crypto/Kconfig Normal file
View 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
View 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
View 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

View File

@ -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> * Copyright (c) 2005 Markus Friedl <markus@openbsd.org>
* *
* Permission to use, copy, modify, and distribute this software for any * 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 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ ****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <sys/types.h> #include <sys/types.h>
#include <crypto/aes.h> #include <sys/param.h>
#include <sys/ioctl.h>
#include <crypto/cryptodev.h>
#include <err.h> #include <err.h>
#include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -26,237 +34,313 @@
#include <limits.h> #include <limits.h>
#include <errno.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 */ /* Test vectors from RFC 3686 */
struct {
char *data[TST_NUM]; struct
} tests[] = { {
FAR char *data[TST_NUM];
}
static tests[] =
{
/* 128 bit key */ /* 128 bit key */
{ {
"AE 68 52 F8 12 10 67 CC 4B F7 A5 76 55 77 F3 9E " {
"ae 68 52 f8 12 10 67 cc 4b f7 a5 76 55 77 f3 9e "
"00 00 00 30", "00 00 00 30",
"00 00 00 00 00 00 00 00", "00 00 00 00 00 00 00 00",
"53 69 6E 67 6C 65 20 62 6C 6F 63 6B 20 6D 73 67", "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" "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)
{
return 0;
}
void explicit_bzero(void *, size_t);
void
explicit_bzero(void *b, size_t len)
{
bzero(b, len);
}
/* 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)
{
u_int8_t block[AESCTR_BLOCKSIZE];
struct aes_ctr_ctx ctx;
int error = 0;
size_t i;
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; },
{
{
"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" */,
}
},
};
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)
{
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);
} }
static int static int match(FAR unsigned char *a, FAR unsigned char *b, size_t len)
match(unsigned char *a, unsigned char *b, size_t len)
{ {
int i; int i;
if (memcmp(a, b, len) == 0) if (memcmp(a, b, len) == 0)
{
return (1); return (1);
}
warnx("ciphertext mismatch"); warnx("ciphertext mismatch");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{
printf("%2.2x", a[i]); printf("%2.2x", a[i]);
}
printf("\n"); printf("\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{
printf("%2.2x", b[i]); printf("%2.2x", b[i]);
}
printf("\n"); printf("\n");
return (0); return (0);
} }
static int static int run(int num)
run(int num)
{ {
int i, fail = 1, len, j, length[TST_NUM]; int i;
int fail = 1;
int len;
int j;
int length[TST_NUM];
u_long val; u_long val;
char *ep, *from; FAR char *ep;
u_char *p, *data[TST_NUM]; FAR char *from;
FAR u_char *p;
FAR u_char *data[TST_NUM];
for (i = 0; i < TST_NUM; i++) for (i = 0; i < TST_NUM; i++)
{
data[i] = NULL; data[i] = NULL;
for (i = 0; i < TST_NUM; i++) { }
for (i = 0; i < TST_NUM; i++)
{
from = tests[num].data[i]; from = tests[num].data[i];
if (debug) if (debug)
{
printf("%s\n", from); printf("%s\n", from);
}
len = strlen(from); len = strlen(from);
if ((p = malloc(len)) == 0) { if ((p = malloc(len)) == 0)
{
warn("malloc"); warn("malloc");
goto done; goto done;
} }
errno = 0; errno = 0;
for (j = 0; j < len; j++) { for (j = 0; j < len; j++)
val = strtoul(&from[j*3], &ep, 16); {
val = strtoul(&from[j * 3], &ep, 16);
p[j] = (u_char)val; p[j] = (u_char)val;
if (*ep == '\0' || errno) if (*ep == '\0' || errno)
break; break;
} }
length[i] = j+1;
length[i] = j + 1;
data[i] = p; data[i] = p;
} }
len = length[TST_PLAIN]; len = length[TST_PLAIN];
if ((p = malloc(len)) == 0) { if ((p = malloc(len)) == 0)
{
warn("malloc"); warn("malloc");
return (1); return (1);
} }
if (docrypt(data[TST_KEY], length[TST_KEY],
if (syscrypt(data[TST_KEY], length[TST_KEY],
data[TST_IV], data[TST_PLAIN], p, data[TST_IV], data[TST_PLAIN], p,
length[TST_PLAIN], 0) < 0) { length[TST_PLAIN], 0) < 0)
warnx("encryption failed"); {
warnx("crypt with /dev/crypto failed");
goto done; goto done;
} }
fail = !match(data[TST_CIPHER], p, len); fail = !match(data[TST_CIPHER], p, len);
printf("%s test vector %d\n", fail ? "FAILED" : "OK", num); printf("%s test vector %d\n", fail ? "FAILED" : "OK", num);
done: done:
for (i = 0; i < TST_NUM; i++) for (i = 0; i < TST_NUM; i++)
{
free(data[i]); free(data[i]);
}
return (fail); 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++) for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++)
{
fail += run(i); fail += run(i);
}
exit((fail > 0) ? 1 : 0); exit((fail > 0) ? 1 : 0);
} }

View File

@ -1,6 +1,7 @@
/* $OpenBSD: aes_xts.c,v 1.4 2017/05/17 17:54:29 mikeb Exp $ */ /****************************************************************************
* 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) 2002 Markus Friedl. All rights reserved.
* Copyright (c) 2008 Damien Miller. All rights reserved. * Copyright (c) 2008 Damien Miller. All rights reserved.
* *
@ -23,10 +24,16 @@
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ ****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <sys/types.h> #include <sys/types.h>
#include <sys/ioctl.h>
#include <crypto/rijndael.h> #include <crypto/rijndael.h>
#include <crypto/cryptodev.h>
#include <err.h> #include <err.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
@ -36,18 +43,15 @@
#define AES_XTS_BLOCKSIZE 16 #define AES_XTS_BLOCKSIZE 16
struct aes_xts_ctx { struct aes_xts_ctx
{
rijndael_ctx key1; rijndael_ctx key1;
rijndael_ctx key2; rijndael_ctx key2;
u_int8_t tweak[AES_XTS_BLOCKSIZE]; u_int8_t tweak[AES_XTS_BLOCKSIZE];
}; };
int aes_xts_setkey(void *, u_int8_t *, int); struct aes_xts_tv
void aes_xts_encrypt(caddr_t, u_int8_t *); {
void aes_xts_decrypt(caddr_t, u_int8_t *);
void aes_xts_reinit(caddr_t, u_int8_t *);
struct aes_xts_tv {
u_int64_t seqno; u_int64_t seqno;
u_int key_len; u_int key_len;
u_int8_t key[64]; u_int8_t key[64];
@ -57,9 +61,11 @@ struct aes_xts_tv {
}; };
/* Test vectors from IEEE P1619/D16, Annex B. */ /* Test vectors from IEEE P1619/D16, Annex B. */
struct aes_xts_tv aes_xts_test_vectors[] = {
struct aes_xts_tv aes_xts_test_vectors[] =
{
{ {
0x00000000ULL, 0x00000000ull,
32, 32,
{ {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@ -73,7 +79,8 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}, { },
{
0x91, 0x7c, 0xf6, 0x9e, 0xbd, 0x68, 0xb2, 0xec, 0x91, 0x7c, 0xf6, 0x9e, 0xbd, 0x68, 0xb2, 0xec,
0x9b, 0x9f, 0xe9, 0xa3, 0xea, 0xdd, 0xa6, 0x92, 0x9b, 0x9f, 0xe9, 0xa3, 0xea, 0xdd, 0xa6, 0x92,
0xcd, 0x43, 0xd2, 0xf5, 0x95, 0x98, 0xed, 0x85, 0xcd, 0x43, 0xd2, 0xf5, 0x95, 0x98, 0xed, 0x85,
@ -81,7 +88,7 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
}, },
}, },
{ {
0x3333333333ULL, 0x3333333333ull,
32, 32,
{ {
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
@ -95,7 +102,8 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
}, { },
{
0xc4, 0x54, 0x18, 0x5e, 0x6a, 0x16, 0x93, 0x6e, 0xc4, 0x54, 0x18, 0x5e, 0x6a, 0x16, 0x93, 0x6e,
0x39, 0x33, 0x40, 0x38, 0xac, 0xef, 0x83, 0x8b, 0x39, 0x33, 0x40, 0x38, 0xac, 0xef, 0x83, 0x8b,
0xfb, 0x18, 0x6f, 0xff, 0x74, 0x80, 0xad, 0xc4, 0xfb, 0x18, 0x6f, 0xff, 0x74, 0x80, 0xad, 0xc4,
@ -103,7 +111,7 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
}, },
}, },
{ {
0x3333333333ULL, 0x3333333333ull,
32, 32,
{ {
0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8,
@ -117,7 +125,8 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
}, { },
{
0xaf, 0x85, 0x33, 0x6b, 0x59, 0x7a, 0xfc, 0x1a, 0xaf, 0x85, 0x33, 0x6b, 0x59, 0x7a, 0xfc, 0x1a,
0x90, 0x0b, 0x2e, 0xb2, 0x1e, 0xc9, 0x49, 0xd2, 0x90, 0x0b, 0x2e, 0xb2, 0x1e, 0xc9, 0x49, 0xd2,
0x92, 0xdf, 0x4c, 0x04, 0x7e, 0x0b, 0x21, 0x53, 0x92, 0xdf, 0x4c, 0x04, 0x7e, 0x0b, 0x21, 0x53,
@ -125,7 +134,7 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
}, },
}, },
{ {
0x00000000ULL, 0x00000000ull,
32, 32,
{ {
0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45,
@ -199,7 +208,8 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
}, { },
{
0x27, 0xa7, 0x47, 0x9b, 0xef, 0xa1, 0xd4, 0x76, 0x27, 0xa7, 0x47, 0x9b, 0xef, 0xa1, 0xd4, 0x76,
0x48, 0x9f, 0x30, 0x8c, 0xd4, 0xcf, 0xa6, 0xe2, 0x48, 0x9f, 0x30, 0x8c, 0xd4, 0xcf, 0xa6, 0xe2,
0xa9, 0x6e, 0x4b, 0xbe, 0x32, 0x08, 0xff, 0x25, 0xa9, 0x6e, 0x4b, 0xbe, 0x32, 0x08, 0xff, 0x25,
@ -267,7 +277,7 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
}, },
}, },
{ {
0x00000001ULL, 0x00000001ull,
32, 32,
{ {
0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45,
@ -341,7 +351,8 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
0xf2, 0x62, 0x73, 0x57, 0x79, 0xa4, 0x18, 0xf2, 0xf2, 0x62, 0x73, 0x57, 0x79, 0xa4, 0x18, 0xf2,
0x0a, 0x28, 0x2d, 0xf9, 0x20, 0x14, 0x7b, 0xea, 0x0a, 0x28, 0x2d, 0xf9, 0x20, 0x14, 0x7b, 0xea,
0xbe, 0x42, 0x1e, 0xe5, 0x31, 0x9d, 0x05, 0x68, 0xbe, 0x42, 0x1e, 0xe5, 0x31, 0x9d, 0x05, 0x68,
}, { },
{
0x26, 0x4d, 0x3c, 0xa8, 0x51, 0x21, 0x94, 0xfe, 0x26, 0x4d, 0x3c, 0xa8, 0x51, 0x21, 0x94, 0xfe,
0xc3, 0x12, 0xc8, 0xc9, 0x89, 0x1f, 0x27, 0x9f, 0xc3, 0x12, 0xc8, 0xc9, 0x89, 0x1f, 0x27, 0x9f,
0xef, 0xdd, 0x60, 0x8d, 0x0c, 0x02, 0x7b, 0x60, 0xef, 0xdd, 0x60, 0x8d, 0x0c, 0x02, 0x7b, 0x60,
@ -409,7 +420,7 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
}, },
}, },
{ {
0x00000002ULL, 0x00000002ull,
32, 32,
{ {
0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45,
@ -483,7 +494,8 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
0xb7, 0xb1, 0xd7, 0x81, 0x8a, 0x4b, 0x75, 0x3d, 0xb7, 0xb1, 0xd7, 0x81, 0x8a, 0x4b, 0x75, 0x3d,
0xfd, 0x2a, 0x89, 0xcc, 0xb4, 0x5e, 0x00, 0x1a, 0xfd, 0x2a, 0x89, 0xcc, 0xb4, 0x5e, 0x00, 0x1a,
0x03, 0xa8, 0x67, 0xb1, 0x87, 0xf2, 0x25, 0xdd, 0x03, 0xa8, 0x67, 0xb1, 0x87, 0xf2, 0x25, 0xdd,
}, { },
{
0xfa, 0x76, 0x2a, 0x36, 0x80, 0xb7, 0x60, 0x07, 0xfa, 0x76, 0x2a, 0x36, 0x80, 0xb7, 0x60, 0x07,
0x92, 0x8e, 0xd4, 0xa4, 0xf4, 0x9a, 0x94, 0x56, 0x92, 0x8e, 0xd4, 0xa4, 0xf4, 0x9a, 0x94, 0x56,
0x03, 0x1b, 0x70, 0x47, 0x82, 0xe6, 0x5e, 0x16, 0x03, 0x1b, 0x70, 0x47, 0x82, 0xe6, 0x5e, 0x16,
@ -551,7 +563,7 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
}, },
}, },
{ {
0x000000fdULL, 0x000000fdull,
32, 32,
{ {
0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45,
@ -625,7 +637,8 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
0x0e, 0xef, 0x0f, 0x2d, 0x3a, 0x1e, 0xaf, 0x61, 0x0e, 0xef, 0x0f, 0x2d, 0x3a, 0x1e, 0xaf, 0x61,
0xd3, 0xe0, 0x35, 0x55, 0xfd, 0x42, 0x4e, 0xed, 0xd3, 0xe0, 0x35, 0x55, 0xfd, 0x42, 0x4e, 0xed,
0xd6, 0x7e, 0x18, 0xa1, 0x80, 0x94, 0xf8, 0x88, 0xd6, 0x7e, 0x18, 0xa1, 0x80, 0x94, 0xf8, 0x88,
}, { },
{
0xd5, 0x5f, 0x68, 0x4f, 0x81, 0xf4, 0x42, 0x6e, 0xd5, 0x5f, 0x68, 0x4f, 0x81, 0xf4, 0x42, 0x6e,
0x9f, 0xde, 0x92, 0xa5, 0xff, 0x02, 0xdf, 0x2a, 0x9f, 0xde, 0x92, 0xa5, 0xff, 0x02, 0xdf, 0x2a,
0xc8, 0x96, 0xaf, 0x63, 0x96, 0x28, 0x88, 0xa9, 0xc8, 0x96, 0xaf, 0x63, 0x96, 0x28, 0x88, 0xa9,
@ -693,7 +706,7 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
}, },
}, },
{ {
0x000000feULL, 0x000000feull,
32, 32,
{ {
0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45,
@ -767,7 +780,8 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
0xb2, 0xe1, 0x22, 0xdd, 0xfd, 0x1c, 0xbd, 0xd1, 0xb2, 0xe1, 0x22, 0xdd, 0xfd, 0x1c, 0xbd, 0xd1,
0x1d, 0x1c, 0x0a, 0x67, 0x83, 0xe0, 0x11, 0xfc, 0x1d, 0x1c, 0x0a, 0x67, 0x83, 0xe0, 0x11, 0xfc,
0x53, 0x6d, 0x63, 0xd0, 0x53, 0x26, 0x06, 0x37, 0x53, 0x6d, 0x63, 0xd0, 0x53, 0x26, 0x06, 0x37,
}, { },
{
0x72, 0xef, 0xc1, 0xeb, 0xfe, 0x1e, 0xe2, 0x59, 0x72, 0xef, 0xc1, 0xeb, 0xfe, 0x1e, 0xe2, 0x59,
0x75, 0xa6, 0xeb, 0x3a, 0xa8, 0x58, 0x9d, 0xda, 0x75, 0xa6, 0xeb, 0x3a, 0xa8, 0x58, 0x9d, 0xda,
0x2b, 0x26, 0x1f, 0x1c, 0x85, 0xbd, 0xab, 0x44, 0x2b, 0x26, 0x1f, 0x1c, 0x85, 0xbd, 0xab, 0x44,
@ -835,7 +849,7 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
}, },
}, },
{ {
0x000000ffULL, 0x000000ffull,
32, 32,
{ {
0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45,
@ -909,7 +923,8 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
0x50, 0x62, 0x23, 0xbe, 0xa1, 0x6a, 0xf3, 0x5c, 0x50, 0x62, 0x23, 0xbe, 0xa1, 0x6a, 0xf3, 0x5c,
0x88, 0x3a, 0xcc, 0xdb, 0xe1, 0x10, 0x4e, 0xef, 0x88, 0x3a, 0xcc, 0xdb, 0xe1, 0x10, 0x4e, 0xef,
0x0c, 0xfd, 0xb5, 0x4e, 0x12, 0xfb, 0x23, 0x0a, 0x0c, 0xfd, 0xb5, 0x4e, 0x12, 0xfb, 0x23, 0x0a,
}, { },
{
0x32, 0x60, 0xae, 0x8d, 0xad, 0x1f, 0x4a, 0x32, 0x32, 0x60, 0xae, 0x8d, 0xad, 0x1f, 0x4a, 0x32,
0xc5, 0xca, 0xfe, 0x3a, 0xb0, 0xeb, 0x95, 0x54, 0xc5, 0xca, 0xfe, 0x3a, 0xb0, 0xeb, 0x95, 0x54,
0x9d, 0x46, 0x1a, 0x67, 0xce, 0xb9, 0xe5, 0xaa, 0x9d, 0x46, 0x1a, 0x67, 0xce, 0xb9, 0xe5, 0xaa,
@ -977,7 +992,7 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
}, },
}, },
{ {
0x000000ffULL, 0x000000ffull,
64, 64,
{ {
0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45,
@ -1055,7 +1070,8 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
}, { },
{
0x1c, 0x3b, 0x3a, 0x10, 0x2f, 0x77, 0x03, 0x86, 0x1c, 0x3b, 0x3a, 0x10, 0x2f, 0x77, 0x03, 0x86,
0xe4, 0x83, 0x6c, 0x99, 0xe3, 0x70, 0xcf, 0x9b, 0xe4, 0x83, 0x6c, 0x99, 0xe3, 0x70, 0xcf, 0x9b,
0xea, 0x00, 0x80, 0x3f, 0x5e, 0x48, 0x23, 0x57, 0xea, 0x00, 0x80, 0x3f, 0x5e, 0x48, 0x23, 0x57,
@ -1123,7 +1139,7 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
}, },
}, },
{ {
0x0000ffffULL, 0x0000ffffull,
64, 64,
{ {
0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45,
@ -1201,7 +1217,8 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
}, { },
{
0x77, 0xa3, 0x12, 0x51, 0x61, 0x8a, 0x15, 0xe6, 0x77, 0xa3, 0x12, 0x51, 0x61, 0x8a, 0x15, 0xe6,
0xb9, 0x2d, 0x1d, 0x66, 0xdf, 0xfe, 0x7b, 0x50, 0xb9, 0x2d, 0x1d, 0x66, 0xdf, 0xfe, 0x7b, 0x50,
0xb5, 0x0b, 0xad, 0x55, 0x23, 0x05, 0xba, 0x02, 0xb5, 0x0b, 0xad, 0x55, 0x23, 0x05, 0xba, 0x02,
@ -1269,7 +1286,7 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
}, },
}, },
{ {
0x00ffffffULL, 0x00ffffffull,
64, 64,
{ {
0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45,
@ -1347,7 +1364,8 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
}, { },
{
0xe3, 0x87, 0xaa, 0xa5, 0x8b, 0xa4, 0x83, 0xaf, 0xe3, 0x87, 0xaa, 0xa5, 0x8b, 0xa4, 0x83, 0xaf,
0xa7, 0xe8, 0xeb, 0x46, 0x97, 0x78, 0x31, 0x7e, 0xa7, 0xe8, 0xeb, 0x46, 0x97, 0x78, 0x31, 0x7e,
0xcf, 0x4c, 0xf5, 0x73, 0xaa, 0x9d, 0x4e, 0xac, 0xcf, 0x4c, 0xf5, 0x73, 0xaa, 0x9d, 0x4e, 0xac,
@ -1415,7 +1433,7 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
}, },
}, },
{ {
0xffffffffULL, 0xffffffffull,
64, 64,
{ {
0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45,
@ -1493,7 +1511,8 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
}, { },
{
0xbf, 0x53, 0xd2, 0xda, 0xde, 0x78, 0xe8, 0x22, 0xbf, 0x53, 0xd2, 0xda, 0xde, 0x78, 0xe8, 0x22,
0xa4, 0xd9, 0x49, 0xa9, 0xbc, 0x67, 0x66, 0xb0, 0xa4, 0xd9, 0x49, 0xa9, 0xbc, 0x67, 0x66, 0xb0,
0x1b, 0x06, 0xa8, 0xef, 0x70, 0xd2, 0x67, 0x48, 0x1b, 0x06, 0xa8, 0xef, 0x70, 0xd2, 0x67, 0x48,
@ -1561,7 +1580,7 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
}, },
}, },
{ {
0xffffffffffULL, 0xffffffffffull,
64, 64,
{ {
0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45,
@ -1639,7 +1658,8 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
}, { },
{
0x64, 0x49, 0x7e, 0x5a, 0x83, 0x1e, 0x4a, 0x93, 0x64, 0x49, 0x7e, 0x5a, 0x83, 0x1e, 0x4a, 0x93,
0x2c, 0x09, 0xbe, 0x3e, 0x53, 0x93, 0x37, 0x6d, 0x2c, 0x09, 0xbe, 0x3e, 0x53, 0x93, 0x37, 0x6d,
0xaa, 0x59, 0x95, 0x48, 0xb8, 0x16, 0x03, 0x1d, 0xaa, 0x59, 0x95, 0x48, 0xb8, 0x16, 0x03, 0x1d,
@ -1709,104 +1729,139 @@ struct aes_xts_tv aes_xts_test_vectors[] = {
}; };
#define N_VECTORS (sizeof(aes_xts_test_vectors) / sizeof(*aes_xts_test_vectors)) #define N_VECTORS (sizeof(aes_xts_test_vectors) / sizeof(*aes_xts_test_vectors))
u_int32_t deflate_global(u_int8_t *, u_int32_t, int, u_int8_t **); static int match(FAR unsigned char *a, FAR unsigned char *b, size_t len)
u_int32_t
deflate_global(u_int8_t *data, u_int32_t size, int comp, u_int8_t **out)
{
return 0;
}
void explicit_bzero(void *, size_t);
void
explicit_bzero(void *b, size_t len)
{
bzero(b, len);
}
static int
match(unsigned char *a, unsigned char *b, size_t len)
{ {
size_t i; size_t i;
if (memcmp(a, b, len) == 0) if (memcmp(a, b, len) == 0)
{
return (1); return (1);
}
warnx("decrypt/plaintext mismatch"); warnx("decrypt/plaintext mismatch");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{
printf("%2.2x", a[i]); printf("%2.2x", a[i]);
}
printf("\n"); printf("\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{
printf("%2.2x", b[i]); printf("%2.2x", b[i]);
}
printf("\n"); printf("\n");
return (0); return (0);
} }
static int static int syscrypt(FAR const unsigned char *key, size_t klen,
do_aes_xts(u_int8_t *key, int klen, u_int64_t seqno, u_int64_t seqno, FAR const unsigned char *in,
const u_int8_t *in, u_int8_t *out, size_t len, int do_encrypt) FAR unsigned char *out, size_t len, int encrypt)
{ {
u_int8_t block[AES_XTS_BLOCKSIZE]; struct session_op session;
struct aes_xts_ctx ctx; struct crypt_op cryp;
size_t i; int cryptodev_fd = -1;
if (aes_xts_setkey(&ctx, key, klen) < 0) if ((cryptodev_fd = open("/dev/crypto", O_RDWR, 0)) < 0)
return -1; {
warn("/dev/crypto");
aes_xts_reinit((caddr_t)&ctx, (u_int8_t *)&seqno); goto err;
for (i = 0; i < (len / AES_XTS_BLOCKSIZE); i++) {
bcopy(in, block, AES_XTS_BLOCKSIZE);
in += AES_XTS_BLOCKSIZE;
if (do_encrypt)
aes_xts_encrypt((caddr_t)&ctx, block);
else
aes_xts_decrypt((caddr_t)&ctx, block);
bcopy(block, out, AES_XTS_BLOCKSIZE);
out += AES_XTS_BLOCKSIZE;
} }
return 0; memset(&session, 0, sizeof(session));
session.cipher = CRYPTO_AES_XTS;
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) &seqno;
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);
} }
int /****************************************************************************
main(int argc, char **argv) * Public Functions
****************************************************************************/
int main(int argc, FAR char **argv)
{ {
struct aes_xts_tv *tv; struct aes_xts_tv *tv;
u_int8_t result[512]; u_int8_t result[512];
int fail = 0; int fail = 0;
size_t i; size_t i;
for (i = 0; i < N_VECTORS; i++) { for (i = 0; i < N_VECTORS; i++)
{
tv = &aes_xts_test_vectors[i]; tv = &aes_xts_test_vectors[i];
/* Encrypt test */ /* Encrypt test */
if (do_aes_xts(tv->key, tv->key_len, tv->seqno, tv->plaintext,
result, tv->text_len, 1) < 0) { if (syscrypt(tv->key, tv->key_len, tv->seqno, tv->plaintext,
result, tv->text_len, 1) < 0)
{
printf("FAIL encrypt test vector %zu\n", i); printf("FAIL encrypt test vector %zu\n", i);
fail++; fail++;
break; break;
} }
if (!match(result, tv->ciphertext, tv->text_len)) {
if (!match(result, tv->ciphertext, tv->text_len))
{
fail++; fail++;
break; break;
} }
printf("OK encrypt test vector %zu\n", i); printf("OK encrypt test vector %zu\n", i);
/* Decrypt test */ /* Decrypt test */
if (do_aes_xts(tv->key, tv->key_len, tv->seqno, tv->ciphertext,
result, tv->text_len, 0) < 0) { if (syscrypt(tv->key, tv->key_len, tv->seqno, tv->ciphertext,
result, tv->text_len, 0) < 0)
{
printf("FAIL decrypt test vector %zu\n", i); printf("FAIL decrypt test vector %zu\n", i);
fail++; fail++;
break; break;
} }
if (!match(result, tv->plaintext, tv->text_len)) {
if (!match(result, tv->plaintext, tv->text_len))
{
fail++; fail++;
break; break;
} }
printf("OK decrypt test vector %zu\n", i); printf("OK decrypt test vector %zu\n", i);
} }

View File

@ -1,9 +0,0 @@
digest = 0x9294727a3638bb1c13f48ef8158bfc9d
digest = 0x750c783e6ab0b503eaa86e310a5db738
digest = 0x56be34521d144c88dbb8c733f0e8b3f6
digest = 0x675b0b3a1b4ddf4e124872da6c2f632bfed957e9
digest = 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
digest = 0xd730594d167e35d5956fd8003d0db3d3f46dc7bb
digest = 0x492ce020fe2534a5789dc3848806c78f4f6711397f08e7e7a12ca5a4483c8aa6
digest = 0x5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843
digest = 0x7dda3cc169743a6484649f94f0eda0f9f2ff496a9733fb796ed5adb40a44c3c1

View File

@ -1,4 +1,5 @@
/*- /****************************************************************************
* apps/testing/crypto/hmac.c
* Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr> * Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
* *
* Permission to use, copy, modify, and distribute this software for any * 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 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ ****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <err.h>
#include <stdio.h> #include <stdio.h>
#include <fcntl.h>
#include <crypto/cryptodev.h>
#include <string.h>
#include <sys/ioctl.h>
#include <crypto/md5.h> #include <crypto/md5.h>
#include <crypto/sha1.h> #include <crypto/sha1.h>
#include <crypto/sha2.h> #include <crypto/sha2.h>
#include <crypto/hmac.h>
#include <string.h>
static void struct tb
print_hex(unsigned char *buf, int len) {
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
},
};
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;
}
static int match(unsigned char *a, unsigned char *b, size_t len)
{ {
int i; int i;
printf("digest = 0x"); if (memcmp(a, b, len) == 0)
return (0);
warnx("hmac mismatch");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
printf("%02x", buf[i]); {
printf("%02x", a[i]);
}
printf("\n"); printf("\n");
for (i = 0; i < len; i++)
{
printf("%02x", b[i]);
}
printf("\n");
return (1);
} }
int /****************************************************************************
main(void) * Public Functions
****************************************************************************/
int main(void)
{ {
HMAC_MD5_CTX md5; char output[32];
HMAC_SHA1_CTX sha1; int ret = 0;
HMAC_SHA256_CTX sha256; for (int i = 0; i < sizeof(testcase) / sizeof(struct tb); i++)
u_int8_t data[50], output[32]; {
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");
}
HMAC_MD5_Init(&md5, "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16); ret += match((unsigned char *)md5_result[i],
HMAC_MD5_Update(&md5, "Hi There", 8); (unsigned char *)output,
HMAC_MD5_Final(output, &md5); MD5_DIGEST_LENGTH);
print_hex(output, MD5_DIGEST_LENGTH); if (ret)
{
printf("match md5 failed\n");
}
else
{
printf("hmac md5 success\n");
}
}
HMAC_MD5_Init(&md5, "Jefe", 4); for (int i = 0; i < sizeof(testcase) / sizeof(struct tb); i++)
HMAC_MD5_Update(&md5, "what do ya want for nothing?", 28); {
HMAC_MD5_Final(output, &md5); ret = syshmac(CRYPTO_SHA1_HMAC, testcase[i].key,
print_hex(output, MD5_DIGEST_LENGTH); testcase[i].keylen,
testcase[i].data, testcase[i].datalen, output);
if (ret)
{
printf("syshamc sha1 failed\n");
}
HMAC_MD5_Init(&md5, "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 16); ret = match((unsigned char *)sha1_result[i],
memset(data, 0xDD, sizeof data); (unsigned char *)output,
HMAC_MD5_Update(&md5, data, sizeof data); SHA1_DIGEST_LENGTH);
HMAC_MD5_Final(output, &md5); if (ret)
print_hex(output, MD5_DIGEST_LENGTH); {
printf("match sha1 failed\n");
}
else
{
printf("hmac sha1 success\n");
}
}
HMAC_SHA1_Init(&sha1, "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16); for (int i = 0; i < sizeof(testcase) / sizeof(struct tb); i++)
HMAC_SHA1_Update(&sha1, "Hi There", 8); {
HMAC_SHA1_Final(output, &sha1); ret = syshmac(CRYPTO_SHA2_256_HMAC, testcase[i].key,
print_hex(output, SHA1_DIGEST_LENGTH); testcase[i].keylen,
testcase[i].data, testcase[i].datalen, output);
if (ret)
{
printf("syshamc sha256 failed\n");
}
HMAC_SHA1_Init(&sha1, "Jefe", 4); ret = match((unsigned char *)sha256_result[i],
HMAC_SHA1_Update(&sha1, "what do ya want for nothing?", 28); (unsigned char *)output,
HMAC_SHA1_Final(output, &sha1); SHA256_DIGEST_LENGTH);
print_hex(output, SHA1_DIGEST_LENGTH); if (ret)
{
HMAC_SHA1_Init(&sha1, "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 16); printf("match sha256 failed\n");
memset(data, 0xDD, sizeof data); }
HMAC_SHA1_Update(&sha1, data, sizeof data); else
HMAC_SHA1_Final(output, &sha1); {
print_hex(output, SHA1_DIGEST_LENGTH); printf("hmac sha256 success\n");
}
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 0;
} }
void
explicit_bzero(void *b, size_t len)
{
bzero(b, len);
}