pax_global_header00006660000000000000000000000064134554215230014516gustar00rootroot0000000000000052 comment=2a98633d7587a4900cfa7cbed340f377f4acd930 md5-1.3/000077500000000000000000000000001345542152300120465ustar00rootroot00000000000000md5-1.3/.gitignore000066400000000000000000000000251345542152300140330ustar00rootroot00000000000000*.o *.obj *.so *.dll md5-1.3/Makefile000066400000000000000000000015741345542152300135150ustar00rootroot00000000000000# $Id: Makefile,v 1.7 2007/10/11 00:02:56 carregal Exp $ CONFIG= ./config include $(CONFIG) COMPAT52_OBJS= src/compat-5.2.o MD5_OBJS= src/md5.o src/md5lib.o MD5_LUAS= src/md5.lua MD5_LIBNAME = core.so DES56_OBJS= src/des56.o src/ldes56.o DES56_LIBNAME= des56.so all: src/$(MD5_LIBNAME) src/$(DES56_LIBNAME) src/$(MD5_LIBNAME) : $(MD5_OBJS) $(COMPAT52_OBJS) $(CC) $(CFLAGS) $(LIB_OPTION) -o src/$(MD5_LIBNAME) $(MD5_OBJS) $(COMPAT52_OBJS) src/$(DES56_LIBNAME) : $(DES56_OBJS) $(COMPAT52_OBJS) $(CC) $(CFLAGS) $(LIB_OPTION) -o src/$(DES56_LIBNAME) $(DES56_OBJS) $(COMPAT52_OBJS) install: src/$(MD5_LIBNAME) src/$(DES56_LIBNAME) cp src/$(DES56_LIBNAME) $(LUA_LIBDIR) mkdir -p $(LUA_LIBDIR)/md5 cp src/$(MD5_LIBNAME) $(LUA_LIBDIR)/md5/ mkdir -p $(LUA_DIR) cp $(MD5_LUAS) $(LUA_DIR) clean: rm -f $(MD5_OBJS) src/$(MD5_LIBNAME) $(DES56_OBJS) src/$(DES56_LIBNAME) $(COMPAT52_OBJS) md5-1.3/Makefile.win000077500000000000000000000023061345542152300143060ustar00rootroot00000000000000# $Id: Makefile.win,v 1.13 2008/05/12 17:49:05 carregal Exp $ include config.win MD5_OBJS= src\md5.obj src\md5lib.obj MD5_LUAS= src\md5.lua MD5_LIBNAME = core.dll DES56_OBJS= src\des56.obj src\ldes56.obj DES56_LIBNAME= des56.dll all: src\$(MD5_LIBNAME) src\$(DES56_LIBNAME) .c.obj: $(CC) /MD /c /Fo$@ $(CFLAGS) $< src\$(MD5_LIBNAME) : $(MD5_OBJS) link /dll /def:src\md5.def /out:src\$(MD5_LIBNAME) $(MD5_OBJS) $(LUA_LIB) IF EXIST src\core.dll.manifest mt -manifest src\core.dll.manifest -outputresource:src\core.dll;2 src\$(DES56_LIBNAME) : $(DES56_OBJS) link /dll /def:src\des56.def /out:src\$(DES56_LIBNAME) $(DES56_OBJS) $(LUA_LIB) IF EXIST src\des56.dll.manifest mt -manifest src\des56.dll.manifest -outputresource:src\des56.dll;2 install: src\$(MD5_LIBNAME) src\$(DES56_LIBNAME) IF NOT EXIST "$(LUA_LIBDIR)\md5" mkdir "$(LUA_LIBDIR)\md5" copy "src\$(MD5_LIBNAME)" "$(LUA_LIBDIR)\md5\core.dll" IF NOT EXIST "$(LUA_DIR)" mkdir "$(LUA_DIR)" copy "$(MD5_LUAS)" "$(LUA_DIR)" copy "src\$(DES56_LIBNAME)" "$(LUA_LIBDIR)\" clean: FOR %i in ($(MD5_OBJS)) DO del %i FOR %i in ($(DES56_OBJS)) DO del %i del "src\$(MD5_LIBNAME)" "src\$(DES56_LIBNAME)" src\core.lib src\core.exp src\des56.lib src\des56.exp md5-1.3/README.md000066400000000000000000000020441345542152300133250ustar00rootroot00000000000000# MD5 - Cryptographic Library for Lua http://keplerproject.github.io/md5/ MD5 offers basic cryptographic facilities for Lua 5.1: a hash (digest) function, a pair crypt/decrypt based on MD5 and CFB, and a pair crypt/decrypt based on DES with 56-bit keys. MD5 current version is 1.2. Please check the documentation at /doc/us/ for more information. ## Installation To install using [LuaRocks](https://github.com/keplerproject/luarocks) run: ``` luarocks install md5 ``` To install on Linux/OSX/BSD, please edit the config file and then call ``` make make install ``` The last step may require root privileges. ## History Version 1.2 [06/Sep/2013] * Code adapted to compile for Lua 5.0, 5.1 and 5.2 Version 1.1.2 [12/May/2008] * Fixed bug in 64-bit systems * Fixed the Windows makefile to accept longer directory names (patch by Alessandro Hecht and Ignacio Burgueño). ## License MD5 is free software and uses the same license as Lua (MIT). The DES 56 C library was implemented by Stuart Levy and uses a MIT license too (check the source). md5-1.3/config000066400000000000000000000013561345542152300132430ustar00rootroot00000000000000# Installation directories # Default prefix PREFIX = /usr/local # Lua version LUA_SYS_VER ?= 5.2 # System's libraries directory (where binary libraries are installed) LUA_LIBDIR= $(PREFIX)/lib/lua/$(LUA_SYS_VER) # System's lua directory (where Lua libraries are installed) LUA_DIR= $(PREFIX)/share/lua/$(LUA_SYS_VER) # Lua includes directory LUA_INC= $(PREFIX)/include # OS dependent LIB_OPTION= -shared #for Linux #LIB_OPTION= -bundle -undefined dynamic_lookup #for MacOS X # Compilation directives WARN= -O2 -Wall -fPIC -W -Waggregate-return -Wcast-align -Wmissing-prototypes -Wnested-externs -Wshadow -Wwrite-strings -Wpointer-arith INCS= -I$(LUA_INC) CFLAGS= $(WARN) $(INCS) CC= gcc # $Id: config,v 1.6 2007/10/29 23:00:23 carregal Exp $ md5-1.3/config.win000077500000000000000000000012111345542152300140300ustar00rootroot00000000000000# Installation directories # System's libraries directory (where binary libraries are installed) LUA_LIBDIR= c:\lua5.1 # Lua directory (where Lua source libraries will be installed) # This is the LUA_PATH LUA_DIR= c:\lua5.1\lua # Lua includes directory LUA_INC= c:\lua5.1\include LUA_LIB= c:\lua5.1\lua5.1.lib # Lua version number # (according to Lua 5.1 definition: # first version digit * 100 + second version digit # e.g. Lua 5.0.2 => 500, Lua 5.1 => 501, Lua 5.1.1 => 501) LUA_VERSION_NUM= 501 # Compilation directives WARN= /O2 INCS= /I$(LUA_INC) CFLAGS= $(WARN) $(INCS) CC= cl # $Id: config.win,v 1.5 2007/07/23 20:24:23 mascarenhas Exp $ md5-1.3/configure000077500000000000000000000010301345542152300137470ustar00rootroot00000000000000#!/bin/sh if [ -f config.orig ]; then mv config.orig config fi echo "Trying to where you installed Lua..." if which lua; then lua_bin=`which lua` lua_bin_dir=`dirname $lua_bin` else lua_bin=`which lua51` lua_bin_dir=`dirname $lua_bin` fi lua_root=`dirname $lua_bin_dir` if [ $lua_root != "" ]; then echo "Lua is in $lua_root" echo "Changing config" sed -i.orig -e "s|/usr/local|$lua_root|" config echo "Now run 'make' and 'make install'" else echo "Lua not found, please install Lua (and put in your PATH)" fi md5-1.3/doc/000077500000000000000000000000001345542152300126135ustar00rootroot00000000000000md5-1.3/doc/us/000077500000000000000000000000001345542152300132425ustar00rootroot00000000000000md5-1.3/doc/us/doc.css000066400000000000000000000064311345542152300145250ustar00rootroot00000000000000body { color: #47555c; font-size: 16px; font-family: "Open Sans", sans-serif; margin: 0; padding: 0; background: #eff4ff; } a:link { color: #008fee; } a:visited { color: #008fee; } a:hover { color: #22a7ff; } h1 { font-size:26px; } h2 { font-size:24px; } h3 { font-size:18px; } h4 { font-size:16px; } hr { height: 1px; background: #c1cce4; border: 0px; margin: 20px 0; } code { font-family: "Open Sans Mono", "Andale Mono", monospace; } tt { font-family: "Open Sans Mono", "Andale Mono", monospace; } body, td, th { } textarea, pre, tt { font-family: "Open Sans Mono", "Andale Mono", monospace; } img { border-width: 0px; } .example { background-color: #323744; color: white; font-size: 16px; padding: 16px 24px; border-radius: 2px; overflow-x: auto; } div.header, div.footer { } #container { } #product { background-color: white; padding: 10px; height: 130px; border-bottom: solid #d3dbec 1px; } #product big { font-size: 42px; } #product strong { font-weight: normal; } #product_logo { float: right; } #product_name { padding-top: 15px; padding-left: 30px; font-size: 42px; font-weight: normal; } #product_description { padding-left: 30px; color: #757779; } #main { background: #eff4ff; margin: 0; } #navigation { width: 100%; background-color: rgb(44,62,103); padding: 10px; margin: 0; } #navigation h1 { display: none; } #navigation a:hover { text-decoration: underline; } #navigation ul li a { color: rgb(136, 208, 255); font-weight: bold; text-decoration: none; } #navigation ul li li a { color: rgb(136, 208, 255); font-weight: normal; text-decoration: none; } #navigation ul { display: inline; color: white; padding: 0px; padding-top: 10px; padding-bottom: 10px; } #navigation li { display: inline; list-style-type: none; padding-left: 5px; padding-right: 5px; } #navigation li { padding: 10px; padding: 10px; } #navigation li li { } #navigation li:hover a { color: rgb(166, 238, 255); } #content { padding: 20px; width: 800px; margin-left: auto; margin-right: auto; } #about { display: none; } dl.reference { background-color: white; padding-left: 20px; padding-right: 20px; padding-bottom: 20px; border: solid #d3dbec 1px; } dl.reference dt { padding: 5px; padding-top: 25px; color: #637bbc; } dl.reference dl dt { padding-top: 5px; color: #637383; } dl.reference dd { } @media print { body { font: 10pt "Times New Roman", "TimeNR", Times, serif; } a { font-weight:bold; color: #004080; text-decoration: underline; } #main { background-color: #ffffff; border-left: 0px; } #container { margin-left: 2%; margin-right: 2%; background-color: #ffffff; } #content { margin-left: 0px; padding: 1em; border-left: 0px; border-right: 0px; background-color: #ffffff; } #navigation { display: none; } #product_logo { display: none; } #about img { display: none; } .example { font-family: "Andale Mono", monospace; font-size: 8pt; page-break-inside: avoid; } } md5-1.3/doc/us/index.html000066400000000000000000000126301345542152300152410ustar00rootroot00000000000000 MD5: Cryptographic Library for Lua
MD5
Cryptographic Library for Lua

