Thanks to github.com/Jkinglyf
This commit is contained in:
Zhi Guan
2016-05-30 12:50:06 +02:00
parent ee4384daeb
commit 2bf25bd29f
55 changed files with 2044 additions and 1672 deletions

View File

@@ -13,12 +13,12 @@ AR= ar r
CFLAGS= $(INCLUDES) $(CFLAG)
GENERAL=Makefile
TEST=
TEST=ffxtest.c
APPS=
LIB=$(TOP)/libcrypto.a
LIBSRC=ffx.c
LIBOBJ=ffx.o
LIBSRC=ffx.c ffx_err.c
LIBOBJ=ffx.o ffx_err.o
SRC= $(LIBSRC)

View File

@@ -50,7 +50,7 @@
/*
* Format-Preserve Encryption
* implementation of NIST 800-38G FF1 schemes
*
*
* FPE is used to encrypt strings such as credit card numbers and phone numbers
* the ciphertext is still in valid format, for example:
* FPE_encrypt("13810631266") == "98723498792"
@@ -65,12 +65,12 @@
#include <inttypes.h>
#include <openssl/err.h>
#include <openssl/aes.h>
#include "ffx.h"
#include <openssl/ffx.h>
#define FFX_MIN_DIGITS 6
#define FFX_MAX_DIGITS 18
#define FFX_MIN_TWEAKLEN 4
#define FFX_MAX_TWEAKLEN 11
#define FFX_MAX_TWEAKLEN 11
#define FFX_NUM_ROUNDS 10
@@ -93,11 +93,11 @@ int FFX_init(FFX_CTX *ctx, int flag, const unsigned char *key, int keybits)
ctx->flag = flag;
if (AES_set_encrypt_key(key, keybits, &ctx->key) < 0) {
fprintf(stderr, "error: %s: %s: %d\n", __FUNCTION__, __FILE__, __LINE__);
return -1;
FFXerr(FFX_F_FFX_INIT, FFX_R_INIT_KEY_FAILED);
return 0;
}
return 0;
return 1;
}
void FFX_cleanup(FFX_CTX *ctx)
@@ -122,15 +122,15 @@ int FFX_encrypt(FFX_CTX *ctx, const char *in, size_t inlen,
assert(in);
assert(tweak);
if (inlen > strlen(in) ||
if (inlen > strlen(in) ||
inlen < FFX_MIN_DIGITS || inlen > FFX_MAX_DIGITS) {
fprintf(stderr, "%s: invalid digits length\n", __FUNCTION__);
return -1;
FFXerr(FFX_F_FFX_ENCRYPT, FFX_R_INVALID_DIGITS_LENGTH);
return 0;
}
for (i = 0; i < inlen; i++) {
if (!isdigit(in[i])) {
fprintf(stderr, "%s: invalid digits format\n", __FUNCTION__);
return -1;
FFXerr(FFX_F_FFX_ENCRYPT, FFX_R_INVALID_DIGITS_FORMAT);
return 0;
}
}
llen = inlen / 2;
@@ -138,8 +138,8 @@ int FFX_encrypt(FFX_CTX *ctx, const char *in, size_t inlen,
if (tweaklen < FFX_MIN_TWEAKLEN || tweaklen > FFX_MAX_TWEAKLEN) {
fprintf(stderr, "%s: invalid tweak length\n", __FUNCTION__);
return -1;
FFXerr(FFX_F_FFX_ENCRYPT, FFX_R_INVALID_TWEAK_LENGTH);
return 0;
}
memcpy(lbuf, in, llen);
@@ -155,9 +155,9 @@ int FFX_encrypt(FFX_CTX *ctx, const char *in, size_t inlen,
memset(qblock, 0, sizeof(qblock));
memcpy(qblock, tweak, tweaklen);
for (i = 0; i < FFX_NUM_ROUNDS; i += 2) {
unsigned char rblock[16];
int j;
@@ -169,7 +169,7 @@ int FFX_encrypt(FFX_CTX *ctx, const char *in, size_t inlen,
AES_encrypt(rblock, rblock, &ctx->key);
yval = *((uint64_t *)rblock) % modulo[llen];
lval = (lval + yval) % modulo[llen];
qblock[11] = (i + 1) & 0xff;
memcpy(qblock + 12, &lval, sizeof(lval));
for (j = 0; j < sizeof(rblock); j++) {
@@ -186,7 +186,7 @@ int FFX_encrypt(FFX_CTX *ctx, const char *in, size_t inlen,
sprintf(lbuf, "%d", lval);
strcpy(out + inlen - strlen(lbuf), lbuf);
return 0;
return 1;
}
int FFX_decrypt(FFX_CTX *ctx, const char *in, size_t inlen,
@@ -208,21 +208,21 @@ int FFX_decrypt(FFX_CTX *ctx, const char *in, size_t inlen,
if (inlen > strlen(in) ||
inlen < FFX_MIN_DIGITS || inlen > FFX_MAX_DIGITS) {
fprintf(stderr, "%s: invalid digits length\n", __FUNCTION__);
return -1;
FFXerr(FFX_F_FFX_DECRYPT, FFX_R_INVALID_DIGITS_LENGTH);
return 0;
}
for (i = 0; i < inlen; i++) {
if (!isdigit(in[i])) {
fprintf(stderr, "%s: invalid digits format\n", __FUNCTION__);
return -1;
FFXerr(FFX_F_FFX_DECRYPT, FFX_R_INVALID_DIGITS_FORMAT);
return 0;
}
}
rlen = inlen / 2;
llen = inlen - rlen;
if (tweaklen < FFX_MIN_TWEAKLEN || tweaklen > FFX_MAX_TWEAKLEN) {
fprintf(stderr, "%s: invalid tweak length\n", __FUNCTION__);
return -1;
FFXerr(FFX_F_FFX_DECRYPT, FFX_R_INVALID_TWEAK_LENGTH);
return 0;
}
memcpy(lbuf, in, llen);
@@ -238,9 +238,9 @@ int FFX_decrypt(FFX_CTX *ctx, const char *in, size_t inlen,
memset(qblock, 0, sizeof(qblock));
memcpy(qblock, tweak, tweaklen);
for (i = FFX_NUM_ROUNDS - 1; i > 0; i -= 2) {
unsigned char rblock[16];
int j;
@@ -252,7 +252,7 @@ int FFX_decrypt(FFX_CTX *ctx, const char *in, size_t inlen,
AES_encrypt(rblock, rblock, &ctx->key);
yval = *((uint64_t *)rblock) % modulo[llen];
lval = (lval >= yval) ? (lval - yval) : lval + modulo[llen] - yval;
qblock[11] = (i - 1) & 0xff;
memcpy(qblock + 12, &lval, sizeof(lval));
for (j = 0; j < sizeof(rblock); j++) {
@@ -272,40 +272,6 @@ int FFX_decrypt(FFX_CTX *ctx, const char *in, size_t inlen,
return 0;
}
static int test()
{
char buf[100];
char buf2[100];
unsigned char key[32] = {0};
unsigned char tweak[8] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 };
FFX_CTX ctx;
int r;
ERR_load_crypto_strings();
if (FFX_init(&ctx, 0, key, sizeof(key) * 8) < 0) {
ERR_print_errors_fp(stderr);
fprintf(stderr, "%s: %d\n", __FILE__, __LINE__);
return -1;
}
char *in = "99999999999999999";
r = FFX_encrypt(&ctx, in, strlen(in), tweak, sizeof(tweak), buf);
if (r < 0) {
printf("failed\n");
return -1;
}
printf("%s\n", buf);
printf("\n");
r = FFX_decrypt(&ctx, buf, strlen(buf), tweak, sizeof(tweak), buf2);
printf("%s\n", buf2);
return 0;
}
static int luhn_table[10] = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
/*
@@ -320,7 +286,7 @@ int FFX_compute_luhn(const char *in, size_t inlen)
for (i = inlen - 1; i >= 0; i--) {
int a;
if (!isdigit(in[i])) {
fprintf(stderr, "%s: invalid digit string\n", __FUNCTION__);
FFXerr(FFX_F_FFX_COMPUTE_LUHN, FFX_R_INVALID_DIGIT_STRING);
return -2;
}
a = in[i] - '0';
@@ -333,15 +299,3 @@ int FFX_compute_luhn(const char *in, size_t inlen)
return r;
}
#if 0
int luhn_test()
{
char *digits = "7992739871";
int r = compute_luhn(digits, strlen(digits));
printf("%c", r);
return 0;
}
#endif

View File

@@ -72,8 +72,30 @@ int FFX_decrypt(FFX_CTX *ctx, const char *in, size_t inlen,
const unsigned char *tweak, size_t tweaklen, char *out);
int FFX_compute_luhn(const char *in, size_t inlen);
#ifdef __cplusplus
/* BEGIN ERROR CODES */
/*
* The following lines are auto generated by the script mkerr.pl. Any changes
* made after this point may be overwritten when the script is next run.
*/
void ERR_load_FFX_strings(void);
/* Error codes for the FFX functions. */
/* Function codes. */
# define FFX_F_FFX_COMPUTE_LUHN 100
# define FFX_F_FFX_DECRYPT 101
# define FFX_F_FFX_ENCRYPT 102
# define FFX_F_FFX_INIT 103
/* Reason codes. */
# define FFX_R_INIT_KEY_FAILED 100
# define FFX_R_INVALID_DIGITS_FORMAT 101
# define FFX_R_INVALID_DIGITS_LENGTH 102
# define FFX_R_INVALID_DIGIT_STRING 103
# define FFX_R_INVALID_TWEAK_LENGTH 104
#ifdef __cplusplus
}
#endif
#endif

