Changeset 15


Ignore:
Timestamp:
05/12/06 02:09:37 (14 years ago)
Author:
sgk
Message:

とりあえず、実装終わり。やっぱ、速いね。

File:
1 edited

Legend:

Unmodified
Added
Removed
  • camellia/trunk/ccamellia.c

    r12 r15  
    11#include <Python.h> 
    22#include <string.h> 
     3#include <assert.h> 
    34#include "camellia.h" 
    45 
     
    89 * http://omake.accense.com/wiki/PythonCamellia 
    910 *  
    10  * Portions Copyright (c) 2006 by Accense Technology, Inc. 
    11  * on the work to port for Python derived from the Camellia source code 
    12  * distributed as 'camellia-BSD-1.0.tar.gz'. 
     11 * Copyright (c) 2006, Accense Technology, Inc. 
     12 * All rights reserved. 
     13 * 
     14 * Redistribution and use in source and binary forms, with or without 
     15 * modification, are permitted provided that the following conditions are met: 
     16 * 
     17 * o Redistributions of source code must retain the above copyright notice, this 
     18 *   list of conditions and the following disclaimer. 
     19 * o Redistributions in binary form must reproduce the above copyright notice, 
     20 *   this list of conditions and the following disclaimer in the documentation 
     21 *   and/or other materials provided with the distribution.  
     22 * o Neither the name of the Accense Technology, Inc. nor the names of its 
     23 *   contributors may be used to endorse or promote products derived from this 
     24 *   software without specific prior written permission.  
     25 * 
     26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
     27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
     28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
     29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
     30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
     31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
     32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
     33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
     34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
     35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
     36 * POSSIBILITY OF SUCH DAMAGE. 
     37 * 
    1338 */ 
     39 
    1440 
    1541/* 
    1642 * def Ekeygen(rawKey): 
    17  *   if len(rawKey) == 16: 
    18  *     return (0,) * 52 
    19  *   elif len(rawKey) == 24 or len(rawKey) == 32: 
    20  *     return (0,) * 68 
     43 *   return keyTable 
     44 * 
     45 * rawKey: 16 characters string 
     46 * keyTable: 204 or 272 characters string 
    2147 */ 
    2248static PyObject * 
     
    2551  int length; 
    2652  unsigned int keyTable[68]; 
    27   PyObject* tuple; 
    28   int i; 
    2953 
    3054  if (!PyArg_ParseTuple(args, "s#", &rawKey, &length)) 
     
    4670  memset(keyTable, 0, sizeof (keyTable)); 
    4771  Camellia_Ekeygen(length * 8, rawKey, keyTable); 
    48  
    49   length = (length == 16 ? 52 : 68); 
    50   tuple = PyTuple_New(length); 
    51   for (i = 0; i < length; ++i) 
    52     PyTuple_SET_ITEM(tuple, i, PyLong_FromUnsignedLong(keyTable[i])); 
    53  
    54   return tuple; 
     72  return PyString_FromStringAndSize( 
     73      (const char*)&keyTable[0], (length == 16 ? 52*4 : 68*4)); 
    5574} 
    5675 
     76 
     77/* 
     78 * def EncryptBlock(plainText, keyTable): 
     79 *   return cipherText 
     80 * 
     81 * plainText: 16 characters string 
     82 * keyTable: 204 or 272 characters string 
     83 * cipherText: 16 characters string 
     84 */ 
     85static PyObject * 
     86EncryptBlock(PyObject* self, PyObject* args) { 
     87  const char* plainText; 
     88  const unsigned int* keyTable; 
     89  int len1, len2; 
     90  char* cipherText; 
     91  PyObject* o; 
     92 
     93  if (!PyArg_ParseTuple(args, "s#s#", &plainText, &len1, &keyTable, &len2)) 
     94    return 0; 
     95  if (len1 != 16) { 
     96    PyErr_SetString(PyExc_ValueError, "plainText must be 16 characters"); 
     97    return 0; 
     98  } 
     99 
     100  o = PyString_FromStringAndSize(0, 16); 
     101  PyString_AsStringAndSize(o, &cipherText, &len1); 
     102  assert(len1 == 16); 
     103 
     104  switch (len2) { 
     105  case 52*4: 
     106    /* keyBitLength 128 */ 
     107    Camellia_EncryptBlock(128, plainText, keyTable, cipherText); 
     108    return o; 
     109 
     110  case 68*4: 
     111    /* keyBitLength 192 or 256; don't worry about '256' below. */ 
     112    Camellia_EncryptBlock(256, plainText, keyTable, cipherText); 
     113    return o; 
     114 
     115  default: 
     116    Py_DECREF(o); 
     117    PyErr_SetString(PyExc_ValueError, "keyTable must be 52 or 68 characters"); 
     118    return 0; 
     119  } 
     120} 
     121 
     122 
     123/* 
     124 * def DecryptBlock(cipherText, keyTable): 
     125 *   return plainText 
     126 * 
     127 * cipherText: 16 characters string 
     128 * keyTable: 204 or 272 characters string 
     129 * plainText: 16 characters string 
     130 */ 
     131static PyObject * 
     132DecryptBlock(PyObject* self, PyObject* args) { 
     133  const char* cipherText; 
     134  const unsigned int* keyTable; 
     135  int len1, len2; 
     136  char* plainText; 
     137  PyObject* o; 
     138 
     139  if (!PyArg_ParseTuple(args, "s#s#", &cipherText, &len1, &keyTable, &len2)) 
     140    return 0; 
     141  if (len1 != 16) { 
     142    PyErr_SetString(PyExc_ValueError, "cipherText must be 16 characters"); 
     143    return 0; 
     144  } 
     145 
     146  o = PyString_FromStringAndSize(0, 16); 
     147  PyString_AsStringAndSize(o, &plainText, &len1); 
     148  assert(len1 == 16); 
     149 
     150  switch (len2) { 
     151  case 52*4: 
     152    /* keyBitLength 128 */ 
     153    Camellia_DecryptBlock(128, cipherText, keyTable, plainText); 
     154    return o; 
     155 
     156  case 68*4: 
     157    /* keyBitLength 192 or 256; don't worry about '256' below. */ 
     158    Camellia_DecryptBlock(256, cipherText, keyTable, plainText); 
     159    return o; 
     160 
     161  default: 
     162    Py_DECREF(o); 
     163    PyErr_SetString(PyExc_ValueError, "keyTable must be 52 or 68 characters"); 
     164    return 0; 
     165  } 
     166} 
     167 
     168 
    57169static PyMethodDef methodTable[] = { 
     170  /* XXX help string */ 
    58171  { "Ekeygen", Ekeygen, METH_VARARGS, "Generate keyTable from key string." }, 
     172  { "EncryptBlock", EncryptBlock, METH_VARARGS, "EncryptBlock" }, 
     173  { "DecryptBlock", DecryptBlock, METH_VARARGS, "DecryptBlock" }, 
    59174  { 0, 0, 0, 0 } 
    60175}; 
Note: See TracChangeset for help on using the changeset viewer.