sm2 with short ciphertext

This commit is contained in:
Zhi Guan
2016-05-16 20:38:44 +02:00
parent 60d14da0cc
commit 62b396d193
19 changed files with 249 additions and 138 deletions

BIN
crypto/otp/a.out Executable file

Binary file not shown.

View File

@@ -1,6 +1,7 @@
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <openssl/evp.h>
@@ -45,11 +46,12 @@ int OTP_generate(const OTP_PARAMS *params, const void *event, size_t eventlen,
unsigned char s[EVP_MAX_MD_SIZE];
size_t slen;
uint32_t od;
int i;
int i, n;
OPENSSL_assert(sizeof(time_t) == 8);
if (!check_params(params)) {
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
return 0;
}
@@ -58,11 +60,12 @@ int OTP_generate(const OTP_PARAMS *params, const void *event, size_t eventlen,
idlen = 16;
}
if (!(id = OPENSSL_malloc(idlen))) {
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
goto end;
}
bzero(id, idlen);
t = time(NULL);
t = time(NULL) + params->offset;
t /= params->te;
memcpy(id, &t, sizeof(t));
@@ -108,7 +111,9 @@ int OTP_generate(const OTP_PARAMS *params, const void *event, size_t eventlen,
OPENSSL_assert(slen % 4 == 0);
od = 0;
for (i = 0; i < slen/4; i++) {
n = (int)slen;
for (i = 0; i < n/4; i++) {
od += GETU32(&s[i * 4]);
}

View File

@@ -64,6 +64,8 @@ typedef struct OTP_PARAMS_st {
void *option;
size_t option_size;
int otp_digits;
/* adjust the clock in seconds */
int offset;
} OTP_PARAMS;
/* OTP reference to the GM/T OTP specification

30
crypto/otp/otptest.c Normal file
View File

@@ -0,0 +1,30 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/otp.h>
int main(int argc, char **argv)
{
OTP_PARAMS params;
unsigned char key[] = {1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8};
unsigned char event[] = "this is a fixed value";
unsigned int otp;
params.type = NID_sm3;
params.te = 60;
params.option = NULL;
params.option_size = 0;
params.otp_digits = 6;
OpenSSL_add_all_algorithms();
if (!OTP_generate(&params, event, sizeof(event), &otp, key, sizeof(key))) {
printf("OTP_generate() failed\n");
return -1;
}
printf("OTP = %06u\n", otp);
return 0;
}