~ubuntu-branches/ubuntu/saucy/python-crypto/saucy-proposed

« back to all changes in this revision

Viewing changes to .pc/fix-unresolved-reference-size.patch/src/_fastmath.c

  • Committer: Package Import Robot
  • Author(s): Sebastian Ramacher
  • Date: 2012-05-24 20:16:34 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20120524201634-de6vxznjsdgekuwp
Tags: 2.6-1
* New upstream release.
  - Fixes CVE-2012-2417: insecure ElGamal key generation.
* Set urgency to high since this fixes a security issue.
* debian/copyright:
  - Fix formatting.
  - Update Format URL.
* debian/control:
  - Bump Standards-Version to 3.9.3 (no changes required).
  - Drop qNEW from Description since qNEW has been removed.
* debian/patches: Remove posixread.patch (not needed anymore).

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
longObjToMPZ (mpz_t m, PyLongObject * p)
67
67
{
68
68
        int size, i;
 
69
        long negative;
69
70
        mpz_t temp, temp2;
70
71
        mpz_init (temp);
71
72
        mpz_init (temp2);
72
73
#ifdef IS_PY3K
73
 
        if (p->ob_base.ob_size > 0)
 
74
        if (p->ob_base.ob_size > 0) {
74
75
                size = p->ob_base.ob_size;
75
 
        else
 
76
                negative = 1;
 
77
        } else {
76
78
                size = -p->ob_base.ob_size;
 
79
                negative = -1;
 
80
        }
77
81
#else
78
 
        if (p->ob_size > 0)
 
82
        if (p->ob_size > 0) {
79
83
                size = p->ob_size;
80
 
        else
 
84
                negative = 1;
 
85
        } else {
81
86
                size = -p->ob_size;
 
87
                negative = -1;
 
88
        }
82
89
#endif
83
90
        mpz_set_ui (m, 0);
84
91
        for (i = 0; i < size; i++)
91
98
#endif
92
99
                mpz_add (m, m, temp2);
93
100
        }
 
101
        mpz_mul_si(m, m, negative);
94
102
        mpz_clear (temp);
95
103
        mpz_clear (temp2);
96
104
}
104
112
#else
105
113
        int size = (mpz_sizeinbase (m, 2) + SHIFT - 1) / SHIFT;
106
114
#endif
 
115
        int sgn;
107
116
        int i;
108
117
        mpz_t temp;
109
118
        PyLongObject *l = _PyLong_New (size);
110
119
        if (!l)
111
120
                return NULL;
112
 
        mpz_init_set (temp, m);
 
121
        sgn = mpz_sgn(m);
 
122
        mpz_init(temp);
 
123
        mpz_mul_si(temp, m, sgn);
113
124
        for (i = 0; i < size; i++)
