forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpal_hmac.c
More file actions
164 lines (133 loc) · 5 KB
/
pal_hmac.c
File metadata and controls
164 lines (133 loc) · 5 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "pal_evp.h"
#include "pal_hmac.h"
jobject CryptoNative_HmacCreate(uint8_t* key, int32_t keyLen, intptr_t type)
{
if (key != NULL && keyLen < 0) {
return NULL;
}
// Mac mac = Mac.getInstance(algName);
// SecretKeySpec key = new SecretKeySpec(key, algName);
// mac.init(key);
JNIEnv* env = GetJNIEnv();
jstring macName = NULL;
if (type == CryptoNative_EvpSha1())
macName = make_java_string(env, "HmacSHA1");
else if (type == CryptoNative_EvpSha256())
macName = make_java_string(env, "HmacSHA256");
else if (type == CryptoNative_EvpSha384())
macName = make_java_string(env, "HmacSHA384");
else if (type == CryptoNative_EvpSha512())
macName = make_java_string(env, "HmacSHA512");
else if (type == CryptoNative_EvpMd5())
macName = make_java_string(env, "HmacMD5");
else
return FAIL;
jbyteArray keyBytes;
if (key && keyLen > 0)
{
keyBytes = make_java_byte_array(env, keyLen);
(*env)->SetByteArrayRegion(env, keyBytes, 0, keyLen, (jbyte*)key);
}
else
{
// Java does not support zero-length byte arrays in the SecretKeySpec type,
// so instead create an empty 1-byte length byte array that's initalized to 0.
// the HMAC algorithm pads keys with zeros until the key is block-length,
// so this effectively creates the same key as if it were a zero byte-length key.
keyBytes = make_java_byte_array(env, 1);
}
jobject sksObj = (*env)->NewObject(env, g_sksClass, g_sksCtor, keyBytes, macName);
if (CheckJNIExceptions(env) || sksObj == NULL)
{
if(sksObj == NULL)
{
LOG_WARN ("Unable to create an instance of SecretKeySpec");
}
(*env)->DeleteLocalRef(env, keyBytes);
(*env)->DeleteLocalRef(env, sksObj);
(*env)->DeleteLocalRef(env, macName);
return FAIL;
}
jobject macObj = ToGRef(env, (*env)->CallStaticObjectMethod(env, g_MacClass, g_MacGetInstance, macName));
(*env)->CallVoidMethod(env, macObj, g_MacInit, sksObj);
(*env)->DeleteLocalRef(env, keyBytes);
(*env)->DeleteLocalRef(env, sksObj);
(*env)->DeleteLocalRef(env, macName);
return CheckJNIExceptions(env) ? FAIL : macObj;
}
int32_t CryptoNative_HmacReset(jobject ctx)
{
if (!ctx)
return FAIL;
JNIEnv* env = GetJNIEnv();
(*env)->CallVoidMethod(env, ctx, g_MacReset);
return CheckJNIExceptions(env) ? FAIL : SUCCESS;
}
int32_t CryptoNative_HmacUpdate(jobject ctx, uint8_t* data, int32_t len)
{
// Callers are expected to skip update calls with no data.
if (!ctx || !data || len <= 0)
return FAIL;
JNIEnv* env = GetJNIEnv();
jbyteArray dataBytes = make_java_byte_array(env, len);
(*env)->SetByteArrayRegion(env, dataBytes, 0, len, (jbyte*)data);
(*env)->CallVoidMethod(env, ctx, g_MacUpdate, dataBytes);
(*env)->DeleteLocalRef(env, dataBytes);
return CheckJNIExceptions(env) ? FAIL : SUCCESS;
}
ARGS_NON_NULL_ALL static int32_t DoFinal(JNIEnv* env, jobject mac, uint8_t* data, int32_t* len)
{
// mac.doFinal();
jbyteArray dataBytes = (jbyteArray)(*env)->CallObjectMethod(env, mac, g_MacDoFinal);
jsize dataBytesLen = (*env)->GetArrayLength(env, dataBytes);
*len = (int32_t)dataBytesLen;
(*env)->GetByteArrayRegion(env, dataBytes, 0, dataBytesLen, (jbyte*) data);
(*env)->DeleteLocalRef(env, dataBytes);
return CheckJNIExceptions(env) ? FAIL : SUCCESS;
}
int32_t CryptoNative_HmacFinal(jobject ctx, uint8_t* data, int32_t* len)
{
abort_if_invalid_pointer_argument (ctx);
JNIEnv* env = GetJNIEnv();
return DoFinal(env, ctx, data, len);
}
int32_t CryptoNative_HmacCurrent(jobject ctx, uint8_t* data, int32_t* len)
{
abort_if_invalid_pointer_argument (ctx);
JNIEnv* env = GetJNIEnv();
int32_t ret = FAIL;
// ctx.clone();
jobject clone = (*env)->CallObjectMethod(env, ctx, g_MacClone);
ON_EXCEPTION_PRINT_AND_GOTO(cleanup);
ret = DoFinal(env, clone, data, len);
cleanup:
(*env)->DeleteLocalRef(env, clone);
return ret;
}
void CryptoNative_HmacDestroy(jobject ctx)
{
ReleaseGRef(GetJNIEnv(), ctx);
}
int32_t CryptoNative_HmacOneShot(intptr_t type,
uint8_t* key,
int32_t keyLen,
uint8_t* source,
int32_t sourceLen,
uint8_t* md,
int32_t* mdSize)
{
jobject hmacCtx = CryptoNative_HmacCreate(key, keyLen, type);
if (hmacCtx == FAIL)
return FAIL;
int32_t ret = sourceLen == 0 ? SUCCESS : CryptoNative_HmacUpdate(hmacCtx, source, sourceLen);
if (ret != SUCCESS)
{
CryptoNative_HmacDestroy(hmacCtx);
return ret;
}
ret = CryptoNative_HmacFinal(hmacCtx, md, mdSize);
CryptoNative_HmacDestroy(hmacCtx);
return ret;
}