Add accurate asn.1 siganture/ciphertext size

This commit is contained in:
Zhi Guan
2018-12-04 15:56:21 +08:00
parent c0ee87b077
commit bc2bb8a335
13 changed files with 5628 additions and 131 deletions

View File

@@ -49,6 +49,7 @@
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/sm3.h>
#include <openssl/asn1.h>
#include <openssl/asn1t.h>
#include <openssl/objects.h>
@@ -197,3 +198,66 @@ int i2d_SM9Ciphertext_fp(FILE *fp, SM9Ciphertext *c)
return ASN1_item_i2d_fp(ASN1_ITEM_rptr(SM9Ciphertext), fp, c);
}
#endif
int SM9_signature_size(const SM9_MASTER_KEY *params)
{
if (params) {
int ret;
ASN1_INTEGER h;
ASN1_OCTET_STRING s;
unsigned char buf[4] = {0xff};
int len = 0;
/* ASN1_INTEGER h convert from hash */
h.length = SM3_DIGEST_LENGTH;
h.data = buf;
h.type = V_ASN1_INTEGER;
len += i2d_ASN1_INTEGER(&h, NULL);
/* ASN1_OCTET_STRING pointS over E'(F_p^2) */
s.length = 129;
s.data = buf;
s.type = V_ASN1_OCTET_STRING;
len += i2d_ASN1_OCTET_STRING(&s, NULL);
ret = ASN1_object_size(1, len, V_ASN1_SEQUENCE);
return ret;
} else {
return 170;
}
}
int SM9_ciphertext_size(const SM9_MASTER_KEY *params, size_t inlen)
{
int ret;
ASN1_OCTET_STRING s;
s.type = V_ASN1_OCTET_STRING;
s.data = NULL;
int len = 0;
if (inlen > SM9_MAX_PLAINTEXT_LENGTH) {
SM9err(SM9_F_SM9_CIPHERTEXT_SIZE, SM9_R_PLAINTEXT_TOO_LONG);
return 0;
}
if (params) {
/* ASN1_OCTET_STRING pointC1 over E(F_p) */
s.length = 65;
len += i2d_ASN1_OCTET_STRING(&s, NULL);
/* ASN1_OCTET_STRING c3 SM3-MAC */
s.length = SM3_DIGEST_LENGTH;
len += i2d_ASN1_OCTET_STRING(&s, NULL);
} else {
/* when no params given, if use point compression is unknown,
* so the maximum uncompressed point length is used */
len += 101;
}
/* ASN1_OCTET_STRING c2 ciphertext */
s.length = inlen;
len += i2d_ASN1_OCTET_STRING(&s, NULL);
ret = ASN1_object_size(1, len, V_ASN1_SEQUENCE);
return ret;
}