|
| 1 | +/**************************************************************************** |
| 2 | + * apps/testing/crypto/ecdsa.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 <fcntl.h> |
| 22 | +#include <stdio.h> |
| 23 | +#include <stdlib.h> |
| 24 | +#include <string.h> |
| 25 | +#include <unistd.h> |
| 26 | +#include <sys/ioctl.h> |
| 27 | +#include <sys/param.h> |
| 28 | +#include <crypto/cryptodev.h> |
| 29 | + |
| 30 | +/**************************************************************************** |
| 31 | + * Pre-processor Definitions |
| 32 | + ****************************************************************************/ |
| 33 | + |
| 34 | +#define SECP256R1_KEYLEN 32 |
| 35 | + |
| 36 | +/**************************************************************************** |
| 37 | + * Private Data |
| 38 | + ****************************************************************************/ |
| 39 | + |
| 40 | +static unsigned char sha256_message[32] = |
| 41 | +{ |
| 42 | + 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, |
| 43 | + 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, |
| 44 | + 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, |
| 45 | + 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad |
| 46 | +}; |
| 47 | + |
| 48 | +/**************************************************************************** |
| 49 | + * Private Functions |
| 50 | + ****************************************************************************/ |
| 51 | + |
| 52 | +static int ecdsa_sign(int op, FAR unsigned char *r, size_t rlen, |
| 53 | + FAR unsigned char *s, size_t slen, |
| 54 | + FAR unsigned char *d, size_t dlen, |
| 55 | + FAR const unsigned char *buf, size_t buflen) |
| 56 | +{ |
| 57 | + struct crypt_kop cryptk; |
| 58 | + int cryptodev_fd = -1; |
| 59 | + int fd = -1; |
| 60 | + |
| 61 | + if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) |
| 62 | + { |
| 63 | + perror("/dev/crypto"); |
| 64 | + goto err; |
| 65 | + } |
| 66 | + |
| 67 | + if (ioctl(fd, CRIOGET, &cryptodev_fd) == -1) |
| 68 | + { |
| 69 | + perror("CRIOGET"); |
| 70 | + goto err; |
| 71 | + } |
| 72 | + |
| 73 | + memset(&cryptk, 0, sizeof(cryptk)); |
| 74 | + cryptk.crk_op = op; |
| 75 | + cryptk.crk_iparams = 2; |
| 76 | + cryptk.crk_oparams = 2; |
| 77 | + cryptk.crk_param[0].crp_p = (caddr_t)d; |
| 78 | + cryptk.crk_param[0].crp_nbits = dlen * 8; |
| 79 | + cryptk.crk_param[1].crp_p = (caddr_t)buf; |
| 80 | + cryptk.crk_param[1].crp_nbits = buflen * 8; |
| 81 | + cryptk.crk_param[2].crp_p = (caddr_t)r; |
| 82 | + cryptk.crk_param[2].crp_nbits = rlen * 8; |
| 83 | + cryptk.crk_param[3].crp_p = (caddr_t)s; |
| 84 | + cryptk.crk_param[3].crp_nbits = slen * 8; |
| 85 | + |
| 86 | + if (ioctl(cryptodev_fd, CIOCKEY, &cryptk) == -1) |
| 87 | + { |
| 88 | + perror("CIOCKEY"); |
| 89 | + goto err; |
| 90 | + } |
| 91 | + |
| 92 | + close(cryptodev_fd); |
| 93 | + close(fd); |
| 94 | + return cryptk.crk_status; |
| 95 | + |
| 96 | +err: |
| 97 | + if (cryptodev_fd != -1) |
| 98 | + { |
| 99 | + close(cryptodev_fd); |
| 100 | + } |
| 101 | + |
| 102 | + if (fd != -1) |
| 103 | + { |
| 104 | + close(fd); |
| 105 | + } |
| 106 | + |
| 107 | + return -1; |
| 108 | +} |
| 109 | + |
| 110 | +static int ecdsa_verify(int op, FAR const unsigned char *qx, size_t qxlen, |
| 111 | + FAR const unsigned char *qy, size_t qylen, |
| 112 | + FAR const unsigned char *qz, size_t qzlen, |
| 113 | + FAR const unsigned char *r, size_t rlen, |
| 114 | + FAR const unsigned char *s, size_t slen, |
| 115 | + FAR const unsigned char *buf, size_t buflen) |
| 116 | +{ |
| 117 | + struct crypt_kop cryptk; |
| 118 | + int cryptodev_fd = -1; |
| 119 | + int fd = -1; |
| 120 | + |
| 121 | + if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) |
| 122 | + { |
| 123 | + perror("/dev/crypto"); |
| 124 | + goto err; |
| 125 | + } |
| 126 | + |
| 127 | + if (ioctl(fd, CRIOGET, &cryptodev_fd) == -1) |
| 128 | + { |
| 129 | + perror("CRIOGET"); |
| 130 | + goto err; |
| 131 | + } |
| 132 | + |
| 133 | + memset(&cryptk, 0, sizeof(cryptk)); |
| 134 | + cryptk.crk_op = op; |
| 135 | + cryptk.crk_iparams = 6; |
| 136 | + cryptk.crk_oparams = 0; |
| 137 | + cryptk.crk_param[0].crp_p = (caddr_t)qx; |
| 138 | + cryptk.crk_param[0].crp_nbits = qxlen * 8; |
| 139 | + cryptk.crk_param[1].crp_p = (caddr_t)qy; |
| 140 | + cryptk.crk_param[1].crp_nbits = qylen * 8; |
| 141 | + cryptk.crk_param[2].crp_p = (caddr_t)qz; |
| 142 | + cryptk.crk_param[2].crp_nbits = qzlen * 8; |
| 143 | + cryptk.crk_param[3].crp_p = (caddr_t)r; |
| 144 | + cryptk.crk_param[3].crp_nbits = rlen * 8; |
| 145 | + cryptk.crk_param[4].crp_p = (caddr_t)s; |
| 146 | + cryptk.crk_param[4].crp_nbits = slen * 8; |
| 147 | + cryptk.crk_param[5].crp_p = (caddr_t)buf; |
| 148 | + cryptk.crk_param[5].crp_nbits = buflen * 8; |
| 149 | + |
| 150 | + if (ioctl(cryptodev_fd, CIOCKEY, &cryptk) == -1) |
| 151 | + { |
| 152 | + perror("CIOCKEY"); |
| 153 | + goto err; |
| 154 | + } |
| 155 | + |
| 156 | + close(cryptodev_fd); |
| 157 | + close(fd); |
| 158 | + return cryptk.crk_status; |
| 159 | + |
| 160 | +err: |
| 161 | + if (cryptodev_fd != -1) |
| 162 | + { |
| 163 | + close(cryptodev_fd); |
| 164 | + } |
| 165 | + |
| 166 | + if (fd != -1) |
| 167 | + { |
| 168 | + close(fd); |
| 169 | + } |
| 170 | + |
| 171 | + return -1; |
| 172 | +} |
| 173 | + |
| 174 | +static int ecdsa_genkey(int op, FAR const unsigned char *d, size_t dlen, |
| 175 | + FAR const unsigned char *qx, size_t qxlen, |
| 176 | + FAR const unsigned char *qy, size_t qylen, |
| 177 | + FAR const unsigned char *qz, size_t qzlen) |
| 178 | +{ |
| 179 | + struct crypt_kop cryptk; |
| 180 | + int cryptodev_fd = -1; |
| 181 | + int fd = -1; |
| 182 | + |
| 183 | + if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) |
| 184 | + { |
| 185 | + perror("/dev/crypto"); |
| 186 | + goto err; |
| 187 | + } |
| 188 | + |
| 189 | + if (ioctl(fd, CRIOGET, &cryptodev_fd) == -1) |
| 190 | + { |
| 191 | + perror("CRIOGET"); |
| 192 | + goto err; |
| 193 | + } |
| 194 | + |
| 195 | + memset(&cryptk, 0, sizeof(cryptk)); |
| 196 | + cryptk.crk_op = op; |
| 197 | + cryptk.crk_iparams = 0; |
| 198 | + cryptk.crk_oparams = 4; |
| 199 | + cryptk.crk_param[0].crp_p = (caddr_t)d; |
| 200 | + cryptk.crk_param[0].crp_nbits = dlen * 8; |
| 201 | + cryptk.crk_param[1].crp_p = (caddr_t)qx; |
| 202 | + cryptk.crk_param[1].crp_nbits = qxlen * 8; |
| 203 | + cryptk.crk_param[2].crp_p = (caddr_t)qy; |
| 204 | + cryptk.crk_param[2].crp_nbits = qylen * 8; |
| 205 | + cryptk.crk_param[3].crp_p = (caddr_t)qz; |
| 206 | + cryptk.crk_param[3].crp_nbits = qzlen * 8; |
| 207 | + |
| 208 | + if (ioctl(cryptodev_fd, CIOCKEY, &cryptk) == -1) |
| 209 | + { |
| 210 | + perror("CIOCKEY"); |
| 211 | + goto err; |
| 212 | + } |
| 213 | + |
| 214 | + close(cryptodev_fd); |
| 215 | + close(fd); |
| 216 | + return cryptk.crk_status; |
| 217 | + |
| 218 | +err: |
| 219 | + if (cryptodev_fd != -1) |
| 220 | + { |
| 221 | + close(cryptodev_fd); |
| 222 | + } |
| 223 | + |
| 224 | + if (fd != -1) |
| 225 | + { |
| 226 | + close(fd); |
| 227 | + } |
| 228 | + |
| 229 | + return -1; |
| 230 | +} |
| 231 | + |
| 232 | +/* Test curve SECP256R1 */ |
| 233 | + |
| 234 | +static int test_p256 (void) |
| 235 | +{ |
| 236 | + int ret = 0; |
| 237 | + int len = SECP256R1_KEYLEN; |
| 238 | + unsigned char *x = (unsigned char *)malloc(len); |
| 239 | + unsigned char *y = (unsigned char *)malloc(len); |
| 240 | + unsigned char *z = (unsigned char *)malloc(len); |
| 241 | + unsigned char *d = (unsigned char *)malloc(len); |
| 242 | + unsigned char *r = (unsigned char *)malloc(len); |
| 243 | + unsigned char *s = (unsigned char *)malloc(len); |
| 244 | + |
| 245 | + if (x == NULL || y == NULL || z == NULL || |
| 246 | + d == NULL || r == NULL || s == NULL) |
| 247 | + { |
| 248 | + perror("Error no enough buffer"); |
| 249 | + goto free; |
| 250 | + } |
| 251 | + |
| 252 | + memset(x, 0, len); |
| 253 | + memset(y, 0, len); |
| 254 | + memset(z, 0, len); |
| 255 | + memset(d, 0, len); |
| 256 | + |
| 257 | + ret = ecdsa_genkey(CRK_ECDSA_SECP256R1_GENKEY, x, len, |
| 258 | + y, len, z, len, d, len); |
| 259 | + if (ret != 0) |
| 260 | + { |
| 261 | + printf("p256 generate key failed\n"); |
| 262 | + goto free; |
| 263 | + } |
| 264 | + |
| 265 | + ret = ecdsa_sign(CRK_ECDSA_SECP256R1_SIGN, r, len, s, len, |
| 266 | + d, len, sha256_message, len); |
| 267 | + if (ret != 0) |
| 268 | + { |
| 269 | + printf("p256 sign failed\n"); |
| 270 | + goto free; |
| 271 | + } |
| 272 | + |
| 273 | + ret = ecdsa_verify(CRK_ECDSA_SECP256R1_VERIFY, x, len, y, len, |
| 274 | + z, len, r, len, s, len, sha256_message, len); |
| 275 | + if (ret != 0) |
| 276 | + { |
| 277 | + printf("p256 verify failed\n"); |
| 278 | + } |
| 279 | + |
| 280 | +free: |
| 281 | + if (x) |
| 282 | + { |
| 283 | + free(x); |
| 284 | + } |
| 285 | + |
| 286 | + if (y) |
| 287 | + { |
| 288 | + free(y); |
| 289 | + } |
| 290 | + |
| 291 | + if (z) |
| 292 | + { |
| 293 | + free(z); |
| 294 | + } |
| 295 | + |
| 296 | + if (d) |
| 297 | + { |
| 298 | + free(d); |
| 299 | + } |
| 300 | + |
| 301 | + if (r) |
| 302 | + { |
| 303 | + free(r); |
| 304 | + } |
| 305 | + |
| 306 | + if (s) |
| 307 | + { |
| 308 | + free(s); |
| 309 | + } |
| 310 | + |
| 311 | + return ret; |
| 312 | +} |
| 313 | + |
| 314 | +/**************************************************************************** |
| 315 | + * Public Functions |
| 316 | + ****************************************************************************/ |
| 317 | + |
| 318 | +int main(void) |
| 319 | +{ |
| 320 | + if (test_p256() == 0) |
| 321 | + { |
| 322 | + printf("p256 test success\n"); |
| 323 | + } |
| 324 | + |
| 325 | + return 0; |
| 326 | +} |
0 commit comments