Tengo una firma ECDSA sin procesar: valores R y S. Necesito una versión codificada en DER de la firma. ¿Hay una forma sencilla de hacer esto en openssl usando la interfaz c?
Mi intento actual es usar i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp) para completar un ECDSA_SIG* . La llamada devuelve un valor distinto de cero, pero el búfer de destino no parece haber cambiado.
Inicialmente estoy llenando mi ECDSA_SIG con los valores r y s . No veo ningún error. La página de manual dice que r y s deben asignarse cuando llamo a ECDSA_SIG_new
ECDSA_SIG* ec_sig = ECDSA_SIG_new(); if (NULL == BN_bin2bn(sig, 32, (ec_sig->r))) { dumpOpenSslErrors(); } DBG("post r :%s\n", BN_bn2hex(ec_sig->r)); if (NULL == BN_bin2bn(sig + 32, 32, (ec_sig->s))) { dumpOpenSslErrors(); } DBG("post s :%s\n", BN_bn2hex(ec_sig->s));S y R ahora están configurados:
post r :397116930C282D1FCB71166A2D06728120CF2EE5CF6CCD4E2D822E8E0AE24A30 post s :9E997D4718A7603942834FBDD22A4B856FC4083704EDE62033CF1A77CB9822A9
ahora para hacer la firma codificada.
int sig_size = i2d_ECDSA_SIG(ec_sig, NULL); if (sig_size > 255) { DBG("signature is too large wants %d\n", sig_size); } DBG("post i2d:%s\n", BN_bn2hex(ec_sig->s));s no ha cambiado:
post i2d:9E997D4718A7603942834FBDD22A4B856FC4083704EDE62033CF1A77CB9822A9
En este punto, tengo más que suficientes bytes listos y configuré el objetivo en 6 para que sea fácil ver qué cambia.
unsigned char* sig_bytes = new unsigned char[256]; memset(sig_bytes, 6, 256); sig_size = i2d_ECDSA_SIG(ec_sig, (&sig_bytes)); DBG("New size %d\n", sig_size); DBG("post i2d:%s\n", BN_bn2hex(ec_sig->s)); hexDump("Sig ", (const byte*)sig_bytes, sig_size); La nueva talla es la 71 La nueva talla es New size 71 y sigue siendo la misma:
`post i2d:9E997D4718A7603942834FBDD22A4B856FC4083704EDE62033CF1A77CB9822A9`El volcado hexadecimal es todo 6s.
--Sig -- 0x06: 0x06: 0x06: 0x06: 0x06: 0x06: 0x06: 0x06: 0x06: ...El volcado sigue siendo todos los 6, aunque la llamada no devolvió 0. ¿Qué me estoy perdiendo al vincular a DER codificar esta firma sin procesar?
i2d_ECDSA_SIG modifica su segundo argumento aumentándolo en el tamaño de la firma. De ecdsa.h:
/** DER encode content of ECDSA_SIG object (note: this function modifies *pp * (*pp += length of the DER encoded signature)). * \param sig pointer to the ECDSA_SIG object * \param pp pointer to a unsigned char pointer for the output or NULL * \return the length of the DER encoded ECDSA_SIG object or 0 */ int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp); Por lo tanto, debe realizar un seguimiento del valor original de sig_bytes cuando llama a i2d_ECDSA_SIG :
int sig_size = i2d_ECDSA_SIG(ec_sig, NULL); unsigned char *sig_bytes = malloc(sig_size); unsigned char *p; memset(sig_bytes, 6, sig_size); p = sig_bytes; new_sig_size = i2d_ECDSA_SIG(_sig, &p); // The value of p is now sig_bytes + sig_size, and the signature resides at sig_bytesProducción:
30 45 02 20 39 71 16 93 0C 28 2D 1F CB 71 16 6A 2D 06 72 81 20 CF 2E E5 CF 6C CD 4E 2D 82 2E 8E 0A E2 4A 30 02 21 00 9E 99 7D 47 18 A7 60 39 42 83 4F BD D2 2A 4B 85 6F C4 08 37 04 ED E6 20 33 CF 1A 77 CB 98 22 A9