Changeset View
Changeset View
Standalone View
Standalone View
g10/build-packet.c
Context not available. | |||||
/* | /* | ||||
* Write an opaque MPI to the output stream without length info. | * Write the mpi A to the output stream OUT as "SOS" (Strange Octet | ||||
* String). If R_NWRITTEN is not NULL the number of bytes written is | |||||
* stored there. To only get the number of bytes which would be | |||||
* written, NULL may be passed for OUT. | |||||
*/ | |||||
static gpg_error_t | |||||
sos_write (iobuf_t out, gcry_mpi_t a, unsigned int *r_nwritten) | |||||
{ | |||||
gpg_error_t err; | |||||
unsigned int nwritten = 0; | |||||
if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE)) | |||||
{ | |||||
unsigned int nbits; | |||||
const unsigned char *p; | |||||
unsigned char lenhdr[2]; | |||||
/* gcry_log_debugmpi ("a", a); */ | |||||
p = gcry_mpi_get_opaque (a, &nbits); | |||||
/* gcry_log_debug (" [%u bit]\n", nbits); */ | |||||
/* gcry_log_debughex (" ", p, (nbits+7)/8); */ | |||||
if (p && *p) | |||||
{ | |||||
nbits = ((nbits + 7) / 8) * 8; | |||||
if (nbits >= 8 && !(*p & 0x80)) | |||||
if (--nbits >= 7 && !(*p & 0x40)) | |||||
if (--nbits >= 6 && !(*p & 0x20)) | |||||
if (--nbits >= 5 && !(*p & 0x10)) | |||||
if (--nbits >= 4 && !(*p & 0x08)) | |||||
if (--nbits >= 3 && !(*p & 0x04)) | |||||
if (--nbits >= 2 && !(*p & 0x02)) | |||||
if (--nbits >= 1 && !(*p & 0x01)) | |||||
--nbits; | |||||
} | |||||
lenhdr[0] = nbits >> 8; | |||||
lenhdr[1] = nbits; | |||||
err = out? iobuf_write (out, lenhdr, 2) : 0; | |||||
if (!err) | |||||
{ | |||||
nwritten += 2; | |||||
if (p) | |||||
{ | |||||
err = out? iobuf_write (out, p, (nbits+7)/8) : 0; | |||||
if (!err) | |||||
nwritten += (nbits+7)/8; | |||||
} | |||||
} | |||||
} | |||||
else | |||||
{ | |||||
log_info ("non-opaque MPI (%u bits) for SOS\n", gcry_mpi_get_nbits (a)); | |||||
err = gpg_error (GPG_ERR_INV_DATA); | |||||
} | |||||
if (r_nwritten) | |||||
*r_nwritten = nwritten; | |||||
return err; | |||||
} | |||||
/* | |||||
* Write an opaque string to the output stream without length info. | |||||
*/ | */ | ||||
gpg_error_t | gpg_error_t | ||||
gpg_mpi_write_nohdr (iobuf_t out, gcry_mpi_t a) | gpg_mpi_write_nohdr (iobuf_t out, gcry_mpi_t a) | ||||
Context not available. | |||||
|| (pk->pubkey_algo == PUBKEY_ALGO_EDDSA && (i == 0)) | || (pk->pubkey_algo == PUBKEY_ALGO_EDDSA && (i == 0)) | ||||
|| (pk->pubkey_algo == PUBKEY_ALGO_ECDH && (i == 0 || i == 2))) | || (pk->pubkey_algo == PUBKEY_ALGO_ECDH && (i == 0 || i == 2))) | ||||
err = gpg_mpi_write_nohdr (a, pk->pkey[i]); | err = gpg_mpi_write_nohdr (a, pk->pkey[i]); | ||||
else if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA | |||||
|| pk->pubkey_algo == PUBKEY_ALGO_EDDSA | |||||
|| pk->pubkey_algo == PUBKEY_ALGO_ECDH) | |||||
err = sos_write (a, pk->pkey[i], NULL); | |||||
else | else | ||||
err = gpg_mpi_write (a, pk->pkey[i], NULL); | err = gpg_mpi_write (a, pk->pkey[i], NULL); | ||||
if (err) | if (err) | ||||
Context not available. | |||||
for (j=i; j < nskey; j++ ) | for (j=i; j < nskey; j++ ) | ||||
{ | { | ||||
if ((err = gpg_mpi_write (NULL, pk->pkey[j], &n))) | if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA | ||||
goto leave; | || pk->pubkey_algo == PUBKEY_ALGO_EDDSA | ||||
|| pk->pubkey_algo == PUBKEY_ALGO_ECDH) | |||||
{ | |||||
if ((err = sos_write (NULL, pk->pkey[j], &n))) | |||||
goto leave; | |||||
} | |||||
else | |||||
{ | |||||
if ( (err = gpg_mpi_write (a, pk->pkey[i], NULL))) | |||||
goto leave; | |||||
} | |||||
skbytes += n; | skbytes += n; | ||||
} | } | ||||
Context not available. | |||||
} | } | ||||
for ( ; i < nskey; i++ ) | for ( ; i < nskey; i++ ) | ||||
if ( (err = gpg_mpi_write (a, pk->pkey[i], NULL))) | if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA | ||||
goto leave; | || pk->pubkey_algo == PUBKEY_ALGO_EDDSA | ||||
|| pk->pubkey_algo == PUBKEY_ALGO_ECDH) | |||||
{ | |||||
if ((err = sos_write (a, pk->pkey[i], NULL))) | |||||
goto leave; | |||||
} | |||||
else | |||||
if ((err = gpg_mpi_write (a, pk->pkey[i], NULL))) | |||||
goto leave; | |||||
write_16 (a, ski->csum ); | write_16 (a, ski->csum ); | ||||
} | } | ||||
Context not available. | |||||
{ | { | ||||
if (enc->pubkey_algo == PUBKEY_ALGO_ECDH && i == 1) | if (enc->pubkey_algo == PUBKEY_ALGO_ECDH && i == 1) | ||||
rc = gpg_mpi_write_nohdr (a, enc->data[i]); | rc = gpg_mpi_write_nohdr (a, enc->data[i]); | ||||
else if (enc->pubkey_algo == PUBKEY_ALGO_ECDH) | |||||
rc = sos_write (a, enc->data[i], NULL); | |||||
else | else | ||||
rc = gpg_mpi_write (a, enc->data[i], NULL); | rc = gpg_mpi_write (a, enc->data[i], NULL); | ||||
} | } | ||||
Context not available. | |||||
n = pubkey_get_nsig( sig->pubkey_algo ); | n = pubkey_get_nsig( sig->pubkey_algo ); | ||||
if ( !n ) | if ( !n ) | ||||
write_fake_data( a, sig->data[0] ); | write_fake_data( a, sig->data[0] ); | ||||
for (i=0; i < n && !rc ; i++ ) | if (sig->pubkey_algo == PUBKEY_ALGO_ECDSA | ||||
rc = gpg_mpi_write (a, sig->data[i], NULL); | || sig->pubkey_algo == PUBKEY_ALGO_EDDSA) | ||||
for (i=0; i < n && !rc ; i++ ) | |||||
rc = sos_write (a, sig->data[i], NULL); | |||||
else | |||||
for (i=0; i < n && !rc ; i++ ) | |||||
rc = gpg_mpi_write (a, sig->data[i], NULL); | |||||
if (!rc) | if (!rc) | ||||
{ | { | ||||
Context not available. |