/*
 *  Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the License); you may
 *  not use this file except in compliance with the License.
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 */


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/sm4_cl.h>
#include <gmssl/endian.h>
#include <gmssl/mem.h>
#include <gmssl/error.h>
#include "cl.h"


static const char *sm4_cl_src;


void sm4_cl_cleanup(SM4_CL_CTX *ctx)
{
	if (ctx) {
		if (ctx->mem_rk) clReleaseMemObject(ctx->mem_rk);
		if (ctx->mem_io) clReleaseMemObject(ctx->mem_io);
		if (ctx->kernel) clReleaseKernel(ctx->kernel);
		if (ctx->program) clReleaseProgram(ctx->program);
		if (ctx->queue) clReleaseCommandQueue(ctx->queue);
		if (ctx->context) clReleaseContext(ctx->context);
		gmssl_secure_clear(ctx, sizeof(*ctx));
	}
}

static void clPrintDeviceInfo(cl_device_id device)
{
	char deviceName[128];
	char vendorName[128];
	char deviceVersion[128];
	char driverVersion[128];
	char deviceProfile[128];
	cl_device_type deviceType;
	cl_uint vendorID;
	cl_ulong globalMemSize;
	cl_ulong localMemSize;
	size_t maxWorkGroupSize;
	cl_uint maxWorkItemDimensions;
	size_t maxWorkItemSizes[3];
	cl_uint maxComputeUnits;
	char openclCVersion[128];
	char extensions[4096];

	clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(deviceName), deviceName, NULL);
	clGetDeviceInfo(device, CL_DEVICE_VENDOR, sizeof(vendorName), vendorName, NULL);
	clGetDeviceInfo(device, CL_DEVICE_VERSION, sizeof(deviceVersion), deviceVersion, NULL);
	clGetDeviceInfo(device, CL_DRIVER_VERSION, sizeof(driverVersion), driverVersion, NULL);
	clGetDeviceInfo(device, CL_DEVICE_PROFILE, sizeof(deviceProfile), deviceProfile, NULL);
	clGetDeviceInfo(device, CL_DEVICE_TYPE, sizeof(deviceType), &deviceType, NULL);
	clGetDeviceInfo(device, CL_DEVICE_VENDOR_ID, sizeof(vendorID), &vendorID, NULL);
	clGetDeviceInfo(device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(globalMemSize), &globalMemSize, NULL);
	clGetDeviceInfo(device, CL_DEVICE_LOCAL_MEM_SIZE, sizeof(localMemSize), &localMemSize, NULL);
	clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(maxWorkGroupSize), &maxWorkGroupSize, NULL);
	clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(maxWorkItemDimensions), &maxWorkItemDimensions, NULL);
	clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(maxWorkItemSizes), maxWorkItemSizes, NULL);
	clGetDeviceInfo(device, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(maxComputeUnits), &maxComputeUnits, NULL);
	clGetDeviceInfo(device, CL_DEVICE_OPENCL_C_VERSION, sizeof(openclCVersion), openclCVersion, NULL);
	clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, sizeof(extensions), extensions, NULL);

	printf("clGetDeviceInfo\n");
	printf("  Device Name: %s\n", deviceName);
	printf("  Vendor: %s\n", vendorName);
	printf("  Device Version: %s\n", deviceVersion);
	printf("  Driver Version: %s\n", driverVersion);
	printf("  Device Profile: %s\n", deviceProfile);
	printf("  Device Type: %s\n",
		(deviceType == CL_DEVICE_TYPE_CPU) ? "CPU" :
		(deviceType == CL_DEVICE_TYPE_GPU) ? "GPU" :
		(deviceType == CL_DEVICE_TYPE_ACCELERATOR) ? "Accelerator" :
		(deviceType == CL_DEVICE_TYPE_DEFAULT) ? "Default" : "Unknown");
	printf("  Vendor ID: %u\n", vendorID);
	printf("  Global Memory Size: %llu bytes\n", globalMemSize);
	printf("  Local Memory Size: %llu bytes\n", localMemSize);
	printf("  Max Work Group Size: %zu\n", maxWorkGroupSize);
	printf("  Max Work Item Dimensions: %u\n", maxWorkItemDimensions);
	printf("  Max Work Item Sizes: (%zu, %zu, %zu)\n", maxWorkItemSizes[0], maxWorkItemSizes[1], maxWorkItemSizes[2]);
	printf("  Max Compute Units: %u\n", maxComputeUnits);
	printf("  OpenCL C Version: %s\n", openclCVersion);
	printf("  Extensions: %s\n", extensions);
}

