|
| 1 | +/**************************************************************************** |
| 2 | + * apps/testing/crypto/aescmac.c |
| 3 | + * |
| 4 | + * Permission to use, copy, modify, and distribute this software for any |
| 5 | + * purpose with or without fee is hereby granted, provided that the above |
| 6 | + * copyright notice and this permission notice appear in all copies. |
| 7 | + * |
| 8 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | + ****************************************************************************/ |
| 16 | + |
| 17 | +/**************************************************************************** |
| 18 | + * Included Files |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +#include <err.h> |
| 22 | +#include <stdio.h> |
| 23 | +#include <fcntl.h> |
| 24 | +#include <stdlib.h> |
| 25 | +#include <string.h> |
| 26 | +#include <unistd.h> |
| 27 | +#include <sys/ioctl.h> |
| 28 | +#include <sys/param.h> |
| 29 | +#include <crypto/cryptodev.h> |
| 30 | + |
| 31 | +/* AES-CMAC test vector from |
| 32 | + * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/AES_CMAC.pdf |
| 33 | + */ |
| 34 | + |
| 35 | +static const unsigned char g_test_message[] = |
| 36 | +{ |
| 37 | + 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, |
| 38 | + 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, |
| 39 | + 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, |
| 40 | + 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, |
| 41 | + 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, |
| 42 | + 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, |
| 43 | + 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, |
| 44 | + 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 |
| 45 | +}; |
| 46 | + |
| 47 | +static const unsigned char g_aes_128_key[16] = |
| 48 | +{ |
| 49 | + 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, |
| 50 | + 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c |
| 51 | +}; |
| 52 | + |
| 53 | +static const unsigned int g_message_lengths[4] = |
| 54 | +{ |
| 55 | + 0, |
| 56 | + 16, |
| 57 | + 20, |
| 58 | + 64 |
| 59 | +}; |
| 60 | + |
| 61 | +static const unsigned char g_aes_128_expected_result[4][16] = |
| 62 | +{ |
| 63 | + { |
| 64 | + /* Example #1 */ |
| 65 | + |
| 66 | + 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28, |
| 67 | + 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46 |
| 68 | + }, |
| 69 | + { |
| 70 | + /* Example #2 */ |
| 71 | + |
| 72 | + 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44, |
| 73 | + 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c |
| 74 | + }, |
| 75 | + { |
| 76 | + /* Example #3 */ |
| 77 | + |
| 78 | + 0x7d, 0x85, 0x44, 0x9e, 0xa6, 0xea, 0x19, 0xc8, |
| 79 | + 0x23, 0xa7, 0xbf, 0x78, 0x83, 0x7d, 0xfa, 0xde |
| 80 | + }, |
| 81 | + { |
| 82 | + /* Example #4 */ |
| 83 | + |
| 84 | + 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92, |
| 85 | + 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +static int syscrypt(FAR const unsigned char *key, size_t klen, |
| 90 | + FAR const unsigned char *message, size_t len, |
| 91 | + FAR unsigned char *output, size_t outlen) |
| 92 | +{ |
| 93 | + struct session_op session; |
| 94 | + struct crypt_op cryp; |
| 95 | + int cryptodev_fd = -1; |
| 96 | + int fd = -1; |
| 97 | + |
| 98 | + if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) |
| 99 | + { |
| 100 | + perror("/dev/crypto"); |
| 101 | + goto err; |
| 102 | + } |
| 103 | + |
| 104 | + if (ioctl(fd, CRIOGET, &cryptodev_fd) == -1) |
| 105 | + { |
| 106 | + perror("CRIOGET"); |
| 107 | + goto err; |
| 108 | + } |
| 109 | + |
| 110 | + memset(&session, 0, sizeof(session)); |
| 111 | + session.cipher = CRYPTO_AES_CMAC; |
| 112 | + session.key = (caddr_t)key; |
| 113 | + session.keylen = klen; |
| 114 | + session.mac = CRYPTO_AES_128_CMAC; |
| 115 | + session.mackey = (caddr_t)key; |
| 116 | + session.mackeylen = klen; |
| 117 | + |
| 118 | + if (ioctl(cryptodev_fd, CIOCGSESSION, &session) == -1) |
| 119 | + { |
| 120 | + perror("CIOCGSESSION"); |
| 121 | + goto err; |
| 122 | + } |
| 123 | + |
| 124 | + memset(&cryp, 0, sizeof(cryp)); |
| 125 | + cryp.ses = session.ses; |
| 126 | + cryp.op = COP_ENCRYPT; |
| 127 | + cryp.flags = 0; |
| 128 | + cryp.len = len; |
| 129 | + cryp.src = (caddr_t)message; |
| 130 | + cryp.mac = (caddr_t)output; |
| 131 | + |
| 132 | + if (ioctl(cryptodev_fd, CIOCCRYPT, &cryp) == -1) |
| 133 | + { |
| 134 | + perror("CIOCCRYPT"); |
| 135 | + goto err; |
| 136 | + } |
| 137 | + |
| 138 | + if (ioctl(cryptodev_fd, CIOCFSESSION, &session.ses) == -1) |
| 139 | + { |
| 140 | + perror("CIOCFSESSION"); |
| 141 | + goto err; |
| 142 | + } |
| 143 | + |
| 144 | + close(cryptodev_fd); |
| 145 | + close(fd); |
| 146 | + return 0; |
| 147 | + |
| 148 | +err: |
| 149 | + if (cryptodev_fd != -1) |
| 150 | + { |
| 151 | + close(cryptodev_fd); |
| 152 | + } |
| 153 | + |
| 154 | + if (fd != -1) |
| 155 | + { |
| 156 | + close(fd); |
| 157 | + } |
| 158 | + |
| 159 | + return -1; |
| 160 | +} |
| 161 | + |
| 162 | +static int match(FAR const unsigned char *a, FAR const unsigned char *b, |
| 163 | + size_t len) |
| 164 | +{ |
| 165 | + int i; |
| 166 | + |
| 167 | + if (memcmp(a, b, len) == 0) |
| 168 | + { |
| 169 | + return 0; |
| 170 | + } |
| 171 | + |
| 172 | + warnx("ciphertext mismatch"); |
| 173 | + |
| 174 | + for (i = 0; i < len; i++) |
| 175 | + { |
| 176 | + printf("%2.2x", a[i]); |
| 177 | + } |
| 178 | + |
| 179 | + printf("\n"); |
| 180 | + for (i = 0; i < len; i++) |
| 181 | + { |
| 182 | + printf("%2.2x", b[i]); |
| 183 | + } |
| 184 | + |
| 185 | + printf("\n"); |
| 186 | + |
| 187 | + return -1; |
| 188 | +} |
| 189 | + |
| 190 | +/**************************************************************************** |
| 191 | + * Public Functions |
| 192 | + ****************************************************************************/ |
| 193 | + |
| 194 | +int main(int argc, FAR char *argv[]) |
| 195 | +{ |
| 196 | + int ret; |
| 197 | + unsigned char out[64]; |
| 198 | + |
| 199 | + for (int i = 0; i < 4; i++) |
| 200 | + { |
| 201 | + ret = syscrypt(g_aes_128_key, sizeof(g_aes_128_key), g_test_message, |
| 202 | + g_message_lengths[i], out, sizeof(out)); |
| 203 | + if (ret) |
| 204 | + { |
| 205 | + printf("aes cmac failed in testcase:%d\n", i); |
| 206 | + return -1; |
| 207 | + } |
| 208 | + |
| 209 | + ret = match(out, g_aes_128_expected_result[i], 16); |
| 210 | + if (ret) |
| 211 | + { |
| 212 | + printf("aes cmac failed in testcase:%d\n", i); |
| 213 | + return -1; |
| 214 | + } |
| 215 | + |
| 216 | + printf("aescbc test %d ok\n", i); |
| 217 | + } |
| 218 | + |
| 219 | + return 0; |
| 220 | +} |
0 commit comments