ReactOS 0.4.17-dev-1005-g171e1de
paddingPkcs7.c
Go to the documentation of this file.
1//
2// paddingPkcs7.c Add/Remove PKCS7 padding
3//
4// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
5//
6
7#include "precomp.h"
8
9
10VOID
13 SIZE_T cbBlockSize,
14 _In_reads_(cbSrc) PCBYTE pbSrc,
15 SIZE_T cbSrc,
16 _Out_writes_to_(cbDst, *pcbResult) PBYTE pbDst,
17 SIZE_T cbDst,
18 SIZE_T* pcbResult)
19{
20 SIZE_T cbPadVal; // PadVal is the number of bytes to pad.
21 SIZE_T cbDataLastBlock; // dwDataLastBlock is the number of bytes of data at the final block.
22 SIZE_T cbResult = 0; // This variable must always have a valid value when we finish the function.
23
24 SYMCRYPT_ASSERT(cbBlockSize < 256); // cbBlockSize must be < 256
25 SYMCRYPT_ASSERT((cbBlockSize & (cbBlockSize - 1)) == 0); // cbBlockSize must be a power of 2
26
27 //
28 // Compute the padding parameters.
29 //
30
31 cbDataLastBlock = (cbSrc & (cbBlockSize - 1));
32
33 cbResult = (cbSrc - cbDataLastBlock + cbBlockSize);
34
35 SYMCRYPT_ASSERT(cbDst >= cbResult); // cbDst >= cbSrc - cbSrc % cbBlockSize + cbBlockSize
36
37 if (cbResult > cbDst)
38 {
39 goto cleanup;
40 }
41
42 cbPadVal = (cbBlockSize - cbDataLastBlock);
43
44 //
45 // perform the padding
46 //
47
48 // cbSrc must be greater than zero. memcpy(pbDst, NULL, 0) is not defined!
49 if (pbDst != pbSrc && cbSrc > 0)
50 {
51 memcpy(pbDst, pbSrc, cbSrc);
52 }
53
54 memset(pbDst + cbSrc, (int)cbPadVal, cbPadVal);
55
57 *pcbResult = cbResult;
58}
59
60
64 SIZE_T cbBlockSize,
65 _In_reads_(cbSrc) PCBYTE pbSrc,
66 SIZE_T cbSrc,
67 _Out_writes_to_(cbDst, *pcbResult) PBYTE pbDst,
68 SIZE_T cbDst,
69 SIZE_T* pcbResult)
70{
71 SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
72
73 UINT32 mPaddingError = 0; // Indicates whether there is an error in padding or not.
74 UINT32 mBufferSizeError = 0; // Indicates whether pbDst is large enough to contain the entire message.
75 UINT32 mask = 0; // Mask for message bytes at the final block.
76 UINT32 cbPadVal; // PadVal is the number of padded bytes.
77 UINT32 cbSrc32;
78 UINT32 cbDst32;
79 UINT32 cbMsg32;
80
81 SIZE_T cbBulk = 0;
82 SIZE_T cbResult; // This variable must always have a valid value when we finish the function.
83
84
85 SYMCRYPT_ASSERT(cbBlockSize < 256); // cbBlockSize must be < 256
86 SYMCRYPT_ASSERT((cbBlockSize & (cbBlockSize - 1)) == 0); // cbBlockSize must be a power of 2
87 SYMCRYPT_ASSERT((cbSrc & (cbBlockSize - 1)) == 0); // cbSrc is a multiple of cbBlockSize
88 SYMCRYPT_ASSERT(cbSrc > 0); // cbSrc is greaten than zero
89
90 cbPadVal = (UINT32)pbSrc[cbSrc - 1];
91
92 // check the Padding to make sure it is valid.
93 mPaddingError |= SymCryptMask32IsZeroU31(cbPadVal) | SymCryptMask32LtU31((UINT32)cbBlockSize, cbPadVal);
94
95 // If cbPadVal is greater than cbSrc, SYMCRYPT_INVALID_ARGUMENT will be returned
96 // and cbResult will not be the right value.
97 cbResult = cbSrc - cbPadVal;
98
99 //
100 // Bulk processing
101 //
102
103 cbDst = SYMCRYPT_MIN(cbDst, cbSrc);
104
105 cbBulk = cbSrc - cbBlockSize;
106
107 // cbSrc, cbDst, and blockSize are not secrets.
108 // This condition can be checked in a non-side channel safe way.
109 if (cbDst < cbBulk)
110 {
111 scError = SYMCRYPT_BUFFER_TOO_SMALL;
112 goto cleanup;
113 }
114
115 if (pbDst != pbSrc)
116 {
117 memcpy(pbDst, pbSrc, cbBulk);
118 }
119
120 // Updating parameters
121 pbSrc += cbBulk; cbSrc -= cbBulk;
122 pbDst += cbBulk; cbDst -= cbBulk;
123
124 cbSrc32 = (UINT32)cbSrc;
125 cbDst32 = (UINT32)cbDst;
126
127 //
128 // Validating padding
129 //
130 // If cbPadVal is greater than cbBlockSize,
131 // we have to limit cbPadVal to be at most equal to cbBlockSize.
132 cbPadVal = 1 + ((cbPadVal - 1) & (cbBlockSize - 1));
133 cbMsg32 = (UINT32)(cbBlockSize - cbPadVal);
134
135 //check Dst buffer length to make sure it is possible copy the whole message (not including the padding).
136 mBufferSizeError |= SymCryptMask32LtU31(cbDst32, cbMsg32);
137
138 //
139 // Final Block processing
140 //
141
142 // Updating only the bytes of the message and leaving the other bytes in pbDst unchanged.
143 // Validating the value of the padded bytes.
144
145 for (UINT32 i = 0; i < cbBlockSize; ++i) // cbDst <= cbSrc == cbBlockSize
146 {
147 mask = SymCryptMask32LtU31(i, cbMsg32);
148
149 mPaddingError |= (SymCryptMask32IsNonzeroU31((UINT32)pbSrc[i] ^ cbPadVal) & ~mask);
150
151 if (i < cbDst)
152 {
153 pbDst[i] ^= (pbDst[i] ^ pbSrc[i]) & mask;
154 }
155 }
156
157cleanup:
158
159 *pcbResult = cbResult;
160
161 // Update scError with the two error masks.
162 // SYMCRYPT_INVALID_ARGUMENT gets precedence over SYMCRYPT_BUFFER_TOO_SMALL
163 scError ^= mBufferSizeError & (scError ^ SYMCRYPT_BUFFER_TOO_SMALL);
164 scError ^= mPaddingError & (scError ^ SYMCRYPT_INVALID_ARGUMENT);
165
166 return scError;
167}
static void cleanup(void)
Definition: main.c:1335
GLenum GLint GLuint mask
Definition: glext.h:6028
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define _In_reads_(s)
Definition: no_sal2.h:168
#define _Out_writes_to_(s, c)
Definition: no_sal2.h:188
VOID SYMCRYPT_CALL SymCryptPaddingPkcs7Add(SIZE_T cbBlockSize, _In_reads_(cbSrc) PCBYTE pbSrc, SIZE_T cbSrc, _Out_writes_to_(cbDst, *pcbResult) PBYTE pbDst, SIZE_T cbDst, SIZE_T *pcbResult)
Definition: paddingPkcs7.c:12
SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptPaddingPkcs7Remove(SIZE_T cbBlockSize, _In_reads_(cbSrc) PCBYTE pbSrc, SIZE_T cbSrc, _Out_writes_to_(cbDst, *pcbResult) PBYTE pbDst, SIZE_T cbDst, SIZE_T *pcbResult)
Definition: paddingPkcs7.c:63
BYTE * PBYTE
Definition: pedump.c:66
#define memset(x, y, z)
Definition: compat.h:39
#define SYMCRYPT_ASSERT(_x)
Definition: symcrypt.h:10807
SYMCRYPT_ERROR
Definition: symcrypt.h:227
PCBYTE pbSrc
#define SYMCRYPT_CALL
#define SYMCRYPT_MIN(_a, _b)
PCBYTE PBYTE pbDst
const BYTE * PCBYTE
UINT32 SYMCRYPT_CALL SymCryptMask32IsNonzeroU31(UINT32 v)
Definition: scsTools.c:28
UINT32 SYMCRYPT_CALL SymCryptMask32IsZeroU31(UINT32 v)
Definition: scsTools.c:36
UINT32 SYMCRYPT_CALL SymCryptMask32LtU31(UINT32 a, UINT32 b)
Definition: scsTools.c:53
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t UINT32
Definition: typedefs.h:59