static int sm4_cl_set_key(SM4_CL_CTX *ctx, const uint8_t key[16], int enc)
{
	cl_platform_id platform;
	cl_device_id device;
	cl_int err;
	char sval[256];
	size_t slen;
	cl_command_queue_properties queue_prop = 0;
	const char *build_opts = NULL;

	memset(ctx, 0, sizeof(*ctx));

	if (gmssl_cl_get_gpu_device(&platform, &device) != 1) {
		error_print();
		return -1;
	}
	//clPrintDeviceInfo(device);

	if (!(ctx->context = clCreateContext(NULL, 1, &device, NULL, NULL, &err))) {
		cl_error_print(err);
		return -1;
	}
	if (!(ctx->queue = clCreateCommandQueue(ctx->context, device, queue_prop, &err))) {
		cl_error_print(err);
		goto end;
	}
	if (!(ctx->program = clCreateProgramWithSource(ctx->context, 1, (const char **)&sm4_cl_src, NULL, &err))) {
		cl_error_print(err);
		goto end;
	}
	if ((err = clBuildProgram(ctx->program, 1, &device, build_opts, NULL, NULL)) != CL_SUCCESS) {
		char *log = NULL;
		size_t loglen;

		cl_error_print(err);

		if ((err = clGetProgramBuildInfo(ctx->program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &loglen)) != CL_SUCCESS) {
			cl_error_print(err);
			goto end;
		}
		if (!(log = (char *)malloc(loglen + 1))) {
			goto end;
		}
		if ((err = clGetProgramBuildInfo(ctx->program, device, CL_PROGRAM_BUILD_LOG, loglen, log, NULL)) != CL_SUCCESS) {
			cl_error_print(err);
			free(log);
			goto end;
		}
		log[loglen] = 0;
		fprintf(stderr, "%s %d: %s\n", __FILE__, __LINE__, log);
		free(log);
		goto end;
	}
	if (!(ctx->kernel = clCreateKernel(ctx->program, "sm4_ctr32_encrypt_blocks", &err))) {
		cl_error_print(err);
		goto end;
	}
	/*
	// Apple M2 the CL_KERNEL_WORK_GROUP_SIZE is 256, but the valid work_group_size is 32
	if ((err = clGetKernelWorkGroupInfo(ctx->kernel, device, CL_KERNEL_WORK_GROUP_SIZE,
		sizeof(ctx->workgroup_size), &ctx->workgroup_size, NULL)) != CL_SUCCESS) {
		cl_error_print(err);
		goto end;
	}
	*/

	if (enc) {
		sm4_set_encrypt_key((SM4_KEY *)ctx->rk, key);
	} else {
		sm4_set_decrypt_key((SM4_KEY *)ctx->rk, key);
	}

	if (!(ctx->mem_rk = clCreateBuffer(ctx->context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, sizeof(SM4_KEY), ctx->rk, &err))) {
		cl_error_print(err);
		goto end;
	}
	if ((err = clSetKernelArg(ctx->kernel, 0, sizeof(cl_mem), &ctx->mem_rk)) != CL_SUCCESS) {
		cl_error_print(err);
		goto end;
	}

	return 1;


end:
	sm4_cl_cleanup(ctx);
	return -1;
}

int sm4_cl_set_encrypt_key(SM4_CL_CTX *ctx, const uint8_t key[16])
{
	return sm4_cl_set_key(ctx, key, 1);
}

int sm4_cl_set_decrypt_key(SM4_CL_CTX *ctx, const uint8_t key[16])
{
	return sm4_cl_set_key(ctx, key, 0);
}

