| 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 | * |
|---|
| | 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 | */ |
|---|
| | 85 | static PyObject * |
|---|
| | 86 | EncryptBlock(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 | */ |
|---|
| | 131 | static PyObject * |
|---|
| | 132 | DecryptBlock(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 | |
|---|