CUDA sha256 与 OpenSSL 相比产生不同的散列

问题描述 投票:0回答:0

我正在尝试将我的 sha256 哈希函数从 CPU 代码移植到 CUDA。谷歌搜索后,我发现 cuda sha256 的工作示例很少。然而在测试时,cuda sha256 的哈希结果与 OpenSSL 不同。

我的输入是“hello world”,声明为 const char*。结果如下;

常量字符* 输入:hello world

CPU 上的哈希值:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

GPU 上的哈希值:c1114db6b517b4db8d360a9e14f5c2a57de95d955ec20cbd4cb73facb2b13e5f

我需要帮助来修复我的 sha256 GPU 代码,以便它产生与 CPU (OpenSSL) 给定的哈希值相同的哈希值。

这是我的 CPU 哈希代码

#pragma warning(disable : 4996)     //disable compiler error
#include <iostream>
#include <openssl/sha.h>

unsigned char hash[SHA256_DIGEST_LENGTH];

void SHA256(const char* input, size_t input_size){

    SHA256_CTX sha256;
    SHA256_Init(&sha256);
    SHA256_Update(&sha256, input, input_size);
    SHA256_Final(hash, &sha256);
    
}

void CPU() {

    const char* input = "hello world";
    size_t input_size = strlen(input);

    SHA256(input, input_size);

    for (size_t i = 0; i < 32; i++) {
        printf("%02x", hash[i]);
    }
    printf("\n");

}

这是我的 GPU 哈希代码

#include <stdio.h>
#include <string.h>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>

#define BLOCK_SIZE 256

__constant__ unsigned int k[64] = {
  0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};

__device__ unsigned int Ch(unsigned int x, unsigned int y, unsigned int z) {
    return (x & y) ^ (~x & z);
}

__device__ unsigned int Maj(unsigned int x, unsigned int y, unsigned int z) {
    return (x & y) ^ (x & z) ^ (y & z);
}

__device__ unsigned int Sigma0(unsigned int x) {
    return (x >> 2u) | (x << 30u);
}

__device__ unsigned int Sigma1(unsigned int x) {
    return (x >> 6u) | (x << 26u);
}

__device__ unsigned int sigma0(unsigned int x) {
    return (x >> 7u) | (x << 25u);
}

__device__ unsigned int sigma1(unsigned int x) {
    return (x >> 17u) | (x << 15u);
}

//solve using 256 thread in 1 block
__global__ void sha256_kernel(const char* input, size_t input_size, unsigned char* output) {
    size_t i = blockIdx.x * blockDim.x + threadIdx.x;
    size_t grid_size = blockDim.x * gridDim.x;

    for (; i < input_size; i += grid_size) {
        unsigned int h[8] = {
          0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
        };

        unsigned int w[64];
        for (size_t j = 0; j < input_size; j += 64) {
            for (size_t t = 0; t < 16; t++) {
                w[t] = ((unsigned int)input[j + t * 4 + 0] << 24u) | ((unsigned int)input[j + t * 4 + 1] << 16u) |
                    ((unsigned int)input[j + t * 4 + 2] << 8u) | ((unsigned int)input[j + t * 4 + 3] << 0u);
            }
            for (size_t t = 16; t < 64; t++) {
                w[t] = sigma1(w[t - 2]) + w[t - 7] + sigma0(w[t - 15]) + w[t - 16];
            }

            unsigned int a = h[0];
            unsigned int b = h[1];
            unsigned int c = h[2];
            unsigned int d = h[3];
            unsigned int e = h[4];
            unsigned int f = h[5];
            unsigned int g = h[6];
            unsigned int hh = h[7];

            for (size_t t = 0; t < 64; t++) {
                unsigned int t1 = hh + Sigma1(e) + Ch(e, f, g) + k[t] + w[t];
                unsigned int t2 = Sigma0(a) + Maj(a, b, c);
                hh = g;
                g = f;
                f = e;
                e = d + t1;
                d = c;
                c = b;
                b = a;
                a = t1 + t2;
            }

            h[0] += a;
            h[1] += b;
            h[2] += c;
            h[3] += d;
            h[4] += e;
            h[5] += f;
            h[6] += g;
            h[7] += hh;
        }

        for (size_t t = 0; t < 8; t++) {
            output[i + t * 4 + 0] = (unsigned char)(h[t] >> 24u);
            output[i + t * 4 + 1] = (unsigned char)(h[t] >> 16u);
            output[i + t * 4 + 2] = (unsigned char)(h[t] >> 8u);
            output[i + t * 4 + 3] = (unsigned char)(h[t] >> 0u);
        }
    }
}

void GPU() {
    const char* input = "hello world";
    size_t input_size = strlen(input);
    size_t output_size = 32;
    unsigned char* output;
    char* input_device;

    cudaMalloc((void**)&output, output_size);
    cudaMalloc((void**)&input_device, input_size);

    cudaMemcpy(input_device, input, input_size, cudaMemcpyHostToDevice);

    //solve using 256 thread and 1 block
    sha256_kernel << < ((input_size + BLOCK_SIZE - 1) / BLOCK_SIZE), BLOCK_SIZE >> > (input_device, input_size, output);

    unsigned char* output_host = (unsigned char*)malloc(output_size);
    cudaMemcpy(output_host, output, output_size, cudaMemcpyDeviceToHost);

    for (size_t i = 0; i < output_size; i++) {
        printf("%02x", output_host[i]);
}
printf("\n");

free(output_host);
cudaFree(output);
cudaFree(input_device);}

提前致谢。

hash cuda sha256
© www.soinside.com 2019 - 2024. All rights reserved.