File: asn12ieeep1336.c

package info (click to toggle)
libnginx-mod-js 0.8.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,808 kB
  • sloc: ansic: 114,096; perl: 8,447; javascript: 2,520; exp: 487; sh: 322; xml: 312; python: 181; makefile: 18
file content (49 lines) | stat: -rw-r--r-- 1,145 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <openssl/ecdsa.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char * argv[]) {
    int                  rbytes, sbytes, len, n;
    ECDSA_SIG            *ecSig;
    unsigned char        *p, *end;
    const unsigned char  *start;
    unsigned char        der[512];
    unsigned char        out[64];

    p = der;
    end = &der[sizeof(der)];

    for ( ;; ) {
        n = read(STDIN_FILENO, der, end - p);

        if (n == 0) {
            break;
        }

        if ((end - p) == 0) {
            printf("too large (> 512) der length in stdin");
            return EXIT_FAILURE;
        }

        p += n;
    }

    start = der;
    ecSig = d2i_ECDSA_SIG(NULL, &start, p - der);
    if (ecSig == NULL) {
        printf("d2i_ECDSA_SIG() failed");
        return EXIT_FAILURE;
    }

    rbytes = BN_num_bytes(ECDSA_SIG_get0_r(ecSig));
    sbytes = BN_num_bytes(ECDSA_SIG_get0_s(ecSig));

    BN_bn2binpad(ECDSA_SIG_get0_r(ecSig), out, rbytes);
    BN_bn2binpad(ECDSA_SIG_get0_s(ecSig), &out[32], sbytes);

    write(STDOUT_FILENO, out, sizeof(out));

    return EXIT_SUCCESS;
}