kpdyer / libffx Goto Github PK
View Code? Open in Web Editor NEWThe FFX Mode of Operation for Format-Preserving Encryption
License: GNU General Public License v3.0
The FFX Mode of Operation for Format-Preserving Encryption
License: GNU General Public License v3.0
pycrypto
(currently a dependency) is a defunct project and has been continued by pycryptodome. This project should be updated to use pycryptodome
instead (should be a drop replacement)
seems to over pad when right block is 2^16
plain = FFXInteger('0000065536', radix=10)
tweak = FFXInteger('0000000000', radix=10)
key = FFXInteger('2b7e151628aed2a6abf7158809cf4f3c', radix=16, blocksize=32)
ffx_obj = ffx.new(key.to_bytes(16), 10)
cypher = ffx_obj.encrypt(tweak, plain)
While working on a PHP implementation of the FFX[radix] algorithm, I compared the output of my implementation with this implementation. I noticed that the output begin to differ if the input range exceeds 8^24. This is due to a wrong implementation of the "expansion" of the Y variable.
In the paper Addendum to “The FFX Mode of Operation for Format-Preserving Encryption” it says:
Y ← first d + 4 bytes of (Y || AESK(Y ⊕ [1]16) || AESK(Y ⊕ [2]16) || AESK(Y ⊕ [3]16)··· )
However this implementation does actually:
Y ← first d + 4 bytes of (Y || AESK(Y ⊞ [1]16) || AESK(Y ⊞ [2]16) || AESK(Y ⊞ [3]16)··· )
Notice the difference between ⊕
and ⊞
. The paper states that for the scheme FFX[radix] ⊞
is block-wise addition. While it is nowhere explicitly stated in the paper that ⊕
means an XOR
operation. However, it could be interpreted from the description of the CBC-MACK(X)
algorithm (on page 2) as it states: for j ← 1 to m, set Y ← AESK(Y ⊕ Xi)
.
To correct this implementation these lines:
left = FFXInteger(bytes_to_long(Y), radix=self._radix, blocksize=32)
right = FFXInteger(str(i), radix=10, blocksize=16)
X = self.add(left, right)
TMP += self._ecb.encrypt(X.to_bytes(16))
should be replaced with:
I = FFXInteger(str(i), radix=10, blocksize=16).to_bytes(16)
X = "".join(chr(ord(Yi) ^ ord(Ii)) for Yi, Ii in zip(Y, I))
TMP += self._ecb.encrypt(X)
Test data:
radix=16 key=0x00000000000000000000000000000000 tweak=
Current implementation
input 000000000000000000000000000000000000000000000000
output ddb77d3be91a8e255fca9389a3d48da2b4476919744febea
input 0000000000000000000000000000000000000000000000000
output 1dad7e05f03706e8af695246afa40a49876c1d3e0699c5be1
Expected
input 000000000000000000000000000000000000000000000000
output ddb77d3be91a8e255fca9389a3d48da2b4476919744febea
input 0000000000000000000000000000000000000000000000000
output 1f7b9459d22b2bee17d5b5616e03241467767c9dcbc424c21
I am not able to encrypt email addresses with libffx.
Am I doing something wrong?
If I run benchmark.py --radix 62
it raises a ValueError.
Traceback (most recent call last):
File "benchmark.py", line 68, in <module>
main()
File "benchmark.py", line 47, in main
C = ffx.encrypt(T, M1)
File "FFX/__init__.py", line 262, in encrypt
C = self.add(A, self.F(n, T, i, B))
File "FFX/__init__.py", line 199, in F
if T == 0:
File "FFX/__init__.py", line 117, in __eq__
retval = (self.to_int() == other)
File "FFX/__init__.py", line 143, in to_int
self._as_int = int(self._x, self._radix)
ValueError: int() base must be >= 2 and <= 36
Please update libffx package on PyPI. The source is ready.
There is old version:
$ pip search libffx
libffx (0.0.1) - FFX
Thank you!
I can't install this lib because__init__.pyx doesn't exist, there is just__init__.py:
$$$:/tmp/libffx$ sudo python setup.py build
Traceback (most recent call last):
File "setup.py", line 8, in <module>
ext_modules = cythonize("FFX/__init__.pyx"),
File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 758, in cythonize
aliases=aliases)
File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 651, in create_extension_list
for file in nonempty(sorted(extended_iglob(filepattern)), "'%s' doesn't match any files" % filepattern):
File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 103, in nonempty
raise ValueError(error_msg)
ValueError: 'FFX/__init__.pyx' doesn't match any files
PyPI ffx right now is this:
https://pypi.python.org/pypi/ffx
Would you like to publish this library to PyPI?
https://packaging.python.org/distributing/#uploading-your-project-to-pypi
Hi,
with zero key ('0'*128) libFFX raises a ValueError:
import FFX
ffx_key = FFX.FFXInteger('0'*128, radix=2, blocksize = 128)
ffx = FFX.new(ffx_key.to_bytes(), 2)
ValueError: AES key must be either 16, 24, or 32 bytes long
This is caused by FFXInteger.to_bytes()
, which strips leading null bytes:
>>> FFXInteger('1'*8+'0'*8, radix=2, blocksize=16).to_bytes()
'\xff\x00'
>>> FFXInteger('0'*8+'1'*8, radix=2, blocksize=16).to_bytes()
'\xff'
Is it possible to support RADIX 62 and hove?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.