Overview

MD5 offers checksum facilities for Lua 5.X: a hash (digest) function, a pair crypt/decrypt based on MD5 and CFB, and a pair crypt/decrypt based on DES with 56-bit keys.

Note that although MD5 was designed to be used as a cryptographic hash function, it was found that key collisions could be calculated in a few seconds, thus it should not be used where a cryptographic hash is required.

MD5 is free software and uses the same license as Lua (MIT).

Status

MD5 version 1.3 is now available for download.

Download

MD5 can be downloaded in source code from its GitHub page.

You can also install MD5 using LuaRocks:

luarocks install md5

History

Version 1.3 [16/Apr/2019]
  • Code updated to compile for Lua 5.X. Tested with Lua 5.1, 5.2, 5.3 and 5.4-work2.
Version 1.2 [06/Sep/2013]
  • Code adapted to compile for Lua 5.0, 5.1 and 5.2
Version 1.1.2 [12/May/2008]
  • Fixed bug in 64-bit systems
  • Fixed the Windows makefile to accept longer directory names (patch by Alessandro Hecht and Ignacio Burgueño).
Version 1.1.1 [28/Nov/2007]
  • Added DES56 tests (by Mauricio Bomfim)
  • Updated DES56 source to the latest release from Stuart Levy
Version 1.1.0 [30/Oct/2007]
Added DES56 crypt/decrypt functions.
Version 1.0.2 [08/May/2007]
  • Added a nmake version for Lua 5.1
  • Windows binaries for Lua 5.1.2
Version 1.0.1 [21/Aug/2006]
  • Adapted to work with both Lua 5.0 and Lua 5.1
  • Windows binaries for Lua 5.0.3
Version 1.0.0
  • First public version

Credits

This Lua version of MD5 was designed and implemented by Roberto Ierusalimschy and Marcela Ozório Suarez (based on the original MD5 by Ron Rivest). The DES 56 C library was implemented by Stuart Levy and uses a MIT license too. The current distribution was packed by Tomás Guisasola and Fábio Mascarenhas as part of the Kepler Project.

Valid XHTML 1.0!

md5-1.3/doc/us/license.html000066400000000000000000000103211345542152300155470ustar00rootroot00000000000000 MD5 License
MD5
Cryptographic Library for Lua

License

MD5 is free software: it can be used for both academic and commercial purposes at absolutely no cost. There are no royalties or GNU-like "copyleft" restrictions. MD5 qualifies as Open Source software. Its licenses are compatible with GPL. MD5 is not in the public domain and PUC-Rio keeps its copyright. The legal details are below.

The spirit of the license is that you are free to use MD5 for any purpose at no cost without having to ask us. The only requirement is that if you do use MD5, then you should give us credit by including the appropriate copyright notice somewhere in your product or its documentation.

MD5 was designed and implemented by Roberto Ierusalimschy and Marcela Ozório Suarez. The implementation is not derived from licensed software. The DES 56 C library was implemented by Stuart Levy and uses a MIT licence too.


Copyright © 2003-2013 PUC-Rio. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Valid XHTML 1.0!

md5-1.3/doc/us/manual.html000066400000000000000000000207031345542152300154070ustar00rootroot00000000000000 MD5: Cryptographic Library for Lua
MD5
Cryptographic Library for Lua

Introduction

MD5 offers basic cryptographic facilities for Lua 5.X: a hash (digest) function, a pair crypt/decrypt based on MD5 and CFB, and a pair crypt/decrypt based on DES with 56-bit keys.

MD5 is free software and uses the same license as Lua (MIT).

Building

If you are using Unix MD5 offers a Makefile and a separate configuration file, config, which should be edited to suit the particularities of the target platform before running make. The file has some definitions like paths to the external libraries, compiler options and the like. One important definition is the Lua version, which is not obtained from the installed software.

If you want to build MD5 on Windows MD5 offers the equivalent config.win and Makefile.win files to be used with Microsoft Visual Studio 2005.

Installation

The easiest way to install MD5 is to use LuaRocks:

luarocks install md5

If you prefer to install the files manually and are using Unix, the build generates two compiled binary files, core.so and des56.so. The first should be copied to a directory called md5 in your C path. Just copy the second to a directory in your Lua C path.

In both Unix and Windows systems the file md5.lua should be copied to a directory in your Lua path.

Reference

Lua API

All MD5-based functions are registered inside md5 module.

md5.sum (message)
Computes the MD5 message-digest of the string message. This function takes as input a message of arbitrary length and content and returns as output a 128-bit "fingerprint" (or "message digest") of the input.
The output is formatted as a binary string with 16 characters. It is conjectured that it is computationally infeasible to produce two messages having the same message digest, or to produce any message having a given pre-specified target message digest. (see RFC 1321)
md5.sumhexa (message)
Similar to md5.sum, but returns its value as a string of 32 hexadecimal digits.
md5.crypt (message, key [,seed])
Encrypts a string, using MD5 in CFB (Cipher-feedback mode). message is an arbitrary binary string to be encrypted. key is an arbitrary binary string to be used as a key. seed is an arbitrary binary string to be used as a seed; Returns the cyphertext (as a binary string).
If no seed is provided, the function uses the result of os.time() as a seed. It is recommended that you use different seeds for each message; the seed itself is not private, and should contain no private data, because it goes plain in the beginning of the encrypted message.
The length of the cyphertext is the length of the message plus the length of the seed plus one.
Note that MD5 based encryption should not be considered a strong solution. MD5 collision can cause to messages with different keys and the same seed to encrypt to the same value, which can then be decrypted with either key. The bigger the key the lesser the chance of this happening. More details about the weaknesses involved can be found here.
md5.decrypt (message, key)
Decrypts a string. The input message must be the result of a previous call to crypt. For any msg, key, and seed, we have that
md5.decrypt(md5.crypt(msg, key, seed), key) == msg
md5.exor (s1, s2)
Does a bit-a-bit exclusive or of strings s1 and s2. Both strings must have the same length, which will be also the length of the resulting string.

The DES-based functions are registered inside des56 module.

des56.crypt (message, key)
Encrypts a string, using DES with a 56-bit key. message is an arbitrary binary string to be encrypted. key is an 8-byte binary string to be used as a key. Returns the cyphertext (as a binary string).
des56.decrypt (message, key)
Decrypts a string. The input message must be the result of a previous call to des56.crypt. For any msg and key, we have that
des56.decrypt(des56.crypt(msg, key), key) == msg

C API

The following functions are declared in md5.h

int luaopen_md5_core (lua_State *L)
Opens the library and registers the "md5" Lua functions in the given state.
void md5 (const char *message, long len, char *output)
Computes the MD5 message-digest of message. len is the length of message. output is a buffer that receives the result; it must have at least 16 bytes (128 bits).

The following function is declared in ldes56.h

int luaopen_des56 (lua_State *L)
Opens the library and registers the "des56" Lua functions in the given state.

Valid XHTML 1.0!