114
125
        {
115
126
#ifdef IS_PY3K
124
135
        while ((i > 0) && (l->ob_digit[i - 1] == 0))
125
136
                i--;
126
137
#ifdef IS_PY3K
127
 
        l->ob_base.ob_size = i;
 
138
        l->ob_base.ob_size = i * sgn;
128
139
#else
129
 
        l->ob_size = i;
 
140
        l->ob_size = i * sgn;
130
141
#endif
131
142
        mpz_clear (temp);
132
143
        return (PyObject *) l;
466
477
                return NULL;
467
478
 
468
479
        key = PyObject_New (dsaKey, &dsaKeyType);
 
480
        if (key == NULL)
 
481
                return NULL;
469
482
        mpz_init (key->y);
470
483
        mpz_init (key->g);
471
484
        mpz_init (key->p);
552
565
static PyObject *
553
566
dsaKey__sign (dsaKey * key, PyObject * args)
554
567
{
555
 
        PyObject *lm, *lk, *lr, *ls;
 
568
        PyObject *lm, *lk, *lr, *ls, *retval;
556
569
        mpz_t m, k, r, s;
557
570
        int result;
558
571
        if (!PyArg_ParseTuple (args, "O!O!", &PyLong_Type, &lm,
574
587
        }
575
588
        lr = mpzToLongObj (r);
576
589
        ls = mpzToLongObj (s);
 
590
        if (lr == NULL || ls == NULL) goto errout;
577
591
        mpz_clear (m);
578
592
        mpz_clear (k);
579
593
        mpz_clear (r);
580
594
        mpz_clear (s);
581
 
        return Py_BuildValue ("(NN)", lr, ls);
 
595
        retval = Py_BuildValue ("(NN)", lr, ls);
 
596
        if (retval == NULL) goto errout;
 
597
        return retval;
 
598
 
 
599
errout:
 
600
        Py_XDECREF(lr);
 
601
        Py_XDECREF(ls);
 
602
        return NULL;
582
603
}
583
604
 
584
605
static PyObject *
703
724
                return NULL;
704
725
 
705
726
        key = PyObject_New (rsaKey, &rsaKeyType);
 
727
        if (key == NULL)
 
728
                return NULL;
706
729
        mpz_init (key->n);
707
730
        mpz_init (key->e);
708
731
        mpz_init (key->d);
838
861
static PyObject *
839
862
rsaKey__encrypt (rsaKey * key, PyObject * args)
840
863
{
841
 
        PyObject *l, *r;
 
864
        PyObject *l, *r, *retval;
842
865
        mpz_t v;
843
866
        int result;
844
867
        if (!PyArg_ParseTuple (args, "O!", &PyLong_Type, &l))
854
877
                return NULL;
855
878
        }
856
879
        r = (PyObject *) mpzToLongObj (v);
 
880
        if (r == NULL) return NULL;
857
881
        mpz_clear (v);
858
 
        return Py_BuildValue ("N", r);
 
882
        retval = Py_BuildValue ("N", r);
 
883
        if (retval == NULL) {
 
884
                Py_DECREF(r);
 
885
                return NULL;
 
886
        }
 
887
        return retval;
859
888
}
860
889
 
861
890
static PyObject *
862
891
rsaKey__decrypt (rsaKey * key, PyObject * args)
863
892
{
864
 
        PyObject *l, *r;
 
893
        PyObject *l, *r, *retval;
865
894
        mpz_t v;
866
895
        int result;
867
896
        if (!PyArg_ParseTuple (args, "O!", &PyLong_Type, &l))
884
913
                return NULL;
885
914
        }
886
915
        r = mpzToLongObj (v);
 
916
        if (r == NULL) return NULL;
887
917
        mpz_clear (v);
888
 
        return Py_BuildValue ("N", r);
 
918
        retval = Py_BuildValue ("N", r);
 
919
        if (retval == NULL) {
 
920
                Py_DECREF(r);
 
921
                return NULL;
 
922
        }
 
923
        return retval;
889
924
}
890
925
 