100
crypto/ffx/ffx_err.c Normal file
View File

@@ -0,0 +1,100 @@
/* crypto/ffx/ffx_err.c */
/* ====================================================================
* Copyright (c) 1999-2016 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* openssl-core@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
/*
* NOTE: this file was auto generated by the mkerr.pl script: any changes
* made to it will be overwritten when the script next updates this file,
* only reason strings will be preserved.
*/
#include <stdio.h>
#include <openssl/err.h>
#include <openssl/ffx.h>
/* BEGIN ERROR CODES */
#ifndef OPENSSL_NO_ERR
# define ERR_FUNC(func) ERR_PACK(ERR_LIB_FFX,func,0)
# define ERR_REASON(reason) ERR_PACK(ERR_LIB_FFX,0,reason)
static ERR_STRING_DATA FFX_str_functs[] = {
{ERR_FUNC(FFX_F_FFX_COMPUTE_LUHN), "FFX_compute_luhn"},
{ERR_FUNC(FFX_F_FFX_DECRYPT), "FFX_decrypt"},
{ERR_FUNC(FFX_F_FFX_ENCRYPT), "FFX_encrypt"},
{ERR_FUNC(FFX_F_FFX_INIT), "FFX_init"},
{0, NULL}
};
static ERR_STRING_DATA FFX_str_reasons[] = {
{ERR_REASON(FFX_R_INIT_KEY_FAILED), "init key failed"},
{ERR_REASON(FFX_R_INVALID_DIGITS_FORMAT), "invalid digits format"},
{ERR_REASON(FFX_R_INVALID_DIGITS_LENGTH), "invalid digits length"},
{ERR_REASON(FFX_R_INVALID_DIGIT_STRING), "invalid digit string"},
{ERR_REASON(FFX_R_INVALID_TWEAK_LENGTH), "invalid tweak length"},
{0, NULL}
};
#endif
void ERR_load_FFX_strings(void)
{
#ifndef OPENSSL_NO_ERR
if (ERR_func_error_string(FFX_str_functs[0].error) == NULL) {
ERR_load_strings(0, FFX_str_functs);
ERR_load_strings(0, FFX_str_reasons);
}
#endif
}

