~ubuntu-branches/ubuntu/natty/python3.1/natty-security

« back to all changes in this revision

Viewing changes to Modules/sha512module.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-07-06 16:52:42 UTC
  • mfrom: (1.2.1 upstream) (2.1.11 sid)
  • Revision ID: james.westby@ubuntu.com-20100706165242-2xv4i019r3et6c0j
Tags: 3.1.2+20100706-1ubuntu1
* Merge with Debian; remaining changes:
  - Regenerate the control file.
  - Add debian/patches/overwrite-semaphore-check for Lucid buildds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
/* Endianness testing and definitions */
26
26
#define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
27
 
        if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
 
27
        if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
28
28
 
29
29
#define PCT_LITTLE_ENDIAN 1
30
30
#define PCT_BIG_ENDIAN 0
34
34
typedef unsigned char SHA_BYTE;
35
35
 
36
36
#if SIZEOF_INT == 4
37
 
typedef unsigned int SHA_INT32; /* 32-bit integer */
38
 
typedef unsigned PY_LONG_LONG SHA_INT64;        /* 64-bit integer */
 
37
typedef unsigned int SHA_INT32; /* 32-bit integer */
 
38
typedef unsigned PY_LONG_LONG SHA_INT64;        /* 64-bit integer */
39
39
#else
40
40
/* not defined. compilation will die. */
41
41
#endif
49
49
 
50
50
typedef struct {
51
51
    PyObject_HEAD
52
 
    SHA_INT64 digest[8];                /* Message digest */
53
 
    SHA_INT32 count_lo, count_hi;       /* 64-bit bit count */
54
 
    SHA_BYTE data[SHA_BLOCKSIZE];       /* SHA data buffer */
 
52
    SHA_INT64 digest[8];                /* Message digest */
 
53
    SHA_INT32 count_lo, count_hi;       /* 64-bit bit count */
 
54
    SHA_BYTE data[SHA_BLOCKSIZE];       /* SHA data buffer */
55
55
    int Endianness;
56
 
    int local;                          /* unprocessed amount in data */
 
56
    int local;                          /* unprocessed amount in data */
57
57
    int digestsize;
58
58
} SHAobject;
59
59
 
65
65
    SHA_INT64 value;
66
66
 
67
67
    if ( Endianness == PCT_BIG_ENDIAN )
68
 
        return;
 
68
        return;
69
69
 
70
70
    byteCount /= sizeof(*buffer);
71
71
    while (byteCount--) {
72
72
        value = *buffer;
73
73
 
74
 
                ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff;
75
 
                ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff;
76
 
                ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff;
77
 
                ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff;
78
 
                ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff;
79
 
                ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff;
80
 
                ((unsigned char*)buffer)[6] = (unsigned char)(value >>  8) & 0xff;
81
 
                ((unsigned char*)buffer)[7] = (unsigned char)(value      ) & 0xff;
82
 
        
83
 
                buffer++;
 
74
                ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff;
 
75
                ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff;
 
76
                ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff;
 
77
                ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff;
 
78
                ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff;
 
79
                ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff;
 
80
                ((unsigned char*)buffer)[6] = (unsigned char)(value >>  8) & 0xff;
 
81
                ((unsigned char*)buffer)[7] = (unsigned char)(value      ) & 0xff;
 
82
 
 
83
                buffer++;
84
84
    }
85
85
}
86
86
 
125
125
    ( ((((x) & Py_ULL(0xFFFFFFFFFFFFFFFF))>>((unsigned PY_LONG_LONG)(y) & 63)) | \
126
126
      ((x)<<((unsigned PY_LONG_LONG)(64-((y) & 63))))) & Py_ULL(0xFFFFFFFFFFFFFFFF))
127
127
#define Ch(x,y,z)       (z ^ (x & (y ^ z)))
128
 
#define Maj(x,y,z)      (((x | y) & z) | (x & y)) 
 
128
#define Maj(x,y,z)      (((x | y) & z) | (x & y))
129
129
#define S(x, n)         ROR64((x),(n))
130
130
#define R(x, n)         (((x) & Py_ULL(0xFFFFFFFFFFFFFFFF)) >> ((unsigned PY_LONG_LONG)n))
131
131
#define Sigma0(x)       (S(x, 28) ^ S(x, 34) ^ S(x, 39))
144
144
    longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness);
145
145
 
146
146
    for (i = 16; i < 80; ++i) {
147
 
                W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
 
147
                W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
148
148
    }
