|
| 1 | +/**************************************************************************** |
| 2 | + * apps/crypto/mbedtls/source/bignum_alt.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + ****************************************************************************/ |
| 19 | + |
| 20 | +/**************************************************************************** |
| 21 | + * Included Files |
| 22 | + ****************************************************************************/ |
| 23 | + |
| 24 | +#include <errno.h> |
| 25 | +#include <fcntl.h> |
| 26 | +#include <stdint.h> |
| 27 | +#include <stdlib.h> |
| 28 | +#include <string.h> |
| 29 | +#include <unistd.h> |
| 30 | +#include <sys/ioctl.h> |
| 31 | +#include <sys/param.h> |
| 32 | +#include <sys/types.h> |
| 33 | +#include <nuttx/math/math_ioctl.h> |
| 34 | +#include <nuttx/math/mpi.h> |
| 35 | +#include "mbedtls/bignum.h" |
| 36 | +#include "mbedtls/platform.h" |
| 37 | + |
| 38 | +#define MBEDTLS_ROUNDUP(v, size) (((v) + (size - 1)) & ~(size - 1)) |
| 39 | + |
| 40 | +/**************************************************************************** |
| 41 | + * Private Functions |
| 42 | + ****************************************************************************/ |
| 43 | + |
| 44 | +static inline |
| 45 | +void mbedtls_mpi_to_mpiparam(FAR struct mpiparam *a, |
| 46 | + FAR const mbedtls_mpi *A) |
| 47 | +{ |
| 48 | + a->n = A->n * sizeof(mbedtls_mpi_uint); |
| 49 | + a->s = A->s; |
| 50 | + a->p = (FAR uint8_t *)A->p; |
| 51 | +} |
| 52 | + |
| 53 | +static inline |
| 54 | +void mpiparam_to_mbedtls_mpi(FAR mbedtls_mpi *A, |
| 55 | + FAR const struct mpiparam *a) |
| 56 | +{ |
| 57 | + A->n = a->n / sizeof(mbedtls_mpi_uint); |
| 58 | + A->s = a->s; |
| 59 | + A->p = (FAR mbedtls_mpi_uint *)a->p; |
| 60 | +} |
| 61 | + |
| 62 | +/**************************************************************************** |
| 63 | + * Public Functions |
| 64 | + ****************************************************************************/ |
| 65 | + |
| 66 | +int mbedtls_mpi_add_mpi(FAR mbedtls_mpi *X, FAR const mbedtls_mpi *A, |
| 67 | + FAR const mbedtls_mpi *B) |
| 68 | +{ |
| 69 | + int ret; |
| 70 | + int fd; |
| 71 | + struct mpi_calc_s mpi; |
| 72 | + |
| 73 | + fd = open("/dev/mpi0", O_RDWR); |
| 74 | + if (fd < 0) |
| 75 | + { |
| 76 | + return -errno; |
| 77 | + } |
| 78 | + |
| 79 | + mpi.op = MPI_CALC_FUNC_ADD; |
| 80 | + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); |
| 81 | + mbedtls_mpi_to_mpiparam(&mpi.param[1], B); |
| 82 | + |
| 83 | + mbedtls_mpi_grow(X, MBEDTLS_ROUNDUP(MAX(A->n, B->n) + 1, |
| 84 | + sizeof(mbedtls_mpi_uint))); |
| 85 | + mbedtls_mpi_to_mpiparam(&mpi.param[2], X); |
| 86 | + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); |
| 87 | + if (ret >= 0) |
| 88 | + { |
| 89 | + mpiparam_to_mbedtls_mpi(X, &mpi.param[2]); |
| 90 | + } |
| 91 | + |
| 92 | + close(fd); |
| 93 | + return ret; |
| 94 | +} |
| 95 | + |
| 96 | +int mbedtls_mpi_sub_mpi(FAR mbedtls_mpi *X, FAR const mbedtls_mpi *A, |
| 97 | + FAR const mbedtls_mpi *B) |
| 98 | +{ |
| 99 | + int ret; |
| 100 | + int fd; |
| 101 | + struct mpi_calc_s mpi; |
| 102 | + |
| 103 | + fd = open("/dev/mpi0", O_RDWR); |
| 104 | + if (fd < 0) |
| 105 | + { |
| 106 | + return -errno; |
| 107 | + } |
| 108 | + |
| 109 | + mpi.op = MPI_CALC_FUNC_SUB; |
| 110 | + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); |
| 111 | + mbedtls_mpi_to_mpiparam(&mpi.param[1], B); |
| 112 | + |
| 113 | + mbedtls_mpi_grow(X, MBEDTLS_ROUNDUP(MAX(A->n, B->n) + 1, |
| 114 | + sizeof(mbedtls_mpi_uint))); |
| 115 | + mbedtls_mpi_to_mpiparam(&mpi.param[2], X); |
| 116 | + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); |
| 117 | + if (ret >= 0) |
| 118 | + { |
| 119 | + mpiparam_to_mbedtls_mpi(X, &mpi.param[2]); |
| 120 | + } |
| 121 | + |
| 122 | + close(fd); |
| 123 | + return ret; |
| 124 | +} |
| 125 | + |
| 126 | +int mbedtls_mpi_mul_mpi(FAR mbedtls_mpi *X, FAR const mbedtls_mpi *A, |
| 127 | + FAR const mbedtls_mpi *B) |
| 128 | +{ |
| 129 | + int ret; |
| 130 | + int fd; |
| 131 | + struct mpi_calc_s mpi; |
| 132 | + |
| 133 | + fd = open("/dev/mpi0", O_RDWR); |
| 134 | + if (fd < 0) |
| 135 | + { |
| 136 | + return -errno; |
| 137 | + } |
| 138 | + |
| 139 | + mpi.op = MPI_CALC_FUNC_MUL; |
| 140 | + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); |
| 141 | + mbedtls_mpi_to_mpiparam(&mpi.param[1], B); |
| 142 | + |
| 143 | + mbedtls_mpi_grow(X, MBEDTLS_ROUNDUP(A->n + B->n, |
| 144 | + sizeof(mbedtls_mpi_uint))); |
| 145 | + mbedtls_mpi_to_mpiparam(&mpi.param[2], X); |
| 146 | + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); |
| 147 | + if (ret >= 0) |
| 148 | + { |
| 149 | + mpiparam_to_mbedtls_mpi(X, &mpi.param[2]); |
| 150 | + } |
| 151 | + |
| 152 | + close(fd); |
| 153 | + return ret; |
| 154 | +} |
| 155 | + |
| 156 | +int mbedtls_mpi_div_mpi(FAR mbedtls_mpi *Q, FAR mbedtls_mpi *R, |
| 157 | + FAR const mbedtls_mpi *A, FAR const mbedtls_mpi *B) |
| 158 | +{ |
| 159 | + int ret; |
| 160 | + int fd; |
| 161 | + struct mpi_calc_s mpi; |
| 162 | + |
| 163 | + fd = open("/dev/mpi0", O_RDWR); |
| 164 | + if (fd < 0) |
| 165 | + { |
| 166 | + return -errno; |
| 167 | + } |
| 168 | + |
| 169 | + mpi.op = MPI_CALC_FUNC_DIV; |
| 170 | + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); |
| 171 | + mbedtls_mpi_to_mpiparam(&mpi.param[1], B); |
| 172 | + mbedtls_mpi_grow(Q, A->n); |
| 173 | + mbedtls_mpi_grow(R, B->n); |
| 174 | + mbedtls_mpi_to_mpiparam(&mpi.param[2], Q); |
| 175 | + mbedtls_mpi_to_mpiparam(&mpi.param[3], R); |
| 176 | + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); |
| 177 | + if (ret >= 0) |
| 178 | + { |
| 179 | + mpiparam_to_mbedtls_mpi(Q, &mpi.param[2]); |
| 180 | + mpiparam_to_mbedtls_mpi(R, &mpi.param[3]); |
| 181 | + } |
| 182 | + |
| 183 | + close(fd); |
| 184 | + return ret; |
| 185 | +} |
| 186 | + |
| 187 | +int mbedtls_mpi_mod_mpi(FAR mbedtls_mpi *R, FAR const mbedtls_mpi *A, |
| 188 | + FAR const mbedtls_mpi *B) |
| 189 | +{ |
| 190 | + int ret; |
| 191 | + int fd; |
| 192 | + struct mpi_calc_s mpi; |
| 193 | + |
| 194 | + fd = open("/dev/mpi0", O_RDWR); |
| 195 | + if (fd < 0) |
| 196 | + { |
| 197 | + return -errno; |
| 198 | + } |
| 199 | + |
| 200 | + mpi.op = MPI_CALC_FUNC_MOD; |
| 201 | + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); |
| 202 | + mbedtls_mpi_to_mpiparam(&mpi.param[1], B); |
| 203 | + mbedtls_mpi_grow(R, B->n); |
| 204 | + mbedtls_mpi_to_mpiparam(&mpi.param[2], R); |
| 205 | + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); |
| 206 | + if (ret >= 0) |
| 207 | + { |
| 208 | + mpiparam_to_mbedtls_mpi(R, &mpi.param[2]); |
| 209 | + } |
| 210 | + |
| 211 | + close(fd); |
| 212 | + return ret; |
| 213 | +} |
| 214 | + |
| 215 | +int mbedtls_mpi_exp_mod(FAR mbedtls_mpi *X, FAR const mbedtls_mpi *A, |
| 216 | + FAR const mbedtls_mpi *E, FAR const mbedtls_mpi *N, |
| 217 | + FAR mbedtls_mpi *) |
| 218 | +{ |
| 219 | + int ret; |
| 220 | + int fd; |
| 221 | + struct mpi_calc_s mpi; |
| 222 | + |
| 223 | + fd = open("/dev/mpi0", O_RDWR); |
| 224 | + if (fd < 0) |
| 225 | + { |
| 226 | + return -errno; |
| 227 | + } |
| 228 | + |
| 229 | + mpi.op = MPI_CALC_FUNC_EXP_MOD; |
| 230 | + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); |
| 231 | + mbedtls_mpi_to_mpiparam(&mpi.param[1], E); |
| 232 | + mbedtls_mpi_to_mpiparam(&mpi.param[2], N); |
| 233 | + mbedtls_mpi_grow(X, N->n); |
| 234 | + mbedtls_mpi_to_mpiparam(&mpi.param[3], X); |
| 235 | + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); |
| 236 | + if (ret >= 0) |
| 237 | + { |
| 238 | + mpiparam_to_mbedtls_mpi(X, &mpi.param[3]); |
| 239 | + } |
| 240 | + |
| 241 | + close(fd); |
| 242 | + return ret; |
| 243 | +} |
| 244 | + |
| 245 | +int mbedtls_mpi_gcd(FAR mbedtls_mpi *G, FAR const mbedtls_mpi *A, |
| 246 | + FAR const mbedtls_mpi *B) |
| 247 | +{ |
| 248 | + int ret; |
| 249 | + int fd; |
| 250 | + struct mpi_calc_s mpi; |
| 251 | + |
| 252 | + fd = open("/dev/mpi0", O_RDWR); |
| 253 | + if (fd < 0) |
| 254 | + { |
| 255 | + return -errno; |
| 256 | + } |
| 257 | + |
| 258 | + mpi.op = MPI_CALC_FUNC_GCD; |
| 259 | + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); |
| 260 | + mbedtls_mpi_to_mpiparam(&mpi.param[1], B); |
| 261 | + mbedtls_mpi_grow(G, MIN(A->n, B->n)); |
| 262 | + mbedtls_mpi_to_mpiparam(&mpi.param[2], G); |
| 263 | + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); |
| 264 | + if (ret >= 0) |
| 265 | + { |
| 266 | + mpiparam_to_mbedtls_mpi(G, &mpi.param[2]); |
| 267 | + } |
| 268 | + |
| 269 | + close(fd); |
| 270 | + return ret; |
| 271 | +} |
| 272 | + |
| 273 | +int mbedtls_mpi_inv_mod(FAR mbedtls_mpi *X, FAR const mbedtls_mpi *A, |
| 274 | + FAR const mbedtls_mpi *N) |
| 275 | +{ |
| 276 | + int ret; |
| 277 | + int fd; |
| 278 | + struct mpi_calc_s mpi; |
| 279 | + |
| 280 | + fd = open("/dev/mpi0", O_RDWR); |
| 281 | + if (fd < 0) |
| 282 | + { |
| 283 | + return -errno; |
| 284 | + } |
| 285 | + |
| 286 | + mpi.op = MPI_CALC_FUNC_INV_MOD; |
| 287 | + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); |
| 288 | + mbedtls_mpi_to_mpiparam(&mpi.param[1], N); |
| 289 | + mbedtls_mpi_grow(X, N->n); |
| 290 | + mbedtls_mpi_to_mpiparam(&mpi.param[2], X); |
| 291 | + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); |
| 292 | + if (ret >= 0) |
| 293 | + { |
| 294 | + mpiparam_to_mbedtls_mpi(X, &mpi.param[2]); |
| 295 | + } |
| 296 | + |
| 297 | + close(fd); |
| 298 | + return ret; |
| 299 | +} |
0 commit comments