md5-1.3/doc/us/md5.png000077500000000000000000000100311345542152300144330ustar00rootroot00000000000000PNG  IHDR>abKGD pHYsOO?TtEXtSoftwareESP Ghostscript 7.07IDATx]+HY K@mm(Å6)f @ NX fX &l3H^n}JK9>XWs߫Wo _.78p .78p .7] Tspc_m?sncY(X&%4zeBx+dgɭdsҵ c; `IeybIQJpΗ5c,2GPG ?_1Fv)b!Q²\ve6@`Y< 75dfBԄb6BV#pZGӁ`Y<* VЛ)9O,^`{2]K ؃},;tJ)("@6W*|bSB 97s{wXu.UJvlVq]cѾpO clp3UoIxm㇘|@B+1@uwL+]j2\{c,cm{ͥX(4{ !bRPpr8;s! r,^X.\Z!`s=bۤ_BGuz>sCsv*}C׸ z פpW2&&/` zIrTK%U!T:` Py=ǁ od$;R8J~ʧ<]C$ɗ^b|&` I9/'wVdρs9yJ]ҫ/FMBfRE$~Mm4Xt`2xNU,"e:"dJV;iY|f< `T0nI)5c,'!YBCF+&~ NHaYҢ':ש5_@#%LB@{-,!TS|@|=':U@[sh<3gKE9/{%*t3ڤ7!. 8 <5]&mE<<ۮB;>v)"~ATNiMXUuHC8t j93bh0DP 9ĄjGT !Lw  V]#? `~0岰ޢO"'1WPkzE eV{4jh#ӷ8$BAΠKoR*W2e@*t JpXs$1_.9pp󿑦ߑ$Q*WImfFQ0I.&S &߶xB` .:8ni..]* gNB{ӤpA/* V{AlhL@EM NפI46, o5>&8!4IxIam{`laD0`\j#ƪT1y4NpMNGc [I7c(meqAV5n"޵m:Q]%2RrmkP Lּa֩,F:07>{f})}0lIwe-i_4r} <]Lpڲ6 zsI2|4rm@G)dPfU14::nVPC Jo ;0۠AGi-T¼<`c-Zrh:Ub`rKU%vpȑ1=8k8 PU'6pD5 !Q,,宭Tw6wuBTi|\Y$3H!:%:u8 4۫F1 BwDyGa_b2a`%_X0_{mT誾3?`f!P̤hUߍFV,KC&q5!̞ߡYKIM*Kz]i@:]W{8P-FuɑVs{%D"+dD1)ĤϺ Q@ɦ/4eq;\z E1R[CБ $U-ل}FO@ c5k @aB chFIt -ŹkӼ̱j[ )A9DX& }#j,GhLM0% XgrL& ٛ&`#y 8l?2 'P{ 1@hT&^bNB=ҤHAݩpS=me6&@%@_\H+IYĶ6g f'M 4CU)()455߄k,7zc3#({)32~ҩ6\H &.*ȳU+3ޝcsȦX9ӫ/a #^pp;]nwq+s uIENDB`md5-1.3/rockspec/000077500000000000000000000000001345542152300136575ustar00rootroot00000000000000md5-1.3/rockspec/md5-1.1.1-1.rockspec000077500000000000000000000013071345542152300166750ustar00rootroot00000000000000package = "MD5" version = "1.1.1-1" source = { url = "http://luaforge.net/frs/download.php/2746/md5-1.1.1.tar.gz" } description = { summary = "Basic cryptographic library", detailed = [[ MD5 offers basic cryptographic facilities for Lua 5.1: a hash (digest) function, and a pair crypt/decrypt. ]], license = "MIT/X11", homepage = "http://www.keplerproject.org/md5/" } dependencies = { "lua >= 5.1" } build = { type = "make", variables = { LUA_VERSION_NUM="501", }, build_variables = { LIB_OPTION = "$(LIBFLAG)", CFLAGS = "$(CFLAGS) -I$(LUA_INCDIR)", }, install_variables = { LUA_LIBDIR = "$(LIBDIR)", LUA_DIR = "$(LUADIR)" } } md5-1.3/rockspec/md5-1.1.2-1.rockspec000077500000000000000000000016151345542152300167000ustar00rootroot00000000000000package = "MD5" version = "1.1.2-1" source = { url = "http://luaforge.net/frs/download.php/3355/md5-1.1.2.tar.gz" } description = { summary = "Basic cryptographic library", detailed = [[ MD5 offers basic cryptographic facilities for Lua 5.1: a hash (digest) function, a pair crypt/decrypt based on MD5 and CFB, and a pair crypt/decrypt based on DES with 56-bit keys. ]], license = "MIT/X11", homepage = "http://www.keplerproject.org/md5/" } dependencies = { "lua >= 5.1" } build = { type = "make", variables = { LUA_VERSION_NUM="501", }, build_variables = { LIB_OPTION = "$(LIBFLAG)", CFLAGS = "$(CFLAGS) -I$(LUA_INCDIR)", }, install_variables = { LUA_LIBDIR = "$(LIBDIR)", LUA_DIR = "$(LUADIR)" }, platforms = { win32 = { build_variables = { LUA_LIB = "$(LUA_LIBDIR)\\lua5.1.lib" } } } } md5-1.3/rockspec/md5-1.2-1.rockspec000066400000000000000000000017101345542152300165320ustar00rootroot00000000000000package = "MD5" version = "1.2-1" source = { url = "https://github.com/keplerproject/md5/archive/v1.2.tar.gz", md5 = "c166f8a983401802a86655a8c733441e", dir = "md5-1.2", } description = { summary = "Basic cryptographic library", detailed = [[ MD5 offers basic cryptographic facilities for Lua 5.X: a hash (digest) function, a pair crypt/decrypt based on MD5 and CFB, and a pair crypt/decrypt based on DES with 56-bit keys. ]], license = "MIT/X11", homepage = "http://www.keplerproject.org/md5/", } dependencies = { "lua >= 5.0" } build = { type = "builtin", modules = { md5 = "src/md5.lua", ["md5.core"] = { sources = { "src/compat-5.2.c", "src/md5.c", "src/md5lib.c", }, incdirs = { "src/", }, }, des56 = { sources = { "src/compat-5.2.c", "src/des56.c", "src/ldes56.c", }, incdirs = { "src/", }, }, }, copy_directories = { "doc", "tests", }, } md5-1.3/rockspec/md5-cvs-1.rockspec000077500000000000000000000015151345542152300170330ustar00rootroot00000000000000package = "MD5" version = "cvs-1" source = { cvs_tag = "HEAD", url = "cvs://:pserver:anonymous@cvs.luaforge.net:/cvsroot/md5" } description = { summary = "Basic cryptographic library", detailed = [[ MD5 offers basic cryptographic facilities for Lua 5.1: a hash (digest) function, and a pair crypt/decrypt. ]], license = "MIT/X11", homepage = "http://www.keplerproject.org/md5/" } dependencies = { "lua >= 5.1" } build = { type = "make", variables = { LUA_VERSION_NUM="501", }, build_variables = { LIB_OPTION = "$(LIBFLAG)", CFLAGS = "$(CFLAGS) -I$(LUA_INCDIR)", }, install_variables = { LUA_LIBDIR = "$(LIBDIR)", LUA_DIR = "$(LUADIR)" }, platforms = { win32 = { build_variables = { LUA_LIB = "$(LUA_LIBDIR)\\lua5.1.lib" } } } } md5-1.3/src/000077500000000000000000000000001345542152300126355ustar00rootroot00000000000000md5-1.3/src/compat-5.2.c000066400000000000000000000013021345542152300145620ustar00rootroot00000000000000#include "lua.h" #include "lauxlib.h" #include "compat-5.2.h" #if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501 /* ** Adapted from Lua 5.2.0 */ void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { luaL_checkstack(L, nup+1, "too many upvalues"); for (; l->name != NULL; l++) { /* fill the table with given functions */ int i; lua_pushstring(L, l->name); for (i = 0; i < nup; i++) /* copy upvalues to the top */ lua_pushvalue(L, -(nup + 1)); lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */ } lua_pop(L, nup); /* remove upvalues */ } #endif md5-1.3/src/compat-5.2.h000066400000000000000000000005351345542152300145760ustar00rootroot00000000000000#if !defined LUA_VERSION_NUM /* Lua 5.0 */ #define luaL_Reg luaL_reg #define luaL_addchar(B,c) \ ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ (*(B)->p++ = (char)(c))) #endif #if LUA_VERSION_NUM==501 /* Lua 5.1 */ #define lua_rawlen lua_objlen #endif void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup); md5-1.3/src/des56.c000077500000000000000000000357521345542152300137460ustar00rootroot00000000000000 /* * Fast implementation of the DES, as described in the Federal Register, * Vol. 40, No. 52, p. 12134, March 17, 1975. * * Stuart Levy, Minnesota Supercomputer Center, April 1988. * Currently (2007) slevy@ncsa.uiuc.edu * NCSA, University of Illinois Urbana-Champaign * * Calling sequence: * * typedef unsigned long keysched[32]; * * fsetkey(key, keysched) / * Converts a DES key to a "key schedule" * / * unsigned char key[8]; * keysched *ks; * * fencrypt(block, decrypt, keysched) / * En/decrypts one 64-bit block * / * unsigned char block[8]; / * data, en/decrypted in place * / * int decrypt; / * 0=>encrypt, 1=>decrypt * / * keysched *ks; / * key schedule, as set by fsetkey * / * * Key and data block representation: * The 56-bit key (bits 1..64 including "parity" bits 8, 16, 24, ..., 64) * and the 64-bit data block (bits 1..64) * are each stored in arrays of 8 bytes. * Following the NBS numbering, the MSB has the bit number 1, so * key[0] = 128*bit1 + 64*bit2 + ... + 1*bit8, ... through * key[7] = 128*bit57 + 64*bit58 + ... + 1*bit64. * In the key, "parity" bits are not checked; their values are ignored. * */ /* =============================================================================== License des56.c is licensed under the terms of the MIT license reproduced below. This means that des56.c is free software and can be used for both academic and commercial purposes at absolutely no cost. =============================================================================== Copyright (C) 1988 Stuart Levy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "des56.h" /* * Key schedule generation. * We begin by pointlessly permuting the 56 useful key bits into * two groups of 28 bits called C and D. * bK_C and bK_D are indexed by C and D bit numbers, respectively, * and give the key bit number (1..64) which should initialize that C/D bit. * This is the "permuted choice 1" table. */ static tiny bK_C[28] = { 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, }; static tiny bK_D[28] = { 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4, }; /* * For speed, we invert these, building tables to map groups of * key bits into the corresponding C and D bits. * We represent C and D each as 28 contiguous bits right-justified in a * word, padded on the left with zeros. * If key byte `i' is said to contain bits Ki,0 (MSB) Ki,1 ... Ki,7 (LSB) * then * wC_K4[i][Ki,0 Ki,1 Ki,2 Ki,3] gives the C bits for Ki,0..3, * wD_K4[i][Ki,0 Ki,1 Ki,2 Ki,3] the corresponding D bits, * wC_K3[i][Ki,4 Ki,5 Ki,6] the C bits for Ki,4..6, * and wD_K3[i][Ki,4 Ki,5 Ki,6] the D bits for Ki,4..6. * Ki,7 is ignored since it is the nominal parity bit. * We could just use a single table for [i][Ki,0 .. Ki,6] but that * would take a lot of storage for such a rarely-used function. */ static word32 wC_K4[8][16], wC_K3[8][8]; static word32 wD_K4[8][16], wD_K3[8][8]; /* * Successive Ci and Di for the sixteen steps in the key schedule are * created by independent 28-bit left circular shifts on C and D. * The shift count varies with the step number. */ static tiny preshift[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, }; /* * Each step in the key schedule is generated by selecting 48 bits * (8 groups of 6 bits) from the appropriately shifted Ci and Di. * bCD_KS, indexed by the key schedule bit number, gives the bit number * in CD (CD1 = MSB of C, CD28 = LSB of C, CD29 = MSB of D, CD56 = LSB of D) * which determines that bit of the key schedule. * Note that only C bits (1..28) appear in the first (upper) 24 bits of * the key schedule, and D bits (29..56) in the second (lower) 24 bits. * This is the "permuted-choice-2" table. */ static tiny bCD_KS[48] = { 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32, }; /* * We invert bCD_KS into a pair of tables which map groups of 4 * C or D bits into corresponding key schedule bits. * We represent each step of the key schedule as 8 groups of 8 bits, * with the 6 real bits right-justified in each 8-bit group. * hKS_C4[i][C4i+1 .. C4i+4] gives the bits in the high order (first four) * key schedule "bytes" which correspond to C bits 4i+1 .. 4i+4. * lKS_D4[i][D4i+1 .. D4i+4] gives the appropriate bits in the latter (last 4) * key schedule bytes, from the corresponding D bits. */ static word32 hKS_C4[7][16]; static word32 lKS_D4[7][16]; /* * Encryption/decryption. * Before beginning, and after ending, we perform another useless permutation * on the bits in the data block. * * The initial permutation and its inverse, final permutation * are too simple to need a table for. If we break the input I1 .. I64 into * 8-bit chunks I0,0 I0,1 ... I0,7 I1,0 I1,1 ... I7,7 * then the initial permutation sets LR as follows: * L = I7,1 I6,1 I5,1 ... I0,1 I7,3 I6,3 ... I0,3 I7,5 ... I0,5 I7,7 ... I0,7 * and * R = I7,0 I6,0 I5,0 ... I0,0 I7,2 I6,2 ... I0,2 I7,4 ... I0,4 I7,6 ... I0,6 * * If we number the bits in the final LR similarly, * L = L0,0 L0,1 ... L3,7 R = R0,0 R0,1 ... R3,7 * then the output is * O = R0,7 L0,7 R1,7 L1,7 ... R3,7 L3,7 R0,6 L0,6 ... L3,6 R0,5 ... R3,0 L3,0 * * To speed I => LR shuffling we use an array of 32-bit values indexed by * 8-bit input bytes. * wL_I8[ 0 I0,1 0 I0,3 0 I0,5 0 I0,7 ] = the corresponding L bits. * Other R and L bits are derived from wL_I8 by shifting. * * To speed LR => O shuffling, an array of 32-bit values indexed by 4-bit lumps: * wO_L4[ L0,4 L0,5 L0,6 L0,7 ] = the corresponding high-order 32 O bits. */ static word32 wL_I8[0x55 + 1]; static word32 wO_L4[16]; /* * Core of encryption/decryption. * In each key schedule stage, we: * take 8 overlapping groups of 6 bits each from R * (the NBS tabulates the bit selections in the E table, * but it's so simple we just use shifting to get the right bits) * XOR each group with the corresponding bits from the key schedule * Use the resulting 6 bits as an index into the appropriate S table * (there are 8 such tables, one per group of 6 bits) * Each S entry yields 4 bits. * The 8 groups of 4 bits are catenated into a 32-bit value. * Those 32 bits are permuted according to the P table. * Finally the permuted 32-bit value is XORed with L and becomes * the R value for the next stage, while the previous R becomes the new L. * * Here, we merge the P permutation with the S tables by making the * S entries be 32-bit masks, already suitably permuted. * Also, the bits in each six-bit group must be permuted before use as * an index into the NBS-tabulated S tables. * We rearrange entries in wPS so that natural bit order can be used. */ static word32 wPS[8][64]; static tiny P[32] = { 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25, }; static tiny S[8][64] = { { 14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7, 0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8, 4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0, 15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13, }, { 15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10, 3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5, 0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15, 13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9, }, { 10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8, 13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1, 13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7, 1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12, }, { 7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15, 13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9, 10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4, 3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14, }, { 2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9, 14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6, 4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14, 11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3, }, { 12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11, 10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8, 9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6, 4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13, }, { 4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1, 13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6, 1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2, 6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12, }, { 13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7, 1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2, 7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8, 2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11, }, }; static void buildtables( void ) { register int i, j; register word32 v; word32 wC_K[64], wD_K[64]; word32 hKS_C[28], lKS_D[28]; int Smap[64]; word32 wP[32]; #if USG # define ZERO(array) memset((char *)(array), '\0', sizeof(array)) #else # if BSD # define ZERO(array) bzero((char *)(array), sizeof(array)) # else # define ZERO(array) { register word32 *p = (word32 *)(array); \ i = sizeof(array) / sizeof(*p); \ do { *p++ = 0; } while(--i > 0); \ } # endif #endif /* Invert permuted-choice-1 (key => C,D) */ ZERO(wC_K); ZERO(wD_K); v = 1; for(j = 28; --j >= 0; ) { wC_K[ bK_C[j] - 1 ] = wD_K[ bK_D[j] - 1 ] = v; v += v; /* (i.e. v <<= 1) */ } for(i = 0; i < 64; i++) { int t = 8 >> (i & 3); for(j = 0; j < 16; j++) { if(j & t) { wC_K4[i >> 3][j] |= wC_K[i]; wD_K4[i >> 3][j] |= wD_K[i]; if(j < 8) { wC_K3[i >> 3][j] |= wC_K[i + 3]; wD_K3[i >> 3][j] |= wD_K[i + 3]; } } } /* Generate the sequence 0,1,2,3, 8,9,10,11, ..., 56,57,58,59. */ if(t == 1) i += 4; } /* Invert permuted-choice-2 */ ZERO(hKS_C); ZERO(lKS_D); v = 1; for(i = 24; (i -= 6) >= 0; ) { j = i+5; do { hKS_C[ bCD_KS[j] - 1 ] = lKS_D[ bCD_KS[j+24] - 28 - 1 ] = v; v += v; /* Like v <<= 1 but may be faster */ } while(--j >= i); v <<= 2; /* Keep byte aligned */ } for(i = 0; i < 28; i++) { v = 8 >> (i & 3); for(j = 0; j < 16; j++) { if(j & v) { hKS_C4[i >> 2][j] |= hKS_C[i]; lKS_D4[i >> 2][j] |= lKS_D[i]; } } } /* Initial permutation */ for(i = 0; i <= 0x55; i++) { v = 0; if(i & 64) v = (word32) 1 << 24; if(i & 16) v |= (word32) 1 << 16; if(i & 4) v |= (word32) 1 << 8; if(i & 1) v |= 1; wL_I8[i] = v; } /* Final permutation */ for(i = 0; i < 16; i++) { v = 0; if(i & 1) v = (word32) 1 << 24; if(i & 2) v |= (word32) 1 << 16; if(i & 4) v |= (word32) 1 << 8; if(i & 8) v |= (word32) 1; wO_L4[i] = v; } /* Funny bit rearrangement on second index into S tables */ for(i = 0; i < 64; i++) { Smap[i] = (i & 0x20) | (i & 1) << 4 | (i & 0x1e) >> 1; } /* Invert permutation P into mask indexed by R bit number */ v = 1; for(i = 32; --i >= 0; ) { wP[ P[i] - 1 ] = v; v += v; } /* Build bit-mask versions of S tables, indexed in natural bit order */ for(i = 0; i < 8; i++) { for(j = 0; j < 64; j++) { int k, t; t = S[i][ Smap[j] ]; for(k = 0; k < 4; k++) { if(t & 8) wPS[i][j] |= wP[4*i + k]; t += t; } } } } void fsetkey(char key[8], keysched *ks) { register int i; register word32 C, D; static int built = 0; if(!built) { buildtables(); built = 1; } C = D = 0; for(i = 0; i < 8; i++) { register int v; v = key[i] >> 1; /* Discard "parity" bit */ C |= wC_K4[i][(v>>3) & 15] | wC_K3[i][v & 7]; D |= wD_K4[i][(v>>3) & 15] | wD_K3[i][v & 7]; } /* * C and D now hold the suitably right-justified * 28 permuted key bits each. */ for(i = 0; i < 16; i++) { #ifdef CRAY #define choice2(x, v) x[6][v&15] | x[5][(v>>4)&15] | x[4][(v>>8)&15] | \ x[3][(v>>12)&15] | x[2][(v>>16)&15] | x[1][(v>>20)&15] | \ x[0][(v>>24)&15] #else register word32 *ap; # define choice2(x, v) ( \ ap = &(x)[0][0], \ ap[16*6 + (v&15)] | \ ap[16*5 + ((v>>4)&15)] | ap[16*4 + ((v>>8)&15)] | \ ap[16*3 + ((v>>12)&15)] | ap[16*2 + ((v>>16)&15)] | \ ap[16*1 + ((v>>20)&15)] | ap[16*0 + ((v>>24)&15)] ) #endif /* 28-bit left circular shift */ C <<= preshift[i]; C = ((C >> 28) & 3) | (C & (((word32)1<<28) - 1)); ks->KS[i].h = choice2(hKS_C4, C); D <<= preshift[i]; D = ((D >> 28) & 3) | (D & (((word32)1<<28) - 1)); ks->KS[i].l = choice2(lKS_D4, D); } } void fencrypt(char block[8], int decrypt, keysched *ks) { int i; register word32 L, R; register struct keystage *ksp; register word32 *ap; /* Initial permutation */ L = R = 0; i = 7; ap = wL_I8; do { register int v; v = block[i]; /* Could optimize according to ENDIAN */ L = ap[v & 0x55] | (L << 1); R = ap[(v >> 1) & 0x55] | (R << 1); } while(--i >= 0); if(decrypt) { ksp = &ks->KS[15]; } else { ksp = &ks->KS[0]; } #ifdef CRAY # define PS(i,j) wPS[i][j] #else # define PS(i,j) ap[64*(i) + (j)] ap = &wPS[0][0]; #endif i = 16; do { register word32 k, tR; tR = (R >> 15) | (R << 17); k = ksp->h; L ^= PS(0, ((tR >> 12) ^ (k >> 24)) & 63) | PS(1, ((tR >> 8) ^ (k >> 16)) & 63) | PS(2, ((tR >> 4) ^ (k >> 8)) & 63) | PS(3, (tR ^ k) & 63); k = ksp->l; L ^= PS(4, ((R >> 11) ^ (k >> 24)) & 63) | PS(5, ((R >> 7) ^ (k >> 16)) & 63) | PS(6, ((R >> 3) ^ (k >> 8)) & 63) | PS(7, ((tR >> 16) ^ k) & 63); tR = L; L = R; R = tR; if(decrypt) ksp--; else ksp++; } while(--i > 0); { register word32 t; #ifdef CRAY # define FP(k) (wO_L4[ (L >> (k)) & 15 ] << 1 | wO_L4[ (R >> (k)) & 15 ]) #else # define FP(k) (ap[ (L >> (k)) & 15 ] << 1 | ap[ (R >> (k)) & 15 ]) ap = wO_L4; #endif t = FP(0) | (FP(8) | (FP(16) | (FP(24) << 2)) << 2) << 2; R = FP(4) | (FP(12) | (FP(20) | (FP(28) << 2)) << 2) << 2; L = t; } { register word32 t; register char *bp; bp = &block[7]; t = R; *bp = t & 255; *--bp = (t >>= 8) & 255; *--bp = (t >>= 8) & 255; *--bp = (t >> 8) & 255; t = L; *--bp = t & 255; *--bp = (t >>= 8) & 255; *--bp = (t >>= 8) & 255; *--bp = (t >> 8) & 255; } } md5-1.3/src/des56.def000066400000000000000000000001101345542152300142330ustar00rootroot00000000000000LIBRARY des56.dll DESCRIPTION "DES56" VERSION 1.3 EXPORTS luaopen_des56 md5-1.3/src/des56.h000077500000000000000000000055201345542152300137410ustar00rootroot00000000000000#ifndef DES56_H #define DES56_H 1 /* * Fast implementation of the DES, as described in the Federal Register, * Vol. 40, No. 52, p. 12134, March 17, 1975. * * Stuart Levy, Minnesota Supercomputer Center, April 1988. * Currently (2007) slevy@ncsa.uiuc.edu * NCSA, University of Illinois Urbana-Champaign * * Calling sequence: * * typedef unsigned long keysched[32]; * * fsetkey(key, keysched) / * Converts a DES key to a "key schedule" * / * unsigned char key[8]; * keysched *ks; * * fencrypt(block, decrypt, keysched) / * En/decrypts one 64-bit block * / * unsigned char block[8]; / * data, en/decrypted in place * / * int decrypt; / * 0=>encrypt, 1=>decrypt * / * keysched *ks; / * key schedule, as set by fsetkey * / * * Key and data block representation: * The 56-bit key (bits 1..64 including "parity" bits 8, 16, 24, ..., 64) * and the 64-bit data block (bits 1..64) * are each stored in arrays of 8 bytes. * Following the NBS numbering, the MSB has the bit number 1, so * key[0] = 128*bit1 + 64*bit2 + ... + 1*bit8, ... through * key[7] = 128*bit57 + 64*bit58 + ... + 1*bit64. * In the key, "parity" bits are not checked; their values are ignored. * */ /* =============================================================================== License des56.c is licensed under the terms of the MIT license reproduced below. This means that des56.c is free software and can be used for both academic and commercial purposes at absolutely no cost. =============================================================================== Copyright (C) 1988 Stuart Levy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ typedef unsigned long word32; typedef unsigned char tiny; typedef struct keysched { struct keystage { word32 h, l; } KS[16]; } keysched; extern void fsetkey(char key[8], keysched *ks); extern void fencrypt(char block[8], int decrypt, keysched *ks); #endif /*DES56_H*/ md5-1.3/src/ldes56.c000066400000000000000000000064111345542152300141050ustar00rootroot00000000000000#include #include #include "des56.h" #include "lua.h" #include "lauxlib.h" #include "compat-5.2.h" #include "ldes56.h" static int des56_decrypt( lua_State *L ) { char* decypheredText; keysched KS; int rel_index, abs_index; size_t cypherlen; const char *cypheredText = luaL_checklstring( L, 1, &cypherlen ); const char *key = luaL_optstring( L, 2, NULL ); int padinfo; padinfo = cypheredText[cypherlen-1]; cypherlen--; /* Aloca array */ decypheredText = (char *) malloc( (cypherlen+1) * sizeof(char)); if(decypheredText == NULL) { lua_pushstring(L, "Error decrypting file. Not enough memory."); lua_error(L); } /* Inicia decifragem */ if (key && strlen(key) >= 8) { char k[8]; int i; for (i=0; i<8; i++) k[i] = (unsigned char)key[i]; fsetkey(k, &KS); } else { lua_pushstring(L, "Error decrypting file. Invalid key."); lua_error(L); } rel_index = 0; abs_index = 0; while (abs_index < (int) cypherlen) { decypheredText[abs_index] = cypheredText[abs_index]; abs_index++; rel_index++; if( rel_index == 8 ) { rel_index = 0; fencrypt(&(decypheredText[abs_index - 8]), 1, &KS); } } decypheredText[abs_index] = 0; lua_pushlstring(L, decypheredText, (abs_index-padinfo)); free( decypheredText ); return 1; } static int des56_crypt( lua_State *L ) { char *cypheredText; keysched KS; int rel_index, pad, abs_index; size_t plainlen; const char *plainText = luaL_checklstring( L, 1, &plainlen ); const char *key = luaL_optstring( L, 2, NULL ); cypheredText = (char *) malloc( (plainlen+8) * sizeof(char)); if(cypheredText == NULL) { lua_pushstring(L, "Error encrypting file. Not enough memory."); lua_error(L); } if (key && strlen(key) >= 8) { char k[8]; int i; for (i=0; i<8; i++) k[i] = (unsigned char)key[i]; fsetkey(k, &KS); } else { lua_pushstring(L, "Error encrypting file. Invalid key."); lua_error(L); } rel_index = 0; abs_index = 0; while (abs_index < (int) plainlen) { cypheredText[abs_index] = plainText[abs_index]; abs_index++; rel_index++; if( rel_index == 8 ) { rel_index = 0; fencrypt(&(cypheredText[abs_index - 8]), 0, &KS); } } pad = 0; if(rel_index != 0) { /* Pads remaining bytes with zeroes */ while(rel_index < 8) { pad++; cypheredText[abs_index++] = 0; rel_index++; } fencrypt(&(cypheredText[abs_index - 8]), 0, &KS); } cypheredText[abs_index] = pad; lua_pushlstring( L, cypheredText, abs_index+1 ); free( cypheredText ); return 1; } /* ** Assumes the table is on top of the stack. */ static void set_info (lua_State *L) { lua_pushliteral (L, "_COPYRIGHT"); lua_pushliteral (L, "Copyright (C) 2007-2019 PUC-Rio"); lua_settable (L, -3); lua_pushliteral (L, "_DESCRIPTION"); lua_pushliteral (L, "DES 56 cryptographic facilities for Lua"); lua_settable (L, -3); lua_pushliteral (L, "_VERSION"); lua_pushliteral (L, "DES56 1.3"); lua_settable (L, -3); } static const struct luaL_Reg des56lib[] = { {"crypt", des56_crypt}, {"decrypt", des56_decrypt}, {NULL, NULL}, }; int luaopen_des56 (lua_State *L) { lua_newtable(L); luaL_setfuncs(L, des56lib, 0); set_info (L); return 1; } md5-1.3/src/ldes56.h000077500000000000000000000000421345542152300141070ustar00rootroot00000000000000int luaopen_des56 (lua_State *L); md5-1.3/src/md5.c000077500000000000000000000173471345542152300135050ustar00rootroot00000000000000/** * $Id: md5.c,v 1.2 2008/03/24 20:59:12 mascarenhas Exp $ * Hash function MD5 * @author Marcela Ozorio Suarez, Roberto I. */ #include #include #include #include #include "md5.h" #define WORD 32 #define MASK 0xFFFFFFFF /** * md5 hash function. * @param message: aribtary string. * @param len: message length. * @param output: buffer to receive the hash value. Its size must be * (at least) HASHSIZE. */ void md5 (const char *message, size_t len, char output[HASHSIZE]); /** * init a new md5 calculate context * @param m a uninitialized md5_t type context */ void md5_init (md5_t *m); /** * update message to md5 context * @param m a initialized md5_t type context * @param message aribtary string * @param len message length * @return true if update completed, means len is not the multiples of 64. * false if you can continue update. */ int md5_update (md5_t *m, const char *message, size_t len); /** * finish md5 calculate. * @param m a md5_type context which previous md5_update on it return true. * @param output buffer to receive the hash value. its size must be * (at least) HASHSIZE. */ void md5_finish (md5_t *m, char output[HASHSIZE]); /* ** Realiza a rotacao no sentido horario dos bits da variavel 'D' do tipo WORD32. ** Os bits sao deslocados de 'num' posicoes */ #define rotate(D, num) (D<>(WORD-num)) /*Macros que definem operacoes relizadas pelo algoritmo md5 */ #define F(x, y, z) (((x) & (y)) | ((~(x)) & (z))) #define G(x, y, z) (((x) & (z)) | ((y) & (~(z)))) #define H(x, y, z) ((x) ^ (y) ^ (z)) #define I(x, y, z) ((y) ^ ((x) | (~(z)))) /*vetor de numeros utilizados pelo algoritmo md5 para embaralhar bits */ static const WORD32 T[64]={ 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 }; static void word32tobytes (const WORD32 *input, char *output) { int j = 0; while (j<4*4) { WORD32 v = *input++; output[j++] = (char)(v & 0xff); v >>= 8; output[j++] = (char)(v & 0xff); v >>= 8; output[j++] = (char)(v & 0xff); v >>= 8; output[j++] = (char)(v & 0xff); } } static void inic_digest(WORD32 *d) { d[0] = 0x67452301; d[1] = 0xEFCDAB89; d[2] = 0x98BADCFE; d[3] = 0x10325476; } /*funcao que implemeta os quatro passos principais do algoritmo MD5 */ static void digest(const WORD32 *m, WORD32 *d) { int j; /*MD5 PASSO1 */ for (j=0; j<4*4; j+=4) { d[0] = d[0]+ F(d[1], d[2], d[3])+ m[j] + T[j]; d[0]=rotate(d[0], 7); d[0]+=d[1]; d[3] = d[3]+ F(d[0], d[1], d[2])+ m[(j)+1] + T[j+1]; d[3]=rotate(d[3], 12); d[3]+=d[0]; d[2] = d[2]+ F(d[3], d[0], d[1])+ m[(j)+2] + T[j+2]; d[2]=rotate(d[2], 17); d[2]+=d[3]; d[1] = d[1]+ F(d[2], d[3], d[0])+ m[(j)+3] + T[j+3]; d[1]=rotate(d[1], 22); d[1]+=d[2]; } /*MD5 PASSO2 */ for (j=0; j<4*4; j+=4) { d[0] = d[0]+ G(d[1], d[2], d[3])+ m[(5*j+1)&0x0f] + T[(j-1)+17]; d[0] = rotate(d[0],5); d[0]+=d[1]; d[3] = d[3]+ G(d[0], d[1], d[2])+ m[((5*(j+1)+1)&0x0f)] + T[(j+0)+17]; d[3] = rotate(d[3], 9); d[3]+=d[0]; d[2] = d[2]+ G(d[3], d[0], d[1])+ m[((5*(j+2)+1)&0x0f)] + T[(j+1)+17]; d[2] = rotate(d[2], 14); d[2]+=d[3]; d[1] = d[1]+ G(d[2], d[3], d[0])+ m[((5*(j+3)+1)&0x0f)] + T[(j+2)+17]; d[1] = rotate(d[1], 20); d[1]+=d[2]; } /*MD5 PASSO3 */ for (j=0; j<4*4; j+=4) { d[0] = d[0]+ H(d[1], d[2], d[3])+ m[(3*j+5)&0x0f] + T[(j-1)+33]; d[0] = rotate(d[0], 4); d[0]+=d[1]; d[3] = d[3]+ H(d[0], d[1], d[2])+ m[(3*(j+1)+5)&0x0f] + T[(j+0)+33]; d[3] = rotate(d[3], 11); d[3]+=d[0]; d[2] = d[2]+ H(d[3], d[0], d[1])+ m[(3*(j+2)+5)&0x0f] + T[(j+1)+33]; d[2] = rotate(d[2], 16); d[2]+=d[3]; d[1] = d[1]+ H(d[2], d[3], d[0])+ m[(3*(j+3)+5)&0x0f] + T[(j+2)+33]; d[1] = rotate(d[1], 23); d[1]+=d[2]; } /*MD5 PASSO4 */ for (j=0; j<4*4; j+=4) { d[0] = d[0]+ I(d[1], d[2], d[3])+ m[(7*j)&0x0f] + T[(j-1)+49]; d[0] = rotate(d[0], 6); d[0]+=d[1]; d[3] = d[3]+ I(d[0], d[1], d[2])+ m[(7*(j+1))&0x0f] + T[(j+0)+49]; d[3] = rotate(d[3], 10); d[3]+=d[0]; d[2] = d[2]+ I(d[3], d[0], d[1])+ m[(7*(j+2))&0x0f] + T[(j+1)+49]; d[2] = rotate(d[2], 15); d[2]+=d[3]; d[1] = d[1]+ I(d[2], d[3], d[0])+ m[(7*(j+3))&0x0f] + T[(j+2)+49]; d[1] = rotate(d[1], 21); d[1]+=d[2]; } } static void bytestoword32 (WORD32 *x, const char *pt) { int i; for (i=0; i<16; i++) { int j=i*4; x[i] = (((WORD32)(unsigned char)pt[j+3] << 8 | (WORD32)(unsigned char)pt[j+2]) << 8 | (WORD32)(unsigned char)pt[j+1]) << 8 | (WORD32)(unsigned char)pt[j]; } } static void put_length(WORD32 *x, long len) { /* in bits! */ x[14] = (WORD32)((len<<3) & MASK); x[15] = (WORD32)(len>>(32-3) & 0x7); } /* ** returned status: * 0 - normal message (full 64 bytes) * 1 - enough room for 0x80, but not for message length (two 4-byte words) * 2 - enough room for 0x80 plus message length (at least 9 bytes free) */ static int converte (WORD32 *x, const char *pt, int num, int old_status) { int new_status = 0; char buff[64]; if (num<64) { memcpy(buff, pt, num); /* to avoid changing original string */ memset(buff+num, 0, 64-num); if (old_status == 0) buff[num] = '\200'; new_status = 1; pt = buff; } bytestoword32(x, pt); if (num <= (64 - 9)) new_status = 2; return new_status; } void md5 (const char *message, size_t len, char output[HASHSIZE]) { WORD32 d[4]; int status = 0; long i = 0; inic_digest(d); while (status != 2) { WORD32 d_old[4]; WORD32 wbuff[16]; int numbytes = (len-i >= 64) ? 64 : len-i; /*salva os valores do vetor digest*/ d_old[0]=d[0]; d_old[1]=d[1]; d_old[2]=d[2]; d_old[3]=d[3]; status = converte(wbuff, message+i, numbytes, status); if (status == 2) put_length(wbuff, len); digest(wbuff, d); d[0]+=d_old[0]; d[1]+=d_old[1]; d[2]+=d_old[2]; d[3]+=d_old[3]; i += numbytes; } word32tobytes(d, output); } void md5_init(md5_t *m) { inic_digest(m->d); m->len = 0; } int md5_update(md5_t *m, const char *message, size_t len) { WORD32 *d = m->d; size_t addlen = m->len; int status = 0, i = 0; while (status != 2) { WORD32 d_old[4]; WORD32 wbuff[16]; int numbytes = (len-i >= 64) ? 64 : len-i; if (status != 1 && numbytes == 0 && len != 0) break; /*salva os valores do vetor digest*/ d_old[0]=d[0]; d_old[1]=d[1]; d_old[2]=d[2]; d_old[3]=d[3]; status = converte(wbuff, message+i, numbytes, status); if (status == 2) put_length(wbuff, addlen+len); digest(wbuff, d); d[0]+=d_old[0]; d[1]+=d_old[1]; d[2]+=d_old[2]; d[3]+=d_old[3]; i += numbytes; } m->len += len; return status == 2; } void md5_finish (md5_t *m, char output[HASHSIZE]) { word32tobytes(m->d, output); } md5-1.3/src/md5.def000066400000000000000000000001131345542152300137750ustar00rootroot00000000000000LIBRARY core.dll DESCRIPTION "LuaMD5" VERSION 1.3 EXPORTS luaopen_md5_core md5-1.3/src/md5.h000077500000000000000000000014501345542152300134760ustar00rootroot00000000000000/** * $Id: md5.h,v 1.2 2006/03/03 15:04:49 tomas Exp $ * Cryptographic module for Lua. * @author Roberto Ierusalimschy */ #ifndef md5_h #define md5_h #include #include #define HASHSIZE 16 #if __STDC_VERSION__ >= 199901L #include typedef uint32_t WORD32; #else /* static assert that int equal or greater than 32bit. */ typedef char static_assert_sizeof_int [sizeof(unsigned int) >= 4 ? 1 : -1]; typedef unsigned int WORD32; #endif typedef struct md5_t { WORD32 d[4]; size_t len; } md5_t; void md5_init (md5_t *m); int md5_update (md5_t *m, const char *message, size_t len); void md5_finish (md5_t *m, char output[HASHSIZE]); void md5 (const char *message, size_t len, char output[HASHSIZE]); LUALIB_API int luaopen_md5_core (lua_State *L); #endif md5-1.3/src/md5.lua000066400000000000000000000015041345542152300140250ustar00rootroot00000000000000---------------------------------------------------------------------------- -- $Id: md5.lua,v 1.4 2006/08/21 19:24:21 carregal Exp $ ---------------------------------------------------------------------------- local core local string = string or require"string" if string.find(_VERSION, "Lua 5.0") then local cpath = os.getenv"LUA_CPATH" or "/usr/local/lib/lua/5.0/" core = loadlib(cpath.."md5/core.so", "luaopen_md5_core")() else core = require"md5.core" end ---------------------------------------------------------------------------- -- @param k String with original message. -- @return String with the md5 hash value converted to hexadecimal digits function core.sumhexa (k) k = core.sum(k) return (string.gsub(k, ".", function (c) return string.format("%02x", string.byte(c)) end)) end return core md5-1.3/src/md5lib.c000066400000000000000000000123241345542152300141570ustar00rootroot00000000000000/** * $Id: md5lib.c,v 1.10 2008/05/12 20:51:27 carregal Exp $ * Cryptographic and Hash functions for Lua * @author Roberto Ierusalimschy */ #include #include #include #include #include #include "md5.h" #include "compat-5.2.h" /** * Hash function. Returns a hash for a given string. * @param message: arbitrary binary string. * @return A 128-bit hash string. */ static int lmd5 (lua_State *L) { char buff[16]; size_t l; const char *message = luaL_checklstring(L, 1, &l); md5(message, l, buff); lua_pushlstring(L, buff, 16L); return 1; } /** * X-Or. Does a bit-a-bit exclusive-or of two strings. * @param s1: arbitrary binary string. * @param s2: arbitrary binary string with same length as s1. * @return a binary string with same length as s1 and s2, * where each bit is the exclusive-or of the corresponding bits in s1-s2. */ static int ex_or (lua_State *L) { size_t l1, l2; const char *s1 = luaL_checklstring(L, 1, &l1); const char *s2 = luaL_checklstring(L, 2, &l2); luaL_Buffer b; luaL_argcheck( L, l1 == l2, 2, "lengths must be equal" ); luaL_buffinit(L, &b); while (l1--) luaL_addchar(&b, (*s1++)^(*s2++)); luaL_pushresult(&b); return 1; } static void checkseed (lua_State *L) { if (lua_isnone(L, 3)) { /* no seed? */ time_t tm = time(NULL); /* for `random' seed */ lua_pushlstring(L, (char *)&tm, sizeof(tm)); } } #define MAXKEY 256 #define BLOCKSIZE 16 static int initblock (lua_State *L, const char *seed, int lseed, char *block) { size_t lkey; const char *key = luaL_checklstring(L, 2, &lkey); if (lkey > MAXKEY) luaL_error(L, "key too long (> %d)", MAXKEY); memset(block, 0, BLOCKSIZE); memcpy(block, seed, lseed); memcpy(block+BLOCKSIZE, key, lkey); return (int)lkey+BLOCKSIZE; } static void codestream (lua_State *L, const char *msg, size_t lmsg, char *block, int lblock) { luaL_Buffer b; luaL_buffinit(L, &b); while (lmsg > 0) { char code[BLOCKSIZE]; int i; md5(block, lblock, code); for (i=0; i 0; i++, lmsg--) code[i] ^= *msg++; luaL_addlstring(&b, code, i); memcpy(block, code, i); /* update seed */ } luaL_pushresult(&b); } static void decodestream (lua_State *L, const char *cypher, size_t lcypher, char *block, int lblock) { luaL_Buffer b; luaL_buffinit(L, &b); while (lcypher > 0) { char code[BLOCKSIZE]; int i; md5(block, lblock, code); /* update seed */ for (i=0; i 0; i++, lcypher--) code[i] ^= *cypher++; luaL_addlstring(&b, code, i); memcpy(block, cypher-i, i); } luaL_pushresult(&b); } /** * Encrypts a string. Uses the hash function md5 in CFB (Cipher-feedback * mode). * @param message: arbitrary binary string to be encrypted. * @param key: arbitrary binary string to be used as a key. * @param [seed]: optional arbitrary binary string to be used as a seed. * if no seed is provided, the function uses the result of * time() as a seed. * @return The cyphertext (as a binary string). */ static int crypt (lua_State *L) { size_t lmsg; const char *msg = luaL_checklstring(L, 1, &lmsg); size_t lseed; const char *seed; int lblock; char block[BLOCKSIZE+MAXKEY]; checkseed(L); seed = luaL_checklstring(L, 3, &lseed); if (lseed > BLOCKSIZE) luaL_error(L, "seed too long (> %d)", BLOCKSIZE); /* put seed and seed length at the beginning of result */ block[0] = (char)lseed; memcpy(block+1, seed, lseed); lua_pushlstring(L, block, lseed+1); /* to concat with result */ lblock = initblock(L, seed, lseed, block); codestream(L, msg, lmsg, block, lblock); lua_concat(L, 2); return 1; } /** * Decrypts a string. For any message, key, and seed, we have that * decrypt(crypt(msg, key, seed), key) == msg. * @param cyphertext: message to be decrypted (this must be the result of a previous call to crypt. * @param key: arbitrary binary string to be used as a key. * @return The plaintext. */ static int decrypt (lua_State *L) { size_t lcyphertext; const char *cyphertext = luaL_checklstring(L, 1, &lcyphertext); size_t lseed = cyphertext[0]; const char *seed = cyphertext+1; int lblock; char block[BLOCKSIZE+MAXKEY]; luaL_argcheck(L, lcyphertext >= lseed+1 && lseed <= BLOCKSIZE, 1, "invalid cyphered string"); cyphertext += lseed+1; lcyphertext -= lseed+1; lblock = initblock(L, seed, lseed, block); decodestream(L, cyphertext, lcyphertext, block, lblock); return 1; } /* ** Assumes the table is on top of the stack. */ static void set_info (lua_State *L) { lua_pushliteral (L, "_COPYRIGHT"); lua_pushliteral (L, "Copyright (C) 2003-2019 PUC-Rio"); lua_settable (L, -3); lua_pushliteral (L, "_DESCRIPTION"); lua_pushliteral (L, "Checksum facilities for Lua"); lua_settable (L, -3); lua_pushliteral (L, "_VERSION"); lua_pushliteral (L, "MD5 1.3"); lua_settable (L, -3); } static struct luaL_Reg md5lib[] = { {"sum", lmd5}, {"exor", ex_or}, {"crypt", crypt}, {"decrypt", decrypt}, {NULL, NULL} }; int luaopen_md5_core (lua_State *L) { lua_newtable(L); luaL_setfuncs(L, md5lib, 0); set_info (L); return 1; } md5-1.3/tests/000077500000000000000000000000001345542152300132105ustar00rootroot00000000000000md5-1.3/tests/DES56/000077500000000000000000000000001345542152300137765ustar00rootroot00000000000000md5-1.3/tests/DES56/Makefile000077500000000000000000000011071345542152300154400ustar00rootroot00000000000000 CFLAGS= -O6 -DUSG=0 -DBSD=0 FILES= Makefile des56.h des56.c fdemo.c ftest.c all: demo test # demo -- evaluate, test and time DES, works even on non-UNIX systems demo: fdemo ./fdemo -demo -c 1000 fdemo: fdemo.o des56.o ${CC} -o $@ $@.o des56.o # test -- evaluates DES, compares with UNIX crypt test: ftest ./ftest 4869207468657265 a820105412de3991 77129bd8c052e7 ./ftest 1.21280ac -c 5000000 -k 1000000 -C 100000 -K 100000 ftest: ftest.o des56.o ${CC} -o $@ $@.o des56.o -lcrypt dist: @tar cvf - ${FILES} clean: rm -f clean core fdemo ftest fdemo.o ftest.o des56.o md5-1.3/tests/DES56/fdemo.c000077500000000000000000000101441345542152300152370ustar00rootroot00000000000000 /* * Fast DES evaluation & benchmarking, not dependent on UNIX */ #include #include #include #include "des56.h" typedef struct { char b[8]; } chunk; char *wprint(chunk *v) { static char s[18]; register int i; register char *p; p = s; for(i = 0; i < 8; i++) { sprintf(p, "%02x", v->b[i] & 0xff); p += 2; if(i == 4-1) *p++ = '.'; } return(s); } void getv(char *s, chunk *v) { register int i, t; if(s[0] == '0' && s[1] == 'x') s += 2; /* Ignore C-style 0x prefix */ for(i = 0; i < 8; i++) { t = 0; if(*s >= '0' && *s <= '9') t = *s++ - '0'; else if(*s >= 'a' && *s <= 'f') t = *s++ - 'a' + 10; else if(*s >= 'A' && *s <= 'F') t = *s++ - 'A' + 10; t <<= 4; if(*s >= '0' && *s <= '9') t |= *s++ - '0'; else if(*s >= 'a' && *s <= 'f') t |= *s++ - 'a' + 10; else if(*s >= 'A' && *s <= 'F') t |= *s++ - 'A' + 10; v->b[i] = t; if(*s == '.' && i == 3) { s++; } } } struct demo { int decrypt; char *key, *data; char *expect; }; struct demo demos[] = { { 0, "00000000.00000000", "00000000.00000000", "8ca64de9.c1b123a7" }, { 0, "11111111.11111111", "00000000.00000000", "82e13665.b4624df5" }, { 0, "12486248.62486248", "f0e1d2c3.b4a59687", "f4682865.376f93ea" }, { 1, "12486248.62486248", "f0e1d2c3.b4a59687", "df597e0f.84fd994f" }, { 0, "1bac8107.6a39042d", "812801da.cbe98103", "d8883b2c.4a7c61dd" }, { 1, "1bac8107.6a39042d", "d8883b2c.4a7c61dd", "812801da.cbe98103" }, { 1, "fedcba98.76543210", "a68cdca9.0c9021f9", "00000000.00000000" }, { 0, "eca86420.13579bdf", "01234567.89abcdef", "a8418a54.ff97a505" }, }; void fdemo(void) { keysched KS; register struct demo *dp; chunk key, data; char *got; printf("\ Op( Key, Data to En/Decrypt) = Computed ?= Expected\n"); for(dp = demos; dp < &demos[sizeof(demos)/sizeof(*demos)]; dp++) { getv(dp->key, &key); fsetkey(key.b, &KS); getv(dp->data, &data); fencrypt(data.b, dp->decrypt, &KS); got = wprint(&data); printf("%c(%s, %s) = %s %c %s\n", "ED"[dp->decrypt], dp->key, dp->data, got, strcmp(got, dp->expect) ? '!' : '=', dp->expect); } } int main(int argc, char *argv[]) { register int i; chunk key, olddata, newdata; int decrypt = 0; int first = 1; keysched KS; if(argc <= 1) { usage: printf("\ Usage: fdemo key [ -demo ] [ -{ck} N ] [ [-{ed}] data ... ]\n\ Demonstrate and/or time fast DES routines.\n\ ``key'' and ``data'' are left-justified, 0-padded hex values <= 16 digits\n\ optionally with a `.' at the 32-bit point\n\ -demo tries a batch of values, compares fencrypt() with precomputed result\n\ -c N encrypt N times using fast DES\n\ -k N set-key N times using fast DES\n\ -e encrypt following data value(s) (default)\n\ -d decrypt following data value(s)\n"); return(1); } for(i = 1; i < argc; i++) { if(argv[i][0] == '-') { int count, n; char c, *op; /* Should use getopt but it might not be there */ c = argv[i][1]; if(c == 'e' || c == 'd') { if(strcmp(argv[i], "-demo") == 0) { fdemo(); } else { decrypt = (c == 'd'); } continue; } fsetkey(key.b, &KS); /* Make sure it's done at least once */ op = &argv[i][2]; if(*op == '\0') if((op = argv[++i]) == NULL) goto usage; count = atoi(op); if(count <= 0) count = 1; n = count; printf("Starting ...<\007"); fflush(stdout); switch(c) { case 'c': op = "fencrypt"; do fencrypt(newdata.b, 0, &KS); while(--n > 0); break; case 'k': op = "fsetkey"; do fsetkey(key.b, &KS); while(--n > 0); break; default: printf("Unknown option -%c\n", c); goto usage; } printf("\007> completed %d %s's\n", count, op); } else if(first) { getv(argv[1], &key); fsetkey(key.b, &KS); printf("key\t%s\n", wprint(&key)); first = 0; } else { getv(argv[i], &olddata); newdata = olddata; printf("%s %s => ", decrypt ? "decrypt" : "encrypt", wprint(&olddata)); fencrypt(newdata.b, decrypt, &KS); printf("%s %s => ", !decrypt ? "decrypt" : "encrypt", wprint(&newdata)); fencrypt(newdata.b, !decrypt, &KS); printf("%s\n", wprint(&newdata)); } } return(0); } md5-1.3/tests/DES56/ftest.c000077500000000000000000000107421345542152300152760ustar00rootroot00000000000000 /* * Fast DES evaluation & benchmarking for UNIX * Compares output of fencrypt()/fsetkey() with UNIX crypt()/encrypt()/setkey() * and measures speed using times(). */ #include #include #if USG #include #endif /*USG*/ #include /* for HZ */ #include #include #include "des56.h" typedef struct { char b[8]; } chunk; #ifdef CRAY # define USG 1 #endif /*CRAY*/ #ifndef USG # define USG 0 #endif /*!USG*/ #if BSD >= 43 && defined(vax) && !defined(HZ) # define HZ 100 #endif char * bprint(char b[64]) { static char s[17]; register int i; for(i = 0; i < 64; i += 4) sprintf(&s[i/4], "%1x", b[i]<<3 | b[i+1]<<2 | b[i+2]<<1 | b[i+3]); return(s); } char * wprint(chunk *v) { static char s[17]; register int i; for(i = 0; i < 8; i++) sprintf(&s[2*i], "%02x", v->b[i] & 0xff); return(s); } void getv(char *s, chunk *v) { register int i, t; if(s[0] == '0' && s[1] == 'x') s += 2; for(i = 0; i < 8; i++) { t = 0; if(*s >= '0' && *s <= '9') t = *s++ - '0'; else if(*s >= 'a' && *s <= 'f') t = *s++ - 'a' + 10; else if(*s >= 'A' && *s <= 'F') t = *s++ - 'A' + 10; t <<= 4; if(*s >= '0' && *s <= '9') t |= *s++ - '0'; else if(*s >= 'a' && *s <= 'f') t |= *s++ - 'a' + 10; else if(*s >= 'A' && *s <= 'F') t |= *s++ - 'A' + 10; v->b[i] = t; if(*s == '.') { s++; i = 4-1; } } } void expand(chunk *v, char bits[64]) { register unsigned int i; for(i = 0; i < 64; i++) bits[i] = (v->b[i/8] >> (7 - i%8)) & 1; } int main(int argc, char *argv[]) { register int i; chunk key, olddata, newdata; char bkey[64], bdata[64]; int decrypt = 0; keysched KS; if(argc < 2 || argv[1][0] == '-') { usage: printf("\ Usage: %s key [ -{ckCK} count ] [ data ... ]\n\ Demonstrate and/or time fast DES routines.\n\ ``key'' and ``data'' are left-justified, 0-padded hex values <= 16 digits\n\ By default, encrypts & decrypts each 'data' block with both fastdes and\n\ crypt() library DES routines to show equality.\n\ -c N encrypt N times using fast DES\n\ -k N set-key N times using fast DES\n\ -C N encrypt N times using library DES\n\ %s", argv[0], USG ? "" : "\ -K N set-key N times using library DES\n"); exit(1); } getv(argv[1], &key); fsetkey(key.b, &KS); expand(&key, bkey); #if USG /* System V systems don't seem to have setkey, just crypt * so we use that to set the key. */ for(i = 0; i < 8; i++) bdata[i] = (key.b[i] >> 1) | 0x80; bdata[8] = '\0'; crypt((char *)bdata, ".."); /* Key, no salt */ #else /*!USG*/ setkey(bkey); #endif /*!USG*/ printf("key\t%s\n", bprint(bkey)); for(i = 2; i < argc; i++) { if(argv[i][0] == '-') { int count, n; char c, *op; struct tms now, then; c = argv[i][1]; op = &argv[i][2]; if(*op == '\0') if((op = argv[++i]) == NULL) goto usage; count = atoi(op); if(count <= 0) count = 1; n = count; expand(&olddata, bdata); times(&now); switch(c) { case 'c': op = "fencrypt"; do fencrypt(newdata.b, 0, &KS); while(--n > 0); break; case 'k': op = "fsetkey"; do fsetkey(key.b, &KS); while(--n > 0); break; case 'C': op = "library encrypt"; do encrypt(bdata, decrypt); while(--n > 0); break; case 'K': #if USG printf("UNIX library has no setkey() function on this system\n"); continue; #else /*!USG*/ op = "library setkey"; do setkey(bkey); while(--n > 0); break; #endif /*!USG*/ default: printf("Unknown option -%c\n", c); goto usage; } times(&then); n = then.tms_utime - now.tms_utime; printf("%d %s's in %0.2f seconds (%.2f us apiece)\n", count, op, (float) n / HZ, (1.0e6 * n / (HZ * count))); } else { /* Demonstrate that it works for a particular data block. * To compare with UNIX encrypt(), we must play its game. * On BSD systems, encrypt(block, 1) is the inverse of (..., 0) * but on USG systems the second parameter is ignored and * encrypt(block, x) always encrypts. */ getv(argv[i], &olddata); newdata = olddata; printf("\tOriginal data\t\tEncrypted\t\t%s\n", USG ? "Encrypted again" : "Decrypted"); printf("fastdes\t%s", wprint(&olddata)); fencrypt(newdata.b, 0, &KS); printf("\t%s", wprint(&newdata)); fencrypt(newdata.b, USG ? 0 : 1, &KS); printf("\t%s\n", wprint(&newdata)); expand(&olddata, bdata); printf("UNIXdes\t%s", bprint(bdata)); encrypt(bdata, 0); printf("\t%s", bprint(bdata)); encrypt(bdata, USG ? 0 : 1); printf("\t%s\n", bprint(bdata)); } } exit(0); } md5-1.3/tests/test.lua000066400000000000000000000074271345542152300147040ustar00rootroot00000000000000#!/usr/local/bin/lua50 -- Testing MD5 if string.find(_VERSION, "Lua 5.0") then LUA_PATH = "?;?.lua;/usr/local/share/lua/5.0/?.lua" string.gmatch = string.gfind end local md5 = require"md5" io.write (md5._VERSION..' '.._VERSION..'\n') assert(md5.exor('', '') == '') assert(md5.exor('alo alo', '\0\0\0\0\0\0\0') == 'alo alo') local function invert (s) return md5.exor(s, string.rep('\255', string.len(s))) end x = string.rep('0123456789', 1000) assert(md5.exor(x,x) == string.rep('\0', 10000)) assert(md5.exor(x,invert(x)) == string.rep('\255', 10000)) assert(invert(invert('alo alo')) == 'alo alo') assert(invert(invert(invert('alo\0\255alo'))) == invert('alo\0\255alo')) -- test some known sums assert(md5.sumhexa("") == "d41d8cd98f00b204e9800998ecf8427e") assert(md5.sumhexa("a") == "0cc175b9c0f1b6a831c399e269772661") assert(md5.sumhexa("abc") == "900150983cd24fb0d6963f7d28e17f72") assert(md5.sumhexa("message digest") == "f96b697d7cb7938d525a2f31aaf161d0") assert(md5.sumhexa("abcdefghijklmnopqrstuvwxyz") == "c3fcd3d76192e4007dfb496cca67e13b") assert(md5.sumhexa("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") == "d174ab98d277d9f5a5611c2c9f419d9f") -- test padding borders assert(md5.sumhexa(string.rep('a',53)) == "e9e7e260dce84ffa6e0e7eb5fd9d37fc") assert(md5.sumhexa(string.rep('a',54)) == "eced9e0b81ef2bba605cbc5e2e76a1d0") assert(md5.sumhexa(string.rep('a',55)) == "ef1772b6dff9a122358552954ad0df65") assert(md5.sumhexa(string.rep('a',56)) == "3b0c8ac703f828b04c6c197006d17218") assert(md5.sumhexa(string.rep('a',57)) == "652b906d60af96844ebd21b674f35e93") assert(md5.sumhexa(string.rep('a',63)) == "b06521f39153d618550606be297466d5") assert(md5.sumhexa(string.rep('a',64)) == "014842d480b571495a4a0363793f7367") assert(md5.sumhexa(string.rep('a',65)) == "c743a45e0d2e6a95cb859adae0248435") assert(md5.sumhexa(string.rep('a',255)) == "46bc249a5a8fc5d622cf12c42c463ae0") assert(md5.sumhexa(string.rep('a',256)) == "81109eec5aa1a284fb5327b10e9c16b9") assert(md5.sumhexa( "12345678901234567890123456789012345678901234567890123456789012345678901234567890") == "57edf4a22be3c955ac49da2e2107b67a") io.write '+' local tolerance = 1.12 local function contchars (s) local a = {} for i=0,255 do a[string.char(i)] = 0 end for c in string.gmatch(s, '.') do a[c] = a[c] + 1 end local av = string.len(s)/256 for i=0,255 do local c = string.char(i) assert(a[c] < av*tolerance and a[c] > av/tolerance, i) end end local key = 'xuxu bacana' assert(md5.decrypt(md5.crypt('', key), key) == '') assert(md5.decrypt(md5.crypt('', key, '\0\0seed\0'), key) == '') assert(md5.decrypt(md5.crypt('a', key), key) == 'a') local msg = string.rep("1233456789\0\1\2\3\0\255", 10000) local code = md5.crypt(msg, key, "seed") assert(md5.decrypt(code, key) == msg) contchars(code) local seed = 'some_seed' assert(md5.crypt('a','a',seed) ~= md5.crypt('a','b',seed)) io.write'+' -- Testing DES 56 local des56 if string.find(_VERSION, "Lua 5.0") then local cpath = os.getenv"LUA_CPATH" or "/usr/local/lib/lua/5.0/" des56 = loadlib(cpath.."des56.so", "luaopen_des56")() else des56 = require 'des56' end local key = '&3g4&gs*&3' assert(des56.decrypt(des56.crypt('', key), key) == '') assert(des56.decrypt(des56.crypt('', key), key) == '') assert(des56.decrypt(des56.crypt('a', key), key) == 'a') assert(des56.decrypt(des56.crypt('1234567890', key), key) == '1234567890') local msg = string.rep("1233456789\0\1\2\3\0\255", 10000) local code = des56.crypt(msg, key) assert(des56.decrypt(code, key) == msg) assert(des56.crypt('a', '12345678') ~= des56.crypt('a', '87654321')) local ascii = "" for i = 0, 255 do ascii = ascii..string.char(i) end assert(des56.decrypt(des56.crypt(ascii, key), key) == ascii) key = string.sub(ascii, 2) assert(des56.decrypt(des56.crypt(ascii, key), key) == ascii) io.write'+\n' md5-1.3/vc6/000077500000000000000000000000001345542152300125445ustar00rootroot00000000000000md5-1.3/vc6/md5.def000077500000000000000000000001101345542152300137040ustar00rootroot00000000000000LIBRARY core.dll DESCRIPTION "LuaMD5" VERSION 1.0.1 EXPORTS luaopen_md5 md5-1.3/vc6/md5.dsw000077500000000000000000000011051345542152300137500ustar00rootroot00000000000000Microsoft Developer Studio Workspace File, Format Version 6.00 # WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE! ############################################################################### Project: "md5_dll"=.\md5_dll.dsp - Package Owner=<4> Package=<5> {{{ begin source code control md5 .. end source code control }}} Package=<4> {{{ }}} ############################################################################### Global: Package=<5> {{{ }}} Package=<3> {{{ }}} ############################################################################### md5-1.3/vc6/md5_dll.dsp000077500000000000000000000111331345542152300145760ustar00rootroot00000000000000# Microsoft Developer Studio Project File - Name="md5_dll" - Package Owner=<4> # Microsoft Developer Studio Generated Build File, Format Version 6.00 # ** DO NOT EDIT ** # TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102 CFG=md5_dll - Win32 Debug !MESSAGE This is not a valid makefile. To build this project using NMAKE, !MESSAGE use the Export Makefile command and run !MESSAGE !MESSAGE NMAKE /f "md5_dll.mak". !MESSAGE !MESSAGE You can specify a configuration when running NMAKE !MESSAGE by defining the macro CFG on the command line. For example: !MESSAGE !MESSAGE NMAKE /f "md5_dll.mak" CFG="md5_dll - Win32 Debug" !MESSAGE !MESSAGE Possible choices for configuration are: !MESSAGE !MESSAGE "md5_dll - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library") !MESSAGE "md5_dll - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library") !MESSAGE # Begin Project # PROP AllowPerConfigDependencies 0 # PROP Scc_ProjName "md5_dll" # PROP Scc_LocalPath ".." CPP=cl.exe MTL=midl.exe RSC=rc.exe !IF "$(CFG)" == "md5_dll - Win32 Release" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 0 # PROP BASE Output_Dir "Release" # PROP BASE Intermediate_Dir "Release" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 0 # PROP Output_Dir "../lib/vc6" # PROP Intermediate_Dir "md5_dll/Release" # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "LUAFILESYSTEM_EXPORTS" /YX /FD /c # ADD CPP /nologo /MD /W3 /GX /O2 /I "../../external-src/lua50/include" /I "../../compat/src" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "LUAFILESYSTEM_EXPORTS" /YX /FD /c # ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32 # ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32 # ADD BASE RSC /l 0x416 /d "NDEBUG" # ADD RSC /l 0x416 /d "NDEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 # ADD LINK32 lua50.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 /out:"../bin/vc6/lib/md5.dll" /libpath:"../../external-src/lua50/lib/dll" # Begin Special Build Tool SOURCE="$(InputPath)" # End Special Build Tool !ELSEIF "$(CFG)" == "md5_dll - Win32 Debug" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 1 # PROP BASE Output_Dir "Debug" # PROP BASE Intermediate_Dir "Debug" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 1 # PROP Output_Dir "../lib/vc6" # PROP Intermediate_Dir "md5_dll/Debug" # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "LUAFILESYSTEM_EXPORTS" /YX /FD /GZ /c # ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "../../external-src/lua50/include" /I "../../compat/src" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "LUAFILESYSTEM_EXPORTS" /YX /FD /GZ /c # ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32 # ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32 # ADD BASE RSC /l 0x416 /d "_DEBUG" # ADD RSC /l 0x416 /d "_DEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept # ADD LINK32 lua50.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /out:"../bin/vc6/lib/md5/cored.dll" /pdbtype:sept /libpath:"../../external-src/lua50/lib/dll" !ENDIF # Begin Target # Name "md5_dll - Win32 Release" # Name "md5_dll - Win32 Debug" # Begin Group "Source Files" # PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" # Begin Source File SOURCE="..\..\compat\src\compat-5.1.c" # End Source File # Begin Source File SOURCE=..\src\md5.c # End Source File # Begin Source File SOURCE=.\md5.def # End Source File # End Group # Begin Group "Header Files" # PROP Default_Filter "h;hpp;hxx;hm;inl" # Begin Source File SOURCE="..\..\compat\src\compat-5.1.h" # End Source File # Begin Source File SOURCE=..\src\md5.h # End Source File # End Group # Begin Group "Resource Files" # PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" # End Group # End Target # End Project