149
149
    for (i = 0; i < 8; ++i) {
150
150
        S[i] = sha_info->digest[i];
238
238
    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,Py_ULL(0x5fcb6fab3ad6faec));
239
239
    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],79,Py_ULL(0x6c44198c4a475817));
240
240
 
241
 
#undef RND     
242
 
    
 
241
#undef RND
 
242
 
243
243
    /* feedback */
244
244
    for (i = 0; i < 8; i++) {
245
245
        sha_info->digest[i] = sha_info->digest[i] + S[i];
341
341
    count = (int) ((lo_bit_count >> 3) & 0x7f);
342
342
    ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
343
343
    if (count > SHA_BLOCKSIZE - 16) {
344
 
        memset(((SHA_BYTE *) sha_info->data) + count, 0,
345
 
               SHA_BLOCKSIZE - count);
346
 
        sha512_transform(sha_info);
347
 
        memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16);
 
344
        memset(((SHA_BYTE *) sha_info->data) + count, 0,
 
345
               SHA_BLOCKSIZE - count);
 
346
        sha512_transform(sha_info);
 
347
        memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16);
348
348
    }
349
349
    else {
350
 
        memset(((SHA_BYTE *) sha_info->data) + count, 0,
351
 
               SHA_BLOCKSIZE - 16 - count);
 
350
        memset(((SHA_BYTE *) sha_info->data) + count, 0,
 
351
               SHA_BLOCKSIZE - 16 - count);
352
352
    }
353
353
 
354
354
    /* GJS: note that we add the hi/lo in big-endian. sha512_transform will
521
521
    /* Create a new string */
522
522
    retval = PyUnicode_FromStringAndSize(NULL, self->digestsize * 2);
523
523
    if (!retval)
524
 
            return NULL;
 
524
            return NULL;
525
525
    hex_digest = PyUnicode_AS_UNICODE(retval);
526
526
    if (!hex_digest) {
527
 
            Py_DECREF(retval);
528
 
            return NULL;
 
527
            Py_DECREF(retval);
 
528
            return NULL;
529
529
    }
530
530
 
531
531
    /* Make hex version of the digest */
532
532
    for (i=j=0; i<self->digestsize; i++) {
533
533
        char c;
534
534
        c = (digest[i] >> 4) & 0xf;
535
 
        c = (c>9) ? c+'a'-10 : c + '0';
 
535
        c = (c>9) ? c+'a'-10 : c + '0';
536
536
        hex_digest[j++] = c;
537
537
        c = (digest[i] & 0xf);
538
 
        c = (c>9) ? c+'a'-10 : c + '0';
 
538
        c = (c>9) ? c+'a'-10 : c + '0';
539
539
        hex_digest[j++] = c;
540
540
    }
541
541
    return retval;
563
563
}
564
564
 
565
565
static PyMethodDef SHA_methods[] = {
566
 
    {"copy",      (PyCFunction)SHA512_copy,      METH_NOARGS, SHA512_copy__doc__},
567
 
    {"digest",    (PyCFunction)SHA512_digest,    METH_NOARGS, SHA512_digest__doc__},
 
566
    {"copy",      (PyCFunction)SHA512_copy,      METH_NOARGS, SHA512_copy__doc__},
 
567
    {"digest",    (PyCFunction)SHA512_digest,    METH_NOARGS, SHA512_digest__doc__},
568
568
    {"hexdigest", (PyCFunction)SHA512_hexdigest, METH_NOARGS, SHA512_hexdigest__doc__},
569
 
    {"update",    (PyCFunction)SHA512_update,    METH_VARARGS, SHA512_update__doc__},
570
 
    {NULL,        NULL}         /* sentinel */
 
569
    {"update",    (PyCFunction)SHA512_update,    METH_VARARGS, SHA512_update__doc__},
 
570
    {NULL,        NULL}         /* sentinel */
571
571
};
572
572
 
573
573
static PyObject *
604
604
 
