|
| 1 | +/**************************************************************************** |
| 2 | + * apps/testing/drivers/crypto/pbkdf2.c |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + * contributor license agreements. See the NOTICE file distributed with |
| 8 | + * this work for additional information regarding copyright ownership. The |
| 9 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance with the |
| 11 | + * License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | + * License for the specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + ****************************************************************************/ |
| 21 | + |
| 22 | +/**************************************************************************** |
| 23 | + * Included Files |
| 24 | + ****************************************************************************/ |
| 25 | + |
| 26 | +#include <err.h> |
| 27 | +#include <stdio.h> |
| 28 | +#include <fcntl.h> |
| 29 | +#include <string.h> |
| 30 | +#include <unistd.h> |
| 31 | +#include <sys/ioctl.h> |
| 32 | +#include <sys/param.h> |
| 33 | +#include <crypto/cryptodev.h> |
| 34 | + |
| 35 | +struct tb |
| 36 | +{ |
| 37 | + FAR char *key; |
| 38 | + int keylen; |
| 39 | + FAR char *data; |
| 40 | + int datalen; |
| 41 | + int iterations; |
| 42 | + int dklen; |
| 43 | +} |
| 44 | +pbkdf2_testcases[] = |
| 45 | +{ |
| 46 | + { |
| 47 | + "password", |
| 48 | + 8, |
| 49 | + "salt", |
| 50 | + 4, |
| 51 | + 1, |
| 52 | + 20, |
| 53 | + }, |
| 54 | + { |
| 55 | + "password", |
| 56 | + 8, |
| 57 | + "salt", |
| 58 | + 4, |
| 59 | + 2, |
| 60 | + 20, |
| 61 | + }, |
| 62 | + { |
| 63 | + "password", |
| 64 | + 8, |
| 65 | + "salt", |
| 66 | + 4, |
| 67 | + 4096, |
| 68 | + 20, |
| 69 | + }, |
| 70 | + { |
| 71 | + "password", |
| 72 | + 8, |
| 73 | + "salt", |
| 74 | + 4, |
| 75 | + 16777216, |
| 76 | + 20, |
| 77 | + }, |
| 78 | + { |
| 79 | + "passwordPASSWORDpassword", |
| 80 | + 24, |
| 81 | + "saltSALTsaltSALTsaltSALTsaltSALTsalt", |
| 82 | + 36, |
| 83 | + 4096, |
| 84 | + 25, |
| 85 | + }, |
| 86 | + { |
| 87 | + "pass\0word", |
| 88 | + 9, |
| 89 | + "sa\0lt", |
| 90 | + 5, |
| 91 | + 4096, |
| 92 | + 16, |
| 93 | + }, |
| 94 | +}; |
| 95 | + |
| 96 | +FAR char *pbkdf2_sha1_result[] = |
| 97 | +{ |
| 98 | + "\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9\xb5\x24\xaf\x60\x12\x06" |
| 99 | + "\x2f\xe0\x37\xa6", |
| 100 | + "\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e\xd9\x2a\xce\x1d\x41\xf0" |
| 101 | + "\xd8\xde\x89\x57", |
| 102 | + "\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad\x49\xd9\x26\xf7\x21\xd0" |
| 103 | + "\x65\xa4\x29\xc1", |
| 104 | + "\xee\xfe\x3d\x61\xcd\x4d\xa4\xe4\xe9\x94\x5b\x3d\x6b\xa2\x15\x8c" |
| 105 | + "\x26\x34\xe9\x84", |
| 106 | + "\x3d\x2e\xec\x4f\xe4\x1c\x84\x9b\x80\xc8\xd8\x36\x62\xc0\xe4\x4a" |
| 107 | + "\x8b\x29\x1a\x96\x4c\xf2\xf0\x70\x38", |
| 108 | + "\x56\xfa\x6a\xa7\x55\x48\x09\x9d\xcc\x37\xd7\xf0\x34\x25\xe0\xc3", |
| 109 | +}; |
| 110 | + |
| 111 | +FAR char *pbkdf2_sha256_result[] = |
| 112 | +{ |
| 113 | + "\x12\x0f\xb6\xcf\xfc\xf8\xb3\x2c\x43\xe7\x22\x52\x56\xc4\xf8\x37" |
| 114 | + "\xa8\x65\x48\xc9", |
| 115 | + "\xae\x4d\x0c\x95\xaf\x6b\x46\xd3\x2d\x0a\xdf\xf9\x28\xf0\x6d\xd0" |
| 116 | + "\x2a\x30\x3f\x8e", |
| 117 | + "\xc5\xe4\x78\xd5\x92\x88\xc8\x41\xaa\x53\x0d\xb6\x84\x5c\x4c\x8d" |
| 118 | + "\x96\x28\x93\xa0", |
| 119 | + "\xcf\x81\xc6\x6f\xe8\xcf\xc0\x4d\x1f\x31\xec\xb6\x5d\xab\x40\x89" |
| 120 | + "\xf7\xf1\x79\xe8", |
| 121 | + "\x34\x8c\x89\xdb\xcb\xd3\x2b\x2f\x32\xd8\x14\xb8\x11\x6e\x84\xcf" |
| 122 | + "\x2b\x17\x34\x7e\xbc\x18\x00\x18\x1c", |
| 123 | + "\x89\xb6\x9d\x05\x16\xf8\x29\x89\x3c\x69\x62\x26\x65\x0a\x86\x87", |
| 124 | +}; |
| 125 | + |
| 126 | +int syspbkdf2(int mac, FAR const char *key, size_t keylen, |
| 127 | + FAR const char *s, size_t len, int iterations, |
| 128 | + size_t dklen, FAR char *out) |
| 129 | +{ |
| 130 | + struct session_op session; |
| 131 | + struct crypt_op cryp; |
| 132 | + int cryptodev_fd = -1; |
| 133 | + int fd = -1; |
| 134 | + |
| 135 | + if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) |
| 136 | + { |
| 137 | + warn("/dev/crypto"); |
| 138 | + goto err; |
| 139 | + } |
| 140 | + |
| 141 | + if (ioctl(fd, CRIOGET, &cryptodev_fd) == -1) |
| 142 | + { |
| 143 | + warn("CRIOGET"); |
| 144 | + goto err; |
| 145 | + } |
| 146 | + |
| 147 | + memset(&session, 0, sizeof(session)); |
| 148 | + session.cipher = 0; |
| 149 | + session.mac = mac; |
| 150 | + session.mackey = (caddr_t)key; |
| 151 | + session.mackeylen = keylen; |
| 152 | + if (ioctl(cryptodev_fd, CIOCGSESSION, &session) == -1) |
| 153 | + { |
| 154 | + warn("CIOCGSESSION"); |
| 155 | + goto err; |
| 156 | + } |
| 157 | + memset(&cryp, 0, sizeof(cryp)); |
| 158 | + cryp.ses = session.ses; |
| 159 | + cryp.flags = 0; |
| 160 | + cryp.src = (caddr_t)s; |
| 161 | + cryp.len = len; |
| 162 | + cryp.dst = 0; |
| 163 | + cryp.mac = (caddr_t) out; |
| 164 | + cryp.iv = 0; |
| 165 | + cryp.iterations = iterations; |
| 166 | + cryp.olen = dklen; |
| 167 | + |
| 168 | + if (ioctl(cryptodev_fd, CIOCCRYPT, &cryp) == -1) |
| 169 | + { |
| 170 | + warn("CIOCCRYPT"); |
| 171 | + goto err; |
| 172 | + } |
| 173 | + |
| 174 | + close(cryptodev_fd); |
| 175 | + close(fd); |
| 176 | + |
| 177 | + return 0; |
| 178 | +err: |
| 179 | + if (cryptodev_fd != -1) |
| 180 | + { |
| 181 | + close(cryptodev_fd); |
| 182 | + } |
| 183 | + |
| 184 | + if (fd != -1) |
| 185 | + { |
| 186 | + close(fd); |
| 187 | + } |
| 188 | + |
| 189 | + return 1; |
| 190 | +} |
| 191 | + |
| 192 | +static int match(unsigned char *a, unsigned char *b, size_t len) |
| 193 | +{ |
| 194 | + int i; |
| 195 | + |
| 196 | + if (memcmp(a, b, len) == 0) |
| 197 | + return (0); |
| 198 | + |
| 199 | + warnx("pbkdf2 mismatch\n"); |
| 200 | + |
| 201 | + for (i = 0; i < len; i++) |
| 202 | + { |
| 203 | + printf("%02x", a[i]); |
| 204 | + } |
| 205 | + |
| 206 | + printf("\n"); |
| 207 | + for (i = 0; i < len; i++) |
| 208 | + { |
| 209 | + printf("%02x", b[i]); |
| 210 | + } |
| 211 | + |
| 212 | + printf("\n"); |
| 213 | + |
| 214 | + return (1); |
| 215 | +} |
| 216 | + |
| 217 | +/**************************************************************************** |
| 218 | + * Public Functions |
| 219 | + ****************************************************************************/ |
| 220 | + |
| 221 | +int main(void) |
| 222 | +{ |
| 223 | + char output[40]; |
| 224 | + int ret = 0; |
| 225 | + for (int i = 0; i < 6; i++) |
| 226 | + { |
| 227 | + ret = syspbkdf2(CRYPTO_PBKDF2_HMAC_SHA1, pbkdf2_testcases[i].key, |
| 228 | + pbkdf2_testcases[i].keylen, |
| 229 | + pbkdf2_testcases[i].data, |
| 230 | + pbkdf2_testcases[i].datalen, |
| 231 | + pbkdf2_testcases[i].iterations, |
| 232 | + pbkdf2_testcases[i].dklen, output); |
| 233 | + if (ret) |
| 234 | + { |
| 235 | + printf("PBKDF2 SHA1 failed\n"); |
| 236 | + } |
| 237 | + |
| 238 | + ret += match((unsigned char *)pbkdf2_sha1_result[i], |
| 239 | + (unsigned char *)output, |
| 240 | + pbkdf2_testcases[i].dklen); |
| 241 | + if (ret) |
| 242 | + { |
| 243 | + printf("match PBKDF2 SHA1 failed\n"); |
| 244 | + } |
| 245 | + else |
| 246 | + { |
| 247 | + printf("hmac PBKDF2 SHA1 success\n"); |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + for (int i = 0; i < 6; i++) |
| 252 | + { |
| 253 | + ret = syspbkdf2(CRYPTO_PBKDF2_HMAC_SHA256, pbkdf2_testcases[i].key, |
| 254 | + pbkdf2_testcases[i].keylen, |
| 255 | + pbkdf2_testcases[i].data, |
| 256 | + pbkdf2_testcases[i].datalen, |
| 257 | + pbkdf2_testcases[i].iterations, |
| 258 | + pbkdf2_testcases[i].dklen, output); |
| 259 | + if (ret) |
| 260 | + { |
| 261 | + printf("PBKDF2 SHA256 failed\n"); |
| 262 | + } |
| 263 | + |
| 264 | + ret += match((unsigned char *)pbkdf2_sha256_result[i], |
| 265 | + (unsigned char *)output, |
| 266 | + pbkdf2_testcases[i].dklen); |
| 267 | + if (ret) |
| 268 | + { |
| 269 | + printf("match PBKDF2 SHA256 failed\n"); |
| 270 | + } |
| 271 | + else |
| 272 | + { |
| 273 | + printf("hmac PBKDF2 SHA256 success\n"); |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + return 0; |
| 278 | +} |
0 commit comments