int
crypto_sign_detached(unsigned char *sig, unsigned long long *siglen_p,
                     const unsigned char *m, unsigned long long mlen,
                     const unsigned char *sk)
{
    return crypto_sign_ed25519_detached(sig, siglen_p, m, mlen, sk);
}
Exemple #2
0
int
crypto_sign_ed25519(unsigned char *sm, unsigned long long *smlen_p,
                    const unsigned char *m, unsigned long long mlen,
                    const unsigned char *sk)
{
    unsigned long long siglen;

    memmove(sm + crypto_sign_ed25519_BYTES, m, mlen);
    /* LCOV_EXCL_START */
    if (crypto_sign_ed25519_detached(
            sm, &siglen, sm + crypto_sign_ed25519_BYTES, mlen, sk) != 0 ||
        siglen != crypto_sign_ed25519_BYTES) {
        if (smlen_p != NULL) {
            *smlen_p = 0;
        }
        memset(sm, 0, mlen + crypto_sign_ed25519_BYTES);
        return -1;
    }
    /* LCOV_EXCL_STOP */

    if (smlen_p != NULL) {
        *smlen_p = mlen + siglen;
    }
    return 0;
}
int sign_file(char *secret_key_name, char *file_name, char *out_file_name) {
    int error = 0;
    unsigned char secret_key[crypto_sign_ed25519_SECRETKEYBYTES];
    unsigned long long signature_final_size;
    unsigned char signature[crypto_sign_ed25519_BYTES];
    size_t file_size;
    size_t read;
    size_t written;
    FILE *secret_key_file = NULL;
    FILE *target_file = NULL;
    FILE *out_file = NULL;
    unsigned char *file_contents = NULL;

    secret_key_file = fopen(secret_key_name, "r");
    if (secret_key_file == NULL) {
        perror("opening secret key file");
        error = -1;
        goto out;
    }

    target_file = fopen(file_name, "r");
    if (target_file == NULL) {
        perror("opening target file");
        error = -1;
        goto out;
    }

    out_file = fopen(out_file_name, "w");
    if (out_file == NULL) {
        perror("opening output file");
        error = -1;
        goto out;
    }

    error = get_file_size(target_file, &file_size);
    if (error != 0) {
        perror("Getting file size");
        goto out;
    }

    file_contents = malloc(file_size);
    if (file_contents == NULL) {
        fprintf(stderr, "Could not allocate memory\n");
        error = -1;
        goto out;
    }

    read = fread(secret_key, sizeof(secret_key), 1, secret_key_file);
    if (read != 1) {
        fprintf(stderr, "Invalid secret key file\n");
        error = -1;
        goto out;
    } else if (ferror(secret_key_file) != 0) {
        perror("reading secret key file");
        error = -1;
        goto out;
    }

    read = fread(file_contents, file_size, 1, target_file);
    if (read != 1 || ferror(target_file) != 0) {
        perror("Reading target file");
        error = -1;
        goto out;
    }

    written = fwrite(file_contents, file_size, 1, out_file);
    if (ferror(out_file) != 0) {
        perror("Writing out file");
        error = -1;
        goto out;
    }

    error = crypto_sign_ed25519_detached(signature, &signature_final_size,
                                         file_contents, file_size, secret_key);
    if (error != 0) {
        fprintf(stderr, "Signature\n");
        error = -1;
        goto out;
    }

    written = fwrite(SIGNATURE_MAGIC_STRING, sizeof(SIGNATURE_MAGIC_STRING), 1, out_file);
    if (written != 1 || ferror(out_file) != 0) {
        perror("writing magic string to out file");
        error = -1;
        goto out;
    }

    written = fwrite(&signature, sizeof(signature), 1, out_file);
    if (written != 1 || ferror(out_file) != 0) {
        perror("writing out file");
        error = -1;
        goto out;
    }

#if DEBUG
    printf("Secret key:\n");
    print_bytes(secret_key, sizeof(secret_key));
    printf("Signature:\n");
    print_bytes(signature, sizeof(signature));
#endif

out:
    fclose(target_file);
    fclose(secret_key_file);
    fclose(out_file);
    free(file_contents);
    return error;
}