mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-10 23:08:05 -05:00
Import code from previous AssetBuilder version
This commit is contained in:
100
thirdparty/libtomcrypt/pk/asn1/der/bit/der_decode_bit_string.c
vendored
Normal file
100
thirdparty/libtomcrypt/pk/asn1/der/bit/der_decode_bit_string.c
vendored
Normal file
@ -0,0 +1,100 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_bit_string.c
|
||||
ASN.1 DER, encode a BIT STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store a BIT STRING
|
||||
@param in The DER encoded BIT STRING
|
||||
@param inlen The size of the DER BIT STRING
|
||||
@param out [out] The array of bits stored (one per char)
|
||||
@param outlen [in/out] The number of bits stored
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_bit_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long dlen, blen, x, y;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* packet must be at least 4 bytes */
|
||||
if (inlen < 4) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* check for 0x03 */
|
||||
if ((in[0]&0x1F) != 0x03) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* offset in the data */
|
||||
x = 1;
|
||||
|
||||
/* get the length of the data */
|
||||
if (in[x] & 0x80) {
|
||||
/* long format get number of length bytes */
|
||||
y = in[x++] & 0x7F;
|
||||
|
||||
/* invalid if 0 or > 2 */
|
||||
if (y == 0 || y > 2) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* read the data len */
|
||||
dlen = 0;
|
||||
while (y--) {
|
||||
dlen = (dlen << 8) | (unsigned long)in[x++];
|
||||
}
|
||||
} else {
|
||||
/* short format */
|
||||
dlen = in[x++] & 0x7F;
|
||||
}
|
||||
|
||||
/* is the data len too long or too short? */
|
||||
if ((dlen == 0) || (dlen + x > inlen)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* get padding count */
|
||||
blen = ((dlen - 1) << 3) - (in[x++] & 7);
|
||||
|
||||
/* too many bits? */
|
||||
if (blen > *outlen) {
|
||||
*outlen = blen;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* decode/store the bits */
|
||||
for (y = 0; y < blen; y++) {
|
||||
out[y] = (in[x] & (1 << (7 - (y & 7)))) ? 1 : 0;
|
||||
if ((y & 7) == 7) {
|
||||
++x;
|
||||
}
|
||||
}
|
||||
|
||||
/* we done */
|
||||
*outlen = blen;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
107
thirdparty/libtomcrypt/pk/asn1/der/bit/der_decode_raw_bit_string.c
vendored
Normal file
107
thirdparty/libtomcrypt/pk/asn1/der/bit/der_decode_raw_bit_string.c
vendored
Normal file
@ -0,0 +1,107 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_bit_string.c
|
||||
ASN.1 DER, encode a BIT STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
#define SETBIT(v, n) (v=((unsigned char)(v) | (1U << (unsigned char)(n))))
|
||||
#define CLRBIT(v, n) (v=((unsigned char)(v) & ~(1U << (unsigned char)(n))))
|
||||
|
||||
/**
|
||||
Store a BIT STRING
|
||||
@param in The DER encoded BIT STRING
|
||||
@param inlen The size of the DER BIT STRING
|
||||
@param out [out] The array of bits stored (8 per char)
|
||||
@param outlen [in/out] The number of bits stored
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_raw_bit_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long dlen, blen, x, y;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* packet must be at least 4 bytes */
|
||||
if (inlen < 4) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* check for 0x03 */
|
||||
if ((in[0]&0x1F) != 0x03) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* offset in the data */
|
||||
x = 1;
|
||||
|
||||
/* get the length of the data */
|
||||
if (in[x] & 0x80) {
|
||||
/* long format get number of length bytes */
|
||||
y = in[x++] & 0x7F;
|
||||
|
||||
/* invalid if 0 or > 2 */
|
||||
if (y == 0 || y > 2) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* read the data len */
|
||||
dlen = 0;
|
||||
while (y--) {
|
||||
dlen = (dlen << 8) | (unsigned long)in[x++];
|
||||
}
|
||||
} else {
|
||||
/* short format */
|
||||
dlen = in[x++] & 0x7F;
|
||||
}
|
||||
|
||||
/* is the data len too long or too short? */
|
||||
if ((dlen == 0) || (dlen + x > inlen)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* get padding count */
|
||||
blen = ((dlen - 1) << 3) - (in[x++] & 7);
|
||||
|
||||
/* too many bits? */
|
||||
if (blen > *outlen) {
|
||||
*outlen = blen;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* decode/store the bits */
|
||||
for (y = 0; y < blen; y++) {
|
||||
if (in[x] & (1 << (7 - (y & 7)))) {
|
||||
SETBIT(out[y/8], 7-(y%8));
|
||||
} else {
|
||||
CLRBIT(out[y/8], 7-(y%8));
|
||||
}
|
||||
if ((y & 7) == 7) {
|
||||
++x;
|
||||
}
|
||||
}
|
||||
|
||||
/* we done */
|
||||
*outlen = blen;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
87
thirdparty/libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.c
vendored
Normal file
87
thirdparty/libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.c
vendored
Normal file
@ -0,0 +1,87 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_bit_string.c
|
||||
ASN.1 DER, encode a BIT STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store a BIT STRING
|
||||
@param in The array of bits to store (one per char)
|
||||
@param inlen The number of bits tostore
|
||||
@param out [out] The destination for the DER encoded BIT STRING
|
||||
@param outlen [in/out] The max size and resulting size of the DER BIT STRING
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long len, x, y;
|
||||
unsigned char buf;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* avoid overflows */
|
||||
if ((err = der_length_bit_string(inlen, &len)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (len > *outlen) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* store header (include bit padding count in length) */
|
||||
x = 0;
|
||||
y = (inlen >> 3) + ((inlen&7) ? 1 : 0) + 1;
|
||||
|
||||
out[x++] = 0x03;
|
||||
if (y < 128) {
|
||||
out[x++] = (unsigned char)y;
|
||||
} else if (y < 256) {
|
||||
out[x++] = 0x81;
|
||||
out[x++] = (unsigned char)y;
|
||||
} else if (y < 65536) {
|
||||
out[x++] = 0x82;
|
||||
out[x++] = (unsigned char)((y>>8)&255);
|
||||
out[x++] = (unsigned char)(y&255);
|
||||
}
|
||||
|
||||
/* store number of zero padding bits */
|
||||
out[x++] = (unsigned char)((8 - inlen) & 7);
|
||||
|
||||
/* store the bits in big endian format */
|
||||
for (y = buf = 0; y < inlen; y++) {
|
||||
buf |= (in[y] ? 1 : 0) << (7 - (y & 7));
|
||||
if ((y & 7) == 7) {
|
||||
out[x++] = buf;
|
||||
buf = 0;
|
||||
}
|
||||
}
|
||||
/* store last byte */
|
||||
if (inlen & 7) {
|
||||
out[x++] = buf;
|
||||
}
|
||||
*outlen = x;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
90
thirdparty/libtomcrypt/pk/asn1/der/bit/der_encode_raw_bit_string.c
vendored
Normal file
90
thirdparty/libtomcrypt/pk/asn1/der/bit/der_encode_raw_bit_string.c
vendored
Normal file
@ -0,0 +1,90 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_bit_string.c
|
||||
ASN.1 DER, encode a BIT STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
#define getbit(n, k) (((n) & ( 1 << (k) )) >> (k))
|
||||
|
||||
/**
|
||||
Store a BIT STRING
|
||||
@param in The array of bits to store (8 per char)
|
||||
@param inlen The number of bits to store
|
||||
@param out [out] The destination for the DER encoded BIT STRING
|
||||
@param outlen [in/out] The max size and resulting size of the DER BIT STRING
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_raw_bit_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long len, x, y;
|
||||
unsigned char buf;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* avoid overflows */
|
||||
if ((err = der_length_bit_string(inlen, &len)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (len > *outlen) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* store header (include bit padding count in length) */
|
||||
x = 0;
|
||||
y = (inlen >> 3) + ((inlen&7) ? 1 : 0) + 1;
|
||||
|
||||
out[x++] = 0x03;
|
||||
if (y < 128) {
|
||||
out[x++] = (unsigned char)y;
|
||||
} else if (y < 256) {
|
||||
out[x++] = 0x81;
|
||||
out[x++] = (unsigned char)y;
|
||||
} else if (y < 65536) {
|
||||
out[x++] = 0x82;
|
||||
out[x++] = (unsigned char)((y>>8)&255);
|
||||
out[x++] = (unsigned char)(y&255);
|
||||
}
|
||||
|
||||
/* store number of zero padding bits */
|
||||
out[x++] = (unsigned char)((8 - inlen) & 7);
|
||||
|
||||
/* store the bits in big endian format */
|
||||
for (y = buf = 0; y < inlen; y++) {
|
||||
buf |= (getbit(in[y/8],7-y%8)?1:0) << (7 - (y & 7));
|
||||
if ((y & 7) == 7) {
|
||||
out[x++] = buf;
|
||||
buf = 0;
|
||||
}
|
||||
}
|
||||
/* store last byte */
|
||||
if (inlen & 7) {
|
||||
out[x++] = buf;
|
||||
}
|
||||
|
||||
*outlen = x;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
52
thirdparty/libtomcrypt/pk/asn1/der/bit/der_length_bit_string.c
vendored
Normal file
52
thirdparty/libtomcrypt/pk/asn1/der/bit/der_length_bit_string.c
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_length_bit_string.c
|
||||
ASN.1 DER, get length of BIT STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
/**
|
||||
Gets length of DER encoding of BIT STRING
|
||||
@param nbits The number of bits in the string to encode
|
||||
@param outlen [out] The length of the DER encoding for the given string
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_bit_string(unsigned long nbits, unsigned long *outlen)
|
||||
{
|
||||
unsigned long nbytes;
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* get the number of the bytes */
|
||||
nbytes = (nbits >> 3) + ((nbits & 7) ? 1 : 0) + 1;
|
||||
|
||||
if (nbytes < 128) {
|
||||
/* 03 LL PP DD DD DD ... */
|
||||
*outlen = 2 + nbytes;
|
||||
} else if (nbytes < 256) {
|
||||
/* 03 81 LL PP DD DD DD ... */
|
||||
*outlen = 3 + nbytes;
|
||||
} else if (nbytes < 65536) {
|
||||
/* 03 82 LL LL PP DD DD DD ... */
|
||||
*outlen = 4 + nbytes;
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
45
thirdparty/libtomcrypt/pk/asn1/der/boolean/der_decode_boolean.c
vendored
Normal file
45
thirdparty/libtomcrypt/pk/asn1/der/boolean/der_decode_boolean.c
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_boolean.c
|
||||
ASN.1 DER, decode a BOOLEAN, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Read a BOOLEAN
|
||||
@param in The destination for the DER encoded BOOLEAN
|
||||
@param inlen The size of the DER BOOLEAN
|
||||
@param out [out] The boolean to decode
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_boolean(const unsigned char *in, unsigned long inlen,
|
||||
int *out)
|
||||
{
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
|
||||
if (inlen < 3 || in[0] != 0x01 || in[1] != 0x01 || (in[2] != 0x00 && in[2] != 0xFF)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
*out = (in[2]==0xFF) ? 1 : 0;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
49
thirdparty/libtomcrypt/pk/asn1/der/boolean/der_encode_boolean.c
vendored
Normal file
49
thirdparty/libtomcrypt/pk/asn1/der/boolean/der_encode_boolean.c
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_boolean.c
|
||||
ASN.1 DER, encode a BOOLEAN, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store a BOOLEAN
|
||||
@param in The boolean to encode
|
||||
@param out [out] The destination for the DER encoded BOOLEAN
|
||||
@param outlen [in/out] The max size and resulting size of the DER BOOLEAN
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_boolean(int in,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
|
||||
if (*outlen < 3) {
|
||||
*outlen = 3;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
*outlen = 3;
|
||||
out[0] = 0x01;
|
||||
out[1] = 0x01;
|
||||
out[2] = in ? 0xFF : 0x00;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
33
thirdparty/libtomcrypt/pk/asn1/der/boolean/der_length_boolean.c
vendored
Normal file
33
thirdparty/libtomcrypt/pk/asn1/der/boolean/der_length_boolean.c
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_length_boolean.c
|
||||
ASN.1 DER, get length of a BOOLEAN, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
/**
|
||||
Gets length of DER encoding of a BOOLEAN
|
||||
@param outlen [out] The length of the DER encoding
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_boolean(unsigned long *outlen)
|
||||
{
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
*outlen = 3;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
223
thirdparty/libtomcrypt/pk/asn1/der/choice/der_decode_choice.c
vendored
Normal file
223
thirdparty/libtomcrypt/pk/asn1/der/choice/der_decode_choice.c
vendored
Normal file
@ -0,0 +1,223 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_choice.c
|
||||
ASN.1 DER, decode a CHOICE, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Decode a CHOICE
|
||||
@param in The DER encoded input
|
||||
@param inlen [in/out] The size of the input and resulting size of read type
|
||||
@param list The list of items to decode
|
||||
@param outlen The number of items in the list
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int der_decode_choice(const unsigned char *in, unsigned long *inlen,
|
||||
ltc_asn1_list *list, unsigned long outlen)
|
||||
{
|
||||
unsigned long size, x, z;
|
||||
void *data;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen != NULL);
|
||||
LTC_ARGCHK(list != NULL);
|
||||
|
||||
/* get blk size */
|
||||
if (*inlen < 2) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* set all of the "used" flags to zero */
|
||||
for (x = 0; x < outlen; x++) {
|
||||
list[x].used = 0;
|
||||
}
|
||||
|
||||
/* now scan until we have a winner */
|
||||
for (x = 0; x < outlen; x++) {
|
||||
size = list[x].size;
|
||||
data = list[x].data;
|
||||
|
||||
switch (list[x].type) {
|
||||
case LTC_ASN1_BOOLEAN:
|
||||
if (der_decode_boolean(in, *inlen, data) == CRYPT_OK) {
|
||||
if (der_length_boolean(&z) == CRYPT_OK) {
|
||||
list[x].used = 1;
|
||||
*inlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_INTEGER:
|
||||
if (der_decode_integer(in, *inlen, data) == CRYPT_OK) {
|
||||
if (der_length_integer(data, &z) == CRYPT_OK) {
|
||||
list[x].used = 1;
|
||||
*inlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_SHORT_INTEGER:
|
||||
if (der_decode_short_integer(in, *inlen, data) == CRYPT_OK) {
|
||||
if (der_length_short_integer(size, &z) == CRYPT_OK) {
|
||||
list[x].used = 1;
|
||||
*inlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_BIT_STRING:
|
||||
if (der_decode_bit_string(in, *inlen, data, &size) == CRYPT_OK) {
|
||||
if (der_length_bit_string(size, &z) == CRYPT_OK) {
|
||||
list[x].used = 1;
|
||||
list[x].size = size;
|
||||
*inlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_RAW_BIT_STRING:
|
||||
if (der_decode_raw_bit_string(in, *inlen, data, &size) == CRYPT_OK) {
|
||||
if (der_length_bit_string(size, &z) == CRYPT_OK) {
|
||||
list[x].used = 1;
|
||||
list[x].size = size;
|
||||
*inlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_OCTET_STRING:
|
||||
if (der_decode_octet_string(in, *inlen, data, &size) == CRYPT_OK) {
|
||||
if (der_length_octet_string(size, &z) == CRYPT_OK) {
|
||||
list[x].used = 1;
|
||||
list[x].size = size;
|
||||
*inlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_NULL:
|
||||
if (*inlen == 2 && in[x] == 0x05 && in[x+1] == 0x00) {
|
||||
*inlen = 2;
|
||||
list[x].used = 1;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_OBJECT_IDENTIFIER:
|
||||
if (der_decode_object_identifier(in, *inlen, data, &size) == CRYPT_OK) {
|
||||
if (der_length_object_identifier(data, size, &z) == CRYPT_OK) {
|
||||
list[x].used = 1;
|
||||
list[x].size = size;
|
||||
*inlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_TELETEX_STRING:
|
||||
if (der_decode_teletex_string(in, *inlen, data, &size) == CRYPT_OK) {
|
||||
if (der_length_teletex_string(data, size, &z) == CRYPT_OK) {
|
||||
list[x].used = 1;
|
||||
list[x].size = size;
|
||||
*inlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_IA5_STRING:
|
||||
if (der_decode_ia5_string(in, *inlen, data, &size) == CRYPT_OK) {
|
||||
if (der_length_ia5_string(data, size, &z) == CRYPT_OK) {
|
||||
list[x].used = 1;
|
||||
list[x].size = size;
|
||||
*inlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_PRINTABLE_STRING:
|
||||
if (der_decode_printable_string(in, *inlen, data, &size) == CRYPT_OK) {
|
||||
if (der_length_printable_string(data, size, &z) == CRYPT_OK) {
|
||||
list[x].used = 1;
|
||||
list[x].size = size;
|
||||
*inlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_UTF8_STRING:
|
||||
if (der_decode_utf8_string(in, *inlen, data, &size) == CRYPT_OK) {
|
||||
if (der_length_utf8_string(data, size, &z) == CRYPT_OK) {
|
||||
list[x].used = 1;
|
||||
list[x].size = size;
|
||||
*inlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_UTCTIME:
|
||||
z = *inlen;
|
||||
if (der_decode_utctime(in, &z, data) == CRYPT_OK) {
|
||||
list[x].used = 1;
|
||||
*inlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_GENERALIZEDTIME:
|
||||
z = *inlen;
|
||||
if (der_decode_generalizedtime(in, &z, data) == CRYPT_OK) {
|
||||
list[x].used = 1;
|
||||
*inlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_SET:
|
||||
case LTC_ASN1_SETOF:
|
||||
case LTC_ASN1_SEQUENCE:
|
||||
if (der_decode_sequence(in, *inlen, data, size) == CRYPT_OK) {
|
||||
if (der_length_sequence(data, size, &z) == CRYPT_OK) {
|
||||
list[x].used = 1;
|
||||
*inlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_CHOICE:
|
||||
case LTC_ASN1_CONSTRUCTED:
|
||||
case LTC_ASN1_CONTEXT_SPECIFIC:
|
||||
case LTC_ASN1_EOL:
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
144
thirdparty/libtomcrypt/pk/asn1/der/generalizedtime/der_decode_generalizedtime.c
vendored
Normal file
144
thirdparty/libtomcrypt/pk/asn1/der/generalizedtime/der_decode_generalizedtime.c
vendored
Normal file
@ -0,0 +1,144 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_generalizedtime.c
|
||||
ASN.1 DER, decode a GeneralizedTime, Steffen Jaeckel
|
||||
Based on der_decode_utctime.c
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
static int _char_to_int(unsigned char x)
|
||||
{
|
||||
switch (x) {
|
||||
case '0': return 0;
|
||||
case '1': return 1;
|
||||
case '2': return 2;
|
||||
case '3': return 3;
|
||||
case '4': return 4;
|
||||
case '5': return 5;
|
||||
case '6': return 6;
|
||||
case '7': return 7;
|
||||
case '8': return 8;
|
||||
case '9': return 9;
|
||||
default: return 100;
|
||||
}
|
||||
}
|
||||
|
||||
#define DECODE_V(y, max) do {\
|
||||
y = _char_to_int(buf[x])*10 + _char_to_int(buf[x+1]); \
|
||||
if (y >= max) return CRYPT_INVALID_PACKET; \
|
||||
x += 2; \
|
||||
} while(0)
|
||||
|
||||
#define DECODE_V4(y, max) do {\
|
||||
y = _char_to_int(buf[x])*1000 + _char_to_int(buf[x+1])*100 + _char_to_int(buf[x+2])*10 + _char_to_int(buf[x+3]); \
|
||||
if (y >= max) return CRYPT_INVALID_PACKET; \
|
||||
x += 4; \
|
||||
} while(0)
|
||||
|
||||
/**
|
||||
Decodes a Generalized time structure in DER format (reads all 6 valid encoding formats)
|
||||
@param in Input buffer
|
||||
@param inlen Length of input buffer in octets
|
||||
@param out [out] Destination of Generalized time structure
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_generalizedtime(const unsigned char *in, unsigned long *inlen,
|
||||
ltc_generalizedtime *out)
|
||||
{
|
||||
unsigned char buf[32];
|
||||
unsigned long x;
|
||||
int y;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
|
||||
/* check header */
|
||||
if (*inlen < 2UL || (in[1] >= sizeof(buf)) || ((in[1] + 2UL) > *inlen)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* decode the string */
|
||||
for (x = 0; x < in[1]; x++) {
|
||||
y = der_ia5_value_decode(in[x+2]);
|
||||
if (y == -1) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
if (!((y >= '0' && y <= '9')
|
||||
|| y == 'Z' || y == '.'
|
||||
|| y == '+' || y == '-')) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
buf[x] = y;
|
||||
}
|
||||
*inlen = 2 + x;
|
||||
|
||||
if (x < 15) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* possible encodings are
|
||||
YYYYMMDDhhmmssZ
|
||||
YYYYMMDDhhmmss+hh'mm'
|
||||
YYYYMMDDhhmmss-hh'mm'
|
||||
YYYYMMDDhhmmss.fsZ
|
||||
YYYYMMDDhhmmss.fs+hh'mm'
|
||||
YYYYMMDDhhmmss.fs-hh'mm'
|
||||
|
||||
So let's do a trivial decode upto [including] ss
|
||||
*/
|
||||
|
||||
x = 0;
|
||||
DECODE_V4(out->YYYY, 10000);
|
||||
DECODE_V(out->MM, 13);
|
||||
DECODE_V(out->DD, 32);
|
||||
DECODE_V(out->hh, 24);
|
||||
DECODE_V(out->mm, 60);
|
||||
DECODE_V(out->ss, 60);
|
||||
|
||||
/* clear fractional seconds info */
|
||||
out->fs = 0;
|
||||
|
||||
/* now is it Z or . */
|
||||
if (buf[x] == 'Z') {
|
||||
return CRYPT_OK;
|
||||
} else if (buf[x] == '.') {
|
||||
x++;
|
||||
while (buf[x] >= '0' && buf[x] <= '9') {
|
||||
unsigned fs = out->fs;
|
||||
if (x >= sizeof(buf)) return CRYPT_INVALID_PACKET;
|
||||
out->fs *= 10;
|
||||
out->fs += _char_to_int(buf[x]);
|
||||
if (fs > out->fs) return CRYPT_OVERFLOW;
|
||||
x++;
|
||||
}
|
||||
}
|
||||
|
||||
/* now is it Z, +, - */
|
||||
if (buf[x] == 'Z') {
|
||||
return CRYPT_OK;
|
||||
} else if (buf[x] == '+' || buf[x] == '-') {
|
||||
out->off_dir = (buf[x++] == '+') ? 0 : 1;
|
||||
DECODE_V(out->off_hh, 24);
|
||||
DECODE_V(out->off_mm, 60);
|
||||
return CRYPT_OK;
|
||||
} else {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
108
thirdparty/libtomcrypt/pk/asn1/der/generalizedtime/der_encode_generalizedtime.c
vendored
Normal file
108
thirdparty/libtomcrypt/pk/asn1/der/generalizedtime/der_encode_generalizedtime.c
vendored
Normal file
@ -0,0 +1,108 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_utctime.c
|
||||
ASN.1 DER, encode a GeneralizedTime, Steffen Jaeckel
|
||||
Based on der_encode_utctime.c
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
static const char * const baseten = "0123456789";
|
||||
|
||||
#define STORE_V(y) do {\
|
||||
out[x++] = der_ia5_char_encode(baseten[(y/10) % 10]); \
|
||||
out[x++] = der_ia5_char_encode(baseten[y % 10]); \
|
||||
} while(0)
|
||||
|
||||
#define STORE_V4(y) do {\
|
||||
out[x++] = der_ia5_char_encode(baseten[(y/1000) % 10]); \
|
||||
out[x++] = der_ia5_char_encode(baseten[(y/100) % 10]); \
|
||||
out[x++] = der_ia5_char_encode(baseten[(y/10) % 10]); \
|
||||
out[x++] = der_ia5_char_encode(baseten[y % 10]); \
|
||||
} while(0)
|
||||
|
||||
/**
|
||||
Encodes a Generalized time structure in DER format
|
||||
@param gtime The GeneralizedTime structure to encode
|
||||
@param out The destination of the DER encoding of the GeneralizedTime structure
|
||||
@param outlen [in/out] The length of the DER encoding
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_generalizedtime(ltc_generalizedtime *gtime,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, tmplen;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(gtime != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
if ((err = der_length_generalizedtime(gtime, &tmplen)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if (tmplen > *outlen) {
|
||||
*outlen = tmplen;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* store header */
|
||||
out[0] = 0x18;
|
||||
|
||||
/* store values */
|
||||
x = 2;
|
||||
STORE_V4(gtime->YYYY);
|
||||
STORE_V(gtime->MM);
|
||||
STORE_V(gtime->DD);
|
||||
STORE_V(gtime->hh);
|
||||
STORE_V(gtime->mm);
|
||||
STORE_V(gtime->ss);
|
||||
|
||||
if (gtime->fs) {
|
||||
unsigned long divisor;
|
||||
unsigned fs = gtime->fs;
|
||||
unsigned len = 0;
|
||||
out[x++] = der_ia5_char_encode('.');
|
||||
divisor = 1;
|
||||
do {
|
||||
fs /= 10;
|
||||
divisor *= 10;
|
||||
len++;
|
||||
} while(fs != 0);
|
||||
while (len-- > 1) {
|
||||
divisor /= 10;
|
||||
out[x++] = der_ia5_char_encode(baseten[(gtime->fs/divisor) % 10]);
|
||||
}
|
||||
out[x++] = der_ia5_char_encode(baseten[gtime->fs % 10]);
|
||||
}
|
||||
|
||||
if (gtime->off_mm || gtime->off_hh) {
|
||||
out[x++] = der_ia5_char_encode(gtime->off_dir ? '-' : '+');
|
||||
STORE_V(gtime->off_hh);
|
||||
STORE_V(gtime->off_mm);
|
||||
} else {
|
||||
out[x++] = der_ia5_char_encode('Z');
|
||||
}
|
||||
|
||||
/* store length */
|
||||
out[1] = (unsigned char)(x - 2);
|
||||
|
||||
/* all good let's return */
|
||||
*outlen = x;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
58
thirdparty/libtomcrypt/pk/asn1/der/generalizedtime/der_length_generalizedtime.c
vendored
Normal file
58
thirdparty/libtomcrypt/pk/asn1/der/generalizedtime/der_length_generalizedtime.c
vendored
Normal file
@ -0,0 +1,58 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_length_utctime.c
|
||||
ASN.1 DER, get length of GeneralizedTime, Steffen Jaeckel
|
||||
Based on der_length_utctime.c
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Gets length of DER encoding of GeneralizedTime
|
||||
@param gtime The GeneralizedTime structure to get the size of
|
||||
@param outlen [out] The length of the DER encoding
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_generalizedtime(ltc_generalizedtime *gtime, unsigned long *outlen)
|
||||
{
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(gtime != NULL);
|
||||
|
||||
if (gtime->fs == 0) {
|
||||
/* we encode as YYYYMMDDhhmmssZ */
|
||||
*outlen = 2 + 14 + 1;
|
||||
} else {
|
||||
unsigned long len = 2 + 14 + 1;
|
||||
unsigned fs = gtime->fs;
|
||||
do {
|
||||
fs /= 10;
|
||||
len++;
|
||||
} while(fs != 0);
|
||||
if (gtime->off_hh == 0 && gtime->off_mm == 0) {
|
||||
/* we encode as YYYYMMDDhhmmss.fsZ */
|
||||
len += 1;
|
||||
}
|
||||
else {
|
||||
/* we encode as YYYYMMDDhhmmss.fs{+|-}hh'mm' */
|
||||
len += 5;
|
||||
}
|
||||
*outlen = len;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
94
thirdparty/libtomcrypt/pk/asn1/der/ia5/der_decode_ia5_string.c
vendored
Normal file
94
thirdparty/libtomcrypt/pk/asn1/der/ia5/der_decode_ia5_string.c
vendored
Normal file
@ -0,0 +1,94 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_ia5_string.c
|
||||
ASN.1 DER, encode a IA5 STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store a IA5 STRING
|
||||
@param in The DER encoded IA5 STRING
|
||||
@param inlen The size of the DER IA5 STRING
|
||||
@param out [out] The array of octets stored (one per char)
|
||||
@param outlen [in/out] The number of octets stored
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, y, len;
|
||||
int t;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* must have header at least */
|
||||
if (inlen < 2) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* check for 0x16 */
|
||||
if ((in[0] & 0x1F) != 0x16) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
x = 1;
|
||||
|
||||
/* decode the length */
|
||||
if (in[x] & 0x80) {
|
||||
/* valid # of bytes in length are 1,2,3 */
|
||||
y = in[x] & 0x7F;
|
||||
if ((y == 0) || (y > 3) || ((x + y) > inlen)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* read the length in */
|
||||
len = 0;
|
||||
++x;
|
||||
while (y--) {
|
||||
len = (len << 8) | in[x++];
|
||||
}
|
||||
} else {
|
||||
len = in[x++] & 0x7F;
|
||||
}
|
||||
|
||||
/* is it too long? */
|
||||
if (len > *outlen) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
if (len + x > inlen) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* read the data */
|
||||
for (y = 0; y < len; y++) {
|
||||
t = der_ia5_value_decode(in[x++]);
|
||||
if (t == -1) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
out[y] = t;
|
||||
}
|
||||
|
||||
*outlen = y;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
83
thirdparty/libtomcrypt/pk/asn1/der/ia5/der_encode_ia5_string.c
vendored
Normal file
83
thirdparty/libtomcrypt/pk/asn1/der/ia5/der_encode_ia5_string.c
vendored
Normal file
@ -0,0 +1,83 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_ia5_string.c
|
||||
ASN.1 DER, encode a IA5 STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store an IA5 STRING
|
||||
@param in The array of IA5 to store (one per char)
|
||||
@param inlen The number of IA5 to store
|
||||
@param out [out] The destination for the DER encoded IA5 STRING
|
||||
@param outlen [in/out] The max size and resulting size of the DER IA5 STRING
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_ia5_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, y, len;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* get the size */
|
||||
if ((err = der_length_ia5_string(in, inlen, &len)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* too big? */
|
||||
if (len > *outlen) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* encode the header+len */
|
||||
x = 0;
|
||||
out[x++] = 0x16;
|
||||
if (inlen < 128) {
|
||||
out[x++] = (unsigned char)inlen;
|
||||
} else if (inlen < 256) {
|
||||
out[x++] = 0x81;
|
||||
out[x++] = (unsigned char)inlen;
|
||||
} else if (inlen < 65536UL) {
|
||||
out[x++] = 0x82;
|
||||
out[x++] = (unsigned char)((inlen>>8)&255);
|
||||
out[x++] = (unsigned char)(inlen&255);
|
||||
} else if (inlen < 16777216UL) {
|
||||
out[x++] = 0x83;
|
||||
out[x++] = (unsigned char)((inlen>>16)&255);
|
||||
out[x++] = (unsigned char)((inlen>>8)&255);
|
||||
out[x++] = (unsigned char)(inlen&255);
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* store octets */
|
||||
for (y = 0; y < inlen; y++) {
|
||||
out[x++] = der_ia5_char_encode(in[y]);
|
||||
}
|
||||
|
||||
/* retun length */
|
||||
*outlen = x;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
192
thirdparty/libtomcrypt/pk/asn1/der/ia5/der_length_ia5_string.c
vendored
Normal file
192
thirdparty/libtomcrypt/pk/asn1/der/ia5/der_length_ia5_string.c
vendored
Normal file
@ -0,0 +1,192 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_length_ia5_string.c
|
||||
ASN.1 DER, get length of IA5 STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
static const struct {
|
||||
int code, value;
|
||||
} ia5_table[] = {
|
||||
{ '\0', 0 },
|
||||
{ '\a', 7 },
|
||||
{ '\b', 8 },
|
||||
{ '\t', 9 },
|
||||
{ '\n', 10 },
|
||||
{ '\f', 12 },
|
||||
{ '\r', 13 },
|
||||
{ ' ', 32 },
|
||||
{ '!', 33 },
|
||||
{ '"', 34 },
|
||||
{ '#', 35 },
|
||||
{ '$', 36 },
|
||||
{ '%', 37 },
|
||||
{ '&', 38 },
|
||||
{ '\'', 39 },
|
||||
{ '(', 40 },
|
||||
{ ')', 41 },
|
||||
{ '*', 42 },
|
||||
{ '+', 43 },
|
||||
{ ',', 44 },
|
||||
{ '-', 45 },
|
||||
{ '.', 46 },
|
||||
{ '/', 47 },
|
||||
{ '0', 48 },
|
||||
{ '1', 49 },
|
||||
{ '2', 50 },
|
||||
{ '3', 51 },
|
||||
{ '4', 52 },
|
||||
{ '5', 53 },
|
||||
{ '6', 54 },
|
||||
{ '7', 55 },
|
||||
{ '8', 56 },
|
||||
{ '9', 57 },
|
||||
{ ':', 58 },
|
||||
{ ';', 59 },
|
||||
{ '<', 60 },
|
||||
{ '=', 61 },
|
||||
{ '>', 62 },
|
||||
{ '?', 63 },
|
||||
{ '@', 64 },
|
||||
{ 'A', 65 },
|
||||
{ 'B', 66 },
|
||||
{ 'C', 67 },
|
||||
{ 'D', 68 },
|
||||
{ 'E', 69 },
|
||||
{ 'F', 70 },
|
||||
{ 'G', 71 },
|
||||
{ 'H', 72 },
|
||||
{ 'I', 73 },
|
||||
{ 'J', 74 },
|
||||
{ 'K', 75 },
|
||||
{ 'L', 76 },
|
||||
{ 'M', 77 },
|
||||
{ 'N', 78 },
|
||||
{ 'O', 79 },
|
||||
{ 'P', 80 },
|
||||
{ 'Q', 81 },
|
||||
{ 'R', 82 },
|
||||
{ 'S', 83 },
|
||||
{ 'T', 84 },
|
||||
{ 'U', 85 },
|
||||
{ 'V', 86 },
|
||||
{ 'W', 87 },
|
||||
{ 'X', 88 },
|
||||
{ 'Y', 89 },
|
||||
{ 'Z', 90 },
|
||||
{ '[', 91 },
|
||||
{ '\\', 92 },
|
||||
{ ']', 93 },
|
||||
{ '^', 94 },
|
||||
{ '_', 95 },
|
||||
{ '`', 96 },
|
||||
{ 'a', 97 },
|
||||
{ 'b', 98 },
|
||||
{ 'c', 99 },
|
||||
{ 'd', 100 },
|
||||
{ 'e', 101 },
|
||||
{ 'f', 102 },
|
||||
{ 'g', 103 },
|
||||
{ 'h', 104 },
|
||||
{ 'i', 105 },
|
||||
{ 'j', 106 },
|
||||
{ 'k', 107 },
|
||||
{ 'l', 108 },
|
||||
{ 'm', 109 },
|
||||
{ 'n', 110 },
|
||||
{ 'o', 111 },
|
||||
{ 'p', 112 },
|
||||
{ 'q', 113 },
|
||||
{ 'r', 114 },
|
||||
{ 's', 115 },
|
||||
{ 't', 116 },
|
||||
{ 'u', 117 },
|
||||
{ 'v', 118 },
|
||||
{ 'w', 119 },
|
||||
{ 'x', 120 },
|
||||
{ 'y', 121 },
|
||||
{ 'z', 122 },
|
||||
{ '{', 123 },
|
||||
{ '|', 124 },
|
||||
{ '}', 125 },
|
||||
{ '~', 126 }
|
||||
};
|
||||
|
||||
int der_ia5_char_encode(int c)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < (int)(sizeof(ia5_table)/sizeof(ia5_table[0])); x++) {
|
||||
if (ia5_table[x].code == c) {
|
||||
return ia5_table[x].value;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int der_ia5_value_decode(int v)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < (int)(sizeof(ia5_table)/sizeof(ia5_table[0])); x++) {
|
||||
if (ia5_table[x].value == v) {
|
||||
return ia5_table[x].code;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
Gets length of DER encoding of IA5 STRING
|
||||
@param octets The values you want to encode
|
||||
@param noctets The number of octets in the string to encode
|
||||
@param outlen [out] The length of the DER encoding for the given string
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x;
|
||||
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(octets != NULL);
|
||||
|
||||
/* scan string for validity */
|
||||
for (x = 0; x < noctets; x++) {
|
||||
if (der_ia5_char_encode(octets[x]) == -1) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
|
||||
if (noctets < 128) {
|
||||
/* 16 LL DD DD DD ... */
|
||||
*outlen = 2 + noctets;
|
||||
} else if (noctets < 256) {
|
||||
/* 16 81 LL DD DD DD ... */
|
||||
*outlen = 3 + noctets;
|
||||
} else if (noctets < 65536UL) {
|
||||
/* 16 82 LL LL DD DD DD ... */
|
||||
*outlen = 4 + noctets;
|
||||
} else if (noctets < 16777216UL) {
|
||||
/* 16 83 LL LL LL DD DD DD ... */
|
||||
*outlen = 5 + noctets;
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
108
thirdparty/libtomcrypt/pk/asn1/der/integer/der_decode_integer.c
vendored
Normal file
108
thirdparty/libtomcrypt/pk/asn1/der/integer/der_decode_integer.c
vendored
Normal file
@ -0,0 +1,108 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_integer.c
|
||||
ASN.1 DER, decode an integer, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Read a mp_int integer
|
||||
@param in The DER encoded data
|
||||
@param inlen Size of DER encoded data
|
||||
@param num The first mp_int to decode
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num)
|
||||
{
|
||||
unsigned long x, y, z;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(num != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
|
||||
/* min DER INTEGER is 0x02 01 00 == 0 */
|
||||
if (inlen < (1 + 1 + 1)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* ok expect 0x02 when we AND with 0001 1111 [1F] */
|
||||
x = 0;
|
||||
if ((in[x++] & 0x1F) != 0x02) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* now decode the len stuff */
|
||||
z = in[x++];
|
||||
|
||||
if ((z & 0x80) == 0x00) {
|
||||
/* short form */
|
||||
|
||||
/* will it overflow? */
|
||||
if (x + z > inlen) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* no so read it */
|
||||
if ((err = mp_read_unsigned_bin(num, (unsigned char *)in + x, z)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
} else {
|
||||
/* long form */
|
||||
z &= 0x7F;
|
||||
|
||||
/* will number of length bytes overflow? (or > 4) */
|
||||
if (((x + z) > inlen) || (z > 4) || (z == 0)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* now read it in */
|
||||
y = 0;
|
||||
while (z--) {
|
||||
y = ((unsigned long)(in[x++])) | (y << 8);
|
||||
}
|
||||
|
||||
/* now will reading y bytes overrun? */
|
||||
if ((x + y) > inlen) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* no so read it */
|
||||
if ((err = mp_read_unsigned_bin(num, (unsigned char *)in + x, y)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
/* see if it's negative */
|
||||
if (in[x] & 0x80) {
|
||||
void *tmp;
|
||||
if (mp_init(&tmp) != CRYPT_OK) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
if (mp_2expt(tmp, mp_count_bits(num)) != CRYPT_OK || mp_sub(num, tmp, num) != CRYPT_OK) {
|
||||
mp_clear(tmp);
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
mp_clear(tmp);
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
128
thirdparty/libtomcrypt/pk/asn1/der/integer/der_encode_integer.c
vendored
Normal file
128
thirdparty/libtomcrypt/pk/asn1/der/integer/der_encode_integer.c
vendored
Normal file
@ -0,0 +1,128 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_integer.c
|
||||
ASN.1 DER, encode an integer, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/* Exports a positive bignum as DER format (upto 2^32 bytes in size) */
|
||||
/**
|
||||
Store a mp_int integer
|
||||
@param num The first mp_int to encode
|
||||
@param out [out] The destination for the DER encoded integers
|
||||
@param outlen [in/out] The max size and resulting size of the DER encoded integers
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long tmplen, y;
|
||||
int err, leading_zero;
|
||||
|
||||
LTC_ARGCHK(num != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* find out how big this will be */
|
||||
if ((err = der_length_integer(num, &tmplen)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (*outlen < tmplen) {
|
||||
*outlen = tmplen;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
if (mp_cmp_d(num, 0) != LTC_MP_LT) {
|
||||
/* we only need a leading zero if the msb of the first byte is one */
|
||||
if ((mp_count_bits(num) & 7) == 0 || mp_iszero(num) == LTC_MP_YES) {
|
||||
leading_zero = 1;
|
||||
} else {
|
||||
leading_zero = 0;
|
||||
}
|
||||
|
||||
/* get length of num in bytes (plus 1 since we force the msbyte to zero) */
|
||||
y = mp_unsigned_bin_size(num) + leading_zero;
|
||||
} else {
|
||||
leading_zero = 0;
|
||||
y = mp_count_bits(num);
|
||||
y = y + (8 - (y & 7));
|
||||
y = y >> 3;
|
||||
if (((mp_cnt_lsb(num)+1)==mp_count_bits(num)) && ((mp_count_bits(num)&7)==0)) --y;
|
||||
}
|
||||
|
||||
/* now store initial data */
|
||||
*out++ = 0x02;
|
||||
if (y < 128) {
|
||||
/* short form */
|
||||
*out++ = (unsigned char)y;
|
||||
} else if (y < 256) {
|
||||
*out++ = 0x81;
|
||||
*out++ = (unsigned char)y;
|
||||
} else if (y < 65536UL) {
|
||||
*out++ = 0x82;
|
||||
*out++ = (unsigned char)((y>>8)&255);
|
||||
*out++ = (unsigned char)y;
|
||||
} else if (y < 16777216UL) {
|
||||
*out++ = 0x83;
|
||||
*out++ = (unsigned char)((y>>16)&255);
|
||||
*out++ = (unsigned char)((y>>8)&255);
|
||||
*out++ = (unsigned char)y;
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* now store msbyte of zero if num is non-zero */
|
||||
if (leading_zero) {
|
||||
*out++ = 0x00;
|
||||
}
|
||||
|
||||
/* if it's not zero store it as big endian */
|
||||
if (mp_cmp_d(num, 0) == LTC_MP_GT) {
|
||||
/* now store the mpint */
|
||||
if ((err = mp_to_unsigned_bin(num, out)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
} else if (mp_iszero(num) != LTC_MP_YES) {
|
||||
void *tmp;
|
||||
|
||||
/* negative */
|
||||
if (mp_init(&tmp) != CRYPT_OK) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* 2^roundup and subtract */
|
||||
y = mp_count_bits(num);
|
||||
y = y + (8 - (y & 7));
|
||||
if (((mp_cnt_lsb(num)+1)==mp_count_bits(num)) && ((mp_count_bits(num)&7)==0)) y -= 8;
|
||||
if (mp_2expt(tmp, y) != CRYPT_OK || mp_add(tmp, num, tmp) != CRYPT_OK) {
|
||||
mp_clear(tmp);
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
if ((err = mp_to_unsigned_bin(tmp, out)) != CRYPT_OK) {
|
||||
mp_clear(tmp);
|
||||
return err;
|
||||
}
|
||||
mp_clear(tmp);
|
||||
}
|
||||
|
||||
/* we good */
|
||||
*outlen = tmplen;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
79
thirdparty/libtomcrypt/pk/asn1/der/integer/der_length_integer.c
vendored
Normal file
79
thirdparty/libtomcrypt/pk/asn1/der/integer/der_length_integer.c
vendored
Normal file
@ -0,0 +1,79 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_length_integer.c
|
||||
ASN.1 DER, get length of encoding, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
/**
|
||||
Gets length of DER encoding of num
|
||||
@param num The int to get the size of
|
||||
@param outlen [out] The length of the DER encoding for the given integer
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_integer(void *num, unsigned long *outlen)
|
||||
{
|
||||
unsigned long z, len;
|
||||
int leading_zero;
|
||||
|
||||
LTC_ARGCHK(num != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
if (mp_cmp_d(num, 0) != LTC_MP_LT) {
|
||||
/* positive */
|
||||
|
||||
/* we only need a leading zero if the msb of the first byte is one */
|
||||
if ((mp_count_bits(num) & 7) == 0 || mp_iszero(num) == LTC_MP_YES) {
|
||||
leading_zero = 1;
|
||||
} else {
|
||||
leading_zero = 0;
|
||||
}
|
||||
|
||||
/* size for bignum */
|
||||
z = len = leading_zero + mp_unsigned_bin_size(num);
|
||||
} else {
|
||||
/* it's negative */
|
||||
/* find power of 2 that is a multiple of eight and greater than count bits */
|
||||
z = mp_count_bits(num);
|
||||
z = z + (8 - (z & 7));
|
||||
if (((mp_cnt_lsb(num)+1)==mp_count_bits(num)) && ((mp_count_bits(num)&7)==0)) --z;
|
||||
len = z = z >> 3;
|
||||
}
|
||||
|
||||
/* now we need a length */
|
||||
if (z < 128) {
|
||||
/* short form */
|
||||
++len;
|
||||
} else {
|
||||
/* long form (relies on z != 0), assumes length bytes < 128 */
|
||||
++len;
|
||||
|
||||
while (z) {
|
||||
++len;
|
||||
z >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
/* we need a 0x02 to indicate it's INTEGER */
|
||||
++len;
|
||||
|
||||
/* return length */
|
||||
*outlen = len;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
106
thirdparty/libtomcrypt/pk/asn1/der/object_identifier/der_decode_object_identifier.c
vendored
Normal file
106
thirdparty/libtomcrypt/pk/asn1/der/object_identifier/der_decode_object_identifier.c
vendored
Normal file
@ -0,0 +1,106 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_object_identifier.c
|
||||
ASN.1 DER, Decode Object Identifier, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
/**
|
||||
Decode OID data and store the array of integers in words
|
||||
@param in The OID DER encoded data
|
||||
@param inlen The length of the OID data
|
||||
@param words [out] The destination of the OID words
|
||||
@param outlen [in/out] The number of OID words
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_object_identifier(const unsigned char *in, unsigned long inlen,
|
||||
unsigned long *words, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, y, t, len;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(words != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* header is at least 3 bytes */
|
||||
if (inlen < 3) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* must be room for at least two words */
|
||||
if (*outlen < 2) {
|
||||
*outlen = 2;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* decode the packet header */
|
||||
x = 0;
|
||||
if ((in[x++] & 0x1F) != 0x06) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* get the length */
|
||||
if (in[x] < 128) {
|
||||
len = in[x++];
|
||||
} else {
|
||||
if (in[x] < 0x81 || in[x] > 0x82) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
y = in[x++] & 0x7F;
|
||||
len = 0;
|
||||
while (y--) {
|
||||
len = (len << 8) | (unsigned long)in[x++];
|
||||
}
|
||||
}
|
||||
|
||||
if (len < 1 || (len + x) > inlen) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* decode words */
|
||||
y = 0;
|
||||
t = 0;
|
||||
while (len--) {
|
||||
t = (t << 7) | (in[x] & 0x7F);
|
||||
if (!(in[x++] & 0x80)) {
|
||||
/* store t */
|
||||
if (y >= *outlen) {
|
||||
y++;
|
||||
} else {
|
||||
if (y == 0) {
|
||||
words[0] = t / 40;
|
||||
words[1] = t % 40;
|
||||
y = 2;
|
||||
} else {
|
||||
words[y++] = t;
|
||||
}
|
||||
}
|
||||
t = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (y > *outlen) {
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
} else {
|
||||
err = CRYPT_OK;
|
||||
}
|
||||
|
||||
*outlen = y;
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
109
thirdparty/libtomcrypt/pk/asn1/der/object_identifier/der_encode_object_identifier.c
vendored
Normal file
109
thirdparty/libtomcrypt/pk/asn1/der/object_identifier/der_encode_object_identifier.c
vendored
Normal file
@ -0,0 +1,109 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_object_identifier.c
|
||||
ASN.1 DER, Encode Object Identifier, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
/**
|
||||
Encode an OID
|
||||
@param words The words to encode (upto 32-bits each)
|
||||
@param nwords The number of words in the OID
|
||||
@param out [out] Destination of OID data
|
||||
@param outlen [in/out] The max and resulting size of the OID
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_object_identifier(unsigned long *words, unsigned long nwords,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long i, x, y, z, t, mask, wordbuf;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(words != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* check length */
|
||||
if ((err = der_length_object_identifier(words, nwords, &x)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if (x > *outlen) {
|
||||
*outlen = x;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* compute length to store OID data */
|
||||
z = 0;
|
||||
wordbuf = words[0] * 40 + words[1];
|
||||
for (y = 1; y < nwords; y++) {
|
||||
t = der_object_identifier_bits(wordbuf);
|
||||
z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0);
|
||||
if (y < nwords - 1) {
|
||||
wordbuf = words[y + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* store header + length */
|
||||
x = 0;
|
||||
out[x++] = 0x06;
|
||||
if (z < 128) {
|
||||
out[x++] = (unsigned char)z;
|
||||
} else if (z < 256) {
|
||||
out[x++] = 0x81;
|
||||
out[x++] = (unsigned char)z;
|
||||
} else if (z < 65536UL) {
|
||||
out[x++] = 0x82;
|
||||
out[x++] = (unsigned char)((z>>8)&255);
|
||||
out[x++] = (unsigned char)(z&255);
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* store first byte */
|
||||
wordbuf = words[0] * 40 + words[1];
|
||||
for (i = 1; i < nwords; i++) {
|
||||
/* store 7 bit words in little endian */
|
||||
t = wordbuf & 0xFFFFFFFF;
|
||||
if (t) {
|
||||
y = x;
|
||||
mask = 0;
|
||||
while (t) {
|
||||
out[x++] = (unsigned char)((t & 0x7F) | mask);
|
||||
t >>= 7;
|
||||
mask |= 0x80; /* upper bit is set on all but the last byte */
|
||||
}
|
||||
/* now swap bytes y...x-1 */
|
||||
z = x - 1;
|
||||
while (y < z) {
|
||||
t = out[y]; out[y] = out[z]; out[z] = (unsigned char)t;
|
||||
++y;
|
||||
--z;
|
||||
}
|
||||
} else {
|
||||
/* zero word */
|
||||
out[x++] = 0x00;
|
||||
}
|
||||
|
||||
if (i < nwords - 1) {
|
||||
wordbuf = words[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
*outlen = x;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
87
thirdparty/libtomcrypt/pk/asn1/der/object_identifier/der_length_object_identifier.c
vendored
Normal file
87
thirdparty/libtomcrypt/pk/asn1/der/object_identifier/der_length_object_identifier.c
vendored
Normal file
@ -0,0 +1,87 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_length_object_identifier.c
|
||||
ASN.1 DER, get length of Object Identifier, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
unsigned long der_object_identifier_bits(unsigned long x)
|
||||
{
|
||||
unsigned long c;
|
||||
x &= 0xFFFFFFFF;
|
||||
c = 0;
|
||||
while (x) {
|
||||
++c;
|
||||
x >>= 1;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Gets length of DER encoding of Object Identifier
|
||||
@param nwords The number of OID words
|
||||
@param words The actual OID words to get the size of
|
||||
@param outlen [out] The length of the DER encoding for the given string
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_object_identifier(unsigned long *words, unsigned long nwords, unsigned long *outlen)
|
||||
{
|
||||
unsigned long y, z, t, wordbuf;
|
||||
|
||||
LTC_ARGCHK(words != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
|
||||
/* must be >= 2 words */
|
||||
if (nwords < 2) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* word1 = 0,1,2,3 and word2 0..39 */
|
||||
if (words[0] > 3 || (words[0] < 2 && words[1] > 39)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* leading word is the first two */
|
||||
z = 0;
|
||||
wordbuf = words[0] * 40 + words[1];
|
||||
for (y = 1; y < nwords; y++) {
|
||||
t = der_object_identifier_bits(wordbuf);
|
||||
z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0);
|
||||
if (y < nwords - 1) {
|
||||
/* grab next word */
|
||||
wordbuf = words[y+1];
|
||||
}
|
||||
}
|
||||
|
||||
/* now depending on the length our length encoding changes */
|
||||
if (z < 128) {
|
||||
z += 2;
|
||||
} else if (z < 256) {
|
||||
z += 3;
|
||||
} else if (z < 65536UL) {
|
||||
z += 4;
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
*outlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
89
thirdparty/libtomcrypt/pk/asn1/der/octet/der_decode_octet_string.c
vendored
Normal file
89
thirdparty/libtomcrypt/pk/asn1/der/octet/der_decode_octet_string.c
vendored
Normal file
@ -0,0 +1,89 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_octet_string.c
|
||||
ASN.1 DER, encode a OCTET STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store a OCTET STRING
|
||||
@param in The DER encoded OCTET STRING
|
||||
@param inlen The size of the DER OCTET STRING
|
||||
@param out [out] The array of octets stored (one per char)
|
||||
@param outlen [in/out] The number of octets stored
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_octet_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, y, len;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* must have header at least */
|
||||
if (inlen < 2) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* check for 0x04 */
|
||||
if ((in[0] & 0x1F) != 0x04) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
x = 1;
|
||||
|
||||
/* decode the length */
|
||||
if (in[x] & 0x80) {
|
||||
/* valid # of bytes in length are 1,2,3 */
|
||||
y = in[x] & 0x7F;
|
||||
if ((y == 0) || (y > 3) || ((x + y) > inlen)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* read the length in */
|
||||
len = 0;
|
||||
++x;
|
||||
while (y--) {
|
||||
len = (len << 8) | in[x++];
|
||||
}
|
||||
} else {
|
||||
len = in[x++] & 0x7F;
|
||||
}
|
||||
|
||||
/* is it too long? */
|
||||
if (len > *outlen) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
if (len + x > inlen) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* read the data */
|
||||
for (y = 0; y < len; y++) {
|
||||
out[y] = in[x++];
|
||||
}
|
||||
|
||||
*outlen = y;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
84
thirdparty/libtomcrypt/pk/asn1/der/octet/der_encode_octet_string.c
vendored
Normal file
84
thirdparty/libtomcrypt/pk/asn1/der/octet/der_encode_octet_string.c
vendored
Normal file
@ -0,0 +1,84 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_octet_string.c
|
||||
ASN.1 DER, encode a OCTET STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store an OCTET STRING
|
||||
@param in The array of OCTETS to store (one per char)
|
||||
@param inlen The number of OCTETS to store
|
||||
@param out [out] The destination for the DER encoded OCTET STRING
|
||||
@param outlen [in/out] The max size and resulting size of the DER OCTET STRING
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, y, len;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* get the size */
|
||||
if ((err = der_length_octet_string(inlen, &len)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* too big? */
|
||||
if (len > *outlen) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* encode the header+len */
|
||||
x = 0;
|
||||
out[x++] = 0x04;
|
||||
if (inlen < 128) {
|
||||
out[x++] = (unsigned char)inlen;
|
||||
} else if (inlen < 256) {
|
||||
out[x++] = 0x81;
|
||||
out[x++] = (unsigned char)inlen;
|
||||
} else if (inlen < 65536UL) {
|
||||
out[x++] = 0x82;
|
||||
out[x++] = (unsigned char)((inlen>>8)&255);
|
||||
out[x++] = (unsigned char)(inlen&255);
|
||||
} else if (inlen < 16777216UL) {
|
||||
out[x++] = 0x83;
|
||||
out[x++] = (unsigned char)((inlen>>16)&255);
|
||||
out[x++] = (unsigned char)((inlen>>8)&255);
|
||||
out[x++] = (unsigned char)(inlen&255);
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* store octets */
|
||||
for (y = 0; y < inlen; y++) {
|
||||
out[x++] = in[y];
|
||||
}
|
||||
|
||||
/* retun length */
|
||||
*outlen = x;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
51
thirdparty/libtomcrypt/pk/asn1/der/octet/der_length_octet_string.c
vendored
Normal file
51
thirdparty/libtomcrypt/pk/asn1/der/octet/der_length_octet_string.c
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_length_octet_string.c
|
||||
ASN.1 DER, get length of OCTET STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
/**
|
||||
Gets length of DER encoding of OCTET STRING
|
||||
@param noctets The number of octets in the string to encode
|
||||
@param outlen [out] The length of the DER encoding for the given string
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_octet_string(unsigned long noctets, unsigned long *outlen)
|
||||
{
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
if (noctets < 128) {
|
||||
/* 04 LL DD DD DD ... */
|
||||
*outlen = 2 + noctets;
|
||||
} else if (noctets < 256) {
|
||||
/* 04 81 LL DD DD DD ... */
|
||||
*outlen = 3 + noctets;
|
||||
} else if (noctets < 65536UL) {
|
||||
/* 04 82 LL LL DD DD DD ... */
|
||||
*outlen = 4 + noctets;
|
||||
} else if (noctets < 16777216UL) {
|
||||
/* 04 83 LL LL LL DD DD DD ... */
|
||||
*outlen = 5 + noctets;
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
94
thirdparty/libtomcrypt/pk/asn1/der/printable_string/der_decode_printable_string.c
vendored
Normal file
94
thirdparty/libtomcrypt/pk/asn1/der/printable_string/der_decode_printable_string.c
vendored
Normal file
@ -0,0 +1,94 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_printable_string.c
|
||||
ASN.1 DER, encode a printable STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store a printable STRING
|
||||
@param in The DER encoded printable STRING
|
||||
@param inlen The size of the DER printable STRING
|
||||
@param out [out] The array of octets stored (one per char)
|
||||
@param outlen [in/out] The number of octets stored
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, y, len;
|
||||
int t;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* must have header at least */
|
||||
if (inlen < 2) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* check for 0x13 */
|
||||
if ((in[0] & 0x1F) != 0x13) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
x = 1;
|
||||
|
||||
/* decode the length */
|
||||
if (in[x] & 0x80) {
|
||||
/* valid # of bytes in length are 1,2,3 */
|
||||
y = in[x] & 0x7F;
|
||||
if ((y == 0) || (y > 3) || ((x + y) > inlen)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* read the length in */
|
||||
len = 0;
|
||||
++x;
|
||||
while (y--) {
|
||||
len = (len << 8) | in[x++];
|
||||
}
|
||||
} else {
|
||||
len = in[x++] & 0x7F;
|
||||
}
|
||||
|
||||
/* is it too long? */
|
||||
if (len > *outlen) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
if (len + x > inlen) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* read the data */
|
||||
for (y = 0; y < len; y++) {
|
||||
t = der_printable_value_decode(in[x++]);
|
||||
if (t == -1) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
out[y] = t;
|
||||
}
|
||||
|
||||
*outlen = y;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
83
thirdparty/libtomcrypt/pk/asn1/der/printable_string/der_encode_printable_string.c
vendored
Normal file
83
thirdparty/libtomcrypt/pk/asn1/der/printable_string/der_encode_printable_string.c
vendored
Normal file
@ -0,0 +1,83 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_printable_string.c
|
||||
ASN.1 DER, encode a printable STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store an printable STRING
|
||||
@param in The array of printable to store (one per char)
|
||||
@param inlen The number of printable to store
|
||||
@param out [out] The destination for the DER encoded printable STRING
|
||||
@param outlen [in/out] The max size and resulting size of the DER printable STRING
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_printable_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, y, len;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* get the size */
|
||||
if ((err = der_length_printable_string(in, inlen, &len)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* too big? */
|
||||
if (len > *outlen) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* encode the header+len */
|
||||
x = 0;
|
||||
out[x++] = 0x13;
|
||||
if (inlen < 128) {
|
||||
out[x++] = (unsigned char)inlen;
|
||||
} else if (inlen < 256) {
|
||||
out[x++] = 0x81;
|
||||
out[x++] = (unsigned char)inlen;
|
||||
} else if (inlen < 65536UL) {
|
||||
out[x++] = 0x82;
|
||||
out[x++] = (unsigned char)((inlen>>8)&255);
|
||||
out[x++] = (unsigned char)(inlen&255);
|
||||
} else if (inlen < 16777216UL) {
|
||||
out[x++] = 0x83;
|
||||
out[x++] = (unsigned char)((inlen>>16)&255);
|
||||
out[x++] = (unsigned char)((inlen>>8)&255);
|
||||
out[x++] = (unsigned char)(inlen&255);
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* store octets */
|
||||
for (y = 0; y < inlen; y++) {
|
||||
out[x++] = der_printable_char_encode(in[y]);
|
||||
}
|
||||
|
||||
/* retun length */
|
||||
*outlen = x;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
164
thirdparty/libtomcrypt/pk/asn1/der/printable_string/der_length_printable_string.c
vendored
Normal file
164
thirdparty/libtomcrypt/pk/asn1/der/printable_string/der_length_printable_string.c
vendored
Normal file
@ -0,0 +1,164 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_length_printable_string.c
|
||||
ASN.1 DER, get length of Printable STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
static const struct {
|
||||
int code, value;
|
||||
} printable_table[] = {
|
||||
{ ' ', 32 },
|
||||
{ '\'', 39 },
|
||||
{ '(', 40 },
|
||||
{ ')', 41 },
|
||||
{ '+', 43 },
|
||||
{ ',', 44 },
|
||||
{ '-', 45 },
|
||||
{ '.', 46 },
|
||||
{ '/', 47 },
|
||||
{ '0', 48 },
|
||||
{ '1', 49 },
|
||||
{ '2', 50 },
|
||||
{ '3', 51 },
|
||||
{ '4', 52 },
|
||||
{ '5', 53 },
|
||||
{ '6', 54 },
|
||||
{ '7', 55 },
|
||||
{ '8', 56 },
|
||||
{ '9', 57 },
|
||||
{ ':', 58 },
|
||||
{ '=', 61 },
|
||||
{ '?', 63 },
|
||||
{ 'A', 65 },
|
||||
{ 'B', 66 },
|
||||
{ 'C', 67 },
|
||||
{ 'D', 68 },
|
||||
{ 'E', 69 },
|
||||
{ 'F', 70 },
|
||||
{ 'G', 71 },
|
||||
{ 'H', 72 },
|
||||
{ 'I', 73 },
|
||||
{ 'J', 74 },
|
||||
{ 'K', 75 },
|
||||
{ 'L', 76 },
|
||||
{ 'M', 77 },
|
||||
{ 'N', 78 },
|
||||
{ 'O', 79 },
|
||||
{ 'P', 80 },
|
||||
{ 'Q', 81 },
|
||||
{ 'R', 82 },
|
||||
{ 'S', 83 },
|
||||
{ 'T', 84 },
|
||||
{ 'U', 85 },
|
||||
{ 'V', 86 },
|
||||
{ 'W', 87 },
|
||||
{ 'X', 88 },
|
||||
{ 'Y', 89 },
|
||||
{ 'Z', 90 },
|
||||
{ 'a', 97 },
|
||||
{ 'b', 98 },
|
||||
{ 'c', 99 },
|
||||
{ 'd', 100 },
|
||||
{ 'e', 101 },
|
||||
{ 'f', 102 },
|
||||
{ 'g', 103 },
|
||||
{ 'h', 104 },
|
||||
{ 'i', 105 },
|
||||
{ 'j', 106 },
|
||||
{ 'k', 107 },
|
||||
{ 'l', 108 },
|
||||
{ 'm', 109 },
|
||||
{ 'n', 110 },
|
||||
{ 'o', 111 },
|
||||
{ 'p', 112 },
|
||||
{ 'q', 113 },
|
||||
{ 'r', 114 },
|
||||
{ 's', 115 },
|
||||
{ 't', 116 },
|
||||
{ 'u', 117 },
|
||||
{ 'v', 118 },
|
||||
{ 'w', 119 },
|
||||
{ 'x', 120 },
|
||||
{ 'y', 121 },
|
||||
{ 'z', 122 },
|
||||
};
|
||||
|
||||
int der_printable_char_encode(int c)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < (int)(sizeof(printable_table)/sizeof(printable_table[0])); x++) {
|
||||
if (printable_table[x].code == c) {
|
||||
return printable_table[x].value;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int der_printable_value_decode(int v)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < (int)(sizeof(printable_table)/sizeof(printable_table[0])); x++) {
|
||||
if (printable_table[x].value == v) {
|
||||
return printable_table[x].code;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
Gets length of DER encoding of Printable STRING
|
||||
@param octets The values you want to encode
|
||||
@param noctets The number of octets in the string to encode
|
||||
@param outlen [out] The length of the DER encoding for the given string
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x;
|
||||
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(octets != NULL);
|
||||
|
||||
/* scan string for validity */
|
||||
for (x = 0; x < noctets; x++) {
|
||||
if (der_printable_char_encode(octets[x]) == -1) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
|
||||
if (noctets < 128) {
|
||||
/* 16 LL DD DD DD ... */
|
||||
*outlen = 2 + noctets;
|
||||
} else if (noctets < 256) {
|
||||
/* 16 81 LL DD DD DD ... */
|
||||
*outlen = 3 + noctets;
|
||||
} else if (noctets < 65536UL) {
|
||||
/* 16 82 LL LL DD DD DD ... */
|
||||
*outlen = 4 + noctets;
|
||||
} else if (noctets < 16777216UL) {
|
||||
/* 16 83 LL LL LL DD DD DD ... */
|
||||
*outlen = 5 + noctets;
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
328
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_ex.c
vendored
Normal file
328
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_ex.c
vendored
Normal file
@ -0,0 +1,328 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
|
||||
/**
|
||||
@file der_decode_sequence_ex.c
|
||||
ASN.1 DER, decode a SEQUENCE, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Decode a SEQUENCE
|
||||
@param in The DER encoded input
|
||||
@param inlen The size of the input
|
||||
@param list The list of items to decode
|
||||
@param outlen The number of items in the list
|
||||
@param ordered Search an unordeded or ordered list
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
|
||||
ltc_asn1_list *list, unsigned long outlen, int ordered)
|
||||
{
|
||||
int err, i;
|
||||
ltc_asn1_type type;
|
||||
unsigned long size, x, y, z, blksize;
|
||||
void *data;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(list != NULL);
|
||||
|
||||
/* get blk size */
|
||||
if (inlen < 2) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* sequence type? We allow 0x30 SEQUENCE and 0x31 SET since fundamentally they're the same structure */
|
||||
x = 0;
|
||||
if (in[x] != 0x30 && in[x] != 0x31) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
++x;
|
||||
|
||||
/* check if the msb is set, which signals that the
|
||||
* 7 lsb bits represent the number of bytes of the length
|
||||
*/
|
||||
if (in[x] < 128) {
|
||||
blksize = in[x++];
|
||||
} else {
|
||||
if (in[x] < 0x81 || in[x] > 0x83) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
y = in[x++] & 0x7F;
|
||||
|
||||
/* would reading the len bytes overrun? */
|
||||
if (x + y > inlen) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* read len */
|
||||
blksize = 0;
|
||||
while (y--) {
|
||||
blksize = (blksize << 8) | (unsigned long)in[x++];
|
||||
}
|
||||
}
|
||||
|
||||
/* would this blksize overflow? */
|
||||
if (x + blksize > inlen) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* mark all as unused */
|
||||
for (i = 0; i < (int)outlen; i++) {
|
||||
list[i].used = 0;
|
||||
}
|
||||
|
||||
/* ok read data */
|
||||
inlen = blksize;
|
||||
for (i = 0; i < (int)outlen; i++) {
|
||||
z = 0;
|
||||
type = list[i].type;
|
||||
size = list[i].size;
|
||||
data = list[i].data;
|
||||
if (!ordered && list[i].used == 1) { continue; }
|
||||
|
||||
if (type == LTC_ASN1_EOL) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case LTC_ASN1_BOOLEAN:
|
||||
z = inlen;
|
||||
if ((err = der_decode_boolean(in + x, z, ((int *)data))) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((err = der_length_boolean(&z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_INTEGER:
|
||||
z = inlen;
|
||||
if ((err = der_decode_integer(in + x, z, data)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((err = der_length_integer(data, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_SHORT_INTEGER:
|
||||
z = inlen;
|
||||
if ((err = der_decode_short_integer(in + x, z, data)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((err = der_length_short_integer(((unsigned long*)data)[0], &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case LTC_ASN1_BIT_STRING:
|
||||
z = inlen;
|
||||
if ((err = der_decode_bit_string(in + x, z, data, &size)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
list[i].size = size;
|
||||
if ((err = der_length_bit_string(size, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_RAW_BIT_STRING:
|
||||
z = inlen;
|
||||
if ((err = der_decode_raw_bit_string(in + x, z, data, &size)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
list[i].size = size;
|
||||
if ((err = der_length_bit_string(size, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_OCTET_STRING:
|
||||
z = inlen;
|
||||
if ((err = der_decode_octet_string(in + x, z, data, &size)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
list[i].size = size;
|
||||
if ((err = der_length_octet_string(size, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_NULL:
|
||||
if (inlen < 2 || in[x] != 0x05 || in[x+1] != 0x00) {
|
||||
if (!ordered) { continue; }
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
z = 2;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_OBJECT_IDENTIFIER:
|
||||
z = inlen;
|
||||
if ((err = der_decode_object_identifier(in + x, z, data, &size)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
list[i].size = size;
|
||||
if ((err = der_length_object_identifier(data, size, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_TELETEX_STRING:
|
||||
z = inlen;
|
||||
if ((err = der_decode_teletex_string(in + x, z, data, &size)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
list[i].size = size;
|
||||
if ((err = der_length_teletex_string(data, size, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_IA5_STRING:
|
||||
z = inlen;
|
||||
if ((err = der_decode_ia5_string(in + x, z, data, &size)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
list[i].size = size;
|
||||
if ((err = der_length_ia5_string(data, size, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case LTC_ASN1_PRINTABLE_STRING:
|
||||
z = inlen;
|
||||
if ((err = der_decode_printable_string(in + x, z, data, &size)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
list[i].size = size;
|
||||
if ((err = der_length_printable_string(data, size, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_UTF8_STRING:
|
||||
z = inlen;
|
||||
if ((err = der_decode_utf8_string(in + x, z, data, &size)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
list[i].size = size;
|
||||
if ((err = der_length_utf8_string(data, size, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_UTCTIME:
|
||||
z = inlen;
|
||||
if ((err = der_decode_utctime(in + x, &z, data)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_GENERALIZEDTIME:
|
||||
z = inlen;
|
||||
if ((err = der_decode_generalizedtime(in + x, &z, data)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_SET:
|
||||
z = inlen;
|
||||
if ((err = der_decode_set(in + x, z, data, size)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((err = der_length_sequence(data, size, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_SETOF:
|
||||
case LTC_ASN1_SEQUENCE:
|
||||
/* detect if we have the right type */
|
||||
if ((type == LTC_ASN1_SETOF && (in[x] & 0x3F) != 0x31) || (type == LTC_ASN1_SEQUENCE && (in[x] & 0x3F) != 0x30)) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
z = inlen;
|
||||
if ((err = der_decode_sequence(in + x, z, data, size)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((err = der_length_sequence(data, size, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case LTC_ASN1_CHOICE:
|
||||
z = inlen;
|
||||
if ((err = der_decode_choice(in + x, &z, data, size)) != CRYPT_OK) {
|
||||
if (!ordered) { continue; }
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_CONSTRUCTED:
|
||||
case LTC_ASN1_CONTEXT_SPECIFIC:
|
||||
case LTC_ASN1_EOL:
|
||||
err = CRYPT_INVALID_ARG;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
x += z;
|
||||
inlen -= z;
|
||||
list[i].used = 1;
|
||||
if (!ordered) {
|
||||
/* restart the decoder */
|
||||
i = -1;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < (int)outlen; i++) {
|
||||
if (list[i].used == 0) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
if (inlen == 0) {
|
||||
err = CRYPT_OK;
|
||||
} else {
|
||||
err = CRYPT_INPUT_TOO_LONG;
|
||||
}
|
||||
|
||||
LBL_ERR:
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
484
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_flexi.c
vendored
Normal file
484
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_flexi.c
vendored
Normal file
@ -0,0 +1,484 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_sequence_flexi.c
|
||||
ASN.1 DER, decode an array of ASN.1 types with a flexi parser, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
static unsigned long _fetch_length(const unsigned char *in, unsigned long inlen, unsigned long *data_offset)
|
||||
{
|
||||
unsigned long x, z;
|
||||
|
||||
*data_offset = 0;
|
||||
|
||||
/* skip type and read len */
|
||||
if (inlen < 2) {
|
||||
return 0xFFFFFFFF;
|
||||
}
|
||||
++in; ++(*data_offset);
|
||||
|
||||
/* read len */
|
||||
x = *in++; ++(*data_offset);
|
||||
|
||||
/* <128 means literal */
|
||||
if (x < 128) {
|
||||
return x+*data_offset;
|
||||
}
|
||||
x &= 0x7F; /* the lower 7 bits are the length of the length */
|
||||
inlen -= 2;
|
||||
|
||||
/* len means len of len! */
|
||||
if (x == 0 || x > 4 || x > inlen) {
|
||||
return 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
*data_offset += x;
|
||||
z = 0;
|
||||
while (x--) {
|
||||
z = (z<<8) | ((unsigned long)*in);
|
||||
++in;
|
||||
}
|
||||
return z+*data_offset;
|
||||
}
|
||||
|
||||
static int _new_element(ltc_asn1_list **l)
|
||||
{
|
||||
/* alloc new link */
|
||||
if (*l == NULL) {
|
||||
*l = XCALLOC(1, sizeof(ltc_asn1_list));
|
||||
if (*l == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
} else {
|
||||
(*l)->next = XCALLOC(1, sizeof(ltc_asn1_list));
|
||||
if ((*l)->next == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
(*l)->next->prev = *l;
|
||||
*l = (*l)->next;
|
||||
}
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
ASN.1 DER Flexi(ble) decoder will decode arbitrary DER packets and create a linked list of the decoded elements.
|
||||
@param in The input buffer
|
||||
@param inlen [in/out] The length of the input buffer and on output the amount of decoded data
|
||||
@param out [out] A pointer to the linked list
|
||||
@return CRYPT_OK on success.
|
||||
*/
|
||||
int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out)
|
||||
{
|
||||
ltc_asn1_list *l, *t;
|
||||
unsigned long err, type, len, totlen, data_offset, len_len;
|
||||
void *realloc_tmp;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
|
||||
l = NULL;
|
||||
totlen = 0;
|
||||
|
||||
if (*inlen == 0) {
|
||||
/* alloc new link */
|
||||
if ((err = _new_element(&l)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
/* scan the input and and get lengths and what not */
|
||||
while (*inlen) {
|
||||
/* read the type byte */
|
||||
type = *in;
|
||||
|
||||
/* fetch length */
|
||||
len = _fetch_length(in, *inlen, &data_offset);
|
||||
if (len > *inlen) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* alloc new link */
|
||||
if ((err = _new_element(&l)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((type & 0x20) && (type != 0x30) && (type != 0x31)) {
|
||||
/* constructed, use the 'used' field to store the original identifier */
|
||||
l->used = type;
|
||||
/* treat constructed elements like SETs */
|
||||
type = 0x20;
|
||||
}
|
||||
else if ((type & 0xC0) == 0x80) {
|
||||
/* context-specific, use the 'used' field to store the original identifier */
|
||||
l->used = type;
|
||||
/* context-specific elements are treated as opaque data */
|
||||
type = 0x80;
|
||||
}
|
||||
|
||||
/* now switch on type */
|
||||
switch (type) {
|
||||
case 0x01: /* BOOLEAN */
|
||||
l->type = LTC_ASN1_BOOLEAN;
|
||||
l->size = 1;
|
||||
l->data = XCALLOC(1, sizeof(int));
|
||||
|
||||
if ((err = der_decode_boolean(in, *inlen, l->data)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_length_boolean(&len)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x02: /* INTEGER */
|
||||
/* init field */
|
||||
l->type = LTC_ASN1_INTEGER;
|
||||
l->size = 1;
|
||||
if ((err = mp_init(&l->data)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* decode field */
|
||||
if ((err = der_decode_integer(in, *inlen, l->data)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* calc length of object */
|
||||
if ((err = der_length_integer(l->data, &len)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x03: /* BIT */
|
||||
/* init field */
|
||||
l->type = LTC_ASN1_BIT_STRING;
|
||||
l->size = len * 8; /* *8 because we store decoded bits one per char and they are encoded 8 per char. */
|
||||
|
||||
if ((l->data = XCALLOC(1, l->size)) == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_decode_bit_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_length_bit_string(l->size, &len)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x04: /* OCTET */
|
||||
|
||||
/* init field */
|
||||
l->type = LTC_ASN1_OCTET_STRING;
|
||||
l->size = len;
|
||||
|
||||
if ((l->data = XCALLOC(1, l->size)) == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_decode_octet_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_length_octet_string(l->size, &len)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x05: /* NULL */
|
||||
|
||||
/* valid NULL is 0x05 0x00 */
|
||||
if (in[0] != 0x05 || in[1] != 0x00) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* simple to store ;-) */
|
||||
l->type = LTC_ASN1_NULL;
|
||||
l->data = NULL;
|
||||
l->size = 0;
|
||||
len = 2;
|
||||
|
||||
break;
|
||||
|
||||
case 0x06: /* OID */
|
||||
|
||||
/* init field */
|
||||
l->type = LTC_ASN1_OBJECT_IDENTIFIER;
|
||||
l->size = len;
|
||||
|
||||
if ((l->data = XCALLOC(len, sizeof(unsigned long))) == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_decode_object_identifier(in, *inlen, l->data, &l->size)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_length_object_identifier(l->data, l->size, &len)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* resize it to save a bunch of mem */
|
||||
if ((realloc_tmp = XREALLOC(l->data, l->size * sizeof(unsigned long))) == NULL) {
|
||||
/* out of heap but this is not an error */
|
||||
break;
|
||||
}
|
||||
l->data = realloc_tmp;
|
||||
break;
|
||||
|
||||
case 0x0C: /* UTF8 */
|
||||
|
||||
/* init field */
|
||||
l->type = LTC_ASN1_UTF8_STRING;
|
||||
l->size = len;
|
||||
|
||||
if ((l->data = XCALLOC(sizeof(wchar_t), l->size)) == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_decode_utf8_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_length_utf8_string(l->data, l->size, &len)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x13: /* PRINTABLE */
|
||||
|
||||
/* init field */
|
||||
l->type = LTC_ASN1_PRINTABLE_STRING;
|
||||
l->size = len;
|
||||
|
||||
if ((l->data = XCALLOC(1, l->size)) == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_decode_printable_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_length_printable_string(l->data, l->size, &len)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x14: /* TELETEXT */
|
||||
|
||||
/* init field */
|
||||
l->type = LTC_ASN1_TELETEX_STRING;
|
||||
l->size = len;
|
||||
|
||||
if ((l->data = XCALLOC(1, l->size)) == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_decode_teletex_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_length_teletex_string(l->data, l->size, &len)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x16: /* IA5 */
|
||||
|
||||
/* init field */
|
||||
l->type = LTC_ASN1_IA5_STRING;
|
||||
l->size = len;
|
||||
|
||||
if ((l->data = XCALLOC(1, l->size)) == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_decode_ia5_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_length_ia5_string(l->data, l->size, &len)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x17: /* UTC TIME */
|
||||
|
||||
/* init field */
|
||||
l->type = LTC_ASN1_UTCTIME;
|
||||
l->size = 1;
|
||||
|
||||
if ((l->data = XCALLOC(1, sizeof(ltc_utctime))) == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
len = *inlen;
|
||||
if ((err = der_decode_utctime(in, &len, l->data)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_length_utctime(l->data, &len)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x18:
|
||||
l->type = LTC_ASN1_GENERALIZEDTIME;
|
||||
l->size = len;
|
||||
|
||||
if ((l->data = XCALLOC(1, sizeof(ltc_generalizedtime))) == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_decode_generalizedtime(in, &len, l->data)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = der_length_generalizedtime(l->data, &len)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 0x20: /* Any CONSTRUCTED element that is neither SEQUENCE nor SET */
|
||||
case 0x30: /* SEQUENCE */
|
||||
case 0x31: /* SET */
|
||||
|
||||
/* init field */
|
||||
if (type == 0x20) {
|
||||
l->type = LTC_ASN1_CONSTRUCTED;
|
||||
}
|
||||
else if (type == 0x30) {
|
||||
l->type = LTC_ASN1_SEQUENCE;
|
||||
}
|
||||
else {
|
||||
l->type = LTC_ASN1_SET;
|
||||
}
|
||||
|
||||
if ((l->data = XMALLOC(len)) == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
XMEMCPY(l->data, in, len);
|
||||
l->size = len;
|
||||
|
||||
|
||||
/* jump to the start of the data */
|
||||
in += data_offset;
|
||||
*inlen -= data_offset;
|
||||
len = len - data_offset;
|
||||
|
||||
/* Sequence elements go as child */
|
||||
if ((err = der_decode_sequence_flexi(in, &len, &(l->child))) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* len update */
|
||||
totlen += data_offset;
|
||||
|
||||
/* the flexi decoder can also do nothing, so make sure a child has been allocated */
|
||||
if (l->child) {
|
||||
/* link them up y0 */
|
||||
l->child->parent = l;
|
||||
}
|
||||
|
||||
t = l;
|
||||
len_len = 0;
|
||||
while((t != NULL) && (t->child != NULL)) {
|
||||
len_len++;
|
||||
t = t->child;
|
||||
}
|
||||
if (len_len > LTC_DER_MAX_RECURSION) {
|
||||
err = CRYPT_ERROR;
|
||||
goto error;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 0x80: /* Context-specific */
|
||||
l->type = LTC_ASN1_CONTEXT_SPECIFIC;
|
||||
|
||||
if ((l->data = XCALLOC(1, len - data_offset)) == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
XMEMCPY(l->data, in + data_offset, len - data_offset);
|
||||
l->size = len - data_offset;
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
/* invalid byte ... this is a soft error */
|
||||
/* remove link */
|
||||
if (l->prev) {
|
||||
l = l->prev;
|
||||
XFREE(l->next);
|
||||
l->next = NULL;
|
||||
}
|
||||
goto outside;
|
||||
}
|
||||
|
||||
/* advance pointers */
|
||||
totlen += len;
|
||||
in += len;
|
||||
*inlen -= len;
|
||||
}
|
||||
|
||||
outside:
|
||||
|
||||
/* in case we processed anything */
|
||||
if (totlen) {
|
||||
/* rewind l please */
|
||||
while (l->prev != NULL || l->parent != NULL) {
|
||||
if (l->parent != NULL) {
|
||||
l = l->parent;
|
||||
} else {
|
||||
l = l->prev;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* return */
|
||||
*out = l;
|
||||
*inlen = totlen;
|
||||
return CRYPT_OK;
|
||||
|
||||
error:
|
||||
/* free list */
|
||||
der_sequence_free(l);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
145
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_multi.c
vendored
Normal file
145
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_multi.c
vendored
Normal file
@ -0,0 +1,145 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
|
||||
/**
|
||||
@file der_decode_sequence_multi.c
|
||||
ASN.1 DER, decode a SEQUENCE, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Decode a SEQUENCE type using a VA list
|
||||
@param in Input buffer
|
||||
@param inlen Length of input in octets
|
||||
@remark <...> is of the form <type, size, data> (int, unsigned long, void*)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...)
|
||||
{
|
||||
int err;
|
||||
ltc_asn1_type type;
|
||||
unsigned long size, x;
|
||||
void *data;
|
||||
va_list args;
|
||||
ltc_asn1_list *list;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
|
||||
/* get size of output that will be required */
|
||||
va_start(args, inlen);
|
||||
x = 0;
|
||||
for (;;) {
|
||||
type = (ltc_asn1_type)va_arg(args, int);
|
||||
size = va_arg(args, unsigned long);
|
||||
data = va_arg(args, void*);
|
||||
LTC_UNUSED_PARAM(size);
|
||||
LTC_UNUSED_PARAM(data);
|
||||
|
||||
if (type == LTC_ASN1_EOL) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case LTC_ASN1_BOOLEAN:
|
||||
case LTC_ASN1_INTEGER:
|
||||
case LTC_ASN1_SHORT_INTEGER:
|
||||
case LTC_ASN1_BIT_STRING:
|
||||
case LTC_ASN1_OCTET_STRING:
|
||||
case LTC_ASN1_NULL:
|
||||
case LTC_ASN1_OBJECT_IDENTIFIER:
|
||||
case LTC_ASN1_IA5_STRING:
|
||||
case LTC_ASN1_PRINTABLE_STRING:
|
||||
case LTC_ASN1_UTF8_STRING:
|
||||
case LTC_ASN1_UTCTIME:
|
||||
case LTC_ASN1_SET:
|
||||
case LTC_ASN1_SETOF:
|
||||
case LTC_ASN1_SEQUENCE:
|
||||
case LTC_ASN1_CHOICE:
|
||||
case LTC_ASN1_RAW_BIT_STRING:
|
||||
case LTC_ASN1_TELETEX_STRING:
|
||||
case LTC_ASN1_GENERALIZEDTIME:
|
||||
++x;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_EOL:
|
||||
case LTC_ASN1_CONSTRUCTED:
|
||||
case LTC_ASN1_CONTEXT_SPECIFIC:
|
||||
va_end(args);
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
/* allocate structure for x elements */
|
||||
if (x == 0) {
|
||||
return CRYPT_NOP;
|
||||
}
|
||||
|
||||
list = XCALLOC(sizeof(*list), x);
|
||||
if (list == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* fill in the structure */
|
||||
va_start(args, inlen);
|
||||
x = 0;
|
||||
for (;;) {
|
||||
type = (ltc_asn1_type)va_arg(args, int);
|
||||
size = va_arg(args, unsigned long);
|
||||
data = va_arg(args, void*);
|
||||
|
||||
if (type == LTC_ASN1_EOL) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case LTC_ASN1_BOOLEAN:
|
||||
case LTC_ASN1_INTEGER:
|
||||
case LTC_ASN1_SHORT_INTEGER:
|
||||
case LTC_ASN1_BIT_STRING:
|
||||
case LTC_ASN1_OCTET_STRING:
|
||||
case LTC_ASN1_NULL:
|
||||
case LTC_ASN1_OBJECT_IDENTIFIER:
|
||||
case LTC_ASN1_IA5_STRING:
|
||||
case LTC_ASN1_PRINTABLE_STRING:
|
||||
case LTC_ASN1_UTF8_STRING:
|
||||
case LTC_ASN1_UTCTIME:
|
||||
case LTC_ASN1_SEQUENCE:
|
||||
case LTC_ASN1_SET:
|
||||
case LTC_ASN1_SETOF:
|
||||
case LTC_ASN1_CHOICE:
|
||||
case LTC_ASN1_RAW_BIT_STRING:
|
||||
case LTC_ASN1_TELETEX_STRING:
|
||||
case LTC_ASN1_GENERALIZEDTIME:
|
||||
LTC_SET_ASN1(list, x++, type, data, size);
|
||||
break;
|
||||
/* coverity[dead_error_line] */
|
||||
case LTC_ASN1_EOL:
|
||||
case LTC_ASN1_CONSTRUCTED:
|
||||
case LTC_ASN1_CONTEXT_SPECIFIC:
|
||||
break;
|
||||
}
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
err = der_decode_sequence(in, inlen, list, x);
|
||||
XFREE(list);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
112
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_decode_subject_public_key_info.c
vendored
Normal file
112
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_decode_subject_public_key_info.c
vendored
Normal file
@ -0,0 +1,112 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/**
|
||||
@file der_decode_subject_public_key_info.c
|
||||
ASN.1 DER, encode a Subject Public Key structure --nmav
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/* AlgorithmIdentifier := SEQUENCE {
|
||||
* algorithm OBJECT IDENTIFIER,
|
||||
* parameters ANY DEFINED BY algorithm
|
||||
* }
|
||||
*
|
||||
* SubjectPublicKeyInfo := SEQUENCE {
|
||||
* algorithm AlgorithmIdentifier,
|
||||
* subjectPublicKey BIT STRING
|
||||
* }
|
||||
*/
|
||||
/**
|
||||
Decode a subject public key info
|
||||
@param in The input buffer
|
||||
@param inlen The length of the input buffer
|
||||
@param algorithm One out of the enum #public_key_algorithms
|
||||
@param public_key The buffer for the public key
|
||||
@param public_key_len [in/out] The length of the public key buffer and the written length
|
||||
@param parameters_type The parameters' type out of the enum ltc_asn1_type
|
||||
@param parameters The parameters to include
|
||||
@param parameters_len The number of parameters to include
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int der_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
|
||||
unsigned int algorithm, void* public_key, unsigned long* public_key_len,
|
||||
unsigned long parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len)
|
||||
{
|
||||
int err;
|
||||
unsigned long len;
|
||||
oid_st oid;
|
||||
unsigned char *tmpbuf;
|
||||
unsigned long tmpoid[16];
|
||||
ltc_asn1_list alg_id[2];
|
||||
ltc_asn1_list subject_pubkey[2];
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen != 0);
|
||||
LTC_ARGCHK(public_key_len != NULL);
|
||||
|
||||
err = pk_get_oid(algorithm, &oid);
|
||||
if (err != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* see if the OpenSSL DER format RSA public key will work */
|
||||
tmpbuf = XCALLOC(1, inlen);
|
||||
if (tmpbuf == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* this includes the internal hash ID and optional params (NULL in this case) */
|
||||
LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, sizeof(tmpoid)/sizeof(tmpoid[0]));
|
||||
LTC_SET_ASN1(alg_id, 1, (ltc_asn1_type)parameters_type, parameters, parameters_len);
|
||||
|
||||
/* the actual format of the SSL DER key is odd, it stores a RSAPublicKey
|
||||
* in a **BIT** string ... so we have to extract it then proceed to convert bit to octet
|
||||
*/
|
||||
LTC_SET_ASN1(subject_pubkey, 0, LTC_ASN1_SEQUENCE, alg_id, 2);
|
||||
LTC_SET_ASN1(subject_pubkey, 1, LTC_ASN1_RAW_BIT_STRING, tmpbuf, inlen*8U);
|
||||
|
||||
err=der_decode_sequence(in, inlen, subject_pubkey, 2UL);
|
||||
if (err != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
if ((alg_id[0].size != oid.OIDlen) ||
|
||||
XMEMCMP(oid.OID, alg_id[0].data, oid.OIDlen * sizeof(oid.OID[0])) != 0) {
|
||||
/* OID mismatch */
|
||||
err = CRYPT_PK_INVALID_TYPE;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
len = subject_pubkey[1].size/8;
|
||||
if (*public_key_len > len) {
|
||||
XMEMCPY(public_key, subject_pubkey[1].data, len);
|
||||
*public_key_len = len;
|
||||
} else {
|
||||
*public_key_len = len;
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
err = CRYPT_OK;
|
||||
|
||||
LBL_ERR:
|
||||
|
||||
XFREE(tmpbuf);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
217
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_ex.c
vendored
Normal file
217
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_ex.c
vendored
Normal file
@ -0,0 +1,217 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
|
||||
/**
|
||||
@file der_encode_sequence_ex.c
|
||||
ASN.1 DER, encode a SEQUENCE, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Encode a SEQUENCE
|
||||
@param list The list of items to encode
|
||||
@param inlen The number of items in the list
|
||||
@param out [out] The destination
|
||||
@param outlen [in/out] The size of the output
|
||||
@param type_of LTC_ASN1_SEQUENCE or LTC_ASN1_SET/LTC_ASN1_SETOF
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen, int type_of)
|
||||
{
|
||||
int err;
|
||||
ltc_asn1_type type;
|
||||
unsigned long size, x, y, z, i;
|
||||
void *data;
|
||||
|
||||
LTC_ARGCHK(list != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* get size of output that will be required */
|
||||
y = 0; z = 0;
|
||||
if ((err = der_length_sequence_ex(list, inlen, &y, &z)) != CRYPT_OK) return CRYPT_INVALID_ARG;
|
||||
|
||||
/* too big ? */
|
||||
if (*outlen < y) {
|
||||
*outlen = y;
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* store header */
|
||||
x = 0;
|
||||
out[x++] = (type_of == LTC_ASN1_SEQUENCE) ? 0x30 : 0x31;
|
||||
|
||||
if (z < 128) {
|
||||
out[x++] = (unsigned char)z;
|
||||
} else if (z < 256) {
|
||||
out[x++] = 0x81;
|
||||
out[x++] = (unsigned char)z;
|
||||
} else if (z < 65536UL) {
|
||||
out[x++] = 0x82;
|
||||
out[x++] = (unsigned char)((z>>8UL)&255);
|
||||
out[x++] = (unsigned char)(z&255);
|
||||
} else if (z < 16777216UL) {
|
||||
out[x++] = 0x83;
|
||||
out[x++] = (unsigned char)((z>>16UL)&255);
|
||||
out[x++] = (unsigned char)((z>>8UL)&255);
|
||||
out[x++] = (unsigned char)(z&255);
|
||||
}
|
||||
|
||||
/* store data */
|
||||
*outlen -= x;
|
||||
for (i = 0; i < inlen; i++) {
|
||||
type = list[i].type;
|
||||
size = list[i].size;
|
||||
data = list[i].data;
|
||||
|
||||
if (type == LTC_ASN1_EOL) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case LTC_ASN1_BOOLEAN:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_boolean(*((int *)data), out + x, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_INTEGER:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_integer(data, out + x, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_SHORT_INTEGER:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_short_integer(*((unsigned long*)data), out + x, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_BIT_STRING:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_bit_string(data, size, out + x, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_RAW_BIT_STRING:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_raw_bit_string(data, size, out + x, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_OCTET_STRING:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_octet_string(data, size, out + x, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_NULL:
|
||||
out[x] = 0x05;
|
||||
out[x+1] = 0x00;
|
||||
z = 2;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_OBJECT_IDENTIFIER:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_object_identifier(data, size, out + x, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_IA5_STRING:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_ia5_string(data, size, out + x, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_PRINTABLE_STRING:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_printable_string(data, size, out + x, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_UTF8_STRING:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_utf8_string(data, size, out + x, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_UTCTIME:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_utctime(data, out + x, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_GENERALIZEDTIME:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_generalizedtime(data, out + x, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_SET:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_set(data, size, out + x, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_SETOF:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_setof(data, size, out + x, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_SEQUENCE:
|
||||
z = *outlen;
|
||||
if ((err = der_encode_sequence_ex(data, size, out + x, &z, type)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTC_ASN1_CHOICE:
|
||||
case LTC_ASN1_CONSTRUCTED:
|
||||
case LTC_ASN1_CONTEXT_SPECIFIC:
|
||||
case LTC_ASN1_EOL:
|
||||
case LTC_ASN1_TELETEX_STRING:
|
||||
err = CRYPT_INVALID_ARG;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
x += z;
|
||||
*outlen -= z;
|
||||
}
|
||||
*outlen = x;
|
||||
err = CRYPT_OK;
|
||||
|
||||
LBL_ERR:
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
149
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_multi.c
vendored
Normal file
149
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_multi.c
vendored
Normal file
@ -0,0 +1,149 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
|
||||
/**
|
||||
@file der_encode_sequence_multi.c
|
||||
ASN.1 DER, encode a SEQUENCE, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Encode a SEQUENCE type using a VA list
|
||||
@param out [out] Destination for data
|
||||
@param outlen [in/out] Length of buffer and resulting length of output
|
||||
@remark <...> is of the form <type, size, data> (int, unsigned long, void*)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...)
|
||||
{
|
||||
int err;
|
||||
ltc_asn1_type type;
|
||||
unsigned long size, x;
|
||||
void *data;
|
||||
va_list args;
|
||||
ltc_asn1_list *list;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* get size of output that will be required */
|
||||
va_start(args, outlen);
|
||||
x = 0;
|
||||
for (;;) {
|
||||
type = (ltc_asn1_type)va_arg(args, int);
|
||||
size = va_arg(args, unsigned long);
|
||||
data = va_arg(args, void*);
|
||||
LTC_UNUSED_PARAM(size);
|
||||
LTC_UNUSED_PARAM(data);
|
||||
|
||||
if (type == LTC_ASN1_EOL) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case LTC_ASN1_BOOLEAN:
|
||||
case LTC_ASN1_INTEGER:
|
||||
case LTC_ASN1_SHORT_INTEGER:
|
||||
case LTC_ASN1_BIT_STRING:
|
||||
case LTC_ASN1_OCTET_STRING:
|
||||
case LTC_ASN1_NULL:
|
||||
case LTC_ASN1_OBJECT_IDENTIFIER:
|
||||
case LTC_ASN1_IA5_STRING:
|
||||
case LTC_ASN1_PRINTABLE_STRING:
|
||||
case LTC_ASN1_UTF8_STRING:
|
||||
case LTC_ASN1_UTCTIME:
|
||||
case LTC_ASN1_SEQUENCE:
|
||||
case LTC_ASN1_SET:
|
||||
case LTC_ASN1_SETOF:
|
||||
case LTC_ASN1_RAW_BIT_STRING:
|
||||
case LTC_ASN1_GENERALIZEDTIME:
|
||||
++x;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_CHOICE:
|
||||
case LTC_ASN1_CONSTRUCTED:
|
||||
case LTC_ASN1_CONTEXT_SPECIFIC:
|
||||
case LTC_ASN1_EOL:
|
||||
case LTC_ASN1_TELETEX_STRING:
|
||||
va_end(args);
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
/* allocate structure for x elements */
|
||||
if (x == 0) {
|
||||
return CRYPT_NOP;
|
||||
}
|
||||
|
||||
list = XCALLOC(sizeof(*list), x);
|
||||
if (list == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* fill in the structure */
|
||||
va_start(args, outlen);
|
||||
x = 0;
|
||||
for (;;) {
|
||||
type = (ltc_asn1_type)va_arg(args, int);
|
||||
size = va_arg(args, unsigned long);
|
||||
data = va_arg(args, void*);
|
||||
|
||||
if (type == LTC_ASN1_EOL) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case LTC_ASN1_BOOLEAN:
|
||||
case LTC_ASN1_INTEGER:
|
||||
case LTC_ASN1_SHORT_INTEGER:
|
||||
case LTC_ASN1_BIT_STRING:
|
||||
case LTC_ASN1_OCTET_STRING:
|
||||
case LTC_ASN1_NULL:
|
||||
case LTC_ASN1_OBJECT_IDENTIFIER:
|
||||
case LTC_ASN1_IA5_STRING:
|
||||
case LTC_ASN1_PRINTABLE_STRING:
|
||||
case LTC_ASN1_UTF8_STRING:
|
||||
case LTC_ASN1_UTCTIME:
|
||||
case LTC_ASN1_SEQUENCE:
|
||||
case LTC_ASN1_SET:
|
||||
case LTC_ASN1_SETOF:
|
||||
case LTC_ASN1_RAW_BIT_STRING:
|
||||
case LTC_ASN1_GENERALIZEDTIME:
|
||||
LTC_SET_ASN1(list, x++, type, data, size);
|
||||
break;
|
||||
|
||||
case LTC_ASN1_CHOICE:
|
||||
case LTC_ASN1_CONSTRUCTED:
|
||||
case LTC_ASN1_CONTEXT_SPECIFIC:
|
||||
case LTC_ASN1_EOL:
|
||||
case LTC_ASN1_TELETEX_STRING:
|
||||
va_end(args);
|
||||
err = CRYPT_INVALID_ARG;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
err = der_encode_sequence(list, x, out, outlen);
|
||||
LBL_ERR:
|
||||
XFREE(list);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
71
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_encode_subject_public_key_info.c
vendored
Normal file
71
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_encode_subject_public_key_info.c
vendored
Normal file
@ -0,0 +1,71 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_subject_public_key_info.c
|
||||
ASN.1 DER, encode a Subject Public Key structure --nmav
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/* AlgorithmIdentifier := SEQUENCE {
|
||||
* algorithm OBJECT IDENTIFIER,
|
||||
* parameters ANY DEFINED BY algorithm
|
||||
* }
|
||||
*
|
||||
* SubjectPublicKeyInfo := SEQUENCE {
|
||||
* algorithm AlgorithmIdentifier,
|
||||
* subjectPublicKey BIT STRING
|
||||
* }
|
||||
*/
|
||||
/**
|
||||
Encode a subject public key info
|
||||
@param out The output buffer
|
||||
@param outlen [in/out] Length of buffer and resulting length of output
|
||||
@param algorithm One out of the enum #public_key_algorithms
|
||||
@param public_key The buffer for the public key
|
||||
@param public_key_len The length of the public key buffer
|
||||
@param parameters_type The parameters' type out of the enum ltc_asn1_type
|
||||
@param parameters The parameters to include
|
||||
@param parameters_len The number of parameters to include
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int der_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
|
||||
unsigned int algorithm, void* public_key, unsigned long public_key_len,
|
||||
unsigned long parameters_type, void* parameters, unsigned long parameters_len)
|
||||
{
|
||||
int err;
|
||||
ltc_asn1_list alg_id[2];
|
||||
oid_st oid;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
err = pk_get_oid(algorithm, &oid);
|
||||
if (err != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, oid.OID, oid.OIDlen);
|
||||
LTC_SET_ASN1(alg_id, 1, (ltc_asn1_type)parameters_type, parameters, parameters_len);
|
||||
|
||||
return der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_SEQUENCE, (unsigned long)sizeof(alg_id)/sizeof(alg_id[0]), alg_id,
|
||||
LTC_ASN1_RAW_BIT_STRING, public_key_len*8U, public_key,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
193
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_length_sequence.c
vendored
Normal file
193
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_length_sequence.c
vendored
Normal file
@ -0,0 +1,193 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_length_sequence.c
|
||||
ASN.1 DER, length a SEQUENCE, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Get the length of a DER sequence
|
||||
@param list The sequences of items in the SEQUENCE
|
||||
@param inlen The number of items
|
||||
@param outlen [out] The length required in octets to store it
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int der_length_sequence(ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned long *outlen)
|
||||
{
|
||||
return der_length_sequence_ex(list, inlen, outlen, NULL);
|
||||
}
|
||||
|
||||
int der_length_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned long *outlen, unsigned long *payloadlen)
|
||||
{
|
||||
int err;
|
||||
ltc_asn1_type type;
|
||||
unsigned long size, x, y, i, z;
|
||||
void *data;
|
||||
|
||||
LTC_ARGCHK(list != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* get size of output that will be required */
|
||||
y = 0;
|
||||
for (i = 0; i < inlen; i++) {
|
||||
type = list[i].type;
|
||||
size = list[i].size;
|
||||
data = list[i].data;
|
||||
|
||||
if (type == LTC_ASN1_EOL) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case LTC_ASN1_BOOLEAN:
|
||||
if ((err = der_length_boolean(&x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
y += x;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_INTEGER:
|
||||
if ((err = der_length_integer(data, &x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
y += x;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_SHORT_INTEGER:
|
||||
if ((err = der_length_short_integer(*((unsigned long *)data), &x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
y += x;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_BIT_STRING:
|
||||
case LTC_ASN1_RAW_BIT_STRING:
|
||||
if ((err = der_length_bit_string(size, &x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
y += x;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_OCTET_STRING:
|
||||
if ((err = der_length_octet_string(size, &x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
y += x;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_NULL:
|
||||
y += 2;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_OBJECT_IDENTIFIER:
|
||||
if ((err = der_length_object_identifier(data, size, &x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
y += x;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_IA5_STRING:
|
||||
if ((err = der_length_ia5_string(data, size, &x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
y += x;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_TELETEX_STRING:
|
||||
if ((err = der_length_teletex_string(data, size, &x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
y += x;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_PRINTABLE_STRING:
|
||||
if ((err = der_length_printable_string(data, size, &x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
y += x;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_UTCTIME:
|
||||
if ((err = der_length_utctime(data, &x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
y += x;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_GENERALIZEDTIME:
|
||||
if ((err = der_length_generalizedtime(data, &x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
y += x;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_UTF8_STRING:
|
||||
if ((err = der_length_utf8_string(data, size, &x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
y += x;
|
||||
break;
|
||||
|
||||
case LTC_ASN1_SET:
|
||||
case LTC_ASN1_SETOF:
|
||||
case LTC_ASN1_SEQUENCE:
|
||||
if ((err = der_length_sequence(data, size, &x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
y += x;
|
||||
break;
|
||||
|
||||
|
||||
case LTC_ASN1_CHOICE:
|
||||
case LTC_ASN1_CONSTRUCTED:
|
||||
case LTC_ASN1_CONTEXT_SPECIFIC:
|
||||
case LTC_ASN1_EOL:
|
||||
err = CRYPT_INVALID_ARG;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
/* calc header size */
|
||||
z = y;
|
||||
if (y < 128) {
|
||||
y += 2;
|
||||
} else if (y < 256) {
|
||||
/* 0x30 0x81 LL */
|
||||
y += 3;
|
||||
} else if (y < 65536UL) {
|
||||
/* 0x30 0x82 LL LL */
|
||||
y += 4;
|
||||
} else if (y < 16777216UL) {
|
||||
/* 0x30 0x83 LL LL LL */
|
||||
y += 5;
|
||||
} else {
|
||||
err = CRYPT_INVALID_ARG;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* store size */
|
||||
if (payloadlen) *payloadlen = z;
|
||||
*outlen = y;
|
||||
err = CRYPT_OK;
|
||||
|
||||
LBL_ERR:
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
63
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_sequence_free.c
vendored
Normal file
63
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_sequence_free.c
vendored
Normal file
@ -0,0 +1,63 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_sequence_free.c
|
||||
ASN.1 DER, free's a structure allocated by der_decode_sequence_flexi(), Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Free memory allocated by der_decode_sequence_flexi()
|
||||
@param in The list to free
|
||||
*/
|
||||
void der_sequence_free(ltc_asn1_list *in)
|
||||
{
|
||||
ltc_asn1_list *l;
|
||||
|
||||
if (!in) return;
|
||||
|
||||
/* walk to the start of the chain */
|
||||
while (in->prev != NULL || in->parent != NULL) {
|
||||
if (in->parent != NULL) {
|
||||
in = in->parent;
|
||||
} else {
|
||||
in = in->prev;
|
||||
}
|
||||
}
|
||||
|
||||
/* now walk the list and free stuff */
|
||||
while (in != NULL) {
|
||||
/* is there a child? */
|
||||
if (in->child) {
|
||||
/* disconnect */
|
||||
in->child->parent = NULL;
|
||||
der_sequence_free(in->child);
|
||||
}
|
||||
|
||||
switch (in->type) {
|
||||
case LTC_ASN1_SETOF: break;
|
||||
case LTC_ASN1_INTEGER : if (in->data != NULL) { mp_clear(in->data); } break;
|
||||
default : if (in->data != NULL) { XFREE(in->data); }
|
||||
}
|
||||
|
||||
/* move to next and free current */
|
||||
l = in->next;
|
||||
XFREE(in);
|
||||
in = l;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
50
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_sequence_shrink.c
vendored
Normal file
50
thirdparty/libtomcrypt/pk/asn1/der/sequence/der_sequence_shrink.c
vendored
Normal file
@ -0,0 +1,50 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_sequence_shrink.c
|
||||
Free memory allocated for CONSTRUCTED, SET or SEQUENCE elements by der_decode_sequence_flexi(), Steffen Jaeckel
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Free memory allocated for CONSTRUCTED,
|
||||
SET or SEQUENCE elements by der_decode_sequence_flexi()
|
||||
@param in The list to shrink
|
||||
*/
|
||||
void der_sequence_shrink(ltc_asn1_list *in)
|
||||
{
|
||||
if (!in) return;
|
||||
|
||||
/* now walk the list and free stuff */
|
||||
while (in != NULL) {
|
||||
/* is there a child? */
|
||||
if (in->child) {
|
||||
der_sequence_shrink(in->child);
|
||||
}
|
||||
|
||||
switch (in->type) {
|
||||
case LTC_ASN1_CONSTRUCTED:
|
||||
case LTC_ASN1_SET:
|
||||
case LTC_ASN1_SEQUENCE : if (in->data != NULL) { XFREE(in->data); in->data = NULL; } break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
/* move to next and free current */
|
||||
in = in->next;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
108
thirdparty/libtomcrypt/pk/asn1/der/set/der_encode_set.c
vendored
Normal file
108
thirdparty/libtomcrypt/pk/asn1/der/set/der_encode_set.c
vendored
Normal file
@ -0,0 +1,108 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_set.c
|
||||
ASN.1 DER, Encode a SET, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/* LTC define to ASN.1 TAG */
|
||||
static int _ltc_to_asn1(ltc_asn1_type v)
|
||||
{
|
||||
switch (v) {
|
||||
case LTC_ASN1_BOOLEAN: return 0x01;
|
||||
case LTC_ASN1_INTEGER:
|
||||
case LTC_ASN1_SHORT_INTEGER: return 0x02;
|
||||
case LTC_ASN1_RAW_BIT_STRING:
|
||||
case LTC_ASN1_BIT_STRING: return 0x03;
|
||||
case LTC_ASN1_OCTET_STRING: return 0x04;
|
||||
case LTC_ASN1_NULL: return 0x05;
|
||||
case LTC_ASN1_OBJECT_IDENTIFIER: return 0x06;
|
||||
case LTC_ASN1_UTF8_STRING: return 0x0C;
|
||||
case LTC_ASN1_PRINTABLE_STRING: return 0x13;
|
||||
case LTC_ASN1_TELETEX_STRING: return 0x14;
|
||||
case LTC_ASN1_IA5_STRING: return 0x16;
|
||||
case LTC_ASN1_UTCTIME: return 0x17;
|
||||
case LTC_ASN1_GENERALIZEDTIME: return 0x18;
|
||||
case LTC_ASN1_SEQUENCE: return 0x30;
|
||||
case LTC_ASN1_SET:
|
||||
case LTC_ASN1_SETOF: return 0x31;
|
||||
case LTC_ASN1_CHOICE:
|
||||
case LTC_ASN1_CONSTRUCTED:
|
||||
case LTC_ASN1_CONTEXT_SPECIFIC:
|
||||
case LTC_ASN1_EOL: return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
static int _qsort_helper(const void *a, const void *b)
|
||||
{
|
||||
ltc_asn1_list *A = (ltc_asn1_list *)a, *B = (ltc_asn1_list *)b;
|
||||
int r;
|
||||
|
||||
r = _ltc_to_asn1(A->type) - _ltc_to_asn1(B->type);
|
||||
|
||||
/* for QSORT the order is UNDEFINED if they are "equal" which means it is NOT DETERMINISTIC. So we force it to be :-) */
|
||||
if (r == 0) {
|
||||
/* their order in the original list now determines the position */
|
||||
return A->used - B->used;
|
||||
} else {
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Encode a SET type
|
||||
@param list The list of items to encode
|
||||
@param inlen The number of items in the list
|
||||
@param out [out] The destination
|
||||
@param outlen [in/out] The size of the output
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int der_encode_set(ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
ltc_asn1_list *copy;
|
||||
unsigned long x;
|
||||
int err;
|
||||
|
||||
/* make copy of list */
|
||||
copy = XCALLOC(inlen, sizeof(*copy));
|
||||
if (copy == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* fill in used member with index so we can fully sort it */
|
||||
for (x = 0; x < inlen; x++) {
|
||||
copy[x] = list[x];
|
||||
copy[x].used = x;
|
||||
}
|
||||
|
||||
/* sort it by the "type" field */
|
||||
XQSORT(copy, inlen, sizeof(*copy), &_qsort_helper);
|
||||
|
||||
/* call der_encode_sequence_ex() */
|
||||
err = der_encode_sequence_ex(copy, inlen, out, outlen, LTC_ASN1_SET);
|
||||
|
||||
/* free list */
|
||||
XFREE(copy);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
161
thirdparty/libtomcrypt/pk/asn1/der/set/der_encode_setof.c
vendored
Normal file
161
thirdparty/libtomcrypt/pk/asn1/der/set/der_encode_setof.c
vendored
Normal file
@ -0,0 +1,161 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_setof.c
|
||||
ASN.1 DER, Encode SET OF, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
struct edge {
|
||||
unsigned char *start;
|
||||
unsigned long size;
|
||||
};
|
||||
|
||||
static int _qsort_helper(const void *a, const void *b)
|
||||
{
|
||||
struct edge *A = (struct edge *)a, *B = (struct edge *)b;
|
||||
int r;
|
||||
unsigned long x;
|
||||
|
||||
/* compare min length */
|
||||
r = XMEMCMP(A->start, B->start, MIN(A->size, B->size));
|
||||
|
||||
if (r == 0 && A->size != B->size) {
|
||||
if (A->size > B->size) {
|
||||
for (x = B->size; x < A->size; x++) {
|
||||
if (A->start[x]) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (x = A->size; x < B->size; x++) {
|
||||
if (B->start[x]) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
Encode a SETOF stucture
|
||||
@param list The list of items to encode
|
||||
@param inlen The number of items in the list
|
||||
@param out [out] The destination
|
||||
@param outlen [in/out] The size of the output
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int der_encode_setof(ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, y, z;
|
||||
ptrdiff_t hdrlen;
|
||||
int err;
|
||||
struct edge *edges;
|
||||
unsigned char *ptr, *buf;
|
||||
|
||||
/* check that they're all the same type */
|
||||
for (x = 1; x < inlen; x++) {
|
||||
if (list[x].type != list[x-1].type) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
|
||||
/* alloc buffer to store copy of output */
|
||||
buf = XCALLOC(1, *outlen);
|
||||
if (buf == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* encode list */
|
||||
if ((err = der_encode_sequence_ex(list, inlen, buf, outlen, LTC_ASN1_SETOF)) != CRYPT_OK) {
|
||||
XFREE(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
/* allocate edges */
|
||||
edges = XCALLOC(inlen, sizeof(*edges));
|
||||
if (edges == NULL) {
|
||||
XFREE(buf);
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* skip header */
|
||||
ptr = buf + 1;
|
||||
|
||||
/* now skip length data */
|
||||
x = *ptr++;
|
||||
if (x >= 0x80) {
|
||||
ptr += (x & 0x7F);
|
||||
}
|
||||
|
||||
/* get the size of the static header */
|
||||
hdrlen = ptr - buf;
|
||||
|
||||
|
||||
/* scan for edges */
|
||||
x = 0;
|
||||
while (ptr < (buf + *outlen)) {
|
||||
/* store start */
|
||||
edges[x].start = ptr;
|
||||
|
||||
/* skip type */
|
||||
z = 1;
|
||||
|
||||
/* parse length */
|
||||
y = ptr[z++];
|
||||
if (y < 128) {
|
||||
edges[x].size = y;
|
||||
} else {
|
||||
y &= 0x7F;
|
||||
edges[x].size = 0;
|
||||
while (y--) {
|
||||
edges[x].size = (edges[x].size << 8) | ((unsigned long)ptr[z++]);
|
||||
}
|
||||
}
|
||||
|
||||
/* skip content */
|
||||
edges[x].size += z;
|
||||
ptr += edges[x].size;
|
||||
++x;
|
||||
}
|
||||
|
||||
/* sort based on contents (using edges) */
|
||||
XQSORT(edges, inlen, sizeof(*edges), &_qsort_helper);
|
||||
|
||||
/* copy static header */
|
||||
XMEMCPY(out, buf, hdrlen);
|
||||
|
||||
/* copy+sort using edges+indecies to output from buffer */
|
||||
for (y = (unsigned long)hdrlen, x = 0; x < inlen; x++) {
|
||||
XMEMCPY(out+y, edges[x].start, edges[x].size);
|
||||
y += edges[x].size;
|
||||
}
|
||||
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(buf, *outlen);
|
||||
#endif
|
||||
|
||||
/* free buffers */
|
||||
XFREE(edges);
|
||||
XFREE(buf);
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
66
thirdparty/libtomcrypt/pk/asn1/der/short_integer/der_decode_short_integer.c
vendored
Normal file
66
thirdparty/libtomcrypt/pk/asn1/der/short_integer/der_decode_short_integer.c
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_short_integer.c
|
||||
ASN.1 DER, decode an integer, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Read a short integer
|
||||
@param in The DER encoded data
|
||||
@param inlen Size of data
|
||||
@param num [out] The integer to decode
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num)
|
||||
{
|
||||
unsigned long len, x, y;
|
||||
|
||||
LTC_ARGCHK(num != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
|
||||
/* check length */
|
||||
if (inlen < 2) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* check header */
|
||||
x = 0;
|
||||
if ((in[x++] & 0x1F) != 0x02) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* get the packet len */
|
||||
len = in[x++];
|
||||
|
||||
if (x + len > inlen) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* read number */
|
||||
y = 0;
|
||||
while (len--) {
|
||||
y = (y<<8) | (unsigned long)in[x++];
|
||||
}
|
||||
*num = y;
|
||||
|
||||
return CRYPT_OK;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
95
thirdparty/libtomcrypt/pk/asn1/der/short_integer/der_encode_short_integer.c
vendored
Normal file
95
thirdparty/libtomcrypt/pk/asn1/der/short_integer/der_encode_short_integer.c
vendored
Normal file
@ -0,0 +1,95 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_short_integer.c
|
||||
ASN.1 DER, encode an integer, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store a short integer in the range (0,2^32-1)
|
||||
@param num The integer to encode
|
||||
@param out [out] The destination for the DER encoded integers
|
||||
@param outlen [in/out] The max size and resulting size of the DER encoded integers
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_short_integer(unsigned long num, unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long len, x, y, z;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* force to 32 bits */
|
||||
num &= 0xFFFFFFFFUL;
|
||||
|
||||
/* find out how big this will be */
|
||||
if ((err = der_length_short_integer(num, &len)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (*outlen < len) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* get len of output */
|
||||
z = 0;
|
||||
y = num;
|
||||
while (y) {
|
||||
++z;
|
||||
y >>= 8;
|
||||
}
|
||||
|
||||
/* handle zero */
|
||||
if (z == 0) {
|
||||
z = 1;
|
||||
}
|
||||
|
||||
/* see if msb is set */
|
||||
z += (num&(1UL<<((z<<3) - 1))) ? 1 : 0;
|
||||
|
||||
/* adjust the number so the msB is non-zero */
|
||||
for (x = 0; (z <= 4) && (x < (4 - z)); x++) {
|
||||
num <<= 8;
|
||||
}
|
||||
|
||||
/* store header */
|
||||
x = 0;
|
||||
out[x++] = 0x02;
|
||||
out[x++] = (unsigned char)z;
|
||||
|
||||
/* if 31st bit is set output a leading zero and decrement count */
|
||||
if (z == 5) {
|
||||
out[x++] = 0;
|
||||
--z;
|
||||
}
|
||||
|
||||
/* store values */
|
||||
for (y = 0; y < z; y++) {
|
||||
out[x++] = (unsigned char)((num >> 24) & 0xFF);
|
||||
num <<= 8;
|
||||
}
|
||||
|
||||
/* we good */
|
||||
*outlen = x;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
68
thirdparty/libtomcrypt/pk/asn1/der/short_integer/der_length_short_integer.c
vendored
Normal file
68
thirdparty/libtomcrypt/pk/asn1/der/short_integer/der_length_short_integer.c
vendored
Normal file
@ -0,0 +1,68 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_length_short_integer.c
|
||||
ASN.1 DER, get length of encoding, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
/**
|
||||
Gets length of DER encoding of num
|
||||
@param num The integer to get the size of
|
||||
@param outlen [out] The length of the DER encoding for the given integer
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_short_integer(unsigned long num, unsigned long *outlen)
|
||||
{
|
||||
unsigned long z, y, len;
|
||||
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* force to 32 bits */
|
||||
num &= 0xFFFFFFFFUL;
|
||||
|
||||
/* get the number of bytes */
|
||||
z = 0;
|
||||
y = num;
|
||||
while (y) {
|
||||
++z;
|
||||
y >>= 8;
|
||||
}
|
||||
|
||||
/* handle zero */
|
||||
if (z == 0) {
|
||||
z = 1;
|
||||
}
|
||||
|
||||
/* we need a 0x02 to indicate it's INTEGER */
|
||||
len = 1;
|
||||
|
||||
/* length byte */
|
||||
++len;
|
||||
|
||||
/* bytes in value */
|
||||
len += z;
|
||||
|
||||
/* see if msb is set */
|
||||
len += (num&(1UL<<((z<<3) - 1))) ? 1 : 0;
|
||||
|
||||
/* return length */
|
||||
*outlen = len;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
93
thirdparty/libtomcrypt/pk/asn1/der/teletex_string/der_decode_teletex_string.c
vendored
Normal file
93
thirdparty/libtomcrypt/pk/asn1/der/teletex_string/der_decode_teletex_string.c
vendored
Normal file
@ -0,0 +1,93 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_teletex_string.c
|
||||
ASN.1 DER, encode a teletex STRING
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store a teletex STRING
|
||||
@param in The DER encoded teletex STRING
|
||||
@param inlen The size of the DER teletex STRING
|
||||
@param out [out] The array of octets stored (one per char)
|
||||
@param outlen [in/out] The number of octets stored
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_teletex_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, y, len;
|
||||
int t;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* must have header at least */
|
||||
if (inlen < 2) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* check for 0x14 */
|
||||
if ((in[0] & 0x1F) != 0x14) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
x = 1;
|
||||
|
||||
/* decode the length */
|
||||
if (in[x] & 0x80) {
|
||||
/* valid # of bytes in length are 1,2,3 */
|
||||
y = in[x] & 0x7F;
|
||||
if ((y == 0) || (y > 3) || ((x + y) > inlen)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* read the length in */
|
||||
len = 0;
|
||||
++x;
|
||||
while (y--) {
|
||||
len = (len << 8) | in[x++];
|
||||
}
|
||||
} else {
|
||||
len = in[x++] & 0x7F;
|
||||
}
|
||||
|
||||
/* is it too long? */
|
||||
if (len > *outlen) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
if (len + x > inlen) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* read the data */
|
||||
for (y = 0; y < len; y++) {
|
||||
t = der_teletex_value_decode(in[x++]);
|
||||
if (t == -1) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
out[y] = t;
|
||||
}
|
||||
|
||||
*outlen = y;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
208
thirdparty/libtomcrypt/pk/asn1/der/teletex_string/der_length_teletex_string.c
vendored
Normal file
208
thirdparty/libtomcrypt/pk/asn1/der/teletex_string/der_length_teletex_string.c
vendored
Normal file
@ -0,0 +1,208 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_length_teletex_string.c
|
||||
ASN.1 DER, get length of teletex STRING
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
static const struct {
|
||||
int code, value;
|
||||
} teletex_table[] = {
|
||||
{ '\0', 0 },
|
||||
{ '\a', 7 },
|
||||
{ '\b', 8 },
|
||||
{ '\t', 9 },
|
||||
{ '\n', 10 },
|
||||
{ '\v', 11 },
|
||||
{ '\f', 12 },
|
||||
{ '\r', 13 },
|
||||
{ ' ', 32 },
|
||||
{ '!', 33 },
|
||||
{ '"', 34 },
|
||||
{ '%', 37 },
|
||||
{ '&', 38 },
|
||||
{ '\'', 39 },
|
||||
{ '(', 40 },
|
||||
{ ')', 41 },
|
||||
{ '+', 43 },
|
||||
{ ',', 44 },
|
||||
{ '-', 45 },
|
||||
{ '.', 46 },
|
||||
{ '/', 47 },
|
||||
{ '0', 48 },
|
||||
{ '1', 49 },
|
||||
{ '2', 50 },
|
||||
{ '3', 51 },
|
||||
{ '4', 52 },
|
||||
{ '5', 53 },
|
||||
{ '6', 54 },
|
||||
{ '7', 55 },
|
||||
{ '8', 56 },
|
||||
{ '9', 57 },
|
||||
{ ':', 58 },
|
||||
{ ';', 59 },
|
||||
{ '<', 60 },
|
||||
{ '=', 61 },
|
||||
{ '>', 62 },
|
||||
{ '?', 63 },
|
||||
{ '@', 64 },
|
||||
{ 'A', 65 },
|
||||
{ 'B', 66 },
|
||||
{ 'C', 67 },
|
||||
{ 'D', 68 },
|
||||
{ 'E', 69 },
|
||||
{ 'F', 70 },
|
||||
{ 'G', 71 },
|
||||
{ 'H', 72 },
|
||||
{ 'I', 73 },
|
||||
{ 'J', 74 },
|
||||
{ 'K', 75 },
|
||||
{ 'L', 76 },
|
||||
{ 'M', 77 },
|
||||
{ 'N', 78 },
|
||||
{ 'O', 79 },
|
||||
{ 'P', 80 },
|
||||
{ 'Q', 81 },
|
||||
{ 'R', 82 },
|
||||
{ 'S', 83 },
|
||||
{ 'T', 84 },
|
||||
{ 'U', 85 },
|
||||
{ 'V', 86 },
|
||||
{ 'W', 87 },
|
||||
{ 'X', 88 },
|
||||
{ 'Y', 89 },
|
||||
{ 'Z', 90 },
|
||||
{ '[', 91 },
|
||||
{ ']', 93 },
|
||||
{ '_', 95 },
|
||||
{ 'a', 97 },
|
||||
{ 'b', 98 },
|
||||
{ 'c', 99 },
|
||||
{ 'd', 100 },
|
||||
{ 'e', 101 },
|
||||
{ 'f', 102 },
|
||||
{ 'g', 103 },
|
||||
{ 'h', 104 },
|
||||
{ 'i', 105 },
|
||||
{ 'j', 106 },
|
||||
{ 'k', 107 },
|
||||
{ 'l', 108 },
|
||||
{ 'm', 109 },
|
||||
{ 'n', 110 },
|
||||
{ 'o', 111 },
|
||||
{ 'p', 112 },
|
||||
{ 'q', 113 },
|
||||
{ 'r', 114 },
|
||||
{ 's', 115 },
|
||||
{ 't', 116 },
|
||||
{ 'u', 117 },
|
||||
{ 'v', 118 },
|
||||
{ 'w', 119 },
|
||||
{ 'x', 120 },
|
||||
{ 'y', 121 },
|
||||
{ 'z', 122 },
|
||||
{ '|', 124 },
|
||||
{ ' ', 160 },
|
||||
{ 0xa1, 161 },
|
||||
{ 0xa2, 162 },
|
||||
{ 0xa3, 163 },
|
||||
{ '$', 164 },
|
||||
{ 0xa5, 165 },
|
||||
{ '#', 166 },
|
||||
{ 0xa7, 167 },
|
||||
{ 0xa4, 168 },
|
||||
{ 0xab, 171 },
|
||||
{ 0xb0, 176 },
|
||||
{ 0xb1, 177 },
|
||||
{ 0xb2, 178 },
|
||||
{ 0xb3, 179 },
|
||||
{ 0xd7, 180 },
|
||||
{ 0xb5, 181 },
|
||||
{ 0xb6, 182 },
|
||||
{ 0xb7, 183 },
|
||||
{ 0xf7, 184 },
|
||||
{ 0xbb, 187 },
|
||||
{ 0xbc, 188 },
|
||||
{ 0xbd, 189 },
|
||||
{ 0xbe, 190 },
|
||||
{ 0xbf, 191 },
|
||||
};
|
||||
|
||||
int der_teletex_char_encode(int c)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < (int)(sizeof(teletex_table)/sizeof(teletex_table[0])); x++) {
|
||||
if (teletex_table[x].code == c) {
|
||||
return teletex_table[x].value;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int der_teletex_value_decode(int v)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < (int)(sizeof(teletex_table)/sizeof(teletex_table[0])); x++) {
|
||||
if (teletex_table[x].value == v) {
|
||||
return teletex_table[x].code;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
Gets length of DER encoding of teletex STRING
|
||||
@param octets The values you want to encode
|
||||
@param noctets The number of octets in the string to encode
|
||||
@param outlen [out] The length of the DER encoding for the given string
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_teletex_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x;
|
||||
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(octets != NULL);
|
||||
|
||||
/* scan string for validity */
|
||||
for (x = 0; x < noctets; x++) {
|
||||
if (der_teletex_char_encode(octets[x]) == -1) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
|
||||
if (noctets < 128) {
|
||||
/* 16 LL DD DD DD ... */
|
||||
*outlen = 2 + noctets;
|
||||
} else if (noctets < 256) {
|
||||
/* 16 81 LL DD DD DD ... */
|
||||
*outlen = 3 + noctets;
|
||||
} else if (noctets < 65536UL) {
|
||||
/* 16 82 LL LL DD DD DD ... */
|
||||
*outlen = 4 + noctets;
|
||||
} else if (noctets < 16777216UL) {
|
||||
/* 16 83 LL LL LL DD DD DD ... */
|
||||
*outlen = 5 + noctets;
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
125
thirdparty/libtomcrypt/pk/asn1/der/utctime/der_decode_utctime.c
vendored
Normal file
125
thirdparty/libtomcrypt/pk/asn1/der/utctime/der_decode_utctime.c
vendored
Normal file
@ -0,0 +1,125 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_utctime.c
|
||||
ASN.1 DER, decode a UTCTIME, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
static int _char_to_int(unsigned char x)
|
||||
{
|
||||
switch (x) {
|
||||
case '0': return 0;
|
||||
case '1': return 1;
|
||||
case '2': return 2;
|
||||
case '3': return 3;
|
||||
case '4': return 4;
|
||||
case '5': return 5;
|
||||
case '6': return 6;
|
||||
case '7': return 7;
|
||||
case '8': return 8;
|
||||
case '9': return 9;
|
||||
default: return 100;
|
||||
}
|
||||
}
|
||||
|
||||
#define DECODE_V(y, max) \
|
||||
y = _char_to_int(buf[x])*10 + _char_to_int(buf[x+1]); \
|
||||
if (y >= max) return CRYPT_INVALID_PACKET; \
|
||||
x += 2;
|
||||
|
||||
/**
|
||||
Decodes a UTC time structure in DER format (reads all 6 valid encoding formats)
|
||||
@param in Input buffer
|
||||
@param inlen Length of input buffer in octets
|
||||
@param out [out] Destination of UTC time structure
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
|
||||
ltc_utctime *out)
|
||||
{
|
||||
unsigned char buf[32] = { 0 }; /* initialize as all zeroes */
|
||||
unsigned long x;
|
||||
int y;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
|
||||
/* check header */
|
||||
if (*inlen < 2UL || (in[1] >= sizeof(buf)) || ((in[1] + 2UL) > *inlen)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* decode the string */
|
||||
for (x = 0; x < in[1]; x++) {
|
||||
y = der_ia5_value_decode(in[x+2]);
|
||||
if (y == -1) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
buf[x] = y;
|
||||
}
|
||||
*inlen = 2 + x;
|
||||
|
||||
|
||||
/* possible encodings are
|
||||
YYMMDDhhmmZ
|
||||
YYMMDDhhmm+hh'mm'
|
||||
YYMMDDhhmm-hh'mm'
|
||||
YYMMDDhhmmssZ
|
||||
YYMMDDhhmmss+hh'mm'
|
||||
YYMMDDhhmmss-hh'mm'
|
||||
|
||||
So let's do a trivial decode upto [including] mm
|
||||
*/
|
||||
|
||||
x = 0;
|
||||
DECODE_V(out->YY, 100);
|
||||
DECODE_V(out->MM, 13);
|
||||
DECODE_V(out->DD, 32);
|
||||
DECODE_V(out->hh, 24);
|
||||
DECODE_V(out->mm, 60);
|
||||
|
||||
/* clear timezone and seconds info */
|
||||
out->off_dir = out->off_hh = out->off_mm = out->ss = 0;
|
||||
|
||||
/* now is it Z, +, - or 0-9 */
|
||||
if (buf[x] == 'Z') {
|
||||
return CRYPT_OK;
|
||||
} else if (buf[x] == '+' || buf[x] == '-') {
|
||||
out->off_dir = (buf[x++] == '+') ? 0 : 1;
|
||||
DECODE_V(out->off_hh, 24);
|
||||
DECODE_V(out->off_mm, 60);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* decode seconds */
|
||||
DECODE_V(out->ss, 60);
|
||||
|
||||
/* now is it Z, +, - */
|
||||
if (buf[x] == 'Z') {
|
||||
return CRYPT_OK;
|
||||
} else if (buf[x] == '+' || buf[x] == '-') {
|
||||
out->off_dir = (buf[x++] == '+') ? 0 : 1;
|
||||
DECODE_V(out->off_hh, 24);
|
||||
DECODE_V(out->off_mm, 60);
|
||||
return CRYPT_OK;
|
||||
} else {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
81
thirdparty/libtomcrypt/pk/asn1/der/utctime/der_encode_utctime.c
vendored
Normal file
81
thirdparty/libtomcrypt/pk/asn1/der/utctime/der_encode_utctime.c
vendored
Normal file
@ -0,0 +1,81 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_utctime.c
|
||||
ASN.1 DER, encode a UTCTIME, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
static const char * const baseten = "0123456789";
|
||||
|
||||
#define STORE_V(y) \
|
||||
out[x++] = der_ia5_char_encode(baseten[(y/10) % 10]); \
|
||||
out[x++] = der_ia5_char_encode(baseten[y % 10]);
|
||||
|
||||
/**
|
||||
Encodes a UTC time structure in DER format
|
||||
@param utctime The UTC time structure to encode
|
||||
@param out The destination of the DER encoding of the UTC time structure
|
||||
@param outlen [in/out] The length of the DER encoding
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_utctime(ltc_utctime *utctime,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, tmplen;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(utctime != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
if ((err = der_length_utctime(utctime, &tmplen)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if (tmplen > *outlen) {
|
||||
*outlen = tmplen;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* store header */
|
||||
out[0] = 0x17;
|
||||
|
||||
/* store values */
|
||||
x = 2;
|
||||
STORE_V(utctime->YY);
|
||||
STORE_V(utctime->MM);
|
||||
STORE_V(utctime->DD);
|
||||
STORE_V(utctime->hh);
|
||||
STORE_V(utctime->mm);
|
||||
STORE_V(utctime->ss);
|
||||
|
||||
if (utctime->off_mm || utctime->off_hh) {
|
||||
out[x++] = der_ia5_char_encode(utctime->off_dir ? '-' : '+');
|
||||
STORE_V(utctime->off_hh);
|
||||
STORE_V(utctime->off_mm);
|
||||
} else {
|
||||
out[x++] = der_ia5_char_encode('Z');
|
||||
}
|
||||
|
||||
/* store length */
|
||||
out[1] = (unsigned char)(x - 2);
|
||||
|
||||
/* all good let's return */
|
||||
*outlen = x;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
44
thirdparty/libtomcrypt/pk/asn1/der/utctime/der_length_utctime.c
vendored
Normal file
44
thirdparty/libtomcrypt/pk/asn1/der/utctime/der_length_utctime.c
vendored
Normal file
@ -0,0 +1,44 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_length_utctime.c
|
||||
ASN.1 DER, get length of UTCTIME, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Gets length of DER encoding of UTCTIME
|
||||
@param utctime The UTC time structure to get the size of
|
||||
@param outlen [out] The length of the DER encoding
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_utctime(ltc_utctime *utctime, unsigned long *outlen)
|
||||
{
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(utctime != NULL);
|
||||
|
||||
if (utctime->off_hh == 0 && utctime->off_mm == 0) {
|
||||
/* we encode as YYMMDDhhmmssZ */
|
||||
*outlen = 2 + 13;
|
||||
} else {
|
||||
/* we encode as YYMMDDhhmmss{+|-}hh'mm' */
|
||||
*outlen = 2 + 17;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
114
thirdparty/libtomcrypt/pk/asn1/der/utf8/der_decode_utf8_string.c
vendored
Normal file
114
thirdparty/libtomcrypt/pk/asn1/der/utf8/der_decode_utf8_string.c
vendored
Normal file
@ -0,0 +1,114 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_utf8_string.c
|
||||
ASN.1 DER, encode a UTF8 STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store a UTF8 STRING
|
||||
@param in The DER encoded UTF8 STRING
|
||||
@param inlen The size of the DER UTF8 STRING
|
||||
@param out [out] The array of utf8s stored (one per char)
|
||||
@param outlen [in/out] The number of utf8s stored
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
|
||||
wchar_t *out, unsigned long *outlen)
|
||||
{
|
||||
wchar_t tmp;
|
||||
unsigned long x, y, z, len;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* must have header at least */
|
||||
if (inlen < 2) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* check for 0x0C */
|
||||
if ((in[0] & 0x1F) != 0x0C) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
x = 1;
|
||||
|
||||
/* decode the length */
|
||||
if (in[x] & 0x80) {
|
||||
/* valid # of bytes in length are 1,2,3 */
|
||||
y = in[x] & 0x7F;
|
||||
if ((y == 0) || (y > 3) || ((x + y) > inlen)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* read the length in */
|
||||
len = 0;
|
||||
++x;
|
||||
while (y--) {
|
||||
len = (len << 8) | in[x++];
|
||||
}
|
||||
} else {
|
||||
len = in[x++] & 0x7F;
|
||||
}
|
||||
|
||||
if (len + x > inlen) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* proceed to decode */
|
||||
for (y = 0; x < inlen; ) {
|
||||
/* get first byte */
|
||||
tmp = in[x++];
|
||||
|
||||
/* count number of bytes */
|
||||
for (z = 0; (tmp & 0x80) && (z <= 4); z++, tmp = (tmp << 1) & 0xFF);
|
||||
|
||||
if (z > 4 || (x + (z - 1) > inlen)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* decode, grab upper bits */
|
||||
tmp >>= z;
|
||||
|
||||
/* grab remaining bytes */
|
||||
if (z > 1) { --z; }
|
||||
while (z-- != 0) {
|
||||
if ((in[x] & 0xC0) != 0x80) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
tmp = (tmp << 6) | ((wchar_t)in[x++] & 0x3F);
|
||||
}
|
||||
|
||||
if (y < *outlen) {
|
||||
out[y] = tmp;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
if (y > *outlen) {
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
} else {
|
||||
err = CRYPT_OK;
|
||||
}
|
||||
*outlen = y;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
104
thirdparty/libtomcrypt/pk/asn1/der/utf8/der_encode_utf8_string.c
vendored
Normal file
104
thirdparty/libtomcrypt/pk/asn1/der/utf8/der_encode_utf8_string.c
vendored
Normal file
@ -0,0 +1,104 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_utf8_string.c
|
||||
ASN.1 DER, encode a UTF8 STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store an UTF8 STRING
|
||||
@param in The array of UTF8 to store (one per wchar_t)
|
||||
@param inlen The number of UTF8 to store
|
||||
@param out [out] The destination for the DER encoded UTF8 STRING
|
||||
@param outlen [in/out] The max size and resulting size of the DER UTF8 STRING
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, y, len;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* get the size */
|
||||
for (x = len = 0; x < inlen; x++) {
|
||||
if (!der_utf8_valid_char(in[x])) return CRYPT_INVALID_ARG;
|
||||
len += der_utf8_charsize(in[x]);
|
||||
}
|
||||
|
||||
if (len < 128) {
|
||||
y = 2 + len;
|
||||
} else if (len < 256) {
|
||||
y = 3 + len;
|
||||
} else if (len < 65536UL) {
|
||||
y = 4 + len;
|
||||
} else if (len < 16777216UL) {
|
||||
y = 5 + len;
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* too big? */
|
||||
if (y > *outlen) {
|
||||
*outlen = y;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* encode the header+len */
|
||||
x = 0;
|
||||
out[x++] = 0x0C;
|
||||
if (len < 128) {
|
||||
out[x++] = (unsigned char)len;
|
||||
} else if (len < 256) {
|
||||
out[x++] = 0x81;
|
||||
out[x++] = (unsigned char)len;
|
||||
} else if (len < 65536UL) {
|
||||
out[x++] = 0x82;
|
||||
out[x++] = (unsigned char)((len>>8)&255);
|
||||
out[x++] = (unsigned char)(len&255);
|
||||
} else if (len < 16777216UL) {
|
||||
out[x++] = 0x83;
|
||||
out[x++] = (unsigned char)((len>>16)&255);
|
||||
out[x++] = (unsigned char)((len>>8)&255);
|
||||
out[x++] = (unsigned char)(len&255);
|
||||
} else {
|
||||
/* coverity[dead_error_line] */
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* store UTF8 */
|
||||
for (y = 0; y < inlen; y++) {
|
||||
switch (der_utf8_charsize(in[y])) {
|
||||
case 1: out[x++] = (unsigned char)in[y]; break;
|
||||
case 2: out[x++] = 0xC0 | ((in[y] >> 6) & 0x1F); out[x++] = 0x80 | (in[y] & 0x3F); break;
|
||||
case 3: out[x++] = 0xE0 | ((in[y] >> 12) & 0x0F); out[x++] = 0x80 | ((in[y] >> 6) & 0x3F); out[x++] = 0x80 | (in[y] & 0x3F); break;
|
||||
#if !defined(LTC_WCHAR_MAX) || LTC_WCHAR_MAX > 0xFFFF
|
||||
case 4: out[x++] = 0xF0 | ((in[y] >> 18) & 0x07); out[x++] = 0x80 | ((in[y] >> 12) & 0x3F); out[x++] = 0x80 | ((in[y] >> 6) & 0x3F); out[x++] = 0x80 | (in[y] & 0x3F); break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/* retun length */
|
||||
*outlen = x;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
102
thirdparty/libtomcrypt/pk/asn1/der/utf8/der_length_utf8_string.c
vendored
Normal file
102
thirdparty/libtomcrypt/pk/asn1/der/utf8/der_length_utf8_string.c
vendored
Normal file
@ -0,0 +1,102 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_length_utf8_string.c
|
||||
ASN.1 DER, get length of UTF8 STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/** Return the size in bytes of a UTF-8 character
|
||||
@param c The UTF-8 character to measure
|
||||
@return The size in bytes
|
||||
*/
|
||||
unsigned long der_utf8_charsize(const wchar_t c)
|
||||
{
|
||||
if (c <= 0x7F) {
|
||||
return 1;
|
||||
} else if (c <= 0x7FF) {
|
||||
return 2;
|
||||
#if LTC_WCHAR_MAX == 0xFFFF
|
||||
} else {
|
||||
return 3;
|
||||
}
|
||||
#else
|
||||
} else if (c <= 0xFFFF) {
|
||||
return 3;
|
||||
} else {
|
||||
return 4;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
Test whether the given code point is valid character
|
||||
@param c The UTF-8 character to test
|
||||
@return 1 - valid, 0 - invalid
|
||||
*/
|
||||
int der_utf8_valid_char(const wchar_t c)
|
||||
{
|
||||
LTC_UNUSED_PARAM(c);
|
||||
#if !defined(LTC_WCHAR_MAX) || LTC_WCHAR_MAX > 0xFFFF
|
||||
if (c > 0x10FFFF) return 0;
|
||||
#endif
|
||||
#if LTC_WCHAR_MAX != 0xFFFF && LTC_WCHAR_MAX != 0xFFFFFFFF
|
||||
if (c < 0) return 0;
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
Gets length of DER encoding of UTF8 STRING
|
||||
@param in The characters to measure the length of
|
||||
@param noctets The number of octets in the string to encode
|
||||
@param outlen [out] The length of the DER encoding for the given string
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, len;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
len = 0;
|
||||
for (x = 0; x < noctets; x++) {
|
||||
if (!der_utf8_valid_char(in[x])) return CRYPT_INVALID_ARG;
|
||||
len += der_utf8_charsize(in[x]);
|
||||
}
|
||||
|
||||
if (len < 128) {
|
||||
/* 0C LL DD DD DD ... */
|
||||
*outlen = 2 + len;
|
||||
} else if (len < 256) {
|
||||
/* 0C 81 LL DD DD DD ... */
|
||||
*outlen = 3 + len;
|
||||
} else if (len < 65536UL) {
|
||||
/* 0C 82 LL LL DD DD DD ... */
|
||||
*outlen = 4 + len;
|
||||
} else if (len < 16777216UL) {
|
||||
/* 0C 83 LL LL LL DD DD DD ... */
|
||||
*outlen = 5 + len;
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
237
thirdparty/libtomcrypt/pk/dh/dh.c
vendored
Normal file
237
thirdparty/libtomcrypt/pk/dh/dh.c
vendored
Normal file
@ -0,0 +1,237 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_MDH
|
||||
|
||||
/* This holds the key settings. ***MUST*** be organized by size from smallest to largest. */
|
||||
const ltc_dh_set_type ltc_dh_sets[] = {
|
||||
#ifdef LTC_DH768
|
||||
{ /* 768-bit MODP Group 1 - https://tools.ietf.org/html/rfc7296#appendix-B.1 */
|
||||
96,
|
||||
"DH-768",
|
||||
"2",
|
||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
||||
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
||||
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
||||
"E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF"
|
||||
},
|
||||
#endif
|
||||
#ifdef LTC_DH1024
|
||||
{ /* 1024-bit MODP Group 2 - https://tools.ietf.org/html/rfc7296#appendix-B.2 */
|
||||
128,
|
||||
"DH-1024",
|
||||
"2",
|
||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
||||
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
||||
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
||||
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
||||
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381"
|
||||
"FFFFFFFFFFFFFFFF"
|
||||
},
|
||||
#endif
|
||||
#ifdef LTC_DH1536
|
||||
{ /* 1536-bit MODP Group 5 - https://tools.ietf.org/html/rfc3526#section-2 */
|
||||
192,
|
||||
"DH-1536",
|
||||
"2",
|
||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
||||
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
||||
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
||||
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
||||
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
||||
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
||||
"83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
||||
"670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF"
|
||||
},
|
||||
#endif
|
||||
#ifdef LTC_DH2048
|
||||
{ /* 2048-bit MODP Group 14 - https://tools.ietf.org/html/rfc3526#section-3 */
|
||||
256,
|
||||
"DH-2048",
|
||||
"2",
|
||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
||||
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
||||
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
||||
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
||||
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
||||
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
||||
"83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
||||
"670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
|
||||
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
|
||||
"DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
|
||||
"15728E5A8AACAA68FFFFFFFFFFFFFFFF"
|
||||
},
|
||||
#endif
|
||||
#ifdef LTC_DH3072
|
||||
{ /* 3072-bit MODP Group 15 - https://tools.ietf.org/html/rfc3526#section-4 */
|
||||
384,
|
||||
"DH-3072",
|
||||
"2",
|
||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
||||
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
||||
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
||||
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
||||
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
||||
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
||||
"83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
||||
"670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
|
||||
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
|
||||
"DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
|
||||
"15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
|
||||
"ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
|
||||
"ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
|
||||
"F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
|
||||
"BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
|
||||
"43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF"
|
||||
},
|
||||
#endif
|
||||
#ifdef LTC_DH4096
|
||||
{ /* 4096-bit MODP Group 16 - https://tools.ietf.org/html/rfc3526#section-5 */
|
||||
512,
|
||||
"DH-4096",
|
||||
"2",
|
||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
||||
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
||||
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
||||
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
||||
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
||||
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
||||
"83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
||||
"670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
|
||||
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
|
||||
"DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
|
||||
"15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
|
||||
"ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
|
||||
"ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
|
||||
"F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
|
||||
"BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
|
||||
"43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7"
|
||||
"88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA"
|
||||
"2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6"
|
||||
"287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED"
|
||||
"1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9"
|
||||
"93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199"
|
||||
"FFFFFFFFFFFFFFFF"
|
||||
},
|
||||
#endif
|
||||
#ifdef LTC_DH6144
|
||||
{ /* 6144-bit MODP Group 17 - https://tools.ietf.org/html/rfc3526#section-6 */
|
||||
768,
|
||||
"DH-6144",
|
||||
"2",
|
||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
||||
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
||||
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
||||
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
||||
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
||||
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
||||
"83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
||||
"670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
|
||||
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
|
||||
"DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
|
||||
"15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
|
||||
"ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
|
||||
"ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
|
||||
"F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
|
||||
"BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
|
||||
"43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7"
|
||||
"88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA"
|
||||
"2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6"
|
||||
"287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED"
|
||||
"1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9"
|
||||
"93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492"
|
||||
"36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD"
|
||||
"F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831"
|
||||
"179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B"
|
||||
"DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF"
|
||||
"5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6"
|
||||
"D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3"
|
||||
"23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA"
|
||||
"CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328"
|
||||
"06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C"
|
||||
"DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE"
|
||||
"12BF2D5B0B7474D6E694F91E6DCC4024FFFFFFFFFFFFFFFF"
|
||||
},
|
||||
#endif
|
||||
#ifdef LTC_DH8192
|
||||
{ /* 8192-bit MODP Group 18 - https://tools.ietf.org/html/rfc3526#section-7 */
|
||||
1024,
|
||||
"DH-8192",
|
||||
"2",
|
||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
||||
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
||||
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
||||
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
||||
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
||||
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
||||
"83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
||||
"670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
|
||||
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
|
||||
"DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
|
||||
"15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
|
||||
"ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
|
||||
"ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
|
||||
"F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
|
||||
"BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
|
||||
"43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7"
|
||||
"88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA"
|
||||
"2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6"
|
||||
"287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED"
|
||||
"1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9"
|
||||
"93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492"
|
||||
"36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD"
|
||||
"F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831"
|
||||
"179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B"
|
||||
"DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF"
|
||||
"5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6"
|
||||
"D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3"
|
||||
"23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA"
|
||||
"CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328"
|
||||
"06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C"
|
||||
"DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE"
|
||||
"12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4"
|
||||
"38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300"
|
||||
"741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568"
|
||||
"3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9"
|
||||
"22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B"
|
||||
"4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A"
|
||||
"062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36"
|
||||
"4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1"
|
||||
"B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92"
|
||||
"4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47"
|
||||
"9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71"
|
||||
"60C980DD98EDD3DFFFFFFFFFFFFFFFFF"
|
||||
},
|
||||
#endif
|
||||
{
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
Returns the DH group size (octets) for given key
|
||||
@param key The DH key to get the size of
|
||||
@return The group size in octets (0 on error)
|
||||
*/
|
||||
int dh_get_groupsize(dh_key *key)
|
||||
{
|
||||
if (key == NULL) return 0;
|
||||
return mp_unsigned_bin_size(key->prime);
|
||||
}
|
||||
|
||||
#endif /* LTC_MDH */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
65
thirdparty/libtomcrypt/pk/dh/dh_check_pubkey.c
vendored
Normal file
65
thirdparty/libtomcrypt/pk/dh/dh_check_pubkey.c
vendored
Normal file
@ -0,0 +1,65 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_MDH
|
||||
|
||||
/**
|
||||
Check DH public key (INTERNAL ONLY, not part of public API)
|
||||
@param key The key you wish to test
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dh_check_pubkey(dh_key *key)
|
||||
{
|
||||
void *p_minus1;
|
||||
ltc_mp_digit digit;
|
||||
int i, digit_count, bits_set = 0, err;
|
||||
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
if ((err = mp_init(&p_minus1)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* avoid: y <= 1 OR y >= p-1 */
|
||||
if ((err = mp_sub_d(key->prime, 1, p_minus1)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
if (mp_cmp(key->y, p_minus1) != LTC_MP_LT || mp_cmp_d(key->y, 1) != LTC_MP_GT) {
|
||||
err = CRYPT_INVALID_ARG;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* public key must have more than one bit set */
|
||||
digit_count = mp_get_digit_count(key->y);
|
||||
for (i = 0; i < digit_count && bits_set < 2; i++) {
|
||||
digit = mp_get_digit(key->y, i);
|
||||
while (digit > 0) {
|
||||
if (digit & 1) bits_set++;
|
||||
digit >>= 1;
|
||||
}
|
||||
}
|
||||
if (bits_set > 1) {
|
||||
err = CRYPT_OK;
|
||||
}
|
||||
else {
|
||||
err = CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
error:
|
||||
mp_clear(p_minus1);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif /* LTC_MDH */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
62
thirdparty/libtomcrypt/pk/dh/dh_export.c
vendored
Normal file
62
thirdparty/libtomcrypt/pk/dh/dh_export.c
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_MDH
|
||||
|
||||
/**
|
||||
Export a DH key to a binary packet
|
||||
@param out [out] The destination for the key
|
||||
@param outlen [in/out] The max size and resulting size of the DH key
|
||||
@param type Which type of key (PK_PRIVATE or PK_PUBLIC)
|
||||
@param key The key you wish to export
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key)
|
||||
{
|
||||
unsigned char flags[1];
|
||||
int err;
|
||||
unsigned long version = 0;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
if (type == PK_PRIVATE) {
|
||||
/* export x - private key */
|
||||
flags[0] = 1;
|
||||
err = der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_SHORT_INTEGER, 1UL, &version,
|
||||
LTC_ASN1_BIT_STRING, 1UL, flags,
|
||||
LTC_ASN1_INTEGER, 1UL, key->prime,
|
||||
LTC_ASN1_INTEGER, 1UL, key->base,
|
||||
LTC_ASN1_INTEGER, 1UL, key->x,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
}
|
||||
else {
|
||||
/* export y - public key */
|
||||
flags[0] = 0;
|
||||
err = der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_SHORT_INTEGER, 1UL, &version,
|
||||
LTC_ASN1_BIT_STRING, 1UL, flags,
|
||||
LTC_ASN1_INTEGER, 1UL, key->prime,
|
||||
LTC_ASN1_INTEGER, 1UL, key->base,
|
||||
LTC_ASN1_INTEGER, 1UL, key->y,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif /* LTC_MDH */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
47
thirdparty/libtomcrypt/pk/dh/dh_export_key.c
vendored
Normal file
47
thirdparty/libtomcrypt/pk/dh/dh_export_key.c
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_MDH
|
||||
|
||||
/**
|
||||
Binary export a DH key to a buffer
|
||||
@param out [out] The destination for the key
|
||||
@param outlen [in/out] The max size and resulting size of the DH key
|
||||
@param type Which type of key (PK_PRIVATE or PK_PUBLIC)
|
||||
@param key The key you wish to export
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dh_export_key(void *out, unsigned long *outlen, int type, dh_key *key)
|
||||
{
|
||||
unsigned long len;
|
||||
void *k;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
k = (type == PK_PRIVATE) ? key->x : key->y;
|
||||
len = mp_unsigned_bin_size(k);
|
||||
|
||||
if (*outlen < len) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
*outlen = len;
|
||||
|
||||
return mp_to_unsigned_bin(k, out);
|
||||
}
|
||||
|
||||
#endif /* LTC_MDH */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
28
thirdparty/libtomcrypt/pk/dh/dh_free.c
vendored
Normal file
28
thirdparty/libtomcrypt/pk/dh/dh_free.c
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_MDH
|
||||
|
||||
/**
|
||||
Free the allocated ram for a DH key
|
||||
@param key The key which you wish to free
|
||||
*/
|
||||
void dh_free(dh_key *key)
|
||||
{
|
||||
LTC_ARGCHKVD(key != NULL);
|
||||
mp_cleanup_multi(&key->prime, &key->base, &key->y, &key->x, NULL);
|
||||
}
|
||||
|
||||
#endif /* LTC_MDH */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
102
thirdparty/libtomcrypt/pk/dh/dh_generate_key.c
vendored
Normal file
102
thirdparty/libtomcrypt/pk/dh/dh_generate_key.c
vendored
Normal file
@ -0,0 +1,102 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_MDH
|
||||
|
||||
static int _dh_groupsize_to_keysize(int groupsize)
|
||||
{
|
||||
/* The strength estimates from https://tools.ietf.org/html/rfc3526#section-8
|
||||
* We use "Estimate 2" to get an appropriate private key (exponent) size.
|
||||
*/
|
||||
if (groupsize <= 0) {
|
||||
return 0;
|
||||
}
|
||||
else if (groupsize <= 192) {
|
||||
return 30; /* 1536-bit => key size 240-bit */
|
||||
}
|
||||
else if (groupsize <= 256) {
|
||||
return 40; /* 2048-bit => key size 320-bit */
|
||||
}
|
||||
else if (groupsize <= 384) {
|
||||
return 52; /* 3072-bit => key size 416-bit */
|
||||
}
|
||||
else if (groupsize <= 512) {
|
||||
return 60; /* 4096-bit => key size 480-bit */
|
||||
}
|
||||
else if (groupsize <= 768) {
|
||||
return 67; /* 6144-bit => key size 536-bit */
|
||||
}
|
||||
else if (groupsize <= 1024) {
|
||||
return 77; /* 8192-bit => key size 616-bit */
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int dh_generate_key(prng_state *prng, int wprng, dh_key *key)
|
||||
{
|
||||
unsigned char *buf;
|
||||
unsigned long keysize;
|
||||
int err, max_iterations = LTC_PK_MAX_RETRIES;
|
||||
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(ltc_mp.name != NULL);
|
||||
|
||||
/* good prng? */
|
||||
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
keysize = _dh_groupsize_to_keysize(mp_unsigned_bin_size(key->prime));
|
||||
if (keysize == 0) {
|
||||
err = CRYPT_INVALID_KEYSIZE;
|
||||
goto freemp;
|
||||
}
|
||||
|
||||
/* allocate buffer */
|
||||
buf = XMALLOC(keysize);
|
||||
if (buf == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto freemp;
|
||||
}
|
||||
|
||||
key->type = PK_PRIVATE;
|
||||
do {
|
||||
/* make up random buf */
|
||||
if (prng_descriptor[wprng].read(buf, keysize, prng) != keysize) {
|
||||
err = CRYPT_ERROR_READPRNG;
|
||||
goto freebuf;
|
||||
}
|
||||
/* load the x value - private key */
|
||||
if ((err = mp_read_unsigned_bin(key->x, buf, keysize)) != CRYPT_OK) {
|
||||
goto freebuf;
|
||||
}
|
||||
/* compute the y value - public key */
|
||||
if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) {
|
||||
goto freebuf;
|
||||
}
|
||||
err = dh_check_pubkey(key);
|
||||
} while (err != CRYPT_OK && max_iterations-- > 0);
|
||||
|
||||
freebuf:
|
||||
zeromem(buf, keysize);
|
||||
XFREE(buf);
|
||||
freemp:
|
||||
if (err != CRYPT_OK) dh_free(key);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif /* LTC_MDH */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
99
thirdparty/libtomcrypt/pk/dh/dh_import.c
vendored
Normal file
99
thirdparty/libtomcrypt/pk/dh/dh_import.c
vendored
Normal file
@ -0,0 +1,99 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_MDH
|
||||
|
||||
/**
|
||||
Import a DH key from a binary packet
|
||||
@param in The packet to read
|
||||
@param inlen The length of the input packet
|
||||
@param key [out] Where to import the key to
|
||||
@return CRYPT_OK if successful, on error all allocated memory is freed automatically
|
||||
*/
|
||||
int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key)
|
||||
{
|
||||
unsigned char flags[1];
|
||||
int err;
|
||||
unsigned long version;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
/* init */
|
||||
if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* find out what type of key it is */
|
||||
err = der_decode_sequence_multi(in, inlen,
|
||||
LTC_ASN1_SHORT_INTEGER, 1UL, &version,
|
||||
LTC_ASN1_BIT_STRING, 1UL, &flags,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (version == 0) {
|
||||
if (flags[0] == 1) {
|
||||
key->type = PK_PRIVATE;
|
||||
if ((err = der_decode_sequence_multi(in, inlen,
|
||||
LTC_ASN1_SHORT_INTEGER, 1UL, &version,
|
||||
LTC_ASN1_BIT_STRING, 1UL, flags,
|
||||
LTC_ASN1_INTEGER, 1UL, key->prime,
|
||||
LTC_ASN1_INTEGER, 1UL, key->base,
|
||||
LTC_ASN1_INTEGER, 1UL, key->x,
|
||||
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
/* compute public key: y = (base ^ x) mod prime */
|
||||
if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
else if (flags[0] == 0) {
|
||||
key->type = PK_PUBLIC;
|
||||
if ((err = der_decode_sequence_multi(in, inlen,
|
||||
LTC_ASN1_SHORT_INTEGER, 1UL, &version,
|
||||
LTC_ASN1_BIT_STRING, 1UL, flags,
|
||||
LTC_ASN1_INTEGER, 1UL, key->prime,
|
||||
LTC_ASN1_INTEGER, 1UL, key->base,
|
||||
LTC_ASN1_INTEGER, 1UL, key->y,
|
||||
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
else {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
else {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* check public key */
|
||||
if ((err = dh_check_pubkey(key)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
|
||||
error:
|
||||
dh_free(key);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif /* LTC_MDH */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
124
thirdparty/libtomcrypt/pk/dh/dh_set.c
vendored
Normal file
124
thirdparty/libtomcrypt/pk/dh/dh_set.c
vendored
Normal file
@ -0,0 +1,124 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_MDH
|
||||
|
||||
/**
|
||||
Import DH key parts p and g from raw numbers
|
||||
|
||||
@param p DH's p (prime)
|
||||
@param plen DH's p's length
|
||||
@param g DH's g (group)
|
||||
@param glen DH's g's length
|
||||
@param key [out] the destination for the imported key
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dh_set_pg(const unsigned char *p, unsigned long plen,
|
||||
const unsigned char *g, unsigned long glen,
|
||||
dh_key *key)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(p != NULL);
|
||||
LTC_ARGCHK(g != NULL);
|
||||
LTC_ARGCHK(ltc_mp.name != NULL);
|
||||
|
||||
if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if ((err = mp_read_unsigned_bin(key->base, (unsigned char*)g, glen)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
if ((err = mp_read_unsigned_bin(key->prime, (unsigned char*)p, plen)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
|
||||
return CRYPT_OK;
|
||||
|
||||
LBL_ERR:
|
||||
dh_free(key);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Import DH key parts p and g from built-in DH groups
|
||||
|
||||
@param groupsize The size of the DH group to use
|
||||
@param key [out] Where the newly created DH key will be stored
|
||||
@return CRYPT_OK if successful, note: on error all allocated memory will be freed automatically.
|
||||
*/
|
||||
int dh_set_pg_groupsize(int groupsize, dh_key *key)
|
||||
{
|
||||
int err, i;
|
||||
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(ltc_mp.name != NULL);
|
||||
LTC_ARGCHK(groupsize > 0);
|
||||
|
||||
for (i = 0; (groupsize > ltc_dh_sets[i].size) && (ltc_dh_sets[i].size != 0); i++);
|
||||
if (ltc_dh_sets[i].size == 0) return CRYPT_INVALID_KEYSIZE;
|
||||
|
||||
if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if ((err = mp_read_radix(key->base, ltc_dh_sets[i].base, 16)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
if ((err = mp_read_radix(key->prime, ltc_dh_sets[i].prime, 16)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
|
||||
return CRYPT_OK;
|
||||
|
||||
LBL_ERR:
|
||||
dh_free(key);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Import DH public or private key part from raw numbers
|
||||
|
||||
NB: The p & g parts must be set beforehand
|
||||
|
||||
@param in The key-part to import, either public or private.
|
||||
@param inlen The key-part's length
|
||||
@param type Which type of key (PK_PRIVATE or PK_PUBLIC)
|
||||
@param key [out] the destination for the imported key
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(ltc_mp.name != NULL);
|
||||
|
||||
if (type == PK_PRIVATE) {
|
||||
key->type = PK_PRIVATE;
|
||||
if ((err = mp_read_unsigned_bin(key->x, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
}
|
||||
else {
|
||||
key->type = PK_PUBLIC;
|
||||
if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
}
|
||||
|
||||
/* check public key */
|
||||
if ((err = dh_check_pubkey(key)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
|
||||
LBL_ERR:
|
||||
dh_free(key);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif /* LTC_MDH */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
54
thirdparty/libtomcrypt/pk/dh/dh_set_pg_dhparam.c
vendored
Normal file
54
thirdparty/libtomcrypt/pk/dh/dh_set_pg_dhparam.c
vendored
Normal file
@ -0,0 +1,54 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_MDH
|
||||
|
||||
/**
|
||||
Import DH key parts p and g from dhparam
|
||||
|
||||
dhparam data: openssl dhparam -outform DER -out dhparam.der 2048
|
||||
|
||||
@param dhparam The DH param DER encoded data
|
||||
@param dhparamlen The length of dhparam data
|
||||
@param key [out] Where the newly created DH key will be stored
|
||||
@return CRYPT_OK if successful, note: on error all allocated memory will be freed automatically.
|
||||
*/
|
||||
int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh_key *key)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(ltc_mp.name != NULL);
|
||||
LTC_ARGCHK(dhparam != NULL);
|
||||
LTC_ARGCHK(dhparamlen > 0);
|
||||
|
||||
if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if ((err = der_decode_sequence_multi(dhparam, dhparamlen,
|
||||
LTC_ASN1_INTEGER, 1UL, key->prime,
|
||||
LTC_ASN1_INTEGER, 1UL, key->base,
|
||||
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
|
||||
LBL_ERR:
|
||||
dh_free(key);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif /* LTC_MDH */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
80
thirdparty/libtomcrypt/pk/dh/dh_shared_secret.c
vendored
Normal file
80
thirdparty/libtomcrypt/pk/dh/dh_shared_secret.c
vendored
Normal file
@ -0,0 +1,80 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_MDH
|
||||
|
||||
/**
|
||||
Create a DH shared secret.
|
||||
@param private_key The private DH key in the pair
|
||||
@param public_key The public DH key in the pair
|
||||
@param out [out] The destination of the shared data
|
||||
@param outlen [in/out] The max size and resulting size of the shared data.
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dh_shared_secret(dh_key *private_key, dh_key *public_key,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
void *tmp;
|
||||
unsigned long x;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(private_key != NULL);
|
||||
LTC_ARGCHK(public_key != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* types valid? */
|
||||
if (private_key->type != PK_PRIVATE) {
|
||||
return CRYPT_PK_NOT_PRIVATE;
|
||||
}
|
||||
|
||||
/* same DH group? */
|
||||
if (mp_cmp(private_key->prime, public_key->prime) != LTC_MP_EQ) { return CRYPT_PK_TYPE_MISMATCH; }
|
||||
if (mp_cmp(private_key->base, public_key->base) != LTC_MP_EQ) { return CRYPT_PK_TYPE_MISMATCH; }
|
||||
|
||||
/* init big numbers */
|
||||
if ((err = mp_init(&tmp)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* check public key */
|
||||
if ((err = dh_check_pubkey(public_key)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* compute tmp = y^x mod p */
|
||||
if ((err = mp_exptmod(public_key->y, private_key->x, private_key->prime, tmp)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* enough space for output? */
|
||||
x = (unsigned long)mp_unsigned_bin_size(tmp);
|
||||
if (*outlen < x) {
|
||||
*outlen = x;
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
goto error;
|
||||
}
|
||||
if ((err = mp_to_unsigned_bin(tmp, out)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
*outlen = x;
|
||||
err = CRYPT_OK;
|
||||
|
||||
error:
|
||||
mp_clear(tmp);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif /* LTC_MDH */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
139
thirdparty/libtomcrypt/pk/dsa/dsa_decrypt_key.c
vendored
Normal file
139
thirdparty/libtomcrypt/pk/dsa/dsa_decrypt_key.c
vendored
Normal file
@ -0,0 +1,139 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file dsa_decrypt_key.c
|
||||
DSA Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/**
|
||||
Decrypt an DSA encrypted key
|
||||
@param in The ciphertext
|
||||
@param inlen The length of the ciphertext (octets)
|
||||
@param out [out] The plaintext
|
||||
@param outlen [in/out] The max size and resulting size of the plaintext
|
||||
@param key The corresponding private DSA key
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dsa_decrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
dsa_key *key)
|
||||
{
|
||||
unsigned char *skey, *expt;
|
||||
void *g_pub;
|
||||
unsigned long x, y;
|
||||
unsigned long hashOID[32] = { 0 };
|
||||
int hash, err;
|
||||
ltc_asn1_list decode[3];
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
/* right key type? */
|
||||
if (key->type != PK_PRIVATE) {
|
||||
return CRYPT_PK_NOT_PRIVATE;
|
||||
}
|
||||
|
||||
/* decode to find out hash */
|
||||
LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, sizeof(hashOID)/sizeof(hashOID[0]));
|
||||
err = der_decode_sequence(in, inlen, decode, 1);
|
||||
if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
|
||||
return err;
|
||||
}
|
||||
|
||||
hash = find_hash_oid(hashOID, decode[0].size);
|
||||
if (hash_is_valid(hash) != CRYPT_OK) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* we now have the hash! */
|
||||
|
||||
if ((err = mp_init(&g_pub)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* allocate memory */
|
||||
expt = XMALLOC(mp_unsigned_bin_size(key->p) + 1);
|
||||
skey = XMALLOC(MAXBLOCKSIZE);
|
||||
if (expt == NULL || skey == NULL) {
|
||||
if (expt != NULL) {
|
||||
XFREE(expt);
|
||||
}
|
||||
if (skey != NULL) {
|
||||
XFREE(skey);
|
||||
}
|
||||
mp_clear(g_pub);
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
LTC_SET_ASN1(decode, 1, LTC_ASN1_INTEGER, g_pub, 1UL);
|
||||
LTC_SET_ASN1(decode, 2, LTC_ASN1_OCTET_STRING, skey, MAXBLOCKSIZE);
|
||||
|
||||
/* read the structure in now */
|
||||
if ((err = der_decode_sequence(in, inlen, decode, 3)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* make shared key */
|
||||
x = mp_unsigned_bin_size(key->p) + 1;
|
||||
if ((err = dsa_shared_secret(key->x, g_pub, key, expt, &x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
y = mp_unsigned_bin_size(key->p) + 1;
|
||||
y = MIN(y, MAXBLOCKSIZE);
|
||||
if ((err = hash_memory(hash, expt, x, expt, &y)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* ensure the hash of the shared secret is at least as big as the encrypt itself */
|
||||
if (decode[2].size > y) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* avoid buffer overflow */
|
||||
if (*outlen < decode[2].size) {
|
||||
*outlen = decode[2].size;
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* Decrypt the key */
|
||||
for (x = 0; x < decode[2].size; x++) {
|
||||
out[x] = expt[x] ^ skey[x];
|
||||
}
|
||||
*outlen = x;
|
||||
|
||||
err = CRYPT_OK;
|
||||
LBL_ERR:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(expt, mp_unsigned_bin_size(key->p) + 1);
|
||||
zeromem(skey, MAXBLOCKSIZE);
|
||||
#endif
|
||||
|
||||
XFREE(expt);
|
||||
XFREE(skey);
|
||||
|
||||
mp_clear(g_pub);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
128
thirdparty/libtomcrypt/pk/dsa/dsa_encrypt_key.c
vendored
Normal file
128
thirdparty/libtomcrypt/pk/dsa/dsa_encrypt_key.c
vendored
Normal file
@ -0,0 +1,128 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file dsa_encrypt_key.c
|
||||
DSA Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/**
|
||||
Encrypt a symmetric key with DSA
|
||||
@param in The symmetric key you want to encrypt
|
||||
@param inlen The length of the key to encrypt (octets)
|
||||
@param out [out] The destination for the ciphertext
|
||||
@param outlen [in/out] The max size and resulting size of the ciphertext
|
||||
@param prng An active PRNG state
|
||||
@param wprng The index of the PRNG you wish to use
|
||||
@param hash The index of the hash you want to use
|
||||
@param key The DSA key you want to encrypt to
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dsa_encrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, int hash,
|
||||
dsa_key *key)
|
||||
{
|
||||
unsigned char *expt, *skey;
|
||||
void *g_pub, *g_priv;
|
||||
unsigned long x, y;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
/* check that wprng/cipher/hash are not invalid */
|
||||
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if ((err = hash_is_valid(hash)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (inlen > hash_descriptor[hash].hashsize) {
|
||||
return CRYPT_INVALID_HASH;
|
||||
}
|
||||
|
||||
/* make a random key and export the public copy */
|
||||
if ((err = mp_init_multi(&g_pub, &g_priv, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
expt = XMALLOC(mp_unsigned_bin_size(key->p) + 1);
|
||||
skey = XMALLOC(MAXBLOCKSIZE);
|
||||
if (expt == NULL || skey == NULL) {
|
||||
if (expt != NULL) {
|
||||
XFREE(expt);
|
||||
}
|
||||
if (skey != NULL) {
|
||||
XFREE(skey);
|
||||
}
|
||||
mp_clear_multi(g_pub, g_priv, NULL);
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* make a random g_priv, g_pub = g^x pair
|
||||
private key x should be in range: 1 <= x <= q-1 (see FIPS 186-4 B.1.2)
|
||||
*/
|
||||
if ((err = rand_bn_upto(g_priv, key->q, prng, wprng)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* compute y */
|
||||
if ((err = mp_exptmod(key->g, g_priv, key->p, g_pub)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* make random key */
|
||||
x = mp_unsigned_bin_size(key->p) + 1;
|
||||
if ((err = dsa_shared_secret(g_priv, key->y, key, expt, &x)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
y = MAXBLOCKSIZE;
|
||||
if ((err = hash_memory(hash, expt, x, skey, &y)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* Encrypt key */
|
||||
for (x = 0; x < inlen; x++) {
|
||||
skey[x] ^= in[x];
|
||||
}
|
||||
|
||||
err = der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_OBJECT_IDENTIFIER, hash_descriptor[hash].OIDlen, hash_descriptor[hash].OID,
|
||||
LTC_ASN1_INTEGER, 1UL, g_pub,
|
||||
LTC_ASN1_OCTET_STRING, inlen, skey,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
|
||||
LBL_ERR:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
/* clean up */
|
||||
zeromem(expt, mp_unsigned_bin_size(key->p) + 1);
|
||||
zeromem(skey, MAXBLOCKSIZE);
|
||||
#endif
|
||||
|
||||
XFREE(skey);
|
||||
XFREE(expt);
|
||||
|
||||
mp_clear_multi(g_pub, g_priv, NULL);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
116
thirdparty/libtomcrypt/pk/dsa/dsa_export.c
vendored
Normal file
116
thirdparty/libtomcrypt/pk/dsa/dsa_export.c
vendored
Normal file
@ -0,0 +1,116 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file dsa_export.c
|
||||
DSA implementation, export key, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/**
|
||||
Export a DSA key to a binary packet
|
||||
@param out [out] Where to store the packet
|
||||
@param outlen [in/out] The max size and resulting size of the packet
|
||||
@param type The type of key to export (PK_PRIVATE or PK_PUBLIC)
|
||||
@param key The key to export
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key)
|
||||
{
|
||||
unsigned long zero=0;
|
||||
int err, std;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
std = type & PK_STD;
|
||||
type &= ~PK_STD;
|
||||
|
||||
/* can we store the static header? */
|
||||
if (type == PK_PRIVATE && key->type != PK_PRIVATE) {
|
||||
return CRYPT_PK_TYPE_MISMATCH;
|
||||
}
|
||||
|
||||
if (type != PK_PUBLIC && type != PK_PRIVATE) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (type == PK_PRIVATE) {
|
||||
if (std) {
|
||||
return der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_SHORT_INTEGER, 1UL, &zero,
|
||||
LTC_ASN1_INTEGER, 1UL, key->p,
|
||||
LTC_ASN1_INTEGER, 1UL, key->q,
|
||||
LTC_ASN1_INTEGER, 1UL, key->g,
|
||||
LTC_ASN1_INTEGER, 1UL, key->y,
|
||||
LTC_ASN1_INTEGER, 1UL, key->x,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
}
|
||||
else {
|
||||
unsigned char flags[1];
|
||||
flags[0] = 1;
|
||||
return der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_BIT_STRING, 1UL, flags,
|
||||
LTC_ASN1_INTEGER, 1UL, key->g,
|
||||
LTC_ASN1_INTEGER, 1UL, key->p,
|
||||
LTC_ASN1_INTEGER, 1UL, key->q,
|
||||
LTC_ASN1_INTEGER, 1UL, key->y,
|
||||
LTC_ASN1_INTEGER, 1UL, key->x,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
}
|
||||
} else {
|
||||
if (std) {
|
||||
unsigned long tmplen = (unsigned long)(mp_count_bits(key->y) / 8) + 8;
|
||||
unsigned char* tmp = XMALLOC(tmplen);
|
||||
ltc_asn1_list int_list[3];
|
||||
|
||||
if (tmp == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
err = der_encode_integer(key->y, tmp, &tmplen);
|
||||
if (err != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
LTC_SET_ASN1(int_list, 0, LTC_ASN1_INTEGER, key->p, 1UL);
|
||||
LTC_SET_ASN1(int_list, 1, LTC_ASN1_INTEGER, key->q, 1UL);
|
||||
LTC_SET_ASN1(int_list, 2, LTC_ASN1_INTEGER, key->g, 1UL);
|
||||
|
||||
err = der_encode_subject_public_key_info(out, outlen, PKA_DSA, tmp,
|
||||
tmplen, LTC_ASN1_SEQUENCE, int_list,
|
||||
sizeof(int_list) / sizeof(int_list[0]));
|
||||
|
||||
error:
|
||||
XFREE(tmp);
|
||||
return err;
|
||||
}
|
||||
else {
|
||||
unsigned char flags[1];
|
||||
flags[0] = 0;
|
||||
return der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_BIT_STRING, 1UL, flags,
|
||||
LTC_ASN1_INTEGER, 1UL, key->g,
|
||||
LTC_ASN1_INTEGER, 1UL, key->p,
|
||||
LTC_ASN1_INTEGER, 1UL, key->q,
|
||||
LTC_ASN1_INTEGER, 1UL, key->y,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
33
thirdparty/libtomcrypt/pk/dsa/dsa_free.c
vendored
Normal file
33
thirdparty/libtomcrypt/pk/dsa/dsa_free.c
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file dsa_free.c
|
||||
DSA implementation, free a DSA key, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/**
|
||||
Free a DSA key
|
||||
@param key The key to free from memory
|
||||
*/
|
||||
void dsa_free(dsa_key *key)
|
||||
{
|
||||
LTC_ARGCHKVD(key != NULL);
|
||||
mp_cleanup_multi(&key->y, &key->x, &key->q, &key->g, &key->p, NULL);
|
||||
key->type = key->qord = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
47
thirdparty/libtomcrypt/pk/dsa/dsa_generate_key.c
vendored
Normal file
47
thirdparty/libtomcrypt/pk/dsa/dsa_generate_key.c
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file dsa_make_key.c
|
||||
DSA implementation, generate a DSA key
|
||||
*/
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/**
|
||||
Create a DSA key
|
||||
@param prng An active PRNG state
|
||||
@param wprng The index of the PRNG desired
|
||||
@param key [in/out] Where to store the created key
|
||||
@return CRYPT_OK if successful.
|
||||
*/
|
||||
int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(ltc_mp.name != NULL);
|
||||
|
||||
/* so now we have our DH structure, generator g, order q, modulus p
|
||||
Now we need a random exponent [mod q] and it's power g^x mod p
|
||||
*/
|
||||
/* private key x should be from range: 1 <= x <= q-1 (see FIPS 186-4 B.1.2) */
|
||||
if ((err = rand_bn_upto(key->x, key->q, prng, wprng)) != CRYPT_OK) { return err; }
|
||||
if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { return err; }
|
||||
key->type = PK_PRIVATE;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
244
thirdparty/libtomcrypt/pk/dsa/dsa_generate_pqg.c
vendored
Normal file
244
thirdparty/libtomcrypt/pk/dsa/dsa_generate_pqg.c
vendored
Normal file
@ -0,0 +1,244 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file dsa_generate_pqg.c
|
||||
DSA implementation - generate DSA parameters p, q & g
|
||||
*/
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/**
|
||||
Create DSA parameters (INTERNAL ONLY, not part of public API)
|
||||
@param prng An active PRNG state
|
||||
@param wprng The index of the PRNG desired
|
||||
@param group_size Size of the multiplicative group (octets)
|
||||
@param modulus_size Size of the modulus (octets)
|
||||
@param p [out] bignum where generated 'p' is stored (must be initialized by caller)
|
||||
@param q [out] bignum where generated 'q' is stored (must be initialized by caller)
|
||||
@param g [out] bignum where generated 'g' is stored (must be initialized by caller)
|
||||
@return CRYPT_OK if successful, upon error this function will free all allocated memory
|
||||
*/
|
||||
static int _dsa_make_params(prng_state *prng, int wprng, int group_size, int modulus_size, void *p, void *q, void *g)
|
||||
{
|
||||
unsigned long L, N, n, outbytes, seedbytes, counter, j, i;
|
||||
int err, res, mr_tests_q, mr_tests_p, found_p, found_q, hash;
|
||||
unsigned char *wbuf, *sbuf, digest[MAXBLOCKSIZE];
|
||||
void *t2L1, *t2N1, *t2q, *t2seedlen, *U, *W, *X, *c, *h, *e, *seedinc;
|
||||
|
||||
/* check size */
|
||||
if (group_size >= LTC_MDSA_MAX_GROUP || group_size < 1 || group_size >= modulus_size) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* FIPS-186-4 A.1.1.2 Generation of the Probable Primes p and q Using an Approved Hash Function
|
||||
*
|
||||
* L = The desired length of the prime p (in bits e.g. L = 1024)
|
||||
* N = The desired length of the prime q (in bits e.g. N = 160)
|
||||
* seedlen = The desired bit length of the domain parameter seed; seedlen shallbe equal to or greater than N
|
||||
* outlen = The bit length of Hash function
|
||||
*
|
||||
* 1. Check that the (L, N)
|
||||
* 2. If (seedlen <N), then return INVALID.
|
||||
* 3. n = ceil(L / outlen) - 1
|
||||
* 4. b = L- 1 - (n * outlen)
|
||||
* 5. domain_parameter_seed = an arbitrary sequence of seedlen bits
|
||||
* 6. U = Hash (domain_parameter_seed) mod 2^(N-1)
|
||||
* 7. q = 2^(N-1) + U + 1 - (U mod 2)
|
||||
* 8. Test whether or not q is prime as specified in Appendix C.3
|
||||
* 9. If qis not a prime, then go to step 5.
|
||||
* 10. offset = 1
|
||||
* 11. For counter = 0 to (4L- 1) do {
|
||||
* For j=0 to n do {
|
||||
* Vj = Hash ((domain_parameter_seed+ offset + j) mod 2^seedlen
|
||||
* }
|
||||
* W = V0 + (V1 *2^outlen) + ... + (Vn-1 * 2^((n-1) * outlen)) + ((Vn mod 2^b) * 2^(n * outlen))
|
||||
* X = W + 2^(L-1) Comment: 0 <= W < 2^(L-1); hence 2^(L-1) <= X < 2^L
|
||||
* c = X mod 2*q
|
||||
* p = X - (c - 1) Comment: p ~ 1 (mod 2*q)
|
||||
* If (p >= 2^(L-1)) {
|
||||
* Test whether or not p is prime as specified in Appendix C.3.
|
||||
* If p is determined to be prime, then return VALID and the values of p, qand (optionally) the values of domain_parameter_seed and counter
|
||||
* }
|
||||
* offset = offset + n + 1 Comment: Increment offset
|
||||
* }
|
||||
*/
|
||||
|
||||
seedbytes = group_size;
|
||||
L = (unsigned long)modulus_size * 8;
|
||||
N = (unsigned long)group_size * 8;
|
||||
|
||||
/* XXX-TODO no Lucas test */
|
||||
#ifdef LTC_MPI_HAS_LUCAS_TEST
|
||||
/* M-R tests (when followed by one Lucas test) according FIPS-186-4 - Appendix C.3 - table C.1 */
|
||||
mr_tests_p = (L <= 2048) ? 3 : 2;
|
||||
if (N <= 160) { mr_tests_q = 19; }
|
||||
else if (N <= 224) { mr_tests_q = 24; }
|
||||
else { mr_tests_q = 27; }
|
||||
#else
|
||||
/* M-R tests (without Lucas test) according FIPS-186-4 - Appendix C.3 - table C.1 */
|
||||
if (L <= 1024) { mr_tests_p = 40; }
|
||||
else if (L <= 2048) { mr_tests_p = 56; }
|
||||
else { mr_tests_p = 64; }
|
||||
|
||||
if (N <= 160) { mr_tests_q = 40; }
|
||||
else if (N <= 224) { mr_tests_q = 56; }
|
||||
else { mr_tests_q = 64; }
|
||||
#endif
|
||||
|
||||
if (N <= 256) {
|
||||
hash = register_hash(&sha256_desc);
|
||||
}
|
||||
else if (N <= 384) {
|
||||
hash = register_hash(&sha384_desc);
|
||||
}
|
||||
else if (N <= 512) {
|
||||
hash = register_hash(&sha512_desc);
|
||||
}
|
||||
else {
|
||||
return CRYPT_INVALID_ARG; /* group_size too big */
|
||||
}
|
||||
|
||||
if ((err = hash_is_valid(hash)) != CRYPT_OK) { return err; }
|
||||
outbytes = hash_descriptor[hash].hashsize;
|
||||
|
||||
n = ((L + outbytes*8 - 1) / (outbytes*8)) - 1;
|
||||
|
||||
if ((wbuf = XMALLOC((n+1)*outbytes)) == NULL) { err = CRYPT_MEM; goto cleanup3; }
|
||||
if ((sbuf = XMALLOC(seedbytes)) == NULL) { err = CRYPT_MEM; goto cleanup2; }
|
||||
|
||||
err = mp_init_multi(&t2L1, &t2N1, &t2q, &t2seedlen, &U, &W, &X, &c, &h, &e, &seedinc, NULL);
|
||||
if (err != CRYPT_OK) { goto cleanup1; }
|
||||
|
||||
if ((err = mp_2expt(t2L1, L-1)) != CRYPT_OK) { goto cleanup; }
|
||||
/* t2L1 = 2^(L-1) */
|
||||
if ((err = mp_2expt(t2N1, N-1)) != CRYPT_OK) { goto cleanup; }
|
||||
/* t2N1 = 2^(N-1) */
|
||||
if ((err = mp_2expt(t2seedlen, seedbytes*8)) != CRYPT_OK) { goto cleanup; }
|
||||
/* t2seedlen = 2^seedlen */
|
||||
|
||||
for(found_p=0; !found_p;) {
|
||||
/* q */
|
||||
for(found_q=0; !found_q;) {
|
||||
if (prng_descriptor[wprng].read(sbuf, seedbytes, prng) != seedbytes) { err = CRYPT_ERROR_READPRNG; goto cleanup; }
|
||||
i = outbytes;
|
||||
if ((err = hash_memory(hash, sbuf, seedbytes, digest, &i)) != CRYPT_OK) { goto cleanup; }
|
||||
if ((err = mp_read_unsigned_bin(U, digest, outbytes)) != CRYPT_OK) { goto cleanup; }
|
||||
if ((err = mp_mod(U, t2N1, U)) != CRYPT_OK) { goto cleanup; }
|
||||
if ((err = mp_add(t2N1, U, q)) != CRYPT_OK) { goto cleanup; }
|
||||
if (!mp_isodd(q)) mp_add_d(q, 1, q);
|
||||
if ((err = mp_prime_is_prime(q, mr_tests_q, &res)) != CRYPT_OK) { goto cleanup; }
|
||||
if (res == LTC_MP_YES) found_q = 1;
|
||||
}
|
||||
|
||||
/* p */
|
||||
if ((err = mp_read_unsigned_bin(seedinc, sbuf, seedbytes)) != CRYPT_OK) { goto cleanup; }
|
||||
if ((err = mp_add(q, q, t2q)) != CRYPT_OK) { goto cleanup; }
|
||||
for(counter=0; counter < 4*L && !found_p; counter++) {
|
||||
for(j=0; j<=n; j++) {
|
||||
if ((err = mp_add_d(seedinc, 1, seedinc)) != CRYPT_OK) { goto cleanup; }
|
||||
if ((err = mp_mod(seedinc, t2seedlen, seedinc)) != CRYPT_OK) { goto cleanup; }
|
||||
/* seedinc = (seedinc+1) % 2^seed_bitlen */
|
||||
if ((i = mp_unsigned_bin_size(seedinc)) > seedbytes) { err = CRYPT_INVALID_ARG; goto cleanup; }
|
||||
zeromem(sbuf, seedbytes);
|
||||
if ((err = mp_to_unsigned_bin(seedinc, sbuf + seedbytes-i)) != CRYPT_OK) { goto cleanup; }
|
||||
i = outbytes;
|
||||
err = hash_memory(hash, sbuf, seedbytes, wbuf+(n-j)*outbytes, &i);
|
||||
if (err != CRYPT_OK) { goto cleanup; }
|
||||
}
|
||||
if ((err = mp_read_unsigned_bin(W, wbuf, (n+1)*outbytes)) != CRYPT_OK) { goto cleanup; }
|
||||
if ((err = mp_mod(W, t2L1, W)) != CRYPT_OK) { goto cleanup; }
|
||||
if ((err = mp_add(W, t2L1, X)) != CRYPT_OK) { goto cleanup; }
|
||||
if ((err = mp_mod(X, t2q, c)) != CRYPT_OK) { goto cleanup; }
|
||||
if ((err = mp_sub_d(c, 1, p)) != CRYPT_OK) { goto cleanup; }
|
||||
if ((err = mp_sub(X, p, p)) != CRYPT_OK) { goto cleanup; }
|
||||
if (mp_cmp(p, t2L1) != LTC_MP_LT) {
|
||||
/* p >= 2^(L-1) */
|
||||
if ((err = mp_prime_is_prime(p, mr_tests_p, &res)) != CRYPT_OK) { goto cleanup; }
|
||||
if (res == LTC_MP_YES) {
|
||||
found_p = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* FIPS-186-4 A.2.1 Unverifiable Generation of the Generator g
|
||||
* 1. e = (p - 1)/q
|
||||
* 2. h = any integer satisfying: 1 < h < (p - 1)
|
||||
* h could be obtained from a random number generator or from a counter that changes after each use
|
||||
* 3. g = h^e mod p
|
||||
* 4. if (g == 1), then go to step 2.
|
||||
*
|
||||
*/
|
||||
|
||||
if ((err = mp_sub_d(p, 1, e)) != CRYPT_OK) { goto cleanup; }
|
||||
if ((err = mp_div(e, q, e, c)) != CRYPT_OK) { goto cleanup; }
|
||||
/* e = (p - 1)/q */
|
||||
i = mp_count_bits(p);
|
||||
do {
|
||||
do {
|
||||
if ((err = rand_bn_bits(h, i, prng, wprng)) != CRYPT_OK) { goto cleanup; }
|
||||
} while (mp_cmp(h, p) != LTC_MP_LT || mp_cmp_d(h, 2) != LTC_MP_GT);
|
||||
if ((err = mp_sub_d(h, 1, h)) != CRYPT_OK) { goto cleanup; }
|
||||
/* h is randon and 1 < h < (p-1) */
|
||||
if ((err = mp_exptmod(h, e, p, g)) != CRYPT_OK) { goto cleanup; }
|
||||
} while (mp_cmp_d(g, 1) == LTC_MP_EQ);
|
||||
|
||||
err = CRYPT_OK;
|
||||
cleanup:
|
||||
mp_clear_multi(t2L1, t2N1, t2q, t2seedlen, U, W, X, c, h, e, seedinc, NULL);
|
||||
cleanup1:
|
||||
XFREE(sbuf);
|
||||
cleanup2:
|
||||
XFREE(wbuf);
|
||||
cleanup3:
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Generate DSA parameters p, q & g
|
||||
@param prng An active PRNG state
|
||||
@param wprng The index of the PRNG desired
|
||||
@param group_size Size of the multiplicative group (octets)
|
||||
@param modulus_size Size of the modulus (octets)
|
||||
@param key [out] Where to store the created key
|
||||
@return CRYPT_OK if successful.
|
||||
*/
|
||||
int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(ltc_mp.name != NULL);
|
||||
|
||||
/* init mp_ints */
|
||||
if ((err = mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
/* generate params */
|
||||
err = _dsa_make_params(prng, wprng, group_size, modulus_size, key->p, key->q, key->g);
|
||||
if (err != CRYPT_OK) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
key->qord = group_size;
|
||||
|
||||
return CRYPT_OK;
|
||||
|
||||
cleanup:
|
||||
dsa_free(key);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
152
thirdparty/libtomcrypt/pk/dsa/dsa_import.c
vendored
Normal file
152
thirdparty/libtomcrypt/pk/dsa/dsa_import.c
vendored
Normal file
@ -0,0 +1,152 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file dsa_import.c
|
||||
DSA implementation, import a DSA key, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/**
|
||||
Import a DSA key
|
||||
@param in The binary packet to import from
|
||||
@param inlen The length of the binary packet
|
||||
@param key [out] Where to store the imported key
|
||||
@return CRYPT_OK if successful, upon error this function will free all allocated memory
|
||||
*/
|
||||
int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key)
|
||||
{
|
||||
int err, stat;
|
||||
unsigned long zero = 0;
|
||||
unsigned char* tmpbuf = NULL;
|
||||
unsigned char flags[1];
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(ltc_mp.name != NULL);
|
||||
|
||||
/* init key */
|
||||
if (mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL) != CRYPT_OK) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* try to match the old libtomcrypt format */
|
||||
err = der_decode_sequence_multi(in, inlen, LTC_ASN1_BIT_STRING, 1UL, flags,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
|
||||
if (err == CRYPT_OK || err == CRYPT_INPUT_TOO_LONG) {
|
||||
/* private key */
|
||||
if (flags[0] == 1) {
|
||||
if ((err = der_decode_sequence_multi(in, inlen,
|
||||
LTC_ASN1_BIT_STRING, 1UL, flags,
|
||||
LTC_ASN1_INTEGER, 1UL, key->g,
|
||||
LTC_ASN1_INTEGER, 1UL, key->p,
|
||||
LTC_ASN1_INTEGER, 1UL, key->q,
|
||||
LTC_ASN1_INTEGER, 1UL, key->y,
|
||||
LTC_ASN1_INTEGER, 1UL, key->x,
|
||||
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
key->type = PK_PRIVATE;
|
||||
goto LBL_OK;
|
||||
}
|
||||
/* public key */
|
||||
else if (flags[0] == 0) {
|
||||
if ((err = der_decode_sequence_multi(in, inlen,
|
||||
LTC_ASN1_BIT_STRING, 1UL, flags,
|
||||
LTC_ASN1_INTEGER, 1UL, key->g,
|
||||
LTC_ASN1_INTEGER, 1UL, key->p,
|
||||
LTC_ASN1_INTEGER, 1UL, key->q,
|
||||
LTC_ASN1_INTEGER, 1UL, key->y,
|
||||
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
key->type = PK_PUBLIC;
|
||||
goto LBL_OK;
|
||||
}
|
||||
else {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
/* get key type */
|
||||
if ((err = der_decode_sequence_multi(in, inlen,
|
||||
LTC_ASN1_SHORT_INTEGER, 1UL, &zero,
|
||||
LTC_ASN1_INTEGER, 1UL, key->p,
|
||||
LTC_ASN1_INTEGER, 1UL, key->q,
|
||||
LTC_ASN1_INTEGER, 1UL, key->g,
|
||||
LTC_ASN1_INTEGER, 1UL, key->y,
|
||||
LTC_ASN1_INTEGER, 1UL, key->x,
|
||||
LTC_ASN1_EOL, 0UL, NULL)) == CRYPT_OK) {
|
||||
|
||||
key->type = PK_PRIVATE;
|
||||
} else { /* public */
|
||||
ltc_asn1_list params[3];
|
||||
unsigned long tmpbuf_len = inlen;
|
||||
|
||||
LTC_SET_ASN1(params, 0, LTC_ASN1_INTEGER, key->p, 1UL);
|
||||
LTC_SET_ASN1(params, 1, LTC_ASN1_INTEGER, key->q, 1UL);
|
||||
LTC_SET_ASN1(params, 2, LTC_ASN1_INTEGER, key->g, 1UL);
|
||||
|
||||
tmpbuf = XCALLOC(1, tmpbuf_len);
|
||||
if (tmpbuf == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
err = der_decode_subject_public_key_info(in, inlen, PKA_DSA,
|
||||
tmpbuf, &tmpbuf_len,
|
||||
LTC_ASN1_SEQUENCE, params, 3);
|
||||
if (err != CRYPT_OK) {
|
||||
XFREE(tmpbuf);
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
if ((err=der_decode_integer(tmpbuf, tmpbuf_len, key->y)) != CRYPT_OK) {
|
||||
XFREE(tmpbuf);
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
XFREE(tmpbuf);
|
||||
key->type = PK_PUBLIC;
|
||||
}
|
||||
|
||||
LBL_OK:
|
||||
key->qord = mp_unsigned_bin_size(key->q);
|
||||
|
||||
/* quick p, q, g validation, without primality testing */
|
||||
if ((err = dsa_int_validate_pqg(key, &stat)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if (stat == 0) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
/* validate x, y */
|
||||
if ((err = dsa_int_validate_xy(key, &stat)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if (stat == 0) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
LBL_ERR:
|
||||
dsa_free(key);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
41
thirdparty/libtomcrypt/pk/dsa/dsa_make_key.c
vendored
Normal file
41
thirdparty/libtomcrypt/pk/dsa/dsa_make_key.c
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file dsa_make_key.c
|
||||
DSA implementation, generate a DSA key
|
||||
*/
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/**
|
||||
Old-style creation of a DSA key
|
||||
@param prng An active PRNG state
|
||||
@param wprng The index of the PRNG desired
|
||||
@param group_size Size of the multiplicative group (octets)
|
||||
@param modulus_size Size of the modulus (octets)
|
||||
@param key [out] Where to store the created key
|
||||
@return CRYPT_OK if successful.
|
||||
*/
|
||||
int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key)
|
||||
{
|
||||
int err;
|
||||
|
||||
if ((err = dsa_generate_pqg(prng, wprng, group_size, modulus_size, key)) != CRYPT_OK) { return err; }
|
||||
if ((err = dsa_generate_key(prng, wprng, key)) != CRYPT_OK) { return err; }
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
112
thirdparty/libtomcrypt/pk/dsa/dsa_set.c
vendored
Normal file
112
thirdparty/libtomcrypt/pk/dsa/dsa_set.c
vendored
Normal file
@ -0,0 +1,112 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/**
|
||||
Import DSA's p, q & g from raw numbers
|
||||
@param p DSA's p in binary representation
|
||||
@param plen The length of p
|
||||
@param q DSA's q in binary representation
|
||||
@param qlen The length of q
|
||||
@param g DSA's g in binary representation
|
||||
@param glen The length of g
|
||||
@param key [out] the destination for the imported key
|
||||
@return CRYPT_OK if successful.
|
||||
*/
|
||||
int dsa_set_pqg(const unsigned char *p, unsigned long plen,
|
||||
const unsigned char *q, unsigned long qlen,
|
||||
const unsigned char *g, unsigned long glen,
|
||||
dsa_key *key)
|
||||
{
|
||||
int err, stat;
|
||||
|
||||
LTC_ARGCHK(p != NULL);
|
||||
LTC_ARGCHK(q != NULL);
|
||||
LTC_ARGCHK(g != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(ltc_mp.name != NULL);
|
||||
|
||||
/* init key */
|
||||
err = mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL);
|
||||
if (err != CRYPT_OK) return err;
|
||||
|
||||
if ((err = mp_read_unsigned_bin(key->p, (unsigned char *)p , plen)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
if ((err = mp_read_unsigned_bin(key->g, (unsigned char *)g , glen)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
if ((err = mp_read_unsigned_bin(key->q, (unsigned char *)q , qlen)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
|
||||
key->qord = mp_unsigned_bin_size(key->q);
|
||||
|
||||
/* do only a quick validation, without primality testing */
|
||||
if ((err = dsa_int_validate_pqg(key, &stat)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
if (stat == 0) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
|
||||
LBL_ERR:
|
||||
dsa_free(key);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Import DSA public or private key-part from raw numbers
|
||||
|
||||
NB: The p, q & g parts must be set beforehand
|
||||
|
||||
@param in The key-part to import, either public or private.
|
||||
@param inlen The key-part's length
|
||||
@param type Which type of key (PK_PRIVATE or PK_PUBLIC)
|
||||
@param key [out] the destination for the imported key
|
||||
@return CRYPT_OK if successful.
|
||||
*/
|
||||
int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key)
|
||||
{
|
||||
int err, stat = 0;
|
||||
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(key->x != NULL);
|
||||
LTC_ARGCHK(key->y != NULL);
|
||||
LTC_ARGCHK(key->p != NULL);
|
||||
LTC_ARGCHK(key->g != NULL);
|
||||
LTC_ARGCHK(key->q != NULL);
|
||||
LTC_ARGCHK(ltc_mp.name != NULL);
|
||||
|
||||
if (type == PK_PRIVATE) {
|
||||
key->type = PK_PRIVATE;
|
||||
if ((err = mp_read_unsigned_bin(key->x, (unsigned char *)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
}
|
||||
else {
|
||||
key->type = PK_PUBLIC;
|
||||
if ((err = mp_read_unsigned_bin(key->y, (unsigned char *)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
}
|
||||
|
||||
if ((err = dsa_int_validate_xy(key, &stat)) != CRYPT_OK) { goto LBL_ERR; }
|
||||
if (stat == 0) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
|
||||
LBL_ERR:
|
||||
dsa_free(key);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
67
thirdparty/libtomcrypt/pk/dsa/dsa_set_pqg_dsaparam.c
vendored
Normal file
67
thirdparty/libtomcrypt/pk/dsa/dsa_set_pqg_dsaparam.c
vendored
Normal file
@ -0,0 +1,67 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/**
|
||||
Import DSA's p, q & g from dsaparam
|
||||
|
||||
dsaparam data: openssl dsaparam -outform DER -out dsaparam.der 2048
|
||||
|
||||
@param dsaparam The DSA param DER encoded data
|
||||
@param dsaparamlen The length of dhparam data
|
||||
@param key [out] the destination for the imported key
|
||||
@return CRYPT_OK if successful.
|
||||
*/
|
||||
int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen,
|
||||
dsa_key *key)
|
||||
{
|
||||
int err, stat;
|
||||
|
||||
LTC_ARGCHK(dsaparam != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(ltc_mp.name != NULL);
|
||||
|
||||
/* init key */
|
||||
err = mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL);
|
||||
if (err != CRYPT_OK) return err;
|
||||
|
||||
if ((err = der_decode_sequence_multi(dsaparam, dsaparamlen,
|
||||
LTC_ASN1_INTEGER, 1UL, key->p,
|
||||
LTC_ASN1_INTEGER, 1UL, key->q,
|
||||
LTC_ASN1_INTEGER, 1UL, key->g,
|
||||
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
key->qord = mp_unsigned_bin_size(key->q);
|
||||
|
||||
/* quick p, q, g validation, without primality testing */
|
||||
if ((err = dsa_int_validate_pqg(key, &stat)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if (stat == 0) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
|
||||
LBL_ERR:
|
||||
dsa_free(key);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
70
thirdparty/libtomcrypt/pk/dsa/dsa_shared_secret.c
vendored
Normal file
70
thirdparty/libtomcrypt/pk/dsa/dsa_shared_secret.c
vendored
Normal file
@ -0,0 +1,70 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file dsa_shared_secret.c
|
||||
DSA Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/**
|
||||
Create a DSA shared secret between two keys
|
||||
@param private_key The private DSA key (the exponent)
|
||||
@param base The base of the exponentiation (allows this to be used for both encrypt and decrypt)
|
||||
@param public_key The public key
|
||||
@param out [out] Destination of the shared secret
|
||||
@param outlen [in/out] The max size and resulting size of the shared secret
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dsa_shared_secret(void *private_key, void *base,
|
||||
dsa_key *public_key,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x;
|
||||
void *res;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(private_key != NULL);
|
||||
LTC_ARGCHK(public_key != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* make new point */
|
||||
if ((err = mp_init(&res)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if ((err = mp_exptmod(base, private_key, public_key->p, res)) != CRYPT_OK) {
|
||||
mp_clear(res);
|
||||
return err;
|
||||
}
|
||||
|
||||
x = (unsigned long)mp_unsigned_bin_size(res);
|
||||
if (*outlen < x) {
|
||||
*outlen = x;
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
goto done;
|
||||
}
|
||||
zeromem(out, x);
|
||||
if ((err = mp_to_unsigned_bin(res, out + (x - mp_unsigned_bin_size(res)))) != CRYPT_OK) { goto done; }
|
||||
|
||||
err = CRYPT_OK;
|
||||
*outlen = x;
|
||||
done:
|
||||
mp_clear(res);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
152
thirdparty/libtomcrypt/pk/dsa/dsa_sign_hash.c
vendored
Normal file
152
thirdparty/libtomcrypt/pk/dsa/dsa_sign_hash.c
vendored
Normal file
@ -0,0 +1,152 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file dsa_sign_hash.c
|
||||
DSA implementation, sign a hash, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/**
|
||||
Sign a hash with DSA
|
||||
@param in The hash to sign
|
||||
@param inlen The length of the hash to sign
|
||||
@param r The "r" integer of the signature (caller must initialize with mp_init() first)
|
||||
@param s The "s" integer of the signature (caller must initialize with mp_init() first)
|
||||
@param prng An active PRNG state
|
||||
@param wprng The index of the PRNG desired
|
||||
@param key A private DSA key
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen,
|
||||
void *r, void *s,
|
||||
prng_state *prng, int wprng, dsa_key *key)
|
||||
{
|
||||
void *k, *kinv, *tmp;
|
||||
unsigned char *buf;
|
||||
int err, qbits;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(r != NULL);
|
||||
LTC_ARGCHK(s != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if (key->type != PK_PRIVATE) {
|
||||
return CRYPT_PK_NOT_PRIVATE;
|
||||
}
|
||||
|
||||
/* check group order size */
|
||||
if (key->qord >= LTC_MDSA_MAX_GROUP) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
buf = XMALLOC(LTC_MDSA_MAX_GROUP);
|
||||
if (buf == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* Init our temps */
|
||||
if ((err = mp_init_multi(&k, &kinv, &tmp, NULL)) != CRYPT_OK) { goto ERRBUF; }
|
||||
|
||||
qbits = mp_count_bits(key->q);
|
||||
retry:
|
||||
|
||||
do {
|
||||
/* gen random k */
|
||||
if ((err = rand_bn_bits(k, qbits, prng, wprng)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* k should be from range: 1 <= k <= q-1 (see FIPS 186-4 B.2.2) */
|
||||
if (mp_cmp_d(k, 0) != LTC_MP_GT || mp_cmp(k, key->q) != LTC_MP_LT) { goto retry; }
|
||||
|
||||
/* test gcd */
|
||||
if ((err = mp_gcd(k, key->q, tmp)) != CRYPT_OK) { goto error; }
|
||||
} while (mp_cmp_d(tmp, 1) != LTC_MP_EQ);
|
||||
|
||||
/* now find 1/k mod q */
|
||||
if ((err = mp_invmod(k, key->q, kinv)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* now find r = g^k mod p mod q */
|
||||
if ((err = mp_exptmod(key->g, k, key->p, r)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_mod(r, key->q, r)) != CRYPT_OK) { goto error; }
|
||||
|
||||
if (mp_iszero(r) == LTC_MP_YES) { goto retry; }
|
||||
|
||||
/* FIPS 186-4 4.6: use leftmost min(bitlen(q), bitlen(hash)) bits of 'hash'*/
|
||||
inlen = MIN(inlen, (unsigned long)(key->qord));
|
||||
|
||||
/* now find s = (in + xr)/k mod q */
|
||||
if ((err = mp_read_unsigned_bin(tmp, (unsigned char *)in, inlen)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_mul(key->x, r, s)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_add(s, tmp, s)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_mulmod(s, kinv, key->q, s)) != CRYPT_OK) { goto error; }
|
||||
|
||||
if (mp_iszero(s) == LTC_MP_YES) { goto retry; }
|
||||
|
||||
err = CRYPT_OK;
|
||||
error:
|
||||
mp_clear_multi(k, kinv, tmp, NULL);
|
||||
ERRBUF:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(buf, LTC_MDSA_MAX_GROUP);
|
||||
#endif
|
||||
XFREE(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Sign a hash with DSA
|
||||
@param in The hash to sign
|
||||
@param inlen The length of the hash to sign
|
||||
@param out [out] Where to store the signature
|
||||
@param outlen [in/out] The max size and resulting size of the signature
|
||||
@param prng An active PRNG state
|
||||
@param wprng The index of the PRNG desired
|
||||
@param key A private DSA key
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, dsa_key *key)
|
||||
{
|
||||
void *r, *s;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
if (mp_init_multi(&r, &s, NULL) != CRYPT_OK) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
if ((err = dsa_sign_hash_raw(in, inlen, r, s, prng, wprng, key)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
err = der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_INTEGER, 1UL, r,
|
||||
LTC_ASN1_INTEGER, 1UL, s,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
|
||||
error:
|
||||
mp_clear_multi(r, s, NULL);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
137
thirdparty/libtomcrypt/pk/dsa/dsa_verify_hash.c
vendored
Normal file
137
thirdparty/libtomcrypt/pk/dsa/dsa_verify_hash.c
vendored
Normal file
@ -0,0 +1,137 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file dsa_verify_hash.c
|
||||
DSA implementation, verify a signature, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/**
|
||||
Verify a DSA signature
|
||||
@param r DSA "r" parameter
|
||||
@param s DSA "s" parameter
|
||||
@param hash The hash that was signed
|
||||
@param hashlen The length of the hash that was signed
|
||||
@param stat [out] The result of the signature verification, 1==valid, 0==invalid
|
||||
@param key The corresponding public DSA key
|
||||
@return CRYPT_OK if successful (even if the signature is invalid)
|
||||
*/
|
||||
int dsa_verify_hash_raw( void *r, void *s,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int *stat, dsa_key *key)
|
||||
{
|
||||
void *w, *v, *u1, *u2;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(r != NULL);
|
||||
LTC_ARGCHK(s != NULL);
|
||||
LTC_ARGCHK(stat != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
/* default to invalid signature */
|
||||
*stat = 0;
|
||||
|
||||
/* init our variables */
|
||||
if ((err = mp_init_multi(&w, &v, &u1, &u2, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* neither r or s can be null or >q*/
|
||||
if (mp_cmp_d(r, 0) != LTC_MP_GT || mp_cmp_d(s, 0) != LTC_MP_GT || mp_cmp(r, key->q) != LTC_MP_LT || mp_cmp(s, key->q) != LTC_MP_LT) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* FIPS 186-4 4.7: use leftmost min(bitlen(q), bitlen(hash)) bits of 'hash' */
|
||||
hashlen = MIN(hashlen, (unsigned long)(key->qord));
|
||||
|
||||
/* w = 1/s mod q */
|
||||
if ((err = mp_invmod(s, key->q, w)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* u1 = m * w mod q */
|
||||
if ((err = mp_read_unsigned_bin(u1, (unsigned char *)hash, hashlen)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_mulmod(u1, w, key->q, u1)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* u2 = r*w mod q */
|
||||
if ((err = mp_mulmod(r, w, key->q, u2)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* v = g^u1 * y^u2 mod p mod q */
|
||||
if ((err = mp_exptmod(key->g, u1, key->p, u1)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_exptmod(key->y, u2, key->p, u2)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_mulmod(u1, u2, key->p, v)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_mod(v, key->q, v)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* if r = v then we're set */
|
||||
if (mp_cmp(r, v) == LTC_MP_EQ) {
|
||||
*stat = 1;
|
||||
}
|
||||
|
||||
err = CRYPT_OK;
|
||||
error:
|
||||
mp_clear_multi(w, v, u1, u2, NULL);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Verify a DSA signature
|
||||
@param sig The signature
|
||||
@param siglen The length of the signature (octets)
|
||||
@param hash The hash that was signed
|
||||
@param hashlen The length of the hash that was signed
|
||||
@param stat [out] The result of the signature verification, 1==valid, 0==invalid
|
||||
@param key The corresponding public DSA key
|
||||
@return CRYPT_OK if successful (even if the signature is invalid)
|
||||
*/
|
||||
int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int *stat, dsa_key *key)
|
||||
{
|
||||
int err;
|
||||
void *r, *s;
|
||||
ltc_asn1_list sig_seq[2];
|
||||
unsigned long reallen = 0;
|
||||
|
||||
LTC_ARGCHK(stat != NULL);
|
||||
*stat = 0; /* must be set before the first return */
|
||||
|
||||
if ((err = mp_init_multi(&r, &s, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
LTC_SET_ASN1(sig_seq, 0, LTC_ASN1_INTEGER, r, 1UL);
|
||||
LTC_SET_ASN1(sig_seq, 1, LTC_ASN1_INTEGER, s, 1UL);
|
||||
|
||||
err = der_decode_sequence(sig, siglen, sig_seq, 2);
|
||||
if (err != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
err = der_length_sequence(sig_seq, 2, &reallen);
|
||||
if (err != CRYPT_OK || reallen != siglen) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* do the op */
|
||||
err = dsa_verify_hash_raw(r, s, hash, hashlen, stat, key);
|
||||
|
||||
LBL_ERR:
|
||||
mp_clear_multi(r, s, NULL);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
199
thirdparty/libtomcrypt/pk/dsa/dsa_verify_key.c
vendored
Normal file
199
thirdparty/libtomcrypt/pk/dsa/dsa_verify_key.c
vendored
Normal file
@ -0,0 +1,199 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file dsa_verify_key.c
|
||||
DSA implementation, verify a key, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/**
|
||||
Validate a DSA key
|
||||
|
||||
Yeah, this function should've been called dsa_validate_key()
|
||||
in the first place and for compat-reasons we keep it
|
||||
as it was (for now).
|
||||
|
||||
@param key The key to validate
|
||||
@param stat [out] Result of test, 1==valid, 0==invalid
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dsa_verify_key(dsa_key *key, int *stat)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = dsa_int_validate_primes(key, stat);
|
||||
if (err != CRYPT_OK || *stat == 0) return err;
|
||||
|
||||
err = dsa_int_validate_pqg(key, stat);
|
||||
if (err != CRYPT_OK || *stat == 0) return err;
|
||||
|
||||
return dsa_int_validate_xy(key, stat);
|
||||
}
|
||||
|
||||
/**
|
||||
Non-complex part (no primality testing) of the validation
|
||||
of DSA params (p, q, g)
|
||||
|
||||
@param key The key to validate
|
||||
@param stat [out] Result of test, 1==valid, 0==invalid
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dsa_int_validate_pqg(dsa_key *key, int *stat)
|
||||
{
|
||||
void *tmp1, *tmp2;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(stat != NULL);
|
||||
*stat = 0;
|
||||
|
||||
/* check q-order */
|
||||
if ( key->qord >= LTC_MDSA_MAX_GROUP || key->qord <= 15 ||
|
||||
(unsigned long)key->qord >= mp_unsigned_bin_size(key->p) ||
|
||||
(mp_unsigned_bin_size(key->p) - key->qord) >= LTC_MDSA_DELTA ) {
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* FIPS 186-4 chapter 4.1: 1 < g < p */
|
||||
if (mp_cmp_d(key->g, 1) != LTC_MP_GT || mp_cmp(key->g, key->p) != LTC_MP_LT) {
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
if ((err = mp_init_multi(&tmp1, &tmp2, NULL)) != CRYPT_OK) { return err; }
|
||||
|
||||
/* FIPS 186-4 chapter 4.1: q is a divisor of (p - 1) */
|
||||
if ((err = mp_sub_d(key->p, 1, tmp1)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_div(tmp1, key->q, tmp1, tmp2)) != CRYPT_OK) { goto error; }
|
||||
if (mp_iszero(tmp2) != LTC_MP_YES) {
|
||||
err = CRYPT_OK;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* FIPS 186-4 chapter 4.1: g is a generator of a subgroup of order q in
|
||||
* the multiplicative group of GF(p) - so we make sure that g^q mod p = 1
|
||||
*/
|
||||
if ((err = mp_exptmod(key->g, key->q, key->p, tmp1)) != CRYPT_OK) { goto error; }
|
||||
if (mp_cmp_d(tmp1, 1) != LTC_MP_EQ) {
|
||||
err = CRYPT_OK;
|
||||
goto error;
|
||||
}
|
||||
|
||||
err = CRYPT_OK;
|
||||
*stat = 1;
|
||||
error:
|
||||
mp_clear_multi(tmp2, tmp1, NULL);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Primality testing of DSA params p and q
|
||||
|
||||
@param key The key to validate
|
||||
@param stat [out] Result of test, 1==valid, 0==invalid
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dsa_int_validate_primes(dsa_key *key, int *stat)
|
||||
{
|
||||
int err, res;
|
||||
|
||||
*stat = 0;
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(stat != NULL);
|
||||
|
||||
/* key->q prime? */
|
||||
if ((err = mp_prime_is_prime(key->q, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if (res == LTC_MP_NO) {
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* key->p prime? */
|
||||
if ((err = mp_prime_is_prime(key->p, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if (res == LTC_MP_NO) {
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
*stat = 1;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Validation of a DSA key (x and y values)
|
||||
|
||||
@param key The key to validate
|
||||
@param stat [out] Result of test, 1==valid, 0==invalid
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int dsa_int_validate_xy(dsa_key *key, int *stat)
|
||||
{
|
||||
void *tmp;
|
||||
int err;
|
||||
|
||||
*stat = 0;
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(stat != NULL);
|
||||
|
||||
/* 1 < y < p-1 */
|
||||
if ((err = mp_init(&tmp)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if ((err = mp_sub_d(key->p, 1, tmp)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
if (mp_cmp_d(key->y, 1) != LTC_MP_GT || mp_cmp(key->y, tmp) != LTC_MP_LT) {
|
||||
err = CRYPT_OK;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (key->type == PK_PRIVATE) {
|
||||
/* FIPS 186-4 chapter 4.1: 0 < x < q */
|
||||
if (mp_cmp_d(key->x, 0) != LTC_MP_GT || mp_cmp(key->x, key->q) != LTC_MP_LT) {
|
||||
err = CRYPT_OK;
|
||||
goto error;
|
||||
}
|
||||
/* FIPS 186-4 chapter 4.1: y = g^x mod p */
|
||||
if ((err = mp_exptmod(key->g, key->x, key->p, tmp)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
if (mp_cmp(tmp, key->y) != LTC_MP_EQ) {
|
||||
err = CRYPT_OK;
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* with just a public key we cannot test y = g^x mod p therefore we
|
||||
* only test that y^q mod p = 1, which makes sure y is in g^x mod p
|
||||
*/
|
||||
if ((err = mp_exptmod(key->y, key->q, key->p, tmp)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
if (mp_cmp_d(tmp, 1) != LTC_MP_EQ) {
|
||||
err = CRYPT_OK;
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
err = CRYPT_OK;
|
||||
*stat = 1;
|
||||
error:
|
||||
mp_clear(tmp);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
125
thirdparty/libtomcrypt/pk/ecc/ecc.c
vendored
Normal file
125
thirdparty/libtomcrypt/pk/ecc/ecc.c
vendored
Normal file
@ -0,0 +1,125 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecc.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/* This holds the key settings. ***MUST*** be organized by size from smallest to largest. */
|
||||
const ltc_ecc_set_type ltc_ecc_sets[] = {
|
||||
#ifdef LTC_ECC112
|
||||
{
|
||||
14,
|
||||
"SECP112R1",
|
||||
"DB7C2ABF62E35E668076BEAD208B",
|
||||
"659EF8BA043916EEDE8911702B22",
|
||||
"DB7C2ABF62E35E7628DFAC6561C5",
|
||||
"09487239995A5EE76B55F9C2F098",
|
||||
"A89CE5AF8724C0A23E0E0FF77500"
|
||||
},
|
||||
#endif
|
||||
#ifdef LTC_ECC128
|
||||
{
|
||||
16,
|
||||
"SECP128R1",
|
||||
"FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF",
|
||||
"E87579C11079F43DD824993C2CEE5ED3",
|
||||
"FFFFFFFE0000000075A30D1B9038A115",
|
||||
"161FF7528B899B2D0C28607CA52C5B86",
|
||||
"CF5AC8395BAFEB13C02DA292DDED7A83",
|
||||
},
|
||||
#endif
|
||||
#ifdef LTC_ECC160
|
||||
{
|
||||
20,
|
||||
"SECP160R1",
|
||||
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF",
|
||||
"1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45",
|
||||
"0100000000000000000001F4C8F927AED3CA752257",
|
||||
"4A96B5688EF573284664698968C38BB913CBFC82",
|
||||
"23A628553168947D59DCC912042351377AC5FB32",
|
||||
},
|
||||
#endif
|
||||
#ifdef LTC_ECC192
|
||||
{
|
||||
24,
|
||||
"ECC-192",
|
||||
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF",
|
||||
"64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1",
|
||||
"FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831",
|
||||
"188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012",
|
||||
"7192B95FFC8DA78631011ED6B24CDD573F977A11E794811",
|
||||
},
|
||||
#endif
|
||||
#ifdef LTC_ECC224
|
||||
{
|
||||
28,
|
||||
"ECC-224",
|
||||
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
|
||||
"B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
|
||||
"FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
|
||||
"B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
|
||||
"BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
|
||||
},
|
||||
#endif
|
||||
#ifdef LTC_ECC256
|
||||
{
|
||||
32,
|
||||
"ECC-256",
|
||||
"FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF",
|
||||
"5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B",
|
||||
"FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551",
|
||||
"6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296",
|
||||
"4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5",
|
||||
},
|
||||
#endif
|
||||
#ifdef LTC_ECC384
|
||||
{
|
||||
48,
|
||||
"ECC-384",
|
||||
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF",
|
||||
"B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF",
|
||||
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973",
|
||||
"AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7",
|
||||
"3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F",
|
||||
},
|
||||
#endif
|
||||
#ifdef LTC_ECC521
|
||||
{
|
||||
66,
|
||||
"ECC-521",
|
||||
"1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
||||
"51953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00",
|
||||
"1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409",
|
||||
"C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66",
|
||||
"11839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650",
|
||||
},
|
||||
#endif
|
||||
{
|
||||
0,
|
||||
NULL, NULL, NULL, NULL, NULL, NULL
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
77
thirdparty/libtomcrypt/pk/ecc/ecc_ansi_x963_export.c
vendored
Normal file
77
thirdparty/libtomcrypt/pk/ecc/ecc_ansi_x963_export.c
vendored
Normal file
@ -0,0 +1,77 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecc_ansi_x963_export.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/** ECC X9.63 (Sec. 4.3.6) uncompressed export
|
||||
@param key Key to export
|
||||
@param out [out] destination of export
|
||||
@param outlen [in/out] Length of destination and final output size
|
||||
Return CRYPT_OK on success
|
||||
*/
|
||||
int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned char buf[ECC_BUF_SIZE];
|
||||
unsigned long numlen, xlen, ylen;
|
||||
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
if (ltc_ecc_is_valid_idx(key->idx) == 0) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
numlen = key->dp->size;
|
||||
xlen = mp_unsigned_bin_size(key->pubkey.x);
|
||||
ylen = mp_unsigned_bin_size(key->pubkey.y);
|
||||
|
||||
if (xlen > numlen || ylen > numlen || sizeof(buf) < numlen) {
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
if (*outlen < (1 + 2*numlen)) {
|
||||
*outlen = 1 + 2*numlen;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
|
||||
/* store byte 0x04 */
|
||||
out[0] = 0x04;
|
||||
|
||||
/* pad and store x */
|
||||
zeromem(buf, sizeof(buf));
|
||||
mp_to_unsigned_bin(key->pubkey.x, buf + (numlen - xlen));
|
||||
XMEMCPY(out+1, buf, numlen);
|
||||
|
||||
/* pad and store y */
|
||||
zeromem(buf, sizeof(buf));
|
||||
mp_to_unsigned_bin(key->pubkey.y, buf + (numlen - ylen));
|
||||
XMEMCPY(out+1+numlen, buf, numlen);
|
||||
|
||||
*outlen = 1 + 2*numlen;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
102
thirdparty/libtomcrypt/pk/ecc/ecc_ansi_x963_import.c
vendored
Normal file
102
thirdparty/libtomcrypt/pk/ecc/ecc_ansi_x963_import.c
vendored
Normal file
@ -0,0 +1,102 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecc_ansi_x963_import.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/** Import an ANSI X9.63 format public key
|
||||
@param in The input data to read
|
||||
@param inlen The length of the input data
|
||||
@param key [out] destination to store imported key \
|
||||
*/
|
||||
int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key)
|
||||
{
|
||||
return ecc_ansi_x963_import_ex(in, inlen, key, NULL);
|
||||
}
|
||||
|
||||
int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp)
|
||||
{
|
||||
int x, err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
/* must be odd */
|
||||
if ((inlen & 1) == 0) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* init key */
|
||||
if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, NULL) != CRYPT_OK) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* check for 4, 6 or 7 */
|
||||
if (in[0] != 4 && in[0] != 6 && in[0] != 7) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* read data */
|
||||
if ((err = mp_read_unsigned_bin(key->pubkey.x, (unsigned char *)in+1, (inlen-1)>>1)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((err = mp_read_unsigned_bin(key->pubkey.y, (unsigned char *)in+1+((inlen-1)>>1), (inlen-1)>>1)) != CRYPT_OK) {
|
||||
goto error;
|
||||
}
|
||||
if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto error; }
|
||||
|
||||
if (dp == NULL) {
|
||||
/* determine the idx */
|
||||
for (x = 0; ltc_ecc_sets[x].size != 0; x++) {
|
||||
if ((unsigned)ltc_ecc_sets[x].size >= ((inlen-1)>>1)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ltc_ecc_sets[x].size == 0) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto error;
|
||||
}
|
||||
/* set the idx */
|
||||
key->idx = x;
|
||||
key->dp = <c_ecc_sets[x];
|
||||
} else {
|
||||
if (((inlen-1)>>1) != (unsigned long) dp->size) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto error;
|
||||
}
|
||||
key->idx = -1;
|
||||
key->dp = dp;
|
||||
}
|
||||
key->type = PK_PUBLIC;
|
||||
|
||||
/* we're done */
|
||||
return CRYPT_OK;
|
||||
error:
|
||||
mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
149
thirdparty/libtomcrypt/pk/ecc/ecc_decrypt_key.c
vendored
Normal file
149
thirdparty/libtomcrypt/pk/ecc/ecc_decrypt_key.c
vendored
Normal file
@ -0,0 +1,149 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecc_decrypt_key.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/**
|
||||
Decrypt an ECC encrypted key
|
||||
@param in The ciphertext
|
||||
@param inlen The length of the ciphertext (octets)
|
||||
@param out [out] The plaintext
|
||||
@param outlen [in/out] The max size and resulting size of the plaintext
|
||||
@param key The corresponding private ECC key
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
ecc_key *key)
|
||||
{
|
||||
unsigned char *ecc_shared, *skey, *pub_expt;
|
||||
unsigned long x, y;
|
||||
unsigned long hashOID[32] = { 0 };
|
||||
int hash, err;
|
||||
ecc_key pubkey;
|
||||
ltc_asn1_list decode[3];
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
/* right key type? */
|
||||
if (key->type != PK_PRIVATE) {
|
||||
return CRYPT_PK_NOT_PRIVATE;
|
||||
}
|
||||
|
||||
/* decode to find out hash */
|
||||
LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, sizeof(hashOID)/sizeof(hashOID[0]));
|
||||
err = der_decode_sequence(in, inlen, decode, 1);
|
||||
if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
|
||||
return err;
|
||||
}
|
||||
|
||||
hash = find_hash_oid(hashOID, decode[0].size);
|
||||
if (hash_is_valid(hash) != CRYPT_OK) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* we now have the hash! */
|
||||
|
||||
/* allocate memory */
|
||||
pub_expt = XMALLOC(ECC_BUF_SIZE);
|
||||
ecc_shared = XMALLOC(ECC_BUF_SIZE);
|
||||
skey = XMALLOC(MAXBLOCKSIZE);
|
||||
if (pub_expt == NULL || ecc_shared == NULL || skey == NULL) {
|
||||
if (pub_expt != NULL) {
|
||||
XFREE(pub_expt);
|
||||
}
|
||||
if (ecc_shared != NULL) {
|
||||
XFREE(ecc_shared);
|
||||
}
|
||||
if (skey != NULL) {
|
||||
XFREE(skey);
|
||||
}
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
LTC_SET_ASN1(decode, 1, LTC_ASN1_OCTET_STRING, pub_expt, ECC_BUF_SIZE);
|
||||
LTC_SET_ASN1(decode, 2, LTC_ASN1_OCTET_STRING, skey, MAXBLOCKSIZE);
|
||||
|
||||
/* read the structure in now */
|
||||
if ((err = der_decode_sequence(in, inlen, decode, 3)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* import ECC key from packet */
|
||||
if ((err = ecc_import(decode[1].data, decode[1].size, &pubkey)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* make shared key */
|
||||
x = ECC_BUF_SIZE;
|
||||
if ((err = ecc_shared_secret(key, &pubkey, ecc_shared, &x)) != CRYPT_OK) {
|
||||
ecc_free(&pubkey);
|
||||
goto LBL_ERR;
|
||||
}
|
||||
ecc_free(&pubkey);
|
||||
|
||||
y = MIN(ECC_BUF_SIZE, MAXBLOCKSIZE);
|
||||
if ((err = hash_memory(hash, ecc_shared, x, ecc_shared, &y)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* ensure the hash of the shared secret is at least as big as the encrypt itself */
|
||||
if (decode[2].size > y) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* avoid buffer overflow */
|
||||
if (*outlen < decode[2].size) {
|
||||
*outlen = decode[2].size;
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* Decrypt the key */
|
||||
for (x = 0; x < decode[2].size; x++) {
|
||||
out[x] = skey[x] ^ ecc_shared[x];
|
||||
}
|
||||
*outlen = x;
|
||||
|
||||
err = CRYPT_OK;
|
||||
LBL_ERR:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(pub_expt, ECC_BUF_SIZE);
|
||||
zeromem(ecc_shared, ECC_BUF_SIZE);
|
||||
zeromem(skey, MAXBLOCKSIZE);
|
||||
#endif
|
||||
|
||||
XFREE(pub_expt);
|
||||
XFREE(ecc_shared);
|
||||
XFREE(skey);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
134
thirdparty/libtomcrypt/pk/ecc/ecc_encrypt_key.c
vendored
Normal file
134
thirdparty/libtomcrypt/pk/ecc/ecc_encrypt_key.c
vendored
Normal file
@ -0,0 +1,134 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecc_encrypt_key.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/**
|
||||
Encrypt a symmetric key with ECC
|
||||
@param in The symmetric key you want to encrypt
|
||||
@param inlen The length of the key to encrypt (octets)
|
||||
@param out [out] The destination for the ciphertext
|
||||
@param outlen [in/out] The max size and resulting size of the ciphertext
|
||||
@param prng An active PRNG state
|
||||
@param wprng The index of the PRNG you wish to use
|
||||
@param hash The index of the hash you want to use
|
||||
@param key The ECC key you want to encrypt to
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, int hash,
|
||||
ecc_key *key)
|
||||
{
|
||||
unsigned char *pub_expt, *ecc_shared, *skey;
|
||||
ecc_key pubkey;
|
||||
unsigned long x, y, pubkeysize;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
/* check that wprng/cipher/hash are not invalid */
|
||||
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if ((err = hash_is_valid(hash)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (inlen > hash_descriptor[hash].hashsize) {
|
||||
return CRYPT_INVALID_HASH;
|
||||
}
|
||||
|
||||
/* make a random key and export the public copy */
|
||||
if ((err = ecc_make_key_ex(prng, wprng, &pubkey, key->dp)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
pub_expt = XMALLOC(ECC_BUF_SIZE);
|
||||
ecc_shared = XMALLOC(ECC_BUF_SIZE);
|
||||
skey = XMALLOC(MAXBLOCKSIZE);
|
||||
if (pub_expt == NULL || ecc_shared == NULL || skey == NULL) {
|
||||
if (pub_expt != NULL) {
|
||||
XFREE(pub_expt);
|
||||
}
|
||||
if (ecc_shared != NULL) {
|
||||
XFREE(ecc_shared);
|
||||
}
|
||||
if (skey != NULL) {
|
||||
XFREE(skey);
|
||||
}
|
||||
ecc_free(&pubkey);
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
pubkeysize = ECC_BUF_SIZE;
|
||||
if ((err = ecc_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) {
|
||||
ecc_free(&pubkey);
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* make random key */
|
||||
x = ECC_BUF_SIZE;
|
||||
if ((err = ecc_shared_secret(&pubkey, key, ecc_shared, &x)) != CRYPT_OK) {
|
||||
ecc_free(&pubkey);
|
||||
goto LBL_ERR;
|
||||
}
|
||||
ecc_free(&pubkey);
|
||||
y = MAXBLOCKSIZE;
|
||||
if ((err = hash_memory(hash, ecc_shared, x, skey, &y)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* Encrypt key */
|
||||
for (x = 0; x < inlen; x++) {
|
||||
skey[x] ^= in[x];
|
||||
}
|
||||
|
||||
err = der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_OBJECT_IDENTIFIER, hash_descriptor[hash].OIDlen, hash_descriptor[hash].OID,
|
||||
LTC_ASN1_OCTET_STRING, pubkeysize, pub_expt,
|
||||
LTC_ASN1_OCTET_STRING, inlen, skey,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
|
||||
LBL_ERR:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
/* clean up */
|
||||
zeromem(pub_expt, ECC_BUF_SIZE);
|
||||
zeromem(ecc_shared, ECC_BUF_SIZE);
|
||||
zeromem(skey, MAXBLOCKSIZE);
|
||||
#endif
|
||||
|
||||
XFREE(skey);
|
||||
XFREE(ecc_shared);
|
||||
XFREE(pub_expt);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
80
thirdparty/libtomcrypt/pk/ecc/ecc_export.c
vendored
Normal file
80
thirdparty/libtomcrypt/pk/ecc/ecc_export.c
vendored
Normal file
@ -0,0 +1,80 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecc_export.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/**
|
||||
Export an ECC key as a binary packet
|
||||
@param out [out] Destination for the key
|
||||
@param outlen [in/out] Max size and resulting size of the exported key
|
||||
@param type The type of key you want to export (PK_PRIVATE or PK_PUBLIC)
|
||||
@param key The key to export
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key)
|
||||
{
|
||||
int err;
|
||||
unsigned char flags[1];
|
||||
unsigned long key_size;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
/* type valid? */
|
||||
if (key->type != PK_PRIVATE && type == PK_PRIVATE) {
|
||||
return CRYPT_PK_TYPE_MISMATCH;
|
||||
}
|
||||
|
||||
if (ltc_ecc_is_valid_idx(key->idx) == 0) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* we store the NIST byte size */
|
||||
key_size = key->dp->size;
|
||||
|
||||
if (type == PK_PRIVATE) {
|
||||
flags[0] = 1;
|
||||
err = der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_BIT_STRING, 1UL, flags,
|
||||
LTC_ASN1_SHORT_INTEGER, 1UL, &key_size,
|
||||
LTC_ASN1_INTEGER, 1UL, key->pubkey.x,
|
||||
LTC_ASN1_INTEGER, 1UL, key->pubkey.y,
|
||||
LTC_ASN1_INTEGER, 1UL, key->k,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
} else {
|
||||
flags[0] = 0;
|
||||
err = der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_BIT_STRING, 1UL, flags,
|
||||
LTC_ASN1_SHORT_INTEGER, 1UL, &key_size,
|
||||
LTC_ASN1_INTEGER, 1UL, key->pubkey.x,
|
||||
LTC_ASN1_INTEGER, 1UL, key->pubkey.y,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
38
thirdparty/libtomcrypt/pk/ecc/ecc_free.c
vendored
Normal file
38
thirdparty/libtomcrypt/pk/ecc/ecc_free.c
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecc_free.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/**
|
||||
Free an ECC key from memory
|
||||
@param key The key you wish to free
|
||||
*/
|
||||
void ecc_free(ecc_key *key)
|
||||
{
|
||||
LTC_ARGCHKVD(key != NULL);
|
||||
mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
|
||||
}
|
||||
|
||||
#endif
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
42
thirdparty/libtomcrypt/pk/ecc/ecc_get_size.c
vendored
Normal file
42
thirdparty/libtomcrypt/pk/ecc/ecc_get_size.c
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecc_get_size.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/**
|
||||
Get the size of an ECC key
|
||||
@param key The key to get the size of
|
||||
@return The size (octets) of the key or INT_MAX on error
|
||||
*/
|
||||
int ecc_get_size(ecc_key *key)
|
||||
{
|
||||
LTC_ARGCHK(key != NULL);
|
||||
if (ltc_ecc_is_valid_idx(key->idx))
|
||||
return key->dp->size;
|
||||
else
|
||||
return INT_MAX; /* large value known to cause it to fail when passed to ecc_make_key() */
|
||||
}
|
||||
|
||||
#endif
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
174
thirdparty/libtomcrypt/pk/ecc/ecc_import.c
vendored
Normal file
174
thirdparty/libtomcrypt/pk/ecc/ecc_import.c
vendored
Normal file
@ -0,0 +1,174 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecc_import.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
static int _is_point(ecc_key *key)
|
||||
{
|
||||
void *prime, *b, *t1, *t2;
|
||||
int err;
|
||||
|
||||
if ((err = mp_init_multi(&prime, &b, &t1, &t2, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* load prime and b */
|
||||
if ((err = mp_read_radix(prime, key->dp->prime, 16)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_read_radix(b, key->dp->B, 16)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* compute y^2 */
|
||||
if ((err = mp_sqr(key->pubkey.y, t1)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* compute x^3 */
|
||||
if ((err = mp_sqr(key->pubkey.x, t2)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_mod(t2, prime, t2)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_mul(key->pubkey.x, t2, t2)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* compute y^2 - x^3 */
|
||||
if ((err = mp_sub(t1, t2, t1)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* compute y^2 - x^3 + 3x */
|
||||
if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_mod(t1, prime, t1)) != CRYPT_OK) { goto error; }
|
||||
while (mp_cmp_d(t1, 0) == LTC_MP_LT) {
|
||||
if ((err = mp_add(t1, prime, t1)) != CRYPT_OK) { goto error; }
|
||||
}
|
||||
while (mp_cmp(t1, prime) != LTC_MP_LT) {
|
||||
if ((err = mp_sub(t1, prime, t1)) != CRYPT_OK) { goto error; }
|
||||
}
|
||||
|
||||
/* compare to b */
|
||||
if (mp_cmp(t1, b) != LTC_MP_EQ) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
} else {
|
||||
err = CRYPT_OK;
|
||||
}
|
||||
|
||||
error:
|
||||
mp_clear_multi(prime, b, t1, t2, NULL);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Import an ECC key from a binary packet
|
||||
@param in The packet to import
|
||||
@param inlen The length of the packet
|
||||
@param key [out] The destination of the import
|
||||
@return CRYPT_OK if successful, upon error all allocated memory will be freed
|
||||
*/
|
||||
int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key)
|
||||
{
|
||||
return ecc_import_ex(in, inlen, key, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
Import an ECC key from a binary packet, using user supplied domain params rather than one of the NIST ones
|
||||
@param in The packet to import
|
||||
@param inlen The length of the packet
|
||||
@param key [out] The destination of the import
|
||||
@param dp pointer to user supplied params; must be the same as the params used when exporting
|
||||
@return CRYPT_OK if successful, upon error all allocated memory will be freed
|
||||
*/
|
||||
int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp)
|
||||
{
|
||||
unsigned long key_size;
|
||||
unsigned char flags[1];
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(ltc_mp.name != NULL);
|
||||
|
||||
/* init key */
|
||||
if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, NULL) != CRYPT_OK) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* find out what type of key it is */
|
||||
err = der_decode_sequence_multi(in, inlen, LTC_ASN1_BIT_STRING, 1UL, flags,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
|
||||
if (flags[0] == 1) {
|
||||
/* private key */
|
||||
key->type = PK_PRIVATE;
|
||||
if ((err = der_decode_sequence_multi(in, inlen,
|
||||
LTC_ASN1_BIT_STRING, 1UL, flags,
|
||||
LTC_ASN1_SHORT_INTEGER, 1UL, &key_size,
|
||||
LTC_ASN1_INTEGER, 1UL, key->pubkey.x,
|
||||
LTC_ASN1_INTEGER, 1UL, key->pubkey.y,
|
||||
LTC_ASN1_INTEGER, 1UL, key->k,
|
||||
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
|
||||
goto done;
|
||||
}
|
||||
} else if (flags[0] == 0) {
|
||||
/* public key */
|
||||
key->type = PK_PUBLIC;
|
||||
if ((err = der_decode_sequence_multi(in, inlen,
|
||||
LTC_ASN1_BIT_STRING, 1UL, flags,
|
||||
LTC_ASN1_SHORT_INTEGER, 1UL, &key_size,
|
||||
LTC_ASN1_INTEGER, 1UL, key->pubkey.x,
|
||||
LTC_ASN1_INTEGER, 1UL, key->pubkey.y,
|
||||
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
else {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (dp == NULL) {
|
||||
/* find the idx */
|
||||
for (key->idx = 0; ltc_ecc_sets[key->idx].size && (unsigned long)ltc_ecc_sets[key->idx].size != key_size; ++key->idx);
|
||||
if (ltc_ecc_sets[key->idx].size == 0) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto done;
|
||||
}
|
||||
key->dp = <c_ecc_sets[key->idx];
|
||||
} else {
|
||||
key->idx = -1;
|
||||
key->dp = dp;
|
||||
}
|
||||
/* set z */
|
||||
if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto done; }
|
||||
|
||||
/* is it a point on the curve? */
|
||||
if ((err = _is_point(key)) != CRYPT_OK) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* we're good */
|
||||
return CRYPT_OK;
|
||||
done:
|
||||
mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
128
thirdparty/libtomcrypt/pk/ecc/ecc_make_key.c
vendored
Normal file
128
thirdparty/libtomcrypt/pk/ecc/ecc_make_key.c
vendored
Normal file
@ -0,0 +1,128 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecc_make_key.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/**
|
||||
Make a new ECC key
|
||||
@param prng An active PRNG state
|
||||
@param wprng The index of the PRNG you wish to use
|
||||
@param keysize The keysize for the new key (in octets from 20 to 65 bytes)
|
||||
@param key [out] Destination of the newly created key
|
||||
@return CRYPT_OK if successful, upon error all allocated memory will be freed
|
||||
*/
|
||||
int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key)
|
||||
{
|
||||
int x, err;
|
||||
|
||||
/* find key size */
|
||||
for (x = 0; (keysize > ltc_ecc_sets[x].size) && (ltc_ecc_sets[x].size != 0); x++);
|
||||
keysize = ltc_ecc_sets[x].size;
|
||||
|
||||
if (keysize > ECC_MAXSIZE || ltc_ecc_sets[x].size == 0) {
|
||||
return CRYPT_INVALID_KEYSIZE;
|
||||
}
|
||||
err = ecc_make_key_ex(prng, wprng, key, <c_ecc_sets[x]);
|
||||
key->idx = x;
|
||||
return err;
|
||||
}
|
||||
|
||||
int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp)
|
||||
{
|
||||
int err;
|
||||
ecc_point *base;
|
||||
void *prime, *order;
|
||||
unsigned char *buf;
|
||||
int keysize;
|
||||
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(ltc_mp.name != NULL);
|
||||
LTC_ARGCHK(dp != NULL);
|
||||
|
||||
/* good prng? */
|
||||
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
key->idx = -1;
|
||||
key->dp = dp;
|
||||
keysize = dp->size;
|
||||
|
||||
/* allocate ram */
|
||||
base = NULL;
|
||||
buf = XMALLOC(ECC_MAXSIZE);
|
||||
if (buf == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* make up random string */
|
||||
if (prng_descriptor[wprng].read(buf, (unsigned long)keysize, prng) != (unsigned long)keysize) {
|
||||
err = CRYPT_ERROR_READPRNG;
|
||||
goto ERR_BUF;
|
||||
}
|
||||
|
||||
/* setup the key variables */
|
||||
if ((err = mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, &prime, &order, NULL)) != CRYPT_OK) {
|
||||
goto ERR_BUF;
|
||||
}
|
||||
base = ltc_ecc_new_point();
|
||||
if (base == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto errkey;
|
||||
}
|
||||
|
||||
/* read in the specs for this key */
|
||||
if ((err = mp_read_radix(prime, (char *)key->dp->prime, 16)) != CRYPT_OK) { goto errkey; }
|
||||
if ((err = mp_read_radix(order, (char *)key->dp->order, 16)) != CRYPT_OK) { goto errkey; }
|
||||
if ((err = mp_read_radix(base->x, (char *)key->dp->Gx, 16)) != CRYPT_OK) { goto errkey; }
|
||||
if ((err = mp_read_radix(base->y, (char *)key->dp->Gy, 16)) != CRYPT_OK) { goto errkey; }
|
||||
if ((err = mp_set(base->z, 1)) != CRYPT_OK) { goto errkey; }
|
||||
if ((err = mp_read_unsigned_bin(key->k, (unsigned char *)buf, keysize)) != CRYPT_OK) { goto errkey; }
|
||||
|
||||
/* the key should be smaller than the order of base point */
|
||||
if (mp_cmp(key->k, order) != LTC_MP_LT) {
|
||||
if((err = mp_mod(key->k, order, key->k)) != CRYPT_OK) { goto errkey; }
|
||||
}
|
||||
/* make the public key */
|
||||
if ((err = ltc_mp.ecc_ptmul(key->k, base, &key->pubkey, prime, 1)) != CRYPT_OK) { goto errkey; }
|
||||
key->type = PK_PRIVATE;
|
||||
|
||||
/* free up ram */
|
||||
err = CRYPT_OK;
|
||||
goto cleanup;
|
||||
errkey:
|
||||
mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
|
||||
cleanup:
|
||||
ltc_ecc_del_point(base);
|
||||
mp_clear_multi(prime, order, NULL);
|
||||
ERR_BUF:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(buf, ECC_MAXSIZE);
|
||||
#endif
|
||||
XFREE(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
93
thirdparty/libtomcrypt/pk/ecc/ecc_shared_secret.c
vendored
Normal file
93
thirdparty/libtomcrypt/pk/ecc/ecc_shared_secret.c
vendored
Normal file
@ -0,0 +1,93 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecc_shared_secret.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/**
|
||||
Create an ECC shared secret between two keys
|
||||
@param private_key The private ECC key
|
||||
@param public_key The public key
|
||||
@param out [out] Destination of the shared secret (Conforms to EC-DH from ANSI X9.63)
|
||||
@param outlen [in/out] The max size and resulting size of the shared secret
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x;
|
||||
ecc_point *result;
|
||||
void *prime;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(private_key != NULL);
|
||||
LTC_ARGCHK(public_key != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* type valid? */
|
||||
if (private_key->type != PK_PRIVATE) {
|
||||
return CRYPT_PK_NOT_PRIVATE;
|
||||
}
|
||||
|
||||
if (ltc_ecc_is_valid_idx(private_key->idx) == 0 || ltc_ecc_is_valid_idx(public_key->idx) == 0) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (XSTRCMP(private_key->dp->name, public_key->dp->name) != 0) {
|
||||
return CRYPT_PK_TYPE_MISMATCH;
|
||||
}
|
||||
|
||||
/* make new point */
|
||||
result = ltc_ecc_new_point();
|
||||
if (result == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
if ((err = mp_init(&prime)) != CRYPT_OK) {
|
||||
ltc_ecc_del_point(result);
|
||||
return err;
|
||||
}
|
||||
|
||||
if ((err = mp_read_radix(prime, (char *)private_key->dp->prime, 16)) != CRYPT_OK) { goto done; }
|
||||
if ((err = ltc_mp.ecc_ptmul(private_key->k, &public_key->pubkey, result, prime, 1)) != CRYPT_OK) { goto done; }
|
||||
|
||||
x = (unsigned long)mp_unsigned_bin_size(prime);
|
||||
if (*outlen < x) {
|
||||
*outlen = x;
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
goto done;
|
||||
}
|
||||
zeromem(out, x);
|
||||
if ((err = mp_to_unsigned_bin(result->x, out + (x - mp_unsigned_bin_size(result->x)))) != CRYPT_OK) { goto done; }
|
||||
|
||||
err = CRYPT_OK;
|
||||
*outlen = x;
|
||||
done:
|
||||
mp_clear(prime);
|
||||
ltc_ecc_del_point(result);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
171
thirdparty/libtomcrypt/pk/ecc/ecc_sign_hash.c
vendored
Normal file
171
thirdparty/libtomcrypt/pk/ecc/ecc_sign_hash.c
vendored
Normal file
@ -0,0 +1,171 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/**
|
||||
@file ecc_sign_hash.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
static int _ecc_sign_hash(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, ecc_key *key, int sigformat)
|
||||
{
|
||||
ecc_key pubkey;
|
||||
void *r, *s, *e, *p, *b;
|
||||
int err, max_iterations = LTC_PK_MAX_RETRIES;
|
||||
unsigned long pbits, pbytes, i, shift_right;
|
||||
unsigned char ch, buf[MAXBLOCKSIZE];
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
/* is this a private key? */
|
||||
if (key->type != PK_PRIVATE) {
|
||||
return CRYPT_PK_NOT_PRIVATE;
|
||||
}
|
||||
|
||||
/* is the IDX valid ? */
|
||||
if (ltc_ecc_is_valid_idx(key->idx) != 1) {
|
||||
return CRYPT_PK_INVALID_TYPE;
|
||||
}
|
||||
|
||||
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* init the bignums */
|
||||
if ((err = mp_init_multi(&r, &s, &p, &e, &b, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if ((err = mp_read_radix(p, (char *)key->dp->order, 16)) != CRYPT_OK) { goto errnokey; }
|
||||
|
||||
/* get the hash and load it as a bignum into 'e' */
|
||||
pbits = mp_count_bits(p);
|
||||
pbytes = (pbits+7) >> 3;
|
||||
if (pbits > inlen*8) {
|
||||
if ((err = mp_read_unsigned_bin(e, (unsigned char *)in, inlen)) != CRYPT_OK) { goto errnokey; }
|
||||
}
|
||||
else if (pbits % 8 == 0) {
|
||||
if ((err = mp_read_unsigned_bin(e, (unsigned char *)in, pbytes)) != CRYPT_OK) { goto errnokey; }
|
||||
}
|
||||
else {
|
||||
shift_right = 8 - pbits % 8;
|
||||
for (i=0, ch=0; i<pbytes; i++) {
|
||||
buf[i] = ch;
|
||||
ch = (in[i] << (8-shift_right));
|
||||
buf[i] = buf[i] ^ (in[i] >> shift_right);
|
||||
}
|
||||
if ((err = mp_read_unsigned_bin(e, (unsigned char *)buf, pbytes)) != CRYPT_OK) { goto errnokey; }
|
||||
}
|
||||
|
||||
/* make up a key and export the public copy */
|
||||
do {
|
||||
if ((err = ecc_make_key_ex(prng, wprng, &pubkey, key->dp)) != CRYPT_OK) {
|
||||
goto errnokey;
|
||||
}
|
||||
|
||||
/* find r = x1 mod n */
|
||||
if ((err = mp_mod(pubkey.pubkey.x, p, r)) != CRYPT_OK) { goto error; }
|
||||
|
||||
if (mp_iszero(r) == LTC_MP_YES) {
|
||||
ecc_free(&pubkey);
|
||||
} else {
|
||||
if ((err = rand_bn_upto(b, p, prng, wprng)) != CRYPT_OK) { goto error; } /* b = blinding value */
|
||||
/* find s = (e + xr)/k */
|
||||
if ((err = mp_mulmod(pubkey.k, b, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = kb */
|
||||
if ((err = mp_invmod(pubkey.k, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = 1/kb */
|
||||
if ((err = mp_mulmod(key->k, r, p, s)) != CRYPT_OK) { goto error; } /* s = xr */
|
||||
if ((err = mp_mulmod(pubkey.k, s, p, s)) != CRYPT_OK) { goto error; } /* s = xr/kb */
|
||||
if ((err = mp_mulmod(pubkey.k, e, p, e)) != CRYPT_OK) { goto error; } /* e = e/kb */
|
||||
if ((err = mp_add(e, s, s)) != CRYPT_OK) { goto error; } /* s = e/kb + xr/kb */
|
||||
if ((err = mp_mulmod(s, b, p, s)) != CRYPT_OK) { goto error; } /* s = b(e/kb + xr/kb) = (e + xr)/k */
|
||||
ecc_free(&pubkey);
|
||||
if (mp_iszero(s) == LTC_MP_NO) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (--max_iterations > 0);
|
||||
|
||||
if (max_iterations == 0) {
|
||||
goto errnokey;
|
||||
}
|
||||
|
||||
if (sigformat == 1) {
|
||||
/* RFC7518 format */
|
||||
if (*outlen < 2*pbytes) { err = CRYPT_MEM; goto errnokey; }
|
||||
zeromem(out, 2*pbytes);
|
||||
i = mp_unsigned_bin_size(r);
|
||||
if ((err = mp_to_unsigned_bin(r, out + (pbytes - i))) != CRYPT_OK) { goto errnokey; }
|
||||
i = mp_unsigned_bin_size(s);
|
||||
if ((err = mp_to_unsigned_bin(s, out + (2*pbytes - i))) != CRYPT_OK) { goto errnokey; }
|
||||
*outlen = 2*pbytes;
|
||||
err = CRYPT_OK;
|
||||
}
|
||||
else {
|
||||
/* store as ASN.1 SEQUENCE { r, s -- integer } */
|
||||
err = der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_INTEGER, 1UL, r,
|
||||
LTC_ASN1_INTEGER, 1UL, s,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
}
|
||||
goto errnokey;
|
||||
error:
|
||||
ecc_free(&pubkey);
|
||||
errnokey:
|
||||
mp_clear_multi(r, s, p, e, b, NULL);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Sign a message digest
|
||||
@param in The message digest to sign
|
||||
@param inlen The length of the digest
|
||||
@param out [out] The destination for the signature
|
||||
@param outlen [in/out] The max size and resulting size of the signature
|
||||
@param prng An active PRNG state
|
||||
@param wprng The index of the PRNG you wish to use
|
||||
@param key A private ECC key
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, ecc_key *key)
|
||||
{
|
||||
return _ecc_sign_hash(in, inlen, out, outlen, prng, wprng, key, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
Sign a message digest in RFC7518 format
|
||||
@param in The message digest to sign
|
||||
@param inlen The length of the digest
|
||||
@param out [out] The destination for the signature
|
||||
@param outlen [in/out] The max size and resulting size of the signature
|
||||
@param prng An active PRNG state
|
||||
@param wprng The index of the PRNG you wish to use
|
||||
@param key A private ECC key
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ecc_sign_hash_rfc7518(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, ecc_key *key)
|
||||
{
|
||||
return _ecc_sign_hash(in, inlen, out, outlen, prng, wprng, key, 1);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
46
thirdparty/libtomcrypt/pk/ecc/ecc_sizes.c
vendored
Normal file
46
thirdparty/libtomcrypt/pk/ecc/ecc_sizes.c
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecc_sizes.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
void ecc_sizes(int *low, int *high)
|
||||
{
|
||||
int i;
|
||||
LTC_ARGCHKVD(low != NULL);
|
||||
LTC_ARGCHKVD(high != NULL);
|
||||
|
||||
*low = INT_MAX;
|
||||
*high = 0;
|
||||
for (i = 0; ltc_ecc_sets[i].size != 0; i++) {
|
||||
if (ltc_ecc_sets[i].size < *low) {
|
||||
*low = ltc_ecc_sets[i].size;
|
||||
}
|
||||
if (ltc_ecc_sets[i].size > *high) {
|
||||
*high = ltc_ecc_sets[i].size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
93
thirdparty/libtomcrypt/pk/ecc/ecc_test.c
vendored
Normal file
93
thirdparty/libtomcrypt/pk/ecc/ecc_test.c
vendored
Normal file
@ -0,0 +1,93 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecc_test.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/**
|
||||
Perform on the ECC system
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ecc_test(void)
|
||||
{
|
||||
void *modulus, *order;
|
||||
ecc_point *G, *GG;
|
||||
int i, err, primality;
|
||||
|
||||
if ((err = mp_init_multi(&modulus, &order, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
G = ltc_ecc_new_point();
|
||||
GG = ltc_ecc_new_point();
|
||||
if (G == NULL || GG == NULL) {
|
||||
mp_clear_multi(modulus, order, NULL);
|
||||
ltc_ecc_del_point(G);
|
||||
ltc_ecc_del_point(GG);
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
for (i = 0; ltc_ecc_sets[i].size; i++) {
|
||||
#if 0
|
||||
printf("Testing %d\n", ltc_ecc_sets[i].size);
|
||||
#endif
|
||||
if ((err = mp_read_radix(modulus, (char *)ltc_ecc_sets[i].prime, 16)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_read_radix(order, (char *)ltc_ecc_sets[i].order, 16)) != CRYPT_OK) { goto done; }
|
||||
|
||||
/* is prime actually prime? */
|
||||
if ((err = mp_prime_is_prime(modulus, 8, &primality)) != CRYPT_OK) { goto done; }
|
||||
if (primality == 0) {
|
||||
err = CRYPT_FAIL_TESTVECTOR;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* is order prime ? */
|
||||
if ((err = mp_prime_is_prime(order, 8, &primality)) != CRYPT_OK) { goto done; }
|
||||
if (primality == 0) {
|
||||
err = CRYPT_FAIL_TESTVECTOR;
|
||||
goto done;
|
||||
}
|
||||
|
||||
if ((err = mp_read_radix(G->x, (char *)ltc_ecc_sets[i].Gx, 16)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_read_radix(G->y, (char *)ltc_ecc_sets[i].Gy, 16)) != CRYPT_OK) { goto done; }
|
||||
mp_set(G->z, 1);
|
||||
|
||||
/* then we should have G == (order + 1)G */
|
||||
if ((err = mp_add_d(order, 1, order)) != CRYPT_OK) { goto done; }
|
||||
if ((err = ltc_mp.ecc_ptmul(order, G, GG, modulus, 1)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp(G->x, GG->x) != LTC_MP_EQ || mp_cmp(G->y, GG->y) != LTC_MP_EQ) {
|
||||
err = CRYPT_FAIL_TESTVECTOR;
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
err = CRYPT_OK;
|
||||
done:
|
||||
ltc_ecc_del_point(GG);
|
||||
ltc_ecc_del_point(G);
|
||||
mp_clear_multi(order, modulus, NULL);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
200
thirdparty/libtomcrypt/pk/ecc/ecc_verify_hash.c
vendored
Normal file
200
thirdparty/libtomcrypt/pk/ecc/ecc_verify_hash.c
vendored
Normal file
@ -0,0 +1,200 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/**
|
||||
@file ecc_verify_hash.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
static int _ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int *stat, ecc_key *key, int sigformat)
|
||||
{
|
||||
ecc_point *mG, *mQ;
|
||||
void *r, *s, *v, *w, *u1, *u2, *e, *p, *m;
|
||||
void *mp;
|
||||
int err;
|
||||
unsigned long pbits, pbytes, i, shift_right;
|
||||
unsigned char ch, buf[MAXBLOCKSIZE];
|
||||
|
||||
LTC_ARGCHK(sig != NULL);
|
||||
LTC_ARGCHK(hash != NULL);
|
||||
LTC_ARGCHK(stat != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
/* default to invalid signature */
|
||||
*stat = 0;
|
||||
mp = NULL;
|
||||
|
||||
/* is the IDX valid ? */
|
||||
if (ltc_ecc_is_valid_idx(key->idx) != 1) {
|
||||
return CRYPT_PK_INVALID_TYPE;
|
||||
}
|
||||
|
||||
/* allocate ints */
|
||||
if ((err = mp_init_multi(&r, &s, &v, &w, &u1, &u2, &p, &e, &m, NULL)) != CRYPT_OK) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* allocate points */
|
||||
mG = ltc_ecc_new_point();
|
||||
mQ = ltc_ecc_new_point();
|
||||
if (mQ == NULL || mG == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (sigformat == 1) {
|
||||
/* RFC7518 format */
|
||||
if ((siglen % 2) == 1) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto error;
|
||||
}
|
||||
i = siglen / 2;
|
||||
if ((err = mp_read_unsigned_bin(r, (unsigned char *)sig, i)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_read_unsigned_bin(s, (unsigned char *)sig+i, i)) != CRYPT_OK) { goto error; }
|
||||
}
|
||||
else {
|
||||
/* ASN.1 format */
|
||||
if ((err = der_decode_sequence_multi(sig, siglen,
|
||||
LTC_ASN1_INTEGER, 1UL, r,
|
||||
LTC_ASN1_INTEGER, 1UL, s,
|
||||
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { goto error; }
|
||||
}
|
||||
|
||||
/* get the order */
|
||||
if ((err = mp_read_radix(p, (char *)key->dp->order, 16)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* get the modulus */
|
||||
if ((err = mp_read_radix(m, (char *)key->dp->prime, 16)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* check for zero */
|
||||
if (mp_iszero(r) || mp_iszero(s) || mp_cmp(r, p) != LTC_MP_LT || mp_cmp(s, p) != LTC_MP_LT) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* read hash - truncate if needed */
|
||||
pbits = mp_count_bits(p);
|
||||
pbytes = (pbits+7) >> 3;
|
||||
if (pbits > hashlen*8) {
|
||||
if ((err = mp_read_unsigned_bin(e, (unsigned char *)hash, hashlen)) != CRYPT_OK) { goto error; }
|
||||
}
|
||||
else if (pbits % 8 == 0) {
|
||||
if ((err = mp_read_unsigned_bin(e, (unsigned char *)hash, pbytes)) != CRYPT_OK) { goto error; }
|
||||
}
|
||||
else {
|
||||
shift_right = 8 - pbits % 8;
|
||||
for (i=0, ch=0; i<pbytes; i++) {
|
||||
buf[i] = ch;
|
||||
ch = (hash[i] << (8-shift_right));
|
||||
buf[i] = buf[i] ^ (hash[i] >> shift_right);
|
||||
}
|
||||
if ((err = mp_read_unsigned_bin(e, (unsigned char *)buf, pbytes)) != CRYPT_OK) { goto error; }
|
||||
}
|
||||
|
||||
/* w = s^-1 mod n */
|
||||
if ((err = mp_invmod(s, p, w)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* u1 = ew */
|
||||
if ((err = mp_mulmod(e, w, p, u1)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* u2 = rw */
|
||||
if ((err = mp_mulmod(r, w, p, u2)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* find mG and mQ */
|
||||
if ((err = mp_read_radix(mG->x, (char *)key->dp->Gx, 16)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_read_radix(mG->y, (char *)key->dp->Gy, 16)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_set(mG->z, 1)) != CRYPT_OK) { goto error; }
|
||||
|
||||
if ((err = mp_copy(key->pubkey.x, mQ->x)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_copy(key->pubkey.y, mQ->y)) != CRYPT_OK) { goto error; }
|
||||
if ((err = mp_copy(key->pubkey.z, mQ->z)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* compute u1*mG + u2*mQ = mG */
|
||||
if (ltc_mp.ecc_mul2add == NULL) {
|
||||
if ((err = ltc_mp.ecc_ptmul(u1, mG, mG, m, 0)) != CRYPT_OK) { goto error; }
|
||||
if ((err = ltc_mp.ecc_ptmul(u2, mQ, mQ, m, 0)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* find the montgomery mp */
|
||||
if ((err = mp_montgomery_setup(m, &mp)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* add them */
|
||||
if ((err = ltc_mp.ecc_ptadd(mQ, mG, mG, m, mp)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* reduce */
|
||||
if ((err = ltc_mp.ecc_map(mG, m, mp)) != CRYPT_OK) { goto error; }
|
||||
} else {
|
||||
/* use Shamir's trick to compute u1*mG + u2*mQ using half of the doubles */
|
||||
if ((err = ltc_mp.ecc_mul2add(mG, u1, mQ, u2, mG, m)) != CRYPT_OK) { goto error; }
|
||||
}
|
||||
|
||||
/* v = X_x1 mod n */
|
||||
if ((err = mp_mod(mG->x, p, v)) != CRYPT_OK) { goto error; }
|
||||
|
||||
/* does v == r */
|
||||
if (mp_cmp(v, r) == LTC_MP_EQ) {
|
||||
*stat = 1;
|
||||
}
|
||||
|
||||
/* clear up and return */
|
||||
err = CRYPT_OK;
|
||||
error:
|
||||
ltc_ecc_del_point(mG);
|
||||
ltc_ecc_del_point(mQ);
|
||||
mp_clear_multi(r, s, v, w, u1, u2, p, e, m, NULL);
|
||||
if (mp != NULL) {
|
||||
mp_montgomery_free(mp);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Verify an ECC signature
|
||||
@param sig The signature to verify
|
||||
@param siglen The length of the signature (octets)
|
||||
@param hash The hash (message digest) that was signed
|
||||
@param hashlen The length of the hash (octets)
|
||||
@param stat Result of signature, 1==valid, 0==invalid
|
||||
@param key The corresponding public ECC key
|
||||
@return CRYPT_OK if successful (even if the signature is not valid)
|
||||
*/
|
||||
int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int *stat, ecc_key *key)
|
||||
{
|
||||
return _ecc_verify_hash(sig, siglen, hash, hashlen, stat, key, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
Verify an ECC signature in RFC7518 format
|
||||
@param sig The signature to verify
|
||||
@param siglen The length of the signature (octets)
|
||||
@param hash The hash (message digest) that was signed
|
||||
@param hashlen The length of the hash (octets)
|
||||
@param stat Result of signature, 1==valid, 0==invalid
|
||||
@param key The corresponding public ECC key
|
||||
@return CRYPT_OK if successful (even if the signature is not valid)
|
||||
*/
|
||||
int ecc_verify_hash_rfc7518(const unsigned char *sig, unsigned long siglen,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int *stat, ecc_key *key)
|
||||
{
|
||||
return _ecc_verify_hash(sig, siglen, hash, hashlen, stat, key, 1);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
44
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_is_valid_idx.c
vendored
Normal file
44
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_is_valid_idx.c
vendored
Normal file
@ -0,0 +1,44 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ltc_ecc_is_valid_idx.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/** Returns whether an ECC idx is valid or not
|
||||
@param n The idx number to check
|
||||
@return 1 if valid, 0 if not
|
||||
*/
|
||||
int ltc_ecc_is_valid_idx(int n)
|
||||
{
|
||||
int x;
|
||||
|
||||
for (x = 0; ltc_ecc_sets[x].size != 0; x++);
|
||||
/* -1 is a valid index --- indicating that the domain params were supplied by the user */
|
||||
if ((n >= -1) && (n < x)) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
74
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_map.c
vendored
Normal file
74
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_map.c
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ltc_ecc_map.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/**
|
||||
Map a projective jacbobian point back to affine space
|
||||
@param P [in/out] The point to map
|
||||
@param modulus The modulus of the field the ECC curve is in
|
||||
@param mp The "b" value from montgomery_setup()
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int ltc_ecc_map(ecc_point *P, void *modulus, void *mp)
|
||||
{
|
||||
void *t1, *t2;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(P != NULL);
|
||||
LTC_ARGCHK(modulus != NULL);
|
||||
LTC_ARGCHK(mp != NULL);
|
||||
|
||||
if ((err = mp_init_multi(&t1, &t2, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* first map z back to normal */
|
||||
if ((err = mp_montgomery_reduce(P->z, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
|
||||
/* get 1/z */
|
||||
if ((err = mp_invmod(P->z, modulus, t1)) != CRYPT_OK) { goto done; }
|
||||
|
||||
/* get 1/z^2 and 1/z^3 */
|
||||
if ((err = mp_sqr(t1, t2)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_mod(t2, modulus, t2)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_mul(t1, t2, t1)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_mod(t1, modulus, t1)) != CRYPT_OK) { goto done; }
|
||||
|
||||
/* multiply against x/y */
|
||||
if ((err = mp_mul(P->x, t2, P->x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(P->x, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_mul(P->y, t1, P->y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(P->y, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_set(P->z, 1)) != CRYPT_OK) { goto done; }
|
||||
|
||||
err = CRYPT_OK;
|
||||
done:
|
||||
mp_clear_multi(t1, t2, NULL);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
206
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_mul2add.c
vendored
Normal file
206
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_mul2add.c
vendored
Normal file
@ -0,0 +1,206 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ltc_ecc_mul2add.c
|
||||
ECC Crypto, Shamir's Trick, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
#ifdef LTC_ECC_SHAMIR
|
||||
|
||||
/** Computes kA*A + kB*B = C using Shamir's Trick
|
||||
@param A First point to multiply
|
||||
@param kA What to multiple A by
|
||||
@param B Second point to multiply
|
||||
@param kB What to multiple B by
|
||||
@param C [out] Destination point (can overlap with A or B
|
||||
@param modulus Modulus for curve
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int ltc_ecc_mul2add(ecc_point *A, void *kA,
|
||||
ecc_point *B, void *kB,
|
||||
ecc_point *C,
|
||||
void *modulus)
|
||||
{
|
||||
ecc_point *precomp[16];
|
||||
unsigned bitbufA, bitbufB, lenA, lenB, len, x, y, nA, nB, nibble;
|
||||
unsigned char *tA, *tB;
|
||||
int err, first;
|
||||
void *mp, *mu;
|
||||
|
||||
/* argchks */
|
||||
LTC_ARGCHK(A != NULL);
|
||||
LTC_ARGCHK(B != NULL);
|
||||
LTC_ARGCHK(C != NULL);
|
||||
LTC_ARGCHK(kA != NULL);
|
||||
LTC_ARGCHK(kB != NULL);
|
||||
LTC_ARGCHK(modulus != NULL);
|
||||
|
||||
/* allocate memory */
|
||||
tA = XCALLOC(1, ECC_BUF_SIZE);
|
||||
if (tA == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
tB = XCALLOC(1, ECC_BUF_SIZE);
|
||||
if (tB == NULL) {
|
||||
XFREE(tA);
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* get sizes */
|
||||
lenA = mp_unsigned_bin_size(kA);
|
||||
lenB = mp_unsigned_bin_size(kB);
|
||||
len = MAX(lenA, lenB);
|
||||
|
||||
/* sanity check */
|
||||
if ((lenA > ECC_BUF_SIZE) || (lenB > ECC_BUF_SIZE)) {
|
||||
err = CRYPT_INVALID_ARG;
|
||||
goto ERR_T;
|
||||
}
|
||||
|
||||
/* extract and justify kA */
|
||||
mp_to_unsigned_bin(kA, (len - lenA) + tA);
|
||||
|
||||
/* extract and justify kB */
|
||||
mp_to_unsigned_bin(kB, (len - lenB) + tB);
|
||||
|
||||
/* allocate the table */
|
||||
for (x = 0; x < 16; x++) {
|
||||
precomp[x] = ltc_ecc_new_point();
|
||||
if (precomp[x] == NULL) {
|
||||
for (y = 0; y < x; ++y) {
|
||||
ltc_ecc_del_point(precomp[y]);
|
||||
}
|
||||
err = CRYPT_MEM;
|
||||
goto ERR_T;
|
||||
}
|
||||
}
|
||||
|
||||
/* init montgomery reduction */
|
||||
if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) {
|
||||
goto ERR_P;
|
||||
}
|
||||
if ((err = mp_init(&mu)) != CRYPT_OK) {
|
||||
goto ERR_MP;
|
||||
}
|
||||
if ((err = mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) {
|
||||
goto ERR_MU;
|
||||
}
|
||||
|
||||
/* copy ones ... */
|
||||
if ((err = mp_mulmod(A->x, mu, modulus, precomp[1]->x)) != CRYPT_OK) { goto ERR_MU; }
|
||||
if ((err = mp_mulmod(A->y, mu, modulus, precomp[1]->y)) != CRYPT_OK) { goto ERR_MU; }
|
||||
if ((err = mp_mulmod(A->z, mu, modulus, precomp[1]->z)) != CRYPT_OK) { goto ERR_MU; }
|
||||
|
||||
if ((err = mp_mulmod(B->x, mu, modulus, precomp[1<<2]->x)) != CRYPT_OK) { goto ERR_MU; }
|
||||
if ((err = mp_mulmod(B->y, mu, modulus, precomp[1<<2]->y)) != CRYPT_OK) { goto ERR_MU; }
|
||||
if ((err = mp_mulmod(B->z, mu, modulus, precomp[1<<2]->z)) != CRYPT_OK) { goto ERR_MU; }
|
||||
|
||||
/* precomp [i,0](A + B) table */
|
||||
if ((err = ltc_mp.ecc_ptdbl(precomp[1], precomp[2], modulus, mp)) != CRYPT_OK) { goto ERR_MU; }
|
||||
if ((err = ltc_mp.ecc_ptadd(precomp[1], precomp[2], precomp[3], modulus, mp)) != CRYPT_OK) { goto ERR_MU; }
|
||||
|
||||
/* precomp [0,i](A + B) table */
|
||||
if ((err = ltc_mp.ecc_ptdbl(precomp[1<<2], precomp[2<<2], modulus, mp)) != CRYPT_OK) { goto ERR_MU; }
|
||||
if ((err = ltc_mp.ecc_ptadd(precomp[1<<2], precomp[2<<2], precomp[3<<2], modulus, mp)) != CRYPT_OK) { goto ERR_MU; }
|
||||
|
||||
/* precomp [i,j](A + B) table (i != 0, j != 0) */
|
||||
for (x = 1; x < 4; x++) {
|
||||
for (y = 1; y < 4; y++) {
|
||||
if ((err = ltc_mp.ecc_ptadd(precomp[x], precomp[(y<<2)], precomp[x+(y<<2)], modulus, mp)) != CRYPT_OK) { goto ERR_MU; }
|
||||
}
|
||||
}
|
||||
|
||||
nibble = 3;
|
||||
first = 1;
|
||||
bitbufA = tA[0];
|
||||
bitbufB = tB[0];
|
||||
|
||||
/* for every byte of the multiplicands */
|
||||
for (x = 0;; ) {
|
||||
/* grab a nibble */
|
||||
if (++nibble == 4) {
|
||||
if (x == len) break;
|
||||
bitbufA = tA[x];
|
||||
bitbufB = tB[x];
|
||||
nibble = 0;
|
||||
++x;
|
||||
}
|
||||
|
||||
/* extract two bits from both, shift/update */
|
||||
nA = (bitbufA >> 6) & 0x03;
|
||||
nB = (bitbufB >> 6) & 0x03;
|
||||
bitbufA = (bitbufA << 2) & 0xFF;
|
||||
bitbufB = (bitbufB << 2) & 0xFF;
|
||||
|
||||
/* if both zero, if first, continue */
|
||||
if ((nA == 0) && (nB == 0) && (first == 1)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/* double twice, only if this isn't the first */
|
||||
if (first == 0) {
|
||||
/* double twice */
|
||||
if ((err = ltc_mp.ecc_ptdbl(C, C, modulus, mp)) != CRYPT_OK) { goto ERR_MU; }
|
||||
if ((err = ltc_mp.ecc_ptdbl(C, C, modulus, mp)) != CRYPT_OK) { goto ERR_MU; }
|
||||
}
|
||||
|
||||
/* if not both zero */
|
||||
if ((nA != 0) || (nB != 0)) {
|
||||
if (first == 1) {
|
||||
/* if first, copy from table */
|
||||
first = 0;
|
||||
if ((err = mp_copy(precomp[nA + (nB<<2)]->x, C->x)) != CRYPT_OK) { goto ERR_MU; }
|
||||
if ((err = mp_copy(precomp[nA + (nB<<2)]->y, C->y)) != CRYPT_OK) { goto ERR_MU; }
|
||||
if ((err = mp_copy(precomp[nA + (nB<<2)]->z, C->z)) != CRYPT_OK) { goto ERR_MU; }
|
||||
} else {
|
||||
/* if not first, add from table */
|
||||
if ((err = ltc_mp.ecc_ptadd(C, precomp[nA + (nB<<2)], C, modulus, mp)) != CRYPT_OK) { goto ERR_MU; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* reduce to affine */
|
||||
err = ltc_ecc_map(C, modulus, mp);
|
||||
|
||||
/* clean up */
|
||||
ERR_MU:
|
||||
mp_clear(mu);
|
||||
ERR_MP:
|
||||
mp_montgomery_free(mp);
|
||||
ERR_P:
|
||||
for (x = 0; x < 16; x++) {
|
||||
ltc_ecc_del_point(precomp[x]);
|
||||
}
|
||||
ERR_T:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(tA, ECC_BUF_SIZE);
|
||||
zeromem(tB, ECC_BUF_SIZE);
|
||||
#endif
|
||||
XFREE(tA);
|
||||
XFREE(tB);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
220
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_mulmod.c
vendored
Normal file
220
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_mulmod.c
vendored
Normal file
@ -0,0 +1,220 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ltc_ecc_mulmod.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
#ifndef LTC_ECC_TIMING_RESISTANT
|
||||
|
||||
/* size of sliding window, don't change this! */
|
||||
#define WINSIZE 4
|
||||
|
||||
/**
|
||||
Perform a point multiplication
|
||||
@param k The scalar to multiply by
|
||||
@param G The base point
|
||||
@param R [out] Destination for kG
|
||||
@param modulus The modulus of the field the ECC curve is in
|
||||
@param map Boolean whether to map back to affine or not (1==map, 0 == leave in projective)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map)
|
||||
{
|
||||
ecc_point *tG, *M[8];
|
||||
int i, j, err;
|
||||
void *mu, *mp;
|
||||
ltc_mp_digit buf;
|
||||
int first, bitbuf, bitcpy, bitcnt, mode, digidx;
|
||||
|
||||
LTC_ARGCHK(k != NULL);
|
||||
LTC_ARGCHK(G != NULL);
|
||||
LTC_ARGCHK(R != NULL);
|
||||
LTC_ARGCHK(modulus != NULL);
|
||||
|
||||
/* init montgomery reduction */
|
||||
if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if ((err = mp_init(&mu)) != CRYPT_OK) {
|
||||
mp_montgomery_free(mp);
|
||||
return err;
|
||||
}
|
||||
if ((err = mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) {
|
||||
mp_montgomery_free(mp);
|
||||
mp_clear(mu);
|
||||
return err;
|
||||
}
|
||||
|
||||
/* alloc ram for window temps */
|
||||
for (i = 0; i < 8; i++) {
|
||||
M[i] = ltc_ecc_new_point();
|
||||
if (M[i] == NULL) {
|
||||
for (j = 0; j < i; j++) {
|
||||
ltc_ecc_del_point(M[j]);
|
||||
}
|
||||
mp_montgomery_free(mp);
|
||||
mp_clear(mu);
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
/* make a copy of G incase R==G */
|
||||
tG = ltc_ecc_new_point();
|
||||
if (tG == NULL) { err = CRYPT_MEM; goto done; }
|
||||
|
||||
/* tG = G and convert to montgomery */
|
||||
if (mp_cmp_d(mu, 1) == LTC_MP_EQ) {
|
||||
if ((err = mp_copy(G->x, tG->x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(G->y, tG->y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(G->z, tG->z)) != CRYPT_OK) { goto done; }
|
||||
} else {
|
||||
if ((err = mp_mulmod(G->x, mu, modulus, tG->x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_mulmod(G->y, mu, modulus, tG->y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_mulmod(G->z, mu, modulus, tG->z)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
mp_clear(mu);
|
||||
mu = NULL;
|
||||
|
||||
/* calc the M tab, which holds kG for k==8..15 */
|
||||
/* M[0] == 8G */
|
||||
if ((err = ltc_mp.ecc_ptdbl(tG, M[0], modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
if ((err = ltc_mp.ecc_ptdbl(M[0], M[0], modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
if ((err = ltc_mp.ecc_ptdbl(M[0], M[0], modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
|
||||
/* now find (8+k)G for k=1..7 */
|
||||
for (j = 9; j < 16; j++) {
|
||||
if ((err = ltc_mp.ecc_ptadd(M[j-9], tG, M[j-8], modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
|
||||
/* setup sliding window */
|
||||
mode = 0;
|
||||
bitcnt = 1;
|
||||
buf = 0;
|
||||
digidx = mp_get_digit_count(k) - 1;
|
||||
bitcpy = bitbuf = 0;
|
||||
first = 1;
|
||||
|
||||
/* perform ops */
|
||||
for (;;) {
|
||||
/* grab next digit as required */
|
||||
if (--bitcnt == 0) {
|
||||
if (digidx == -1) {
|
||||
break;
|
||||
}
|
||||
buf = mp_get_digit(k, digidx);
|
||||
bitcnt = (int) ltc_mp.bits_per_digit;
|
||||
--digidx;
|
||||
}
|
||||
|
||||
/* grab the next msb from the ltiplicand */
|
||||
i = (buf >> (ltc_mp.bits_per_digit - 1)) & 1;
|
||||
buf <<= 1;
|
||||
|
||||
/* skip leading zero bits */
|
||||
if (mode == 0 && i == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/* if the bit is zero and mode == 1 then we double */
|
||||
if (mode == 1 && i == 0) {
|
||||
if ((err = ltc_mp.ecc_ptdbl(R, R, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
continue;
|
||||
}
|
||||
|
||||
/* else we add it to the window */
|
||||
bitbuf |= (i << (WINSIZE - ++bitcpy));
|
||||
mode = 2;
|
||||
|
||||
if (bitcpy == WINSIZE) {
|
||||
/* if this is the first window we do a simple copy */
|
||||
if (first == 1) {
|
||||
/* R = kG [k = first window] */
|
||||
if ((err = mp_copy(M[bitbuf-8]->x, R->x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(M[bitbuf-8]->y, R->y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(M[bitbuf-8]->z, R->z)) != CRYPT_OK) { goto done; }
|
||||
first = 0;
|
||||
} else {
|
||||
/* normal window */
|
||||
/* ok window is filled so double as required and add */
|
||||
/* double first */
|
||||
for (j = 0; j < WINSIZE; j++) {
|
||||
if ((err = ltc_mp.ecc_ptdbl(R, R, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
|
||||
/* then add, bitbuf will be 8..15 [8..2^WINSIZE] guaranteed */
|
||||
if ((err = ltc_mp.ecc_ptadd(R, M[bitbuf-8], R, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* empty window and reset */
|
||||
bitcpy = bitbuf = 0;
|
||||
mode = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* if bits remain then double/add */
|
||||
if (mode == 2 && bitcpy > 0) {
|
||||
/* double then add */
|
||||
for (j = 0; j < bitcpy; j++) {
|
||||
/* only double if we have had at least one add first */
|
||||
if (first == 0) {
|
||||
if ((err = ltc_mp.ecc_ptdbl(R, R, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
|
||||
bitbuf <<= 1;
|
||||
if ((bitbuf & (1 << WINSIZE)) != 0) {
|
||||
if (first == 1){
|
||||
/* first add, so copy */
|
||||
if ((err = mp_copy(tG->x, R->x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(tG->y, R->y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(tG->z, R->z)) != CRYPT_OK) { goto done; }
|
||||
first = 0;
|
||||
} else {
|
||||
/* then add */
|
||||
if ((err = ltc_mp.ecc_ptadd(R, tG, R, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* map R back from projective space */
|
||||
if (map) {
|
||||
err = ltc_ecc_map(R, modulus, mp);
|
||||
} else {
|
||||
err = CRYPT_OK;
|
||||
}
|
||||
done:
|
||||
if (mu != NULL) {
|
||||
mp_clear(mu);
|
||||
}
|
||||
mp_montgomery_free(mp);
|
||||
ltc_ecc_del_point(tG);
|
||||
for (i = 0; i < 8; i++) {
|
||||
ltc_ecc_del_point(M[i]);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#undef WINSIZE
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
163
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_mulmod_timing.c
vendored
Normal file
163
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_mulmod_timing.c
vendored
Normal file
@ -0,0 +1,163 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ltc_ecc_mulmod_timing.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
#ifdef LTC_ECC_TIMING_RESISTANT
|
||||
|
||||
/**
|
||||
Perform a point multiplication (timing resistant)
|
||||
@param k The scalar to multiply by
|
||||
@param G The base point
|
||||
@param R [out] Destination for kG
|
||||
@param modulus The modulus of the field the ECC curve is in
|
||||
@param map Boolean whether to map back to affine or not (1==map, 0 == leave in projective)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map)
|
||||
{
|
||||
ecc_point *tG, *M[3];
|
||||
int i, j, err;
|
||||
void *mu, *mp;
|
||||
ltc_mp_digit buf;
|
||||
int bitcnt, mode, digidx;
|
||||
|
||||
LTC_ARGCHK(k != NULL);
|
||||
LTC_ARGCHK(G != NULL);
|
||||
LTC_ARGCHK(R != NULL);
|
||||
LTC_ARGCHK(modulus != NULL);
|
||||
|
||||
/* init montgomery reduction */
|
||||
if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if ((err = mp_init(&mu)) != CRYPT_OK) {
|
||||
mp_montgomery_free(mp);
|
||||
return err;
|
||||
}
|
||||
if ((err = mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) {
|
||||
mp_clear(mu);
|
||||
mp_montgomery_free(mp);
|
||||
return err;
|
||||
}
|
||||
|
||||
/* alloc ram for window temps */
|
||||
for (i = 0; i < 3; i++) {
|
||||
M[i] = ltc_ecc_new_point();
|
||||
if (M[i] == NULL) {
|
||||
for (j = 0; j < i; j++) {
|
||||
ltc_ecc_del_point(M[j]);
|
||||
}
|
||||
mp_clear(mu);
|
||||
mp_montgomery_free(mp);
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
/* make a copy of G incase R==G */
|
||||
tG = ltc_ecc_new_point();
|
||||
if (tG == NULL) { err = CRYPT_MEM; goto done; }
|
||||
|
||||
/* tG = G and convert to montgomery */
|
||||
if ((err = mp_mulmod(G->x, mu, modulus, tG->x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_mulmod(G->y, mu, modulus, tG->y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_mulmod(G->z, mu, modulus, tG->z)) != CRYPT_OK) { goto done; }
|
||||
mp_clear(mu);
|
||||
mu = NULL;
|
||||
|
||||
/* calc the M tab */
|
||||
/* M[0] == G */
|
||||
if ((err = mp_copy(tG->x, M[0]->x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(tG->y, M[0]->y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(tG->z, M[0]->z)) != CRYPT_OK) { goto done; }
|
||||
/* M[1] == 2G */
|
||||
if ((err = ltc_mp.ecc_ptdbl(tG, M[1], modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
|
||||
/* setup sliding window */
|
||||
mode = 0;
|
||||
bitcnt = 1;
|
||||
buf = 0;
|
||||
digidx = mp_get_digit_count(k) - 1;
|
||||
|
||||
/* perform ops */
|
||||
for (;;) {
|
||||
/* grab next digit as required */
|
||||
if (--bitcnt == 0) {
|
||||
if (digidx == -1) {
|
||||
break;
|
||||
}
|
||||
buf = mp_get_digit(k, digidx);
|
||||
bitcnt = (int) MP_DIGIT_BIT;
|
||||
--digidx;
|
||||
}
|
||||
|
||||
/* grab the next msb from the ltiplicand */
|
||||
i = (buf >> (MP_DIGIT_BIT - 1)) & 1;
|
||||
buf <<= 1;
|
||||
|
||||
if (mode == 0 && i == 0) {
|
||||
/* dummy operations */
|
||||
if ((err = ltc_mp.ecc_ptadd(M[0], M[1], M[2], modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
if ((err = ltc_mp.ecc_ptdbl(M[1], M[2], modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
continue;
|
||||
}
|
||||
|
||||
if (mode == 0 && i == 1) {
|
||||
mode = 1;
|
||||
/* dummy operations */
|
||||
if ((err = ltc_mp.ecc_ptadd(M[0], M[1], M[2], modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
if ((err = ltc_mp.ecc_ptdbl(M[1], M[2], modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((err = ltc_mp.ecc_ptadd(M[0], M[1], M[i^1], modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
if ((err = ltc_mp.ecc_ptdbl(M[i], M[i], modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
|
||||
/* copy result out */
|
||||
if ((err = mp_copy(M[0]->x, R->x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(M[0]->y, R->y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(M[0]->z, R->z)) != CRYPT_OK) { goto done; }
|
||||
|
||||
/* map R back from projective space */
|
||||
if (map) {
|
||||
err = ltc_ecc_map(R, modulus, mp);
|
||||
} else {
|
||||
err = CRYPT_OK;
|
||||
}
|
||||
done:
|
||||
if (mu != NULL) {
|
||||
mp_clear(mu);
|
||||
}
|
||||
mp_montgomery_free(mp);
|
||||
ltc_ecc_del_point(tG);
|
||||
for (i = 0; i < 3; i++) {
|
||||
ltc_ecc_del_point(M[i]);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
58
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_points.c
vendored
Normal file
58
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_points.c
vendored
Normal file
@ -0,0 +1,58 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ltc_ecc_points.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/**
|
||||
Allocate a new ECC point
|
||||
@return A newly allocated point or NULL on error
|
||||
*/
|
||||
ecc_point *ltc_ecc_new_point(void)
|
||||
{
|
||||
ecc_point *p;
|
||||
p = XCALLOC(1, sizeof(*p));
|
||||
if (p == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (mp_init_multi(&p->x, &p->y, &p->z, NULL) != CRYPT_OK) {
|
||||
XFREE(p);
|
||||
return NULL;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
/** Free an ECC point from memory
|
||||
@param p The point to free
|
||||
*/
|
||||
void ltc_ecc_del_point(ecc_point *p)
|
||||
{
|
||||
/* prevents free'ing null arguments */
|
||||
if (p != NULL) {
|
||||
mp_clear_multi(p->x, p->y, p->z, NULL); /* note: p->z may be NULL but that's ok with this function anyways */
|
||||
XFREE(p);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
194
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_projective_add_point.c
vendored
Normal file
194
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_projective_add_point.c
vendored
Normal file
@ -0,0 +1,194 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ltc_ecc_projective_add_point.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#if defined(LTC_MECC) && (!defined(LTC_MECC_ACCEL) || defined(LTM_DESC))
|
||||
|
||||
/**
|
||||
Add two ECC points
|
||||
@param P The point to add
|
||||
@param Q The point to add
|
||||
@param R [out] The destination of the double
|
||||
@param modulus The modulus of the field the ECC curve is in
|
||||
@param mp The "b" value from montgomery_setup()
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp)
|
||||
{
|
||||
void *t1, *t2, *x, *y, *z;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(P != NULL);
|
||||
LTC_ARGCHK(Q != NULL);
|
||||
LTC_ARGCHK(R != NULL);
|
||||
LTC_ARGCHK(modulus != NULL);
|
||||
LTC_ARGCHK(mp != NULL);
|
||||
|
||||
if ((err = mp_init_multi(&t1, &t2, &x, &y, &z, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* should we dbl instead? */
|
||||
if ((err = mp_sub(modulus, Q->y, t1)) != CRYPT_OK) { goto done; }
|
||||
|
||||
if ( (mp_cmp(P->x, Q->x) == LTC_MP_EQ) &&
|
||||
(Q->z != NULL && mp_cmp(P->z, Q->z) == LTC_MP_EQ) &&
|
||||
(mp_cmp(P->y, Q->y) == LTC_MP_EQ || mp_cmp(P->y, t1) == LTC_MP_EQ)) {
|
||||
mp_clear_multi(t1, t2, x, y, z, NULL);
|
||||
return ltc_ecc_projective_dbl_point(P, R, modulus, mp);
|
||||
}
|
||||
|
||||
if ((err = mp_copy(P->x, x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(P->y, y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(P->z, z)) != CRYPT_OK) { goto done; }
|
||||
|
||||
/* if Z is one then these are no-operations */
|
||||
if (Q->z != NULL) {
|
||||
/* T1 = Z' * Z' */
|
||||
if ((err = mp_sqr(Q->z, t1)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* X = X * T1 */
|
||||
if ((err = mp_mul(t1, x, x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(x, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* T1 = Z' * T1 */
|
||||
if ((err = mp_mul(Q->z, t1, t1)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* Y = Y * T1 */
|
||||
if ((err = mp_mul(t1, y, y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(y, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
|
||||
/* T1 = Z*Z */
|
||||
if ((err = mp_sqr(z, t1)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* T2 = X' * T1 */
|
||||
if ((err = mp_mul(Q->x, t1, t2)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* T1 = Z * T1 */
|
||||
if ((err = mp_mul(z, t1, t1)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* T1 = Y' * T1 */
|
||||
if ((err = mp_mul(Q->y, t1, t1)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
|
||||
/* Y = Y - T1 */
|
||||
if ((err = mp_sub(y, t1, y)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp_d(y, 0) == LTC_MP_LT) {
|
||||
if ((err = mp_add(y, modulus, y)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* T1 = 2T1 */
|
||||
if ((err = mp_add(t1, t1, t1)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp(t1, modulus) != LTC_MP_LT) {
|
||||
if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* T1 = Y + T1 */
|
||||
if ((err = mp_add(t1, y, t1)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp(t1, modulus) != LTC_MP_LT) {
|
||||
if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* X = X - T2 */
|
||||
if ((err = mp_sub(x, t2, x)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp_d(x, 0) == LTC_MP_LT) {
|
||||
if ((err = mp_add(x, modulus, x)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* T2 = 2T2 */
|
||||
if ((err = mp_add(t2, t2, t2)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp(t2, modulus) != LTC_MP_LT) {
|
||||
if ((err = mp_sub(t2, modulus, t2)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* T2 = X + T2 */
|
||||
if ((err = mp_add(t2, x, t2)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp(t2, modulus) != LTC_MP_LT) {
|
||||
if ((err = mp_sub(t2, modulus, t2)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
|
||||
/* if Z' != 1 */
|
||||
if (Q->z != NULL) {
|
||||
/* Z = Z * Z' */
|
||||
if ((err = mp_mul(z, Q->z, z)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(z, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
|
||||
/* Z = Z * X */
|
||||
if ((err = mp_mul(z, x, z)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(z, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
|
||||
/* T1 = T1 * X */
|
||||
if ((err = mp_mul(t1, x, t1)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* X = X * X */
|
||||
if ((err = mp_sqr(x, x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(x, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* T2 = T2 * x */
|
||||
if ((err = mp_mul(t2, x, t2)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* T1 = T1 * X */
|
||||
if ((err = mp_mul(t1, x, t1)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
|
||||
/* X = Y*Y */
|
||||
if ((err = mp_sqr(y, x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(x, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* X = X - T2 */
|
||||
if ((err = mp_sub(x, t2, x)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp_d(x, 0) == LTC_MP_LT) {
|
||||
if ((err = mp_add(x, modulus, x)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
|
||||
/* T2 = T2 - X */
|
||||
if ((err = mp_sub(t2, x, t2)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp_d(t2, 0) == LTC_MP_LT) {
|
||||
if ((err = mp_add(t2, modulus, t2)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* T2 = T2 - X */
|
||||
if ((err = mp_sub(t2, x, t2)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp_d(t2, 0) == LTC_MP_LT) {
|
||||
if ((err = mp_add(t2, modulus, t2)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* T2 = T2 * Y */
|
||||
if ((err = mp_mul(t2, y, t2)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* Y = T2 - T1 */
|
||||
if ((err = mp_sub(t2, t1, y)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp_d(y, 0) == LTC_MP_LT) {
|
||||
if ((err = mp_add(y, modulus, y)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* Y = Y/2 */
|
||||
if (mp_isodd(y)) {
|
||||
if ((err = mp_add(y, modulus, y)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
if ((err = mp_div_2(y, y)) != CRYPT_OK) { goto done; }
|
||||
|
||||
if ((err = mp_copy(x, R->x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(y, R->y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(z, R->z)) != CRYPT_OK) { goto done; }
|
||||
|
||||
err = CRYPT_OK;
|
||||
done:
|
||||
mp_clear_multi(t1, t2, x, y, z, NULL);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
145
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_projective_dbl_point.c
vendored
Normal file
145
thirdparty/libtomcrypt/pk/ecc/ltc_ecc_projective_dbl_point.c
vendored
Normal file
@ -0,0 +1,145 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
||||
*
|
||||
* All curves taken from NIST recommendation paper of July 1999
|
||||
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ltc_ecc_projective_dbl_point.c
|
||||
ECC Crypto, Tom St Denis
|
||||
*/
|
||||
|
||||
#if defined(LTC_MECC) && (!defined(LTC_MECC_ACCEL) || defined(LTM_DESC))
|
||||
|
||||
/**
|
||||
Double an ECC point
|
||||
@param P The point to double
|
||||
@param R [out] The destination of the double
|
||||
@param modulus The modulus of the field the ECC curve is in
|
||||
@param mp The "b" value from montgomery_setup()
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp)
|
||||
{
|
||||
void *t1, *t2;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(P != NULL);
|
||||
LTC_ARGCHK(R != NULL);
|
||||
LTC_ARGCHK(modulus != NULL);
|
||||
LTC_ARGCHK(mp != NULL);
|
||||
|
||||
if ((err = mp_init_multi(&t1, &t2, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (P != R) {
|
||||
if ((err = mp_copy(P->x, R->x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(P->y, R->y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_copy(P->z, R->z)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
|
||||
/* t1 = Z * Z */
|
||||
if ((err = mp_sqr(R->z, t1)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* Z = Y * Z */
|
||||
if ((err = mp_mul(R->z, R->y, R->z)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(R->z, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* Z = 2Z */
|
||||
if ((err = mp_add(R->z, R->z, R->z)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp(R->z, modulus) != LTC_MP_LT) {
|
||||
if ((err = mp_sub(R->z, modulus, R->z)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
|
||||
/* T2 = X - T1 */
|
||||
if ((err = mp_sub(R->x, t1, t2)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp_d(t2, 0) == LTC_MP_LT) {
|
||||
if ((err = mp_add(t2, modulus, t2)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* T1 = X + T1 */
|
||||
if ((err = mp_add(t1, R->x, t1)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp(t1, modulus) != LTC_MP_LT) {
|
||||
if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* T2 = T1 * T2 */
|
||||
if ((err = mp_mul(t1, t2, t2)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* T1 = 2T2 */
|
||||
if ((err = mp_add(t2, t2, t1)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp(t1, modulus) != LTC_MP_LT) {
|
||||
if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* T1 = T1 + T2 */
|
||||
if ((err = mp_add(t1, t2, t1)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp(t1, modulus) != LTC_MP_LT) {
|
||||
if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
|
||||
/* Y = 2Y */
|
||||
if ((err = mp_add(R->y, R->y, R->y)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp(R->y, modulus) != LTC_MP_LT) {
|
||||
if ((err = mp_sub(R->y, modulus, R->y)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* Y = Y * Y */
|
||||
if ((err = mp_sqr(R->y, R->y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(R->y, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* T2 = Y * Y */
|
||||
if ((err = mp_sqr(R->y, t2)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* T2 = T2/2 */
|
||||
if (mp_isodd(t2)) {
|
||||
if ((err = mp_add(t2, modulus, t2)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
if ((err = mp_div_2(t2, t2)) != CRYPT_OK) { goto done; }
|
||||
/* Y = Y * X */
|
||||
if ((err = mp_mul(R->y, R->x, R->y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(R->y, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
|
||||
/* X = T1 * T1 */
|
||||
if ((err = mp_sqr(t1, R->x)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(R->x, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* X = X - Y */
|
||||
if ((err = mp_sub(R->x, R->y, R->x)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp_d(R->x, 0) == LTC_MP_LT) {
|
||||
if ((err = mp_add(R->x, modulus, R->x)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* X = X - Y */
|
||||
if ((err = mp_sub(R->x, R->y, R->x)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp_d(R->x, 0) == LTC_MP_LT) {
|
||||
if ((err = mp_add(R->x, modulus, R->x)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
|
||||
/* Y = Y - X */
|
||||
if ((err = mp_sub(R->y, R->x, R->y)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp_d(R->y, 0) == LTC_MP_LT) {
|
||||
if ((err = mp_add(R->y, modulus, R->y)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
/* Y = Y * T1 */
|
||||
if ((err = mp_mul(R->y, t1, R->y)) != CRYPT_OK) { goto done; }
|
||||
if ((err = mp_montgomery_reduce(R->y, modulus, mp)) != CRYPT_OK) { goto done; }
|
||||
/* Y = Y - T2 */
|
||||
if ((err = mp_sub(R->y, t2, R->y)) != CRYPT_OK) { goto done; }
|
||||
if (mp_cmp_d(R->y, 0) == LTC_MP_LT) {
|
||||
if ((err = mp_add(R->y, modulus, R->y)) != CRYPT_OK) { goto done; }
|
||||
}
|
||||
|
||||
err = CRYPT_OK;
|
||||
done:
|
||||
mp_clear_multi(t1, t2, NULL);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
||||
|
103
thirdparty/libtomcrypt/pk/katja/katja_decrypt_key.c
vendored
Normal file
103
thirdparty/libtomcrypt/pk/katja/katja_decrypt_key.c
vendored
Normal file
@ -0,0 +1,103 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file katja_decrypt_key.c
|
||||
Katja PKCS #1 OAEP Decryption, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MKAT
|
||||
|
||||
/**
|
||||
(PKCS #1 v2.0) decrypt then OAEP depad
|
||||
@param in The ciphertext
|
||||
@param inlen The length of the ciphertext (octets)
|
||||
@param out [out] The plaintext
|
||||
@param outlen [in/out] The max size and resulting size of the plaintext (octets)
|
||||
@param lparam The system "lparam" value
|
||||
@param lparamlen The length of the lparam value (octets)
|
||||
@param hash_idx The index of the hash desired
|
||||
@param stat [out] Result of the decryption, 1==valid, 0==invalid
|
||||
@param key The corresponding private Katja key
|
||||
@return CRYPT_OK if succcessul (even if invalid)
|
||||
*/
|
||||
int katja_decrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *lparam, unsigned long lparamlen,
|
||||
int hash_idx, int *stat,
|
||||
katja_key *key)
|
||||
{
|
||||
unsigned long modulus_bitlen, modulus_bytelen, x;
|
||||
int err;
|
||||
unsigned char *tmp;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(stat != NULL);
|
||||
|
||||
/* default to invalid */
|
||||
*stat = 0;
|
||||
|
||||
/* valid hash ? */
|
||||
if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* get modulus len in bits */
|
||||
modulus_bitlen = mp_count_bits( (key->N));
|
||||
|
||||
/* payload is upto pq, so we know q is 1/3rd the size of N and therefore pq is 2/3th the size */
|
||||
modulus_bitlen = ((modulus_bitlen << 1) / 3);
|
||||
|
||||
/* round down to next byte */
|
||||
modulus_bitlen -= (modulus_bitlen & 7) + 8;
|
||||
|
||||
/* outlen must be at least the size of the modulus */
|
||||
modulus_bytelen = mp_unsigned_bin_size( (key->N));
|
||||
if (modulus_bytelen != inlen) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* allocate ram */
|
||||
tmp = XMALLOC(inlen);
|
||||
if (tmp == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* rsa decode the packet */
|
||||
x = inlen;
|
||||
if ((err = katja_exptmod(in, inlen, tmp, &x, PK_PRIVATE, key)) != CRYPT_OK) {
|
||||
XFREE(tmp);
|
||||
return err;
|
||||
}
|
||||
|
||||
/* shift right by modulus_bytelen - modulus_bitlen/8 bytes */
|
||||
for (x = 0; x < (modulus_bitlen >> 3); x++) {
|
||||
tmp[x] = tmp[x+(modulus_bytelen-(modulus_bitlen>>3))];
|
||||
}
|
||||
|
||||
/* now OAEP decode the packet */
|
||||
err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx,
|
||||
out, outlen, stat);
|
||||
|
||||
XFREE(tmp);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif /* LTC_MRSA */
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
85
thirdparty/libtomcrypt/pk/katja/katja_encrypt_key.c
vendored
Normal file
85
thirdparty/libtomcrypt/pk/katja/katja_encrypt_key.c
vendored
Normal file
@ -0,0 +1,85 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file katja_encrypt_key.c
|
||||
Katja PKCS-style OAEP encryption, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MKAT
|
||||
|
||||
/**
|
||||
(PKCS #1 v2.0) OAEP pad then encrypt
|
||||
@param in The plaintext
|
||||
@param inlen The length of the plaintext (octets)
|
||||
@param out [out] The ciphertext
|
||||
@param outlen [in/out] The max size and resulting size of the ciphertext
|
||||
@param lparam The system "lparam" for the encryption
|
||||
@param lparamlen The length of lparam (octets)
|
||||
@param prng An active PRNG
|
||||
@param prng_idx The index of the desired prng
|
||||
@param hash_idx The index of the desired hash
|
||||
@param key The Katja key to encrypt to
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int katja_encrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *lparam, unsigned long lparamlen,
|
||||
prng_state *prng, int prng_idx, int hash_idx, katja_key *key)
|
||||
{
|
||||
unsigned long modulus_bitlen, modulus_bytelen, x;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
/* valid prng and hash ? */
|
||||
if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* get modulus len in bits */
|
||||
modulus_bitlen = mp_count_bits((key->N));
|
||||
|
||||
/* payload is upto pq, so we know q is 1/3rd the size of N and therefore pq is 2/3th the size */
|
||||
modulus_bitlen = ((modulus_bitlen << 1) / 3);
|
||||
|
||||
/* round down to next byte */
|
||||
modulus_bitlen -= (modulus_bitlen & 7) + 8;
|
||||
|
||||
/* outlen must be at least the size of the modulus */
|
||||
modulus_bytelen = mp_unsigned_bin_size((key->N));
|
||||
if (modulus_bytelen > *outlen) {
|
||||
*outlen = modulus_bytelen;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* OAEP pad the key */
|
||||
x = *outlen;
|
||||
if ((err = pkcs_1_oaep_encode(in, inlen, lparam,
|
||||
lparamlen, modulus_bitlen, prng, prng_idx, hash_idx,
|
||||
out, &x)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* Katja exptmod the OAEP pad */
|
||||
return katja_exptmod(out, x, out, outlen, PK_PUBLIC, key);
|
||||
}
|
||||
|
||||
#endif /* LTC_MRSA */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
73
thirdparty/libtomcrypt/pk/katja/katja_export.c
vendored
Normal file
73
thirdparty/libtomcrypt/pk/katja/katja_export.c
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file katja_export.c
|
||||
Export Katja PKCS-style keys, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_MKAT
|
||||
|
||||
/**
|
||||
This will export either an KatjaPublicKey or KatjaPrivateKey
|
||||
@param out [out] Destination of the packet
|
||||
@param outlen [in/out] The max size and resulting size of the packet
|
||||
@param type The type of exported key (PK_PRIVATE or PK_PUBLIC)
|
||||
@param key The Katja key to export
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int katja_export(unsigned char *out, unsigned long *outlen, int type, katja_key *key)
|
||||
{
|
||||
int err;
|
||||
unsigned long zero=0;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
|
||||
/* type valid? */
|
||||
if (!(key->type == PK_PRIVATE) && (type == PK_PRIVATE)) {
|
||||
return CRYPT_PK_INVALID_TYPE;
|
||||
}
|
||||
|
||||
if (type == PK_PRIVATE) {
|
||||
/* private key */
|
||||
/* output is
|
||||
Version, n, d, p, q, d mod (p-1), d mod (q - 1), 1/q mod p, pq
|
||||
*/
|
||||
if ((err = der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_SHORT_INTEGER, 1UL, &zero,
|
||||
LTC_ASN1_INTEGER, 1UL, key->N,
|
||||
LTC_ASN1_INTEGER, 1UL, key->d,
|
||||
LTC_ASN1_INTEGER, 1UL, key->p,
|
||||
LTC_ASN1_INTEGER, 1UL, key->q,
|
||||
LTC_ASN1_INTEGER, 1UL, key->dP,
|
||||
LTC_ASN1_INTEGER, 1UL, key->dQ,
|
||||
LTC_ASN1_INTEGER, 1UL, key->qP,
|
||||
LTC_ASN1_INTEGER, 1UL, key->pq,
|
||||
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* clear zero and return */
|
||||
return CRYPT_OK;
|
||||
} else {
|
||||
/* public key */
|
||||
return der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_INTEGER, 1UL, key->N,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* LTC_MRSA */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user