118
crypto/ffx/ffxtest.c Normal file
View File

@@ -0,0 +1,118 @@
/* ====================================================================
* Copyright (c) 2015 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
*/
/*
* Format-Preserve Encryption
* implementation of NIST 800-38G FF1 schemes
*
* FPE is used to encrypt strings such as credit card numbers and phone numbers
* the ciphertext is still in valid format, for example:
* FPE_encrypt("13810631266") == "98723498792"
* the output is still 11 digits
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <openssl/ffx.h>
static int test()
{
char buf[100];
char buf2[100];
unsigned char key[32] = {0};
unsigned char tweak[8] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 };
FFX_CTX ctx;
int r;
ERR_load_crypto_strings();
if (FFX_init(&ctx, 0, key, sizeof(key) * 8) < 0) {
ERR_print_errors_fp(stderr);
fprintf(stderr, "%s: %d\n", __FILE__, __LINE__);
return -1;
}
char *in = "99999999999999999";
r = FFX_encrypt(&ctx, in, strlen(in), tweak, sizeof(tweak), buf);
if (r < 0) {
printf("failed\n");
return -1;
}
printf("%s\n", buf);
printf("\n");
r = FFX_decrypt(&ctx, buf, strlen(buf), tweak, sizeof(tweak), buf2);
printf("%s\n", buf2);
return 0;
}
/*
* 7992739871, checksum = 3
*/
int luhn_test()
{
char *digits = "7992739871";
int r = compute_luhn(digits, strlen(digits));
printf("%c", r);
return 0;
}
int main(int argc, char **argv)
{
return 0;
}