891
926
static PyObject *
916
951
static PyObject *
917
952
rsaKey__blind (rsaKey * key, PyObject * args)
918
953
{
919
 
        PyObject *l, *lblind, *r;
 
954
        PyObject *l, *lblind, *r, *retval;
920
955
        mpz_t v, vblind;
921
956
        int result;
922
957
        if (!PyArg_ParseTuple (args, "O!O!", &PyLong_Type, &l, 
940
975
                        return NULL;
941
976
                }
942
977
        r = (PyObject *) mpzToLongObj (v);
 
978
        if (r == NULL)
 
979
                return NULL;
943
980
        mpz_clear (v);
944
981
        mpz_clear (vblind);
945
 
        return Py_BuildValue ("N", r);
 
982
        retval = Py_BuildValue ("N", r);
 
983
        if (retval == NULL) {
 
984
                Py_DECREF(r);
 
985
                return NULL;
 
986
        }
 
987
        return retval;
946
988
}
947
989
 
948
990
static PyObject *
949
991
rsaKey__unblind (rsaKey * key, PyObject * args)
950
992
{
951
 
        PyObject *l, *lblind, *r;
 
993
        PyObject *l, *lblind, *r, *retval;
952
994
        mpz_t v, vblind;
953
995
        int result;
954
996
        if (!PyArg_ParseTuple (args, "O!O!", &PyLong_Type, &l, 
977
1019
                        return NULL;
978
1020
                }
979
1021
        r = (PyObject *) mpzToLongObj (v);
 
1022
        if (r == NULL) return NULL;
980
1023
        mpz_clear (v);
981
1024
        mpz_clear (vblind);
982
 
        return Py_BuildValue ("N", r);
 
1025
        retval = Py_BuildValue ("N", r);
 
1026
        if (retval == NULL) {
 
1027
                Py_DECREF(r);
 
1028
                return NULL;
 
1029
        }
 
1030
        return retval;
983
1031
}
984
1032
 
985
1033
static PyObject *
1025
1073
        longObjToMPZ (n, (PyLongObject *) l);
1026
1074
 
1027
1075
        Py_BEGIN_ALLOW_THREADS;
1028
 
        /* first check if n is known to be prime and do some trail division */
 
1076
        /* first check if n is known to be prime and do some trial division */
1029
1077
        for (i = 0; i < SIEVE_BASE_SIZE; ++i)
1030
1078
        {
1031
1079
                if (mpz_cmp_ui (n, sieve_base[i]) == 0)
1099
1147
        module_dict = PyModule_GetDict (module);
1100
1148
        Py_DECREF (module);
1101
1149
        new_func = PyDict_GetItemString (module_dict, "new");
 
1150
        if (new_func == NULL) {
 
1151
                PyErr_SetString (PyExc_RuntimeError,
 
1152
                                                 "Crypto.Random.new is missing.");
 
1153
                return NULL;
 
1154
        }
1102
1155
        if (!PyCallable_Check (new_func))
1103
1156
        {
1104
1157
                PyErr_SetString (PyExc_RuntimeError,
1105
 
                                                 "Cryptor.Random.new is not callable.");
 
1158
                                                 "Crypto.Random.new is not callable.");
1106
1159
                return NULL;
1107
1160
        }
1108
1161
        rng = PyObject_CallObject (new_func, NULL);
1153
1206
        }
1154
1207
 
1155
1208
        arglist = Py_BuildValue ("(l)", (long int)bytes);
 
1209
        if (arglist == NULL) {
 
1210
                return_val = 0;
 
1211
                goto cleanup;
 
1212
        }
1156
1213
        rand_bytes = PyObject_CallObject (randfunc, arglist);
 
1214
        if (rand_bytes == NULL) {
 
1215
                return_val = 0;
 
1216
                goto cleanup;
 
1217
        }
1157
1218
        Py_DECREF (arglist);
1158
1219
        if (!PyBytes_Check (rand_bytes))
1159
1220
        {
1292
1353
        }
1293
1354
 
1294
1355
        Py_BEGIN_ALLOW_THREADS;
1295
 
        if ((mpz_tstbit (n, 0) == 0) || (mpz_cmp_ui (n, 3) < 0))
1296
 
                return (mpz_cmp_ui (n, 2) == 0);
 
1356
        /* check special cases (n==2, n even, n < 2) */
 
1357
        if ((mpz_tstbit (n, 0) == 0) || (mpz_cmp_ui (n, 3) < 0)) {
 
1358
                return_val = (mpz_cmp_ui (n, 2) == 0);
 
1359
                Py_BLOCK_THREADS;
 
1360
                return return_val;
 
1361
        }
1297
1362
 
1298
1363
        mpz_init (tmp);
1299
1364
        mpz_init (n_1);
1650
1715
#endif
1651
1716
        _fastmath_dict = PyModule_GetDict (_fastmath_module);
1652
1717
        fastmathError = PyErr_NewException ("_fastmath.error", NULL, NULL);
 
1718
#ifdef IS_PY3K
 
1719
        if (fastmathError == NULL) return NULL;
 
1720
#endif
1653
1721
        PyDict_SetItemString (_fastmath_dict, "error", fastmathError);
1654
1722
 
1655
1723
        PyModule_AddIntConstant(_fastmath_module, "HAVE_DECL_MPZ_POWM_SEC", HAVE_DECL_MPZ_POWM_SEC);