|
| 1 | +/**************************************************************************** |
| 2 | + * apps/testing/crypto/aescbc.c |
| 3 | + * $OpenBSD: aesctr.c,v 1.1 2005/05/25 05:47:53 markus Exp $ |
| 4 | + * |
| 5 | + * Copyright (c) 2005 Markus Friedl <markus@openbsd.org> |
| 6 | + * |
| 7 | + * Permission to use, copy, modify, and distribute this software for any |
| 8 | + * purpose with or without fee is hereby granted, provided that the above |
| 9 | + * copyright notice and this permission notice appear in all copies. |
| 10 | + * |
| 11 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 12 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 13 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 14 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 15 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 16 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 17 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 | + ****************************************************************************/ |
| 19 | + |
| 20 | +/**************************************************************************** |
| 21 | + * Included Files |
| 22 | + ****************************************************************************/ |
| 23 | + |
| 24 | +#include <err.h> |
| 25 | +#include <stdio.h> |
| 26 | +#include <fcntl.h> |
| 27 | +#include <crypto/cryptodev.h> |
| 28 | +#include <string.h> |
| 29 | +#include <sys/ioctl.h> |
| 30 | +#include <crypto/md5.h> |
| 31 | +#include <crypto/sha1.h> |
| 32 | +#include <crypto/sha2.h> |
| 33 | + |
| 34 | +/* aescbc test case is come form |
| 35 | + * https://www.rfc-editor.org/rfc/rfc3602 |
| 36 | + */ |
| 37 | + |
| 38 | +struct tb |
| 39 | +{ |
| 40 | + FAR const char *key; |
| 41 | + FAR const char *iv; |
| 42 | + FAR const char *plain; |
| 43 | + FAR const char *cipher; |
| 44 | + uint32_t len; |
| 45 | +} |
| 46 | +static testcase [] = |
| 47 | +{ |
| 48 | + { |
| 49 | + "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06", |
| 50 | + "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30\xb4\x22\xda\x80\x2c\x9f\xac\x41", |
| 51 | + "Single block msg", |
| 52 | + "\xe3\x53\x77\x9c\x10\x79\xae\xb8\x27\x08\x94\x2d\xbe\x77\x18\x1a", |
| 53 | + 16, |
| 54 | + }, |
| 55 | + { |
| 56 | + "\xc2\x86\x69\x6d\x88\x7c\x9a\xa0\x61\x1b\xbb\x3e\x20\x25\xa4\x5a", |
| 57 | + "\x56\x2e\x17\x99\x6d\x09\x3d\x28\xdd\xb3\xba\x69\x5a\x2e\x6f\x58", |
| 58 | + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" |
| 59 | + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", |
| 60 | + "\xd2\x96\xcd\x94\xc2\xcc\xcf\x8a\x3a\x86\x30\x28\xb5\xe1\xdc\x0a" |
| 61 | + "\x75\x86\x60\x2d\x25\x3c\xff\xf9\x1b\x82\x66\xbe\xa6\xd6\x1a\xb1", |
| 62 | + 32 |
| 63 | + }, |
| 64 | + { |
| 65 | + "\x6c\x3e\xa0\x47\x76\x30\xce\x21\xa2\xce\x33\x4a\xa7\x46\xc2\xcd", |
| 66 | + "\xc7\x82\xdc\x4c\x09\x8c\x66\xcb\xd9\xcd\x27\xd8\x25\x68\x2c\x81", |
| 67 | + "This is a 48-byte message (exactly 3 AES blocks)", |
| 68 | + "\xd0\xa0\x2b\x38\x36\x45\x17\x53\xd4\x93\x66\x5d\x33\xf0\xe8\x86" |
| 69 | + "\x2d\xea\x54\xcd\xb2\x93\xab\xc7\x50\x69\x39\x27\x67\x72\xf8\xd5" |
| 70 | + "\x02\x1c\x19\x21\x6b\xad\x52\x5c\x85\x79\x69\x5d\x83\xba\x26\x84", |
| 71 | + 48 |
| 72 | + }, |
| 73 | + { |
| 74 | + "\x56\xe4\x7a\x38\xc5\x59\x89\x74\xbc\x46\x90\x3d\xba\x29\x03\x49", |
| 75 | + "\x8c\xe8\x2e\xef\xbe\xa0\xda\x3c\x44\x69\x9e\xd7\xdb\x51\xb7\xd9", |
| 76 | + "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" |
| 77 | + "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" |
| 78 | + "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" |
| 79 | + "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf", |
| 80 | + "\xc3\x0e\x32\xff\xed\xc0\x77\x4e\x6a\xff\x6a\xf0\x86\x9f\x71\xaa" |
| 81 | + "\x0f\x3a\xf0\x7a\x9a\x31\xa9\xc6\x84\xdb\x20\x7e\xb0\xef\x8e\x4e" |
| 82 | + "\x35\x90\x7a\xa6\x32\xc3\xff\xdf\x86\x8b\xb7\xb2\x9d\x3d\x46\xad" |
| 83 | + "\x83\xce\x9f\x9a\x10\x2e\xe9\x9d\x49\xa5\x3e\x87\xf4\xc3\xda\x55", |
| 84 | + 64 |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +static int syscrypt(FAR const char *key, size_t klen, |
| 89 | + FAR const char *iv, FAR const char *in, |
| 90 | + FAR unsigned char *out, size_t len, int encrypt) |
| 91 | +{ |
| 92 | + struct session_op session; |
| 93 | + struct crypt_op cryp; |
| 94 | + int cryptodev_fd = -1; |
| 95 | + int fd = -1; |
| 96 | + |
| 97 | + if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) |
| 98 | + { |
| 99 | + warn("/dev/crypto"); |
| 100 | + goto err; |
| 101 | + } |
| 102 | + |
| 103 | + if (ioctl(fd, CRIOGET, &cryptodev_fd) == -1) |
| 104 | + { |
| 105 | + warn("CRIOGET"); |
| 106 | + goto err; |
| 107 | + } |
| 108 | + |
| 109 | + memset(&session, 0, sizeof(session)); |
| 110 | + session.cipher = CRYPTO_AES_CBC; |
| 111 | + session.key = (caddr_t) key; |
| 112 | + session.keylen = klen; |
| 113 | + if (ioctl(cryptodev_fd, CIOCGSESSION, &session) == -1) |
| 114 | + { |
| 115 | + warn("CIOCGSESSION"); |
| 116 | + goto err; |
| 117 | + } |
| 118 | + |
| 119 | + memset(&cryp, 0, sizeof(cryp)); |
| 120 | + cryp.ses = session.ses; |
| 121 | + cryp.op = encrypt ? COP_ENCRYPT : COP_DECRYPT; |
| 122 | + cryp.flags = 0; |
| 123 | + cryp.len = len; |
| 124 | + cryp.src = (caddr_t) in; |
| 125 | + cryp.dst = (caddr_t) out; |
| 126 | + cryp.iv = (caddr_t) iv; |
| 127 | + cryp.mac = 0; |
| 128 | + if (ioctl(cryptodev_fd, CIOCCRYPT, &cryp) == -1) |
| 129 | + { |
| 130 | + warn("CIOCCRYPT"); |
| 131 | + goto err; |
| 132 | + } |
| 133 | + |
| 134 | + if (ioctl(cryptodev_fd, CIOCFSESSION, &session.ses) == -1) |
| 135 | + { |
| 136 | + warn("CIOCFSESSION"); |
| 137 | + goto err; |
| 138 | + } |
| 139 | + |
| 140 | + close(cryptodev_fd); |
| 141 | + close(fd); |
| 142 | + return (0); |
| 143 | + |
| 144 | +err: |
| 145 | + if (cryptodev_fd != -1) |
| 146 | + { |
| 147 | + close(cryptodev_fd); |
| 148 | + } |
| 149 | + |
| 150 | + if (fd != -1) |
| 151 | + { |
| 152 | + close(fd); |
| 153 | + } |
| 154 | + |
| 155 | + return (-1); |
| 156 | +} |
| 157 | + |
| 158 | +static int match(FAR unsigned char *a, FAR unsigned char *b, size_t len) |
| 159 | +{ |
| 160 | + int i; |
| 161 | + |
| 162 | + if (memcmp(a, b, len) == 0) |
| 163 | + { |
| 164 | + return 0; |
| 165 | + } |
| 166 | + |
| 167 | + warnx("ciphertext mismatch"); |
| 168 | + |
| 169 | + for (i = 0; i < len; i++) |
| 170 | + { |
| 171 | + printf("%2.2x", a[i]); |
| 172 | + } |
| 173 | + |
| 174 | + printf("\n"); |
| 175 | + for (i = 0; i < len; i++) |
| 176 | + { |
| 177 | + printf("%2.2x", b[i]); |
| 178 | + } |
| 179 | + |
| 180 | + printf("\n"); |
| 181 | + |
| 182 | + return -1; |
| 183 | +} |
| 184 | + |
| 185 | +/**************************************************************************** |
| 186 | + * Public Functions |
| 187 | + ****************************************************************************/ |
| 188 | + |
| 189 | +int main(int argc, char *argv[]) |
| 190 | +{ |
| 191 | + int ret; |
| 192 | + unsigned char out[64]; |
| 193 | + |
| 194 | + for (int i = 0; i < sizeof(testcase) / sizeof(struct tb); i++) |
| 195 | + { |
| 196 | + ret = syscrypt(testcase[i].key, 16, testcase[i].iv, testcase[i].plain, |
| 197 | + out, testcase[i].len, 1); |
| 198 | + if (ret) |
| 199 | + { |
| 200 | + printf("aescbc encrypt field in testcase:%d\n", i); |
| 201 | + return -1; |
| 202 | + } |
| 203 | + |
| 204 | + ret = match(out, (FAR unsigned char *)testcase[i].cipher, |
| 205 | + testcase[i].len); |
| 206 | + if (ret) |
| 207 | + { |
| 208 | + printf("aescbc encrypt field in testcase:%d\n", i); |
| 209 | + return -1; |
| 210 | + } |
| 211 | + |
| 212 | + ret = syscrypt(testcase[i].key, 16, testcase[i].iv, testcase[i].cipher, |
| 213 | + out, testcase[i].len, 0); |
| 214 | + if (ret) |
| 215 | + { |
| 216 | + printf("aescbc decrypt field in testcase:%d\n", i); |
| 217 | + return -1; |
| 218 | + } |
| 219 | + |
| 220 | + ret = match(out, (FAR unsigned char *)testcase[i].plain, |
| 221 | + testcase[i].len); |
| 222 | + if (ret) |
| 223 | + { |
| 224 | + printf("aescbc decrypt field in testcase:%d\n", i); |
| 225 | + return -1; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + printf("aescbc test ok\n"); |
| 230 | + return 0; |
| 231 | +} |
0 commit comments