This commit is contained in:
Zhi Guan
2015-08-15 15:02:15 +08:00
parent 06df2fab54
commit 3bdc0ea895
2536 changed files with 417052 additions and 271997 deletions

View File

@@ -1,6 +1,7 @@
/* rsa_x931.c */
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
* project 2005.
/*
* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
* 2005.
*/
/* ====================================================================
* Copyright (c) 2005 The OpenSSL Project. All rights reserved.
@@ -10,7 +11,7 @@
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 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
@@ -64,114 +65,103 @@
#include <openssl/objects.h>
int RSA_padding_add_X931(unsigned char *to, int tlen,
const unsigned char *from, int flen)
{
int j;
unsigned char *p;
const unsigned char *from, int flen)
{
int j;
unsigned char *p;
/* Absolute minimum amount of padding is 1 header nibble, 1 padding
* nibble and 2 trailer bytes: but 1 hash if is already in 'from'.
*/
/*
* Absolute minimum amount of padding is 1 header nibble, 1 padding
* nibble and 2 trailer bytes: but 1 hash if is already in 'from'.
*/
j = tlen - flen - 2;
j = tlen - flen - 2;
if (j < 0)
{
RSAerr(RSA_F_RSA_PADDING_ADD_X931,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
return -1;
}
p=(unsigned char *)to;
if (j < 0) {
RSAerr(RSA_F_RSA_PADDING_ADD_X931, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
return -1;
}
/* If no padding start and end nibbles are in one byte */
if (j == 0)
*p++ = 0x6A;
else
{
*p++ = 0x6B;
if (j > 1)
{
memset(p, 0xBB, j - 1);
p += j - 1;
}
*p++ = 0xBA;
}
memcpy(p,from,(unsigned int)flen);
p += flen;
*p = 0xCC;
return(1);
}
p = (unsigned char *)to;
/* If no padding start and end nibbles are in one byte */
if (j == 0)
*p++ = 0x6A;
else {
*p++ = 0x6B;
if (j > 1) {
memset(p, 0xBB, j - 1);
p += j - 1;
}
*p++ = 0xBA;
}
memcpy(p, from, (unsigned int)flen);
p += flen;
*p = 0xCC;
return (1);
}
int RSA_padding_check_X931(unsigned char *to, int tlen,
const unsigned char *from, int flen, int num)
{
int i = 0,j;
const unsigned char *p;
const unsigned char *from, int flen, int num)
{
int i = 0, j;
const unsigned char *p;
p=from;
if ((num != flen) || ((*p != 0x6A) && (*p != 0x6B)))
{
RSAerr(RSA_F_RSA_PADDING_CHECK_X931,RSA_R_INVALID_HEADER);
return -1;
}
p = from;
if ((num != flen) || ((*p != 0x6A) && (*p != 0x6B))) {
RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_HEADER);
return -1;
}
if (*p++ == 0x6B)
{
j=flen-3;
for (i = 0; i < j; i++)
{
unsigned char c = *p++;
if (c == 0xBA)
break;
if (c != 0xBB)
{
RSAerr(RSA_F_RSA_PADDING_CHECK_X931,
RSA_R_INVALID_PADDING);
return -1;
}
}
if (*p++ == 0x6B) {
j = flen - 3;
for (i = 0; i < j; i++) {
unsigned char c = *p++;
if (c == 0xBA)
break;
if (c != 0xBB) {
RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_PADDING);
return -1;
}
}
j -= i;
j -= i;
if (i == 0)
{
RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_PADDING);
return -1;
}
if (i == 0) {
RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_PADDING);
return -1;
}
}
else j = flen - 2;
} else
j = flen - 2;
if (p[j] != 0xCC)
{
RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_TRAILER);
return -1;
}
if (p[j] != 0xCC) {
RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_TRAILER);
return -1;
}
memcpy(to,p,(unsigned int)j);
memcpy(to, p, (unsigned int)j);
return(j);
}
return (j);
}
/* Translate between X931 hash ids and NIDs */
int RSA_X931_hash_id(int nid)
{
switch (nid)
{
case NID_sha1:
return 0x33;
{
switch (nid) {
case NID_sha1:
return 0x33;
case NID_sha256:
return 0x34;
case NID_sha256:
return 0x34;
case NID_sha384:
return 0x36;
case NID_sha384:
return 0x36;
case NID_sha512:
return 0x35;
}
return -1;
}
case NID_sha512:
return 0x35;
}
return -1;
}