8#if !defined(MBEDTLS_CONFIG_FILE)
11#include MBEDTLS_CONFIG_FILE
14#if defined(MBEDTLS_DSA_C)
22#if defined(MBEDTLS_PLATFORM_C)
27#define mbedtls_printf printf
28#define mbedtls_calloc calloc
29#define mbedtls_free free
32#if !defined(MBEDTLS_DSA_ALT)
34#define DSA_VALIDATE_RET( cond ) \
35 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_DSA_BAD_INPUT_DATA )
36#define DSA_VALIDATE( cond ) \
37 MBEDTLS_INTERNAL_VALIDATE( cond )
77 DSA_VALIDATE_RET(
ctx !=
NULL );
78 DSA_VALIDATE_RET(
P !=
NULL );
79 DSA_VALIDATE_RET( Q !=
NULL );
80 DSA_VALIDATE_RET(
G !=
NULL );
112 DSA_VALIDATE_RET(
a !=
NULL );
125 int (*f_rng)(
void *,
unsigned char *,
size_t),
void *p_rng )
128 unsigned long L,
N,
n, outbytes, seedbytes,
counter,
j,
i;
129 int mr_tests_q, mr_tests_p, found_p, found_q;
130 mbedtls_mpi t2L1, t2N1, t2seedlen,
U, U1,
W, W1,
X,
q, q1,
p, p1,
e,
h, h1,
g,
c, t2q, seedinc, seedinc1;
134 DSA_VALIDATE_RET(
ctx !=
NULL );
135 DSA_VALIDATE_RET( f_rng !=
NULL );
139 DSA_VALIDATE_RET( group_size >= 1 );
140 DSA_VALIDATE_RET( group_size < modulus_size );
143#if !defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA256_C)
147#if defined(MBEDTLS_SHA512_C)
153 seedbytes = group_size;
154 L = modulus_size * 8;
159 n = ((
L + outbytes*8 - 1) / (outbytes*8)) - 1;
186 if (
L <= 1024) { mr_tests_p = 40; }
187 else if (
L <= 2048) { mr_tests_p = 56; }
188 else { mr_tests_p = 64; }
190 if (
N <= 160) { mr_tests_q = 40; }
191 else if (
N <= 224) { mr_tests_q = 56; }
192 else { mr_tests_q = 64; }
233 for(found_p=0; !found_p;) {
235 for(found_q=0; !found_q;) {
236 f_rng( p_rng, sbuf, seedbytes );
253 for(
j=0;
j<=
n;
j++) {
353 DSA_VALIDATE_RET(
P !=
NULL );
354 DSA_VALIDATE_RET( Q !=
NULL );
355 DSA_VALIDATE_RET(
G !=
NULL );
417 DSA_VALIDATE_RET(
ctx !=
NULL );
448 DSA_VALIDATE_RET(
ctx !=
NULL );
478 DSA_VALIDATE_RET(
ctx !=
NULL );
498 unsigned char *sig,
size_t *slen )
502 unsigned char *
p =
buf +
sizeof(
buf );
522 const unsigned char *
hash,
size_t hlen,
523 unsigned char *sig,
size_t *slen,
524 int (*f_rng)(
void *,
unsigned char *,
size_t),
529 size_t hashlen, qlen;
531 DSA_VALIDATE_RET(
ctx !=
NULL );
533 DSA_VALIDATE_RET( sig !=
NULL );
534 DSA_VALIDATE_RET( slen !=
NULL );
535 DSA_VALIDATE_RET( f_rng !=
NULL );
551 if (hashlen > qlen) hashlen = qlen;
603 const unsigned char *
hash,
size_t hlen,
604 const unsigned char *sig,
size_t slen )
607 unsigned char *
p = (
unsigned char *) sig;
608 const unsigned char *
end = sig + slen;
609 size_t len, hashlen, qlen;
610 mbedtls_mpi r,
s,
h,
w, hw,
rw,
u1,
u2, gu1, yu2, u12, u12p,
v, _rr;
612 DSA_VALIDATE_RET(
ctx !=
NULL );
614 DSA_VALIDATE_RET( sig !=
NULL );
661 if (hashlen > qlen) hashlen = qlen;
710#if defined(MBEDTLS_SELF_TEST)
712static const char *dsa_test_p_hex =
"F7E75FDC469067FFDC4E847C51F452DF27303F51D8F2E3E444924563740179E1";
713static const char *dsa_test_q_hex =
"87E85B349454331564214218435B420E53368DB1";
714static const char *dsa_test_g_hex =
"F7E75FDC469067FFDC4E847C51F452DF27303F51D8F2E3E444924563740179E1";
715static const char *dsa_test_x_hex =
"20B4822143298349213489213498213498213948";
716static const char *dsa_test_y_hex =
"154285095685091850198501985091850918509185091850918509185091850918509185091850918509185091850918509185091850918509185091850918509185";
717static const char *dsa_test_hash_hex =
"A9993E364706816ABA3E25717850C26C9CD0D89D";
722int mbedtls_dsa_self_test(
int verbose )
728 unsigned char hash[20];
766 if( (
ret = mbedtls_dsa_read_signature( &dsa,
hash, 20, sig, slen ) ) != 0 )
ASN.1 buffer writing functionality.
int mbedtls_asn1_write_tag(unsigned char **p, unsigned char *start, unsigned char tag)
Write an ASN.1 tag in ASN.1 format.
#define MBEDTLS_ASN1_CHK_ADD(g, f)
int mbedtls_asn1_write_len(unsigned char **p, unsigned char *start, size_t len)
Write a length field in ASN.1 format.
int mbedtls_asn1_write_mpi(unsigned char **p, unsigned char *start, const mbedtls_mpi *X)
Write a arbitrary-precision number (MBEDTLS_ASN1_INTEGER) in ASN.1 format.
int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Import an MPI from an ASCII string.
int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a signed subtraction of MPIs: X = A - B.
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Perform a signed subtraction of an MPI and an integer: X = A - b.
int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Perform a signed addition of an MPI and an integer: X = A + b.
int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Miller-Rabin primality test.
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Make a copy of an MPI.
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Modify a specific bit in an MPI.
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Return the total size of an MPI value in bytes.
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR)
Perform a sliding-window exponentiation: X = A^E mod N.
int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a division with remainder of two MPIs: A = Q * B + R.
int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a signed addition of MPIs: X = A + B.
int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Store integer value in MPI.
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Import an MPI from unsigned big endian binary data.
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare two MPIs.
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a modular reduction. R = A mod B.
int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Fill an MPI with a number of random bytes.
void mbedtls_mpi_init(mbedtls_mpi *X)
Initialize an MPI context.
#define MBEDTLS_ERR_MPI_ALLOC_FAILED
int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a multiplication of two MPIs: X = A * B.
#define MBEDTLS_MPI_CHK(f)
int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Compute the modular inverse: X = A^-1 mod N.
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Get a specific bit from an MPI.
void mbedtls_mpi_free(mbedtls_mpi *X)
This function frees the components of an MPI context.
int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
Export an MPI into unsigned big endian binary data of fixed size.
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Compare an MPI with an integer.
int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Compute the greatest common divisor: G = gcd(A, B)
#define G(r, i, a, b, c, d)
static void cleanup(void)
int mbedtls_dsa_set_group(mbedtls_dsa_context *ctx, const mbedtls_mpi *P, const mbedtls_mpi *Q, const mbedtls_mpi *G)
This function sets the DSA group parameters.
int mbedtls_dsa_pubkey_from_privkey(mbedtls_dsa_context *ctx)
This function derived public key from private key.
int mbedtls_dsa_verify(mbedtls_dsa_context *ctx, const unsigned char *hash, size_t hlen, const unsigned char *sig, size_t slen)
This function reads and verifies a DSA signature.
int mbedtls_dsa_check_pubkey(const mbedtls_dsa_context *ctx)
This function checks if the public key is valid.
int mbedtls_dsa_sign(mbedtls_dsa_context *ctx, const unsigned char *hash, size_t hlen, unsigned char *sig, size_t *slen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function computes the DSA signature of a message hash.
#define MBEDTLS_DSA_ASN_SIGNATURE_MAX_LEN
void mbedtls_dsa_init(mbedtls_dsa_context *ctx)
This function initializes a DSA context.
#define MBEDTLS_DSA_MAX_GROUP
int mbedtls_dsa_check_privkey(const mbedtls_dsa_context *ctx)
This function checks if the private key is valid.
int mbedtls_dsa_genkey(mbedtls_dsa_context *ctx, size_t group_size, size_t modulus_size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates a DSA keypair.
#define MBEDTLS_ERR_DSA_VERIFY_FAILED
int mbedtls_dsa_check_pqg(const mbedtls_mpi *P, const mbedtls_mpi *Q, const mbedtls_mpi *G)
This function checks if the DSA group parameters are valid.
void mbedtls_dsa_free(mbedtls_dsa_context *ctx)
This function frees a DSA context.
#define MBEDTLS_ERR_DSA_BAD_INPUT_DATA
#define MBEDTLS_DSA_MAX_MODULUS
GLdouble GLdouble GLdouble r
GLdouble GLdouble GLdouble GLdouble q
GLenum GLuint GLenum GLsizei const GLchar * buf
GLboolean GLboolean GLboolean GLboolean a
GLubyte GLubyte GLubyte GLubyte w
GLfloat GLfloat GLfloat GLfloat h
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
int mbedtls_asn1_get_mpi(unsigned char **p, const unsigned char *end, mbedtls_mpi *X)
Retrieve a MPI value from an integer ASN.1 tag. Updates the pointer to immediately behind the full ta...
#define MBEDTLS_ASN1_SEQUENCE
#define MBEDTLS_ASN1_CONSTRUCTED
int mbedtls_asn1_get_tag(unsigned char **p, const unsigned char *end, size_t *len, int tag)
Get the tag and length of the tag. Check for the requested tag. Updates the pointer to immediately be...
int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
This function calculates the message-digest of a buffer, with respect to a configurable message-diges...
unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info)
This function extracts the message-digest size from the message-digest information structure.
#define memcpy(s1, s2, n)
#define mbedtls_md_info_from_type
Configuration options (set of defines)