605
605
static PyTypeObject SHA384type = {
606
606
    PyVarObject_HEAD_INIT(NULL, 0)
607
 
    "_sha512.sha384",   /*tp_name*/
608
 
    sizeof(SHAobject),  /*tp_size*/
609
 
    0,                  /*tp_itemsize*/
 
607
    "_sha512.sha384",   /*tp_name*/
 
608
    sizeof(SHAobject),  /*tp_size*/
 
609
    0,                  /*tp_itemsize*/
610
610
    /* methods */
611
 
    SHA512_dealloc,     /*tp_dealloc*/
612
 
    0,                  /*tp_print*/
613
 
    0,                  /*tp_getattr*/
 
611
    SHA512_dealloc,     /*tp_dealloc*/
 
612
    0,                  /*tp_print*/
 
613
    0,                  /*tp_getattr*/
614
614
    0,                  /*tp_setattr*/
615
615
    0,                  /*tp_reserved*/
616
616
    0,                  /*tp_repr*/
626
626
    Py_TPFLAGS_DEFAULT, /*tp_flags*/
627
627
    0,                  /*tp_doc*/
628
628
    0,                  /*tp_traverse*/
629
 
    0,                  /*tp_clear*/
630
 
    0,                  /*tp_richcompare*/
631
 
    0,                  /*tp_weaklistoffset*/
632
 
    0,                  /*tp_iter*/
633
 
    0,                  /*tp_iternext*/
634
 
    SHA_methods,        /* tp_methods */
635
 
    SHA_members,        /* tp_members */
 
629
    0,                  /*tp_clear*/
 
630
    0,                  /*tp_richcompare*/
 
631
    0,                  /*tp_weaklistoffset*/
 
632
    0,                  /*tp_iter*/
 
633
    0,                  /*tp_iternext*/
 
634
    SHA_methods,        /* tp_methods */
 
635
    SHA_members,        /* tp_members */
636
636
    SHA_getseters,      /* tp_getset */
637
637
};
638
638
 
639
639
static PyTypeObject SHA512type = {
640
640
    PyVarObject_HEAD_INIT(NULL, 0)
641
 
    "_sha512.sha512",   /*tp_name*/
642
 
    sizeof(SHAobject),  /*tp_size*/
643
 
    0,                  /*tp_itemsize*/
 
641
    "_sha512.sha512",   /*tp_name*/
 
642
    sizeof(SHAobject),  /*tp_size*/
 
643
    0,                  /*tp_itemsize*/
644
644
    /* methods */
645
 
    SHA512_dealloc,     /*tp_dealloc*/
646
 
    0,                  /*tp_print*/
647
 
    0,                  /*tp_getattr*/
 
645
    SHA512_dealloc,     /*tp_dealloc*/
 
646
    0,                  /*tp_print*/
 
647
    0,                  /*tp_getattr*/
648
648
    0,                  /*tp_setattr*/
649
649
    0,                  /*tp_reserved*/
650
650
    0,                  /*tp_repr*/
660
660
    Py_TPFLAGS_DEFAULT, /*tp_flags*/
661
661
    0,                  /*tp_doc*/
662
662
    0,                  /*tp_traverse*/
663
 
    0,                  /*tp_clear*/
664
 
    0,                  /*tp_richcompare*/
665
 
    0,                  /*tp_weaklistoffset*/
666
 
    0,                  /*tp_iter*/
667
 
    0,                  /*tp_iternext*/
668
 
    SHA_methods,        /* tp_methods */
669
 
    SHA_members,        /* tp_members */
 
663
    0,                  /*tp_clear*/
 
664
    0,                  /*tp_richcompare*/
 
665
    0,                  /*tp_weaklistoffset*/
 
666
    0,                  /*tp_iter*/
 
667
    0,                  /*tp_iternext*/
 
668
    SHA_methods,        /* tp_methods */
 
669
    SHA_members,        /* tp_members */
670
670
    SHA_getseters,      /* tp_getset */
671
671
};
672
672
 
761
761
static struct PyMethodDef SHA_functions[] = {
762
762
    {"sha512", (PyCFunction)SHA512_new, METH_VARARGS|METH_KEYWORDS, SHA512_new__doc__},
763
763
    {"sha384", (PyCFunction)SHA384_new, METH_VARARGS|METH_KEYWORDS, SHA384_new__doc__},
764
 
    {NULL,      NULL}            /* Sentinel */
 
764
    {NULL,      NULL}            /* Sentinel */
765
765
};
766
766
 
767
767
 
771
771
 
772
772
 
773
773
static struct PyModuleDef _sha512module = {
774
 
        PyModuleDef_HEAD_INIT,
775
 
        "_sha512",
776
 
        NULL,
777
 
        -1,
778
 
        SHA_functions,
779
 
        NULL,
780
 
        NULL,
781
 
        NULL,
782
 
        NULL
 
774
        PyModuleDef_HEAD_INIT,
 
775
        "_sha512",
 
776
        NULL,
 
777
        -1,
 
778
        SHA_functions,
 
779
        NULL,
 
780
        NULL,
 
781
        NULL,
 
782
        NULL
783
783
};
784
784
 
785
785
PyMODINIT_FUNC