int sm4_cl_ctr32_encrypt_blocks(SM4_CL_CTX *ctx, uint8_t iv[16], const uint8_t *in, size_t nblocks, uint8_t *out)
{
	int ret = -1;
	cl_int err;
	uint32_t ctr[4];
	cl_mem mem_ctr = NULL;
	size_t inlen = SM4_BLOCK_SIZE * nblocks;
	cl_mem mem_buf = NULL;
	cl_uint dim = 1;
	size_t global_work_size = nblocks;
	size_t local_work_size = 32;

	if (global_work_size % local_work_size) {
		error_print();
		return -1;
	}

	ctr[0] = GETU32(iv);
	ctr[1] = GETU32(iv + 4);
	ctr[2] = GETU32(iv + 8);
	ctr[3] = GETU32(iv + 12);

	if (!(mem_ctr = clCreateBuffer(ctx->context, CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR, sizeof(ctr), ctr, &err))) {
		cl_error_print(err);
		return -1;
	}
	if ((err = clSetKernelArg(ctx->kernel, 1, sizeof(cl_mem), &mem_ctr)) != CL_SUCCESS) {
		cl_error_print(err);
		goto end;
	}

	if (out != in) {
		memcpy(out, in, inlen);
	}
	if (!(mem_buf = clCreateBuffer(ctx->context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, inlen, out, &err))) {
		cl_error_print(err);
		return -1;
	}
	if ((err = clSetKernelArg(ctx->kernel, 2, sizeof(cl_mem), &mem_buf)) != CL_SUCCESS) {
		cl_error_print(err);
		goto end;
	}

	// on Apple M2, CL_KERNEL_WORK_GROUP_SIZE = 256
	// but kernel will fail when local_work_size > 32.
	// local_work_size might be restricted by the resources the kernel used.
	if ((err = clEnqueueNDRangeKernel(ctx->queue, ctx->kernel,
		dim, NULL, &global_work_size, &local_work_size, 0, NULL, NULL)) != CL_SUCCESS) {
		cl_error_print(err);
		goto end;
	}
	if (!clEnqueueMapBuffer(ctx->queue, mem_buf, CL_TRUE, 0, 0, inlen, 0, NULL, NULL, &err)) {
		cl_error_print(err);
		goto end;
	}

	ctr[3] += (uint32_t)nblocks;
	PUTU32(iv + 12, ctr[3]);
	ret = 1;
end:
	if (mem_ctr) clReleaseMemObject(mem_ctr);
	if (mem_buf) clReleaseMemObject(mem_buf);
	return ret;
}

static const char *sm4_cl_src = KERNEL(

__constant unsigned char SBOX[256] = {
	0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7, 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05,
	0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3, 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99,
	0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a, 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62,
	0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95, 0x80, 0xdf, 0x94, 0xfa, 0x75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7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20, 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48,
};

__kernel void sm4_ctr32_encrypt_blocks(__global const unsigned int *rkey, __global const unsigned int *ctr, __global unsigned char *data)
{
	unsigned int x0, x1, x2, x3, x4, i, t;
	uint global_id = get_global_id(0);
	__global unsigned int *out = (__global unsigned int *)(data + 16 * global_id);

#if 0
	// use local mem is little slower on Apple M2
	__local unsigned char S[256];
	__local unsigned int rk[32];

	if (get_local_id(0) == 0) {
		for (i = 0; i < 256; i++) {
			S[i] = SBOX[i];
		}
		for (i = 0; i < 32; i++) {
			rk[i] = rkey[i];
		}
	}
#else
	__constant unsigned char *S = SBOX;
	__global const unsigned int *rk = rkey;
#endif

	x0 = ctr[0];
	x1 = ctr[1];
	x2 = ctr[2];
	x3 = ctr[3] + global_id;

	for (i = 0; i < 31; i++) {
		x4 = x1 ^ x2 ^ x3 ^ rk[i];
		x4 = (S[x4 >> 24] << 24) ^ (S[(x4 >> 16) & 0xff] << 16) ^ (S[(x4 >> 8) & 0xff] <<  8) ^ S[x4 & 0xff];

		x4 = x0 ^ (x4 ^
			((x4 <<  2) | (x4 >> (32 -  2))) ^
			((x4 << 10) | (x4 >> (32 - 10))) ^
			((x4 << 18) | (x4 >> (32 - 18))) ^
			((x4 << 24) | (x4 >> (32 - 24))));

		t = x0;
		x0 = x1;
		x1 = x2;
		x2 = x3;
		x3 = x4;
		x4 = t;
	}
	x4 = x1 ^ x2 ^ x3 ^ rk[i];
	x4 = (S[x4 >> 24] << 24) ^ (S[(x4 >> 16) & 0xff] << 16) ^ (S[(x4 >> 8) & 0xff] <<  8) ^ S[x4 & 0xff];

	x4 = x0 ^ (x4 ^
		((x4 <<  2) | (x4 >> (32 -  2))) ^
		((x4 << 10) | (x4 >> (32 - 10))) ^
		((x4 << 18) | (x4 >> (32 - 18))) ^
		((x4 << 24) | (x4 >> (32 - 24))));

	out[0] ^= (x4 >> 24) | ((x4 >> 8) & 0xff00) | ((x4 << 8) & 0xff0000) | (x4 << 24);
	out[1] ^= (x3 >> 24) | ((x3 >> 8) & 0xff00) | ((x3 << 8) & 0xff0000) | (x3 << 24);
	out[2] ^= (x2 >> 24) | ((x2 >> 8) & 0xff00) | ((x2 << 8) & 0xff0000) | (x2 << 24);
	out[3] ^= (x1 >> 24) | ((x1 >> 8) & 0xff00) | ((x1 << 8) & 0xff0000) | (x1 << 24);
}

);
