0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #include "mpi-internal.h"
0022
0023
0024 static MPI constants[MPI_NUMBER_OF_CONSTANTS];
0025
0026
0027
0028
0029 static int __init mpi_init(void)
0030 {
0031 int idx;
0032 unsigned long value;
0033
0034 for (idx = 0; idx < MPI_NUMBER_OF_CONSTANTS; idx++) {
0035 switch (idx) {
0036 case MPI_C_ZERO:
0037 value = 0;
0038 break;
0039 case MPI_C_ONE:
0040 value = 1;
0041 break;
0042 case MPI_C_TWO:
0043 value = 2;
0044 break;
0045 case MPI_C_THREE:
0046 value = 3;
0047 break;
0048 case MPI_C_FOUR:
0049 value = 4;
0050 break;
0051 case MPI_C_EIGHT:
0052 value = 8;
0053 break;
0054 default:
0055 pr_err("MPI: invalid mpi_const selector %d\n", idx);
0056 return -EFAULT;
0057 }
0058 constants[idx] = mpi_alloc_set_ui(value);
0059 constants[idx]->flags = (16|32);
0060 }
0061
0062 return 0;
0063 }
0064 postcore_initcall(mpi_init);
0065
0066
0067
0068
0069
0070 MPI mpi_const(enum gcry_mpi_constants no)
0071 {
0072 if ((int)no < 0 || no > MPI_NUMBER_OF_CONSTANTS)
0073 pr_err("MPI: invalid mpi_const selector %d\n", no);
0074 if (!constants[no])
0075 pr_err("MPI: MPI subsystem not initialized\n");
0076 return constants[no];
0077 }
0078 EXPORT_SYMBOL_GPL(mpi_const);
0079
0080
0081
0082
0083
0084
0085
0086
0087 MPI mpi_alloc(unsigned nlimbs)
0088 {
0089 MPI a;
0090
0091 a = kmalloc(sizeof *a, GFP_KERNEL);
0092 if (!a)
0093 return a;
0094
0095 if (nlimbs) {
0096 a->d = mpi_alloc_limb_space(nlimbs);
0097 if (!a->d) {
0098 kfree(a);
0099 return NULL;
0100 }
0101 } else {
0102 a->d = NULL;
0103 }
0104
0105 a->alloced = nlimbs;
0106 a->nlimbs = 0;
0107 a->sign = 0;
0108 a->flags = 0;
0109 a->nbits = 0;
0110 return a;
0111 }
0112 EXPORT_SYMBOL_GPL(mpi_alloc);
0113
0114 mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs)
0115 {
0116 size_t len = nlimbs * sizeof(mpi_limb_t);
0117
0118 if (!len)
0119 return NULL;
0120
0121 return kmalloc(len, GFP_KERNEL);
0122 }
0123
0124 void mpi_free_limb_space(mpi_ptr_t a)
0125 {
0126 if (!a)
0127 return;
0128
0129 kfree_sensitive(a);
0130 }
0131
0132 void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs)
0133 {
0134 mpi_free_limb_space(a->d);
0135 a->d = ap;
0136 a->alloced = nlimbs;
0137 }
0138
0139
0140
0141
0142
0143 int mpi_resize(MPI a, unsigned nlimbs)
0144 {
0145 void *p;
0146
0147 if (nlimbs <= a->alloced)
0148 return 0;
0149
0150 if (a->d) {
0151 p = kcalloc(nlimbs, sizeof(mpi_limb_t), GFP_KERNEL);
0152 if (!p)
0153 return -ENOMEM;
0154 memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t));
0155 kfree_sensitive(a->d);
0156 a->d = p;
0157 } else {
0158 a->d = kcalloc(nlimbs, sizeof(mpi_limb_t), GFP_KERNEL);
0159 if (!a->d)
0160 return -ENOMEM;
0161 }
0162 a->alloced = nlimbs;
0163 return 0;
0164 }
0165
0166 void mpi_clear(MPI a)
0167 {
0168 if (!a)
0169 return;
0170 a->nlimbs = 0;
0171 a->flags = 0;
0172 }
0173 EXPORT_SYMBOL_GPL(mpi_clear);
0174
0175 void mpi_free(MPI a)
0176 {
0177 if (!a)
0178 return;
0179
0180 if (a->flags & 4)
0181 kfree_sensitive(a->d);
0182 else
0183 mpi_free_limb_space(a->d);
0184
0185 if (a->flags & ~7)
0186 pr_info("invalid flag value in mpi\n");
0187 kfree(a);
0188 }
0189 EXPORT_SYMBOL_GPL(mpi_free);
0190
0191
0192
0193
0194
0195 MPI mpi_copy(MPI a)
0196 {
0197 int i;
0198 MPI b;
0199
0200 if (a) {
0201 b = mpi_alloc(a->nlimbs);
0202 b->nlimbs = a->nlimbs;
0203 b->sign = a->sign;
0204 b->flags = a->flags;
0205 b->flags &= ~(16|32);
0206 for (i = 0; i < b->nlimbs; i++)
0207 b->d[i] = a->d[i];
0208 } else
0209 b = NULL;
0210 return b;
0211 }
0212
0213
0214
0215
0216
0217
0218 MPI mpi_alloc_like(MPI a)
0219 {
0220 MPI b;
0221
0222 if (a) {
0223 b = mpi_alloc(a->nlimbs);
0224 b->nlimbs = 0;
0225 b->sign = 0;
0226 b->flags = a->flags;
0227 } else
0228 b = NULL;
0229
0230 return b;
0231 }
0232
0233
0234
0235 void mpi_snatch(MPI w, MPI u)
0236 {
0237 if (w) {
0238 mpi_assign_limb_space(w, u->d, u->alloced);
0239 w->nlimbs = u->nlimbs;
0240 w->sign = u->sign;
0241 w->flags = u->flags;
0242 u->alloced = 0;
0243 u->nlimbs = 0;
0244 u->d = NULL;
0245 }
0246 mpi_free(u);
0247 }
0248
0249
0250 MPI mpi_set(MPI w, MPI u)
0251 {
0252 mpi_ptr_t wp, up;
0253 mpi_size_t usize = u->nlimbs;
0254 int usign = u->sign;
0255
0256 if (!w)
0257 w = mpi_alloc(mpi_get_nlimbs(u));
0258 RESIZE_IF_NEEDED(w, usize);
0259 wp = w->d;
0260 up = u->d;
0261 MPN_COPY(wp, up, usize);
0262 w->nlimbs = usize;
0263 w->flags = u->flags;
0264 w->flags &= ~(16|32);
0265 w->sign = usign;
0266 return w;
0267 }
0268 EXPORT_SYMBOL_GPL(mpi_set);
0269
0270 MPI mpi_set_ui(MPI w, unsigned long u)
0271 {
0272 if (!w)
0273 w = mpi_alloc(1);
0274
0275
0276
0277 RESIZE_IF_NEEDED(w, 1);
0278 w->d[0] = u;
0279 w->nlimbs = u ? 1 : 0;
0280 w->sign = 0;
0281 w->flags = 0;
0282 return w;
0283 }
0284 EXPORT_SYMBOL_GPL(mpi_set_ui);
0285
0286 MPI mpi_alloc_set_ui(unsigned long u)
0287 {
0288 MPI w = mpi_alloc(1);
0289 w->d[0] = u;
0290 w->nlimbs = u ? 1 : 0;
0291 w->sign = 0;
0292 return w;
0293 }
0294
0295
0296
0297
0298
0299
0300 void mpi_swap_cond(MPI a, MPI b, unsigned long swap)
0301 {
0302 mpi_size_t i;
0303 mpi_size_t nlimbs;
0304 mpi_limb_t mask = ((mpi_limb_t)0) - swap;
0305 mpi_limb_t x;
0306
0307 if (a->alloced > b->alloced)
0308 nlimbs = b->alloced;
0309 else
0310 nlimbs = a->alloced;
0311 if (a->nlimbs > nlimbs || b->nlimbs > nlimbs)
0312 return;
0313
0314 for (i = 0; i < nlimbs; i++) {
0315 x = mask & (a->d[i] ^ b->d[i]);
0316 a->d[i] = a->d[i] ^ x;
0317 b->d[i] = b->d[i] ^ x;
0318 }
0319
0320 x = mask & (a->nlimbs ^ b->nlimbs);
0321 a->nlimbs = a->nlimbs ^ x;
0322 b->nlimbs = b->nlimbs ^ x;
0323
0324 x = mask & (a->sign ^ b->sign);
0325 a->sign = a->sign ^ x;
0326 b->sign = b->sign ^ x;
0327 }
0328
0329 MODULE_DESCRIPTION("Multiprecision maths library");
0330 MODULE_LICENSE("GPL");